root / trunk / tbeta / Windows / addons / ofxNCore / src / Controls / ofxGui.cpp @ 87

View | Annotate | Download (5.7 KB)

1
/*
2
 *  ofxGui.cpp
3
 *  openFrameworks
4
 *
5
 *  Created by Stefan Kirch on 18.06.08.
6
 *  Copyright 2008 alphakanal. All rights reserved.
7
 *
8
 */
9
10
//        ----------------------------------------------------------------------------------------------------
11
12
#include "ofxGui.h"
13
14
//        ----------------------------------------------------------------------------------------------------
15
16
ofxGui* ofxGui::Instance(ofxGuiListener* listener)
17
{
18
        static ofxGui gui(listener);        
19
        return &gui;
20
}
21
22
//        ----------------------------------------------------------------------------------------------------
23
24
ofxGui::ofxGui(ofxGuiListener* listener)
25
{
26
        mIsActive                        = false;
27
        mDoUpdate                        = false;
28
        mXmlDone                        = true;
29
        mGlobals                        = ofxGuiGlobals::Instance();
30
        mGlobals->mListener        = listener;
31
}
32
33
//        ----------------------------------------------------------------------------------------------------
34
35
void ofxGui::update(int parameterId, int type, void* data, int length)
36
{
37
        if(mIsActive || mDoUpdate)
38
        {
39
                ofxGuiObject*        tmpObj;
40
                bool                        handled;
41
                
42
                for(int i = 0; i < mObjects.size(); i++)
43
                {
44
                        tmpObj        = (ofxGuiObject*)mObjects.at(i);
45
                        handled        = tmpObj->update(parameterId, type, data, length);
46
47
                        if(handled)
48
                                break;
49
                }
50
        }
51
}
52
53
//        ----------------------------------------------------------------------------------------------------
54
55
void ofxGui::draw()
56
{
57
        if(mIsActive)
58
        {
59
                ofEnableAlphaBlending();
60
                
61
                ofxGuiObject* tmpObj;
62
                
63
                for(int i = 0; i < mObjects.size(); i++)
64
                {
65
                        tmpObj = (ofxGuiObject*)mObjects.at(i);
66
                        tmpObj->draw();
67
                }
68
                
69
                ofDisableAlphaBlending();
70
        }
71
}
72
73
//        ----------------------------------------------------------------------------------------------------
74
75
void ofxGui::activate(bool activate)
76
{
77
        mIsActive = activate;
78
}
79
80
//        ----------------------------------------------------------------------------------------------------
81
82
void ofxGui::forceUpdate(bool update)
83
{
84
        mDoUpdate = update;
85
}
86
87
//        ----------------------------------------------------------------------------------------------------
88
89
void ofxGui::keyPressed(int key)
90
{
91
        
92
}
93
94
//        ----------------------------------------------------------------------------------------------------
95
96
void ofxGui::keyReleased(int key)
97
{
98
        
99
}
100
101
//        ----------------------------------------------------------------------------------------------------
102
103
void ofxGui::mouseDragged(int x, int y, int button)
104
{
105
        if(mIsActive)
106
        {
107
                ofxGuiObject*        tmpObj;
108
                bool                        handled;
109
110
                for(int i = 0; i < mObjects.size(); i++)
111
                {
112
                        tmpObj        = (ofxGuiObject*)mObjects.at(i);
113
                        handled        = tmpObj->mouseDragged(x, y, button);
114
115
                        if(handled)
116
                                break;
117
                }
118
        }
119
}
120
121
//        ----------------------------------------------------------------------------------------------------
122
123
void ofxGui::mousePressed(int x, int y, int button)
124
{
125
        if(mIsActive)
126
        {
127
                ofxGuiObject*        tmpObj;
128
                bool                        handled;
129
                
130
                for(int i = 0; i < mObjects.size(); i++)
131
                {
132
                        tmpObj        = (ofxGuiObject*)mObjects.at(i);
133
                        handled        = tmpObj->mousePressed(x, y, button);
134
                        
135
                        if(handled)
136
                                break;
137
                }
138
        }
139
}
140
141
//        ----------------------------------------------------------------------------------------------------
142
143
void ofxGui::mouseReleased(int x, int y, int button)
144
{
145
        if(mIsActive)
146
        {
147
                ofxGuiObject* tmpObj;
148
                
149
                for(int i = 0; i < mObjects.size(); i++)
150
                {
151
                        tmpObj = (ofxGuiObject*)mObjects.at(i);
152
                        tmpObj->mouseReleased(x, y, button);
153
                }
154
        }
155
}
156
157
//        ----------------------------------------------------------------------------------------------------
158
159
ofxGuiPanel* ofxGui::addPanel(int id, string name, int x, int y, int border, int spacing)
160
{
161
        ofxGuiPanel* newPanel = new ofxGuiPanel();
162
        newPanel->init(id, name, x, y, border, spacing);
163
        mObjects.push_back(newPanel);
164
        
165
        return newPanel;
166
}
167
168
//        ----------------------------------------------------------------------------------------------------
169
170
bool ofxGui::buildFromXml(string file)
171
{
172
        if(!mXmlDone)
173
                return false;
174
        
175
        if(!mGlobals->mXml.loadFile(file))
176
                return false;
177
        
178
        int numberOfTags = mGlobals->mXml.getNumTags("UI");
179
        
180
        if(numberOfTags != 1)
181
                return false;
182
        
183
        mObjects.clear();
184
        
185
        mXmlDone                        = false;
186
        mGlobals->mXmlfile        = file;
187
        
188
        mGlobals->mXml.pushTag("UI", 0);
189
190
        mIsActive        = mGlobals->mXml.getValue("ISACTIVE", 0);
191
        mDoUpdate        = mGlobals->mXml.getValue("DOUPDATE", 0);
192
193
        mGlobals->buildFromXml();
194
        
195
        numberOfTags = mGlobals->mXml.getNumTags("OBJECT");
196
        
197
        if(numberOfTags > 0)
198
        {
199
                for(int i = 0; i < numberOfTags; i++)
200
                {
201
                        mGlobals->mXml.pushTag("OBJECT", i);
202
                        
203
                        int                id                = mGlobals->mXml.getValue("ID", 0);
204
                        string        type        = mGlobals->mXml.getValue("TYPE", "");
205
                        string        name        = mGlobals->mXml.getValue("NAME", "");
206
                        int                x                = mGlobals->mXml.getValue("LEFT", 0);
207
                        int                y                = mGlobals->mXml.getValue("TOP", 0);
208
                        int                border        = mGlobals->mXml.getValue("BORDER", 0);
209
                        int                spacing        = mGlobals->mXml.getValue("SPACING", 0);
210
                        
211
                        if(type == "PANEL")
212
                        {
213
                                ofxGuiPanel* panel = addPanel(id, name, x, y, border, spacing);
214
                                panel->buildFromXml();
215
                        }
216
                        
217
                        mGlobals->mXml.popTag();
218
                }
219
        }
220
        
221
        mGlobals->mXml.popTag();
222
        
223
        mXmlDone = true;
224
        
225
        return true;
226
}
227
228
//        ----------------------------------------------------------------------------------------------------
229
230
void ofxGui::saveToXml(string file)
231
{
232
        if(!mXmlDone)
233
                return;
234
235
        mXmlDone = false;
236
237
        mGlobals->mXml.clear();
238
                
239
        int id = mGlobals->mXml.addTag("UI");
240
        
241
        mGlobals->mXml.setValue("UI:VERSION", OFXGUI_VERSION, id);
242
243
        mGlobals->mXml.setValue("UI:ISACTIVE", mIsActive, id);
244
        mGlobals->mXml.setValue("UI:DOUPDATE", mDoUpdate, id);
245
                
246
        mGlobals->mXml.pushTag("UI", id);
247
248
        mGlobals->saveToXml();
249
250
        ofxGuiObject* tmpObj;
251
252
        for(int i = 0; i < mObjects.size(); i++)
253
        {
254
                tmpObj = (ofxGuiObject*)mObjects.at(i);
255
                tmpObj->saveToXml();
256
        }
257
        
258
        mGlobals->mXml.popTag();
259
        mGlobals->mXml.saveFile(file);
260
261
        mXmlDone = true;
262
}
263
264
//        ----------------------------------------------------------------------------------------------------