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