Board index FlightGear Development Canvas

Creating a Widget of widgets  Topic is solved

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.

Re: Creating a Widget of widgets

Postby Necolatis » Mon Dec 01, 2014 10:47 am

Alright will look into that if I solve the problem.

In the meantime I have made a minimal widget, and it also has the problem. When a button gets focus, a failure occurs:

Code: Select all
gui.widgets.TestWidget = {
  new: func(parent, style, cfg)
  {
    var m = gui.Widget.new(gui.widgets.TestWidget);

    m._vbox = VBoxLayout.new();
    append(m.parents, m._vbox);

    m._cfg = Config.new(cfg);
    m._focus_policy = m.NoFocus;

    var w = {
      parents: [TestWidgetFactory],
      _style: style,
      _vbox: m._vbox
    };
    call(TestWidgetFactory.new, [parent, m._cfg], w, var err = []);

    if(size(err)) {
      debug.dump(err);
    }

    m._setView(w);

    return m;
  }
};

TestWidgetFactory = {
  new: func(parent, cfg)
  {
    me._root = parent.createChild("group", "testing-panel");

    me._tab_bar = HBoxLayout.new();
    me._vbox.addItem(me._tab_bar);

    me._tab_failures = gui.widgets.Button.new(me._root, me._style, {})
                                    .setCheckable(1)
                                    .setChecked(1)
                                    .setFixedSize(75,25)
                                    .setText("Button 1");
    me._tab_random = gui.widgets.Button.new(me._root, me._style, {})
                                    .setCheckable(1)
                                    .setChecked(0)
                                    .setFixedSize(75,25)
                                    .setText("Button 2");
   
    me._tab_failures.listen("toggled", func (e) {
      if( e.detail.checked ) {
        me._tab_random.setChecked(0);
      }
    });
    me._tab_random.listen("toggled", func (e) {
      if( e.detail.checked ) {
        me._tab_failures.setChecked(0);
      }
    });

    me._tab_bar.addStretch(1);
    me._tab_bar.addItem(me._tab_failures);
    me._tab_bar.addItem(me._tab_random);
    me._tab_bar.addStretch(1);


    return me;
  },

  setSize: func(model, w, h)
  {
    me._vbox.setGeometry([0,0,w,h]);

    return me;
  }, 

  update: func(model)
  {

  }, 
};
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Creating a Widget of widgets

Postby Necolatis » Mon Dec 01, 2014 12:18 pm

Okay, I made 2 hacks to get it to work:

I called explicit setParent and setCanvas on the layout. Code to help others with same problem here:

Code: Select all
gui.widgets.TestWidget = {
  new: func(parent, style, cfg)
  {
    var m = gui.Widget.new(gui.widgets.TestWidget);

    m._cfg = Config.new(cfg);
    m._focus_policy = m.NoFocus;

    var w = {
      parents: [TestWidgetFactory],
      _style: style,
    };
    call(TestWidgetFactory.new, [parent, m._cfg], w, var err = []);

    if(size(err)) {
      debug.dump(err);
    }

    m._setView(w);

    return m;
  }
};

TestWidgetFactory = {
  new: func(parent, cfg)
  {
    me._root = parent.createChild("group", "testing-panel");

    me._vbox = VBoxLayout.new();
    me._vbox.setCanvas(me._root.getCanvas());                           #hack 1

    me._tab_bar = HBoxLayout.new();
    me._vbox.addItem(me._tab_bar);

    me._tab_failures = gui.widgets.Button.new(me._root, me._style, {})
                                    .setCheckable(1)
                                    .setChecked(1)
                                    .setFixedSize(75,25)
                                    .setText("Button 1");
    me._tab_random = gui.widgets.Button.new(me._root, me._style, {})
                                    .setCheckable(1)
                                    .setChecked(0)
                                    .setFixedSize(75,25)
                                    .setText("Button 2");
   
    me._tab_failures.listen("toggled", func (e) {
      if( e.detail.checked ) {
        me._tab_random.setChecked(0);
      }
    });
    me._tab_random.listen("toggled", func (e) {
      if( e.detail.checked ) {
        me._tab_failures.setChecked(0);
      }
    });

    me._tab_bar.addStretch(1);
    me._tab_bar.addItem(me._tab_failures);
    me._tab_bar.addItem(me._tab_random);
    me._tab_bar.addStretch(1);


    return me;
  },

  # Reset the size of the content area, e.g. on window resize.
  #
  setSize: func(model, w, h)
  {
    me._vbox.setGeometry([0,0,w,h]);
    return me;
  },

  update: func(model)
  {
    if(me._vbox.getParent() == nil) {
      me._vbox.setParent(model);                      #hack 2
    }
   
    me._root.update();
  }, 
};
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Creating a Widget of widgets

Postby Necolatis » Wed Dec 03, 2014 3:58 pm

Hooray wrote in Sun Nov 30, 2014 6:58 pm:I'd consider moving the "tab" functionality to a dedicated/separate widget


Done. Its rather crude and probably not so generic, but it works.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Creating a Widget of widgets

Postby Hooray » Wed Dec 03, 2014 7:30 pm

Necolatis wrote in Wed Dec 03, 2014 3:58 pm:
Hooray wrote in Sun Nov 30, 2014 6:58 pm:I'd consider moving the "tab" functionality to a dedicated/separate widget


Done. Its rather crude and probably not so generic, but it works.


it would seem like a good idea to update the "Canvas Widgets" article accordingly, so that other Canvas contributors are made aware of related widgets/work, to help avoid duplicate efforts: http://wiki.flightgear.org/Canvas_Widgets
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

Previous

Return to Canvas

Who is online

Users browsing this forum: No registered users and 2 guests