Layer abstract class
This is the abstract base class for a layer in `dartkart.
DOM elements
-
the layer has a
div
as root element, see container with the CSS classdartkart-layer
and a uniqueid
.
abstract class Layer extends Object with PropertyObservable{ static var _layerIDCounter = 0; static get _nextLayerId => _layerIDCounter++; MapViewport _map; DivElement _container; int _nid; _defaultDOMId() => "dartkart-layer-$_nid"; String get _defaultName => "Layer $_nid"; /** * Creates a new layer. * * If present, [domId] is assigned to the `id` attribute of the layer * [container]. Otherwise, a new unique id is created. */ Layer([String domId]) { _nid = _nextLayerId; if (domId != null) domId = domId.trim(); if (domId == null || domId.isEmpty) { domId = _defaultDOMId(); } _container = new Element.tag("div") ..attributes["id"] = domId ..classes.add("dartkart-layer"); } /// the unique numeric id for this layer int get id => _nid; /// the unique layer id String get domId => _container.attributes["id"]; /** * Sets the DOM id on the layer [container]. * * If [value] is null or empty, sets a default id. */ void set domId(String value) { if (value != null) value = value.trim(); if (value == null || value.isEmpty) { value = _defaultDOMId(); } _container.attributes["id"] = value; } /// the map this layer is attached to, or [:null:] MapViewport get map => _map; /** * Attaches the layer to a map viewport [m]. * * Throws [StateError] if this layer is already attached. */ void attach(MapViewport m) { if (_map != null) { throw new StateError("layer is already attached"); } _map = m; } /** * Detaches this layer from the map viewport it is * currently attached to. */ void detach(){ _map = null; } // the [Element] this layer uses as container. DivElement get container => _container; /// renders the layer void render(); /* ------------------------ opacity ------------------------------- */ double _opacity = 1.0; /// the opacity of this layer double get opacity => _opacity; /// set the opacity of this layer. [value] is a [num] in the range /// (0.0 - 1.0). The lower the value, the more transparent the layer. void set opacity(num value) { value = math.max(0, value); value = math.min(value, 1); var oldvalue = _opacity; this._opacity = value.toDouble(); if (oldvalue != this._opacity) { notify("opacity", oldvalue, this._opacity); if (_map != null) { _map.render(); } } } /* ------------------------ name------------------------------- */ String _name; /// the layer name String get name { if (_name == null) return _defaultName; return _name; } /// sets the layer name. If [value] is null or consists of white space only, /// a default name is chosen. void set name(String value) { if (value != null) value = value.trim(); var old = _name; if (value == null || value.isEmpty) { _name = null; } else { _name = value; } notify("name", old, name); } /* ------------------------ visibility -------------------------- */ bool _visible = true; /// the layer visibility bool get visible => _visible; /// sets whether this layer is visible or not void set visible(bool value) { var old = _visible; if (value != old) { if (_container != null) { _container.style.visibility = value ? "visible" : "hidden"; } _visible = value; notify("visible", old, value); } } }
Extends
Object_PropertyObservable > Layer
Subclasses
Constructors
new Layer([String domId]) #
Creates a new layer.
If present,
domId is assigned to the id
attribute of the layer
container. Otherwise, a new unique id is created.
Layer([String domId]) { _nid = _nextLayerId; if (domId != null) domId = domId.trim(); if (domId == null || domId.isEmpty) { domId = _defaultDOMId(); } _container = new Element.tag("div") ..attributes["id"] = domId ..classes.add("dartkart-layer"); }
Properties
final DivElement container #
DivElement get container => _container;
void set domId(String value) #
Sets the DOM id on the layer container.
If value is null or empty, sets a default id.
void set domId(String value) { if (value != null) value = value.trim(); if (value == null || value.isEmpty) { value = _defaultDOMId(); } _container.attributes["id"] = value; }
final MapViewport map #
the map this layer is attached to, or null
MapViewport get map => _map;
String get name #
the layer name
String get name { if (_name == null) return _defaultName; return _name; }
void set name(String value) #
sets the layer name. If value is null or consists of white space only, a default name is chosen.
void set name(String value) { if (value != null) value = value.trim(); var old = _name; if (value == null || value.isEmpty) { _name = null; } else { _name = value; } notify("name", old, name); }
final Stream<PropertyChangeEvent> onPropertyChanged #
the stream of property change events
Example
// an observable with a mixed in PropertyObservable
var observable = ...;
// listen for property change events for the property
// 'my_property'
observable.onPropertyChanged
.where((evt) => evt.name == "my_property")
.listen((evt) => print("new value: ${evt.newValue}"));
Stream<PropertyChangeEvent> get onPropertyChanged { //TODO: fix this - if at least one listener is present, // change events are emitted, regardless of whether the // individual listeners are paused or not. Consequence: // lots of change events are possibly queued up in // paused listener streams. => need a custom implementation // of a multiplexing stream which disards events if // they are streamed to a disabled listener // if (_stream == null) { _stream = _controller.stream.asBroadcastStream(); } return _stream; }
void set opacity(num value) #
set the opacity of this layer.
value is a num
in the range
(0.0 - 1.0). The lower the value, the more transparent the layer.
void set opacity(num value) { value = math.max(0, value); value = math.min(value, 1); var oldvalue = _opacity; this._opacity = value.toDouble(); if (oldvalue != this._opacity) { notify("opacity", oldvalue, this._opacity); if (_map != null) { _map.render(); } } }
Methods
void attach(MapViewport m) #
Attaches the layer to a map viewport m.
Throws StateError
if this layer is already attached.
void attach(MapViewport m) { if (_map != null) { throw new StateError("layer is already attached"); } _map = m; }
void detach() #
Detaches this layer from the map viewport it is currently attached to.
void detach(){ _map = null; }
void notify(String property, oldValue, newValue) #
Notifies observers about an update of the property with name property in this object. oldValue was replaced by newValue.
Observers are only notified, provided newValue is different from oldValue and if there is at least one listener.
void notify(String property, oldValue, newValue) { if (oldValue == newValue) return; //TODO: fix me - see notes in onPropertyChanged if (!_controller.hasListener || _controller.isPaused) return; _controller.sink.add( new PropertyChangeEvent(this, property,oldValue,newValue) ); }
abstract void render() #
renders the layer