v0.7.4b: refactored input event handling structure
This commit is contained in:
@@ -24,97 +24,65 @@ package com.watabou.input;
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.watabou.noosa.Game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class InputHandler extends InputAdapter {
|
||||
|
||||
// Accumulated touch events
|
||||
protected ArrayList<Touchscreen.Touch> touchEvents = new ArrayList<>();
|
||||
|
||||
// Accumulated key events
|
||||
protected ArrayList<Keys.Key> keyEvents = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public boolean keyDown( int keyCode ) {
|
||||
|
||||
if (keyCode != Keys.BACK &&
|
||||
keyCode != Keys.MENU) {
|
||||
return false;
|
||||
}
|
||||
|
||||
synchronized (keyEvents) {
|
||||
keyEvents.add( new Keys.Key(keyCode, true) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp( int keyCode ) {
|
||||
|
||||
if (keyCode != Keys.BACK &&
|
||||
keyCode != Keys.MENU) {
|
||||
return false;
|
||||
}
|
||||
|
||||
synchronized (keyEvents) {
|
||||
keyEvents.add( new Keys.Key(keyCode, false) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyTyped(char character) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
synchronized (touchEvents) {
|
||||
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
synchronized (touchEvents) {
|
||||
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, false));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
synchronized (touchEvents) {
|
||||
touchEvents.add(new Touchscreen.Touch(screenX, screenY, pointer, true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrolled(int amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void processAllEvents(){
|
||||
synchronized (touchEvents) {
|
||||
Touchscreen.processTouchEvents( touchEvents );
|
||||
touchEvents.clear();
|
||||
}
|
||||
synchronized (keyEvents) {
|
||||
Keys.processKeyEvents( keyEvents );
|
||||
keyEvents.clear();
|
||||
}
|
||||
PointerEvent.processPointerEvents();
|
||||
KeyEvent.processKeyEvents();
|
||||
}
|
||||
|
||||
// *********************
|
||||
// *** Pointer Input ***
|
||||
// *********************
|
||||
|
||||
@Override
|
||||
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, false));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
screenX /= (Game.dispWidth / (float)Game.width);
|
||||
screenY /= (Game.dispHeight / (float)Game.height);
|
||||
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
|
||||
return true;
|
||||
}
|
||||
|
||||
// *****************
|
||||
// *** Key Input ***
|
||||
// *****************
|
||||
|
||||
@Override
|
||||
public synchronized boolean keyDown( int keyCode ) {
|
||||
|
||||
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
|
||||
return false;
|
||||
}
|
||||
|
||||
KeyEvent.addKeyEvent( new KeyEvent(keyCode, true) );
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean keyUp( int keyCode ) {
|
||||
|
||||
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
|
||||
return false;
|
||||
}
|
||||
|
||||
KeyEvent.addKeyEvent( new KeyEvent(keyCode, false) );
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,28 +26,48 @@ import com.watabou.utils.Signal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
//TODO probably want to merge this into a central input processor class
|
||||
public class Keys {
|
||||
public class KeyEvent {
|
||||
|
||||
public int code;
|
||||
public boolean pressed;
|
||||
|
||||
public KeyEvent( int code, boolean pressed ) {
|
||||
this.code = code;
|
||||
this.pressed = pressed;
|
||||
}
|
||||
|
||||
// **********************
|
||||
// *** Static members ***
|
||||
// **********************
|
||||
|
||||
public static final int BACK = Input.Keys.BACK;
|
||||
public static final int MENU = Input.Keys.MENU;
|
||||
|
||||
public static Signal<Key> event = new Signal<>( true );
|
||||
|
||||
public static void processKeyEvents( ArrayList<Key> events ){
|
||||
for (Key k : events){
|
||||
event.dispatch(k);
|
||||
}
|
||||
private static Signal<KeyEvent> keySignal = new Signal<>( true );
|
||||
|
||||
public static void addKeyListener( Signal.Listener<KeyEvent> listener ){
|
||||
keySignal.add(listener);
|
||||
}
|
||||
|
||||
public static class Key {
|
||||
|
||||
public int code;
|
||||
public boolean pressed;
|
||||
|
||||
public Key( int code, boolean pressed ) {
|
||||
this.code = code;
|
||||
this.pressed = pressed;
|
||||
public static void removeKeyListener( Signal.Listener<KeyEvent> listener ){
|
||||
keySignal.remove(listener);
|
||||
}
|
||||
|
||||
public static void clearListeners(){
|
||||
keySignal.removeAll();
|
||||
}
|
||||
|
||||
//Accumulated key events
|
||||
private static ArrayList<KeyEvent> keyEvents = new ArrayList<>();
|
||||
|
||||
public static synchronized void addKeyEvent( KeyEvent event ){
|
||||
keyEvents.add( event );
|
||||
}
|
||||
|
||||
public static synchronized void processKeyEvents(){
|
||||
for (KeyEvent k : keyEvents){
|
||||
keySignal.dispatch(k);
|
||||
}
|
||||
keyEvents.clear();
|
||||
}
|
||||
}
|
||||
104
SPD-classes/src/main/java/com/watabou/input/PointerEvent.java
Normal file
104
SPD-classes/src/main/java/com/watabou/input/PointerEvent.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2019 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.input;
|
||||
|
||||
import com.watabou.utils.PointF;
|
||||
import com.watabou.utils.Signal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class PointerEvent {
|
||||
|
||||
public PointF start;
|
||||
public PointF current;
|
||||
public int id;
|
||||
public boolean down;
|
||||
|
||||
public PointerEvent( int x, int y, int id, boolean down){
|
||||
start = current = new PointF(x, y);
|
||||
this.id = id;
|
||||
this.down = down;
|
||||
}
|
||||
|
||||
public void update( PointerEvent other ){
|
||||
this.current = other.current;
|
||||
}
|
||||
|
||||
public void update( int x, int y ){
|
||||
current.set( x, y );
|
||||
}
|
||||
|
||||
public PointerEvent up() {
|
||||
down = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
// **********************
|
||||
// *** Static members ***
|
||||
// **********************
|
||||
|
||||
private static Signal<PointerEvent> pointerSignal = new Signal<>( true );
|
||||
|
||||
public static void addPointerListener( Signal.Listener<PointerEvent> listener ){
|
||||
pointerSignal.add(listener);
|
||||
}
|
||||
|
||||
public static void removePointerListener( Signal.Listener<PointerEvent> listener ){
|
||||
pointerSignal.remove(listener);
|
||||
}
|
||||
|
||||
public static void clearListeners(){
|
||||
pointerSignal.removeAll();
|
||||
}
|
||||
|
||||
// Accumulated pointer events
|
||||
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
|
||||
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
|
||||
|
||||
public static synchronized void addPointerEvent( PointerEvent event ){
|
||||
pointerEvents.add( event );
|
||||
}
|
||||
|
||||
public static synchronized void processPointerEvents(){
|
||||
for (PointerEvent p : pointerEvents){
|
||||
if (activePointers.containsKey(p.id)){
|
||||
PointerEvent existing = activePointers.get(p.id);
|
||||
existing.current = p.current;
|
||||
if (existing.down == p.down){
|
||||
pointerSignal.dispatch( null );
|
||||
} else if (p.down) {
|
||||
pointerSignal.dispatch( existing );
|
||||
} else {
|
||||
activePointers.remove(existing.id);
|
||||
pointerSignal.dispatch(existing.up());
|
||||
}
|
||||
} else {
|
||||
if (p.down) {
|
||||
activePointers.put(p.id, p);
|
||||
}
|
||||
pointerSignal.dispatch(p);
|
||||
}
|
||||
}
|
||||
pointerEvents.clear();
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2019 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.input;
|
||||
|
||||
import com.watabou.utils.PointF;
|
||||
import com.watabou.utils.Signal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
//TODO integrate into a central input handler class
|
||||
public class Touchscreen {
|
||||
|
||||
public static Signal<Touch> event = new Signal<>( true );
|
||||
|
||||
public static HashMap<Integer,Touch> pointers = new HashMap<>();
|
||||
|
||||
public static void processTouchEvents( ArrayList<Touch> events ) {
|
||||
for (Touch t : events){
|
||||
if (pointers.containsKey(t.id)){
|
||||
Touch existing = pointers.get(t.id);
|
||||
existing.current = t.current;
|
||||
if (existing.down == t.down){
|
||||
event.dispatch( null );
|
||||
} else if (t.down) {
|
||||
event.dispatch( existing );
|
||||
} else {
|
||||
pointers.remove(existing.id);
|
||||
event.dispatch(existing.up());
|
||||
}
|
||||
} else {
|
||||
if (t.down) {
|
||||
pointers.put(t.id, t);
|
||||
}
|
||||
event.dispatch(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Touch {
|
||||
|
||||
public PointF start;
|
||||
public PointF current;
|
||||
public int id;
|
||||
public boolean down;
|
||||
|
||||
public Touch( int x, int y, int id, boolean down){
|
||||
start = current = new PointF(x, y);
|
||||
this.id = id;
|
||||
this.down = down;
|
||||
}
|
||||
|
||||
public void update( Touch other ){
|
||||
this.current = other.current;
|
||||
}
|
||||
|
||||
public void update( int x, int y ){
|
||||
current.set( x, y );
|
||||
}
|
||||
|
||||
public Touch up() {
|
||||
down = false;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -36,7 +36,7 @@ import com.watabou.gltextures.TextureCache;
|
||||
import com.watabou.glwrap.Blending;
|
||||
import com.watabou.glwrap.Vertexbuffer;
|
||||
import com.watabou.input.InputHandler;
|
||||
import com.watabou.input.Keys;
|
||||
import com.watabou.input.KeyEvent;
|
||||
import com.watabou.noosa.audio.Music;
|
||||
import com.watabou.utils.SystemTime;
|
||||
|
||||
@@ -132,8 +132,8 @@ public class Game extends AndroidApplication implements ApplicationListener {
|
||||
|
||||
inputHandler = new InputHandler();
|
||||
Gdx.input.setInputProcessor(inputHandler);
|
||||
Gdx.input.setCatchKey(Keys.BACK, true);
|
||||
Gdx.input.setCatchKey(Keys.MENU, true);
|
||||
Gdx.input.setCatchKey(KeyEvent.BACK, true);
|
||||
Gdx.input.setCatchKey(KeyEvent.MENU, true);
|
||||
|
||||
//FIXME this doesn't seem to work quite right. That might not be due to LibGDX though.
|
||||
Music.setMuteListener();
|
||||
|
||||
123
SPD-classes/src/main/java/com/watabou/noosa/PointerArea.java
Normal file
123
SPD-classes/src/main/java/com/watabou/noosa/PointerArea.java
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2019 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.noosa;
|
||||
|
||||
import com.watabou.input.PointerEvent;
|
||||
import com.watabou.utils.Signal;
|
||||
|
||||
public class PointerArea extends Visual implements Signal.Listener<PointerEvent> {
|
||||
|
||||
// Its target can be pointerarea itself
|
||||
public Visual target;
|
||||
|
||||
protected PointerEvent curEvent = null;
|
||||
|
||||
//if true, this PointerArea will always block input, even when it is inactive
|
||||
public boolean blockWhenInactive = false;
|
||||
|
||||
public PointerArea(Visual target ) {
|
||||
super( 0, 0, 0, 0 );
|
||||
this.target = target;
|
||||
|
||||
PointerEvent.addPointerListener( this );
|
||||
}
|
||||
|
||||
public PointerArea(float x, float y, float width, float height ) {
|
||||
super( x, y, width, height );
|
||||
this.target = this;
|
||||
|
||||
visible = false;
|
||||
|
||||
PointerEvent.addPointerListener( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onSignal( PointerEvent event ) {
|
||||
|
||||
boolean hit = event != null && target.overlapsScreenPoint( (int)event.current.x, (int)event.current.y );
|
||||
|
||||
if (!isActive()) {
|
||||
return (hit && blockWhenInactive);
|
||||
}
|
||||
|
||||
if (hit) {
|
||||
|
||||
boolean returnValue = (event.down || event == curEvent);
|
||||
|
||||
if (event.down) {
|
||||
|
||||
if (curEvent == null) {
|
||||
curEvent = event;
|
||||
}
|
||||
onPointerDown( event );
|
||||
|
||||
} else {
|
||||
|
||||
onPointerUp( event );
|
||||
|
||||
if (curEvent == event) {
|
||||
curEvent = null;
|
||||
onClick( event );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
|
||||
} else {
|
||||
|
||||
if (event == null && curEvent != null) {
|
||||
onDrag(curEvent);
|
||||
}
|
||||
|
||||
else if (curEvent != null && !event.down) {
|
||||
onPointerUp( event );
|
||||
curEvent = null;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected void onPointerDown( PointerEvent event ) {
|
||||
}
|
||||
|
||||
protected void onPointerUp( PointerEvent event) {
|
||||
}
|
||||
|
||||
protected void onClick( PointerEvent event ) {
|
||||
}
|
||||
|
||||
protected void onDrag( PointerEvent event ) {
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
curEvent = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
PointerEvent.removePointerListener( this );
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
@@ -21,34 +21,35 @@
|
||||
|
||||
package com.watabou.noosa;
|
||||
|
||||
import com.watabou.input.Keys;
|
||||
import com.watabou.input.KeyEvent;
|
||||
import com.watabou.utils.Signal;
|
||||
|
||||
public class Scene extends Group {
|
||||
|
||||
private Signal.Listener<Keys.Key> keyListener;
|
||||
private Signal.Listener<KeyEvent> keyListener;
|
||||
|
||||
public void create() {
|
||||
Keys.event.add( keyListener = new Signal.Listener<Keys.Key>() {
|
||||
KeyEvent.addKeyListener( keyListener = new Signal.Listener<KeyEvent>() {
|
||||
@Override
|
||||
public void onSignal( Keys.Key key ) {
|
||||
if (Game.instance != null && key.pressed) {
|
||||
switch (key.code) {
|
||||
case Keys.BACK:
|
||||
public boolean onSignal( KeyEvent event ) {
|
||||
if (Game.instance != null && event.pressed) {
|
||||
switch (event.code) {
|
||||
case KeyEvent.BACK:
|
||||
onBackPressed();
|
||||
break;
|
||||
case Keys.MENU:
|
||||
case KeyEvent.MENU:
|
||||
onMenuPressed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
Keys.event.remove( keyListener );
|
||||
KeyEvent.removeKeyListener( keyListener );
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2019 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.noosa;
|
||||
|
||||
import com.watabou.input.Touchscreen;
|
||||
import com.watabou.input.Touchscreen.Touch;
|
||||
import com.watabou.utils.Signal;
|
||||
|
||||
public class TouchArea extends Visual implements Signal.Listener<Touchscreen.Touch> {
|
||||
|
||||
// Its target can be toucharea itself
|
||||
public Visual target;
|
||||
|
||||
protected Touchscreen.Touch touch = null;
|
||||
|
||||
//if true, this TouchArea will always block input, even when it is inactive
|
||||
public boolean blockWhenInactive = false;
|
||||
|
||||
public TouchArea( Visual target ) {
|
||||
super( 0, 0, 0, 0 );
|
||||
this.target = target;
|
||||
|
||||
Touchscreen.event.add( this );
|
||||
}
|
||||
|
||||
public TouchArea( float x, float y, float width, float height ) {
|
||||
super( x, y, width, height );
|
||||
this.target = this;
|
||||
|
||||
visible = false;
|
||||
|
||||
Touchscreen.event.add( this );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSignal( Touch touch ) {
|
||||
|
||||
boolean hit = touch != null && target.overlapsScreenPoint( (int)touch.current.x, (int)touch.current.y );
|
||||
|
||||
if (!isActive()) {
|
||||
if (hit && blockWhenInactive) Touchscreen.event.cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hit) {
|
||||
|
||||
if (touch.down || touch == this.touch) Touchscreen.event.cancel();
|
||||
|
||||
if (touch.down) {
|
||||
|
||||
if (this.touch == null) {
|
||||
this.touch = touch;
|
||||
}
|
||||
onTouchDown( touch );
|
||||
|
||||
} else {
|
||||
|
||||
onTouchUp( touch );
|
||||
|
||||
if (this.touch == touch) {
|
||||
this.touch = null;
|
||||
onClick( touch );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
if (touch == null && this.touch != null) {
|
||||
onDrag( this.touch );
|
||||
}
|
||||
|
||||
else if (this.touch != null && !touch.down) {
|
||||
onTouchUp( touch );
|
||||
this.touch = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected void onTouchDown( Touch touch ) {
|
||||
}
|
||||
|
||||
protected void onTouchUp( Touch touch ) {
|
||||
}
|
||||
|
||||
protected void onClick( Touch touch ) {
|
||||
}
|
||||
|
||||
protected void onDrag( Touch touch ) {
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
touch = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
Touchscreen.event.remove( this );
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
@@ -21,15 +21,15 @@
|
||||
|
||||
package com.watabou.noosa.ui;
|
||||
|
||||
import com.watabou.input.Touchscreen.Touch;
|
||||
import com.watabou.input.PointerEvent;
|
||||
import com.watabou.noosa.Game;
|
||||
import com.watabou.noosa.TouchArea;
|
||||
import com.watabou.noosa.PointerArea;
|
||||
|
||||
public class Button extends Component {
|
||||
|
||||
public static float longClick = 1f;
|
||||
|
||||
protected TouchArea hotArea;
|
||||
protected PointerArea hotArea;
|
||||
|
||||
protected boolean pressed;
|
||||
protected float pressTime;
|
||||
@@ -38,25 +38,25 @@ public class Button extends Component {
|
||||
|
||||
@Override
|
||||
protected void createChildren() {
|
||||
hotArea = new TouchArea( 0, 0, 0, 0 ) {
|
||||
hotArea = new PointerArea( 0, 0, 0, 0 ) {
|
||||
@Override
|
||||
protected void onTouchDown(Touch touch) {
|
||||
protected void onPointerDown( PointerEvent event ) {
|
||||
pressed = true;
|
||||
pressTime = 0;
|
||||
processed = false;
|
||||
Button.this.onTouchDown();
|
||||
};
|
||||
Button.this.onPointerDown();
|
||||
}
|
||||
@Override
|
||||
protected void onTouchUp(Touch touch) {
|
||||
protected void onPointerUp( PointerEvent event ) {
|
||||
pressed = false;
|
||||
Button.this.onTouchUp();
|
||||
};
|
||||
Button.this.onPointerUp();
|
||||
}
|
||||
@Override
|
||||
protected void onClick( Touch touch ) {
|
||||
protected void onClick( PointerEvent event ) {
|
||||
if (!processed) {
|
||||
Button.this.onClick();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
add( hotArea );
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class Button extends Component {
|
||||
|
||||
hotArea.reset();
|
||||
processed = true;
|
||||
onTouchUp();
|
||||
onPointerUp();
|
||||
|
||||
Game.vibrate( 50 );
|
||||
}
|
||||
@@ -82,13 +82,12 @@ public class Button extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
protected void onTouchDown() {};
|
||||
protected void onTouchUp() {};
|
||||
protected void onClick() {};
|
||||
|
||||
protected void onPointerDown() {}
|
||||
protected void onPointerUp() {}
|
||||
protected void onClick() {}
|
||||
protected boolean onLongClick() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void layout() {
|
||||
|
||||
@@ -25,9 +25,7 @@ import java.util.LinkedList;
|
||||
|
||||
public class Signal<T> {
|
||||
|
||||
private LinkedList<Listener<T>> listeners = new LinkedList<Signal.Listener<T>>();
|
||||
|
||||
private boolean canceled;
|
||||
private LinkedList<Listener<T>> listeners = new LinkedList<>();
|
||||
|
||||
private boolean stackMode;
|
||||
|
||||
@@ -70,13 +68,11 @@ public class Signal<T> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Listener<T>[] list = listeners.toArray( new Listener[0] );
|
||||
|
||||
canceled = false;
|
||||
|
||||
for (Listener<T> listener : list) {
|
||||
|
||||
if (listeners.contains(listener)) {
|
||||
listener.onSignal(t);
|
||||
if (canceled) {
|
||||
if (listener.onSignal(t)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -84,11 +80,8 @@ public class Signal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
canceled = true;
|
||||
}
|
||||
|
||||
public static interface Listener<T> {
|
||||
public void onSignal( T t );
|
||||
public interface Listener<T> {
|
||||
//return true if the signal has been handled
|
||||
boolean onSignal( T t );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user