Search in sources :

Example 6 with SpriteDrawable

use of com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable in project gdx-skineditor by cobolfoo.

the class OptionsPane method updateTableFields.

/**
	 * 
	 */
private void updateTableFields(final String style) {
    ImageButton button = (ImageButton) game.screenMain.barWidgets.group.getChecked();
    String widget = button.getUserObject().toString();
    Gdx.app.log("OptionsPane", "Update fields table for widget: " + widget + ", style: " + style);
    tableFields.clear();
    tableFields.add(new Label("Name", game.skin, "title")).left().width(170);
    tableFields.add(new Label("Value", game.skin, "title")).left().width(60).padRight(50);
    tableFields.row();
    Field[] fields = ClassReflection.getFields(currentStyle.getClass());
    for (final Field field : fields) {
        try {
            // Grey optional
            if (game.opt.isFieldOptional(currentStyle.getClass(), field.getName())) {
                tableFields.add(new Label(field.getName(), game.skin, "optional")).left();
            } else {
                tableFields.add(new Label(field.getName(), game.skin, "default")).left();
            }
            Actor actor;
            // field type
            String name = field.getType().getSimpleName();
            Object obj = field.get(currentStyle);
            if (name.equals("Drawable")) {
                /**
					 * Handle Drawable object
					 */
                Drawable drawable = (Drawable) field.get(currentStyle);
                String resourceName = "";
                ImageTextButton.ImageTextButtonStyle buttonStyle = new ImageTextButton.ImageTextButtonStyle(game.skin.getDrawable("default-round"), game.skin.getDrawable("default-round-down"), game.skin.getDrawable("default-round"), game.skin.getFont("default-font"));
                if (drawable != null) {
                    resourceName = game.skinProject.resolveObjectName(Drawable.class, drawable);
                    buttonStyle.imageUp = drawable;
                } else {
                    buttonStyle.up = game.skinProject.getDrawable("default-rect");
                    buttonStyle.checked = game.skinProject.getDrawable("default-rect");
                }
                actor = new ImageTextButton(resourceName, buttonStyle);
                ((ImageTextButton) actor).setClip(true);
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        showDrawableDialog(field);
                    }
                });
            } else if (name.equals("Color")) {
                /**
					 * Handle Color object
					 */
                Color color = (Color) field.get(currentStyle);
                ImageTextButton.ImageTextButtonStyle buttonStyle = new ImageTextButton.ImageTextButtonStyle(game.skin.getDrawable("default-round"), game.skin.getDrawable("default-round-down"), game.skin.getDrawable("default-round"), game.skin.getFont("default-font"));
                String resourceName = "";
                if (color != null) {
                    resourceName = game.skinProject.resolveObjectName(Color.class, color);
                    resourceName += " (" + color.toString() + ")";
                    // Create drawable on the fly
                    Pixmap pixmap = new Pixmap(18, 18, Pixmap.Format.RGBA8888);
                    pixmap.setColor(color);
                    pixmap.fill();
                    pixmap.setColor(Color.BLACK);
                    pixmap.drawRectangle(0, 0, 18, 18);
                    Texture texture = new Texture(pixmap);
                    buttonStyle.imageUp = new SpriteDrawable(new Sprite(texture));
                    pixmap.dispose();
                } else {
                    buttonStyle.up = game.skinProject.getDrawable("default-rect");
                    buttonStyle.checked = game.skinProject.getDrawable("default-rect");
                }
                actor = new ImageTextButton(resourceName, buttonStyle);
                ((ImageTextButton) actor).setClip(true);
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        showColorPickerDialog(field);
                    }
                });
            } else if (name.equals("BitmapFont")) {
                /**
					 * Handle BitmapFont object
					 */
                BitmapFont font = (BitmapFont) field.get(currentStyle);
                String resourceName = "";
                ImageTextButton.ImageTextButtonStyle buttonStyle = new ImageTextButton.ImageTextButtonStyle(game.skin.getDrawable("default-round"), game.skin.getDrawable("default-round-down"), game.skin.getDrawable("default-round"), game.skin.getFont("default-font"));
                if (font != null) {
                    resourceName = game.skinProject.resolveObjectName(BitmapFont.class, font);
                    buttonStyle.font = font;
                } else {
                    buttonStyle.up = game.skinProject.getDrawable("default-rect");
                    buttonStyle.checked = game.skinProject.getDrawable("default-rect");
                }
                actor = new ImageTextButton(resourceName, buttonStyle);
                ((ImageTextButton) actor).setClip(true);
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        showFontPickerDialog(field);
                    }
                });
            } else if (name.equals("float")) {
                /**
					 * Handle Float object
					 */
                Float value = (Float) field.get(currentStyle);
                String resourceName = "";
                ImageTextButton.ImageTextButtonStyle buttonStyle = new ImageTextButton.ImageTextButtonStyle(game.skin.getDrawable("default-round"), game.skin.getDrawable("default-round-down"), game.skin.getDrawable("default-round"), game.skin.getFont("default-font"));
                if ((value != null) && (value != 0)) {
                    resourceName = String.valueOf(value);
                } else {
                    buttonStyle.up = game.skinProject.getDrawable("default-rect");
                    buttonStyle.checked = game.skinProject.getDrawable("default-rect");
                }
                actor = new ImageTextButton(resourceName, buttonStyle);
                ((ImageTextButton) actor).setClip(true);
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        showFloatInputDialog(field);
                    }
                });
            } else if (name.equals("ListStyle")) {
                /**
					 * Handle ListStyle object
					 */
                ListStyle listStyle = (ListStyle) field.get(currentStyle);
                actor = new SelectBox<String>(game.skin, "default");
                Array<String> items = new Array<String>();
                final ObjectMap<String, ListStyle> values = game.skinProject.getAll(ListStyle.class);
                Iterator<String> it = values.keys().iterator();
                String selection = null;
                while (it.hasNext()) {
                    String key = it.next();
                    items.add(key);
                    if (listStyle == values.get(key)) {
                        selection = key;
                    }
                }
                ((SelectBox) actor).setItems(items);
                if (selection != null) {
                    ((SelectBox) actor).setSelected(selection);
                }
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        String selection = (String) ((SelectBox) actor).getSelected();
                        try {
                            field.set(currentStyle, values.get(selection));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        game.screenMain.saveToSkin();
                        refresh();
                        game.screenMain.paneOptions.updateSelectedTableFields();
                        game.screenMain.panePreview.refresh();
                    }
                });
            } else if (name.equals("ScrollPaneStyle")) {
                /**
					 * Handle ListStyle object
					 */
                ScrollPaneStyle scrollStyle = (ScrollPaneStyle) field.get(currentStyle);
                actor = new SelectBox<String>(game.skin, "default");
                Array<String> items = new Array<String>();
                final ObjectMap<String, ScrollPaneStyle> values = game.skinProject.getAll(ScrollPaneStyle.class);
                Iterator<String> it = values.keys().iterator();
                String selection = null;
                while (it.hasNext()) {
                    String key = it.next();
                    items.add(key);
                    if (scrollStyle == values.get(key)) {
                        selection = key;
                    }
                }
                ((SelectBox) actor).setItems(items);
                if (selection != null) {
                    ((SelectBox) actor).setSelected(selection);
                }
                actor.addListener(new ChangeListener() {

                    @Override
                    public void changed(ChangeEvent event, Actor actor) {
                        String selection = (String) ((SelectBox) actor).getSelected();
                        try {
                            field.set(currentStyle, values.get(selection));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        game.screenMain.saveToSkin();
                        refresh();
                        game.screenMain.paneOptions.updateSelectedTableFields();
                        game.screenMain.panePreview.refresh();
                    }
                });
            } else {
                Gdx.app.log("OptionsPane", "Unknown type: " + name);
                actor = new Label("Unknown Type", game.skin);
            }
            tableFields.add(actor).left().height(32).padRight(24).expandX().fillX();
            tableFields.row();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : ListStyle(com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle) ImageTextButton(com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) Texture(com.badlogic.gdx.graphics.Texture) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) Field(com.badlogic.gdx.utils.reflect.Field) ImageButton(com.badlogic.gdx.scenes.scene2d.ui.ImageButton) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Actor(com.badlogic.gdx.scenes.scene2d.Actor) Iterator(java.util.Iterator) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) SelectBox(com.badlogic.gdx.scenes.scene2d.ui.SelectBox) Color(com.badlogic.gdx.graphics.Color) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) Array(com.badlogic.gdx.utils.Array) Pixmap(com.badlogic.gdx.graphics.Pixmap) ScrollPaneStyle(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle)

