mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-05-06 19:39:56 -05:00
Improved the interface. Added recursive traversal of the property. The interface is now created in main.cpp and owns a pointer to the openspacenengine so that the callback chain is GUI -> Interface -> Engine
This commit is contained in:
+51
-52
@@ -30,75 +30,74 @@
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
Interface::Interface() {}
|
||||
Interface::Interface(OpenSpaceEngine* engine) : _engine(engine) {}
|
||||
Interface::~Interface() {}
|
||||
|
||||
void Interface::callback(const char* receivedChars) {
|
||||
std::cout << receivedChars;
|
||||
|
||||
boost::property_tree::ptree pt;
|
||||
std::stringstream input(receivedChars);
|
||||
boost::property_tree::json_parser::read_json(input, pt);
|
||||
_nodes = std::vector<Node>();
|
||||
|
||||
// handleNodes(pt);
|
||||
|
||||
auto it = pt.begin();
|
||||
while (it != pt.end()) {
|
||||
|
||||
std::string key = (*it).first;
|
||||
auto data = (*it).second;
|
||||
|
||||
if (data.size() == 0) { // JSON Single value
|
||||
if (strcmp(key.c_str(), "stats") == 0)
|
||||
sgct::Engine::instance()->setDisplayInfoVisibility(atoi(pt.get<std::string>(key).c_str()));
|
||||
else if (strcmp(key.c_str(), "graph") == 0)
|
||||
sgct::Engine::instance()->setStatsGraphVisibility(atoi(pt.get<std::string>(key).c_str()));
|
||||
else if (strcmp(key.c_str(), "renderer") == 0){
|
||||
if (strcmp(pt.get<std::string>(key).c_str(), "volumeraycaster") == 0) {
|
||||
// _useVolumeRaycaster = true;
|
||||
// _useFlare = false;
|
||||
} else if (strcmp(pt.get<std::string>(key).c_str(), "flare") == 0) {
|
||||
// _useVolumeRaycaster = false;
|
||||
// _useFlare = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else { // JSON Array
|
||||
std::cout << key << " = { " << std::flush;
|
||||
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child(key)) {
|
||||
assert(v.first.empty()); // array elements have no names
|
||||
std::cout << v.second.data() << " " << std::flush;
|
||||
}
|
||||
std::cout << "}" << std::endl;
|
||||
}
|
||||
|
||||
it++;
|
||||
}
|
||||
loadIntoNodes(pt);
|
||||
handleNodes();
|
||||
|
||||
// for (int i = 0; i < _nodes.size(); ++i) {
|
||||
// std::cout << _nodes.at(i)._key << " " << _nodes.at(i)._value;
|
||||
// for (int j = 0; j < _nodes.at(i)._children.size(); ++j) {
|
||||
// std::cout << _nodes.at(i)._children.at(j)._value << " ";
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
// }
|
||||
_nodes.clear();
|
||||
}
|
||||
|
||||
// Workd in progress on recursive node handler
|
||||
void Interface::handleNodes(boost::property_tree::ptree pt, std::string key) {
|
||||
auto it = pt.begin();
|
||||
while (it != pt.end()) {
|
||||
std::string name = (*it).first;
|
||||
auto data = (*it).second;
|
||||
if (!name.empty())
|
||||
key = name;
|
||||
|
||||
std::cout << key << " " << data.size() << " " << std::flush;
|
||||
|
||||
if (data.size() == 0) { // leaf node
|
||||
std::cout << key << " : "<< data.data() << std::endl;
|
||||
} else {
|
||||
handleNodes(pt.get_child(key), key);
|
||||
void Interface::handleNodes() {
|
||||
for (int i = 0; i < _nodes.size(); ++i) {
|
||||
Node node = _nodes.at(i);
|
||||
if (node == "stats") {
|
||||
sgct::Engine::instance()->setDisplayInfoVisibility(atoi(node._value.c_str()));
|
||||
} else if (node == "graph") {
|
||||
sgct::Engine::instance()->setStatsGraphVisibility(atoi(node._value.c_str()));
|
||||
} else if (node == "renderer") {
|
||||
if (strcmp(node._value.c_str(), "volumeraycaster") == 0)
|
||||
_engine->setRenderer(OpenSpaceEngine::Renderers::VolumeRaycaster);
|
||||
else if (strcmp(node._value.c_str(), "flare") == 0)
|
||||
_engine->setRenderer(OpenSpaceEngine::Renderers::Flare);
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
// http://duck-wrath.blogspot.com/2012/02/how-to-recursive-parse.html
|
||||
void Interface::loadIntoNodes(const boost::property_tree::ptree& tree, std::string parent, const int depth) {
|
||||
BOOST_FOREACH( boost::property_tree::ptree::value_type const&v, tree.get_child("") ) {
|
||||
boost::property_tree::ptree subtree = v.second;
|
||||
std::string value = v.second.data();
|
||||
std::string key = v.first;
|
||||
|
||||
// classify and store nodes
|
||||
if ( key.length() > 0 ) { // value
|
||||
_nodes.push_back(Node(key, value));
|
||||
} else { // array
|
||||
// Find parent and add to its children vector
|
||||
std::vector<Node>::iterator it = std::find(_nodes.begin(), _nodes.end(), Node(parent));
|
||||
if (it != _nodes.end()) {
|
||||
(*it)._children.push_back(Node(parent, value));
|
||||
} else {
|
||||
std::cout << "Parent not found" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// recursive go down the hierarchy
|
||||
loadIntoNodes(subtree,key,depth+1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
|
||||
@@ -26,19 +26,47 @@
|
||||
#define INTERFACE_H_
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <openspaceengine.h>
|
||||
#include <vector>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class Interface {
|
||||
|
||||
struct Node {
|
||||
std::string _key;
|
||||
std::string _value;
|
||||
std::vector<Node> _children;
|
||||
Node(std::string key, std::string value) {
|
||||
_key = key;
|
||||
_value = value;
|
||||
_children = std::vector<Node>();
|
||||
}
|
||||
Node(std::string key) {
|
||||
_key = key;
|
||||
_value = "";
|
||||
_children = std::vector<Node>();
|
||||
}
|
||||
|
||||
inline bool operator==(const Node& rhs){
|
||||
return (strcmp(_key.c_str(), rhs._key.c_str()) == 0);
|
||||
}
|
||||
inline bool operator==(const std::string& rhs){
|
||||
return (strcmp(_key.c_str(), rhs.c_str()) == 0);
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
Interface();
|
||||
Interface(OpenSpaceEngine* engine);
|
||||
~Interface();
|
||||
|
||||
void callback(const char * receivedChars);
|
||||
|
||||
private:
|
||||
void handleNodes(boost::property_tree::ptree pt, std::string key = "root");
|
||||
void handleNodes();
|
||||
void loadIntoNodes(const boost::property_tree::ptree& tree, std::string parent = "", const int depth = 0);
|
||||
|
||||
OpenSpaceEngine* _engine;
|
||||
std::vector<Node> _nodes;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
+5
-2
@@ -25,11 +25,13 @@
|
||||
|
||||
// open space includes
|
||||
#include "openspaceengine.h"
|
||||
#include <interface/interface.h>
|
||||
|
||||
// sgct includes
|
||||
#include "sgct.h"
|
||||
|
||||
sgct::Engine* _sgctEngine;
|
||||
openspace::Interface* _interface;
|
||||
|
||||
// function pointer declarations
|
||||
void mainInitFunc(void);
|
||||
@@ -69,7 +71,8 @@ int main(int argc, char **argv) {
|
||||
sgct::SharedData::instance()->setEncodeFunction(mainEncodeFun);
|
||||
sgct::SharedData::instance()->setDecodeFunction(mainDecodeFun);
|
||||
|
||||
|
||||
// init the interface which will handle callbacks from an external gui
|
||||
_interface = new openspace::Interface(&OsEng);
|
||||
|
||||
// try to open a window
|
||||
if( ! _sgctEngine->init(sgct::Engine::OpenGL_4_0_Core_Profile)) {
|
||||
@@ -91,7 +94,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
void mainExternalControlCallback(const char * receivedChars, int size, int clientId) {
|
||||
if (_sgctEngine->isMaster())
|
||||
OsEng.externalControlCallback(receivedChars, size, clientId);
|
||||
_interface->callback(receivedChars);
|
||||
}
|
||||
|
||||
void mainInitFunc(void) {
|
||||
|
||||
+13
-5
@@ -166,6 +166,9 @@ bool OpenSpaceEngine::initialize() {
|
||||
|
||||
_engine->_interactionHandler->connectDevices();
|
||||
|
||||
// Init interface
|
||||
// _interface = new Interface(this);
|
||||
|
||||
// Choose rendering
|
||||
_volumeRaycaster = new VolumeRaycaster();
|
||||
_flare = new Flare();
|
||||
@@ -173,6 +176,16 @@ bool OpenSpaceEngine::initialize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void OpenSpaceEngine::setRenderer(OpenSpaceEngine::Renderers renderer) {
|
||||
if (renderer == Renderers::VolumeRaycaster) {
|
||||
_useVolumeRaycaster = true;
|
||||
_useFlare = false;
|
||||
} else if (renderer == Renderers::Flare) {
|
||||
_useVolumeRaycaster = false;
|
||||
_useFlare = true;
|
||||
}
|
||||
}
|
||||
|
||||
ghoul::ConfigurationManager& OpenSpaceEngine::configurationManager() {
|
||||
// TODO custom assert (ticket #5)
|
||||
assert(_configurationManager != nullptr);
|
||||
@@ -248,11 +261,6 @@ void OpenSpaceEngine::mouseScrollWheelCallback(int pos) {
|
||||
_interactionHandler->mouseScrollWheelCallback(pos);
|
||||
}
|
||||
|
||||
void OpenSpaceEngine::externalControlCallback(const char * receivedChars, int size, int clientId) {
|
||||
std::cout << receivedChars << std::endl;
|
||||
_interface->callback(receivedChars);
|
||||
}
|
||||
|
||||
void OpenSpaceEngine::encode() {
|
||||
if (_useFlare) _flare->encode();
|
||||
if (_useVolumeRaycaster) _volumeRaycaster->encode();
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
#include <rendering/volumeraycaster.h>
|
||||
#include <flare/flare.h>
|
||||
#include <interface/interface.h>
|
||||
//#include <interface/interface.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
@@ -39,11 +39,17 @@ class ScriptEngine;
|
||||
|
||||
class OpenSpaceEngine {
|
||||
public:
|
||||
enum class Renderers {
|
||||
VolumeRaycaster,
|
||||
Flare
|
||||
};
|
||||
|
||||
static void create(int argc, char** argv, int& newArgc, char**& newArgv);
|
||||
static void destroy();
|
||||
static OpenSpaceEngine& ref();
|
||||
|
||||
bool initialize();
|
||||
void setRenderer(OpenSpaceEngine::Renderers renderer);
|
||||
|
||||
ghoul::ConfigurationManager& configurationManager();
|
||||
InteractionHandler& interactionHandler();
|
||||
@@ -59,8 +65,6 @@ public:
|
||||
void mouseButtonCallback(int key, int action);
|
||||
void mousePositionCallback(int x, int y);
|
||||
void mouseScrollWheelCallback(int pos);
|
||||
void externalControlCallback(const char* receivedChars, int size, int clientId);
|
||||
|
||||
void encode();
|
||||
void decode();
|
||||
|
||||
@@ -76,7 +80,7 @@ private:
|
||||
InteractionHandler* _interactionHandler;
|
||||
RenderEngine* _renderEngine;
|
||||
ScriptEngine* _scriptEngine;
|
||||
Interface* _interface;
|
||||
// Interface* _interface;
|
||||
bool _useVolumeRaycaster, _useFlare;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user