From 52f12dfc10edfc4dd063b610f65dd2e901d3080c Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Wed, 30 Aug 2017 01:31:57 +0100 Subject: load save --- gui/src/chainImage.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++------ gui/src/chainImage.h | 23 ++++++++++++--- gui/src/ofApp.cpp | 1 + 3 files changed, 89 insertions(+), 13 deletions(-) (limited to 'gui') diff --git a/gui/src/chainImage.cpp b/gui/src/chainImage.cpp index 44aaace..67fbad6 100644 --- a/gui/src/chainImage.cpp +++ b/gui/src/chainImage.cpp @@ -86,6 +86,27 @@ void chainImage::drawChain(float fadeIn){ glPopMatrix(); } +Json::Value chainImage::toJson(){ + Json::Value json=Json::Value(Json::objectValue); + json["linkPos"]=Json::Value(Json::arrayValue); + json["linkPos"].append(linkPos.x); + json["linkPos"].append(linkPos.y); + json["linkScale"]=linkScale; + json["linkRot"]=linkRot; + json["filename"]=filename; + return json; +} + +bool chainImage::fromJson(Json::Value json){ + if (load(json["filename"].asString())){ + linkPos=ofPoint(json["linkPos"][0].asDouble(),json["linkPos"][1].asDouble()); + linkScale=json["linkScale"].asDouble(); + linkRot=json["linkRot"].asDouble(); + return true; + } + return false; +} + void chainImageSet::drawGui(){ float t_xoffs=0.0; float borderwidth=ofGetHeight()*(1.0-THUMB_BORDER_RATIO)*0.5; @@ -203,10 +224,7 @@ if not make a symbolic link */ chainImage image; if (image.load(filename)){ - image.makeThumbnail(); - - //could there be a way to load without committing the texture - image.setUseTexture(false); + image.init(ofPoint(0,0), currentDefaultImageRatio, 0 //default rotation @@ -295,10 +313,13 @@ void chainImageSet::keyPressed(ofKeyEventArgs &keyargs){ //Check if the user opened a file if (openFileResult.bSuccess){ - ofLogVerbose("Selected %s\n",openFileResult.filePath); - //We have a file, check it and process it - //processOpenFileSelection(openFileResult); + bool success=loadJson(openFileResult.fileName); + + ofLogVerbose("Load %s %s\n", + openFileResult.filePath.c_str(), + success?"succeeded":"failed"); + }else { ofLogVerbose("User hit cancel"); @@ -345,10 +366,49 @@ void chainImageSet::mouseReleased(int x, int y, int button){ } bool chainImageSet::saveJson(std::string filename){ + ofxJSON json; + + json["images"] = Json::Value(Json::arrayValue); + + for(std::list::iterator ii=images.begin(); ii != images.end(); ii++){ + json["images"].append(ii->toJson()); + } + + + json.save(filename, true); } -bool chainImageSet::loadJson(std::string filename){ - +bool chainImageSet::loadJson(std::string _filename){ + ofxJSON json; + bool parsingSuccessful = json.open(_filename); + + if (parsingSuccessful) + { + images.clear(); + + for (int i=0;i::iterator ii=images.begin(); ii != images.end(); ii++){ + auto li=ii; + li++; + if (li==images.end()) { + li=images.begin(); + } + ii->link=&(*li); + + } + selected=images.begin(); + filename=_filename; + return true; + + + } + ofLogVerbose("JSON load: parsing unsuccesful\n"); + return false; } diff --git a/gui/src/chainImage.h b/gui/src/chainImage.h index ac78bd3..6fa5bca 100644 --- a/gui/src/chainImage.h +++ b/gui/src/chainImage.h @@ -1,6 +1,7 @@ #pragma once #include "ofMain.h" +#include "ofxJSON.h" #define THUMB_BORDER_RATIO 0.8 @@ -17,7 +18,15 @@ class chainImage : public ofImage{ void start(); bool load(std::string _filename){ filename=_filename; - return ofImage::load(filename); + if (ofImage::load(filename)){ + makeThumbnail(); + + //could there be a way to load without committing the texture + setUseTexture(false); + return true; + } + + return false; } bool update(float decayRatio); @@ -29,17 +38,23 @@ class chainImage : public ofImage{ ofImage thumbnail; void makeThumbnail(); + Json::Value toJson(); + bool fromJson(Json::Value json); + chainImage *link; + ofPoint linkPos; float linkScale; float linkRot; + std::string filename; + float transition; float fadeIn; float speed; float time; float scale; - std::string filename; + }; @@ -56,8 +71,8 @@ class chainImageSet{ void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); - saveJson(std::string filename); - loadJson(std::string filename); + bool saveJson(std::string filename); + bool loadJson(std::string filename); std::list images; float currentDefaultImageRatio; diff --git a/gui/src/ofApp.cpp b/gui/src/ofApp.cpp index cb808d1..7e25b42 100644 --- a/gui/src/ofApp.cpp +++ b/gui/src/ofApp.cpp @@ -84,6 +84,7 @@ void ofApp::dragEvent(ofDragInfo dragInfo){ filenames=filenames+", "; } filenames=filenames+*f; + images.add(*f,dragInfo.position); } -- cgit v1.2.3