Home | History | Annotate | Download | only in tests
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.tests;
     18 
     19 import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
     20 
     21 import com.badlogic.gdx.Gdx;
     22 import com.badlogic.gdx.graphics.Color;
     23 import com.badlogic.gdx.graphics.GL20;
     24 import com.badlogic.gdx.graphics.Texture;
     25 import com.badlogic.gdx.graphics.g2d.Batch;
     26 import com.badlogic.gdx.graphics.g2d.TextureRegion;
     27 import com.badlogic.gdx.scenes.scene2d.Actor;
     28 import com.badlogic.gdx.scenes.scene2d.InputEvent;
     29 import com.badlogic.gdx.scenes.scene2d.InputListener;
     30 import com.badlogic.gdx.scenes.scene2d.Stage;
     31 import com.badlogic.gdx.scenes.scene2d.actions.FloatAction;
     32 import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
     33 import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;
     34 import com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton;
     35 import com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton.ImageTextButtonStyle;
     36 import com.badlogic.gdx.scenes.scene2d.ui.Label;
     37 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
     38 import com.badlogic.gdx.scenes.scene2d.ui.Table;
     39 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
     40 import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
     41 import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
     42 import com.badlogic.gdx.scenes.scene2d.ui.Window;
     43 import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
     44 import com.badlogic.gdx.scenes.scene2d.utils.TiledDrawable;
     45 import com.badlogic.gdx.tests.utils.GdxTest;
     46 
     47 public class Scene2dTest extends GdxTest {
     48 	Stage stage;
     49 	private FloatAction meow = new FloatAction(10, 5);
     50 	private TiledDrawable patch;
     51 
     52 	public void create () {
     53 		stage = new Stage();
     54 		Gdx.input.setInputProcessor(stage);
     55 
     56 		final TextureRegion region = new TextureRegion(new Texture("data/badlogic.jpg"));
     57 		final Actor actor = new Actor() {
     58 			public void draw (Batch batch, float parentAlpha) {
     59 				Color color = getColor();
     60 				batch.setColor(color.r, color.g, color.b, parentAlpha);
     61 				batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(),
     62 					getRotation());
     63 			}
     64 		};
     65 		actor.setBounds(15, 15, 100, 100);
     66 		actor.setOrigin(50, 50);
     67 		stage.addActor(actor);
     68 		actor.addListener(new InputListener() {
     69 			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
     70 				System.out.println("down");
     71 				return true;
     72 			}
     73 
     74 			public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
     75 				System.out.println("up " + event.getTarget());
     76 			}
     77 		});
     78 
     79 		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
     80 
     81 		VerticalGroup g = new VerticalGroup().space(5).reverse().pad(5).fill();
     82 		for (int i = 0; i < 10; i++)
     83 			g.addActor(new TextButton("button " + i, skin));
     84 		g.addActor(new TextButton("longer button", skin));
     85 		Table table = new Table().debug();
     86 		table.add(g);
     87 		table.pack();
     88 		table.setPosition(5, 100);
     89 		stage.addActor(table);
     90 
     91 		HorizontalGroup h = new HorizontalGroup().space(5).reverse().pad(5).fill();
     92 		for (int i = 0; i < 5; i++)
     93 			h.addActor(new TextButton("button " + i, skin));
     94 		h.addActor(new TextButton("some taller\nbutton", skin));
     95 		table = new Table().debug();
     96 		table.add(h);
     97 		table.pack();
     98 		table.setPosition(130, 100);
     99 		stage.addActor(table);
    100 		table.toFront();
    101 
    102 		final TextButton button = new TextButton("Fancy Background", skin);
    103 
    104 // button.addListener(new ClickListener() {
    105 // public void clicked (InputEvent event, float x, float y) {
    106 // System.out.println("click! " + x + " " + y);
    107 // }
    108 // });
    109 
    110 		button.addListener(new ActorGestureListener() {
    111 			public boolean longPress (Actor actor, float x, float y) {
    112 				System.out.println("long press " + x + ", " + y);
    113 				return true;
    114 			}
    115 
    116 			public void fling (InputEvent event, float velocityX, float velocityY, int button) {
    117 				System.out.println("fling " + velocityX + ", " + velocityY);
    118 			}
    119 
    120 			public void zoom (InputEvent event, float initialDistance, float distance) {
    121 				System.out.println("zoom " + initialDistance + ", " + distance);
    122 			}
    123 
    124 			public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
    125 				event.getListenerActor().moveBy(deltaX, deltaY);
    126 				if (deltaX < 0) System.out.println("panning " + deltaX + ", " + deltaY + " " + event.getTarget());
    127 			}
    128 		});
    129 
    130 // button.addListener(new ChangeListener() {
    131 // public void changed (ChangeEvent event, Actor actor) {
    132 // // event.cancel();
    133 // }
    134 // });
    135 
    136 		button.setPosition(50, 50);
    137 		stage.addActor(button);
    138 
    139 // List select = new List(skin);
    140 // select.setBounds(200, 200, 100, 100);
    141 // select.setItems(new Object[] {1, 2, 3, 4, 5});
    142 // stage.addActor(select);
    143 
    144 // stage.addListener(new ChangeListener() {
    145 // public void changed (ChangeEvent event, Actor actor) {
    146 // System.out.println(actor);
    147 // }
    148 // });
    149 
    150 		meow.setDuration(2);
    151 
    152 		actor.addAction(forever(sequence(moveBy(50, 0, 2), moveBy(-50, 0, 2), run(new Runnable() {
    153 			public void run () {
    154 				actor.setZIndex(0);
    155 			}
    156 		}))));
    157 		// actor.addAction(parallel(rotateBy(90, 2), rotateBy(90, 2)));
    158 		// actor.addAction(parallel(moveTo(250, 250, 2, elasticOut), color(RED, 6), delay(0.5f), rotateTo(180, 5, swing)));
    159 		// actor.addAction(forever(sequence(scaleTo(2, 2, 0.5f), scaleTo(1, 1, 0.5f), delay(0.5f))));
    160 
    161 		patch = new TiledDrawable(skin.getRegion("default-round"));
    162 
    163 		Window window = new Window("Moo", skin);
    164 		Label lbl = new Label("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ", skin);
    165 		lbl.setWrap(true);
    166 		window.row();
    167 		window.add(lbl).width(400);
    168 		window.pack();
    169 		window.pack();
    170 		stage.addActor(window);
    171 
    172 		ImageTextButtonStyle style = new ImageTextButtonStyle(skin.get("default", TextButtonStyle.class));
    173 		style.imageUp = skin.getDrawable("default-round");
    174 		ImageTextButton buttonLeft = new ImageTextButton("HI IM LEFT", style);
    175 		ImageTextButton buttonRight = new ImageTextButton("HI IM RIGHT", style) {
    176 			{
    177 				clearChildren();
    178 				add(getLabel());
    179 				add(getImage());
    180 			}
    181 		};
    182 		CheckBox checkBoxLeft = new CheckBox("HI IM LEFT", skin, "default");
    183 		CheckBox checkBoxRight = new CheckBox("HI IM RIGHT", skin, "default") {
    184 			{
    185 				clearChildren();
    186 				add(getLabel());
    187 				add(getImage());
    188 			}
    189 		};
    190 
    191 		buttonLeft.setPosition(300, 400);
    192 		buttonRight.setPosition(300, 370);
    193 		checkBoxLeft.setPosition(150, 400);
    194 		checkBoxRight.setPosition(150, 370);
    195 
    196 		stage.addActor(buttonLeft);
    197 		stage.addActor(buttonRight);
    198 		stage.addActor(checkBoxLeft);
    199 		stage.addActor(checkBoxRight);
    200 
    201 		buttonLeft.debug();
    202 		buttonRight.debug();
    203 		checkBoxLeft.debug();
    204 		checkBoxRight.debug();
    205 	}
    206 
    207 	public void render () {
    208 		// System.out.println(meow.getValue());
    209 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    210 		stage.act(Gdx.graphics.getDeltaTime());
    211 		stage.draw();
    212 
    213 		stage.getBatch().begin();
    214 		patch.draw(stage.getBatch(), 300, 100, 126, 126);
    215 		stage.getBatch().end();
    216 	}
    217 
    218 	public void resize (int width, int height) {
    219 		stage.getViewport().update(width, height, true);
    220 	}
    221 
    222 	public void dispose () {
    223 		stage.dispose();
    224 	}
    225 }
    226