vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Menu.h
Go to the documentation of this file.
1 /*
2  * VRECKO - Virtual reality engine
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License version 2.1, as published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * Copyright (c) 2011 Martin Bezdeka <mbezdeka@mail.muni.cz>
14  *
15  */
16 #pragma once
17 #include <vreckoUtils/Export>
18 
19 namespace vreckoUtils
20 {
21 
22 class MenuVisitor;
23 
24 class VRECKO_UTILS_EXPORT MenuCommon
25 {
26 public:
27  static void createMenuItem( std::ostringstream& menuSS, const char* itemName, const char* caption, osg::Vec3 color = osg::Vec3(0.9f, 0.8f, 0.8f) );
28  static void createMenuSlider(std::ostringstream& menuSS, const char* itemName, const char* caption, double sliderFrom,
29  double sliderTo, bool integerOnly, double sliderPos, osg::Vec3 color = osg::Vec3(0.9f, 0.8f, 0.8f));
30 };
31 
32 class VRECKO_UTILS_EXPORT IMenuComponent : public osg::Referenced
33 {
34 public:
35  virtual std::string toXMLString() = 0;
36  virtual std::string getName() = 0;
37  virtual std::string getCaption() = 0;
38  virtual void accept(MenuVisitor& visitor) = 0;
39  virtual ~IMenuComponent() {};
40 };
41 
42 typedef std::vector< osg::ref_ptr<IMenuComponent> >::const_iterator IMenuComponentIter;
43 
44 class VRECKO_UTILS_EXPORT MenuBase : public IMenuComponent
45 {
46 protected:
47  std::vector<osg::ref_ptr<IMenuComponent>> _items;
48 
49 public:
50  void addChild(osg::ref_ptr<IMenuComponent> menuComponent) {
51  _items.push_back(menuComponent);
52  }
53 
54  void removeChild(int index) {
55  _items.erase(_items.begin() + index);
56  }
57  IMenuComponent* getChild(u_int index) {
58  if(index < 0 || index >= _items.size())
59  return NULL;
60  return _items[index];
61  }
62 };
63 
64 class VRECKO_UTILS_EXPORT MenuRoot : public MenuBase
65 {
66 protected:
67  std::map<std::string, double> _attributes;
68 public:
69  MenuRoot() {};
70  virtual std::string toXMLString();
71 
72  virtual std::string getName() { return ""; };
73  virtual std::string getCaption() { return ""; };
74 
75  void addAttribute(const std::string& name, double value) {
76  _attributes[name] = value;
77  }
78 
79  bool removeAttribute(const std::string& name)
80  {
81  return _attributes.erase(name) == 1;
82  }
83 
84  virtual void accept(MenuVisitor& visitor);
85 };
86 
87 class VRECKO_UTILS_EXPORT Menu : public MenuBase
88 {
89 protected:
90  std::string _menuName, _menuCaption;
91 public:
92  Menu(string name = "", string caption= "") : _menuName(name), _menuCaption(caption) {};
93 
94  virtual std::string toXMLString();
95 
96  virtual std::string getName() {
97  return _menuName;
98  }
99 
100  virtual std::string getCaption() {
101  return _menuCaption;
102  }
103 
104  virtual void accept(MenuVisitor& visitor);
105 };
106 
107 typedef osg::ref_ptr<Menu> MenuPtr;
108 
109 class VRECKO_UTILS_EXPORT MenuItem : public IMenuComponent
110 {
111 protected:
112  std::string _itemName, _itemCaption;
113  osg::Vec3 _color;
114 
115 public:
116  MenuItem(string name, string caption, osg::Vec3 color = osg::Vec3(0.9f, 0.8f, 0.8f)) :
117  _itemName(name), _itemCaption(caption), _color(color) {};
118  MenuItem(string name, string caption, osg::Vec4 color) :
119  _itemName(name), _itemCaption(caption), _color(color._v[0], color._v[1], color._v[2]) {};
120 
121  virtual std::string toXMLString();
122 
123  virtual std::string getName() {
124  return _itemName;
125  }
126 
127  virtual std::string getCaption() {
128  return _itemCaption;
129  }
130 
131  virtual void accept(MenuVisitor& visitor);
132 };
133 
134 class VRECKO_UTILS_EXPORT MenuSlider : public IMenuComponent
135 {
136 protected:
137  std::string _sliderName, _sliderCaption;
138  osg::Vec3 _color;
139  double _from, _to;
140  double _value;
141  bool _onlyInt;
142 
143 public:
144  MenuSlider(string name, string caption, double from, double to,
145  double value, bool onlyInt, osg::Vec3 color = osg::Vec3(0.9f, 0.8f, 0.8f)) :
146  _sliderName(name), _sliderCaption(caption), _from(from), _to(to),
147  _value(value), _onlyInt(onlyInt), _color(color) {};
148 
149  virtual std::string toXMLString();
150 
151  virtual std::string getName() {
152  return _sliderName;
153  }
154 
155  virtual std::string getCaption() {
156  return _sliderCaption;
157  }
158 
159  double getValue() const {
160  return _value;
161  }
162 
163  void setValue(double val) {
164  _value = val;
165  }
166 
167  virtual void accept(MenuVisitor& visitor);
168 };
169 
170 class VRECKO_UTILS_EXPORT MenuVisitor
171 {
172 public:
174  virtual ~MenuVisitor() {}
175 
176  virtual void apply(IMenuComponent&) {}
177  virtual void apply(Menu&) {}
178  virtual void apply(MenuRoot&) {}
179  virtual void apply(MenuItem&) {}
180  virtual void apply(MenuSlider&) {}
181 };
182 
183 }