root / branches / tbeta / ccv_ofx_0061 / Windows / addons / ofxNCore / src / Controls / ofxGuiPanel.cpp @ 217

View | Annotate | Download (15 KB)

1
/*
2
 *  ofxGuiPanel.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 "ofxGuiPanel.h"
13
14
//        ----------------------------------------------------------------------------------------------------
15
16
ofxGuiPanel::ofxGuiPanel()
17
{
18
        mParamType = kofxGui_Object_Panel;
19
}
20
21
//        ----------------------------------------------------------------------------------------------------
22
23
void ofxGuiPanel::init(int id, string name, int x, int y, int border, int spacing)
24
{
25
        int        textHeight        = (name == "") ? 2 * border :  2 * border +mGlobals->mHeadFontHeight;
26
27
        mParamId                = id;
28
        mParamName                = name;
29
30
        mObjX                        = x; 
31
        mObjY                        = y;
32
33
        mBorder                        = border;
34
        mSpacing                = spacing;
35
        
36
        adjustToNewContent(roundInt(mGlobals->mHeadFont.stringWidth(name)), textHeight);
37
}
38
39
//        ----------------------------------------------------------------------------------------------------
40
41
bool ofxGuiPanel::update(int parameterId, int task, void* data, int length)
42
{
43
        bool                        handled        = false;
44
        ofxGuiObject*        tmpObj        = NULL;
45
        
46
        for(int i = 0; i < mObjects.size(); i++)
47
        {
48
                tmpObj        = (ofxGuiObject*)mObjects.at(i);
49
                handled        = tmpObj->update(parameterId, task, data, length);
50
                
51
                if(handled)
52
                        break;
53
        }
54
        
55
        return handled;
56
}
57
58
//        ----------------------------------------------------------------------------------------------------
59
60
void ofxGuiPanel::draw()
61
{
62
        glPushMatrix();
63
                
64
                glTranslatef(mObjX, mObjY, 0.0f);
65
66
                ofFill();
67
                
68
                //        background
69
                glColor4f(mGlobals->mCoverColor.r, mGlobals->mCoverColor.g, mGlobals->mCoverColor.b, mGlobals->mCoverColor.a);
70
                ofRect(0.0f, 0.0f, mObjWidth, mObjHeight);
71
        
72
                ofNoFill();
73
                
74
                if(mParamName != "")
75
                        drawHeadString(mBorder, mBorder, mParamName, false);
76
77
                if(mBorder > 0)
78
                {
79
                        //        border
80
                        glColor4f(mGlobals->mBorderColor.r, mGlobals->mBorderColor.g, mGlobals->mBorderColor.b, mGlobals->mBorderColor.a);
81
                        ofRect(0.0f, 0.0f, mObjWidth, mObjHeight);
82
                }
83
        
84
                ofxGuiObject* tmpObj;
85
        
86
                for(int i = 0; i < mObjects.size(); i++)
87
                {
88
                        tmpObj = (ofxGuiObject*)mObjects.at(i);
89
                        tmpObj->draw();
90
                }
91
        
92
        glPopMatrix();
93
}
94
95
//        ----------------------------------------------------------------------------------------------------
96
97
bool ofxGuiPanel::mouseDragged(int x, int y, int button)
98
{
99
        bool handled = false;
100
        
101
        if(mMouseIsDown)
102
        {                
103
                ofxPoint2f inside = mouseToLocal(x, y);
104
                
105
                ofxGuiObject* tmpObj;
106
                
107
                for(int i = 0; i < mObjects.size(); i++)
108
                {
109
                        tmpObj        = (ofxGuiObject*)mObjects.at(i);
110
                        handled        = tmpObj->mouseDragged(inside.x, inside.y, button);
111
                        
112
                        if(handled)
113
                                break;
114
                }
115
        }
116
        
117
        return handled;
118
}
119
120
//        ----------------------------------------------------------------------------------------------------
121
122
bool ofxGuiPanel::mousePressed(int x, int y, int button)
123
{
124
        bool                handled        = false;
125
        ofxPoint2f        inside        = mouseToLocal(x, y);
126
        
127
        if(isPointInsideMe(inside))
128
        {                
129
                ofxGuiObject* tmpObj;
130
                
131
                for(int i = 0; i < mObjects.size(); i++)
132
                {
133
                        tmpObj        = (ofxGuiObject*)mObjects.at(i);
134
                        handled        = tmpObj->mousePressed(inside.x, inside.y, button);
135
                        
136
                        if(handled)
137
                                break;
138
                }
139
                
140
                mMouseIsDown = true;
141
        }
142
        
143
        return handled;
144
}
145
146
//        ----------------------------------------------------------------------------------------------------
147
148
bool ofxGuiPanel::mouseReleased(int x, int y, int button)
149
{
150
        bool                handled        = false;
151
        ofxPoint2f        inside        = mouseToLocal(x, y);
152
        
153
        ofxGuiObject* tmpObj;
154
        
155
        for(int i = 0; i < mObjects.size(); i++)
156
        {
157
                tmpObj        = (ofxGuiObject*)mObjects.at(i);
158
                handled        = tmpObj->mouseReleased(inside.x, inside.y, button);
159
                
160
                if(handled)
161
                        break;
162
        }
163
        
164
        mMouseIsDown = false;
165
        
166
        return handled;
167
}
168
169
//        ----------------------------------------------------------------------------------------------------
170
171
ofxGuiObject* ofxGuiPanel::addSlider(int id, string name, int width, int height, float min, float max, float value, int display, int steps)
172
{
173
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
174
        
175
        ofxGuiSlider* slider = new ofxGuiSlider();
176
        slider->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, min, max, value, display, steps);
177
        mObjects.push_back(slider);
178
179
        adjustToNewContent(slider->mObjWidth, slider->mObjHeight + offset);
180
        
181
        return slider;
182
}
183
184
//        ----------------------------------------------------------------------------------------------------
185
186
ofxGuiObject* ofxGuiPanel::addXYPad(int id, string name, int width, int height, ofxPoint2f min, ofxPoint2f max, ofxPoint2f value, int display, int steps)
187
{
188
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
189
190
        ofxGuiXYPad* xypad = new ofxGuiXYPad();
191
        xypad->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, min, max, value, display, steps);
192
        mObjects.push_back(xypad);
193
        
194
        adjustToNewContent(xypad->mObjWidth, xypad->mObjHeight + offset);
195
        
196
        return xypad;
197
}
198
199
//        ----------------------------------------------------------------------------------------------------
200
201
ofxGuiObject* ofxGuiPanel::addPoints(int id, string name, int width, int height, ofxPoint2f min, ofxPoint2f max, ofxPoint2f value, int display, int steps)
202
{
203
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
204
205
        ofxGuiPoints* points = new ofxGuiPoints();
206
        points->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, min, max, value, display, steps);
207
        mObjects.push_back(points);
208
        
209
        adjustToNewContent(points->mObjWidth, points->mObjHeight + offset);
210
        
211
        return points;
212
}
213
214
//        ----------------------------------------------------------------------------------------------------
215
216
ofxGuiObject* ofxGuiPanel::addButton(int id, string name, int width, int height, bool value, int display)
217
{
218
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
219
220
        ofxGuiButton* button = new ofxGuiButton();
221
        button->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, value, display);
222
        mObjects.push_back(button);
223
        
224
        adjustToNewContent(button->mObjWidth, button->mObjHeight + offset);
225
        
226
        return button;
227
}
228
229
//        ----------------------------------------------------------------------------------------------------
230
231
ofxGuiObject* ofxGuiPanel::addFiles(int id, string name, int width, int height, string value, string subPath, string suffix)
232
{
233
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
234
235
        ofxGuiFiles* files = new ofxGuiFiles();
236
        files->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, value, subPath, suffix);
237
        mObjects.push_back(files);
238
        
239
        adjustToNewContent(files->mObjWidth, files->mObjHeight + offset);
240
        
241
        return files;
242
}
243
244
//        ----------------------------------------------------------------------------------------------------
245
246
ofxGuiObject* ofxGuiPanel::addColor(int id, string name, int width, int height, ofRGBA value, int display)
247
{
248
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
249
        
250
        ofxGuiColor* color = new ofxGuiColor();
251
        color->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, value, display);
252
        mObjects.push_back(color);
253
        
254
        adjustToNewContent(color->mObjWidth, color->mObjHeight + offset);
255
        
256
        return color;
257
}
258
259
//        ----------------------------------------------------------------------------------------------------
260
261
ofxGuiObject* ofxGuiPanel::addMatrix(int id, string name, int width, int height, int xGrid, int yGrid, int value, int mode, int spacing)
262
{
263
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
264
        
265
        ofxGuiMatrix* matrix = new ofxGuiMatrix();
266
        matrix->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, xGrid, yGrid, value, mode, spacing);
267
        mObjects.push_back(matrix);
268
        
269
        adjustToNewContent(matrix->mObjWidth, matrix->mObjHeight + offset);
270
        
271
        return matrix;
272
}
273
274
//        ----------------------------------------------------------------------------------------------------
275
276
ofxGuiObject* ofxGuiPanel::addScope(int id, string name, int width, int height, int length, ofxPoint2f value, int display)
277
{
278
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
279
        
280
        ofxGuiScope* scope = new ofxGuiScope();
281
        scope->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, length, value, display);
282
        mObjects.push_back(scope);
283
        
284
        adjustToNewContent(scope->mObjWidth, scope->mObjHeight + offset);
285
        
286
        return scope;
287
}
288
289
//        ----------------------------------------------------------------------------------------------------
290
291
ofxGuiObject* ofxGuiPanel::addKnob(int id, string name, int width, int height, float min, float max, float value, int display, int steps)
292
{
293
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
294
        
295
        ofxGuiKnob* knob = new ofxGuiKnob();
296
        knob->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, min, max, value, display, steps);
297
        mObjects.push_back(knob);
298
        
299
        adjustToNewContent(knob->mObjWidth, knob->mObjHeight + offset);
300
        
301
        return knob;
302
}
303
304
//        ----------------------------------------------------------------------------------------------------
305
306
ofxGuiObject* ofxGuiPanel::addRadar(int id, string name, int width, int height, float min, float max, float value, int display, int steps)
307
{
308
        //        todo
309
        return NULL;
310
}
311
312
//        ----------------------------------------------------------------------------------------------------
313
314
ofxGuiObject* ofxGuiPanel::addSwitch(int id, string name, int width, int height, int min, int max, int value, const string* paramStrings)
315
{
316
        int offset = (mObjects.size() == 0 && mParamName == "") ? 0 : mSpacing;
317
        
318
        ofxGuiSwitch* swtch = new ofxGuiSwitch();
319
        swtch->init(id, name, mBorder, mObjHeight - mBorder + offset, width, height, min, max, value, paramStrings);
320
        mObjects.push_back(swtch);
321
        
322
        adjustToNewContent(swtch->mObjWidth, swtch->mObjHeight + offset);
323
        
324
        return swtch;
325
}
326
327
//        ----------------------------------------------------------------------------------------------------
328
329
void ofxGuiPanel::adjustToNewContent(int width, int height)
330
{
331
        if(width > mObjWidth - mBorder * 2)
332
                mObjWidth = width + mBorder * 2;
333
334
        mObjHeight += height;
335
        
336
        setControlRegion(mBorder, mBorder, mObjWidth - mBorder, mObjHeight - mBorder);
337
}
338
339
//        ----------------------------------------------------------------------------------------------------
340
341
void ofxGuiPanel::buildFromXml()
342
{
343
        int numberOfTags = mGlobals->mXml.getNumTags("OBJECT");
344
        
345
        if(numberOfTags > 0)
346
        {
347
                for(int i = 0; i < numberOfTags; i++)
348
                {
349
                        mGlobals->mXml.pushTag("OBJECT", i);
350
351
                        int                id                = mGlobals->mXml.getValue("ID", 0);
352
                        string        type        = mGlobals->mXml.getValue("TYPE", "");
353
                        string        name        = mGlobals->mXml.getValue("NAME", "");
354
                        int                width        = mGlobals->mXml.getValue("WIDTH", 0);
355
                        int                height        = mGlobals->mXml.getValue("HEIGHT", 0);
356
                        int                display        = mGlobals->mXml.getValue("DISPLAY", 0);
357
                        int                steps        = mGlobals->mXml.getValue("STEPS", 0);
358
                        int                mode        = mGlobals->mXml.getValue("MODE", 0);
359
                        
360
                        if(type == "SLIDER")
361
                        {
362
                                float        min                = mGlobals->mXml.getValue("MIN", 0.0f);
363
                                float        max                = mGlobals->mXml.getValue("MAX", 0.0f);
364
                                float        value        = mGlobals->mXml.getValue("VALUE", 0.0f);
365
366
                                ofxGuiObject* slider = addSlider(id, name, width, height, min, max, value, display, steps);
367
                                slider->buildFromXml();
368
                        }
369
                        else if(type == "XYPAD")
370
                        {
371
                                float        minx                = mGlobals->mXml.getValue("MIN_X", 0.0f);
372
                                float        miny                = mGlobals->mXml.getValue("MIN_Y", 0.0f);
373
                                float        maxx                = mGlobals->mXml.getValue("MAX_X", 0.0f);
374
                                float        maxy                = mGlobals->mXml.getValue("MAX_Y", 0.0f);
375
                                float        valuex                = mGlobals->mXml.getValue("VALUE_X", 0.0f);
376
                                float        valuey                = mGlobals->mXml.getValue("VALUE_Y", 0.0f);
377
                                
378
                                ofxPoint2f min                = ofxPoint2f(minx, miny);
379
                                ofxPoint2f max                = ofxPoint2f(maxx, maxy);
380
                                ofxPoint2f value        = ofxPoint2f(valuex, valuey);
381
                                
382
                                ofxGuiObject* xypad = addXYPad(id, name, width, height, min, max, value, display, steps);
383
                                xypad->buildFromXml();
384
                        }
385
                        else if(type == "POINTS")
386
                        {
387
                                float        minx                = mGlobals->mXml.getValue("MIN_X", 0.0f);
388
                                float        miny                = mGlobals->mXml.getValue("MIN_Y", 0.0f);
389
                                float        maxx                = mGlobals->mXml.getValue("MAX_X", 0.0f);
390
                                float        maxy                = mGlobals->mXml.getValue("MAX_Y", 0.0f);
391
                                float        valuex                = mGlobals->mXml.getValue("VALUE_X", 0.0f);
392
                                float        valuey                = mGlobals->mXml.getValue("VALUE_Y", 0.0f);
393
                                
394
                                ofxPoint2f min                = ofxPoint2f(minx, miny);
395
                                ofxPoint2f max                = ofxPoint2f(maxx, maxy);
396
                                ofxPoint2f value        = ofxPoint2f(valuex, valuey);
397
                                
398
                                ofxGuiObject* points = addPoints(id, name, width, height, min, max, value, display, steps);
399
                                points->buildFromXml();
400
                        }
401
                        else if(type == "BUTTON")
402
                        {
403
                                bool value = mGlobals->mXml.getValue("VALUE", 0);
404
                                
405
                                ofxGuiObject* button = addButton(id, name, width, height, value, mode);
406
                                button->buildFromXml();
407
                        }
408
                        else if(type == "FILES")
409
                        {
410
                                string        subPath        = mGlobals->mXml.getValue("SUBPATH", "");
411
                                string        suffix        = mGlobals->mXml.getValue("SUFFIX", "");
412
                                
413
                                ofxGuiObject* files = addFiles(id, name, width, height, mGlobals->mXmlfile, subPath, suffix);
414
                                files->buildFromXml();
415
                        }
416
                        else if(type == "COLOR")
417
                        {
418
                                ofRGBA value = ofRGBA(mGlobals->mXml.getValue("VALUE", "FFFFFFFF"));
419
                                
420
                                ofxGuiObject* color = addColor(id, name, width, height, value, mode);
421
                                color->buildFromXml();
422
                        }
423
                        else if(type == "MATRIX")
424
                        {
425
                                int        xGrid        = mGlobals->mXml.getValue("XGRID", 0);
426
                                int        yGrid        = mGlobals->mXml.getValue("YGRID", 0);
427
                                int        value        = mGlobals->mXml.getValue("VALUE", 0);
428
                                int        spacing        = mGlobals->mXml.getValue("SPACING", 0);
429
                                
430
                                ofxGuiObject* matrix = addMatrix(id, name, width, height, xGrid, yGrid, value, mode, spacing);
431
432
                                matrix->buildFromXml();
433
                        }
434
                        else if(type == "SCOPE")
435
                        {
436
                                int                        length        = mGlobals->mXml.getValue("LENGTH", 0);
437
                                float                valuex        = mGlobals->mXml.getValue("VALUE_X", 0.0f);
438
                                float                valuey        = mGlobals->mXml.getValue("VALUE_Y", 0.0f);
439
                                ofxPoint2f        value        = ofxPoint2f(valuex, valuey);
440
                                
441
                                ofxGuiObject* scope = addScope(id, name, width, height, length, value, mode);
442
                                scope->buildFromXml();
443
                        }
444
                        else if(type == "KNOB")
445
                        {
446
                                float        min                = mGlobals->mXml.getValue("MIN", 0.0f);
447
                                float        max                = mGlobals->mXml.getValue("MAX", 0.0f);
448
                                float        value        = mGlobals->mXml.getValue("VALUE", 0.0f);
449
                                
450
                                ofxGuiObject* knob = addKnob(id, name, width, height, min, max, value, display, steps);
451
                                knob->buildFromXml();
452
                        }
453
                        else if(type == "SWITCH")
454
                        {
455
                                float        min                = mGlobals->mXml.getValue("MIN", 0.0f);
456
                                float        max                = mGlobals->mXml.getValue("MAX", 0.0f);
457
                                float        value        = mGlobals->mXml.getValue("VALUE", 0.0f);
458
                                
459
                                //        const string* strings = 
460
                                
461
                                ofxGuiObject* swtch = addSwitch(id, name, width, height, min, max, value, NULL);
462
                                swtch->buildFromXml();
463
                        }
464
                        
465
                        mGlobals->mXml.popTag();
466
                }
467
        }
468
}
469
470
//        ----------------------------------------------------------------------------------------------------
471
472
void ofxGuiPanel::saveToXml()
473
{
474
        ofxGuiObject* tmpObj;
475
        
476
        int id = saveObjectData();
477
        
478
        mGlobals->mXml.setValue("OBJECT:BORDER", mBorder, id);
479
        mGlobals->mXml.setValue("OBJECT:SPACING", mSpacing, id);
480
        mGlobals->mXml.pushTag("OBJECT", id);
481
        
482
        for(int i = 0; i < mObjects.size(); i++)
483
        {
484
                tmpObj = (ofxGuiObject*)mObjects.at(i);
485
                tmpObj->saveToXml();
486
        }
487
        
488
        mGlobals->mXml.popTag();
489
}
490
491
//        ----------------------------------------------------------------------------------------------------