Worldmaker

Drawing & Layers

Reimplementing the Layers Pane was easier than I expected. I had a lot of work done from before and was able to copy most of the code directly from the previous iteration.

The major difference for layers this time comes down to code structure. Instead of sending events to the Map class, this time the Layers Pane and the Map pane both directly access the layers ArrayCollection that's created and stored in the TaskManager's currentTask. Since it's a singleton, there's no difficulty keeping the Map and Layers Pane in sync.

The TaskManager itself keeps tabs on the ArrayCollection and dispatches events through the Signal singleton letting any listener to the Signal class know if a change to the Layers has occurred.

Each task only initially stores the unique IDs of the layers that are to be displayed for that task. The actual Layer objects are initialized when the resetLayers() function is called (at the end of each Task class constructor). This function is also called when the layers are reset (returned to their original visibility and arrangement).

public function resetLayers():void
{
layers.removeAll();
for each (var l:String in _layerIds)
{
var layer:Layer = new Layer();
layer.id = l;
layer.visible = true;
layer.allowed = true;
layers.addItem(layer);
}
}
Finally, a functional layers pane for the new build

Drawing the map itself takes on a different approach as well. The onDraw(event) function contains a switch by layer id, and loops over each layer in the currentTask's Layers ArrayCollection. I decided to separate the act of drawing each layer into individual functions dedicated to that particular layer's visualization.

for each (var layer:Layer in layers)
{
if (!layer.visible)
continue;

switch (layer.id)
{
case Layer.POINTS:
drawPoints();
break;
case Layer.VORONOI:
drawVoronoi();
break;
case Layer.DELAUNAY:
drawDelaunay();
break;
case Layer.TECTONIC_PLATES:
drawTectonics();
break;
}
}

If a new layer is to be added to some task, the easy scalability of this approach makes it very straightforward to expand tasks without needing to delve deep into existing code.