| 1 |
# See: http://wiki.flightgear.org/MapStructure |
| 2 |
# This layer is currently being restructured to better support 1) styling, 2) LOD and 3) caching of instanced symbols |
| 3 |
# The corresponding APIs in MapStructure.nas and api.nas should probably be extended acccordingly |
| 4 |
# |
| 5 |
# We can look into adapting the other layers once we have a single use-case that works properly - including styling, LOD and caching - |
| 6 |
# at that point, the framework should have evolved sufficiently. |
| 7 |
# |
| 8 |
# It would probably be a good idea to incrementally port some other layers and provide the corresponding helpers/APIs, to reduce the amount |
| 9 |
# of specialized/identical code in the .symbol files. |
| 10 |
# |
| 11 |
|
| 12 |
|
| 13 |
# Class things: |
| 14 |
var name = 'DME'; |
| 15 |
var parents = [DotSym]; |
| 16 |
var __self__ = caller(0)[0]; |
| 17 |
DotSym.makeinstance( name, __self__ ); |
| 18 |
|
| 19 |
var element_type = "group"; # we want a group, becomes "me.element" |
| 20 |
|
| 21 |
var timer = nil; |
| 22 |
|
| 23 |
### |
| 24 |
# symbols (canvas groups) managed by this file |
| 25 |
# |
| 26 |
var icon_dme = nil; |
| 27 |
|
| 28 |
var scale_animation = 1; |
| 29 |
|
| 30 |
var animate = func { |
| 31 |
if (me.scale_animation >= 1) |
| 32 |
me.scale_animation -= .5; |
| 33 |
else |
| 34 |
me.scale_animation += .5; |
| 35 |
me.element.setScale( me.scale_animation ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
var del = func { |
| 40 |
# me.timer.stop(); |
| 41 |
} |
| 42 |
|
| 43 |
# CachedElement |
| 44 |
# StyleAttribute |
| 45 |
# Styleable |
| 46 |
# RangeAwareElement |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
# var DMEIcon = StyleableElement.new( [{color:IconColor}, ] ); |
| 51 |
|
| 52 |
### |
| 53 |
# FIXME: these should probably be part of MapStructure itself |
| 54 |
# TODO: Need to come up with a StyleableElement class for this sort of stuff |
| 55 |
var apply_styling = func { |
| 56 |
# add all styleable groups here |
| 57 |
|
| 58 |
# no need to do this whenever draw is called - typically, attributes won't change at all - so this is kinda redundant |
| 59 |
var current_color = me.icon_dme.getColor(); |
| 60 |
var required_color = nil; |
| 61 |
|
| 62 |
if (typeof(me.map.controller["is_tuned"]) == 'func' and me.map.controller.is_tuned(me.model.frequency/100)) |
| 63 |
#TODO: once we support caching/instancing, we cannot just change the symbol like this - we need to use a different symbol from the cache instead |
| 64 |
# which is why these things need to be done ONCE during init to set up a cache entry for each symbol variation to come up with a corresponding raster image |
| 65 |
# TODO: API-wise it would make sense to maintain a vector of required keys, so that the style hash can be validated in the ctor of the layer |
| 66 |
# to ensure that all mandatory fields are supplied |
| 67 |
required_color = canvas._getColor( me.layer.style.color_tuned); |
| 68 |
else |
| 69 |
required_color = canvas._getColor( me.layer.style.color_default); |
| 70 |
|
| 71 |
if (current_color != required_color) { |
| 72 |
# print("Setting color!"); |
| 73 |
# TODO: this is where we would select another cached symbol |
| 74 |
me.icon_dme.setColor( required_color ); |
| 75 |
} |
| 76 |
# else print("Not changing color (unnecessary)"); |
| 77 |
|
| 78 |
# debug.dump(me.layer.style); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
### |
| 83 |
# NOTE: This is only applied during initialization |
| 84 |
# TODO: expose via addLayer/style param |
| 85 |
# TODO: also needs to be aware of current range, so that proper LOD handling can be implemented |
| 86 |
var apply_scale = func { |
| 87 |
# add all symbols here that need scaling |
| 88 |
me.icon_dme.setScale( me.layer.style.scale_factor ); |
| 89 |
} |
| 90 |
|
| 91 |
### |
| 92 |
# draw routines must be called here to create a lookup table |
| 93 |
# with different symbols, based on styling (colors etc) |
| 94 |
# because we can no longer change instanced symbols later on |
| 95 |
# as they will be shared, so we need one cache entry for each |
| 96 |
# variation in style |
| 97 |
var init_cache = func { |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
## |
| 102 |
# init is done separately to prepare support for caching (instanced symbols) |
| 103 |
# |
| 104 |
var init = func { |
| 105 |
# debug.dump(me.layer.style); |
| 106 |
var draw_func = me.getOption('draw_dme'); |
| 107 |
if(typeof(draw_func) == 'func'){ |
| 108 |
me.icon_dme = draw_func(me); |
| 109 |
} else { |
| 110 |
me.icon_dme = me.element.createChild("path") |
| 111 |
.moveTo(-15,0) |
| 112 |
.line(-12.5,-7.5) |
| 113 |
.line(7.5,-12.5) |
| 114 |
.line(12.5,7.5) |
| 115 |
.lineTo(7.5,-12.5) |
| 116 |
.line(12.5,-7.5) |
| 117 |
.line(7.5,12.5) |
| 118 |
.line(-12.5,7.5) |
| 119 |
.lineTo(15,0) |
| 120 |
.lineTo(7.5,12.5) |
| 121 |
.vert(14.5) |
| 122 |
.horiz(-14.5) |
| 123 |
.vert(-14.5) |
| 124 |
.close() |
| 125 |
.setStrokeLineWidth( me.layer.style.line_width ); |
| 126 |
} |
| 127 |
|
| 128 |
# finally scale the symbol as requested, this is done last so that people can override this when creating the layer |
| 129 |
me.apply_scale(); |
| 130 |
if (me.layer.style.animation_test) { |
| 131 |
me.timer = maketimer(0.33, func me.animate() ); |
| 132 |
me.timer.start(); |
| 133 |
} |
| 134 |
if(me.getOption('draw_text', 0)){ |
| 135 |
me.dme_text = me.element.createChild("text") |
| 136 |
.setDrawMode( canvas.Text.TEXT ) |
| 137 |
.setText(me.model.id) |
| 138 |
.setFont("LiberationFonts/LiberationSans-Regular.ttf") |
| 139 |
.setFontSize(28) |
| 140 |
.setColor(me.getStyle('text_color', [1,1,1])) |
| 141 |
.setTranslation(me.getStyle('translation', [45,25])); |
| 142 |
} |
| 143 |
me.draw(); |
| 144 |
} |
| 145 |
|
| 146 |
## |
| 147 |
# this method is REQUIRED |
| 148 |
# check if the current station is tuned or not - and style the symbol accordingly (color) |
| 149 |
var draw = func { |
| 150 |
me.apply_styling(); |
| 151 |
}; |
| 152 |
|