]> git.mxchange.org Git - flightgear.git/blob - src/GUI/new_gui.cxx
Started new, XML-configurable GUI subsystem.
[flightgear.git] / src / GUI / new_gui.cxx
1 // new_gui.cxx: implementation of XML-configurable GUI support.
2
3 #include "new_gui.hxx"
4
5 #include <plib/pu.h>
6
7 #include <vector>
8 SG_USING_STD(vector);
9
10 #include <simgear/misc/exception.hxx>
11 #include <Main/fg_props.hxx>
12
13
14 NewGUI::NewGUI ()
15     : _activeObject(0)
16 {
17 }
18
19 NewGUI::~NewGUI ()
20 {
21 }
22
23 void
24 NewGUI::init ()
25 {
26     SGPropertyNode props;
27
28     try {
29         fgLoadProps("gui.xml", &props);
30     } catch (const sg_exception &ex) {
31         SG_LOG(SG_INPUT, SG_ALERT, "Error parsing gui.xml: "
32                << ex.getMessage());
33         return;
34     }
35
36     int nChildren = props.nChildren();
37     for (int i = 0; i < nChildren; i++) {
38         SGPropertyNode_ptr child = props.getChild(i);
39         if (!child->hasValue("name")) {
40             SG_LOG(SG_INPUT, SG_WARN, "GUI node " << child->getName()
41                    << " has no name; skipping.");
42         } else {
43             string name = child->getStringValue("name");
44             SG_LOG(SG_INPUT, SG_BULK, "Saving GUI node " << name);
45             _objects[name] = child;
46         }
47     }
48 }
49
50 void
51 NewGUI::update (double delta_time_sec)
52 {
53     // NO OP
54 }
55
56 static void
57 close_callback (puObject * object)
58 {
59     ((NewGUI *)object->getUserData())->closeActiveObject();
60 }
61
62 void
63 NewGUI::closeActiveObject ()
64 {
65     delete _activeObject;
66     _activeObject = 0;
67 }
68
69 void
70 NewGUI::display (const string &name)
71 {
72     if (_activeObject != 0) {
73         SG_LOG(SG_GENERAL, SG_ALERT, "Another GUI object is still active");
74         return;
75     }
76
77     if (_objects.find(name) == _objects.end()) {
78         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name << " not defined");
79         return;
80     }
81     SGPropertyNode_ptr props = _objects[name];
82     
83     _activeObject = makeObject(props, 1024, 768);
84
85     if (_activeObject != 0) {
86         _activeObject->reveal();
87     } else {
88         SG_LOG(SG_GENERAL, SG_ALERT, "Dialog " << name
89                << " does not contain a proper GUI definition");
90     }
91 }
92
93 puObject *
94 NewGUI::makeObject (SGPropertyNode * props, int parentWidth, int parentHeight)
95 {
96     int width = props->getIntValue("width", parentWidth);
97     int height = props->getIntValue("height", parentHeight);
98
99     int x = props->getIntValue("x", (parentWidth - width) / 2);
100     int y = props->getIntValue("y", (parentHeight - height) / 2);
101
102     string type = props->getName();
103
104     if (type == "dialog") {
105         puPopup * dialog;
106         if (props->getBoolValue("modal", false))
107             dialog = new puDialogBox(x, y);
108         else
109             dialog = new puPopup(x, y);
110         setupGroup(dialog, props, width, height, true);
111         return dialog;
112     } else if (type == "group") {
113         puGroup * group = new puGroup(x, y);
114         setupGroup(group, props, width, height, false);
115         return group;
116     } else if (type == "input") {
117         puInput * input = new puInput(x, y, x + width, y + height);
118         setupObject(input, props);
119         return input;
120     } else if (type == "text") {
121         puText * text = new puText(x, y);
122         setupObject(text, props);
123         return text;
124     } else if (type == "button") {
125         puButton * b;
126         const char * legend = props->getStringValue("legend", "[none]");
127         if (props->getBoolValue("one-shot", true))
128             b = new puOneShot(x, y, legend);
129         else
130             b = new puButton(x, y, legend);
131         setupObject(b, props);
132         b->setCallback(close_callback);
133         b->setUserData(this);
134         return b;
135     } else {
136         return 0;
137     }
138 }
139
140 void
141 NewGUI::setupObject (puObject * object, SGPropertyNode * props)
142 {
143     if (props->hasValue("legend"))
144         object->setLegend(props->getStringValue("legend"));
145
146     if (props->hasValue("label"))
147         object->setLabel(props->getStringValue("label"));
148
149     if (props->hasValue("default-value-prop"))
150         object->setValue(fgGetString(props->getStringValue("default-value-prop")));
151
152     object->makeReturnDefault(props->getBoolValue("default"));
153 }
154
155 void
156 NewGUI::setupGroup (puGroup * group, SGPropertyNode * props,
157                     int width, int height, bool makeFrame)
158 {
159     setupObject(group, props);
160
161     if (makeFrame)
162         new puFrame(0, 0, width, height);
163
164     int nChildren = props->nChildren();
165     for (int i = 0; i < nChildren; i++)
166         makeObject(props->getChild(i), width, height);
167     group->close();
168 }
169
170 // end of new_gui.cxx