lpcmanager

1679bae6e7f1
Added support for LHC1_GOT_002 product.
// widget_swipe.ysl2
widget_desc("Swipe") {
longdesc
||
Swipe widget detects left, right, up and down swiping motion and executes
associated actions. The widget should be placed on top of the area where the
movement should be detected. It is a group containing a graphical element
"area" which defines the area where the swipe should be detected.
For each of the motions to be detected there must exist several parameters
named "{direction}_{command}={value}" where {direction} is from the set:
left, right, up, down; and {command} is from the set: xthreshold (in percents
of widget width), ythreshold (also percentage), jump (value should be name of
the page to jump to), change (value should be the change to apply, e.g. +2 to
increase by 2, or -1 to decrement) or set (value should be the value to set
to). change and set commands should also be accompanied by paths with the
same name and their values should be variable names to apply the command to.
Additional parameters to add are:
- movethreshold: Percentage of the widget dimensions that define the pointer
movement. Anything below that value will not be considered
a movement. If omitted, 5 will be used.
- presstimeout: Time in milliseconds which will be measured on the pointer
down event. If time elapses without any significant movement
(defined by movethreshold), the pointer down/click event
will be propagated on an element on a lower level than the
swiping area. Similar thing will happen on pointer up event
if there was no significant movement.
Examples:
HMI:Swipe:movethreshold=3:left_xthreshold=30:left_ythreshold=5:left_jump=Home:up_xthreshold=25:up_ythreshold=5:up_change=+2@up_change=/VAR0
This detects left and right swipe motion. To detect swipe left, movement must
be at least 30% of the widget width to the left and at most 5% of the widget
height up or down. If detected, it will jump to a page named Home. To detect
swipe up, movement must be at most 5% of the widget width left or right, and
at least 25% of the widget height up. If detected, it will increase VAR0 by 2.
||
shortdesc > Detect swipe motion and react accordingly
}
widget_class("Swipe") {
||
frequency = 2;
startX = -1;
startY = -1;
currX = -1;
currY = -1;
moveThreshold = 5;
pressTimeout = 300;
touchTimer = null;
settings = {
left: {
actions: [],
xThreshold: 100,
yThreshold: 0,
},
right: {
actions: [],
xThreshold: 100,
yThreshold: 0,
},
up: {
actions: [],
xThreshold: 0,
yThreshold: 100,
},
down: {
actions: [],
xThreshold: 0,
yThreshold: 100,
},
};
propagateMouseDownEvent(simulateUp) {
const elements = document.elementsFromPoint(this.startX, this.startY);
if (elements.length > 1) {
const eventDown = new MouseEvent("pointerdown", {
view: window,
bubbles: true,
cancelable: true,
});
const eventClick = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
const eventUp = new MouseEvent("pointerup", {
view: window,
bubbles: true,
cancelable: true,
});
const cb = document.getElementById(elements[1].id);
cb.dispatchEvent(eventDown);
cb.dispatchEvent(eventClick);
if (simulateUp) {
window.setTimeout(() => {
cb.dispatchEvent(eventUp);
}, 100);
}
}
}
onMouseUp(evt) {
window.clearTimeout(this.touchTimer);
this.touchTimer = null;
svg_root.removeEventListener("pointerup", this.boundOnMouseUp, true);
svg_root.removeEventListener("pointermove", this.boundOnMouseMove, true);
const area = this.element.getBoundingClientRect();
var xDiff = (evt.pageX - this.startX) * 100.0 / area.width;
var yDiff = (evt.pageY - this.startY) * 100.0 / area.height;
var action = null;
if (xDiff < 0 && Math.abs(xDiff) >= this.settings.left.xThreshold && Math.abs(yDiff) < this.settings.left.yThreshold) {
action = "left";
} else if (xDiff > 0 && Math.abs(xDiff) >= this.settings.right.xThreshold && Math.abs(yDiff) < this.settings.right.yThreshold) {
action = "right";
} else if (yDiff < 0 && Math.abs(yDiff) >= this.settings.up.yThreshold && Math.abs(xDiff) < this.settings.up.xThreshold) {
action = "up";
} else if (yDiff > 0 && Math.abs(yDiff) >= this.settings.down.yThreshold && Math.abs(xDiff) < this.settings.down.xThreshold) {
action = "down";
} else if (Math.abs(xDiff) < this.moveThreshold && Math.abs(yDiff) < this.moveThreshold) {
this.propagateMouseDownEvent(true);
}
if (action) {
for (var a of this.settings[action].actions) {
if (a.action == "jump") {
fading_page_switch(a.target);
} else if (a.action == "change") {
this.change_hmi_value(a.var_idx, a.value);
} else if (a.action == "set") {
this.apply_hmi_value(a.var_idx, a.value);
}
}
}
}
onMouseMove(evt) {
this.currX = evt.pageX;
this.currY = evt.pageY;
}
onMouseDown(evt) {
this.startX = evt.pageX;
this.startY = evt.pageY;
this.currX = evt.pageX;
this.currY = evt.pageY;
svg_root.addEventListener("pointerup", this.boundOnMouseUp, true);
svg_root.addEventListener("pointermove", this.boundOnMouseMove, true);
this.touchTimer = window.setTimeout(() => {
const area = this.element.getBBox();
var xDiff = (this.currX - this.startX) * 100.0 / area.width;
var yDiff = (this.currY - this.startY) * 100.0 / area.height;
if (Math.abs(xDiff) < this.moveThreshold && Math.abs(yDiff) < this.moveThreshold) {
svg_root.removeEventListener("pointerup", this.boundOnMouseUp, true);
svg_root.removeEventListener("pointermove", this.boundOnMouseMove, true);
this.propagateMouseDownEvent(false);
}
this.touchTimer = null;
}, this.pressTimeout);
this.request_animate();
}
||
}
widget_defs("Swipe") {
| init: function() {
| this.boundOnMouseUp = this.onMouseUp.bind(this);
| this.boundOnMouseMove = this.onMouseMove.bind(this);
| this.element.addEventListener("pointerdown", this.onMouseDown.bind(this));
| const dirs = ["left", "right", "up", "down"];
| var properDir = false;
| var pathIndex = -1;
const "paths", "path";
foreach "arg[contains(@value, '=')]" {
const "name", "substring-before(@value, '=')";
const "value", "substring-after(@value, '=')";
const "index" foreach "$paths" if "@assign = $name" value "position()-1";
const "direction", "substring-before($name, '_')";
const "command", "substring-after($name, '_')";
| if ("«$index»".length > 0) {
| pathIndex = Number("«$index»");
| }
| properDir = dirs.findIndex((x) => x == "«$direction»") > -1;
| if (properDir) {
| switch ("«$command»") {
| case "xthreshold":
| this.settings["«$direction»"].xThreshold = «$value»;
| break;
| case "ythreshold":
| this.settings["«$direction»"].yThreshold = «$value»;
| break;
| case "jump":
| this.settings["«$direction»"].actions.push({
| action: "jump",
| target: "«$value»",
| });
| break;
| case "change":
| this.settings["«$direction»"].actions.push({
| action: "change",
| var_idx: pathIndex,
| value: "«$value»",
| });
| break;
| case "set":
| this.settings["«$direction»"].actions.push({
| action: "set",
| var_idx: pathIndex,
| value: "«$value»",
| });
| break;
| }
| }
| if ("«$name»" == "movethreshold") {
| this.moveThreshold = "«$value»";
| } else if ("«$name»" == "presstimeout") {
| this.pressTimeout = "«$value»";
| }
}
| },
}