| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "ishaders.h" |
| 4 |
#include "util/ScopedBoolLock.h" |
| 5 |
#include <wx/checkbox.h> |
| 6 |
|
| 7 |
namespace ui |
| 8 |
{ |
| 9 |
|
| 10 |
template<typename Source> |
| 11 |
class Binding |
| 12 |
{ |
| 13 |
private: |
| 14 |
Source _source; |
| 15 |
|
| 16 |
public: |
| 17 |
using Ptr = std::shared_ptr<Binding>; |
| 18 |
|
| 19 |
virtual ~Binding() |
| 20 |
{} |
| 21 |
|
| 22 |
const Source& getSource() const |
| 23 |
{ |
| 24 |
return _source; |
| 25 |
} |
| 26 |
|
| 27 |
void setSource(const Source& source) |
| 28 |
{ |
| 29 |
_source = source; |
| 30 |
onSourceChanged(); |
| 31 |
} |
| 32 |
|
| 33 |
virtual void updateFromSource() = 0; |
| 34 |
|
| 35 |
protected: |
| 36 |
virtual void onSourceChanged() |
| 37 |
{} |
| 38 |
}; |
| 39 |
|
| 40 |
// Target selection, usually the Source type is identical to the target type |
| 41 |
template<typename Source> |
| 42 |
struct TargetSelector |
| 43 |
{ |
| 44 |
using TargetType = Source; |
| 45 |
|
| 46 |
static TargetType CastToTarget(const Source& source) |
| 47 |
{ |
| 48 |
return source; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
// Specialisation for shader layer editing, where the target type is an IEditableShaderLayer |
| 53 |
template<> |
| 54 |
struct TargetSelector<IShaderLayer::Ptr> |
| 55 |
{ |
| 56 |
using TargetType = IEditableShaderLayer::Ptr; |
| 57 |
|
| 58 |
static TargetType CastToTarget(const IShaderLayer::Ptr& source) |
| 59 |
{ |
| 60 |
return std::static_pointer_cast<IEditableShaderLayer>(source); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
template<typename Source, typename ValueType> |
| 65 |
class TwoWayBinding : |
| 66 |
public Binding<Source> |
| 67 |
{ |
| 68 |
public: |
| 69 |
// Select target: will map Material => Material, but IShaderLayer => IEditableShaderLayer |
| 70 |
using Target = typename TargetSelector<Source>::TargetType; |
| 71 |
|
| 72 |
using LoadFunc = std::function<ValueType(const Source&)>; |
| 73 |
using UpdateFunc = std::function<void(const Target&, ValueType)>; |
| 74 |
using AcquireTargetFunc = std::function<Target()>; |
| 75 |
using PostUpdateFunc = std::function<void()>; |
| 76 |
|
| 77 |
protected: |
| 78 |
LoadFunc _loadValue; |
| 79 |
AcquireTargetFunc _acquireTarget; |
| 80 |
UpdateFunc _updateValue; |
| 81 |
PostUpdateFunc _postUpdate; |
| 82 |
|
| 83 |
protected: |
| 84 |
bool _blockUpdates; |
| 85 |
|
| 86 |
protected: |
| 87 |
TwoWayBinding(const LoadFunc& loadValue, |
| 88 |
const UpdateFunc& updateValue, |
| 89 |
const PostUpdateFunc& postChangeNotify = PostUpdateFunc(), |
| 90 |
const AcquireTargetFunc& acquireTarget = AcquireTargetFunc()) : |
| 91 |
_loadValue(loadValue), |
| 92 |
_acquireTarget(acquireTarget), |
| 93 |
_updateValue(updateValue), |
| 94 |
_postUpdate(postChangeNotify), |
| 95 |
_blockUpdates(false) |
| 96 |
{} |
| 97 |
|
| 98 |
Target getTarget() |
| 99 |
{ |
| 100 |
if (_blockUpdates) |
| 101 |
{ |
| 102 |
return Target(); |
| 103 |
} |
| 104 |
|
| 105 |
if (std::is_same<Source, Target>::value) |
| 106 |
{ |
| 107 |
return TargetSelector<Source>::CastToTarget(Binding<Source>::getSource()); |
| 108 |
} |
| 109 |
|
| 110 |
return _acquireTarget ? _acquireTarget() : Target(); |
| 111 |
} |
| 112 |
|
| 113 |
protected: |
| 114 |
// Just load the given value into the control |
| 115 |
virtual void setValueOnControl(const ValueType& value) = 0; |
| 116 |
|
| 117 |
virtual void updateFromSource() override |
| 118 |
{ |
| 119 |
util::ScopedBoolLock lock(_blockUpdates); |
| 120 |
|
| 121 |
if (!Binding<Source>::getSource()) |
| 122 |
{ |
| 123 |
setValueOnControl(ValueType()); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
setValueOnControl(_loadValue(Binding<Source>::getSource())); |
| 128 |
} |
| 129 |
|
| 130 |
virtual void updateValueOnTarget(const ValueType& newValue) |
| 131 |
{ |
| 132 |
auto target = getTarget(); |
| 133 |
|
| 134 |
if (target) |
| 135 |
{ |
| 136 |
_updateValue(target, newValue); |
| 137 |
|
| 138 |
if (_postUpdate) |
| 139 |
{ |
| 140 |
_postUpdate(); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
} |