Search in sources :

Example 11 with NinePatch

use of com.badlogic.gdx.graphics.g2d.NinePatch in project libgdx by libgdx.

the class NinePatchTest method render.

@Override
public void render() {
    final int screenWidth = Gdx.graphics.getWidth();
    final int screenHeight = Gdx.graphics.getHeight();
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    timePassed += Gdx.graphics.getDeltaTime();
    b.begin();
    final int sz = ninePatches.size;
    final int XGAP = 10;
    final int pheight = (int) ((screenHeight * 0.5f) / ((sz + 1) / 2));
    int x = XGAP;
    int y = 10;
    // Test that batch color is applied to NinePatch
    if (timePassed < 2) {
        b.setColor(1, 1, 1, Interpolation.sine.apply(timePassed / 2f));
    }
    // Test that the various nine patches render
    for (int i = 0; i < sz; i += 2) {
        int pwidth = (int) (0.44f * screenWidth);
        final NinePatch np1 = ninePatches.get(i).ninePatch;
        np1.draw(b, x, y, pwidth, pheight);
        if (i + 1 < sz) {
            final NinePatch np2 = ninePatches.get(i + 1).ninePatch;
            final int x2 = x + pwidth + XGAP;
            final int pwidth2 = screenWidth - XGAP - x2;
            np2.draw(b, x2, y, pwidth2, pheight);
        }
        y += pheight + 2;
    }
    // Dim a np by setting its color. Also test sending same np to batch twice
    NinePatch np = ninePatches.get(0).ninePatch;
    oldColor.set(np.getColor());
    filterColor.set(0.3f, 0.3f, 0.3f, 1.0f);
    np.setColor(filterColor);
    np.draw(b, x, y, 100, 30);
    np.setColor(oldColor);
    b.end();
}
Also used : NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch)

Example 12 with NinePatch

use of com.badlogic.gdx.graphics.g2d.NinePatch in project gdx-skineditor by cobolfoo.

the class Skin method getPatch.

/**
	 * Returns a registered ninepatch. If no ninepatch is found but a region
	 * exists with the name, a ninepatch is created from the region and stored
	 * in the skin. If the region is an {@link AtlasRegion} then the
	 * {@link AtlasRegion#splits} are used, otherwise the ninepatch will have
	 * the region as the center patch.
	 */
public NinePatch getPatch(String name) {
    NinePatch patch = optional(name, NinePatch.class);
    if (patch != null)
        return patch;
    try {
        TextureRegion region = getRegion(name);
        if (region instanceof AtlasRegion) {
            int[] splits = ((AtlasRegion) region).splits;
            if (splits != null) {
                patch = new NinePatch(region, splits[0], splits[1], splits[2], splits[3]);
                int[] pads = ((AtlasRegion) region).pads;
                if (pads != null)
                    patch.setPadding(pads[0], pads[1], pads[2], pads[3]);
            }
        }
        if (patch == null)
            patch = new NinePatch(region);
        add(name, patch, NinePatch.class);
        return patch;
    } catch (GdxRuntimeException ex) {
        throw new GdxRuntimeException("No NinePatch, TextureRegion, or Texture registered with name: " + name);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)

Example 13 with NinePatch

use of com.badlogic.gdx.graphics.g2d.NinePatch 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)

Example 14 with NinePatch

use of com.badlogic.gdx.graphics.g2d.NinePatch in project gdx-skineditor by cobolfoo.

the class NinePatchEditorDialog method refreshPreview.

public void refreshPreview() {
    Gdx.app.log("NinePatchEditorDialog", "refresh preview.");
    Pixmap pixmapImage = new Pixmap(Gdx.files.internal(textSourceImage.getText()));
    Pixmap pixmap = new Pixmap((int) (pixmapImage.getWidth() + 2), (int) (pixmapImage.getHeight() + 2), Pixmap.Format.RGBA8888);
    pixmap.drawPixmap(pixmapImage, 1, 1);
    pixmap.setColor(Color.BLACK);
    // Range left
    int h = pixmapImage.getHeight() + 1;
    pixmap.drawLine(0, (int) (h * rangeLeft.rangeStart), 0, (int) (h * rangeLeft.rangeStop));
    // Range top
    int w = pixmapImage.getWidth() + 1;
    pixmap.drawLine((int) (w * rangeTop.rangeStart), 0, (int) (w * rangeTop.rangeStop), 0);
    // Range right
    h = pixmapImage.getHeight() + 1;
    pixmap.drawLine(pixmapImage.getWidth() + 1, (int) (h * rangeRight.rangeStart), pixmapImage.getWidth() + 1, (int) (h * rangeRight.rangeStop));
    // Range bottom
    w = pixmapImage.getWidth() + 1;
    pixmap.drawLine((int) (w * rangeBottom.rangeStart), pixmap.getHeight() - 1, (int) (w * rangeBottom.rangeStop), pixmap.getHeight() - 1);
    PixmapIO.writePNG(tmpFile, pixmap);
    pixmapImage.dispose();
    pixmap.dispose();
    FileHandle fh = new FileHandle(System.getProperty("java.io.tmpdir")).child("skin_ninepatch");
    TexturePacker.Settings settings = new TexturePacker.Settings();
    TexturePacker.process(settings, fh.path(), fh.path(), "pack");
    TextureAtlas ta = new TextureAtlas(fh.child("pack.atlas"));
    NinePatch np = ta.createPatch("button");
    NinePatchDrawable drawable = new NinePatchDrawable(np);
    reviewTablePreview();
    buttonPreview1.getStyle().up = drawable;
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) NinePatch(com.badlogic.gdx.graphics.g2d.NinePatch) TexturePacker(com.badlogic.gdx.tools.texturepacker.TexturePacker) Pixmap(com.badlogic.gdx.graphics.Pixmap) NinePatchDrawable(com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)

Aggregations

NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)14 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)5 NinePatchDrawable (com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable)5 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)5 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)4 SpriteDrawable (com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable)4 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)3 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)3 TextureRegionDrawable (com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable)3 FileHandle (com.badlogic.gdx.files.FileHandle)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 Texture (com.badlogic.gdx.graphics.Texture)2 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)2 TextButton (com.badlogic.gdx.scenes.scene2d.ui.TextButton)2 TiledDrawable (com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable)2 Batch (com.badlogic.gdx.graphics.g2d.Batch)1 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)1 Actor (com.badlogic.gdx.scenes.scene2d.Actor)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1