#include "ofApp.h"
void ofApp::setup(){
if(!xml.load("points.xml")){
ofLogError() << "Couldn't load file";
}
auto drawing = xml.getChild("drawing");
if(!drawing){
drawing = xml.appendChild("drawing");
}
bg = drawing.findFirst("background[red and green and blue]");
if(!bg){
drawing.removeChild("background");
bg = drawing.appendChild("background");
bg.appendChild("red").set(255);
bg.appendChild("green").set(255);
bg.appendChild("blue").set(255);
}
auto background = xml.findFirst("//background");
if(background){
bgColor.r = background.getChild("red").getIntValue();
bgColor.g = background.getChild("green").getIntValue();
bgColor.b = background.getChild("blue").getIntValue();
}
auto strokesXml = xml.find("//drawing/stroke");
for(auto & stroke: strokesXml){
strokes.emplace_back();
strokes.back().setMode(OF_PRIMITIVE_LINE_STRIP);
auto pts = stroke.getChildren("pt");
for(auto & pt: pts){
auto x = pt.getAttribute("x").getIntValue();
auto y = pt.getAttribute("y").getIntValue();
strokes.back().addVertex({x,y,0});
}
}
ttf.load("mono.ttf", 7);
ofDisableAntiAliasing();
}
void ofApp::update(){
if(xmlChanged){
ofBuffer auxBuffer;
auxBuffer.set(xml.toString(" "));
xmlText.clear();
auto i = 0;
for(auto & line: auxBuffer.getReverseLines()){
xmlText = line + "\n" + xmlText;
++i;
if(i>58) break;
}
xmlChanged = false;
}
}
void ofApp::draw(){
ofClear(bgColor);
ofSetColor(0);
for(auto & stroke: strokes){
stroke.draw();
}
ofEnableAlphaBlending();
ofSetColor(0, 0, 0, 200);
ofDrawRectangle(0, 0, 160, ofGetHeight());
ofDisableAlphaBlending();
ofSetColor(240, 240, 240);
string drawString = "How the data is stored:\n\n" + xmlText;
ttf.drawString(drawString, 5, 10);
ofEnableAlphaBlending();
ofSetColor(0, 0, 0, 200);
ofDrawRectangle(160, 0, ofGetWidth()-160, 20);
ofDrawRectangle(160, ofGetHeight()-20, ofGetWidth()-160, 20);
ofSetColor(240, 240, 240);
ttf.drawString("Save settings to XML hit 's' key", 170, 12);
ttf.drawString("mouse drag changes background color and records stroke", 168, ofGetHeight() - 9);
}
void ofApp::keyPressed(int key){
if(key == 's'){
if(!xml.save("points.xml")){
ofLogError() << "Couldn't save points.xml";
}
}
}
void ofApp::keyReleased(int key){
}
void ofApp::mouseMoved(int x, int y ){
}
void ofApp::mouseDragged(int x, int y, int button){
float xpct = (float)x / ofGetWidth();
float ypct = (float)y / ofGetHeight();
bgColor.r = xpct * 255.0f;
bgColor.g = ypct * 255.0f;
bgColor.b = (int)(bgColor.r - bgColor.g) % 255;
bg.getChild("red").set(bgColor.r);
bg.getChild("green").set(bgColor.g);
bg.getChild("blue").set(bgColor.b);
auto pt = currentStroke.appendChild("pt");
pt.setAttribute("x", x);
pt.setAttribute("y", y);
strokes.back().addVertex({x,y,0});
xmlChanged = true;
}
void ofApp::mousePressed(int x, int y, int button){
currentStroke = xml.getChild("drawing").insertChildBefore("stroke", bg);
strokes.emplace_back();
strokes.back().setMode(OF_PRIMITIVE_LINE_STRIP);
xmlChanged = true;
}
void ofApp::mouseReleased(int x, int y, int button){
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments