From 73822dc95adbca3f5fe1379b58847ddc0f02b94e Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 18 Jun 2019 20:47:04 +0300 Subject: [PATCH] [client] add JoystickDevice --- src/client/haxe/ru/m/control/ControlBus.hx | 15 ++++- src/client/haxe/ru/m/control/IControlBus.hx | 3 + .../haxe/ru/m/control/JoystickDevice.hx | 62 +++++++++++++++++++ src/client/haxe/ru/m/tankz/Init.hx | 15 ++--- .../haxe/ru/m/tankz/control/HumanControl.hx | 1 - src/client/haxe/ru/m/tankz/view/GameFrame.hx | 7 ++- .../m/tankz/view/settings/SettingsEditor.hx | 20 ++++-- 7 files changed, 107 insertions(+), 16 deletions(-) create mode 100644 src/client/haxe/ru/m/control/JoystickDevice.hx diff --git a/src/client/haxe/ru/m/control/ControlBus.hx b/src/client/haxe/ru/m/control/ControlBus.hx index cdbcdd9..2fc32d3 100644 --- a/src/client/haxe/ru/m/control/ControlBus.hx +++ b/src/client/haxe/ru/m/control/ControlBus.hx @@ -1,30 +1,43 @@ package ru.m.control; -import ru.m.control.IControlBus; import haxework.signal.Signal; +import ru.m.control.IControlBus; class ControlBus implements IControlBus { + private static inline var TAG = "control"; + + public var devices(default, null):Array; + public var signal(default, null):Signal3; + public var devicesSignal(default, null):Signal>; + private var connections:MapBool->Void>; public function new() { + devices = []; signal = new Signal3(); + devicesSignal = new Signal(); connections = new Map(); } public function connect(device:IControlDevice):Void { var connector = function(action:DeviceAction, on:Bool):Void { + //L.d("TAG", '${device.type}, ${action}, ${on}'); signal.emit(device.type, action, on); } connections.set(device.type, connector); device.signal.connect(connector); + devices.push(device); + devicesSignal.emit(devices); } public function disconnect(device:IControlDevice):Void { if (connections.exists(device.type)) { device.signal.disconnect(connections.get(device.type)); connections.remove(device.type); + devices.remove(device); + devicesSignal.emit(devices); } } } diff --git a/src/client/haxe/ru/m/control/IControlBus.hx b/src/client/haxe/ru/m/control/IControlBus.hx index 3757b9c..dee18bb 100644 --- a/src/client/haxe/ru/m/control/IControlBus.hx +++ b/src/client/haxe/ru/m/control/IControlBus.hx @@ -3,7 +3,10 @@ package ru.m.control; import haxework.signal.Signal; interface IControlBus { + public var devices(default, null):Array; + public var signal(default, null):Signal3; + public var devicesSignal(default, null):Signal>; public function connect(device:IControlDevice):Void; public function disconnect(device:IControlDevice):Void; diff --git a/src/client/haxe/ru/m/control/JoystickDevice.hx b/src/client/haxe/ru/m/control/JoystickDevice.hx new file mode 100644 index 0000000..8da1d42 --- /dev/null +++ b/src/client/haxe/ru/m/control/JoystickDevice.hx @@ -0,0 +1,62 @@ +package ru.m.control; + +import haxework.signal.Signal; +import lime.ui.Joystick; +import ru.m.control.DeviceAction; +import ru.m.control.DeviceType; +import ru.m.geom.Direction; + +class JoystickDevice implements IControlDevice { + + public var type(default, null):DeviceType; + public var signal(default, null):Signal2; + + private var joystick:Joystick; + private var axisState:Map; + + public function new(joystick:Joystick) { + this.joystick = joystick; + type = GAMEPAD(joystick.id); + signal = new Signal2(); + axisState = new Map(); + joystick.onButtonDown.add(onButtonDown); + joystick.onButtonUp.add(onButtonUp); + joystick.onAxisMove.add(onAxisMove); + } + + private function onButtonDown(code:Int):Void { + signal.emit(KEY(code), true); + } + + private function onButtonUp(code:Int):Void { + signal.emit(KEY(code), false); + } + + private function onAxisMove(axis:Int, position:Float):Void { + var direction:Direction = switch [axis, position] { + case [0, 1]: Direction.RIGHT; + case [0, -1]: Direction.LEFT; + case [1, 1]: Direction.BOTTOM; + case [1, -1]: Direction.TOP; + case _: null; + } + if (direction != null) { + axisState.set(axis, direction); + signal.emit(DIRECTION(direction), true); + } else if (position == 0) { + if (axisState.exists(axis)) { + signal.emit(DIRECTION(axisState.get(axis)), false); + axisState.remove(axis); + } + } + } + + public function dispose():Void { + if (joystick != null) { + joystick.onButtonDown.remove(onButtonDown); + joystick.onButtonUp.remove(onButtonUp); + joystick.onAxisMove.remove(onAxisMove); + joystick = null; + } + } +} diff --git a/src/client/haxe/ru/m/tankz/Init.hx b/src/client/haxe/ru/m/tankz/Init.hx index 3d2b080..87afe9e 100644 --- a/src/client/haxe/ru/m/tankz/Init.hx +++ b/src/client/haxe/ru/m/tankz/Init.hx @@ -1,17 +1,18 @@ package ru.m.tankz; import flash.Lib; -import ru.m.control.KeyboardDevice; -import ru.m.control.ControlBus; -import ru.m.control.IControlBus; -import lime.ui.Joystick; -import lime.ui.Gamepad; import haxework.animate.FadeAnimate; import haxework.animate.UnFadeAnimate; import haxework.resources.IResources; import haxework.resources.Resources; import haxework.view.popup.PopupManager; +import lime.ui.Gamepad; +import lime.ui.Joystick; import ru.m.connect.IConnection; +import ru.m.control.ControlBus; +import ru.m.control.IControlBus; +import ru.m.control.JoystickDevice; +import ru.m.control.KeyboardDevice; import ru.m.tankz.bundle.ConfigBundle; import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.bundle.ILevelBundle; @@ -81,10 +82,10 @@ class Init { }); for (device in Joystick.devices) { - trace('joystick', device); + bus.connect(new JoystickDevice(device)); } Joystick.onConnect.add(function(device) { - trace('connect joystick', device); + bus.connect(new JoystickDevice(device)); }); } } diff --git a/src/client/haxe/ru/m/tankz/control/HumanControl.hx b/src/client/haxe/ru/m/tankz/control/HumanControl.hx index 7557660..2690d7d 100644 --- a/src/client/haxe/ru/m/tankz/control/HumanControl.hx +++ b/src/client/haxe/ru/m/tankz/control/HumanControl.hx @@ -24,7 +24,6 @@ class HumanControl extends Control { public function new(playerId:PlayerId, controlIndex:Int) { super(playerId); config = storage.getActionConfig(controlIndex); - trace(controlIndex, config.device, config.binding); moveQueue = []; } diff --git a/src/client/haxe/ru/m/tankz/view/GameFrame.hx b/src/client/haxe/ru/m/tankz/view/GameFrame.hx index e7a0268..995cf24 100644 --- a/src/client/haxe/ru/m/tankz/view/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/GameFrame.hx @@ -34,8 +34,13 @@ import ru.m.tankz.view.GamepadView; @:provide var game:IGame; @:provide static var bus:IControlBus; + public function init():Void { + bus.connect(gamepad); + } + public function onShow():Void { gamepad.visible = false; + // ToDo: for (i in 0...1) { switch settings.getActionConfig(i).device { case GAMEPAD(GamepadView.ID): @@ -44,7 +49,6 @@ import ru.m.tankz.view.GamepadView; case _: } } - bus.connect(gamepad); gameView.type = game.type; soundManager.config = game.config; gameView.render.config = game.config; @@ -76,7 +80,6 @@ import ru.m.tankz.view.GamepadView; } public function onHide():Void { - bus.disconnect(gamepad); stop(); soundManager.stopAll(); } diff --git a/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx b/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx index 244e8be..2d214db 100644 --- a/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx +++ b/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx @@ -1,14 +1,15 @@ package ru.m.tankz.view.settings; import haxe.EnumTools.EnumValueTools; -import haxework.view.GroupView; import haxework.view.ButtonGroup; import haxework.view.DataView; +import haxework.view.GroupView; import haxework.view.LabelView; import haxework.view.VGroupView; import promhx.Promise; import ru.m.control.DeviceAction; import ru.m.control.DeviceType; +import ru.m.control.IControlBus; import ru.m.tankz.control.ActionConfig; import ru.m.tankz.storage.SettingsStorage; import ru.m.tankz.view.settings.ActionView; @@ -23,15 +24,24 @@ import ru.m.tankz.view.settings.ActionView; @:view var device:ButtonGroup; @:provide static var storage:SettingsStorage; + @:provide static var bus:IControlBus; - private function set_controlIndex(value: Int): Int { - this.controlIndex = value; + public function init() { + bus.devicesSignal.connect(function(devices) refresh()); + } + + private function refresh():Void { label.text = 'Player ${controlIndex+1}'; var config = storage.getActionConfig(controlIndex); list.data = bindingToArray(config.binding); - device.data = [KEYBOARD, GAMEPAD(GamepadView.ID)]; + device.data = bus.devices.map(function(device) return device.type); device.selected = config.device; panel.visible = list.visible = EnumValueTools.equals(config.device, KEYBOARD); + } + + private function set_controlIndex(value: Int): Int { + this.controlIndex = value; + refresh(); return this.controlIndex; } @@ -42,7 +52,7 @@ import ru.m.tankz.view.settings.ActionView; } panel.visible = list.visible = EnumValueTools.equals(config.device, KEYBOARD); storage.setActionConffig(controlIndex, config); - controlIndex = controlIndex; + refresh(); } private function viewFactory(index:Int, value:ActionItem) {