Permalink
Browse files

Dialog, easily add key events.

  • Loading branch information...
1 parent 8b88627 commit db7c7481bdc6d12a668aedeaaf3a5674a175d8fa @NathanSweet NathanSweet committed Aug 19, 2012
@@ -1,8 +1,11 @@
package com.badlogic.gdx.scenes.scene2d.ui;
+import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
+import com.badlogic.gdx.scenes.scene2d.InputEvent;
+import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
@@ -128,6 +131,21 @@ public void setObject (Actor actor, Object object) {
values.put(actor, object);
}
+ /** If this key is pressed, {@link #clicked(Object)} is called with the specified object.
+ * @see Keys */
+ public Dialog key (final int keycode, final Object object) {
+ addListener(new InputListener() {
+ public boolean keyDown (InputEvent event, int keycode2) {
+ if (keycode == keycode2) {
+ clicked(object);
+ hide();
+ }
+ return false;
+ }
+ });
+ return this;
+ }
+
/** Called when a button is clicked.
* @param object The object specified when the button was added. */
protected void clicked (Object object) {
@@ -17,6 +17,7 @@
package com.badlogic.gdx.tests;
import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
@@ -159,7 +160,8 @@ public void changed (ChangeEvent event, Actor actor) {
protected void clicked (Object object) {
System.out.println("Chosen: " + object);
}
- }.text("Are you enjoying this demo?").button("Yes", true).button("No", false).show(stage, 0.4f);
+ }.text("Are you enjoying this demo?").button("Yes", true).button("No", false).key(Keys.ENTER, true)
+ .key(Keys.ESCAPE, false).show(stage, 0.4f);
}
});

0 comments on commit db7c748

Please sign in to comment.