From 3a975db66a3711f34e8b64bb27a8eaca79fdeca9 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Sun, 1 Feb 2026 13:14:32 -0600 Subject: Initial commit --- src/ui/textbox.moon | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/ui/textbox.moon (limited to 'src/ui/textbox.moon') diff --git a/src/ui/textbox.moon b/src/ui/textbox.moon new file mode 100644 index 0000000..38c6d84 --- /dev/null +++ b/src/ui/textbox.moon @@ -0,0 +1,78 @@ + +color = require("color") +Button = require("ui.button") +am.eval_js(require("textbox_bridge")) + +valid_chars = "abcdefghijklmnopqrstuvwxyz" +shifted_nums = "!@#$%^&*()" +i = 0 +class Textbox extends Button + new: (x,y,w,h,value,placeholder) => + super(x,y,w,h,value) + @id = i + i = i + 1 + args = am.to_json({ + name: value or "" + placeholder: placeholder or "" + }) + --am.eval_js("window.amulet.window_has_focus = 0;") + am.eval_js("window.TEXTBOX.create_textbox(" .. args .. ");") + if value == "" + @text.text = placeholder + --@text.color = color.am_color.shadow + @cursor = am.group( + am.translate(@em,0)\append( + am.rect(0,0,@em/4,-@em,color.am_color.foreground) + )) + @cursor\action(() => + if not @should_hide + @hidden = math.floor(am.current_time! * 2) % 2 == 0 + else + @hidden = true + ) + @cursor.should_hide = true + @text\append(@cursor) + @cursor_pos = #@text.text + @update_cursor_pos! + @max_chars = math.huge + @cursor + down: () => + super! + --@cursor.should_hide = false + --@text.color = color.am_color.foreground + print("textbox down") + am.eval_js("window.TEXTBOX.focus(" .. am.to_json({id: @id}) .. ");") + up: () => + super! + print("Textbox up") + am.eval_js("window.TEXTBOX.blur(" .. am.to_json({id: @id}) .. ");") + val = am.eval_js("window.TEXTBOX.get_text(" .. am.to_json({id:@id}) .. ");") + print("Up, got val:", val) + --@cursor.should_hide = true + --@text.color = color.am_color.shadow + update_cursor_pos: () => + @.cursor("translate").x = @cursor_pos * 9 + fire: (e) => + if e.event == "mouse_down" + @down! + if @on + @on(e) + add_key = e.event == "keys_pressed" and @depressed + if add_key + for key in *e.data + if key == "kp_enter" or key == "enter" + if @on + @on(e) + elseif key == "escape" + @up! + + @update_cursor_pos! + text = @text.text + newtext = am.eval_js("window.TEXTBOX.get_text(" .. am.to_json({id:@id}) .. ");") + if newtext != text + @text.text = newtext + if @onchange + @onchange! + false + +Textbox -- cgit v1.2.3-70-g09d2