diff --git a/modules/gfx/pymod/wrap_gfx.cc b/modules/gfx/pymod/wrap_gfx.cc index dd2049702cb4ca0d643517dce0de21917630ae6c..741675581e2632b2b8fa0bba181db6b8fc89c3ed 100644 --- a/modules/gfx/pymod/wrap_gfx.cc +++ b/modules/gfx/pymod/wrap_gfx.cc @@ -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>()) diff --git a/modules/gfx/src/CMakeLists.txt b/modules/gfx/src/CMakeLists.txt index c07798109ce865af880e7d071888cbdac7ae7991..0d10efc8c48a9857bc12a3dcd04e8e6038f13383 100644 --- a/modules/gfx/src/CMakeLists.txt +++ b/modules/gfx/src/CMakeLists.txt @@ -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 diff --git a/modules/gfx/src/gradient.cc b/modules/gfx/src/gradient.cc index 7b9566a1bb64d3c18bd13ed8e650e2f645c6cd11..e0b50a92f09e6a92ddf8892c58fbd3de1244e067 100644 --- a/modules/gfx/src/gradient.cc +++ b/modules/gfx/src/gradient.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()) { diff --git a/modules/gfx/src/gradient.hh b/modules/gfx/src/gradient.hh index a27136528fa36cb52bf7a175191151d2fc08fee9..5b28b849b05e81af567bce2232b768f8b619ec62 100644 --- a/modules/gfx/src/gradient.hh +++ b/modules/gfx/src/gradient.hh @@ -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 diff --git a/modules/gfx/src/gradient_manager.cc b/modules/gfx/src/gradient_manager.cc new file mode 100644 index 0000000000000000000000000000000000000000..9b6fb63f75e07f7d77e07fbde670251e3e717caa --- /dev/null +++ b/modules/gfx/src/gradient_manager.cc @@ -0,0 +1,91 @@ +// 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 diff --git a/modules/gfx/src/gradient_manager.hh b/modules/gfx/src/gradient_manager.hh new file mode 100644 index 0000000000000000000000000000000000000000..7551570bae93049c36a6d5bf4505ed2eafa07b86 --- /dev/null +++ b/modules/gfx/src/gradient_manager.hh @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// 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 */