root / trunk / tbeta / OSX / addons / ofxXmlSettings / example / src / testApp.cpp @ 151

View | Annotate | Download (6.9 KB)

1 151 ss
#include "testApp.h"
2 151 ss
3 151 ss
4 151 ss
//--------------------------------------------------------------
5 151 ss
void testApp::setup(){
6 151 ss
        ofBackground(255,255,255);
7 151 ss
8 151 ss
        //-----------
9 151 ss
        //the string is printed at the top of the app
10 151 ss
        //to give the user some feedback
11 151 ss
        message = "loading mySettings.xml";
12 151 ss
13 151 ss
        //we load our settings file
14 151 ss
        //if it doesn't exist we can still make one
15 151 ss
        //by hitting the 's' key
16 151 ss
        if( XML.loadFile("mySettings.xml") ){
17 151 ss
                message = "mySettings.xml loaded!";
18 151 ss
        }else{
19 151 ss
                message = "unable to load mySettings.xml check data/ folder";
20 151 ss
        }
21 151 ss
22 151 ss
        //read the colors from XML
23 151 ss
        //if the settings file doesn't exist we assigns default values (170, 190, 240)
24 151 ss
        red                = XML.getValue("BACKGROUND:COLOR:RED", 170);
25 151 ss
        green        = XML.getValue("BACKGROUND:COLOR:GREEN", 190);
26 151 ss
        blue        = XML.getValue("BACKGROUND:COLOR:BLUE", 240);
27 151 ss
28 151 ss
        /*
29 151 ss
                "BACKGROUND:COLOR:RED" referes to a structure like this:
30 151 ss
31 151 ss
                <BACKGROUND>
32 151 ss
                        <COLOR>
33 151 ss
                                <RED>101.103516</RED>
34 151 ss
                        </COLOR>
35 151 ss
                </BACKGROUND>
36 151 ss
        */
37 151 ss
38 151 ss
        //we initalize some of our variables
39 151 ss
        lastTagNumber        = 0;
40 151 ss
        pointCount                = 0;
41 151 ss
        lineCount                = 0;
42 151 ss
43 151 ss
44 151 ss
        //-------
45 151 ss
        //this is a more advanced use of ofXMLSettings
46 151 ss
        //we are going to be reading multiple tags with the same name
47 151 ss
48 151 ss
        //lets see how many <STROKE> </STROKE> tags there are in the xml file
49 151 ss
        int numDragTags = XML.getNumTags("STROKE:PT");
50 151 ss
51 151 ss
        //if there is at least one <STROKE> tag we can read the list of points
52 151 ss
        //and then try and draw it as a line on the screen
53 151 ss
        if(numDragTags > 0){
54 151 ss
55 151 ss
                //we push into the last STROKE tag
56 151 ss
                //this temporarirly treats the tag as
57 151 ss
                //the document root.
58 151 ss
                XML.pushTag("STROKE", numDragTags-1);
59 151 ss
60 151 ss
                        //we see how many points we have stored in <PT> tags
61 151 ss
                        int numPtTags = XML.getNumTags("PT");
62 151 ss
63 151 ss
                        if(numPtTags > 0){
64 151 ss
65 151 ss
                                //We then read those x y values into our
66 151 ss
                                //array - so that we can then draw the points as
67 151 ss
                                //a line on the screen
68 151 ss
69 151 ss
                                //we have only allocated a certan amount of space for our array
70 151 ss
                                //so we don't want to read more than that amount of points
71 151 ss
                                int totalToRead = MIN(numPtTags, NUM_PTS);
72 151 ss
73 151 ss
                                for(int i = 0; i < totalToRead; i++){
74 151 ss
                                        //the last argument of getValue can be used to specify
75 151 ss
                                        //which tag out of multiple tags you are refering to.
76 151 ss
                                        int x = XML.getValue("PT:X", 0, i);
77 151 ss
                                        int y = XML.getValue("PT:Y", 0, i);
78 151 ss
                                        dragPts[i].set(x, y);
79 151 ss
                                        pointCount++;
80 151 ss
                                }
81 151 ss
                        }
82 151 ss
83 151 ss
                //this pops us out of the STROKE tag
84 151 ss
                //sets the root back to the xml document
85 151 ss
                XML.popTag();
86 151 ss
        }
87 151 ss
88 151 ss
        //load a monospaced font
89 151 ss
        //which we will use to show part of the xml structure
90 151 ss
        TTF.loadFont("mono.ttf", 7);
91 151 ss
}
92 151 ss
93 151 ss
//--------------------------------------------------------------
94 151 ss
void testApp::update(){
95 151 ss
        //we change the background color here based on the values
96 151 ss
        //affected by the mouse position
97 151 ss
        ofBackground((int)red,(int)green,(int)blue);
98 151 ss
99 151 ss
}
100 151 ss
101 151 ss
//--------------------------------------------------------------
102 151 ss
void testApp::draw(){
103 151 ss
104 151 ss
        //---------
105 151 ss
        //Lets draw the stroke as a continous line
106 151 ss
        ofSetColor(0x222222);
107 151 ss
        ofNoFill();
108 151 ss
        ofBeginShape();
109 151 ss
        for(int i = 0; i < pointCount; i++){
110 151 ss
                ofVertex(dragPts[i].x, dragPts[i].y);
111 151 ss
        }
112 151 ss
        ofEndShape(false);
113 151 ss
114 151 ss
    ofFill();
115 151 ss
116 151 ss
        //--------
117 151 ss
        //we make a black area on the left
118 151 ss
        //which we can print the xml text on
119 151 ss
        ofEnableAlphaBlending();
120 151 ss
        ofSetColor(0, 0, 0, 200);
121 151 ss
        ofRect(0, 0, 160, ofGetHeight());
122 151 ss
        ofDisableAlphaBlending();
123 151 ss
124 151 ss
        //our text that shows how the <STROKE> data looks in the xml file
125 151 ss
        ofSetColor(240, 240, 240);
126 151 ss
127 151 ss
        string drawString = "How the data is stored:\n\n";
128 151 ss
        if(xmlStructure.size() > 0) drawString += xmlStructure+"</STROKE>";
129 151 ss
        TTF.drawString(drawString, 5, 40);
130 151 ss
131 151 ss
        //the message bars at the top and bottom of the app
132 151 ss
        //ofSetColor(0xDDDDDD);
133 151 ss
        ofEnableAlphaBlending();
134 151 ss
        ofSetColor(0, 0, 0, 200);
135 151 ss
136 151 ss
        ofRect(160, 0, ofGetWidth()-160, 20);
137 151 ss
        ofRect(160, ofGetHeight()-20, ofGetWidth()-160, 20);
138 151 ss
139 151 ss
        //we draw our status message at the top
140 151 ss
        //ofSetColor(210, 90, 100);
141 151 ss
        ofSetColor(240, 240, 240);
142 151 ss
        TTF.drawString("Save settings to XML hit 's' key     status: "+message, 170, 12);
143 151 ss
144 151 ss
        //instructions at the bottom
145 151 ss
        TTF.drawString("mouse drag changes background color and records stroke", 168, ofGetHeight() - 9);
146 151 ss
}
147 151 ss
148 151 ss
//--------------------------------------------------------------
149 151 ss
void testApp::keyPressed  (int key){
150 151 ss
151 151 ss
                //no data gets saved unless you hit the s key
152 151 ss
                if(key == 's'){
153 151 ss
                        XML.saveFile("mySettings.xml");
154 151 ss
                        message ="settings saved to xml!";
155 151 ss
                }
156 151 ss
}
157 151 ss
158 151 ss
//--------------------------------------------------------------
159 151 ss
void testApp::keyReleased(int key){
160 151 ss
161 151 ss
}
162 151 ss
163 151 ss
//--------------------------------------------------------------
164 151 ss
void testApp::mouseMoved(int x, int y ){
165 151 ss
166 151 ss
}
167 151 ss
168 151 ss
//--------------------------------------------------------------
169 151 ss
void testApp::mouseDragged(int x, int y, int button){
170 151 ss
171 151 ss
        //-------
172 151 ss
        //we change the background color based on
173 151 ss
        //the two mouse coords coming in
174 151 ss
        float xpct = (float)x / ofGetWidth();
175 151 ss
        float ypct = (float)y / ofGetHeight();
176 151 ss
        red                        = xpct * 255.0;
177 151 ss
        green                = ypct * 255.0;
178 151 ss
        blue                = (int)(red - green) % 255;
179 151 ss
180 151 ss
181 151 ss
        //-------------
182 151 ss
        //we also want to record the stroke
183 151 ss
184 151 ss
        //lets store the drag of the user.
185 151 ss
        //we will push into the most recent
186 151 ss
        //<STROKE> tag - add the mouse points
187 151 ss
        //then pop out
188 151 ss
        if( XML.pushTag("STROKE", lastTagNumber) ){
189 151 ss
190 151 ss
                //now we will add a pt tag - with two
191 151 ss
                //children - X and Y
192 151 ss
193 151 ss
                int tagNum = XML.addTag("PT");
194 151 ss
                XML.setValue("PT:X", x, tagNum);
195 151 ss
                XML.setValue("PT:Y", y, tagNum);
196 151 ss
197 151 ss
                XML.popTag();
198 151 ss
        }
199 151 ss
200 151 ss
        //-------------
201 151 ss
        //here we make a string of text that looks like how the xml data
202 151 ss
        //is stored in the settings file - this is just so people can
203 151 ss
        //visually see how the data is stored.
204 151 ss
205 151 ss
        //if the text is about to go off screen
206 151 ss
        if(lineCount > 64){
207 151 ss
                //we find the first <PT> tag with the
208 151 ss
                //x and y data and we remove it from the begining
209 151 ss
                //this way the displayed text always shows the newest data
210 151 ss
                //without going offscreen.
211 151 ss
                int pos = xmlStructure.find("</PT>");
212 151 ss
                xmlStructure = xmlStructure.substr(pos+6);
213 151 ss
        }
214 151 ss
215 151 ss
        //here we add the most recent point to our fake xml string
216 151 ss
        xmlStructure += "    <PT>\n        <X>"+ofToString(x)+"</X>\n        <Y>"+ofToString(y)+"</Y>\n    </PT>\n";
217 151 ss
        lineCount+=4; //we have added 4 lines so increment by 4
218 151 ss
219 151 ss
        //------------
220 151 ss
        //we also record the x y points into an array - so we can draw it
221 151 ss
        if(pointCount < NUM_PTS -1){
222 151 ss
                dragPts[pointCount].set(x, y);
223 151 ss
                pointCount++;
224 151 ss
        }
225 151 ss
226 151 ss
}
227 151 ss
228 151 ss
//--------------------------------------------------------------
229 151 ss
void testApp::mousePressed(int x, int y, int button){
230 151 ss
231 151 ss
        //we can also add tags with the same name.
232 151 ss
        //here we are just adding an empty tag
233 151 ss
        //and when the user drags their mouse
234 151 ss
        //we will store the pts in this tag
235 151 ss
        lastTagNumber        = XML.addTag("STROKE");
236 151 ss
        xmlStructure        = "<STROKE>\n";
237 151 ss
238 151 ss
        //We start a new stroke
239 151 ss
        lineCount                = 0;
240 151 ss
        pointCount                = 0;
241 151 ss
242 151 ss
}
243 151 ss
244 151 ss
//--------------------------------------------------------------
245 151 ss
void testApp::mouseReleased(int x, int y, int button){
246 151 ss
247 151 ss
        //update the colors to the XML structure when the mouse is released
248 151 ss
        XML.setValue("BACKGROUND:COLOR:RED", red);
249 151 ss
        XML.setValue("BACKGROUND:COLOR:GREEN", green);
250 151 ss
        XML.setValue("BACKGROUND:COLOR:BLUE", blue);
251 151 ss
252 151 ss
}
253 151 ss
254 151 ss
255 151 ss
//--------------------------------------------------------------
256 151 ss
void testApp::windowResized(int w, int h){
257 151 ss
}
258 151 ss
259 151 ss