1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
|