Debian Codegraph @

Nasal/canvas/PFD/DataEntryElement.nas

raw
1 # PFD DataEntryElement - Data Entry UI Element.
2 #
3 # Uses the FMS Knobs to enter a text value with a given length and character set.
4 #
5 # To use, there must be an element called [PageName][ElementName] which will
6 # be used for the complete string, and a set of [PageName][ElementName]{0...n}
7 # elements, each consisting of a single character for input.
8 #
9 var DataEntryElement =
10 {
11 new : func (pagename, svg, name, value, size, charSet, style=nil)
12 {
13 var obj = {
14 parents : [ DataEntryElement, PFD.UIElement ],
15 _name : pagename ~ name,
16 _size : size,
17 _charSet : charSet,
18 _dataEntryPos : -1,
19 _dataEntrySymbol : [],
20 _style : style,
21 };
22
23 if (style == nil) obj._style = PFD.DefaultStyle;
24
25 obj._symbol = svg.getElementById(obj._name);
26 assert(obj._symbol != nil, "Unable to find element" ~ obj._name);
27 obj.setValue(value);
28
29 for (var i = 0; i < size; i = i + 1) {
30 var nodeName = obj._name ~ i;
31 append(obj._dataEntrySymbol, svg.getElementById(nodeName));
32 assert(obj._dataEntrySymbol[i] != nil, "Unable to find element " ~ nodeName);
33 obj._dataEntrySymbol[i].setVisible(0);
34 }
35
36 # State and timer for flashing highlighting of elements
37 obj._highlightEnabled = 0;
38 obj._flash = 0;
39 obj._highlightCharEnabled = 0;
40 obj._flashChar = 0;
41
42 return obj;
43 },
44
45 getName : func() { return me._name; },
46 getValue : func() { return me._symbol.getText(); },
47 setValue : func(value) { me._symbol.setText(value); },
48 setVisible : func(vis) {
49 me._symbol.setVisible(vis);
50
51 # Only ever hide the character entry symbols, as they are displayed
52 # only when editing
53 if (vis == 0) {
54 for (var i = 0; i < me._size; i = i + 1) me._dataEntrySymbol[i].setVisible(0);
55 }
56 },
57
58 _flashElement : func() {
59 if ((me._highlightEnabled == 1) and (me._highlightCharEnabled == 0)) {
60 if (me._flash == 0) {
61 me._symbol.setDrawMode(canvas.Text.TEXT + canvas.Text.FILLEDBOUNDINGBOX);
62 me._symbol.setColorFill(me._style.HIGHLIGHT_COLOR);
63 me._symbol.setColor(me._style.HIGHLIGHT_TEXT_COLOR);
64 me._flash = 1;
65 } else {
66 me._symbol.setDrawMode(canvas.Text.TEXT);
67 me._symbol.setColor(me._style.NORMAL_TEXT_COLOR);
68 me._flash = 0;
69 }
70 }
71
72 if (me._highlightCharEnabled == 1) {
73 if (me._flashChar == 0) {
74 me._dataEntrySymbol[me._dataEntryPos].setDrawMode(canvas.Text.TEXT + canvas.Text.FILLEDBOUNDINGBOX);
75 me._dataEntrySymbol[me._dataEntryPos].setColorFill(me._style.HIGHLIGHT_COLOR);
76 me._dataEntrySymbol[me._dataEntryPos].setColor(me._style.HIGHLIGHT_TEXT_COLOR);
77 me._flashChar = 1;
78 } else {
79 me._dataEntrySymbol[me._dataEntryPos].setDrawMode(canvas.Text.TEXT);
80 me._dataEntrySymbol[me._dataEntryPos].setColor(me._style.NORMAL_TEXT_COLOR);
81 me._flashChar = 0;
82 }
83 }
84 },
85 highlightElement : func() {
86 me._highlightEnabled = 1;
87 me._flash = 0;
88 PFD.HighlightTimer.startHighlight(me, -1);
89 },
90 unhighlightElement : func() {
91 me._highlightEnabled = 0;
92 me._symbol.setDrawMode(canvas.Text.TEXT);
93 me._symbol.setColor(me._style.NORMAL_TEXT_COLOR);
94 PFD.HighlightTimer.stopHighlight(me);
95 },
96
97 _highlightCharElement : func() {
98 me._highlightCharEnabled = 1;
99 me._flashChar = 0;
100 },
101 _unhighlightCharElement : func() {
102 me._highlightCharEnabled = 0;
103 me._dataEntrySymbol[me._dataEntryPos].setDrawMode(canvas.Text.TEXT);
104 me._dataEntrySymbol[me._dataEntryPos].setColor(me._style.NORMAL_TEXT_COLOR);
105 },
106
107 isEditable : func () { return 1; },
108 isInEdit : func() { return (me._dataEntryPos != -1); },
109 isHighlighted : func() { return me._highlightEnabled; },
110
111 enterElement : func() {
112 # Handle pressing enter to confirm this element.
113 # - Hiding the per-character entry fields
114 # - concatenating the data that's been entered into a string
115 # - displaying the string in the (highlighted) top level element
116 #
117 # Also pass back the string to the caller.
118
119 var val = "";
120
121 for (var i = 0; i < me._size; i = i + 1) {
122 if (me._dataEntrySymbol[i].getText() != "_") {
123 val = val ~ me._dataEntrySymbol[i].getText();
124 }
125 me._dataEntrySymbol[i].setVisible(0);
126 }
127
128 me._symbol.setText(val);
129 me._symbol.setVisible(1);
130 me.highlightElement();
131 me._dataEntryPos = -1;
132 return val;
133 },
134 clearElement : func() {
135 # Cancel editing this element by
136 # - Hiding the per-character entry fields
137 # - Highlighting the top level element
138
139 for (var i = 0; i < me._size; i = i + 1) {
140 me._dataEntrySymbol[i].setVisible(0);
141 }
142
143 me._symbol.setVisible(1);
144 me.highlightElement();
145 me._dataEntryPos = -1;
146 },
147 _startEdit: func() {
148 # Start editing by hiding the top level element, and displaying and
149 # resetting the character entry fields.
150 me._dataEntryPos = 0;
151
152 me._symbol.setVisible(0);
153
154 for (var i = 0; i < me._size; i = i + 1) {
155 me._dataEntrySymbol[i].setText("_");
156 me._dataEntrySymbol[i].setVisible(1);
157 }
158
159 # Highlight the first character element to indicate we're editing it
160 me._highlightCharElement();
161 },
162 keyPress: func(value) {
163 if (me._dataEntryPos == -1) {
164 me._startEdit();
165 }
166 var charSym = me._dataEntrySymbol[me._dataEntryPos];
167 charSym.setText(value);
168
169 if ( me._dataEntryPos == me._size -1 ) return;
170
171 me._unhighlightCharElement();
172 me._dataEntryPos = me._dataEntryPos + 1;
173 me._highlightCharElement();
174 },
175 incrSmall : func(value) {
176 # Change the value of this element, or start editing it if we're not already
177 # doing so.
178
179 if (me._dataEntryPos == -1) {
180 me._startEdit();
181 } else {
182 var charSym = me._dataEntrySymbol[me._dataEntryPos];
183 var incr_or_decr = (value > 0) ? 1 : -1;
184
185 # Change the value of the data element
186 var val = charSym.getText();
187
188 if (val == "_") {
189 # Not previously set, so set to the first or last characterset entry
190 # depending on direction
191 if (incr_or_decr > 0) {
192 charSym.setText(chr(me._charSet[0]));
193 } else {
194 charSym.setText(chr(me._charSet[size(me._charSet) -1]));
195 }
196 } else {
197 var curIdx = find(val, me._charSet);
198
199 if (curIdx == -1) die("Failed to find character " ~ val ~ " in dataEntryElement " ~ element);
200 curIdx = math.mod(curIdx + incr_or_decr, size(me._charSet));
201 charSym.setText(chr(me._charSet[curIdx]));
202 }
203 }
204 },
205 incrLarge : func(value) {
206 # Change the cursor position within a data element
207 var incr_or_decr = (value > 0) ? 1 : -1;
208
209 if ((me._dataEntryPos == 0) and (incr_or_decr == -1)) return; # Don't scroll off the start
210 if ((me._dataEntryPos == me._size -1) and (incr_or_decr == 1)) return; # Don't scroll off the end
211
212 me._unhighlightCharElement();
213 me._dataEntryPos = me._dataEntryPos + incr_or_decr;
214 me._highlightCharElement();
215 },
216 };