[client] use touch events in GamepadView
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.tankz;
|
package ru.m.tankz;
|
||||||
|
|
||||||
|
import lime.ui.Joystick;
|
||||||
|
import lime.ui.Gamepad;
|
||||||
import haxework.animate.FadeAnimate;
|
import haxework.animate.FadeAnimate;
|
||||||
import haxework.animate.UnFadeAnimate;
|
import haxework.animate.UnFadeAnimate;
|
||||||
import haxework.resources.IResources;
|
import haxework.resources.IResources;
|
||||||
@@ -62,5 +64,19 @@ class Init {
|
|||||||
|
|
||||||
connection = buildConnection();
|
connection = buildConnection();
|
||||||
networkManager = new NetworkManager();
|
networkManager = new NetworkManager();
|
||||||
|
|
||||||
|
for (device in Gamepad.devices) {
|
||||||
|
trace('gamepad', device);
|
||||||
|
}
|
||||||
|
Gamepad.onConnect.add(function(device) {
|
||||||
|
trace('connect gamepad', device);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (device in Joystick.devices) {
|
||||||
|
trace('joystick', device);
|
||||||
|
}
|
||||||
|
Joystick.onConnect.add(function(device) {
|
||||||
|
trace('connect joystick', device);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package ru.m.tankz.view;
|
package ru.m.tankz.view;
|
||||||
|
|
||||||
import flash.display.Graphics;
|
import flash.display.Graphics;
|
||||||
import flash.events.MouseEvent;
|
import flash.events.TouchEvent;
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import haxework.view.skin.ISkin;
|
import haxework.view.skin.ISkin;
|
||||||
import haxework.view.SpriteView;
|
import haxework.view.SpriteView;
|
||||||
@@ -49,51 +49,56 @@ class GamepadView extends SpriteView {
|
|||||||
public var actionSignal(default, null):Signal2<GamepadAction, Bool>;
|
public var actionSignal(default, null):Signal2<GamepadAction, Bool>;
|
||||||
|
|
||||||
public var areas(default, null):Array<ActionArea>;
|
public var areas(default, null):Array<ActionArea>;
|
||||||
public var currentArea(default, null):ActionArea;
|
public var currentAreas(default, null):Map<Int, ActionArea>;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
|
areas = [];
|
||||||
|
currentAreas = new Map();
|
||||||
actionSignal = new Signal2();
|
actionSignal = new Signal2();
|
||||||
skin = [new GamepadSkin(0x00ff00)];
|
skin = [new GamepadSkin(0x00ff00)];
|
||||||
content.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
content.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onMouseDown(event:MouseEvent):Void {
|
private function onTouchBegin(event:TouchEvent):Void {
|
||||||
onMouseMove(event);
|
onTouchMove(event);
|
||||||
content.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
content.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
|
||||||
content.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
content.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onMouseMove(event:MouseEvent):Void {
|
private function onTouchMove(event:TouchEvent):Void {
|
||||||
var point = new Point(event.localX, event.localY);
|
var point = new Point(event.localX, event.localY);
|
||||||
if (currentArea != null) {
|
if (currentAreas.exists(event.touchPointID)) {
|
||||||
if (!currentArea.rect.contain(point)) {
|
var area = currentAreas[event.touchPointID];
|
||||||
actionSignal.emit(currentArea.action, false);
|
if (!area.rect.contain(point)) {
|
||||||
currentArea = null;
|
currentAreas.remove(event.touchPointID);
|
||||||
|
actionSignal.emit(area.action, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (area in areas) {
|
for (area in areas) {
|
||||||
if (area.rect.contain(point)) {
|
if (area.rect.contain(point)) {
|
||||||
currentArea = area;
|
currentAreas[event.touchPointID] = area;
|
||||||
actionSignal.emit(currentArea.action, true);
|
actionSignal.emit(area.action, true);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onMouseUp(event:MouseEvent):Void {
|
private function onTouchEnd(event:TouchEvent):Void {
|
||||||
content.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
if (currentAreas.exists(event.touchPointID)) {
|
||||||
content.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
actionSignal.emit(currentAreas[event.touchPointID].action, false);
|
||||||
if (currentArea != null) {
|
currentAreas.remove(event.touchPointID);
|
||||||
actionSignal.emit(currentArea.action, false);
|
}
|
||||||
currentArea = null;
|
if (Lambda.count(currentAreas) == 0) {
|
||||||
|
content.stage.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
|
||||||
|
content.stage.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function update():Void {
|
override public function update():Void {
|
||||||
super.update();
|
super.update();
|
||||||
areas = [];
|
areas = [];
|
||||||
var size = Math.min(width, height) / 8;
|
var size = Math.min(width, height) / 7;
|
||||||
var padding = size / 2;
|
var padding = size / 2;
|
||||||
areas.push({
|
areas.push({
|
||||||
action: GamepadAction.UP,
|
action: GamepadAction.UP,
|
||||||
|
|||||||
Reference in New Issue
Block a user