11
2+ -- base table structure
23Components .Base .InputField = {
34 PosX = 0 ,
45 PosY = 0 ,
56 Width = 50 ,
67 Value = " " ,
8+ Type = " string" ,
9+ Label = " " ,
10+ LabelWidth = 0 ,
711 Color = {0 , 0 , 0 },
812 _cursor_on = false
913}
1014
15+ -- object create
1116function Components .Base .InputField :new (o )
1217 o = o or {}
1318 setmetatable (o , self )
1419 self .__index = self
1520 return o
1621end
1722
18- function Components .Base .InputField :_touches (x , y )
19- local w , h = Lib .Gui .GetStringSize (self .Value )
20- if x >= self .PosX and x <= (self .PosX + self .Width ) then
21- if y >= self .PosY and y <= (self .PosY + h ) then
22- return true
23- end
24- end
25- return false
26- end
27-
23+ -- utils
2824function Components .Base .InputField :_get_cursor (focused )
2925 if not focused then return " " end
3026 if self ._cursor_on then return " |" end
@@ -42,30 +38,87 @@ function Components.Base.InputField:_get_draw_text(focused)
4238 return " ..." .. dt .. self :_get_cursor (focused )
4339end
4440
41+ function Components .Base .InputField :_get_label_width ()
42+ if self .Label == " " then
43+ return 0
44+ end
45+ if self .LabelWidth ~= 0 then
46+ return self .LabelWidth
47+ end
48+ local wl , _ = Lib .Gui .GetStringSize (self .Label .. " " )
49+ return wl
50+ end
51+
52+ function Components .Base .InputField :_draw_label (gc )
53+ if self .Label == " " then return end
54+ gc :drawString (self .Label .. " " , self .PosX , self .PosY , " top" )
55+ end
56+
57+ -- required functions
58+ function Components .Base .InputField :_touches (x , y )
59+ local _ , h = Lib .Gui .GetStringSize (self .Value )
60+ local wl = self :_get_label_width ()
61+ if x >= self .PosX and x <= (self .PosX + wl + self .Width ) then
62+ if y >= self .PosY and y <= (self .PosY + h ) then
63+ return true
64+ end
65+ end
66+ return false
67+ end
68+
4569function Components .Base .InputField :_draw (gc , focused )
4670 self ._cursor_on = not self ._cursor_on
71+
4772 local dt = self :_get_draw_text (focused )
48- local w , h = Lib .Gui .GetStringSize (dt )
73+
74+ local _ , h = Lib .Gui .GetStringSize (dt )
75+ local wl = self :_get_label_width ()
76+
4977 if not focused then
5078 gc :setColorRGB (unpack (Lib .Colors .Silver ))
51- gc :fillRect (self .PosX , self .PosY , self .Width , h )
79+ gc :fillRect (self .PosX + wl , self .PosY , self .Width , h )
5280 end
81+
5382 gc :setColorRGB (Lib .Colors .Parse (self .Color ))
54- gc :drawString (dt , self .PosX , self .PosY , " top" )
83+ self :_draw_label (gc )
84+ gc :drawString (dt , self .PosX + wl , self .PosY , " top" )
85+
5586 if h == 0 then
5687 h = 20
5788 end
58- gc :drawRect (self .PosX , self .PosY , self .Width , h )
89+ gc :drawRect (self .PosX + wl , self .PosY , self .Width , h )
5990 gc :setColorRGB (0 , 0 , 0 )
91+
6092 if focused then
61- Lib .Gui .DrawFocusBox (self .PosX , self .PosY , self .Width , h , gc )
93+ Lib .Gui .DrawFocusBox (self .PosX + wl , self .PosY , self .Width , h , gc )
6294 end
6395end
6496
6597function Components .Base .InputField :AcceptChar (c )
98+ if self .Type == " number" then
99+ if (not Lib .Internal .IsDigit (c )) and c ~= " ." and string.byte (c ) ~= 226 then return end
100+ if c == " ." and string.find (self .Value , " %." ) then return end
101+ if string.byte (c ) == 226 then
102+ if self .Value :sub (1 ,1 ) == " -" then
103+ self .Value = self .Value :sub (2 ,# (self .Value ))
104+ else
105+ self .Value = " -" .. self .Value
106+ end
107+ return
108+ end
109+ end
110+
66111 self .Value = self .Value .. c
112+
113+ if self .Type == " number" then
114+ self .Value = Lib .Internal .NormalizeNumber (self .Value )
115+ end
67116end
68117
69118function Components .Base .InputField :AcceptBackspace (c )
70119 self .Value = self .Value :sub (1 , # (self .Value )- 1 )
120+
121+ if self .Type == " number" then
122+ self .Value = Lib .Internal .NormalizeNumber (self .Value )
123+ end
71124end
0 commit comments