Permalink
Browse files

Adding draggable image implementation, example in GameLoop, added swi…

…tch state for play button for testing
  • Loading branch information...
1 parent 926c374 commit 52d1a99298557be7adb9b01e55c8dc7558fd6148 @jmselk jmselk committed Apr 20, 2012
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -1,20 +1,66 @@
package com.redditandroiddevelopers.tamagotchi.screens;
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.graphics.GL10;
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.scenes.scene2d.Actor;
+import com.badlogic.gdx.scenes.scene2d.Stage;
+import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
+import com.redditandroiddevelopers.tamagotchi.ui.Draggable;
+import com.redditandroiddevelopers.tamagotchi.ui.DraggableImage;
+
/**
* This screen instance will represent the normal game execution
*/
-public class GameLoop extends CommonScreen {
+public class GameLoop extends CommonScreen implements ClickListener, Draggable {
+
+ private Stage stage;
+ DraggableImage img;
+
+ @Override
+ public void show() {
+ stage = new Stage(800, 480, true, new SpriteBatch());
+// pullDownTexture = new Texture(Gdx.files.internal("pulldown.png"));
+// sprite = new Sprite(pullDownTexture);
+// spriteBatch = new SpriteBatch();
+ img = new DraggableImage(new Texture(Gdx.files.internal("pulldown.png")));
+// img.scaleX = 2;
+// img.scaleY = 2;
+// img.x = 50;
+// img.y = 50;
+ img.setClickListener(this);
+ img.setDraggable(this);
+ stage.addActor(img);
+ Gdx.input.setInputProcessor(stage);
+ }
+
+ @Override
+ public void update(float delta) {
+
+ }
- @Override
- public void update(float delta) {
- // TODO Auto-generated method stub
+ @Override
+ public void draw(float delta) {
+ Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
+ stage.draw();
+// spriteBatch.begin();
+// sprite.draw(spriteBatch);
+// spriteBatch.draw(pullDownTexture, 550, 550, 0, 0, pullDownTexture.getWidth(),
+// pullDownTexture.getHeight());
+// spriteBatch.end();
+ }
- }
+ @Override
+ public void click(Actor actor, float x, float y) {
- @Override
- public void draw(float delta) {
- // TODO Auto-generated method stub
+ }
- }
+ @Override
+ public void drag(Actor a, float x, float y, int pointer) {
+ a.x += x;
+ a.y += y;
+ //System.out.println("x=" + x + ", y=" + y + ", pointer=" + pointer);
+ }
}
@@ -11,6 +11,7 @@
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
+import com.redditandroiddevelopers.tamagotchi.TamagotchiGame;
public class MainMenuScreen extends CommonScreen implements ClickListener {
@@ -82,6 +83,7 @@ public void draw(float delta) {
public void click(Actor actor, float x, float y) {
if (actor == btnPlay) {
Gdx.app.debug(TAG, "Touch on Play");
+ TamagotchiGame.updateState(TamagotchiGame.STATE_RUNNING);
} else if (actor == btnSelect) {
Gdx.app.debug(TAG, "Touch on Select");
} else if (actor == btnMemories) {
@@ -12,7 +12,7 @@
* A simple splash screen that is shown when the game is started. It
* automatically transitions to the MainMenuScreen after 3 seconds.
*/
-public class SplashScreen extends CommonScreen {
+public class SplashScreen extends CommonScreen{
private static final float SPLASH_DURATION = .5f;
@@ -0,0 +1,21 @@
+package com.redditandroiddevelopers.tamagotchi.ui;
+
+import com.badlogic.gdx.scenes.scene2d.Actor;
+
+/**
+ * Our custom Draggable interface which will receive delegated Drag events
+ * from DragImage (or other classes using this implementation)
+ *
+ * @author Jeffrey Selk
+ *
+ */
+public interface Draggable {
+ /**
+ * Dispatch a drag event
+ * @param a The actor on which the event hit
+ * @param x The X coord of the touch translated to the scene
+ * @param y The Y coord of the touch translated to the scene
+ * @param pointer The pointer of the touch
+ */
+ void drag(Actor a, float x, float y, int pointer);
+}
@@ -0,0 +1,49 @@
+package com.redditandroiddevelopers.tamagotchi.ui;
+
+import com.badlogic.gdx.graphics.Texture;
+import com.badlogic.gdx.scenes.scene2d.ui.Image;
+
+/**
+ * A custom implementation of a libGDX Image which overrides the drag functions
+ * to allow dragging via a custom Draggable interface.
+ *
+ * @author Jeffrey Selk
+ *
+ */
+public class DraggableImage extends Image {
+
+ /**
+ * The Draggable instance which will receive delegated Drag events
+ */
+ private Draggable draggable;
+
+ /**
+ * Constructor (the only one needed thus far, override others as needed)
+ *
+ * @param texture
+ * The texture of the Image
+ */
+ public DraggableImage(Texture texture) {
+ super(texture);
+ }
+
+ /**
+ * Set the Draggable instance
+ *
+ * @param d
+ * The Draggable instance
+ */
+ public void setDraggable(Draggable d) {
+ this.draggable = d;
+ }
+
+ /* (non-Javadoc)
+ * @see com.badlogic.gdx.scenes.scene2d.ui.Image#touchDragged(float, float, int)
+ */
+ @Override
+ public void touchDragged(float x, float y, int pointer) {
+ if (draggable != null) {
+ draggable.drag(this, x, y, pointer);
+ }
+ }
+}

0 comments on commit 52d1a99

Please sign in to comment.