Reply To: Multiple use of the same object

PennController for IBEX Forums Support Multiple use of the same object Reply To: Multiple use of the same object

#10693
Jeremy
Keymaster

Hello Christin,

There is a method to set the text of a Text element: .text

Here is an illustration of what I think you are trying to achieve:

TEXTS = [
    null, // index 0, unused
    (m,l)=>`Der ${m} hat eine Leistung von ${l} PS`, // index 1
    (m,l)=>` er hat eine Leistung von ${l} PS.`, // index 2
    (m,l)=>` test ${l} test2 ${m}.`, // index 3
    // etc. 
]

Tempalte( variable =>
 newTrial(
    newButton("PS") // create this button now (don't apply the defaultButton below to this button)
    ,
    // I just use these two buttons for testing purposes
    newButton("Next text").callback( getVar("nr").set(v=>v+1) ),
    newButton("Switch side").callback( getVar("side").set(d=>d=="left"?"right":"left") )
    ,
    newVar("side", "left"), // which side to print the text
    newVar("nr", 1),        // which text to print (will look up TEXTS using the value)
    newVar("canvasnr",1)    // which canvas to print on
    ,
    defaultCanvas.css( "border" , "solid 1px black" ).print() // add a border a print all the canvases created below
    ,
    defaultButton.callback( getVar("canvasnr").set(v=>v+1) ) // increment canvasnr upon click on the buttons created below
    ,
    newCanvas("canvas1", 780, 20) // this button will make canvas2 visible
        .add("right at 100%", "middle at 50%", newButton("+1").callback(getCanvas("canvas2").visible(),self.remove()) )
        .add(0, "middle at 50%", newText("left1",""))
        .add("right at 90%", "middle at 50%", newText("right1",""))
    ,
    defaultCanvas.hidden() // hide all the canvases created below
    ,
    newCanvas("canvas2", 780, 20) // this button will make canvas3 visible
        .add("right at 100%", "middle at 50%", newButton("+2").callback(getCanvas("canvas3").visible(),self.remove()) )
        .add(0, "middle at 50%", newText("left2",""))
        .add("right at 90%", "middle at 50%", newText("right2",""))
    ,
    newCanvas("canvas3", 780, 20)
        .add("right at 100%", "middle at 50%", newButton("+3").callback(getCanvas("canvas4").visible(),self.remove()) )
        .add(0, "middle at 50%", newText("left3",""))
        .add("right at 90%", "middle at 50%", newText("right3",""))
    ,
    newCanvas("canvas4", 780, 20)
        // etc.
    ,
    getButton("PS")
        .callback( 
            // Create a Var element whose text content is fetched from TEXTS, based on the index in nr
            newVar("txt").set(getVar("nr")).set(n=>TEXTS[n](variable.model,variable.LeistungPS))
            ,
            getVar("side").test.is("left").success( 
                // left
                getVar("canvasnr")
                    .test.is(1).success( getText("left1").text(getVar("txt")) )
                    .test.is(2).success( getText("left2").text(getVar("txt")) )
                    .test.is(3).success( getText("left3").text(getVar("txt")) )
                    // etc.
            ).failure(
                // right
                getVar("canvasnr")
                    .test.is(1).success( getText("right1").text(getVar("txt")) )
                    .test.is(2).success( getText("right2").text(getVar("txt")) )
                    .test.is(3).success( getText("right3").text(getVar("txt")) )
                    // etc.
            )
        )
        .center()
        .print()
    ,
    // etc.
    getButton("Next text").center().print(),getButton("Switch side").center().print()
    ,
    newKey(" ").wait()
  )
)

Jeremy