javascript variables and if-statement in actions

  • Hello,

    I am new to krpano and I totally love what I have seen so far. Simply the best pano viewer. Great work Klaus and everybody else who has contributed.

    At the moment I am trying to figure out what is possible with this tool, so I decided to write a simple navigation. The basic idea is that a textfield opens when the user clicks on a button and closes again on the next click on the button.
    Here is the action I wrote for it:

    Code
    <action name="callhelp">
    	var helpvis = get(plugin[helptext].visible);
    	if (helpvis == "false") {
    		set(plugin[helptext].visible,true);
    	}
    	else {
    		set(plugin[helptext].visible,false);
    	}
    </action>


    It does not return an error once it is loaded, however if I click the button nothing happens.
    The basic version of it with the following action works fine, so it must have to do with the variable and/or if statement. Based on the documentation I am not even sure if javascript is possible in the xml file.

    Code
    <action name="callhelp">
    		set(plugin[helptext].visible,true);
    </action>

    Help would be greatly appreciated,
    Magnus

    Edit: I am using the 1.0.7 demo for testing.

    Edited once, last by magnus (September 17, 2009 at 9:34 PM).

  • Thanks a lot for the help, michel
    This means it is time to update to 1.0.8 beta. Seems like the is a new feature. Great to know it works.

    As I need the variable the full action would have to look like this:

    Code
    <action name="callhelp">
    set(helpvis,get(plugin[helptext].visible));
    if(helpvis  == false, set(plugin[helptext].visible,true);, set(plugin[helptext].visible,false;);
    </action>

    helpvis is a global variable in this case, so I can use it in other actions as well?
    And what about quote signs in line 3 for false? I think things can turn out strange when a variable is named the same as any argument of any attribute set in the xml file, so it might be better to used quote signs for normal text.
    Klaus, if you are reading this, what is the reason for the syntax? I am not familiar with actionscript, but I would somewhat prefer to see javascript syntax and full javascript support, as it would open the door to web2.0 features integrated into krpano.
    Whatever, I am looking forward to play with this some more tomorrow and getting used to the syntax.

    Magnus

  • Hi magnus,

    Quote

    As I need the variable the full action would have to look like this:
    ........

    Yes, but there is a missing ) into the code:
    if(helpvis == false, set(plugin[helptext].visible,true);, set(plugin[helptext].visible,false););

    Code
    <action name="callhelp">
    set(helpvis,get(plugin[helptext].visible));
    if(helpvis  == false, set(plugin[helptext].visible,true);, set(plugin[helptext].visible,false););
    </action>


    I have tried this and it works..
    I am absolutely new playing with user defined variables, and this thread was the occasion to learn about this. *wink*

    Salut.

  • Thanks for finding the missing bracket. Finding that out of an xml parser error would have taken lots of time most likely.
    I just tested this with the 1.0.8beta8 and found out you must not put quote sign around false, because you'll run into a parser error otherwise.
    Both if statements in this thread are working fine. Thanks again for pointing me into the correct direction michel.

    Next part to find out is how to realize basic mathematic operations with my variables, in case the variable is an int, float whatever. And how to call actions with parameters.

  • Hi,

    Next part to find out is how to realize basic mathematic operations with my variables, in case the variable is an int, float whatever. And how to call actions with parameters.

    the math values are always floats (="Number" in AS3),

    there are currently these math actions:

    add/sub/mul/div/mod math operators:

    • add(dst,val1,val2) => dst=val1+val2
    • sub(dst,val1,val2) => dst=val1-val2
    • mul(dst,val1,val2) => dst=val1*val2
    • div(dst,val1,val2) => dst=val1/val2
    • mod(dst,val1,val2) => dst=val1%val2

    val1/val2 can be values or variables


    see here:
    krpano 1.0.8 beta 7
    and here:
    krpano 1.0.8 beta 8


    about the actions with parameters:

    an action can be called for example in this way:

    Code
    actionname(parameter1,parameter2);

    and this will be the action:

    Code
    <action name="actionname">
      trace('parameter1=', %1);
      trace('parameter2=', %2);
    </action>

    the %1 and %2 will be replaced with the given parameter,
    it's possible to have 1-9 parameters (%1 - %9)
    %0 = is the name of the action itself (in this example -"actionname"),


    to pass the content of variables to an action use get() on the action call,
    e.g.

    Code
    actionname( get(view.hlookat), get(view.vlookat) );

    the get() will be resolved before calling, and then the values will be passed to the action,

    best regards,
    Klaus

  • Thanks a lot Klaus.
    Adding plugins and calling actions with parameters works just fine. However, the maths leaving me puzzled.

    I tried to write an action that moves a plugin based on its width. (The example does not make a lot of sense yet, but it part of something bigger.) When calling the action it gets the name of the textfield plugin to move as a parameter. When I am calling the action nothing happens whatsoever.

    Code
    <action ="moveplugin">
    	<!-- %1 = nameoftextfield -->
    	set(txtwidth, get(plugin[%1].width));
    	sub(newx, txtwidth, 20);
    
    
    	set(plugin[%1].x, newx);
    </action>

    I tried adding another plugin with it which works, so the problem must be on the mathematic part of the action or the variables.
    Any ideas? I also tried storing the value of the substraction in txtwidth, instead of newx, but that did not change anything.

    Thanks in advance,
    Magnus

    Edited once, last by magnus (September 24, 2009 at 5:20 PM).

  • Hi magnus,

    Notice the explanation from KLAUS above: *rolleyes*

    to pass the content of variables to an action use get() on the action call,
    e.g.

    Code
    actionname( get(view.hlookat), get(view.vlookat) );


    the get() will be resolved before calling, and then the values will be passed to the action,


    So, your code will be: *wink*

    Code
    <action ="moveplugin">
    <!-- %1 = nameoftextfield -->
    set(txtwidth, get(plugin[%1].width));
    sub(newx, txtwidth, 20);
    
    
    set(plugin[%1].x, get(newx););
    </action>


    set(plugin[%1].x, get(newx);); instead of set(plugin[%1].x, newx);

    Salut.

  • Thanks a lot for the help michel. I just tested it, but without success though.

    Here is the full script I am working on. It adds a close button to the top right corner of a textfield, at least that it what it is supposed to do. (The maths is a little of at the moment.)

    I added the showtext to test if my variables are ok. What I ran into is that it has to be

    Code
    showtext(get(newx));

    , to show the value of the variable. If it is

    Code
    showtext(get(newx););

    instead of the value ot the variable the text "get(newx);" will be displayed, which is a little strange I think.
    As a result of that I think it also has to be

    Code
    set(plugin[%1_close].x, get(newx));

    and not

    Code
    set(plugin[%1_close].x, get(newx););

    . However, I tested both without succes. The button does not even show up.When I am setting the values for x and y by hand (180 and 80 instead of the get statements, same values as the variables) it works fine. So the problem must be on the get part.

    Help would be greatly appreciated.
    Magnus

  • Hi magnus,

    You are right ( my mistake *unsure* ):

    Code
    set(plugin[%1_close].x, get(newx));
    Quote

    Here is the full script I am working on. It adds a close button to the top right corner of a textfield, at least that it what it is supposed to do. (The maths is a little of at the moment.)

    I have tried your code and it work perfectly.... *whistling*
    Here an example that use your code for test:

    Quote

    I added the showtext to test if my variables are ok. What I ran into is that it has to be

    Instead of using showtext(get(newx)); to trace your variable, you can use the trace() action as you can see in the code above... this way you can trace what you want pressing the o key to pop up the krpano debugger.

    hope this can help. Let me know. *wink*

    Salut.

  • Strange. I am using the krpano1.0.8beta8 for this. No html embedding, just the swf and xml file. Not even a panorama picture included.
    I will check your version as soon as I am on my home PC michel. Hopefully it works for me. I will also reduce my xml file to this action only, to make sure the problem is not coming from another part of the script. I'll keep you updated.

    Thanks a lot for your help, michel. It is great to know there is a forum with people helping whenever you are running into a problem. Hopefully I can give back some of that one day. :)

  • So I finally got this working.
    It always worked as it was supposed to be, but using a black button on black background is probably not the best idea.

    Thanks for the help again. :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!