1
0
Fork 0

add getTranslation and setGeoPosition to transform.nas, adapt element.nas accordingly;

improve readability and comments
This commit is contained in:
Henning Stahlke 2020-04-28 14:08:21 +02:00 committed by James Turner
parent 2eb4c78794
commit e996f4c8c6
2 changed files with 30 additions and 14 deletions

View file

@ -13,10 +13,11 @@ var Element = {
# #
# @param ghost Element ghost as retrieved from core methods # @param ghost Element ghost as retrieved from core methods
new: func(ghost) { new: func(ghost) {
return { var obj = {
parents: [PropertyElement, Element, ghost], parents: [Element, PropertyElement, ghost],
_node: props.wrapNode(ghost._node_ghost) _node: props.wrapNode(ghost._node_ghost),
}; };
return obj;
}, },
# Get parent group/element # Get parent group/element
@ -26,7 +27,7 @@ var Element = {
return nil; return nil;
var type = props.wrapNode(parent_ghost._node_ghost).getName(); var type = props.wrapNode(parent_ghost._node_ghost).getName();
var factory = me._getFactory(type); var factory = Group._getFactory(type);
if (factory == nil) if (factory == nil)
return parent_ghost; return parent_ghost;
@ -72,8 +73,7 @@ var Element = {
}, },
setGeoPosition: func(lat, lon) { setGeoPosition: func(lat, lon) {
me._getTf()._node.getNode("m-geo[4]", 1).setValue("N"~lat); me._getTf().setGeoPosition(lat, lon);
me._getTf()._node.getNode("m-geo[5]", 1).setValue("E"~lon);
return me; return me;
}, },
@ -94,10 +94,10 @@ var Element = {
# Get translation set with #setTranslation # Get translation set with #setTranslation
getTranslation: func() { getTranslation: func() {
if (me["_tf"] == nil) if (me["_tf"] == nil) {
return [0, 0]; return [0, 0];
}
return [me._tf.e.getValue(), me._tf.f.getValue()]; return me._tf.getTranslation();
}, },
# Set rotation around transformation center (see #setCenter). # Set rotation around transformation center (see #setCenter).
@ -182,6 +182,7 @@ var Element = {
return center; return center;
}, },
#return vector [sx, sy] with dimensions of bounding box
getSize: func { getSize: func {
var bb = me.getTightBoundingBox(); var bb = me.getTightBoundingBox();
return [bb[2] - bb[0], bb[3] - bb[1]]; return [bb[2] - bb[0], bb[3] - bb[1]];
@ -189,7 +190,7 @@ var Element = {
# convert bounding box vector into clip string (yes, different order) # convert bounding box vector into clip string (yes, different order)
boundingbox2clip: func(bb) { boundingbox2clip: func(bb) {
return sprintf("rect(%d,%d,%d,%d)", bb[1], bb[2], bb[3], bb[0]) return sprintf("rect(%d,%d,%d,%d)", bb[1], bb[2], bb[3], bb[0]);
}, },
# set clip by bounding box # set clip by bounding box

View file

@ -9,6 +9,7 @@
# 0 0 1 # 0 0 1
# #
# See http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined for details. # See http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined for details.
# https://www.w3.org/TR/css-transforms-1/#mathematical-description
# #
var Transform = { var Transform = {
new: func(node, vals = nil) { new: func(node, vals = nil) {
@ -44,7 +45,11 @@ var Transform = {
return me; return me;
}, },
getTranslation: func() {
return [me.e.getValue(), me.f.getValue()];
},
# Set rotation (Optionally around a specified point instead of (0,0)) # Set rotation (Optionally around a specified point instead of (0,0))
# #
# setRotation(rot) # setRotation(rot)
@ -58,6 +63,7 @@ var Transform = {
var s = math.sin(angle); var s = math.sin(angle);
var c = math.cos(angle); var c = math.cos(angle);
# rotation goes to the top-left 2x2 part of the matrix
me.a.setDoubleValue(c); me.c.setDoubleValue(-s); me.a.setDoubleValue(c); me.c.setDoubleValue(-s);
me.b.setDoubleValue(s); me.d.setDoubleValue(c); me.b.setDoubleValue(s); me.d.setDoubleValue(c);
@ -76,15 +82,24 @@ var Transform = {
# setScale([x, y]) # setScale([x, y])
setScale: func { setScale: func {
var scale = _arg2valarray(arg); var scale = _arg2valarray(arg);
# the scale factors go to the diagonal elements of the matrix
me.a.setDoubleValue(scale[0]); me.a.setDoubleValue(scale[0]);
me.d.setDoubleValue(size(scale) >= 2 ? scale[1] : scale[0]); me.d.setDoubleValue(size(scale) >= 2 ? scale[1] : scale[0]);
return me; return me;
}, },
getScale: func() { getScale: func() {
# TODO handle rotation # TODO handle rotation
return [me.a.getValue(), me.d.getValue()]; return [me.a.getValue(), me.d.getValue()];
} },
# this function is called very often, if a canvas map is active and
# the aircraft symbol is shown.
setGeoPosition: func(lat, lon) {
if (me["_gn"] == nil) { me._gn = me._node.getNode("m-geo[4]", 1); }
if (me["_ge"] == nil) { me._ge = me._node.getNode("m-geo[5]", 1); }
me._gn.setValue("N" ~ lat);
me._ge.setValue("E" ~ lon);
return me;
},
}; };