20,741
edits
| Line 107: | Line 107: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Once you have created your own "draw" implementation, you still need to have a data source or "provider" that determines for what data the callback needs to be invoked. This is currently still a little "hackish" and still work in progress. So we have just a very simple MVC model at the moment, whose interface needs to be implemented. | |||
For example, the various airport-specific "layers" all use the same "AirportModel" to be found in airport.model. In the future, other models can be found in *.model files. For a simple example of how to populate the model, just refer to the file "navaid.model", which is shown here: | |||
<syntaxhighlight lang="php"> | |||
var NavaidModel = {}; | |||
NavaidModel.new = func make(LayerModel, NavaidModel); | |||
NavaidModel.init = func { | |||
var navaids = findNavaidsWithinRange(50); | |||
foreach(var n; navaids) | |||
me.push(n); | |||
me.notifyView(); | |||
} | |||
</syntaxhighlight> | |||
As can be seen, each "Model" needs to derive from the "LayerModel" class. At the moment, the only method that needs to be implemented is the "init" method, which is invoked once the Layer is re-initialized. In this case, the init() method merely runs findNavaidsWithinRange(50); and then populates the model by appending each navaid to the MVC model in the top-level "LayerModel". Afterwards, the "notifyView" method is invoked to update the view (this will probably change pretty soon, once a real MVC controller abstraction is added). | |||
Now, to actually make a new layer known to the system, we need to add another file that registers the layer. For an example of how to do this, please see navaids.layer: | |||
<syntaxhighlight lang="php"> | |||
var NavLayer = {}; | |||
NavLayer.new = func(group,name) { | |||
var m=Layer.new(group, name, NavaidModel); | |||
m.setDraw (func draw_layer(layer:m, callback: MAP_LAYERS["navaids"], lod:0) ); | |||
return m; | |||
} | |||
register_layer("navaids", NavLayer); | |||
</syntaxhighlight> | |||
A new layer hash is created by returning a new Layer object via "Layer.new" which derives from the corresponding model (NavaidModel in this case), and setting the draw callback to the draw routine that we created earlier, in this case using the MAP_LAYERS hash - which, currently, needs to be extended in map.nas (but which will soon be changed such that it automatically loads all *.layer files). | |||
The layer is registered at the end of the file using the "register_layer" call and passing a symbolic/lookup name, and the name of the hash that implements the layer. | |||