|
| 1 | +#include "ofApp.h" |
| 2 | + |
| 3 | +//-------------------------------------------------------------- |
| 4 | +void ofApp::setup(){ |
| 5 | + ofSetBackgroundColor( 230 ); |
| 6 | + |
| 7 | + svgAddTypes = { |
| 8 | + OFXSVG_TYPE_RECTANGLE, |
| 9 | + OFXSVG_TYPE_CIRCLE, |
| 10 | + OFXSVG_TYPE_PATH |
| 11 | + }; |
| 12 | + |
| 13 | + svg.load("ofxSvg.svg"); |
| 14 | + |
| 15 | +} |
| 16 | + |
| 17 | +//-------------------------------------------------------------- |
| 18 | +void ofApp::update(){ |
| 19 | + svg.setBoundsWidth(ofGetWidth()); |
| 20 | + svg.setBoundsHeight(ofGetHeight()); |
| 21 | +} |
| 22 | + |
| 23 | +//-------------------------------------------------------------- |
| 24 | +void ofApp::draw(){ |
| 25 | + svg.draw(); |
| 26 | + ofSetColor( ofColor::cyan ); |
| 27 | + polyline.draw(); |
| 28 | + |
| 29 | + std::stringstream ss; |
| 30 | + ss << "Add Type (left/right): " << (svgTypeIndex+1) << " / " << svgAddTypes.size() << " - " << ofxSvgElement::sGetTypeAsString(svgAddTypes[svgTypeIndex]); |
| 31 | + if( svgAddTypes[svgTypeIndex] == OFXSVG_TYPE_PATH ) { |
| 32 | + ss << std::endl << "Click and drag mouse to add points."; |
| 33 | + } |
| 34 | + ss << std::endl << "Size (up/down): " << size; |
| 35 | + ss << std::endl << "Save (s)"; |
| 36 | + ss << std::endl << "Clear (delete)"; |
| 37 | + ofDrawBitmapStringHighlight(ss.str(), 40, 40); |
| 38 | +} |
| 39 | + |
| 40 | +//-------------------------------------------------------------- |
| 41 | +void ofApp::exit(){ |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | +//-------------------------------------------------------------- |
| 46 | +void ofApp::keyPressed(int key){ |
| 47 | + if( key == OF_KEY_DEL || key == OF_KEY_BACKSPACE ) { |
| 48 | + svg.clear(); |
| 49 | + } |
| 50 | + if( key == 's' ) { |
| 51 | + std::string filename = ofGetTimestampString()+".svg"; |
| 52 | + ofLogNotice("ofApp") << "saving svg to file: " << filename; |
| 53 | + svg.save(filename); |
| 54 | + } |
| 55 | + if( key == OF_KEY_RIGHT ) { |
| 56 | + svgTypeIndex++; |
| 57 | + svgTypeIndex %= svgAddTypes.size(); |
| 58 | + } |
| 59 | + if( key == OF_KEY_LEFT ) { |
| 60 | + svgTypeIndex--; |
| 61 | + if( svgTypeIndex < 0 ) { |
| 62 | + svgTypeIndex = svgAddTypes.size()-1; |
| 63 | + } |
| 64 | + } |
| 65 | + if( key == OF_KEY_UP ) { |
| 66 | + size += 2; |
| 67 | + } |
| 68 | + if( key == OF_KEY_DOWN ) { |
| 69 | + size -= 2; |
| 70 | + } |
| 71 | + |
| 72 | + size = ofClamp( size, 2, 2000 ); |
| 73 | +} |
| 74 | + |
| 75 | +//-------------------------------------------------------------- |
| 76 | +void ofApp::keyReleased(int key){ |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +//-------------------------------------------------------------- |
| 81 | +void ofApp::mouseMoved(int x, int y ){ |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +//-------------------------------------------------------------- |
| 86 | +void ofApp::mouseDragged(int x, int y, int button){ |
| 87 | + if(polyline.size() > 0 ) { |
| 88 | + polyline.addVertex(glm::vec3(x,y,0.f)); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +//-------------------------------------------------------------- |
| 93 | +void ofApp::mousePressed(int x, int y, int button){ |
| 94 | + polyline.clear(); |
| 95 | + svg.setFilled(true); |
| 96 | + svg.setHasStroke(false); |
| 97 | +// svg.setStrokeWidth(0); |
| 98 | + if( svgAddTypes[svgTypeIndex] == OFXSVG_TYPE_RECTANGLE ) { |
| 99 | + ofRectangle rect; |
| 100 | + rect.setFromCenter(x, y, size, size); |
| 101 | + svg.setFillColor(ofColor(255, x%255, (y*10) % 255)); |
| 102 | + auto svgRect = svg.add( rect ); |
| 103 | + svgRect->setName("r-"+ofToString(ofGetFrameNum())); |
| 104 | + } else if( svgAddTypes[svgTypeIndex] == OFXSVG_TYPE_CIRCLE ) { |
| 105 | + svg.setFillColor(ofColor(x%255, 205, (y*10) % 255)); |
| 106 | + auto svgCircle = svg.addCircle( glm::vec2(x,y), size ); |
| 107 | + svgCircle->setName("c-"+ofToString(ofGetFrameNum())); |
| 108 | + } else if( svgAddTypes[svgTypeIndex] == OFXSVG_TYPE_PATH ) { |
| 109 | + svg.setFilled(false); |
| 110 | + svg.setStrokeColor(ofColor::magenta); |
| 111 | + svg.setStrokeWidth(3); |
| 112 | + polyline.addVertex(glm::vec3(x,y,0.f)); |
| 113 | + } |
| 114 | + |
| 115 | + std::cout << "------------------------------------" << std::endl; |
| 116 | + std::cout << svg.toString() << std::endl; |
| 117 | + std::cout << "------------------------------------" << std::endl; |
| 118 | +} |
| 119 | + |
| 120 | +//-------------------------------------------------------------- |
| 121 | +void ofApp::mouseReleased(int x, int y, int button){ |
| 122 | + if( svgAddTypes[svgTypeIndex] == OFXSVG_TYPE_PATH ) { |
| 123 | + if( polyline.size() > 2 ) { |
| 124 | + auto svgPath = svg.add(polyline); |
| 125 | + svgPath->setName("p-"+ofToString(ofGetFrameNum())); |
| 126 | + polyline.clear(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +//-------------------------------------------------------------- |
| 132 | +void ofApp::mouseScrolled(int x, int y, float scrollX, float scrollY){ |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +//-------------------------------------------------------------- |
| 137 | +void ofApp::mouseEntered(int x, int y){ |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +//-------------------------------------------------------------- |
| 142 | +void ofApp::mouseExited(int x, int y){ |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +//-------------------------------------------------------------- |
| 147 | +void ofApp::windowResized(int w, int h){ |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +//-------------------------------------------------------------- |
| 152 | +void ofApp::gotMessage(ofMessage msg){ |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +//-------------------------------------------------------------- |
| 157 | +void ofApp::dragEvent(ofDragInfo dragInfo){ |
| 158 | + |
| 159 | +} |
0 commit comments