Board index FlightGear Development Canvas

Adding a canvas to an old XML dialog?

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.

Adding a canvas to an old XML dialog?

Postby omega95 » Sun Aug 05, 2012 2:39 pm

TheTom wrote in Sun Aug 05, 2012 1:17 pm:
omega95 wrote in Sun Aug 05, 2012 1:00 pm:Is there a way, I can load THIS canvas onto a canvas GUI? Like in the above example?

Add the following to canvas/gui.nas:
Code: Select all
  setCanvas: func(canvas_)
  {
    if( !isa(canvas_, canvas.Canvas) )
      return debug.warn("Not a canvas.Canvas");
     
    canvas_.addPlacement({type: "window", index: me._node.getIndex()});
  },


and then use
Code: Select all
dlg.setCanvas(fmcScreen);

I will push it later...


What if I want to get canvas into a gui canvas area?
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: Getting a handle to an existing canvas and reusing it ?

Postby Hooray » Sun Aug 05, 2012 2:43 pm

the code that Tom has posted will assign an existing canvas to a dialog/window.
If you want to create a new canvas to be used by the GUI, you'll need to create canvas first ( canvas.new() ) and then add a group and add a window placement (as shown above).
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: Getting a handle to an existing canvas and reusing it ?

Postby omega95 » Sun Aug 05, 2012 2:50 pm

Hooray wrote in Sun Aug 05, 2012 2:43 pm:the code that Tom has posted will assign an existing canvas to a dialog/window.
If you want to create a new canvas to be used by the GUI, you'll need to create canvas first ( canvas.new() ) and then add a group and add a window placement (as shown above).


No, I want to simply assign an existing canvas but NOT to a canvas gui, an XML gui.

Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>

<!-- ATR72-500(C) Navigation Display Settings -->

<PropertyList>

   <name>fmc</name>
   <layout>vbox</layout>

      <group>
            <layout>hbox</layout>
            <empty><stretch>1</stretch></empty>

            <text>
                <label>ATR72-500(C) Flight Management Computer</label>
            </text>

            <empty><stretch>1</stretch></empty>

            <button>
                <pref-width>16</pref-width>
                <pref-height>16</pref-height>
                <legend></legend>
                <keynum>27</keynum>
                <border>2</border>
                <binding>
                    <command>dialog-close</command>
                </binding>
            </button>
        </group>
       
        <hrule/>

      <canvas>
        <name>fmcScreen</name>
        <valign>fill</valign>
        <halign>fill</halign>
        <stretch>false</stretch>
        <pref-width>512</pref-width>
        <pref-height>400</pref-height>
        <view n="0">400</view>
       
        <nasal>
         <load>
           <![CDATA[

            # I want to assign the fmc.fmcScreen canvas over here
          
           ]]>
         </load>
        </nasal>
      </canvas>
      
</PropertyList>
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: Getting a handle to an existing canvas and reusing it ?

Postby Hooray » Sun Aug 05, 2012 3:08 pm

Okay I understand, you want to add a canvas area to an "old" XML dialog, right?

... that used to work when we were still using the "CanvasWidget" technique which used PLIB/PUI, I am not sure if that's meanwhile been depreciated (or going to be) in favor of the new "pure OSG" method? The general opinion is that we will get rid of PUI completely.

That being said, to see how to add a "canvas" widget to an existing XML dialog, see the examples here: https://gitorious.org/~tomprogs/fg/toms ... 285a9d8bf5

First of all, you'll need to add a "canvas" section:
Code: Select all
<canvas>
                      <name>canvas-gui-test</name>
                     <valign>fill</valign>
                     <halign>fill</halign>
                     <stretch>true</stretch>
                     <pref-width>384</pref-width>
                     <pref-height>512</pref-height>
                     <view n="0">768</view>
                     <view n="1">1024</view>
                          <nasal>
                       <load>
                            <![CDATA[
                                    # your Nasal code goes here
                               ]]>
                       </load>
                     </nasal>
                   </canvas>



To get a handle to your canvas, just use cmdarg(): var my_canvas = canvas.get(cmdarg());
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: Adding a canvas to an old XML dialog?

Postby omega95 » Sun Aug 05, 2012 3:10 pm

Hooray wrote in Sun Aug 05, 2012 3:08 pm:To get a handle to your canvas, just use cmdarg(): var my_canvas = canvas.get(cmdarg());


Yea, I already got that example working, but what I really wanted to do is to get the fmcScreen canvas into a section in the gui. Basically, replace or set the canvas in the old XML gui with teh fmcScreen. :)
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: Adding a canvas to an old XML dialog?

Postby Hooray » Sun Aug 05, 2012 3:21 pm

Like I said previously here: viewtopic.php?f=71&t=17182#p163579 you need to set the canvas to the index of the canvas texture that you want.

However, this is not so trivial in this case, because the constructor of the CanvasWidget class will automatically allocate a new/empty canvas texture: http://gitorious.org/fg/flightgear/blob ... cxx#line19

I don't think there is any simple way to work around this, not without replicating the whole canvas via Nasal at least.
But maybe Tom has an idea?

In summary, most people seem to agree that the CanvasWidget approach was neat for a demo, but that it will be phased out and replaced with (ported to) pure OSG code.
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: Adding a canvas to an old XML dialog?

Postby omega95 » Sun Aug 05, 2012 3:25 pm

But I still don't understand how to set placement for fmcScreen to the xml gui. :oops:
Last edited by Gijs on Wed Feb 20, 2013 5:03 pm, edited 1 time in total.
Reason: No useless quoting please.
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: Adding a canvas to an old XML dialog?

Postby TheTom » Sun Aug 05, 2012 3:26 pm

You can not reuse an existing canvas, but instead can only add new things to the widget canvas. To use the same canvas contents in multiple places you should put the code which fills your canvas into a separate Nasal file and call it from the load tag. The CanvasWidget will be removed in the future, as soon as it is possible to use canvases inside other canvases. If you split canvas creation and filling the canvas into different parts you won't have any problems of upgrading to new features.

Place a file which fills a canvas eg. in your Aircrafts Nasal directory and load in your -set.xml (eg. in module fmc):
Code: Select all
var FMC = {
  new: func(canvas_group)
  {
    var m = { ...
    canvas_group.createChild("whatever is needed");
    # ...
  }
};


Show the canvas inside a PUI widget:
Code: Select all
<load>
    <![CDATA[
     var my_canvas = canvas.get(cmdarg());
     var gui_fmc = fmc.FMC.new(my_canvas.createGroup());
     ]]>
</load>


Show it somewhere inside your model:
Code: Select all
var my_canvas = canvas.new({
    "name": "FMC",
    "size": [768, 1024],
    "view": [768, 1024],
    "mipmapping": 1
  });
my_canvas.addPlacement({parent: "FMC 1", node: "FMC-Screen"});
TheTom
 
Posts: 322
Joined: Sun Oct 09, 2011 11:20 am

Re: Adding a canvas to an old XML dialog?

Postby Hooray » Sun Aug 05, 2012 4:25 pm

Copying a canvas to another canvas is explained here: viewtopic.php?f=71&t=17188
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: Adding a canvas to an old XML dialog?

Postby omega95 » Sun Aug 05, 2012 4:26 pm

Lol, right, it'd be much easier when canvas supports layers but for now, I'm trying to get this on gui temporarily. Anyway, I have another question, let's say I create a nasal variable in an xml gui, how would I access it from the Console or anywhere else? Anyway, I was thinking of a continuous listener loop that copies props from the actual to new canvas, so what would I need to check to look for changes in the first group? :)
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: Adding a canvas to an old XML dialog?

Postby Gijs » Mon Feb 25, 2013 1:43 pm

Somewhat related to omega's question, so adding my question here.

I've managed to add a SVG canvas to a XML dialog. All looks fine (left image). But, when I resize the dialog (just one pixel and then move it back to its original size), the canvas jumps to its maximum size and stays there (right image). Is there anything I need to do to make the canvas resize with the dialog?
Code: Select all
   <canvas>
      <name>canvas-gui-test</name>
      <valign>fill</valign>
      <halign>fill</halign>
      <stretch>true</stretch>
      <pref-width>400</pref-width>
      <pref-height>400</pref-height>
      <view n="0">1024</view>
      <view n="1">1024</view>
   </canvas>

Image Image
Airports: EHAM, EHLE, KSFO
Aircraft: 747-400
User avatar
Gijs
Moderator
 
Posts: 9542
Joined: Tue Jul 03, 2007 3:55 pm
Location: Delft, the Netherlands
Callsign: PH-GYS
Version: Git
OS: Windows 10

Re: Adding a canvas to an old XML dialog?

Postby Hooray » Mon Feb 25, 2013 3:41 pm

have you checked if that works for the airport-selection dialog ?
If it also doesn't, then the problem probably is that resize/update event isn't yet propagated to the canvas subsystem.
What about explicitly updating the canvas via Nasal (even just hide/show should do), is the issue fixed then ?
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: Adding a canvas to an old XML dialog?

Postby TheTom » Mon Feb 25, 2013 3:47 pm

The viewport of the canvas is automatically updated to the actual dimensions of the PUI widget. I've pushed a fix which prevents this if 'view' is given, and only updates it automatically if it is not given.

/*edit */ Just be aware that this allows for non aspect-preserving scaling. Maybe this should become another option...
TheTom
 
Posts: 322
Joined: Sun Oct 09, 2011 11:20 am

Re: Adding a canvas to an old XML dialog?

Postby Gijs » Mon Feb 25, 2013 4:04 pm

Well, having odd-scaled images is at least better than having images falling of the dialog ;-)
It might make sense to introduce an aspect ratio option. Not just for canvas, but for dialogs as a whole...
Airports: EHAM, EHLE, KSFO
Aircraft: 747-400
User avatar
Gijs
Moderator
 
Posts: 9542
Joined: Tue Jul 03, 2007 3:55 pm
Location: Delft, the Netherlands
Callsign: PH-GYS
Version: Git
OS: Windows 10

Re: Adding a canvas to an old XML dialog?

Postby Hooray » Thu May 23, 2013 11:34 pm

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

Next

Return to Canvas

Who is online

Users browsing this forum: No registered users and 2 guests