Example 7 with SpriteDrawable

use of com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable in project gdx-skineditor by cobolfoo.

the class DrawablePickerDialog method updateTable.

/**
	 * 
	 */
public void updateTable() {
    tableDrawables.clear();
    Iterator<String> keys = items.keys().iterator();
    int count = 0;
    while (keys.hasNext()) {
        final String key = keys.next();
        if (key.startsWith("widgets/")) {
            continue;
        }
        Button buttonItem = new Button(game.skin);
        Image img = null;
        if (items.get(key) instanceof Drawable) {
            img = new Image((Drawable) items.get(key));
        } else {
            img = new Image((TextureRegion) items.get(key));
        }
        if (zoom == true) {
            buttonItem.add(img).expand().fill().pad(5);
        } else {
            buttonItem.add(img).expand().pad(5);
        }
        buttonItem.addListener(new ChangeListener() {

            @Override
            public void changed(ChangeEvent event, Actor actor) {
                if (field == null) {
                    return;
                }
                try {
                    // game.screenMain.paneOptions.refreshSelection();
                    if (items.get(key) instanceof Drawable) {
                        field.set(game.screenMain.paneOptions.currentStyle, items.get(key));
                    } else {
                        boolean ninepatch = false;
                        FileHandle test = new FileHandle("projects/" + game.screenMain.getcurrentProject() + "/assets/" + key + ".9.png");
                        if (test.exists() == true) {
                            ninepatch = true;
                        }
                        if (ninepatch == true) {
                            game.skinProject.add(key, new NinePatchDrawable(new NinePatch((TextureRegion) items.get(key))));
                            field.set(game.screenMain.paneOptions.currentStyle, game.skinProject.getDrawable(key));
                        } else {
                            game.skinProject.add(key, new SpriteDrawable(new Sprite((TextureRegion) items.get(key))));
                            field.set(game.screenMain.paneOptions.currentStyle, game.skinProject.getDrawable(key));
                        }
                    }
                    game.screenMain.saveToSkin();
                    hide();
                    game.screenMain.panePreview.refresh();
                    game.screenMain.paneOptions.updateSelectedTableFields();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        String objectType = items.get(key).getClass().getSimpleName();
        objectType = objectType.replace("Drawable", "");
        buttonItem.row();
        buttonItem.add(new Label(key, game.skin));
        buttonItem.row();
        buttonItem.add(new Label(objectType, game.skin, "title"));
        buttonItem.row();
        buttonItem.setClip(true);
        tableDrawables.add(buttonItem).width(160).height(184).pad(5);
        if (count == 4) {
            count = 0;
            tableDrawables.row();
            continue;
        }
        count++;
    }
}
Also used : SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) FileHandle(com.badlogic.gdx.files.FileHandle) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) Drawable(com.badlogic.gdx.scenes.scene2d.utils.Drawable) SpriteDrawable(com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable) Label(com.badlogic.gdx.scenes.scene2d.ui.Label) Image(com.badlogic.gdx.scenes.scene2d.ui.Image) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) ChangeEvent(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent) TextButton(com.badlogic.gdx.scenes.scene2d.ui.TextButton) Button(com.badlogic.gdx.scenes.scene2d.ui.Button) ImageButton(com.badlogic.gdx.scenes.scene2d.ui.ImageButton) Actor(com.badlogic.gdx.scenes.scene2d.Actor) ChangeListener(com.badlogic.gdx.scenes.scene2d.utils.ChangeListener) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)

Aggregations

Sprite (com.badlogic.gdx.graphics.g2d.Sprite)7 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)7 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)4 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)4 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)4 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)4 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)3 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)3 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)3 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)3 Color (com.badlogic.gdx.graphics.Color)2 Texture (com.badlogic.gdx.graphics.Texture)2 Actor (com.badlogic.gdx.scenes.scene2d.Actor)2 ImageButton (com.badlogic.gdx.scenes.scene2d.ui.ImageButton)2 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)2 ChangeListener (com.badlogic.gdx.scenes.scene2d.utils.ChangeListener)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 FileHandle (com.badlogic.gdx.files.FileHandle)1 Pixmap (com.badlogic.gdx.graphics.Pixmap)1