ASPiK SDK
modelbinding.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #pragma once
6 
7 #include "../../iuidescwindow.h"
8 #include "../valuelistener.h"
9 #include <functional>
10 #include <unordered_map>
11 
12 //------------------------------------------------------------------------
13 namespace VSTGUI {
14 namespace Standalone {
15 namespace UIDesc {
16 
17 //------------------------------------------------------------------------
18 struct ValueCalls
19 {
20  using Call = std::function<void (IValue&)>;
21 
22  Call onBeginEditCall;
23  Call onPerformEditCall;
24  Call onEndEditCall;
25  Call onStateChangeCall;
26 
27  static ValueCalls onPerformEdit (Call&& call)
28  {
29  ValueCalls c;
30  c.onPerformEditCall = std::move (call);
31  return c;
32  }
33 
34  static ValueCalls onEndEdit (Call&& call)
35  {
36  ValueCalls c;
37  c.onEndEditCall = std::move (call);
38  return c;
39  }
40 
41  static ValueCalls onAction (Call&& call)
42  {
43  ValueCalls c;
44  c.onEndEditCall = [call = std::move (call)] (IValue & v)
45  {
46  if (v.getValue () > 0.5)
47  call (v);
48  };
49  return c;
50  }
51 };
52 
54 using ModelBindingCallbacksPtr = std::shared_ptr<ModelBindingCallbacks>;
55 
56 //------------------------------------------------------------------------
58 {
59 public:
60  static ModelBindingCallbacksPtr make () { return std::make_shared<ModelBindingCallbacks> (); }
61  ~ModelBindingCallbacks () override;
62 
63  ValuePtr addValue (ValuePtr value, const ValueCalls& callbacks = {});
64  ValuePtr addValue (ValuePtr value, ValueCalls&& callbacks);
65 
66  ValuePtr getValue (UTF8StringView valueID) const;
67 
68 private:
69  const ValueList& getValues () const override { return valueList; }
70 
71  void onBeginEdit (IValue& value) override;
72  void onPerformEdit (IValue& value, IValue::Type newValue) override;
73  void onEndEdit (IValue& value) override;
74  void onStateChange (IValue& value) override;
75 
76  using ValueMap = std::unordered_map<const IValue*, ValueCalls>;
77  ValueList valueList;
78  ValueMap values;
79 };
80 
81 //------------------------------------------------------------------------
82 inline ModelBindingCallbacks::~ModelBindingCallbacks ()
83 {
84  for (auto& v : valueList)
85  v->unregisterListener (this);
86  values.clear ();
87  valueList.clear ();
88 }
89 
90 //------------------------------------------------------------------------
91 inline ValuePtr ModelBindingCallbacks::addValue (ValuePtr value, const ValueCalls& callbacks)
92 {
93  values.emplace (value.get (), callbacks);
94  valueList.emplace_back (value);
95  value->registerListener (this);
96  return value;
97 }
98 
99 //------------------------------------------------------------------------
100 inline ValuePtr ModelBindingCallbacks::addValue (ValuePtr value, ValueCalls&& callbacks)
101 {
102  values.emplace (value.get (), std::move (callbacks));
103  valueList.emplace_back (value);
104  value->registerListener (this);
105  return value;
106 }
107 
108 //------------------------------------------------------------------------
109 inline ValuePtr ModelBindingCallbacks::getValue (UTF8StringView valueID) const
110 {
111  for (auto& v : valueList)
112  {
113  if (v->getID () == valueID)
114  return v;
115  }
116  return nullptr;
117 }
118 
119 //------------------------------------------------------------------------
120 inline void ModelBindingCallbacks::onBeginEdit (IValue& value)
121 {
122  auto it = values.find (&value);
123  if (it != values.end () && it->second.onBeginEditCall)
124  it->second.onBeginEditCall (value);
125 }
126 
127 //------------------------------------------------------------------------
128 inline void ModelBindingCallbacks::onPerformEdit (IValue& value, IValue::Type newValue)
129 {
130  auto it = values.find (&value);
131  if (it != values.end () && it->second.onPerformEditCall)
132  it->second.onPerformEditCall (value);
133 }
134 
135 //------------------------------------------------------------------------
136 inline void ModelBindingCallbacks::onEndEdit (IValue& value)
137 {
138  auto it = values.find (&value);
139  if (it != values.end () && it->second.onEndEditCall)
140  it->second.onEndEditCall (value);
141 }
142 
143 //------------------------------------------------------------------------
144 inline void ModelBindingCallbacks::onStateChange (IValue& value)
145 {
146  auto it = values.find (&value);
147  if (it != values.end () && it->second.onStateChangeCall)
148  it->second.onStateChangeCall (value);
149 }
150 
151 //------------------------------------------------------------------------
152 } // UIDesc
153 } // Standalone
154 } // VSTGUI
double Type
Definition: ivalue.h:24
Definition: valuelistener.h:18
Definition: customcontrols.cpp:8
Definition: iuidescwindow.h:26
a view on a null terminated UTF-8 String
Definition: cstring.h:172
Definition: ivalue.h:20
Definition: modelbinding.h:18