cgv
Loading...
Searching...
No Matches
control.h
1#pragma once
2
3#include "view.h"
4
5#include <cgv/signal/signal.h>
6
7#include "lib_begin.h"
8
9namespace cgv {
10 namespace gui {
11
13class CGV_API abst_control : public abst_view
14{
15public:
17 abst_control(const std::string& name);
19 bool shows(const void* ptr) const;
21 virtual bool controls(const void* ptr) const = 0;
23 virtual void attach_to_value_change(cgv::signal::functor_base* func) = 0;
25 virtual void attach_to_check_value(cgv::signal::functor_base* bool_func) = 0;
26};
27
30
31#if _MSC_VER >= 1400
32CGV_TEMPLATE template class CGV_API data::ref_ptr<abst_control>;
33#endif
34
37{
39
41 virtual bool controls(const void* ptr, void* user_data) const = 0;
42};
43
50template <typename T>
52{
54 virtual void set_value(const T& value, void* user_data) = 0;
56 virtual const T get_value(void* user_data) const = 0;
58 virtual bool controls(const void* ptr, void* user_data) const { return false; }
59};
60
80template <typename T>
81class control : public abst_control
82{
83private:
84 T* value_ptr;
86 T new_value;
87protected:
88 // protected function for the setter
89 void set_value(const T& v) {
90 if (cp)
91 cp->set_value(v, value_ptr);
92 else {
93 *value_ptr = v;
94 update_views();
95 }
96 }
97 // return pointer to value or 0 if not available
98 const T* get_value_ptr() const { return cp ? 0 : value_ptr; }
99public:
101 typedef cgv::signal::bool_signal<control<T>&> value_check_signal_type;
103 typedef cgv::signal::signal<control<T>&> value_change_signal_type;
105 control(const std::string& _name, T& _value) : abst_control(_name), value_ptr(&_value), new_value(_value), cp(0) {
106 attach_to_reference(value_ptr);
107 }
109 control(const std::string& _name, T* _value) : abst_control(_name) {
110 value_ptr = _value;
111 new_value = *_value;
112 cp = 0;
113 attach_to_reference(value_ptr);
114 }
118 control(const std::string& _name, abst_control_provider* _cp, void* user_data) : abst_control(_name), cp(0) {
119 if (_cp) {
120 cp = static_cast<control_provider<T>*>(_cp);
121 (void*&)value_ptr = user_data;
122 new_value = cp->get_value(user_data);
123 }
124 else {
125 value_ptr = (T*)user_data;
126 new_value = *value_ptr;
127 attach_to_reference(value_ptr);
128 }
129 }
131
133 cgv::signal::bool_signal<control<T>&> check_value;
135
136 cgv::signal::signal<control<T>&> value_change;
138 const T get_value() const { return cp ? cp->get_value(value_ptr) : *value_ptr; }
140 const T& get_new_value() const { return new_value; }
142 void set_new_value(const T& nv) { new_value = nv; }
144 const T& get_old_value() const { return new_value; }
146 bool check_and_set_value(const T& nv) {
147 set_new_value(nv);
148 if (check_value(*this)) {
149 T tmp_value = get_value();
150 set_value(this->get_new_value());
151 set_new_value(tmp_value);
152 value_change(*this);
153 return true;
154 }
155 return false;
156 }
158 bool controls(const void* ptr) const { return cp ? cp->controls(ptr,value_ptr) : (value_ptr == ptr); }
160 void attach_to_value_change(cgv::signal::functor_base* func) {
161 value_change.connect_abst(func);
162 }
164 void attach_to_check_value(cgv::signal::functor_base* bool_func) {
165 check_value.connect_abst(bool_func);
166 }
167};
168
169 }
170}
171
172#include <cgv/config/lib_end.h>
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
gui and type independent base class of all controls
Definition control.h:14
virtual bool controls(const void *ptr) const =0
return whether the control controls the value pointed to by ptr
virtual void attach_to_value_change(cgv::signal::functor_base *func)=0
attach a functor to the value change signal
virtual void attach_to_check_value(cgv::signal::functor_base *bool_func)=0
attach a functor to the value change signal
type independent &base class of all views
Definition view.h:13
control(const std::string &_name, T *_value)
construct abstract element from control_provider
Definition control.h:109
cgv::signal::bool_signal< control< T > & > value_check_signal_type
type of the value check signal
Definition control.h:101
bool check_and_set_value(const T &nv)
set new value only if check_value signal succeeds and send value_change signal. Return true if value ...
Definition control.h:146
const T & get_new_value() const
return the new value to the callbacks attached to the check_value signal
Definition control.h:140
void set_new_value(const T &nv)
set a different new value from the callbacks attached to the check_value signal
Definition control.h:142
cgv::signal::signal< control< T > & > value_change
this signal is sent after the user triggered a change of value and the check_value succeeded.
Definition control.h:136
const T get_value() const
return a reference to the current value
Definition control.h:138
control(const std::string &_name, T &_value)
construct abstract element from reference to value
Definition control.h:105
const T & get_old_value() const
return the old value to the callbacks attached to the change_value signal
Definition control.h:144
cgv::signal::bool_signal< control< T > & > check_value
this signal is sent when the user triggered a change of value in order to check whether the new value...
Definition control.h:133
void attach_to_value_change(cgv::signal::functor_base *func)
attach a functor to the value change signal
Definition control.h:160
void attach_to_check_value(cgv::signal::functor_base *bool_func)
attach a functor to the value change signal
Definition control.h:164
bool controls(const void *ptr) const
check whether the value represented by this element is pointing to the passed pointer
Definition control.h:158
control(const std::string &_name, abst_control_provider *_cp, void *user_data)
this constructor allows contruction from control_provider with user data or if the pointer to the con...
Definition control.h:118
cgv::signal::signal< control< T > & > value_change_signal_type
type of the value change signal
Definition control.h:103
data::ref_ptr< abst_control > control_ptr
ref counted pointer to abst control
Definition control.h:29
the cgv namespace
Definition print.h:11
type independent base class of control provider interface
Definition control.h:37
virtual bool controls(const void *ptr, void *user_data) const =0
overload to check if ptr points to the controlled value
virtual void set_value(const T &value, void *user_data)=0
overload to set the value
virtual bool controls(const void *ptr, void *user_data) const
the default implementation compares ptr to &get_value().
Definition control.h:58
virtual const T get_value(void *user_data) const =0
overload to get the value