Board index FlightGear Development Canvas

Canvas Textbox (kind of) Help

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 Textbox (kind of) Help

Postby omega95 » Sun Aug 11, 2013 5:04 pm

So, I wanted to make a textbox (can't wait for it be included in canvas because the system I'm working on needs to be distributed to pilots using older versions of FlightGear (upto 2.8 atleasT)) for a flightplan form in canvas.

The form should look like an ICAO flightplan form.
Image

But I got most of it worked out, I just need to put text-boxes there. I was thinking of something like in the A380 mCDU, where you click no a touchbox and just type - I don't need a cursor or anything like that, just text inputs that update canvas property values. I was wondering if I can use http://flightgear.org/forums/viewtopic.php?f=30&t=20533#p187990 to get key values without it affect anything else in sim. I mean, if I type a, I don't want it to accelerate time. :)

Thanks a lot!
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Canvas Textbox (kind of) Help

Postby Philosopher » Sun Aug 11, 2013 5:09 pm

Yeah, it works, see the comment (that I mercilessly stole from the original code):
key.setValue(-1); # drop key event

Thus everything forgets that the key was even pressed ;).
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm

Re: Canvas Textbox (kind of) Help

Postby omega95 » Sun Aug 11, 2013 6:09 pm

I can't exactly get that to work, it keeps doing other stuff, like d dumps models (I'm using the ufo to test), p pauses etc. :|

Code: Select all

var textboxArray = [];
         
         var genTextBox = func(m, left, top, length, height) {
         
            m.canvasbox.setColor(0.5,0.5,0.5,0.2)
                        .setStrokeLineWidth(1)
                        .moveTo(left, top)
                        .line(length, 0)
                        .line(0, height)
                        .line(-length, 0)
                        .line(0, -height)
                        .setColorFill(0.8,0.8,0.8,0.1)
                        .addEventListener("click", func(e) {
               
               foreach(var box; textboxArray) {
                  box.canvasbox.setColorFill(0.8,0.8,0.8,0.1);
               }
               
               m.canvasbox.setColorFill(1,0,0,0.1);
               
               listener = setlistener("/devices/status/keyboard/event", func(event) {
      if (!event.getNode("pressed").getValue())
         return;
      var key = event.getNode("key");
      var shift = event.getNode("modifier/shift").getValue();
      if (handle_key(key.getValue(), shift))
         key.setValue(-1);           # drop key event

      m.addChar(chr(key.getValue())); # Just to test

   });
               
            });
         };

var textbox = {
            id: "",
            left: 0,
            top: 0,
            length: 0,
            height: 0,
            text: "",
            canvasbox: textboxes.createChild("path"),
            canvastext: textboxes.createChild("text"),
            addChar: func(char) {
               me.text += char;
            },
            backSpace: func() {
               me.text = substr(me.text, 0, size(me.text) - 1);
            },
            new: func(id, left, top, length, height, group, fontsize) {
               var m = {parents: [textbox]};
               m.id = id;
               m.left = left;
               m.top = top;
               m.length = length;
               m.height = height;
               
               # Draw textbox
               genTextBox(m, left, top, length, height);
                        
               m.canvastext.setText("")
                        .setFontSize(fontsize)
                        .setFont("Helvetica.txf")
                        .setColor(0,0,0)
                        .setAlignment("left-center")
                        .setTranslation(left + 6, top + 4 + (fontsize/2));
                        
               return m;
            }
         };

Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Canvas Textbox (kind of) Help

Postby Philosopher » Sun Aug 11, 2013 6:31 pm

You need a handle_key predicate-with-side-effects (lol, IOW: a function that does what you want and returns 1 if it captured the event or 0 if it wants to pass it through). In your case:
Code: Select all
var handle_key = func(char) {
    if(char == 8)
        m.removeChar();
    else
        m.addChar(chr(char));
    return 1;
}

At some point you'll have to go back and make it return 0 for non-printable keys. Also, here's two methods for your class, just since Nasal does string concatenation with the tilde operator (for various reasons) and since the substr function can be confusing ;):
Code: Select all
addChar: func(char) {
   me.text ~= char;
},
removeChar: func() {
   me.text = substr(me.text,0,size(me.text)-1);
},
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm


Return to Canvas

Who is online

Users browsing this forum: No registered users and 2 guests