svghmi/widget_switch.ysl2
author Edouard Tisserant
Thu, 09 Dec 2021 10:21:45 +0100
branchRuntimeLists
changeset 3395 93ad018fb602
parent 3386 608f48ad3dfc
child 3513 7d4a16e59337
permissions -rw-r--r--
RUNTIME: Variable forcing now uses limited list and buffer instead of systematical instance tree traversal and in-tree "fvalue" to keep track of forced value for pointed variables (external, located). Pointer swapping is performed when forcing externals and located, with backup being restored when forcing is reset. Retain still uses tree traversal.
// widget_switch.ysl2

widget_desc("Switch") {
    longdesc
    ||
    Switch widget hides all subelements whose label do not match given
    variable current value representation. For exemple if given variable type
    is HMI_INT and value is 1, then elements with label '1' will be displayed.
    Label can have comments, so '1#some comment' would also match. If matching
    variable of type HMI_STRING, then double quotes must be used. For exemple,
    '"hello"' or '"hello"#another comment' match HMI_STRING 'hello'.
    ||

    shortdesc > Show elements whose label matches value.

    // TODO: add optional format/precision argument to support floating points
    // TODO: support (in)equations and ranges

    path name="value" accepts="HMI_INT,HMI_STRING" > value to compare to labels
    
}

widget_class("Switch")
    ||
        frequency = 5;
        dispatch(value) {
            for(let choice of this.choices){
                if(value != choice.value){
                    choice.elt.setAttribute("style", "display:none");
                } else {
                    choice.elt.setAttribute("style", choice.style);
                }
            }
        }
    ||

widget_defs("Switch") {
    |     choices: [
    const "regex",!"'^(\"[^\"].*\"|\-?[0-9]+|false|true)(#.*)?$'"!;

    const "subelts", "$result_widgets[@id = $hmi_element/@id]//*";
    const "subwidgets", "$subelts//*[@id = $hmi_widgets/@id]";
    const "accepted", "$subelts[not(ancestor-or-self::*/@id = $subwidgets/@id)]";

    foreach "$accepted[regexp:test(@inkscape:label,$regex)]" {
        const "literal", "regexp:match(@inkscape:label,$regex)[2]";
    |         {
    |             elt:id("«@id»"),
                  // TODO : use style.display = "none" to hide element
    |             style:"«@style»",
    |             value:«$literal»
    |         }`if "position()!=last()" > ,`
    }
    |     ],
}