2013-03-06 00:03:47 +00:00
|
|
|
// Airports forward declarations
|
|
|
|
//
|
|
|
|
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2012-07-04 11:15:12 +00:00
|
|
|
|
2012-08-01 06:53:40 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2012-07-04 11:15:12 +00:00
|
|
|
#include "CanvasWidget.hxx"
|
|
|
|
|
|
|
|
#include <Canvas/canvas_mgr.hxx>
|
|
|
|
#include <Main/fg_os.hxx> // fgGetKeyModifiers()
|
|
|
|
#include <Scripting/NasalSys.hxx>
|
|
|
|
|
2017-10-22 18:25:12 +00:00
|
|
|
#include <osg/GLExtensions>
|
2017-12-05 21:39:30 +00:00
|
|
|
#include <osg/Version>
|
2017-10-22 18:25:12 +00:00
|
|
|
|
2012-11-18 15:38:35 +00:00
|
|
|
#include <simgear/canvas/Canvas.hxx>
|
2014-05-18 22:25:34 +00:00
|
|
|
#include <simgear/canvas/events/MouseEvent.hxx>
|
2012-11-18 15:38:35 +00:00
|
|
|
|
2012-07-04 11:15:12 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
CanvasWidget::CanvasWidget( int x, int y,
|
|
|
|
int width, int height,
|
|
|
|
SGPropertyNode* props,
|
|
|
|
const std::string& module ):
|
|
|
|
puObject(x, y, width, height),
|
2017-10-05 22:06:26 +00:00
|
|
|
_canvas_mgr(globals->get_subsystem<CanvasMgr>()),
|
2012-12-02 12:27:37 +00:00
|
|
|
_last_x(0),
|
2013-02-25 14:30:01 +00:00
|
|
|
_last_y(0),
|
2013-02-25 14:39:30 +00:00
|
|
|
// automatically resize viewport of canvas if no size is given
|
|
|
|
_auto_viewport( !props->hasChild("view") )
|
2012-07-04 11:15:12 +00:00
|
|
|
{
|
|
|
|
if( !_canvas_mgr )
|
|
|
|
{
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-18 15:38:35 +00:00
|
|
|
_canvas = _canvas_mgr->createCanvas
|
|
|
|
(
|
|
|
|
props->getStringValue("name", "gui-anonymous")
|
|
|
|
);
|
|
|
|
|
|
|
|
int view[2] = {
|
|
|
|
// Get canvas viewport size. If not specified use the widget dimensions
|
|
|
|
props->getIntValue("view[0]", width),
|
|
|
|
props->getIntValue("view[1]", height)
|
|
|
|
};
|
|
|
|
|
|
|
|
SGPropertyNode* cprops = _canvas->getProps();
|
|
|
|
cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
|
|
|
|
cprops->setIntValue("size[1]", view[1] * 2); // for antialias
|
|
|
|
cprops->setIntValue("view[0]", view[0]);
|
|
|
|
cprops->setIntValue("view[1]", view[1]);
|
|
|
|
cprops->setBoolValue("render-always", true);
|
|
|
|
cprops->setStringValue( "name",
|
|
|
|
props->getStringValue("name", "gui-anonymous") );
|
|
|
|
|
|
|
|
SGPropertyNode *nasal = props->getNode("nasal");
|
|
|
|
if( !nasal )
|
|
|
|
return;
|
|
|
|
|
2016-09-27 10:58:39 +00:00
|
|
|
FGNasalSys *nas = globals->get_subsystem<FGNasalSys>();
|
2012-11-18 15:38:35 +00:00
|
|
|
if( !nas )
|
|
|
|
SG_LOG( SG_GENERAL,
|
|
|
|
SG_ALERT,
|
|
|
|
"CanvasWidget: Failed to get nasal subsystem!" );
|
|
|
|
|
|
|
|
const std::string file = std::string("__canvas:")
|
|
|
|
+ cprops->getStringValue("name");
|
|
|
|
|
|
|
|
SGPropertyNode *load = nasal->getNode("load");
|
|
|
|
if( load )
|
2012-07-04 11:15:12 +00:00
|
|
|
{
|
2012-11-18 15:38:35 +00:00
|
|
|
const char *s = load->getStringValue();
|
|
|
|
nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
|
2012-07-04 11:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
CanvasWidget::~CanvasWidget()
|
|
|
|
{
|
|
|
|
if( _canvas )
|
2013-05-31 17:30:48 +00:00
|
|
|
_canvas->destroy();
|
2012-07-04 11:15:12 +00:00
|
|
|
}
|
|
|
|
|
2012-12-02 14:47:21 +00:00
|
|
|
// Old versions of PUI are missing this defines...
|
|
|
|
#ifndef PU_SCROLL_UP_BUTTON
|
|
|
|
# define PU_SCROLL_UP_BUTTON 3
|
|
|
|
#endif
|
|
|
|
#ifndef PU_SCROLL_DOWN_BUTTON
|
|
|
|
# define PU_SCROLL_DOWN_BUTTON 4
|
|
|
|
#endif
|
|
|
|
|
2012-07-04 11:15:12 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void CanvasWidget::doHit(int button, int updown, int x, int y)
|
|
|
|
{
|
|
|
|
puObject::doHit(button, updown, x, y);
|
|
|
|
|
|
|
|
// CTRL allows resizing and SHIFT allows moving the window
|
|
|
|
if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
|
|
|
|
return;
|
|
|
|
|
2012-12-02 12:27:37 +00:00
|
|
|
namespace sc = simgear::canvas;
|
|
|
|
sc::MouseEventPtr event(new sc::MouseEvent);
|
2012-12-06 22:27:25 +00:00
|
|
|
|
|
|
|
if( !_time )
|
|
|
|
_time = globals->get_props()->getNode("/sim/time/elapsed-sec");
|
|
|
|
event->time = _time->getDoubleValue();
|
|
|
|
|
2012-12-08 14:02:15 +00:00
|
|
|
if( !_view_height )
|
|
|
|
_view_height = globals->get_props()->getNode("/sim/gui/canvas/size[1]");
|
|
|
|
event->screen_pos.set(x, _view_height->getIntValue() - y);
|
2012-12-02 12:27:37 +00:00
|
|
|
|
2012-12-08 14:02:15 +00:00
|
|
|
event->client_pos.set(x - abox.min[0], abox.max[1] - y);
|
|
|
|
event->delta.set( event->getScreenX() - _last_x,
|
|
|
|
event->getScreenY() - _last_y );
|
|
|
|
|
|
|
|
_last_x = event->getScreenX();
|
|
|
|
_last_y = event->getScreenY();
|
2012-12-02 12:27:37 +00:00
|
|
|
|
|
|
|
switch( button )
|
|
|
|
{
|
|
|
|
case PU_LEFT_BUTTON:
|
|
|
|
case PU_MIDDLE_BUTTON:
|
|
|
|
case PU_RIGHT_BUTTON:
|
2014-03-15 14:01:09 +00:00
|
|
|
event->button = button;
|
2012-12-02 12:27:37 +00:00
|
|
|
break;
|
|
|
|
case PU_SCROLL_UP_BUTTON:
|
|
|
|
case PU_SCROLL_DOWN_BUTTON:
|
|
|
|
// Only let PU_DOWN trigger a scroll wheel event
|
|
|
|
if( updown != PU_DOWN )
|
|
|
|
return;
|
|
|
|
|
|
|
|
event->type = sc::Event::WHEEL;
|
|
|
|
event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
|
|
|
|
|
|
|
|
_canvas->handleMouseEvent(event);
|
|
|
|
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( updown )
|
|
|
|
{
|
|
|
|
case PU_DOWN:
|
|
|
|
event->type = sc::Event::MOUSE_DOWN;
|
|
|
|
puSetActiveWidget(this, x, y);
|
|
|
|
break;
|
|
|
|
case PU_UP:
|
|
|
|
event->type = sc::Event::MOUSE_UP;
|
|
|
|
puDeactivateWidget();
|
|
|
|
break;
|
|
|
|
case PU_DRAG:
|
|
|
|
event->type = sc::Event::DRAG;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_canvas->handleMouseEvent(event);
|
2012-07-04 11:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int CanvasWidget::checkKey(int key, int updown)
|
|
|
|
{
|
|
|
|
return puObject::checkKey(key, updown);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void CanvasWidget::setSize(int w, int h)
|
|
|
|
{
|
|
|
|
puObject::setSize(w, h);
|
|
|
|
|
2013-02-25 14:30:01 +00:00
|
|
|
if( _auto_viewport )
|
|
|
|
{
|
|
|
|
_canvas->getProps()->setIntValue("view[0]", w);
|
|
|
|
_canvas->getProps()->setIntValue("view[1]", h);
|
|
|
|
}
|
2012-07-04 11:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void CanvasWidget::draw(int dx, int dy)
|
|
|
|
{
|
2018-01-28 13:38:06 +00:00
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
2017-12-05 21:39:30 +00:00
|
|
|
#if OSG_VERSION_LESS_THAN(3,4,0)
|
|
|
|
#else
|
|
|
|
osg::GLExtensions* extensions = osg::GLExtensions::Get(0, true);
|
2018-01-28 13:38:06 +00:00
|
|
|
extensions->glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ZERO, GL_ONE);
|
2017-12-05 21:39:30 +00:00
|
|
|
#endif
|
2012-12-09 22:15:17 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, _canvas_mgr->getCanvasTexId(_canvas));
|
2012-07-04 11:15:12 +00:00
|
|
|
glBegin( GL_QUADS );
|
2017-10-05 22:06:26 +00:00
|
|
|
glColor4f(1,1,1, 1.0f);
|
2012-07-04 11:15:12 +00:00
|
|
|
glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
|
|
|
|
glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
|
|
|
|
glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
|
|
|
|
glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
|
|
|
|
glEnd();
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
2017-10-22 18:25:12 +00:00
|
|
|
glDisable(GL_BLEND);
|
2012-07-04 11:15:12 +00:00
|
|
|
}
|