Board index FlightGear Development Canvas

Canvas gui button - emulate mod-up

Canvas is FlightGear's new fully scriptable 2D drawing system that will allow you to easily create new instruments, HUDs and even GUI dialogs and custom GUI widgets, without having to write C++ code and without having to rebuild FlightGear.

Canvas gui button - emulate mod-up

Postby Alant » Fri Dec 01, 2017 11:06 pm

Is it possible to emulate the mod-up or one-shot binding with a canvas gui button?

I am setting a property to true when a button is clicked, but wish to set it false when the mouse is released.

Code: Select all
me.Enter_button = canvas.gui.widgets.Button.new(root, canvas.style, {})
         .setText("ENTER")
         .setFixedSize(100, 30)
         .listen("clicked",func {Enter_key()});

buttons1.addItem(me.Enter_button);

var Enter_key = func {
   print("Enter key clicked");
   setprop("systems/navigation/switches/Enter_key",1);
   }


TIA

Alan
Alant
 
Posts: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Re: Canvas gui button - emulate mod-up

Postby wlbragg » Sat Dec 02, 2017 7:02 am

If you don't get a direct answer like a mod-up equivalent, could you use a timer to set it back to false?
Kansas and Ohio/Midwest scenery development.
KEQA, 3AU, KRCP Airport Layout
Intel i7/GeForce RTX 2070/Max-Q
User avatar
wlbragg
 
Posts: 7586
Joined: Sun Aug 26, 2012 12:31 am
Location: Kansas (Tornado Alley), USA
Callsign: WC2020
Version: next
OS: Win10/Linux/RTX 2070

Re: Canvas gui button - emulate mod-up

Postby Alant » Sat Dec 02, 2017 10:57 am

Yes that would work in this case.I could also clear the button-press property once it has been used.

However I have other buttons which have a specific action when released, and yet others which operate for as long as the button is pressed (e.g. slew, key repeat) and would like to simulate them also.
My Gui dialogs are designed to emulate (and work in parallel with) the relevant aircraft panels .

Tanks for the reply.

Alan
Alant
 
Posts: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Re: Canvas gui button - emulate mod-up

Postby Hooray » Sat Dec 02, 2017 11:33 am

I guess I would also use timer objects (via maketimer) and then use lower-level canvas events: http://wiki.flightgear.org/Canvas_Event ... vent_types

You'd basically start a timer task and stop/suspend it separately, in your timer, you'd run a while loop doing check to make sure that it's not an infinite loop.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Canvas gui button - emulate mod-up

Postby Alant » Sat Dec 02, 2017 12:05 pm

Hooray.

I will give that a go. I would prefer not to use a timer and will try to integrate the events directly with the button widget and my canvas code.

Alan
Alant
 
Posts: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Re: Canvas gui button - emulate mod-up

Postby Alant » Sun Dec 03, 2017 12:06 am

Hooray

I have a working solution - it turned out to be quite simple. :D

In fgdata/Nasal/canvas/gui/Buttons.nas, some code added to setDown. Note that at present it only works if the button is not checkable.

Code: Select all
  setDown: func(down = 1)
  {
    if( me._checkable or me._down == down )
      return me;

    me._down = down;
    me._onStateChange();

   if (!me._down){
      me._trigger("mouse_up", {checked: 0});
     print ("mouse up");
     }
    else{
      me._trigger("mouse_down", {checked: 0});
     print ("mouse down");
     }   
    
    return me;
  },


It is called from my canvas gui
Code: Select all
      me.Enter_button = canvas.gui.widgets.Button.new(root, canvas.style, {})
         .setText("ENTER")
         .setFixedSize(100, 30)
         .listen("mouse_down",func {Enter_key_press()})
         .listen("mouse_up",func {Enter_key_release()});
        buttons1.addItem(me.Enter_button);

var Enter_key_press = func {
   print("Enter key press");
   setprop("systems/navigation/switches/Enter_key",1);
   }
var Enter_key_release = func {
   print("Enter key release");
   setprop("systems/navigation/switches/Enter_key",0);
   }


For many purposes setting a property on mouse down is preferable to waiting for a complete mouse click cycle (click = down followed by release).

I will submit this to the devel list after some more testing, and deciding what is best to do if the button is checkable. If you have any observations please let me know.

Alan
Alant
 
Posts: 1219
Joined: Wed Jun 23, 2010 6:58 am
Location: Portugal
Callsign: Tarnish99
Version: latest Git
OS: Windows 10/11

Re: Canvas gui button - emulate mod-up

Postby benih » Mon May 17, 2021 10:06 am

Hi there,
i needed that today for FGCom-mumble to make it easier to access COM3' PTT (for which there is no default keybind).

The solution above still works, but it would be cool if this could be added to the default distribution, as i figure spring loaded buttons are not that uncommon.
User avatar
benih
 
Posts: 1689
Joined: Tue Aug 15, 2017 10:34 am
Callsign: D-EBHX
Version: next
OS: Debian Linux 64bit


Return to Canvas

Who is online

Users browsing this forum: No registered users and 4 guests