Skip to content

Commit 82e2d6d

Browse files
committed
save example
1 parent 0122e95 commit 82e2d6d

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed
Lines changed: 20 additions & 0 deletions
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "ofMain.h"
2+
#include "ofApp.h"
3+
4+
//========================================================================
5+
int main( ){
6+
7+
//Use ofGLFWWindowSettings for more options like multi-monitor fullscreen
8+
ofGLWindowSettings settings;
9+
settings.setSize(1024, 768);
10+
settings.setGLVersion(3,2);
11+
settings.windowMode = OF_WINDOW; //can also be OF_FULLSCREEN
12+
13+
auto window = ofCreateWindow(settings);
14+
15+
ofRunApp(window, std::make_shared<ofApp>());
16+
ofRunMainLoop();
17+
18+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include "ofMain.h"
4+
#include "ofxSvg.h"
5+
6+
class ofApp : public ofBaseApp{
7+
8+
public:
9+
void setup() override;
10+
void update() override;
11+
void draw() override;
12+
void exit() override;
13+
14+
void keyPressed(int key) override;
15+
void keyReleased(int key) override;
16+
void mouseMoved(int x, int y ) override;
17+
void mouseDragged(int x, int y, int button) override;
18+
void mousePressed(int x, int y, int button) override;
19+
void mouseReleased(int x, int y, int button) override;
20+
void mouseScrolled(int x, int y, float scrollX, float scrollY) override;
21+
void mouseEntered(int x, int y) override;
22+
void mouseExited(int x, int y) override;
23+
void windowResized(int w, int h) override;
24+
void dragEvent(ofDragInfo dragInfo) override;
25+
void gotMessage(ofMessage msg) override;
26+
27+
ofxSvg svg;
28+
29+
std::vector<ofxSvgType> svgAddTypes;
30+
int svgTypeIndex = 0;
31+
int size = 20;
32+
ofPolyline polyline;
33+
34+
};
22.7 KB
Loading

0 commit comments

Comments
 (0)