]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CanvasWidget.cxx
Clean up CanvasWidget and more work on Canvas/Nasal bindings
[flightgear.git] / src / GUI / CanvasWidget.cxx
1 /*
2  * CanvasWidget.cxx
3  *
4  *  Created on: 03.07.2012
5  *      Author: tom
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #  include <config.h>
10 #endif
11
12 #include "CanvasWidget.hxx"
13
14 #include <Canvas/canvas_mgr.hxx>
15 #include <Main/fg_os.hxx>      // fgGetKeyModifiers()
16 #include <Scripting/NasalSys.hxx>
17
18 #include <simgear/canvas/Canvas.hxx>
19
20 //------------------------------------------------------------------------------
21 CanvasWidget::CanvasWidget( int x, int y,
22                             int width, int height,
23                             SGPropertyNode* props,
24                             const std::string& module ):
25   puObject(x, y, width, height),
26   _canvas_mgr( dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas")) ),
27   _tex_id(0),
28   _no_tex_cnt(0)
29 {
30   if( !_canvas_mgr )
31   {
32     SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
33     return;
34   }
35
36   _canvas = _canvas_mgr->createCanvas
37   (
38     props->getStringValue("name", "gui-anonymous")
39   );
40
41   int view[2] = {
42     // Get canvas viewport size. If not specified use the widget dimensions
43     props->getIntValue("view[0]", width),
44     props->getIntValue("view[1]", height)
45   };
46
47   SGPropertyNode* cprops = _canvas->getProps();
48   cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
49   cprops->setIntValue("size[1]", view[1] * 2); // for antialias
50   cprops->setIntValue("view[0]", view[0]);
51   cprops->setIntValue("view[1]", view[1]);
52   cprops->setBoolValue("render-always", true);
53   cprops->setStringValue( "name",
54                            props->getStringValue("name", "gui-anonymous") );
55   SGPropertyNode* input = cprops->getChild("input", 0, true);
56   _mouse_x = input->getChild("mouse-x", 0, true);
57   _mouse_y = input->getChild("mouse-y", 0, true);
58   _mouse_down = input->getChild("mouse-down", 0, true);
59   _mouse_drag = input->getChild("mouse-drag", 0, true);
60
61   SGPropertyNode *nasal = props->getNode("nasal");
62   if( !nasal )
63     return;
64
65   FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
66   if( !nas )
67     SG_LOG( SG_GENERAL,
68             SG_ALERT,
69             "CanvasWidget: Failed to get nasal subsystem!" );
70
71   const std::string file = std::string("__canvas:")
72                          + cprops->getStringValue("name");
73
74   SGPropertyNode *load = nasal->getNode("load");
75   if( load )
76   {
77     const char *s = load->getStringValue();
78     nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
79   }
80 }
81
82 //------------------------------------------------------------------------------
83 CanvasWidget::~CanvasWidget()
84 {
85   if( _canvas )
86     // TODO check if really not in use anymore
87     _canvas->getProps()
88            ->getParent()
89            ->removeChild( _canvas->getProps()->getName(),
90                           _canvas->getProps()->getIndex(),
91                           false );
92 }
93
94 //------------------------------------------------------------------------------
95 void CanvasWidget::doHit(int button, int updown, int x, int y)
96 {
97   puObject::doHit(button, updown, x, y);
98
99   // CTRL allows resizing and SHIFT allows moving the window
100   if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
101     return;
102
103   _mouse_x->setIntValue(x - abox.min[0]);
104   _mouse_y->setIntValue(abox.max[1] - y);
105
106   if( updown == PU_DRAG )
107     _mouse_drag->setIntValue(button);
108   else if( updown == PU_DOWN )
109     _mouse_down->setIntValue(button);
110
111   if( button != active_mouse_button )
112     return;
113
114   if (updown == PU_UP)
115     puDeactivateWidget();
116   else if (updown == PU_DOWN)
117     puSetActiveWidget(this, x, y);
118 }
119
120 //------------------------------------------------------------------------------
121 int CanvasWidget::checkKey(int key, int updown)
122 {
123   return puObject::checkKey(key, updown);
124 }
125
126 //------------------------------------------------------------------------------
127 void CanvasWidget::setSize(int w, int h)
128 {
129   puObject::setSize(w, h);
130
131   _canvas->getProps()->setIntValue("view[0]", w);
132   _canvas->getProps()->setIntValue("view[1]", h);
133 }
134
135 //------------------------------------------------------------------------------
136 void CanvasWidget::draw(int dx, int dy)
137 {
138   if( !_tex_id )
139   {
140     _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
141
142     // Normally we should be able to get the texture after one frame. I don't
143     // know if there are circumstances where it can take longer, so we don't
144     // log a warning message until we have tried a few times.
145     if( !_tex_id )
146     {
147       if( ++_no_tex_cnt == 5 )
148         SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
149       return;
150     }
151     else
152     {
153       if( _no_tex_cnt >= 5 )
154         SG_LOG
155         (
156           SG_GENERAL,
157           SG_INFO,
158           "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
159         );
160       _no_tex_cnt = 0;
161     }
162   }
163
164   glEnable(GL_TEXTURE_2D);
165   glBindTexture(GL_TEXTURE_2D, _tex_id);
166   glBegin( GL_QUADS );
167     glColor3f(1,1,1);
168     glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
169     glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
170     glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
171     glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
172   glEnd();
173   glDisable(GL_TEXTURE_2D);
174 }