Debian Codegraph @

Nasal/canvas/PFD/PointerElement.nas

raw
1 # PFD UI Element - Pointer UI Element. Displays value by moving the element on a horizontal or vertical scale.
2 var PointerElement =
3 {
4 new : func (pagename, svg, name, minVal, maxVal, scalePx, vertical=0, value=0, style=nil)
5 {
6 var obj = {
7 parents : [ PointerElement, PFD.UIElement ],
8 _name : pagename ~ name,
9 _edit : 0,
10 _min : minVal,
11 _max : maxVal,
12 _scale : scalePx,
13 _vertical : vertical,
14 _style : style,
15 };
16
17 if (style == nil) obj._style = PFD.DefaultStyle;
18
19 obj._symbol = svg.getElementById(obj._name);
20 assert(obj._symbol != nil, "Unable to find element " ~ obj._name);
21 obj._baseTranslation = obj._symbol.getTranslation();
22 obj.setValue(value);
23
24 # State and timer for flashing highlighting of elements
25 # We need a separate Enabled flag as the timers are in a separate thread.
26 obj._highlightEnabled = 0;
27 obj._highlighted = 0;
28 obj._flashTimer = nil;
29
30 return obj;
31 },
32
33 getName : func() { return me._name; },
34 getValue : func() { return me._value; },
35 setValue : func(value) {
36 if (value == nil) value = 0.0;
37
38 # Bound value to the minimum and maximum values.
39 value = math.max(me._min, value);
40 value = math.min(me._max, value);
41
42 # Convert to normalized value
43 value = (value - me._min) / (me._max - me._min);
44
45 # Simply shift the slider along.
46 if (me._vertical) {
47 # Vertical
48 me._symbol.setTranslation([
49 me._baseTranslation[0],
50 me._baseTranslation[1] + (value * me._scale)
51 ]);
52 } else {
53 # Horizontal
54 me._symbol.setTranslation([
55 me._baseTranslation[0] + (value * me._scale),
56 me._baseTranslation[1]
57 ]);
58
59 }
60 },
61
62 setVisible : func(vis) { me._symbol.setVisible(vis); },
63 _flashElement : func() {
64 if (me._highlightEnabled == 0) {
65 me._symbol.setVisible(0);
66 me._highlighted = 0;
67 } else {
68 if (me._highlighted == 0) {
69 me._symbol.setVisible(1);
70 me._highlighted = 1;
71 } else {
72 me._symbol.setVisible(0);
73 me._highlighted = 0;
74 }
75 }
76 },
77 highlightElement : func() {
78 me._highlightEnabled = 1;
79 me._highlighted = 0;
80 me._flashElement();
81 me._flashTimer = maketimer(me._style.CURSOR_BLINK_PERIOD, me, me._flashElement);
82 me._flashTimer.start();
83 },
84 unhighlightElement : func() {
85 if (me._flashTimer != nil) me._flashTimer.stop();
86 me._flashTimer = nil;
87 me._highlightEnabled = 0;
88 me._highlighted = 0;
89 me._flashElement();
90 },
91 isEditable : func () { return 0; },
92 isInEdit : func() { return 0; },
93 enterElement : func() { return me.getValue(); },
94 clearElement : func() { },
95 editElement : func() { },
96 incrSmall : func(value) { },
97 incrLarge : func(value) { },
98 };