use of com.badlogic.gdx.scenes.scene2d.ui.Tree.TreeStyle in project skin-composer by raeleus.
the class RootTable method refreshPreview.
public void refreshPreview() {
if (previewTable != null) {
previewTable.clear();
previewTable.setBackground("white");
previewTable.setColor((Color) previewProperties.get("bgcolor"));
for (BitmapFont font : previewFonts) {
font.dispose();
}
if (classSelectBox.getSelectedIndex() >= 0 && classSelectBox.getSelectedIndex() < Main.BASIC_CLASSES.length) {
StyleData styleData = getSelectedStyle();
Class clazz = Main.BASIC_CLASSES[classSelectBox.getSelectedIndex()];
if (!styleData.hasMandatoryFields()) {
Label label;
if (clazz.equals(SelectBox.class)) {
label = new Label("Please fill all mandatory fields\n(Highlighted on the left)\n\nscrollStyle and listStyle\nmust already be defined", getSkin());
} else if (clazz.equals(TextTooltip.class)) {
label = new Label("Please fill all mandatory fields\n(Highlighted on the left)\n\nlabel must already be defined", getSkin());
} else {
label = new Label("Please fill all mandatory fields\n(Highlighted on the left)", getSkin());
}
label.setAlignment(Align.center);
previewTable.add(label);
} else if (styleData.hasAllNullFields()) {
Label label;
label = new Label("All fields are empty!\nEmpty classes are not exported\nAdd style properties in the menu to the left", getSkin());
label.setAlignment(Align.center);
previewTable.add(label);
} else {
Actor widget = null;
var style = createPreviewStyle(Main.basicToStyleClass(clazz), styleData);
if (style != null) {
if (clazz.equals(Button.class)) {
widget = new Button((ButtonStyle) style);
((Button) widget).setDisabled((boolean) previewProperties.get("disabled"));
widget.addListener(handListener);
} else if (clazz.equals(CheckBox.class)) {
widget = new CheckBox("", (CheckBoxStyle) style);
((CheckBox) widget).setDisabled((boolean) previewProperties.get("disabled"));
((CheckBox) widget).setText((String) previewProperties.get("text"));
widget.addListener(handListener);
} else if (clazz.equals(ImageButton.class)) {
widget = new ImageButton((ImageButtonStyle) style);
((ImageButton) widget).setDisabled((boolean) previewProperties.get("disabled"));
widget.addListener(handListener);
} else if (clazz.equals(ImageTextButton.class)) {
widget = new ImageTextButton("", (ImageTextButtonStyle) style);
((ImageTextButton) widget).setDisabled((boolean) previewProperties.get("disabled"));
((ImageTextButton) widget).setText((String) previewProperties.get("text"));
widget.addListener(handListener);
} else if (clazz.equals(Label.class)) {
widget = new Label("", (LabelStyle) style);
((Label) widget).setText((String) previewProperties.get("text"));
} else if (clazz.equals(List.class)) {
widget = new List((ListStyle) style);
Array<String> items = new Array<>(((String) previewProperties.get("text")).split("\\n"));
((List) widget).setItems(items);
widget.addListener(handListener);
} else if (clazz.equals(ProgressBar.class)) {
widget = new ProgressBar((float) (double) previewProperties.get("minimum"), (float) (double) previewProperties.get("maximum"), (float) (double) previewProperties.get("increment"), (boolean) previewProperties.get("orientation"), (ProgressBarStyle) style);
((ProgressBar) widget).setValue((float) (double) previewProperties.get("value"));
((ProgressBar) widget).setDisabled((boolean) previewProperties.get("disabled"));
} else if (clazz.equals(ScrollPane.class)) {
Label label = new Label("", getSkin());
widget = new ScrollPane(label, (ScrollPaneStyle) style);
((ScrollPane) widget).setScrollbarsOnTop((boolean) previewProperties.get("scrollbarsOnTop"));
((ScrollPane) widget).setScrollBarPositions((boolean) previewProperties.get("hScrollBarPosition"), (boolean) previewProperties.get("vScrollBarPosition"));
((ScrollPane) widget).setScrollingDisabled((boolean) previewProperties.get("hScrollDisabled"), (boolean) previewProperties.get("vScrollDisabled"));
((ScrollPane) widget).setForceScroll((boolean) previewProperties.get("forceHscroll"), (boolean) previewProperties.get("forceVscroll"));
((ScrollPane) widget).setVariableSizeKnobs((boolean) previewProperties.get("variableSizeKnobs"));
((ScrollPane) widget).setOverscroll((boolean) previewProperties.get("hOverscroll"), (boolean) previewProperties.get("vOverscroll"));
((ScrollPane) widget).setFadeScrollBars((boolean) previewProperties.get("fadeScroll"));
((ScrollPane) widget).setSmoothScrolling((boolean) previewProperties.get("smoothScroll"));
((ScrollPane) widget).setFlickScroll((boolean) previewProperties.get("flickScroll"));
((ScrollPane) widget).setClamp((boolean) previewProperties.get("clamp"));
label.setText((String) previewProperties.get("text"));
} else if (clazz.equals(SelectBox.class)) {
widget = new SelectBox((SelectBoxStyle) style);
((SelectBox) widget).setDisabled((boolean) previewProperties.get("disabled"));
Array<String> items = new Array<>(((String) previewProperties.get("text")).split("\\n"));
((SelectBox) widget).setItems(items);
((SelectBox) widget).setMaxListCount((int) previewProperties.get("max-list-count"));
((SelectBox) widget).setAlignment((int) previewProperties.get("alignment"));
widget.addListener(handListener);
((SelectBox) widget).getList().addListener(handListener);
} else if (clazz.equals(Slider.class)) {
widget = new Slider((float) (double) previewProperties.get("minimum"), (float) (double) previewProperties.get("maximum"), (float) (double) previewProperties.get("increment"), (boolean) previewProperties.get("orientation"), (SliderStyle) style);
((Slider) widget).setDisabled((boolean) previewProperties.get("disabled"));
widget.addListener(handListener);
} else if (clazz.equals(SplitPane.class)) {
var table1 = new Table();
Label label1 = new Label("", getSkin());
table1.add(label1).minSize(0);
var table2 = new Table();
Label label2 = new Label("", getSkin());
table2.add(label2).minSize(0);
widget = new SplitPane(table1, table2, (boolean) previewProperties.get("orientation"), (SplitPaneStyle) style);
((SplitPane) widget).setMinSplitAmount(0);
((SplitPane) widget).setMaxSplitAmount(1);
label1.setText((String) previewProperties.get("text"));
label2.setText((String) previewProperties.get("text"));
if ((boolean) previewProperties.get("orientation")) {
widget.addListener(verticalResizeArrowListener);
} else {
widget.addListener(horizontalResizeArrowListener);
}
} else if (clazz.equals(TextButton.class)) {
widget = new TextButton("", (TextButtonStyle) style);
((TextButton) widget).setDisabled((boolean) previewProperties.get("disabled"));
((TextButton) widget).setText((String) previewProperties.get("text"));
widget.addListener(handListener);
} else if (clazz.equals(TextField.class)) {
widget = new TextField("", (TextFieldStyle) style);
((TextField) widget).setFocusTraversal(false);
((TextField) widget).setDisabled((boolean) previewProperties.get("disabled"));
((TextField) widget).setPasswordMode((boolean) previewProperties.get("passwordMode"));
((TextField) widget).setAlignment((int) previewProperties.get("alignment"));
((TextField) widget).setText((String) previewProperties.get("text"));
((TextField) widget).setMessageText((String) previewProperties.get("message"));
String string = (String) previewProperties.get("password");
if (string.length() > 0) {
((TextField) widget).setPasswordCharacter(string.charAt(0));
}
widget.addListener(ibeamListener);
} else if (clazz.equals(TextTooltip.class)) {
TooltipManager manager = new TooltipManager();
manager.animations = false;
manager.initialTime = 0.0f;
manager.resetTime = 0.0f;
manager.subsequentTime = 0.0f;
manager.hideAll();
manager.instant();
TextTooltip toolTip = new TextTooltip((String) previewProperties.get("text"), manager, (TextTooltipStyle) style);
widget = new Label("Hover over me", getSkin());
widget.addListener(toolTip);
} else if (clazz.equals(Touchpad.class)) {
widget = new Touchpad(0, (TouchpadStyle) style);
widget.addListener(handListener);
} else if (clazz.equals(Tree.class)) {
widget = new Tree((TreeStyle) style);
((Tree) widget).setIconSpacing((int) previewProperties.get("icon-spacing-left"), (int) previewProperties.get("icon-spacing-right"));
((Tree) widget).setIndentSpacing((int) previewProperties.get("indent-spacing"));
((Tree) widget).setYSpacing((int) previewProperties.get("y-spacing"));
String[] lines = { "this", "is", "a", "test" };
Tree.Node parentNode = null;
for (String line : lines) {
Label label = new Label(line, getSkin());
Tree.Node node = new Tree.Node(label) {
};
if (parentNode == null) {
((Tree) widget).add(node);
} else {
parentNode.add(node);
}
parentNode = node;
}
widget.addListener(handListener);
} else if (clazz.equals(Window.class)) {
WindowStyle windowStyle = (WindowStyle) style;
if (windowStyle.stageBackground != null) {
previewTable.setBackground(windowStyle.stageBackground);
previewTable.setColor(Color.WHITE);
windowStyle.stageBackground = null;
}
Label sampleText = new Label("", getSkin());
sampleText.setText((String) previewProperties.get("text"));
widget = new Window((String) previewProperties.get("title"), windowStyle);
((Window) widget).add(sampleText);
}
}
if (widget != null) {
switch((int) previewProperties.get("size")) {
case (0):
previewTable.add(widget).size(10.0f);
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (1):
previewTable.add(widget);
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (2):
previewTable.add(widget).size(200.0f);
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (3):
previewTable.add(widget).growX();
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (4):
previewTable.add(widget).growY();
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (5):
previewTable.add(widget).grow();
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (6):
previewResizeWidget.setActor(widget);
previewResizeWidget.setTouchable(Touchable.enabled);
previewTable.add(previewResizeWidget).grow();
previewSizeSelectBox.setItems(DEFAULT_SIZES);
break;
case (7):
Actor addWidget = widget;
TraversalTextField widthField = new TraversalTextField("", getSkin());
TraversalTextField heightField = new TraversalTextField("", getSkin());
widthField.setNextFocus(heightField);
heightField.setNextFocus(widthField);
Dialog dialog = new Dialog("Enter dimensions...", getSkin()) {
@Override
protected void result(Object object) {
if ((boolean) object) {
previewTable.add(addWidget).size(Integer.parseInt(widthField.getText()), Integer.parseInt(heightField.getText()));
Array<String> items = new Array<>(DEFAULT_SIZES);
items.add(widthField.getText() + "x" + heightField.getText());
previewProperties.put("sizeX", Integer.parseInt(widthField.getText()));
previewProperties.put("sizeY", Integer.parseInt(heightField.getText()));
previewSizeSelectBox.setItems(items);
previewSizeSelectBox.setSelectedIndex(8);
} else {
previewSizeSelectBox.setSelectedIndex(1);
}
}
};
dialog.getTitleTable().getCells().first().padLeft(5.0f);
dialog.text("Enter the preview dimensions: ");
dialog.getContentTable().getCells().first().pad(10.0f);
dialog.getContentTable().row();
Table sizeTable = new Table();
sizeTable.add(widthField).padLeft(10.0f);
sizeTable.add(new Label(" x ", getSkin()));
sizeTable.add(heightField).padRight(10.0f);
dialog.getContentTable().add(sizeTable);
dialog.getButtonTable().defaults().padBottom(10.0f).minWidth(50.0f);
dialog.button("OK", true);
dialog.button("Cancel", false);
dialog.key(Keys.ENTER, true).key(Keys.NUMPAD_ENTER, true).key(Keys.ESCAPE, false);
TextButton okButton = (TextButton) dialog.getButtonTable().getCells().first().getActor();
okButton.setDisabled(true);
okButton.addListener(handListener);
widthField.addListener(ibeamListener);
widthField.addListener(new ChangeListener() {
@Override
public void changed(ChangeListener.ChangeEvent event, Actor actor) {
okButton.setDisabled(!widthField.getText().matches("^\\d+$") || !heightField.getText().matches("^\\d+$"));
}
});
heightField.addListener(ibeamListener);
heightField.addListener(new ChangeListener() {
@Override
public void changed(ChangeListener.ChangeEvent event, Actor actor) {
okButton.setDisabled(!widthField.getText().matches("^\\d+$") || !heightField.getText().matches("^\\d+$"));
}
});
dialog.getButtonTable().getCells().get(1).getActor().addListener(handListener);
dialog.key(Input.Keys.ESCAPE, false);
dialog.show(stage);
stage.setKeyboardFocus(widthField);
break;
case (8):
previewTable.add(widget).size((int) previewProperties.get("sizeX"), (int) previewProperties.get("sizeY"));
break;
}
}
}
} else {
var customClass = (CustomClass) classSelectBox.getSelected();
CustomStyle customStyle = customClass.getStyles().get(styleBox.getSelectedIndex());
boolean showMessage = true;
if (customStyle.getProperties().size == 0) {
Label label = new Label("No style properties!\nEmpty classes are not exported\nAdd style properties in the menu to the left", getSkin());
label.setAlignment(0);
previewTable.add(label);
} else {
for (CustomProperty customProperty : customStyle.getProperties()) {
if (customProperty.getValue() != null && !(customProperty.getValue() instanceof String) || customProperty.getValue() != null && !((String) customProperty.getValue()).equals("")) {
showMessage = false;
break;
}
}
if (showMessage) {
Label label = new Label("All properties are empty!\nEmpty classes are not exported\nAdd style properties in the menu to the left", getSkin());
label.setAlignment(0);
previewTable.add(label);
}
}
if (!showMessage) {
HorizontalGroup horizontalGroup = new HorizontalGroup();
horizontalGroup.wrap();
horizontalGroup.wrapSpace(10.0f);
previewTable.add(horizontalGroup).grow().pad(10.0f);
for (CustomProperty customProperty : customStyle.getProperties()) {
if (customProperty.getValue() != null) {
Container container = new Container();
container.pad(5.0f);
horizontalGroup.addActor(container);
switch(customProperty.getType()) {
case TEXT:
case RAW_TEXT:
case STYLE:
if (!(customProperty.getValue() instanceof String)) {
customProperty.setValue("");
}
Label labelText = new Label((String) customProperty.getValue(), getSkin());
container.setActor(labelText);
break;
case NUMBER:
if (!(customProperty.getValue() instanceof Double)) {
customProperty.setValue(0.0);
}
Label labelNumber = new Label(Double.toString((double) customProperty.getValue()), getSkin());
container.setActor(labelNumber);
break;
case BOOL:
if (!(customProperty.getValue() instanceof Boolean)) {
customProperty.setValue(false);
}
Label labelBoolean = new Label(Boolean.toString((boolean) customProperty.getValue()), getSkin());
container.setActor(labelBoolean);
break;
case COLOR:
if (!(customProperty.getValue() instanceof String)) {
customProperty.setValue("");
}
ColorData colorData = null;
String colorName = (String) customProperty.getValue();
for (ColorData cd : jsonData.getColors()) {
if (cd.getName().equals(colorName)) {
colorData = cd;
break;
}
}
if (colorData != null) {
Table colorTable = new Table(getSkin());
colorTable.setBackground("white");
colorTable.setColor(colorData.color);
colorTable.add().size(25.0f);
colorTable.setTouchable(Touchable.enabled);
container.setActor(colorTable);
container.addListener(new TextTooltip(colorName, tooltipManager, getSkin()));
}
break;
case FONT:
if (!(customProperty.getValue() instanceof String)) {
customProperty.setValue("");
}
BitmapFont font = null;
FontData fontData = null;
String fontName = (String) customProperty.getValue();
for (FontData fd : jsonData.getFonts()) {
if (fd.getName().equals(fontName)) {
fontData = fd;
font = new BitmapFont(fd.file);
previewFonts.add(font);
break;
}
}
if (font != null) {
Label labelFont = new Label(fontData.getName(), new LabelStyle(font, Color.WHITE));
container.setActor(labelFont);
container.addListener(new TextTooltip(fontData.getName(), tooltipManager, getSkin()));
}
FreeTypeFontData freeTypeFontData = null;
for (FreeTypeFontData fd : jsonData.getFreeTypeFonts()) {
if (fd.name.equals(fontName)) {
freeTypeFontData = fd;
break;
}
}
if (freeTypeFontData != null && freeTypeFontData.bitmapFont != null) {
Label labelFont = new Label(freeTypeFontData.name, new LabelStyle(freeTypeFontData.bitmapFont, Color.WHITE));
container.setActor(labelFont);
container.addListener(new TextTooltip(freeTypeFontData.name, tooltipManager, getSkin()));
}
break;
case DRAWABLE:
if (!(customProperty.getValue() instanceof String)) {
customProperty.setValue("");
}
DrawableData drawable = null;
String drawableName = (String) customProperty.getValue();
for (DrawableData dd : atlasData.getDrawables()) {
if (dd.name.equals(drawableName)) {
drawable = dd;
break;
}
}
if (drawable != null) {
Image image = new Image(atlasData.getDrawablePairs().get(drawable));
container.setActor(image);
container.addListener(new TextTooltip(drawable.name, tooltipManager, getSkin()));
}
break;
}
}
}
}
}
}
}
Aggregations