Java tutorial
/* * This file is part of HackNet * Copyright (C) <2014> <Ivan Kulikov> * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package free.hacknet.game.tests; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture.TextureFilter; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.FreeTypeFontLoaderParameter; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.List; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.ScreenViewport; import free.hacknet.game.Assets; public class ListTestsScreen extends ScreenAdapter { public static String test1 = "test1"; public static String test2 = "test2"; private Stage stage; private Skin skin; private Table table; public ListTestsScreen() { FreeTypeFontLoaderParameter size1Params = new FreeTypeFontLoaderParameter(); size1Params.fontFileName = Assets.FontPath; size1Params.fontParameters.size = 30; size1Params.fontParameters.magFilter = TextureFilter.Linear; size1Params.fontParameters.minFilter = TextureFilter.Linear; Assets.manager.load("size10.ttf", BitmapFont.class, size1Params); Assets.manager.finishLoading(); Assets.manager.load(Assets.uiskin, Skin.class); Assets.manager.finishLoading(); skin = new Skin(); skin.addRegions(new TextureAtlas(Gdx.files.internal("data/uiskin.atlas"))); skin.add("default-font", Assets.manager.get("size10.ttf", BitmapFont.class), BitmapFont.class); skin.load(Gdx.files.internal("data/uiskin.json")); stage = new Stage(new ScreenViewport()); table = new Table(); Label label = new Label("lol", skin); table.add(label).row(); TextButton button = new TextButton("start test", skin); table.setFillParent(true); Gdx.input.setInputProcessor(stage); final List list = new List(skin); table.add(list).expandX().fill().padLeft(20).padRight(20); table.add(button).width(100).height(50).row(); Array<String> items = new Array<String>(); items.add(test1); items.add(test2); button.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { Gdx.app.log("ListTestsScreen", "button clicked"); Gdx.app.log("ListTestsScreen", list.getSelected().toString()); } }); list.setItems(items); stage.addActor(table); stage.setDebugAll(true); } @Override public void render(float delta) { Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f)); stage.draw(); } @Override public void resize(int width, int height) { stage.getViewport().update(width, height, true); } @Override public void dispose() { Gdx.input.setInputProcessor(null); stage.dispose(); skin.dispose(); } }