From b846aed5b9e4e1575146a4572e34554b9424705d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 31 Jul 2017 23:20:17 -0400 Subject: [PATCH] gamnit: virtual gamepad services return the created controls MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/asteronits/src/touch_ui.nit | 6 ++++-- lib/gamnit/virtual_gamepad/virtual_gamepad.nit | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/contrib/asteronits/src/touch_ui.nit b/contrib/asteronits/src/touch_ui.nit index bd8bfa5..5cae0d4 100644 --- a/contrib/asteronits/src/touch_ui.nit +++ b/contrib/asteronits/src/touch_ui.nit @@ -25,8 +25,10 @@ redef class App super var gamepad = new VirtualGamepad - gamepad.add_dpad - gamepad.controls.first.as(DPad).show_down = false + + var dpad = gamepad.add_dpad + if dpad != null then dpad.show_down = false + gamepad.add_button("space", gamepad_spritesheet.fire) gamepad.visible = true self.gamepad = gamepad diff --git a/lib/gamnit/virtual_gamepad/virtual_gamepad.nit b/lib/gamnit/virtual_gamepad/virtual_gamepad.nit index 2ffb84e..f5b6bec 100644 --- a/lib/gamnit/virtual_gamepad/virtual_gamepad.nit +++ b/lib/gamnit/virtual_gamepad/virtual_gamepad.nit @@ -58,7 +58,7 @@ import virtual_gamepad_spritesheet redef class App # Current touch gamepad, still may be invisible - var gamepad: nullable VirtualGamepad = null + var gamepad: nullable VirtualGamepad = null is writable # Textures used for `DPad` and available to clients var gamepad_spritesheet = new VirtualGamepadSpritesheet @@ -86,7 +86,7 @@ class VirtualGamepad # and `add_button`. var controls = new Array[RoundControl] - # Add a directional pad (`DPad`) to a default location + # Add and return a directional pad (`DPad`) to a default location # # The 4 buttons fire events with the corresponding name in `names`. # Items in `names` should be in order of top, left, down and right. @@ -101,17 +101,22 @@ class VirtualGamepad # added by `add_button`. # # Require: `names == null or names.length == 4` - fun add_dpad(names: nullable Array[String]) + fun add_dpad(names: nullable Array[String]): nullable DPad do if names == null then names = ["w","a","s","d"] assert names.length == 4 if n_dpads == 0 then - controls.add new DPad(app.ui_camera.bottom_left.offset(200.0, 100.0, 0.0), names) + var dpad = new DPad(app.ui_camera.bottom_left.offset(200.0, 100.0, 0.0), names) + controls.add dpad + return dpad else if n_dpads == 1 then - controls.add new DPad(app.ui_camera.bottom_right.offset(-200.0, 100.0, 0.0), names) + var dpad = new DPad(app.ui_camera.bottom_right.offset(-200.0, 100.0, 0.0), names) + controls.add dpad + return dpad else print_error "Too many DPad ({n_dpads}) in {self}" + return null end end @@ -132,7 +137,7 @@ class VirtualGamepad new Point[Float](-350.0, 350.0), new Point[Float](-350.0, 550.0)) - # Add a round button to a default location + # Add and return a round button to a default location # # Fired events use `name`, it should usually correspond to a # keyboard key like "space" or "a". @@ -144,7 +149,7 @@ class VirtualGamepad # # A maximum of 6 buttons may be added using this method when # there is less than 2 `DPad`. Otherwise, only 2 buttons can be added. - fun add_button(name: String, texture: Texture) + fun add_button(name: String, texture: Texture): nullable RoundButton do if n_dpads == 2 and button_positions.length == 6 then # Drop the bottom 4 buttons @@ -156,8 +161,10 @@ class VirtualGamepad assert button_positions.not_empty else print_error "Too many buttons in {self}" var pos = button_positions.shift - controls.add new RoundButton( + var but = new RoundButton( app.ui_camera.bottom_right.offset(pos.x, pos.y, 0.0), name, texture) + controls.add but + return but end private fun prepare -- 1.7.9.5