Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2015-2016, theosirian * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.theosirian.ppioo.util; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable; /** * Created by theosirian on 07/01/2016. */ public class GUI { private static void loadResources(Skin skin) { FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("graduate.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter param = new FreeTypeFontGenerator.FreeTypeFontParameter(); param.size = 12; skin.add("x-small", gen.generateFont(param)); param.size = 16; skin.add("small", gen.generateFont(param)); param.size = 20; skin.add("default", gen.generateFont(param)); param.size = 24; skin.add("medium", gen.generateFont(param)); param.size = 32; skin.add("big", gen.generateFont(param)); param.size = 48; skin.add("huge", gen.generateFont(param)); param.size = 72; skin.add("x-huge", gen.generateFont(param)); skin.add("background", new SpriteDrawable(new Sprite(new Texture(Gdx.files.internal("main.png"))))); skin.add("default.up", new NinePatch(new Texture(Gdx.files.internal("button.up.default.9.png")), 16, 16, 16, 16)); skin.add("default.over", new NinePatch(new Texture(Gdx.files.internal("button.over.default.9.png")), 16, 16, 16, 16)); skin.add("default.down", new NinePatch(new Texture(Gdx.files.internal("button.down.default.9.png")), 16, 16, 16, 16)); skin.add("success.up", new NinePatch(new Texture(Gdx.files.internal("button.up.success.9.png")), 16, 16, 16, 16)); skin.add("success.over", new NinePatch(new Texture(Gdx.files.internal("button.over.success.9.png")), 16, 16, 16, 16)); skin.add("success.down", new NinePatch(new Texture(Gdx.files.internal("button.down.success.9.png")), 16, 16, 16, 16)); skin.add("warning.up", new NinePatch(new Texture(Gdx.files.internal("button.up.warning.9.png")), 16, 16, 16, 16)); skin.add("warning.over", new NinePatch(new Texture(Gdx.files.internal("button.over.warning.9.png")), 16, 16, 16, 16)); skin.add("warning.down", new NinePatch(new Texture(Gdx.files.internal("button.down.warning.9.png")), 16, 16, 16, 16)); skin.add("error.up", new NinePatch(new Texture(Gdx.files.internal("button.up.error.9.png")), 16, 16, 16, 16)); skin.add("error.over", new NinePatch(new Texture(Gdx.files.internal("button.over.error.9.png")), 16, 16, 16, 16)); skin.add("error.down", new NinePatch(new Texture(Gdx.files.internal("button.down.error.9.png")), 16, 16, 16, 16)); skin.add("window", new NinePatch(new Texture(Gdx.files.internal("window.9.png")), 32, 32, 32, 32)); skin.add("checkbox.on", new Texture(Gdx.files.internal("checkbox.on.default.png"))); skin.add("checkbox.off", new Texture(Gdx.files.internal("checkbox.off.default.png"))); skin.add("checkbox.on.disabled", new Texture(Gdx.files.internal("checkbox.on.disabled.png"))); skin.add("checkbox.off.disabled", new Texture(Gdx.files.internal("checkbox.off.disabled.png"))); skin.add("checkbox.on.slider.horizontal", new Texture(Gdx.files.internal("checkbox.on.slider.horizontal.png"))); skin.add("checkbox.off.slider.horizontal", new Texture(Gdx.files.internal("checkbox.off.slider.horizontal.png"))); skin.add("radiobutton.on", new Texture(Gdx.files.internal("radiobutton.on.default.png"))); skin.add("radiobutton.on.over", new Texture(Gdx.files.internal("radiobutton.on.over.default.png"))); skin.add("radiobutton.off", new Texture(Gdx.files.internal("radiobutton.off.default.png"))); skin.add("radiobutton.off.over", new Texture(Gdx.files.internal("radiobutton.off.over.default.png"))); skin.add("radiobutton.disabled", new Texture(Gdx.files.internal("radiobutton.disabled.default.png"))); skin.add("sound-on", new Texture(Gdx.files.internal("sound-on.png"))); skin.add("sound-off", new Texture(Gdx.files.internal("sound-off.png"))); skin.add("arrow.up", new Texture(Gdx.files.internal("arrow.up.default.png"))); skin.add("arrow.up.disabled", new Texture(Gdx.files.internal("arrow.up.disabled.png"))); skin.add("arrow.down", new Texture(Gdx.files.internal("arrow.down.default.png"))); skin.add("arrow.down.disabled", new Texture(Gdx.files.internal("arrow.down.disabled.png"))); skin.add("arrow.left", new Texture(Gdx.files.internal("arrow.left.default.png"))); skin.add("arrow.left.disabled", new Texture(Gdx.files.internal("arrow.left.disabled.png"))); skin.add("arrow.right", new Texture(Gdx.files.internal("arrow.right.default.png"))); skin.add("arrow.right.disabled", new Texture(Gdx.files.internal("arrow.right.disabled.png"))); for (ColorEnum color : ColorEnum.values()) { skin.add(color.colorName, color.getColor()); } Pixmap pixmap; pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888); pixmap.setColor(skin.getColor("white")); pixmap.fill(); skin.add("white", new Texture(pixmap)); pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888); pixmap.setColor(skin.getColor("transparent")); pixmap.fill(); skin.add("transparent", new Texture(pixmap)); pixmap = new Pixmap(8, 1, Pixmap.Format.RGBA8888); pixmap.setColor(Color.WHITE); pixmap.fill(); skin.add("white1x8", new Texture(pixmap)); pixmap = new Pixmap(1, 8, Pixmap.Format.RGBA8888); pixmap.setColor(Color.WHITE); pixmap.fill(); skin.add("white8x1", new Texture(pixmap)); pixmap = new Pixmap(32, 32, Pixmap.Format.RGBA8888); pixmap.setColor(Color.WHITE); pixmap.fill(); skin.add("white32s", new Texture(pixmap)); } private static void loadLabels(Skin skin) { Label.LabelStyle labelStyle; for (ColorEnum color : LabelEnum.colors) { for (LabelEnum label : LabelEnum.values()) { labelStyle = new Label.LabelStyle(); labelStyle.font = skin.getFont(label.fontName); labelStyle.fontColor = color.getColor(); labelStyle.background = skin.getDrawable("transparent"); if (LabelEnum.defaultColor == color) { skin.add(label.fontName, labelStyle); } skin.add(label.fontName + "." + color.colorName, labelStyle); } } skin.add("default", skin.get("default.black", LabelStyle.class)); } private static void loadButtons(Skin skin) { TextButtonStyle textButtonStyle; for (ColorEnum color : ButtonEnum.colors) { for (LabelEnum size : ButtonEnum.sizes) { for (ButtonEnum button : ButtonEnum.values()) { textButtonStyle = new TextButtonStyle(); textButtonStyle.font = skin.getFont(size.fontName); textButtonStyle.fontColor = color.getColor(); textButtonStyle.up = skin.getDrawable(button.type + ".up"); textButtonStyle.over = skin.getDrawable(button.type + ".over"); textButtonStyle.down = skin.getDrawable(button.type + ".down"); if (ButtonEnum.defaultColor == color) { if (ButtonEnum.defaultSize == size) { skin.add(button.type, textButtonStyle); } skin.add(button.type + "." + size.fontName, textButtonStyle); } skin.add(button.type + "." + size.fontName + "." + color.colorName, textButtonStyle); } } } skin.add("default", skin.get("default.default.black", TextButtonStyle.class)); } public static void configureSkin(Skin skin) { if (skin == null) { return; } loadResources(skin); loadLabels(skin); loadButtons(skin); ScrollPane.ScrollPaneStyle scrollPaneStyle = new ScrollPane.ScrollPaneStyle(); scrollPaneStyle.background = skin.newDrawable("white", Color.BLACK); scrollPaneStyle.hScroll = skin.newDrawable("white8x1", Color.GRAY); scrollPaneStyle.hScrollKnob = skin.newDrawable("white8x1", Color.WHITE); scrollPaneStyle.vScroll = skin.newDrawable("white1x8", Color.GRAY); scrollPaneStyle.vScrollKnob = skin.newDrawable("white1x8", Color.WHITE); scrollPaneStyle.corner = skin.newDrawable("white", Color.GRAY); skin.add("default", scrollPaneStyle); CheckBox.CheckBoxStyle checkBoxStyle = new CheckBox.CheckBoxStyle(); checkBoxStyle.font = skin.getFont("default"); checkBoxStyle.fontColor = Color.BLACK; checkBoxStyle.checkboxOn = skin.getDrawable("checkbox.on"); checkBoxStyle.checkboxOnDisabled = skin.getDrawable("checkbox.on.disabled"); checkBoxStyle.checkboxOff = skin.getDrawable("checkbox.off"); checkBoxStyle.checkboxOffDisabled = skin.getDrawable("checkbox.off.disabled"); skin.add("default", checkBoxStyle); checkBoxStyle = new CheckBox.CheckBoxStyle(); checkBoxStyle.font = skin.getFont("default"); checkBoxStyle.fontColor = Color.BLACK; checkBoxStyle.checkboxOn = skin.getDrawable("checkbox.on.slider.horizontal"); //checkBoxStyle.checkboxOnDisabled = skin.getDrawable("checkbox.on.disabled"); checkBoxStyle.checkboxOff = skin.getDrawable("checkbox.off.slider.horizontal"); //checkBoxStyle.checkboxOffDisabled = skin.getDrawable("checkbox.off.disabled"); skin.add("slider.horizontal", checkBoxStyle); Window.WindowStyle dialogStyle = new Window.WindowStyle(); dialogStyle.background = skin.newDrawable("window"); dialogStyle.stageBackground = skin.newDrawable("white", Color.valueOf("00000080")); dialogStyle.titleFont = skin.getFont("medium"); dialogStyle.titleFontColor = Color.BLACK; skin.add("default", dialogStyle); ImageButton.ImageButtonStyle upButtonStyle = new ImageButton.ImageButtonStyle(); upButtonStyle.imageUp = skin.getDrawable("arrow.up"); upButtonStyle.imageDown = skin.getDrawable("arrow.up"); upButtonStyle.imageDisabled = skin.getDrawable("arrow.up.disabled"); skin.add("arrow.up", upButtonStyle); ImageButton.ImageButtonStyle downButtonStyle = new ImageButton.ImageButtonStyle(); downButtonStyle.imageUp = skin.getDrawable("arrow.down"); downButtonStyle.imageDown = skin.getDrawable("arrow.down"); downButtonStyle.imageDisabled = skin.getDrawable("arrow.down.disabled"); skin.add("arrow.down", downButtonStyle); ImageButton.ImageButtonStyle radioButtonStyle = new ImageButton.ImageButtonStyle(); radioButtonStyle.imageUp = skin.getDrawable("radiobutton.off"); radioButtonStyle.imageOver = skin.getDrawable("radiobutton.off.over"); radioButtonStyle.imageChecked = skin.getDrawable("radiobutton.on"); radioButtonStyle.imageCheckedOver = skin.getDrawable("radiobutton.on.over"); radioButtonStyle.imageDisabled = skin.getDrawable("radiobutton.disabled"); skin.add("radiobutton", radioButtonStyle); ImageButton.ImageButtonStyle soundButtonOn = new ImageButton.ImageButtonStyle(); soundButtonOn.imageUp = skin.getDrawable("sound-on"); soundButtonOn.imageChecked = skin.getDrawable("sound-on"); skin.add("sound-on", soundButtonOn); ImageButton.ImageButtonStyle soundButtonOff = new ImageButton.ImageButtonStyle(); soundButtonOff.imageUp = skin.getDrawable("sound-off"); soundButtonOff.imageChecked = skin.getDrawable("sound-off"); skin.add("sound-off", soundButtonOff); } private enum ColorEnum { White("white", "ffffffff"), Black("black", "000000ff"), Transparent("transparent", "00000000"); private final String colorName; private final String value; ColorEnum(String colorName, String value) { this.colorName = colorName; this.value = value; } public final Color getColor() { return Color.valueOf(value); } } private enum LabelEnum { xSmall("x-small"), Small("small"), Default("default"), Medium("medium"), Big("big"), Huge("huge"), xHuge( "x-huge"); private static final ColorEnum[] colors = new ColorEnum[] { ColorEnum.White, ColorEnum.Black }; private static final ColorEnum defaultColor = colors[1]; private final String fontName; LabelEnum(String fontName) { this.fontName = fontName; } } private enum ButtonEnum { Default("default"), Success("success"), Warning("warning"), Error("error"); private static final LabelEnum[] sizes = new LabelEnum[] { LabelEnum.xSmall, LabelEnum.Small, LabelEnum.Default, LabelEnum.Medium, LabelEnum.Big, LabelEnum.Huge, LabelEnum.xHuge }; private static final ColorEnum[] colors = new ColorEnum[] { ColorEnum.White, ColorEnum.Black }; private static final LabelEnum defaultSize = sizes[2]; private static final ColorEnum defaultColor = colors[1]; private final String type; ButtonEnum(String type) { this.type = type; } } }