From 7d0c0ac09653e05aafe38f10687eab04433f32a1 Mon Sep 17 00:00:00 2001 From: RandomityGuy <31925790+RandomityGuy@users.noreply.github.com> Date: Tue, 17 Jun 2025 23:03:28 +0530 Subject: [PATCH] push gamepad support for textlistctrl --- src/gui/GuiMLTextListCtrl.hx | 51 ++++++++++++++++++++++++++++++++++++ src/gui/GuiTextListCtrl.hx | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/gui/GuiMLTextListCtrl.hx b/src/gui/GuiMLTextListCtrl.hx index b82aba62..c018c040 100644 --- a/src/gui/GuiMLTextListCtrl.hx +++ b/src/gui/GuiMLTextListCtrl.hx @@ -1,5 +1,6 @@ package gui; +import hxd.Key; import h2d.filter.Filter; import h2d.HtmlText; import h2d.Flow; @@ -15,6 +16,7 @@ import h2d.Text; import h2d.Font; import src.MarbleGame; import src.Settings; +import src.Gamepad; class GuiMLTextListCtrl extends GuiControl { public var texts:Array; @@ -39,6 +41,8 @@ class GuiMLTextListCtrl extends GuiControl { var flow:Flow; var _imageLoader:String->Tile; + var usedGamepad:Bool = false; + public function new(font:Font, texts:Array, imageLoader:String->Tile, ?filter:Filter = null) { super(); this.font = font; @@ -282,4 +286,51 @@ class GuiMLTextListCtrl extends GuiControl { } redrawSelectionRect(hittestrect); } + + public override function update(dt:Float, mouseState:MouseState) { + super.update(dt, mouseState); + + var ps = _prevSelected; + + if (Key.isPressed(Key.DOWN) || Gamepad.isPressed(["dpadDown"]) || (Gamepad.getAxis('analogY') > 0.75 && !usedGamepad)) { + _prevSelected++; + if (_prevSelected >= this.texts.length) { + _prevSelected = 0; + } + } + if (Key.isPressed(Key.UP) || Gamepad.isPressed(["dpadUp"]) || (Gamepad.getAxis('analogY') < -0.75 && !usedGamepad)) { + _prevSelected--; + if (_prevSelected < 0) { + _prevSelected = this.texts.length - 1; + } + } + + if (ps != _prevSelected) { + // check if we need to scroll + var y = 2 * Settings.uiScale + (_prevSelected * (font.size + 4 * Settings.uiScale)) + g.y; + + var renderRect = this.getRenderRectangle(); + redrawSelectionRect(renderRect); + if (onSelectedFunc != null) { + onSelectedFunc(_prevSelected); + } + + var hittestrect = this.getHitTestRect(false); + + if (y < 0) { + // Scroll up + this.scroll = (font.size + 4 * Settings.uiScale) * _prevSelected; + this.onScroll(0, this.scroll); + } else if (y + font.size + 4 * Settings.uiScale > hittestrect.extent.y) { + // Scroll down + this.scroll = (font.size + 4 * Settings.uiScale) * _prevSelected; + this.onScroll(0, this.scroll); + } + } + + if (Math.abs(Gamepad.getAxis('analogY')) > 0.75) + usedGamepad = true; + else + usedGamepad = false; + } } diff --git a/src/gui/GuiTextListCtrl.hx b/src/gui/GuiTextListCtrl.hx index b1373f02..eca45994 100644 --- a/src/gui/GuiTextListCtrl.hx +++ b/src/gui/GuiTextListCtrl.hx @@ -1,5 +1,7 @@ package gui; +import src.Gamepad; +import hxd.Key; import h2d.Flow; import h3d.Engine; import h2d.Tile; @@ -33,6 +35,8 @@ class GuiTextListCtrl extends GuiControl { public var scrollable:Bool = false; + var usedGamepad:Bool = false; + var flow:Flow; public function new(font:Font, texts:Array, textColor:Int = 0) { @@ -270,4 +274,51 @@ class GuiTextListCtrl extends GuiControl { } redrawSelectionRect(hittestrect); } + + public override function update(dt:Float, mouseState:MouseState) { + super.update(dt, mouseState); + + var ps = _prevSelected; + + if (Key.isPressed(Key.DOWN) || Gamepad.isPressed(["dpadDown"]) || (Gamepad.getAxis('analogY') > 0.75 && !usedGamepad)) { + _prevSelected++; + if (_prevSelected >= this.texts.length) { + _prevSelected = 0; + } + } + if (Key.isPressed(Key.UP) || Gamepad.isPressed(["dpadUp"]) || (Gamepad.getAxis('analogY') < -0.75 && !usedGamepad)) { + _prevSelected--; + if (_prevSelected < 0) { + _prevSelected = this.texts.length - 1; + } + } + + if (ps != _prevSelected) { + // check if we need to scroll + var y = 2 * Settings.uiScale + (_prevSelected * (font.size + 4 * Settings.uiScale)) + g.y; + + var renderRect = this.getRenderRectangle(); + redrawSelectionRect(renderRect); + if (onSelectedFunc != null) { + onSelectedFunc(_prevSelected); + } + + var hittestrect = this.getHitTestRect(false); + + if (y < 0) { + // Scroll up + this.scroll = (font.size + 4 * Settings.uiScale) * _prevSelected; + this.onScroll(0, this.scroll); + } else if (y + font.size + 4 * Settings.uiScale > hittestrect.extent.y) { + // Scroll down + this.scroll = (font.size + 4 * Settings.uiScale) * _prevSelected; + this.onScroll(0, this.scroll); + } + } + + if (Math.abs(Gamepad.getAxis('analogY')) > 0.75) + usedGamepad = true; + else + usedGamepad = false; + } }