aboutsummaryrefslogtreecommitdiff
path: root/src/ui/textbox.moon
diff options
context:
space:
mode:
authorAlex Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
committerAlexander M Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
commit3a975db66a3711f34e8b64bb27a8eaca79fdeca9 (patch)
treefcc12f8f9d638ff575c1963796de76b7628854b4 /src/ui/textbox.moon
downloadggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.gz
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.bz2
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.zip
Initial commitHEADmaster
Diffstat (limited to 'src/ui/textbox.moon')
-rw-r--r--src/ui/textbox.moon78
1 files changed, 78 insertions, 0 deletions
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