push gamepad support for textlistctrl

This commit is contained in:
RandomityGuy 2025-06-17 23:03:28 +05:30
parent bb47690dc0
commit 7d0c0ac096
2 changed files with 102 additions and 0 deletions

View file

@ -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<String>;
@ -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<String>, 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;
}
}

View file

@ -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<String>, 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;
}
}