root / ofxArgosUI / src / Controls / ofxArgosUI_Toggle.h @ 66

View | Annotate | Download (3.7 KB)

1
/***********************************************************************
2
 
3
 Copyright (c) 2009, 2010 Dimitri Diakopoulos, http://www.dimitridiakopoulos.com/
4
5
 Portions Copyright (c) 2008, 2009 Memo Aktens, http://www.memo.tv/
6
 -> Based on ofxSimpleGuiToo
7
 
8
 Portions Copyright (c) 2008 Todd Vanderlin, http://toddvanderlin.com/
9
 -> Inspired by ofxSimpleGui API
10
11
        Redistribution and use in source and binary forms, with or without modification, 
12
        are permitted provided that the following conditions are met:
13
14
        1. Redistributions of source code must retain the above copyright notice, 
15
        this list of conditions and the following disclaimer.
16
17
        2. Redistributions in binary form must reproduce the above copyright notice, 
18
        this list of conditions and the following disclaimer in the documentation and/or 
19
        other materials provided with the distribution.
20
21
        3. The name of the author may not be used to endorse or promote products derived 
22
        from this software without specific prior written permission.
23
24
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
25
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
26
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
27
        ARE DISCLAIMED. IN NOEVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
28
        INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
29
        (INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
30
        LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
31
        AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
33
        OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
34
35
*************************************************************************/ 
36
#pragma once
37
38
#include "ofxArgosUI_Control.h"
39
40
class ofxArgosUI_Toggle : public ofxArgosUI_Control {
41
        
42
public:
43
44
        bool        *value;
45
        
46
        ofxArgosUI_Toggle(string name, int x, int y, int width, int height, bool *value) : ofxArgosUI_Control(name) {
47
                this->value        = value;
48
                controlType = "Toggle";
49
                setup(x, y, width, height);
50
51
                OSCaddress = "/toggle"; 
52
        }
53
        
54
        void setup(int _x, int _y, int _width, int _height) {
55
                setPos(_x, _y); 
56
                setSize(_width, _height);
57
        }
58
        
59
        void loadFromXML(ofxXmlSettings &XML) {
60
                set(XML.getValue("controls:" + controlType + "_" + key + ":value", 0));
61
        }
62
        
63
        void saveToXML(ofxXmlSettings &XML) {
64
                XML.addTag(controlType + "_" + key);
65
                        XML.pushTag(controlType + "_" + key);
66
                                XML.addValue("name", name);
67
                                XML.addValue("value", getValue());
68
                        XML.popTag();
69
        }
70
        
71
        bool getValue() {
72
                return (*value);
73
        }
74
        void set(bool b) {
75
                (*value) = b;
76
        }
77
        void toggle() {
78
                (*value) = !(*value); 
79
                oschandler.sendOSC(*value, OSCaddress);
80
        }
81
82
        // ============================================= Mouse
83
84
        void focusActive() { if (canfocus) focus.set(this); }
85
86
        void onPress(int x, int y, int button) {
87
                toggle();
88
        }
89
90
        // ============================================= Touch
91
        void onTouchDown(float x, float y, int ID){
92
                toggle();
93
        }
94
95
        void update() {
96
                if(!enabled) return;
97
                enabled = false;
98
        }
99
        
100
        void draw(float x, float y) {
101
102
                enabled = true;
103
104
                setPos(x, y);
105
                
106
                ofEnableAlphaBlending();
107
108
                glPushMatrix();
109
110
                        glTranslatef(x, y, 0);
111
                        
112
                        // Left-side Box
113
                        ofNoFill();
114
                        setTextBGColor();  
115
                        ofRect(0, 0, height, height);
116
117
                        // Draw X
118
                        if((*value)) {
119
                                ofEnableSmoothing();
120
                                        ofFill();
121
                                        ofLine(0, 0, height, height);
122
                                        ofLine(height, 0, 0, height);
123
                                        ofNoFill();
124
                                        ofLine(0, 0, height, height);
125
                                        ofLine(height, 0, 0, height);
126
                                ofDisableSmoothing();
127
                        }
128
129
                        ofFill();
130
131
                        ofSetColor(0xdfdfdf); 
132
                        argosText::font.drawString(name, height + 3, 14);
133
134
                glPopMatrix();
135
136
                ofDisableAlphaBlending();
137
        }
138
};