• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.utils.Pool.Poolable;
20 
21 /** The base class for all events.
22  * <p>
23  * By default an event will "bubble" up through an actor's parent's handlers (see {@link #setBubbles(boolean)}).
24  * <p>
25  * An actor's capture listeners can {@link #stop()} an event to prevent child actors from seeing it.
26  * <p>
27  * An Event may be marked as "handled" which will end its propagation outside of the Stage (see {@link #handle()}). The default
28  * {@link Actor#fire(Event)} will mark events handled if an {@link EventListener} returns true.
29  * <p>
30  * A cancelled event will be stopped and handled. Additionally, many actors will undo the side-effects of a canceled event. (See
31  * {@link #cancel()}.)
32  *
33  * @see InputEvent
34  * @see Actor#fire(Event) */
35 public class Event implements Poolable {
36 	private Stage stage;
37 	private Actor targetActor;
38 	private Actor listenerActor;
39 	private boolean capture; // true means event occurred during the capture phase
40 	private boolean bubbles = true; // true means propagate to target's parents
41 	private boolean handled; // true means the event was handled (the stage will eat the input)
42 	private boolean stopped; // true means event propagation was stopped
43 	private boolean cancelled; // true means propagation was stopped and any action that this event would cause should not happen
44 
45 	/** Marks this event as handled. This does not affect event propagation inside scene2d, but causes the {@link Stage} event
46 	 * methods to return true, which will eat the event so it is not passed on to the application under the stage. */
handle()47 	public void handle () {
48 		handled = true;
49 	}
50 
51 	/** Marks this event cancelled. This {@link #handle() handles} the event and {@link #stop() stops} the event propagation. It
52 	 * also cancels any default action that would have been taken by the code that fired the event. Eg, if the event is for a
53 	 * checkbox being checked, cancelling the event could uncheck the checkbox. */
cancel()54 	public void cancel () {
55 		cancelled = true;
56 		stopped = true;
57 		handled = true;
58 	}
59 
60 	/** Marks this event has being stopped. This halts event propagation. Any other listeners on the {@link #getListenerActor()
61 	 * listener actor} are notified, but after that no other listeners are notified. */
stop()62 	public void stop () {
63 		stopped = true;
64 	}
65 
reset()66 	public void reset () {
67 		stage = null;
68 		targetActor = null;
69 		listenerActor = null;
70 		capture = false;
71 		bubbles = true;
72 		handled = false;
73 		stopped = false;
74 		cancelled = false;
75 	}
76 
77 	/** Returns the actor that the event originated from. */
getTarget()78 	public Actor getTarget () {
79 		return targetActor;
80 	}
81 
setTarget(Actor targetActor)82 	public void setTarget (Actor targetActor) {
83 		this.targetActor = targetActor;
84 	}
85 
86 	/** Returns the actor that this listener is attached to. */
getListenerActor()87 	public Actor getListenerActor () {
88 		return listenerActor;
89 	}
90 
setListenerActor(Actor listenerActor)91 	public void setListenerActor (Actor listenerActor) {
92 		this.listenerActor = listenerActor;
93 	}
94 
getBubbles()95 	public boolean getBubbles () {
96 		return bubbles;
97 	}
98 
99 	/** If true, after the event is fired on the target actor, it will also be fired on each of the parent actors, all the way to
100 	 * the root. */
setBubbles(boolean bubbles)101 	public void setBubbles (boolean bubbles) {
102 		this.bubbles = bubbles;
103 	}
104 
105 	/** {@link #handle()} */
isHandled()106 	public boolean isHandled () {
107 		return handled;
108 	}
109 
110 	/** @see #stop() */
isStopped()111 	public boolean isStopped () {
112 		return stopped;
113 	}
114 
115 	/** @see #cancel() */
isCancelled()116 	public boolean isCancelled () {
117 		return cancelled;
118 	}
119 
setCapture(boolean capture)120 	public void setCapture (boolean capture) {
121 		this.capture = capture;
122 	}
123 
124 	/** If true, the event was fired during the capture phase.
125 	 * @see Actor#fire(Event) */
isCapture()126 	public boolean isCapture () {
127 		return capture;
128 	}
129 
setStage(Stage stage)130 	public void setStage (Stage stage) {
131 		this.stage = stage;
132 	}
133 
134 	/** The stage for the actor the event was fired on. */
getStage()135 	public Stage getStage () {
136 		return stage;
137 	}
138 }
139