From 2de88f278ba7bf4c5b38f9721d1240f5acee77cc Mon Sep 17 00:00:00 2001 From: david Date: Thu, 7 Nov 2002 16:27:47 +0000 Subject: [PATCH] Started new, XML-configurable GUI subsystem. --- src/GUI/Makefile.am | 1 + src/GUI/new_gui.cxx | 170 +++++++++++++++++++++++++++++++++++++++ src/GUI/new_gui.hxx | 46 +++++++++++ src/Main/fg_commands.cxx | 10 +++ src/Main/fg_init.cxx | 12 ++- 5 files changed, 238 insertions(+), 1 deletion(-) create mode 100644 src/GUI/new_gui.cxx create mode 100644 src/GUI/new_gui.hxx diff --git a/src/GUI/Makefile.am b/src/GUI/Makefile.am index ad177f0b2..e62874385 100644 --- a/src/GUI/Makefile.am +++ b/src/GUI/Makefile.am @@ -8,6 +8,7 @@ endif libGUI_a_SOURCES = \ apt_dlg.cxx apt_dlg.hxx \ + new_gui.cxx new_gui.hxx \ gui.cxx gui.h gui_funcs.cxx \ gui_local.cxx gui_local.hxx \ mouse.cxx \ diff --git a/src/GUI/new_gui.cxx b/src/GUI/new_gui.cxx new file mode 100644 index 000000000..9301ba92b --- /dev/null +++ b/src/GUI/new_gui.cxx @@ -0,0 +1,170 @@ +// new_gui.cxx: implementation of XML-configurable GUI support. + +#include "new_gui.hxx" + +#include + +#include +SG_USING_STD(vector); + +#include +#include
+ + +NewGUI::NewGUI () + : _activeObject(0) +{ +} + +NewGUI::~NewGUI () +{ +} + +void +NewGUI::init () +{ + SGPropertyNode props; + + try { + fgLoadProps("gui.xml", &props); + } catch (const sg_exception &ex) { + SG_LOG(SG_INPUT, SG_ALERT, "Error parsing gui.xml: " + << ex.getMessage()); + return; + } + + int nChildren = props.nChildren(); + for (int i = 0; i < nChildren; i++) { + SGPropertyNode_ptr child = props.getChild(i); + if (!child->hasValue("name")) { + SG_LOG(SG_INPUT, SG_WARN, "GUI node " << child->getName() + << " has no name; skipping."); + } else { + string name = child->getStringValue("name"); + SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name); + _objects[name] = child; + } + } +} + +void +NewGUI::update (double delta_time_sec) +{ + // NO OP +} + +static void +close_callback (puObject * object) +{ + ((NewGUI *)object->getUserData())->closeActiveObject(); +} + +void +NewGUI::closeActiveObject () +{ + delete _activeObject; + _activeObject = 0; +} + +void +NewGUI::display (const string &name) +{ + if (_activeObject != 0) { + SG_LOG(SG_GENERAL, SG_ALERT, "Another GUI object is still active"); + return; + } + + if (_objects.find(name) == _objects.end()) { + SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined"); + return; + } + SGPropertyNode_ptr props = _objects[name]; + + _activeObject = makeObject(props, 1024, 768); + + if (_activeObject != 0) { + _activeObject->reveal(); + } else { + SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name + << " does not contain a proper GUI definition"); + } +} + +puObject * +NewGUI::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight) +{ + int width = props->getIntValue("width", parentWidth); + int height = props->getIntValue("height", parentHeight); + + int x = props->getIntValue("x", (parentWidth - width) / 2); + int y = props->getIntValue("y", (parentHeight - height) / 2); + + string type = props->getName(); + + if (type == "dialog") { + puPopup * dialog; + if (props->getBoolValue("modal", false)) + dialog = new puDialogBox(x, y); + else + dialog = new puPopup(x, y); + setupGroup(dialog, props, width, height, true); + return dialog; + } else if (type == "group") { + puGroup * group = new puGroup(x, y); + setupGroup(group, props, width, height, false); + return group; + } else if (type == "input") { + puInput * input = new puInput(x, y, x + width, y + height); + setupObject(input, props); + return input; + } else if (type == "text") { + puText * text = new puText(x, y); + setupObject(text, props); + return text; + } else if (type == "button") { + puButton * b; + const char * legend = props->getStringValue("legend", "[none]"); + if (props->getBoolValue("one-shot", true)) + b = new puOneShot(x, y, legend); + else + b = new puButton(x, y, legend); + setupObject(b, props); + b->setCallback(close_callback); + b->setUserData(this); + return b; + } else { + return 0; + } +} + +void +NewGUI::setupObject (puObject * object, SGPropertyNode * props) +{ + if (props->hasValue("legend")) + object->setLegend(props->getStringValue("legend")); + + if (props->hasValue("label")) + object->setLabel(props->getStringValue("label")); + + if (props->hasValue("default-value-prop")) + object->setValue(fgGetString(props->getStringValue("default-value-prop"))); + + object->makeReturnDefault(props->getBoolValue("default")); +} + +void +NewGUI::setupGroup (puGroup * group, SGPropertyNode * props, + int width, int height, bool makeFrame) +{ + setupObject(group, props); + + if (makeFrame) + new puFrame(0, 0, width, height); + + int nChildren = props->nChildren(); + for (int i = 0; i < nChildren; i++) + makeObject(props->getChild(i), width, height); + group->close(); +} + +// end of new_gui.cxx diff --git a/src/GUI/new_gui.hxx b/src/GUI/new_gui.hxx new file mode 100644 index 000000000..3030cae77 --- /dev/null +++ b/src/GUI/new_gui.hxx @@ -0,0 +1,46 @@ +#ifndef __NEW_GUI_HXX +#define __NEW_GUI_HXX 1 + +#ifndef __cplusplus +# error This library requires C++ +#endif + +#include + +#include // for SG_USING_STD +#include + +#include +SG_USING_STD(map); + +#include
+ + +class NewGUI : public FGSubsystem +{ +public: + + NewGUI (); + virtual ~NewGUI (); + virtual void init (); + virtual void update (double delta_time_sec); + + virtual void closeActiveObject (); + virtual void display (const string &name); + +private: + + puObject * makeObject (SGPropertyNode * props, + int parentWidth, int parentHeight); + + void setupObject (puObject * object, SGPropertyNode * props); + + void setupGroup (puGroup * group, SGPropertyNode * props, + int width, int height, bool makeFrame = false); + + map _objects; + puObject * _activeObject; + +}; + +#endif // __NEW_GUI_HXX diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 4f6284709..5f79c96f5 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -580,6 +581,14 @@ do_property_scale (const SGPropertyNode * arg) return prop->setDoubleValue((setting + offset) * factor); } +static bool +do_gui (const SGPropertyNode * arg) +{ + NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui"); + gui->display(arg->getStringValue("name")); + return true; +} + //////////////////////////////////////////////////////////////////////// @@ -614,6 +623,7 @@ static struct { { "property-multiply", do_property_multiply }, { "property-swap", do_property_swap }, { "property-scale", do_property_scale }, + { "gui", do_gui }, { 0, 0 } // zero-terminated }; diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index a603ef61f..22f9ee34f 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -94,6 +94,7 @@ #include #include #include +#include #include #include #include @@ -965,7 +966,7 @@ bool fgInitSubsystems( void ) { //////////////////////////////////////////////////////////////////// - // Initialize the logger. + // Create and register the logger. //////////////////////////////////////////////////////////////////// globals->get_subsystem_mgr()->add(FGSubsystemMgr::GENERAL, @@ -973,6 +974,15 @@ bool fgInitSubsystems( void ) { new FGLogger); + //////////////////////////////////////////////////////////////////// + // Create and register the XML GUI. + //////////////////////////////////////////////////////////////////// + + globals->get_subsystem_mgr()->add(FGSubsystemMgr::INIT, + "gui", + new NewGUI); + + //////////////////////////////////////////////////////////////////// // Initialize the local time subsystem. //////////////////////////////////////////////////////////////////// -- 2.39.5