use of com.ray3k.tenpatch.TenPatchDrawable in project skin-composer by raeleus.
the class DialogTenPatch method populate.
private void populate() {
setSize(stage.getWidth(), stage.getHeight());
var root = getContentTable();
root.pad(10);
root.clear();
var top = new Table();
top.setTouchable(Touchable.enabled);
var bottom = new Table();
bottom.setBackground(skin.getDrawable("white"));
bottom.setTouchable(Touchable.enabled);
var splitPane = new SplitPane(top, bottom, true, skin);
splitPane.setSplitAmount(splitValue);
root.add(splitPane).grow();
splitPane.addListener(verticalResizeArrowListener);
splitPane.addListener(new DragListener() {
{
setTapSquareSize(0f);
}
@Override
public void drag(InputEvent event, float x, float y, int pointer) {
super.drag(event, x, y, pointer);
if (event.getListenerActor().equals(event.getTarget())) {
tenPatchWidget.center();
}
splitValue = splitPane.getSplitAmount();
}
});
var label = new Label("Ten Patch Editor", skin, "title-no-line");
top.add(label);
top.row();
var table = new Table();
top.add(table).growX().space(5);
table.defaults().space(5);
label = new Label("Name:", skin, "white");
label.setName("nameLabel");
table.add(label);
var textField = new TextField(drawableData.name, skin);
textField.setName("nameField");
table.add(textField).growX();
textField.addListener(ibeamListener);
textField.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
drawableData.name = textField.getText();
validateName();
}
});
table.defaults().uniform().fill();
var textButton = new TextButton("Save to File", skin);
table.add(textButton);
textButton.addListener(handListener);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Runnable runnable = () -> {
String[] filterPatterns = null;
if (!Utils.isMac()) {
filterPatterns = new String[] { "*.png" };
}
var file = desktopWorker.saveDialog("Save as 9patch...", projectData.getLastDrawablePath(), filterPatterns, "Image files");
if (file != null) {
Gdx.app.postRunnable(() -> {
var fileHandle = new FileHandle(file);
if (!fileHandle.name().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
fileHandle = fileHandle.sibling(fileHandle.nameWithoutExtension() + ".9.png");
}
saveToImageFile(fileHandle);
});
}
};
dialogFactory.showDialogLoading(runnable);
}
});
textButton = new TextButton("Clear", skin);
table.add(textButton);
textButton.addListener(handListener);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
drawableData.tenPatchData.clear();
}
});
textButton = new TextButton("More info...", skin);
table.add(textButton);
textButton.addListener(handListener);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
showAboutDialog();
}
});
top.row();
tenPatchWidget = new TenPatchWidget(skin);
tenPatchWidget.setTenPatchData(drawableData.tenPatchData);
tenPatchWidget.setTextureRegion(loadTextureFile(drawableData.file));
top.add(tenPatchWidget).grow();
tenPatchWidget.getStretchSwitchButton().addListener(handListener);
tenPatchWidget.getModeSwitchButton().addListener(handListener);
tenPatchWidget.addListener(new InputListener() {
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
getStage().setScrollFocus(tenPatchWidget);
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
zoomToMouse = false;
if (pointer == -1) {
if (getStage() != null) {
getStage().setScrollFocus(null);
}
}
}
@Override
public boolean scrolled(InputEvent event, float x, float y, float amountX, float amountY) {
zoomToMouse = true;
Slider slider = findActor("ten-patch-zoom");
slider.setValue(slider.getValue() - SCROLL_AMOUNT * amountY);
return true;
}
});
tenPatchWidget.addListener(new TenPatchWidget.TenPatchListener() {
@Override
public void valueChanged(TenPatchData tenPatchData) {
updatePreview();
}
});
top.row();
table = new Table();
table.pad(5);
top.add(table).growX();
table.defaults().space(5);
var imageButton = new ImageButton(skin, "grid-light");
imageButton.setName("grid-light");
imageButton.setProgrammaticChangeEvents(false);
table.add(imageButton);
imageButton.addListener(handListener);
imageButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
var imageButton = (ImageButton) actor;
if (imageButton.isChecked()) {
tenPatchWidget.setGridMode(TenPatchWidget.GridMode.LIGHT);
} else {
tenPatchWidget.setGridMode(TenPatchWidget.GridMode.NONE);
}
imageButton = findActor("grid-dark");
imageButton.setChecked(false);
}
});
imageButton = new ImageButton(skin, "grid-dark");
imageButton.setName("grid-dark");
imageButton.setProgrammaticChangeEvents(false);
table.add(imageButton);
imageButton.addListener(handListener);
imageButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
var imageButton = (ImageButton) actor;
if (imageButton.isChecked()) {
tenPatchWidget.setGridMode(TenPatchWidget.GridMode.DARK);
} else {
tenPatchWidget.setGridMode(TenPatchWidget.GridMode.NONE);
}
imageButton = findActor("grid-light");
imageButton.setChecked(false);
}
});
imageButton = new ImageButton(skin, "resize");
table.add(imageButton).expandX().right();
imageButton.addListener(handListener);
imageButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeListener.ChangeEvent event, Actor actor) {
tenPatchWidget.zoomAndCenter();
Slider slider = findActor("ten-patch-zoom");
slider.setValue(tenPatchWidget.getZoomScale());
}
});
var slider = new Slider(1, 100, 1f, false, skin, "zoom-horizontal");
slider.setName("ten-patch-zoom");
slider.setValue(1);
table.add(slider);
slider.addListener(handListener);
slider.addListener(new ChangeListener() {
@Override
public void changed(ChangeListener.ChangeEvent event, Actor actor) {
var slider = (Slider) actor;
if (!zoomToMouse) {
tenPatchWidget.setZoomScale(slider.getValue());
} else {
temp.set(Gdx.input.getX(), Gdx.input.getY());
tenPatchWidget.screenToLocalCoordinates(temp);
tenPatchWidget.setZoomScale(temp.x, temp.y, slider.getValue());
}
}
});
top.row();
table = new Table();
top.add(table).growX();
table.defaults().space(3);
table.pad(5);
var imageTextButton = new ImageTextButton("Animation...", skin, "ten-patch-animation");
table.add(imageTextButton);
imageTextButton.addListener(handListener);
imageTextButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
var dialog = new DialogTenPatchAnimation(drawableData);
dialog.addListener(new DialogTenPatchAnimation.DialogTenPatchAnimationListener() {
@Override
public void animationUpdated(DialogTenPatchAnimation.DialogTenPatchAnimationEvent event) {
updatePreview();
}
});
dialog.show(getStage());
}
});
textButton = new TextButton("Load Patches", skin);
table.add(textButton);
textButton.addListener(handListener);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
Runnable runnable = () -> {
String[] filterPatterns = null;
if (!Utils.isMac()) {
filterPatterns = new String[] { "*.9.png" };
}
var file = desktopWorker.openDialog("Load patches from file...", projectData.getLastDrawablePath(), filterPatterns, "Nine patch files");
if (file != null) {
Gdx.app.postRunnable(() -> {
var fileHandle = new FileHandle(file);
if (fileHandle.name().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
loadPatchesFromFile(fileHandle);
}
});
}
};
dialogFactory.showDialogLoading(runnable);
}
});
textButton = new TextButton("Auto Patches", skin);
table.add(textButton);
textButton.addListener(handListener);
textButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
var pixmap = new Pixmap(fileHandle);
if (fileHandle.path().toLowerCase(Locale.ROOT).endsWith(".9.png")) {
var cropped = new Pixmap(pixmap.getWidth() - 2, pixmap.getHeight() - 2, Pixmap.Format.RGBA8888);
cropped.setBlending(Pixmap.Blending.None);
cropped.drawPixmap(pixmap, 0, 0, 1, 1, pixmap.getWidth() - 2, pixmap.getHeight() - 2);
pixmap.dispose();
pixmap = cropped;
}
var patches = Utils.calculatePatches(pixmap);
drawableData.tenPatchData.horizontalStretchAreas.clear();
drawableData.tenPatchData.horizontalStretchAreas.add(patches.left);
drawableData.tenPatchData.horizontalStretchAreas.add(pixmap.getWidth() - patches.right - 1);
drawableData.tenPatchData.verticalStretchAreas.clear();
drawableData.tenPatchData.verticalStretchAreas.add(patches.bottom);
drawableData.tenPatchData.verticalStretchAreas.add(pixmap.getHeight() - patches.top - 1);
drawableData.tenPatchData.combineContiguousSretchAreas(true);
drawableData.tenPatchData.removeInvalidStretchAreas(true);
drawableData.tenPatchData.combineContiguousSretchAreas(false);
drawableData.tenPatchData.removeInvalidStretchAreas(false);
updatePreview();
pixmap.dispose();
}
});
imageTextButton = new ImageTextButton("More settings...", skin, "ten-patch-scrolling");
table.add(imageTextButton);
imageTextButton.addListener(handListener);
imageTextButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
var dialog = new DialogTenPatchSettings(drawableData, tenPatchDrawable);
dialog.addListener(new DialogTenPatchSettings.DialogTenPatchSettingsListener() {
@Override
public void settingsUpdated(DialogTenPatchSettings.DialogTenPatchSettingsEvent event) {
updatePreview();
}
});
dialog.show(getStage());
}
});
label = new Label("Preview", skin, "title-no-line");
bottom.add(label);
bottom.row();
table = new Table();
bottom.add(table).grow();
var resizer = new ResizeWidget(null, skin);
resizer.setName("resizer");
resizer.setTouchable(Touchable.enabled);
resizer.setResizingFromCenter(true);
resizer.setAllowDragging(false);
var cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_ne"), 16, 16);
var resizeFourArrowListener = new ResizeFourArrowListener(cursor);
resizer.getBottomLeftHandle().addListener(resizeFourArrowListener);
resizer.getTopRightHandle().addListener(resizeFourArrowListener);
cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_nw"), 16, 16);
resizeFourArrowListener = new ResizeFourArrowListener(cursor);
resizer.getTopLeftHandle().addListener(resizeFourArrowListener);
resizer.getBottomRightHandle().addListener(resizeFourArrowListener);
cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_vertical"), 16, 16);
resizeFourArrowListener = new ResizeFourArrowListener(cursor);
resizer.getBottomHandle().addListener(resizeFourArrowListener);
resizer.getTopHandle().addListener(resizeFourArrowListener);
cursor = Utils.textureRegionToCursor(skin.getRegion("cursor_resize_horizontal"), 16, 16);
resizeFourArrowListener = new ResizeFourArrowListener(cursor);
resizer.getLeftHandle().addListener(resizeFourArrowListener);
resizer.getRightHandle().addListener(resizeFourArrowListener);
table.add(resizer).grow();
String name = drawableData.file.nameWithoutExtension();
var matcher = Pattern.compile(".*(?=\\.9$)").matcher(name);
if (matcher.find()) {
name = matcher.group();
}
originalRegion = atlasData.getAtlas().findRegion(name);
tenPatchDrawable = new TenPatchDrawable(new int[0], new int[0], false, originalRegion);
if (drawableData.tenPatchData.colorName != null) {
tenPatchDrawable.getColor().set(jsonData.getColorByName(drawableData.tenPatchData.colorName).color);
}
tenPatchDrawable.setTiling(drawableData.tenPatchData.tile);
table = new Table();
table.setName("tenPatchTable");
table.setBackground(tenPatchDrawable);
resizer.setActor(table);
layout();
resizer.getStack().setSize(100, 100);
bottom.row();
table = new Table();
table.setBackground(skin.getDrawable("white"));
table.pad(5);
bottom.add(table).growX();
table.defaults().space(5);
label = new Label("Content:", skin);
table.add(label).right();
var selectBox = new SelectBox<String>(skin);
selectBox.setName("contentSelectBox");
selectBox.setItems("None", "Text", "Color", "Drawable");
table.add(selectBox);
selectBox.addListener(handListener);
selectBox.getList().addListener(handListener);
selectBox.getList().addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
selectBox.fire(new ChangeListener.ChangeEvent());
}
});
selectBox.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
createPreview();
}
});
imageButton = new ImageButton(skin, "color");
table.add(imageButton);
imageButton.addListener(handListener);
imageButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
dialogFactory.showDialogColors(new StyleProperty(), (colorData, pressedCancel) -> {
if (colorData == null) {
previewColor.set(DEFAULT_PREVIEW_COLOR);
} else {
previewColor.set(colorData.color);
}
updatePreview();
}, null);
}
});
label = new Label("Background:", skin);
table.add(label).padLeft(15);
imageButton = new ImageButton(skin, "color");
table.add(imageButton);
imageButton.addListener(handListener);
imageButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
dialogFactory.showDialogColors(new StyleProperty(), (colorData, pressedCancel) -> {
if (colorData == null) {
bottom.setColor(Color.WHITE);
} else {
bottom.setColor(colorData.color);
}
updatePreview();
}, null);
}
});
table.add().expandX();
root = getButtonTable();
root.pad(10);
root.clearChildren();
root.defaults().uniform().fill();
textButton = new TextButton("OK", skin);
textButton.setName("okayButton");
button(textButton, true);
validateName();
textButton.addListener(handListener);
textButton = new TextButton("Cancel", skin);
button(textButton, false);
textButton.addListener(handListener);
updatePreview();
layout();
tenPatchWidget.zoomAndCenter();
}
use of com.ray3k.tenpatch.TenPatchDrawable in project skin-composer by raeleus.
the class AtlasData method produceAtlas.
/**
* Writes a TextureAtlas based on drawables list. Creates drawables to be
* displayed on screen
* @return
*/
public boolean produceAtlas() {
try {
drawablePairs.clear();
if (!atlasData.atlasCurrent) {
if (atlas != null) {
atlas.dispose();
atlas = null;
}
FileHandle defaultsFile = Main.appFolder.child("texturepacker/atlas-internal-settings.json");
atlasData.writeAtlas(defaultsFile);
atlasData.atlasCurrent = true;
// clear all regions in any tenPatchData
for (var data : atlasData.getDrawables()) {
if (data.tenPatchData != null) {
data.tenPatchData.regions = null;
}
}
}
atlas = atlasData.getAtlas();
var combined = new Array<>(getDrawables());
combined.addAll(getFontDrawables());
for (DrawableData data : combined) {
Drawable drawable;
if (data.type == DrawableType.CUSTOM) {
drawable = skin.getDrawable("custom");
} else if (data.type == DrawableType.PIXEL) {
drawable = ((TextureRegionDrawable) skin.getDrawable("white")).tint(jsonData.getColorByName(data.tintName).color);
drawable.setMinWidth(data.minWidth);
drawable.setMinHeight(data.minHeight);
} else if (data.type == DrawableType.TENPATCH) {
var region = atlas.findRegion(DrawableData.proper(data.file.name()));
drawable = new TenPatchDrawable(data.tenPatchData.horizontalStretchAreas.toArray(), data.tenPatchData.verticalStretchAreas.toArray(), data.tenPatchData.tile, region);
if (((TenPatchDrawable) drawable).horizontalStretchAreas.length == 0) {
((TenPatchDrawable) drawable).horizontalStretchAreas = new int[] { 0, region.getRegionWidth() - 1 };
}
if (((TenPatchDrawable) drawable).verticalStretchAreas.length == 0) {
((TenPatchDrawable) drawable).verticalStretchAreas = new int[] { 0, region.getRegionHeight() - 1 };
}
drawable.setLeftWidth(data.tenPatchData.contentLeft);
drawable.setRightWidth(data.tenPatchData.contentRight);
drawable.setTopHeight(data.tenPatchData.contentTop);
drawable.setBottomHeight(data.tenPatchData.contentBottom);
if (!MathUtils.isEqual(data.minWidth, -1))
drawable.setMinWidth(data.minWidth);
if (!MathUtils.isEqual(data.minHeight, -1))
drawable.setMinHeight(data.minHeight);
if (data.tenPatchData.colorName != null)
((TenPatchDrawable) drawable).setColor(jsonData.getColorByName(data.tenPatchData.colorName).color);
if (data.tenPatchData.color1Name != null)
((TenPatchDrawable) drawable).setColor1(jsonData.getColorByName(data.tenPatchData.color1Name).color);
if (data.tenPatchData.color2Name != null)
((TenPatchDrawable) drawable).setColor2(jsonData.getColorByName(data.tenPatchData.color2Name).color);
if (data.tenPatchData.color3Name != null)
((TenPatchDrawable) drawable).setColor3(jsonData.getColorByName(data.tenPatchData.color3Name).color);
if (data.tenPatchData.color4Name != null)
((TenPatchDrawable) drawable).setColor4(jsonData.getColorByName(data.tenPatchData.color4Name).color);
((TenPatchDrawable) drawable).setOffsetX(data.tenPatchData.offsetX);
((TenPatchDrawable) drawable).setOffsetY(data.tenPatchData.offsetY);
((TenPatchDrawable) drawable).setOffsetXspeed(data.tenPatchData.offsetXspeed);
((TenPatchDrawable) drawable).setOffsetYspeed(data.tenPatchData.offsetYspeed);
((TenPatchDrawable) drawable).setFrameDuration(data.tenPatchData.frameDuration);
((TenPatchDrawable) drawable).setPlayMode(data.tenPatchData.playMode);
((TenPatchDrawable) drawable).crushMode = data.tenPatchData.crushMode;
if (data.tenPatchData.regions == null) {
data.tenPatchData.regions = new Array<>();
for (var name : data.tenPatchData.regionNames) {
data.tenPatchData.regions.add(atlas.findRegion(name));
}
}
((TenPatchDrawable) drawable).setRegions(data.tenPatchData.regions);
} else if (data.type == DrawableType.TILED) {
String name = data.file.name();
name = DrawableData.proper(name);
drawable = new TiledDrawable(atlas.findRegion(name));
drawable.setMinWidth(data.minWidth);
drawable.setMinHeight(data.minHeight);
((TiledDrawable) drawable).getColor().set(jsonData.getColorByName(data.tintName).color);
} else if (data.type == DrawableType.NINE_PATCH) {
String name = data.file.name();
name = DrawableData.proper(name);
drawable = new NinePatchDrawable(atlas.createPatch(name));
if (data.tint != null) {
drawable = ((NinePatchDrawable) drawable).tint(data.tint);
} else if (data.tintName != null) {
drawable = ((NinePatchDrawable) drawable).tint(jsonData.getColorByName(data.tintName).color);
}
if (!MathUtils.isEqual(data.minWidth, -1))
drawable.setMinWidth(data.minWidth);
if (!MathUtils.isEqual(data.minHeight, -1))
drawable.setMinHeight(data.minHeight);
} else if (data.type == DrawableType.TINTED) {
String name = DrawableData.proper(data.file.name());
if (Utils.isNinePatch(data.file.name())) {
NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(atlas.createPatch(name));
drawable = ninePatchDrawable.tint(data.tint);
} else {
SpriteDrawable spriteDrawable = new SpriteDrawable(atlas.createSprite(name));
drawable = spriteDrawable.tint(data.tint);
}
if (!MathUtils.isEqual(data.minWidth, -1))
drawable.setMinWidth(data.minWidth);
if (!MathUtils.isEqual(data.minHeight, -1))
drawable.setMinHeight(data.minHeight);
} else if (data.type == DrawableType.TINTED_FROM_COLOR_DATA) {
String name = DrawableData.proper(data.file.name());
if (Utils.isNinePatch(data.file.name())) {
NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(atlas.createPatch(name));
drawable = ninePatchDrawable.tint(jsonData.getColorByName(data.tintName).color);
} else {
SpriteDrawable spriteDrawable = new SpriteDrawable(atlas.createSprite(name));
drawable = spriteDrawable.tint(jsonData.getColorByName(data.tintName).color);
}
if (!MathUtils.isEqual(data.minWidth, -1))
drawable.setMinWidth(data.minWidth);
if (!MathUtils.isEqual(data.minHeight, -1))
drawable.setMinHeight(data.minHeight);
} else {
String name = data.file.name();
name = DrawableData.proper(name);
drawable = new SpriteDrawable(atlas.createSprite(name));
if (!MathUtils.isEqual(data.minWidth, -1))
drawable.setMinWidth(data.minWidth);
if (!MathUtils.isEqual(data.minHeight, -1))
drawable.setMinHeight(data.minHeight);
}
drawablePairs.put(data, drawable);
}
return true;
} catch (Exception e) {
Gdx.app.error(getClass().getName(), "Error while attempting to generate drawables.", e);
dialogFactory.showDialogError("Atlas Error...", "Error while attempting to generate drawables.\n\nOpen log?");
return false;
}
}
Aggregations