Home | History | Annotate | Download | only in scene2d
      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.scenes.scene2d;
     18 
     19 import com.badlogic.gdx.Input.Buttons;
     20 import com.badlogic.gdx.math.Vector2;
     21 
     22 /** Event for actor input: touch, mouse, keyboard, and scroll.
     23  * @see InputListener */
     24 public class InputEvent extends Event {
     25 	private Type type;
     26 	private float stageX, stageY;
     27 	private int pointer, button, keyCode, scrollAmount;
     28 	private char character;
     29 	private Actor relatedActor;
     30 
     31 	public void reset () {
     32 		super.reset();
     33 		relatedActor = null;
     34 		button = -1;
     35 	}
     36 
     37 	/** The stage x coordinate where the event occurred. Valid for: touchDown, touchDragged, touchUp, mouseMoved, enter, and exit. */
     38 	public float getStageX () {
     39 		return stageX;
     40 	}
     41 
     42 	public void setStageX (float stageX) {
     43 		this.stageX = stageX;
     44 	}
     45 
     46 	/** The stage x coordinate where the event occurred. Valid for: touchDown, touchDragged, touchUp, mouseMoved, enter, and exit. */
     47 	public float getStageY () {
     48 		return stageY;
     49 	}
     50 
     51 	public void setStageY (float stageY) {
     52 		this.stageY = stageY;
     53 	}
     54 
     55 	/** The type of input event. */
     56 	public Type getType () {
     57 		return type;
     58 	}
     59 
     60 	public void setType (Type type) {
     61 		this.type = type;
     62 	}
     63 
     64 	/** The pointer index for the event. The first touch is index 0, second touch is index 1, etc. Always -1 on desktop. Valid for:
     65 	 * touchDown, touchDragged, touchUp, enter, and exit. */
     66 	public int getPointer () {
     67 		return pointer;
     68 	}
     69 
     70 	public void setPointer (int pointer) {
     71 		this.pointer = pointer;
     72 	}
     73 
     74 	/** The index for the mouse button pressed. Always 0 on Android. Valid for: touchDown and touchUp.
     75 	 * @see Buttons */
     76 	public int getButton () {
     77 		return button;
     78 	}
     79 
     80 	public void setButton (int button) {
     81 		this.button = button;
     82 	}
     83 
     84 	/** The key code of the key that was pressed. Valid for: keyDown and keyUp. */
     85 	public int getKeyCode () {
     86 		return keyCode;
     87 	}
     88 
     89 	public void setKeyCode (int keyCode) {
     90 		this.keyCode = keyCode;
     91 	}
     92 
     93 	/** The character for the key that was type. Valid for: keyTyped. */
     94 	public char getCharacter () {
     95 		return character;
     96 	}
     97 
     98 	public void setCharacter (char character) {
     99 		this.character = character;
    100 	}
    101 
    102 	/** The amount the mouse was scrolled. Valid for: scrolled. */
    103 	public int getScrollAmount () {
    104 		return scrollAmount;
    105 	}
    106 
    107 	public void setScrollAmount (int scrollAmount) {
    108 		this.scrollAmount = scrollAmount;
    109 	}
    110 
    111 	/** The actor related to the event. Valid for: enter and exit. For enter, this is the actor being exited, or null. For exit,
    112 	 * this is the actor being entered, or null. */
    113 	public Actor getRelatedActor () {
    114 		return relatedActor;
    115 	}
    116 
    117 	/** @param relatedActor May be null. */
    118 	public void setRelatedActor (Actor relatedActor) {
    119 		this.relatedActor = relatedActor;
    120 	}
    121 
    122 	/** Sets actorCoords to this event's coordinates relative to the specified actor.
    123 	 * @param actorCoords Output for resulting coordinates. */
    124 	public Vector2 toCoordinates (Actor actor, Vector2 actorCoords) {
    125 		actorCoords.set(stageX, stageY);
    126 		actor.stageToLocalCoordinates(actorCoords);
    127 		return actorCoords;
    128 	}
    129 
    130 	/** Returns true of this event is a touchUp triggered by {@link Stage#cancelTouchFocus()}. */
    131 	public boolean isTouchFocusCancel () {
    132 		return stageX == Integer.MIN_VALUE || stageY == Integer.MIN_VALUE;
    133 	}
    134 
    135 	public String toString () {
    136 		return type.toString();
    137 	}
    138 
    139 	/** Types of low-level input events supported by scene2d. */
    140 	static public enum Type {
    141 		/** A new touch for a pointer on the stage was detected */
    142 		touchDown,
    143 		/** A pointer has stopped touching the stage. */
    144 		touchUp,
    145 		/** A pointer that is touching the stage has moved. */
    146 		touchDragged,
    147 		/** The mouse pointer has moved (without a mouse button being active). */
    148 		mouseMoved,
    149 		/** The mouse pointer or an active touch have entered (i.e., {@link Actor#hit(float, float, boolean) hit}) an actor. */
    150 		enter,
    151 		/** The mouse pointer or an active touch have exited an actor. */
    152 		exit,
    153 		/** The mouse scroll wheel has changed. */
    154 		scrolled,
    155 		/** A keyboard key has been pressed. */
    156 		keyDown,
    157 		/** A keyboard key has been released. */
    158 		keyUp,
    159 		/** A keyboard key has been pressed and released. */
    160 		keyTyped
    161 	}
    162 }
    163