tb_widget_value.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef TB_WIDGET_VALUE_H
  6. #define TB_WIDGET_VALUE_H
  7. #include "tb_core.h"
  8. #include "tb_linklist.h"
  9. #include "tb_hashtable.h"
  10. #include "tb_value.h"
  11. #include "tb_id.h"
  12. namespace tb {
  13. class TBWidget;
  14. class TBWidgetValue;
  15. class TBValueGroup;
  16. /** TBWidgetValueConnection maintains a connection between TBWidget and TBWidgetValue. */
  17. class TBWidgetValueConnection : public TBLinkOf<TBWidgetValueConnection>
  18. {
  19. public:
  20. TBWidgetValueConnection() : m_value(nullptr) {}
  21. ~TBWidgetValueConnection() { Unconnect(); }
  22. /** Connect the value and widget. */
  23. void Connect(TBWidgetValue *value, TBWidget *m_widget);
  24. /** Unconnect the value and widget if it is connected. */
  25. void Unconnect();
  26. /** Synchronize the value of the widget to the TBWidgetValue and all other
  27. connected widgets. */
  28. void SyncFromWidget(TBWidget *source_widget);
  29. private:
  30. friend class TBWidgetValue;
  31. TBWidgetValue *m_value;
  32. TBWidget *m_widget;
  33. };
  34. /** TBWidgetValue stores a TBValue that will be synchronized with all widgets connected to it.
  35. It has a TBID name, that can be used to identify this value within its TBValueGroup.
  36. It will synchronize with widgets when any of the connected widgets change and trig the
  37. EVENT_TYPE_CHANGED event, and when the value is changed with any of the setters here.
  38. The synchronization with widgets is done through the generic TBWidget setters/getters,
  39. TBWidget::SetValue/GetValue/SetValueDouble/GetValueDouble/GetText/SetText.
  40. The type that is synchronized is determined by the TBValue::TYPE specified in the
  41. constructor.
  42. Note: The type that is synchronized changes if you request it in a different format!
  43. */
  44. class TBWidgetValue
  45. {
  46. public:
  47. TBWidgetValue(const TBID &name, TBValue::TYPE type = TBValue::TYPE_INT);
  48. ~TBWidgetValue();
  49. /** Set integer value and sync to connected widgets. */
  50. void SetInt(int value);
  51. /** Set text value and sync to connected widgets. */
  52. bool SetText(const char *text);
  53. /** Set double value and sync to connected widgets. */
  54. void SetDouble(double value);
  55. /** Set the value from the given widget. Using the current format type.*/
  56. void SetFromWidget(TBWidget *source_widget);
  57. /** Get value as integer. */
  58. int GetInt() { return m_value.GetInt(); }
  59. /** Get value as text. Return false on fail. */
  60. bool GetText(TBStr &text) { return text.Set(m_value.GetString()); }
  61. /** Get value as text. */
  62. TBStr GetText() { TBStr text; GetText(text); return text; }
  63. /** Get the value as double. */
  64. double GetDouble() { return m_value.GetFloat(); }
  65. /** Get the TBValue used to store the value. */
  66. const TBValue &GetValue() const { return m_value; }
  67. /** Get the name id. */
  68. TBID GetName() const { return m_name; }
  69. private:
  70. friend class TBWidgetValueConnection;
  71. TBID m_name;
  72. TBValue m_value;
  73. TBLinkListOf<TBWidgetValueConnection> m_connections;
  74. bool m_syncing;
  75. bool SyncToWidget(TBWidget *dst_widget);
  76. bool SyncToWidgets(TBWidget *exclude_widget);
  77. };
  78. /** Listener that will be notified when any of the values in a TBValueGroup is changed. */
  79. class TBValueGroupListener : public TBLinkOf<TBValueGroupListener>
  80. {
  81. public:
  82. virtual ~TBValueGroupListener() { if (linklist) linklist->Remove(this); }
  83. /** Called when a value has changed and all widgets connected to it has been updated. */
  84. virtual void OnValueChanged(const TBValueGroup *group, const TBWidgetValue *value) = 0;
  85. };
  86. /** TBValueGroup is a collection of widget values (TBWidgetValue) that can be fetched
  87. by name (using a TBID). It also keeps a list of TBValueGroupListener that listens to
  88. changes to any of the values. */
  89. class TBValueGroup
  90. {
  91. public:
  92. /** Create a TBWidgetValue with the given name if it does not already exist.
  93. Returns nullptr if out of memory. */
  94. TBWidgetValue *CreateValueIfNeeded(const TBID &name, TBValue::TYPE type = TBValue::TYPE_INT);
  95. /** Get the TBWidgetValue with the given name, or nullptr if no match is found. */
  96. TBWidgetValue *GetValue(const TBID &name) const { return m_values.Get(name); }
  97. /** Add listener to this group. It will be removed automatically when deleted,
  98. but can also be removed by RemoveListener. */
  99. void AddListener(TBValueGroupListener *listener) { m_listeners.AddLast(listener); }
  100. /** Remove listener from this group. */
  101. void RemoveListener(TBValueGroupListener *listener) { m_listeners.Remove(listener); }
  102. private:
  103. friend class TBWidgetValue;
  104. void InvokeOnValueChanged(const TBWidgetValue *value);
  105. TBHashTableAutoDeleteOf<TBWidgetValue> m_values; ///< Hash table of values
  106. TBLinkListOf<TBValueGroupListener> m_listeners; ///< List of listeners
  107. };
  108. /** The global value group. */
  109. extern TBValueGroup g_value_group;
  110. }; // namespace tb
  111. #endif // TB_WIDGET_VALUE_H