Skip to content
Permalink
Browse files

Update

  • Loading branch information...
vinjn committed Feb 12, 2016
1 parent ffc25d2 commit 8560fbe7cc855caf719197c140e5da534096592e
Showing with 241 additions and 0 deletions.
  1. BIN cinderblock.png
  2. +15 −0 cinderblock.xml
  3. +180 −0 src/MiniConfig.cpp
  4. +46 −0 src/MiniConfig.h
BIN +16.6 KB cinderblock.png
Binary file not shown.
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<cinder>
<block
name="vnm"
id="org.vnm.utils"
author="Vinjn Zhang"
license="BSD"
summary="Implements the utils"
core="false"
>
<sourcePattern>src/*.cpp</sourcePattern>
<headerPattern>src/*.h</headerPattern>
<includePath>src</includePath>
</block>
</cinder>
@@ -0,0 +1,180 @@
#include "MiniConfig.h"
#include <cinder/xml.h>
#include <cinder/Utilities.h>
#include <cinder/app/App.h>
#include <cinder/params/Params.h>

using namespace ci;
using namespace ci::app;
using namespace std;

#define GROUP_DEF(grp)
#define ITEM_DEF(type, var, default) type var = default;
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF

namespace
{
const string kConfigFileName = "MiniConfig.xml";

void revertToDefaultValues()
{
#define GROUP_DEF(grp)
#define ITEM_DEF(type, var, default) var = default;
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF
console() << "MiniConfig reverted to default values" << endl;
}
}

void readConfig()
{
fs::path configPath = getAssetPath("./") / kConfigFileName;
try
{
XmlTree tree(loadFile(configPath));
XmlTree group;

#define GROUP_DEF(grp) group = tree.getChild(#grp);
#define ITEM_DEF(type, var, default) \
do \
{ \
if (group.getTag().empty()) \
var = tree.getChild(#var).getValue<type>(); \
else \
var = group.getChild(#var).getValue<type>(); \
} while (0);
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF
console() << "Reads from " << configPath.string() << endl;
}
catch (exception& e)
{
console() << e.what() << endl;
console() << "[Warning] Fails to read from " << configPath.string() << endl;
revertToDefaultValues();
writeConfig();
}
}

void writeConfig()
{
fs::path configPath = getAssetPath("./") / kConfigFileName;
try
{
XmlTree tree = XmlTree::createDoc();
XmlTree group;

#define GROUP_DEF(grp) \
do \
{ \
if (!group.getTag().empty()) tree.push_back(group); \
group = XmlTree(#grp, ""); \
} while (0);

#define ITEM_DEF(type, var, default) \
do \
{ \
XmlTree item(#var, toString(var)); \
if (group.getTag().empty()) tree.push_back(item); \
else group.push_back(item); \
} while (0);
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF
if (!group.getTag().empty()) tree.push_back(group);

#ifdef CHINESE_GBK_ENCODING_ENABLED
// OStreamRef os = writeFile(configPath)->getStream();
ofstream of(configPath.string().c_str());
const string kGbkHeader = "<?xml version=\"1.0\" encoding=\"gbk\"?>";
of << kGbkHeader << endl << tree;
#else
tree.write( writeFile(configPath));
#endif
console() << "Writes to " << configPath.string() <<endl;
}
catch( ... ) {
console() << "[Warning] Fails to write to " << configPath.string() <<endl;
}
}

void setupConfigUI(cinder::params::InterfaceGl* params)
{
params->addButton("SAVE", writeConfig);
#define GROUP_DEF(grp) params->addSeparator(#grp);
#define ITEM_DEF(type, var, default) params->addParam(#var, &var);
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) \
do \
{ \
type step = (Max - Min) / (type)500; \
params->addParam(#var, &var).min(Min).max(Max).step(step); \
} while(0);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF
params->addSeparator();

getWindow()->getSignalPostDraw().connect(std::bind(&params::InterfaceGl::draw, params));
}

namespace
{
const int kPODItemHeight = 16;
template <typename T>
int getItemHeight(const T&)
{
return kPODItemHeight;
}

template <>
int getItemHeight(const quat&)
{
return 80;
}

template <>
int getItemHeight(const vec3&)
{
return 120;
}

template <>
int getItemHeight(const Color&)
{
return 80;
}

template <>
int getItemHeight(const ColorA&)
{
return 80;
}
}

int getConfigUIHeight()
{
int height = kPODItemHeight * 4; // top + bottom

#define GROUP_DEF(grp) height += kPODItemHeight;
#define ITEM_DEF(type, var, default) height += getItemHeight(var);
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF

return height;
}
@@ -0,0 +1,46 @@
#pragma once

#include <string>
#include "cinder/Vector.h"
#include "cinder/Quaternion.h"
#include "cinder/Color.h"

using std::string;
using ci::vec3;
using ci::quat;
using ci::Color;
using ci::ColorA;

#define GROUP_DEF(grp)
#define ITEM_DEF(type, var, default) extern type var;
#define ITEM_DEF_MINMAX(type, var, default, Min, Max) ITEM_DEF(type, var, default);
#include "item.def"
#undef ITEM_DEF_MINMAX
#undef ITEM_DEF
#undef GROUP_DEF

// item.def is the place to define your global variables
// in the format of
//
/*
ITEM_DEF(int, APP_WIDTH, 882)
ITEM_DEF(int, APP_HEIGHT, 725)
ITEM_DEF(string, NAME, "vinjn")
ITEM_DEF(float, velocity, 3.0f)
ITEM_DEF_MINMAX(float, delay, 3.0f, 0.0f, 10.0f)
*/

void readConfig();
void writeConfig();

namespace cinder { namespace params {
class InterfaceGl;
} }

#define ADD_ENUM_TO_INT(param, intValue, enumNames) \
param->removeParam(#intValue); \
param->addParam(#intValue, enumNames, &intValue);

void setupConfigUI(cinder::params::InterfaceGl* params);
int getConfigUIHeight();

0 comments on commit 8560fbe

Please sign in to comment.
You can’t perform that action at this time.