| 1 |
#------------------------------------------ |
| 2 |
# efis-canvas.nas - Canvas EFIS framework |
| 3 |
# author: jsb |
| 4 |
# created: 12/2017 |
| 5 |
#------------------------------------------ |
| 6 |
|
| 7 |
#-- EFISCanvas - base class to create canvas displays / pages -- |
| 8 |
# * manages a canvas |
| 9 |
# * can load a SVG file and create clipping from <name>_clip elements |
| 10 |
# * allows to register multiple update functions with individual update intervals |
| 11 |
# * update functions can be en-/disabled by a single property that should |
| 12 |
# reflect the visibility of the canvas |
| 13 |
# * several listener factories for common animations |
| 14 |
|
| 15 |
var EFISCanvas = { |
| 16 |
# static members |
| 17 |
_instances: [], |
| 18 |
unload: func() { |
| 19 |
print("-- Removing EFISCanvas instances --"); |
| 20 |
foreach (var instance; EFISCanvas._instances) { |
| 21 |
print(" - "~instance.name); |
| 22 |
instance.del(); |
| 23 |
} |
| 24 |
EFISCanvas._instances = []; |
| 25 |
}, |
| 26 |
|
| 27 |
# destructor |
| 28 |
del: func() { |
| 29 |
me._canvas.del(); |
| 30 |
foreach (var timer; me._timers) { |
| 31 |
timer.stop(); |
| 32 |
} |
| 33 |
me._timers = []; |
| 34 |
}, |
| 35 |
|
| 36 |
colors: canvas.colors, |
| 37 |
canvas_settings: EFIS.defaultcanvas_settings, |
| 38 |
|
| 39 |
new: func(name) { |
| 40 |
var obj = { |
| 41 |
parents: [me, canvas.SVGCanvas.new(name, me.canvas_settings)], |
| 42 |
_id: size(EFISCanvas._instances), # internal ID for EFIS window mgmt. |
| 43 |
id: 0, # instance id e.g. for PFD/MFD |
| 44 |
name: name, |
| 45 |
svg_keys: [], |
| 46 |
# for reload support while efis development |
| 47 |
_timers: [], |
| 48 |
updateN: nil, # to be used in update() to pause updates |
| 49 |
_instr_props: {}, |
| 50 |
}; |
| 51 |
append(EFISCanvas._instances, obj); |
| 52 |
var n = props.Node.makeValidPropName(name); |
| 53 |
obj.updateCountN = EFIS_root_node.getNode("update/count-"~n, 1); |
| 54 |
obj.updateCountN.setIntValue(0); |
| 55 |
obj.debugN = EFIS_root_node.getNode("debug/"~n, 1); |
| 56 |
obj.debugN.setBoolValue(0); |
| 57 |
return obj; |
| 58 |
}, |
| 59 |
|
| 60 |
|
| 61 |
#set node that en-/dis-ables canvas updates |
| 62 |
setUpdateN: func(n) { |
| 63 |
me.updateN = n; |
| 64 |
}, |
| 65 |
|
| 66 |
# register an update function with a certain update interval |
| 67 |
# f: function |
| 68 |
# f_me: if there is any "me" reference in "f", you can set "me" with this |
| 69 |
# defaults to EFISCanvas instance calling this method, useful if "f" |
| 70 |
# is a member of the EFISCanvas instance |
| 71 |
addUpdateFunction: func(f, interval, f_me = nil) { |
| 72 |
if (!isfunc(f)) { |
| 73 |
logprint(DEV_ALERT, "EFISCanvas.addUpdateFunction: argument is not a function."); |
| 74 |
return; |
| 75 |
} |
| 76 |
interval = num(interval); |
| 77 |
f_me = (f_me == nil) ? me : f_me; |
| 78 |
if (interval != nil and interval >= 0) { |
| 79 |
var timer = maketimer(interval, me, func { |
| 80 |
if (me.updateN != nil and me.updateN.getValue()) { |
| 81 |
var err = []; |
| 82 |
call(f, [], f_me, nil, err); |
| 83 |
if (size(err)) |
| 84 |
debug.printerror(err); |
| 85 |
# debug/performance monitoring |
| 86 |
me.updateCountN.increment(); |
| 87 |
} |
| 88 |
}); |
| 89 |
append(me._timers, timer); |
| 90 |
return timer; |
| 91 |
} |
| 92 |
}, |
| 93 |
|
| 94 |
# start all registered update functions |
| 95 |
startUpdates: func() { |
| 96 |
foreach (var t; me._timers) |
| 97 |
t.start(); |
| 98 |
}, |
| 99 |
|
| 100 |
# stop all registered update functions |
| 101 |
stopUpdates: func() { |
| 102 |
foreach (var t; me._timers) { |
| 103 |
t.stop(); |
| 104 |
} |
| 105 |
}, |
| 106 |
|
| 107 |
# getInstr - get props from /instrumentation/<sys>[i]/<prop> |
| 108 |
# creates prop node objects for efficient access |
| 109 |
# sys: the instrument name (path) (me.id is appended as index!!) |
| 110 |
# prop: the property(path) |
| 111 |
getInstr: func(sys, prop, default=0, id=nil) { |
| 112 |
if (me._instr_props[sys] == nil) |
| 113 |
me._instr_props[sys] = {}; |
| 114 |
if (me._instr_props[sys][prop] == nil) { |
| 115 |
if (id == nil) { id = me.id; } |
| 116 |
me._instr_props[sys][prop] = |
| 117 |
props.getNode("/instrumentation/"~sys~"["~id~"]/"~prop, 1); |
| 118 |
} |
| 119 |
var value = me._instr_props[sys][prop].getValue(); |
| 120 |
if (value != nil) return value; |
| 121 |
else return default; |
| 122 |
}, |
| 123 |
}; |