Skip to content
Snippets Groups Projects
Commit 996a27af authored by stefan's avatar stefan
Browse files

Added GradientManager, loads preset gradients from info xml file

String constructor for gfx.Gradients (loads given gradient from GradientManager)

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2554 5a81b35b-ba03-0410-adc8-b2c5c5119f08
parent a51a8e17
No related branches found
No related tags found
No related merge requests found
......@@ -154,12 +154,14 @@ BOOST_PYTHON_MODULE(_gfx)
;
class_<Gradient>("Gradient", init<>())
.def(init<const String&>())
.def("SetColorAt", &Gradient::SetColorAt)
.def("GetColorAt", &Gradient::GetColorAt)
.def("GetStops", &Gradient::GetStops)
.def("GradientToInfo", &Gradient::GradientToInfo)
.def("GradientFromInfo", &Gradient::GradientFromInfo).staticmethod("GradientFromInfo")
;
implicitly_convertible<String, Gradient>();
class_<StopList>("StopList", init<>())
.def(vector_indexing_suite<StopList>())
......
......@@ -30,6 +30,7 @@ glext_include.hh
render_mode.hh
material.hh
gradient.hh
gradient_manager.hh
module_config.hh
primitives.hh
povray_fw.hh
......@@ -104,6 +105,7 @@ scene.cc
selection.cc
surface.cc
gradient.cc
gradient_manager.cc
vertex_array.cc
vertex_array_helper.cc
material.cc
......
......@@ -23,6 +23,8 @@
#include <ost/info/info.hh>
#include <ost/info/info_fw.hh>
#include <ost/gfx/gradient_manager.hh>
namespace ost { namespace gfx {
......@@ -30,6 +32,16 @@ Gradient::Gradient()
{
}
Gradient::Gradient(const String& name)
{
Gradient gradient = GradientManager::Instance().GetGradient(name);
StopList stops = gradient.GetStops();
for(unsigned int i = 0; i < stops.size(); i++){
Stop stop = stops[i];
this->SetColorAt(stop.t, stop.color);
}
}
void Gradient::SetColorAt(float t, const Color& c)
{
std::vector<Stop>::iterator i;
......@@ -44,7 +56,7 @@ void Gradient::SetColorAt(float t, const Color& c)
Color Gradient::GetColorAt(float t) const
{
if (stops_.empty())
return Color(0.0, 0.0, 0.0, 1.0);
throw Error("Can not calculate color with 0 Stops set!");
uint c=0;
while (t>=stops_[c].t && c<stops_.size()) {
......
......@@ -71,6 +71,9 @@ class DLLEXPORT_OST_GFX Gradient {
public:
Gradient();
Gradient(const String& name);
/// \brief get color
Color GetColorAt(float t) const;
/// \brief set color
......
// Copyright (C) 2008-2010 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
/*
Author: Stefan Scheuber
*/
#include "boost/filesystem/operations.hpp"
#include <ost/platform.hh>
#include <ost/info/info.hh>
#include "gradient_manager.hh"
namespace ost { namespace gfx {
String STORED_GRADIENT_PATH = "/scene/gradients.xml";
GradientManager& GradientManager::Instance()
{
static GradientManager inst;
return inst;
}
GradientManager::GradientManager()
{
String path = GetSharedDataPath()+STORED_GRADIENT_PATH;
if(boost::filesystem::exists(path)){
info::InfoHandle info_handler = info::LoadInfo(path);
if(info_handler.HasGroup("Gradients")){
info::InfoGroup gradients = info_handler.GetGroup("Gradients");
info::InfoGroupList list = gradients.GetGroups("Gradient");
for(unsigned int i = 0; i< list.size();i++){
info::InfoGroup info_gradient = list[i];
String name = info_gradient.GetAttribute("Name");
Gradient gradient = Gradient::GradientFromInfo(info_gradient);
this->SetGradient(name,gradient);
}
}
}
}
GradientManager::~GradientManager()
{
}
const std::vector<String> GradientManager::GetGradientNames() const
{
std::vector<String> keys;
for(std::map<String , Gradient>::const_iterator it = gradients_.begin(); it != gradients_.end(); ++it) {
keys.push_back(it->first);
}
return keys;
}
const Gradient& GradientManager::GetGradient(String name) const
{
std::map<String,Gradient>::const_iterator it=gradients_.find(name);
if(gradients_.end() != it){
return gradients_[name];
}
return empty_gradient_;
}
void GradientManager::SetGradient(String name, Gradient gradient)
{
gradients_[name] = gradient;
}
void GradientManager::RemoveGradient(String name)
{
std::map<String,Gradient>::const_iterator it=gradients_.find(name);
if(gradients_.end() != it){
gradients_.erase(name);
}
}
}} //ns
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2010 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//------------------------------------------------------------------------------
#ifndef OST_GFX_GRADIENT_MANAGER_HH
#define OST_GFX_GRADIENT_MANAGER_HH
#include <vector>
#include <map>
#include <ost/gfx/module_config.hh>
#include <ost/gfx/gradient.hh>
/*
Author: Stefan Scheuber
*/
namespace ost { namespace gfx {
class
//DLLEXPORT_OST_GFX
GradientManager {
public:
static GradientManager& Instance();
const std::vector<String> GetGradientNames() const;
const Gradient& GetGradient(String name) const;
void SetGradient(String name, Gradient gradient);
void RemoveGradient(String name);
virtual ~GradientManager();
private:
GradientManager();
mutable std::map<String, Gradient> gradients_;
Gradient empty_gradient_;
};
}} //ns
#endif /* OST_GFX_GRADIENT_MANAGER_HH */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment