Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_widget_value.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_WIDGET_VALUE_H
7 #define TB_WIDGET_VALUE_H
8 
9 #include "tb_core.h"
10 #include "tb_linklist.h"
11 #include "tb_hashtable.h"
12 #include "tb_value.h"
13 #include "tb_id.h"
14 
15 namespace tb {
16 
17 class TBWidget;
18 class TBWidgetValue;
19 class TBValueGroup;
20 
23 class TBWidgetValueConnection : public TBLinkOf<TBWidgetValueConnection>
24 {
25 public:
26  TBWidgetValueConnection() : m_value(nullptr) {}
28 
30  void Connect(TBWidgetValue *value, TBWidget *m_widget);
31 
33  void Unconnect();
34 
36  TBID GetName() const;
37 
40  void SyncFromWidget(TBWidget *source_widget);
41 private:
42  friend class TBWidgetValue;
43  TBWidgetValue *m_value;
44  TBWidget *m_widget;
45 };
46 
63 {
64 public:
65  TBWidgetValue(const TBID &name, TBValue::TYPE type = TBValue::TYPE_INT);
66  ~TBWidgetValue();
67 
69  void SetInt(long value);
70 
72  void SetInt(int value) { SetInt((long)value); }
73 
75  bool SetText(const TBStr & text);
76 
78  void SetDouble(double value);
79 
81  void SetValue(const TBValue & value);
82 
84  void SetFromWidget(TBWidget *source_widget);
85 
87  long GetInt() const { return m_value.GetInt(); }
88 
90  bool GetText(TBStr &text) const { text = m_value.GetString(); return true; }
91 
93  TBStr GetText() const { return m_value.GetString(); }
94 
96  double GetDouble() const { return m_value.GetFloat(); }
97 
99  const TBValue &GetValue() const { return m_value; }
100 
102  TBID GetName() const { return m_name; }
103 
104 private:
105  friend class TBWidgetValueConnection;
106  TBID m_name;
107  TBValue m_value;
109  bool m_syncing;
110 
111  bool SyncToWidget(TBWidget *dst_widget);
112  bool SyncToWidgets(TBWidget *exclude_widget);
113 };
114 
117 class TBValueGroupListener : public TBLinkOf<TBValueGroupListener>
118 {
119 public:
120  virtual ~TBValueGroupListener() { if (linklist) linklist->Remove(this); }
121 
123  virtual void OnValueChanged(const TBValueGroup *group, const TBWidgetValue *value) = 0;
124 };
125 
131 {
132 public:
135  TBWidgetValue *CreateValueIfNeeded(const TBID &name, TBValue::TYPE type = TBValue::TYPE_INT);
136 
138  TBWidgetValue *GetValue(const TBID &name) const { return m_values.Get(name); }
139 
142  void AddListener(TBValueGroupListener *listener) { m_listeners.AddLast(listener); }
143 
145  void RemoveListener(TBValueGroupListener *listener) { m_listeners.Remove(listener); }
146 private:
147  friend class TBWidgetValue;
148  void InvokeOnValueChanged(const TBWidgetValue *value);
149 
152 };
153 
155 extern TBValueGroup g_value_group;
156 
157 } // namespace tb
158 
159 #endif // TB_WIDGET_VALUE_H
TBWidgetValue * CreateValueIfNeeded(const TBID &name, TBValue::TYPE type=TBValue::TYPE_INT)
Create a TBWidgetValue with the given name if it does not already exist.
Definition: tb_widget_value.cpp:170
double GetDouble() const
Get the value as double.
Definition: tb_widget_value.h:96
void SetValue(const TBValue &value)
Set tb value and sync to connected widgets.
Definition: tb_widget_value.cpp:158
bool SetText(const TBStr &text)
Set text value and sync to connected widgets.
Definition: tb_widget_value.cpp:141
TBID GetName() const
Get the connection name id.
Definition: tb_widget_value.cpp:30
const TBValue & GetValue() const
Get the TBValue used to store the value.
Definition: tb_widget_value.h:99
TYPE
The current type of the value.
Definition: tb_value.h:64
The base TBWidget class.
Definition: tb_widgets.h:418
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:112
void RemoveListener(TBValueGroupListener *listener)
Remove listener from this group.
Definition: tb_widget_value.h:145
long GetInt() const
Get value as integer.
Definition: tb_widget_value.h:87
TBWidgetValue * GetValue(const TBID &name) const
Get the TBWidgetValue with the given name, or nullptr if no match is found.
Definition: tb_widget_value.h:138
TBID is a wrapper for a uint32_t to be used as ID.
Definition: tb_id.h:18
TBWidgetValue stores a TBValue that will be synchronized with all widgets connected to it...
Definition: tb_widget_value.h:62
void SetInt(long value)
Set integer value and sync to connected widgets.
Definition: tb_widget_value.cpp:133
void SetDouble(double value)
Set double value and sync to connected widgets.
Definition: tb_widget_value.cpp:150
void SyncFromWidget(TBWidget *source_widget)
Synchronize the value of the widget to the TBWidgetValue and all other connected widgets.
Definition: tb_widget_value.cpp:37
TBID GetName() const
Get the name id.
Definition: tb_widget_value.h:102
TBStr is a simple string class.
Definition: tb_str.h:62
TBValue holds value of a specific type.
Definition: tb_value.h:59
TBValueGroup is a collection of widget values (TBWidgetValue) that can be fetched by name (using a TB...
Definition: tb_widget_value.h:130
void Connect(TBWidgetValue *value, TBWidget *m_widget)
Connect the value and widget.
Definition: tb_widget_value.cpp:13
TBWidgetValueConnection maintains a connection between TBWidget and TBWidgetValue.
Definition: tb_widget_value.h:23
bool GetText(TBStr &text) const
Get value as text.
Definition: tb_widget_value.h:90
void SetInt(int value)
Set integer value and sync to connected widgets.
Definition: tb_widget_value.h:72
virtual void OnValueChanged(const TBValueGroup *group, const TBWidgetValue *value)=0
Called when a value has changed and all widgets connected to it has been updated. ...
Listener that will be notified when any of the values in a TBValueGroup is changed.
Definition: tb_widget_value.h:117
TBValueGroup g_value_group
The global value group.
Definition: tb_widget_value.cpp:168
void Unconnect()
Unconnect the value and widget if it is connected.
Definition: tb_widget_value.cpp:22
void SetFromWidget(TBWidget *source_widget)
Set the value from the given widget.
Definition: tb_widget_value.cpp:58
void AddListener(TBValueGroupListener *listener)
Add listener to this group.
Definition: tb_widget_value.h:142
TBStr GetText() const
Get value as text.
Definition: tb_widget_value.h:93