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 "vstgui/standalone/include/helpers/value.h"
8 #include "vstgui/standalone/include/helpers/valuelistener.h"
9 #include "vstgui/standalone/include/iuidescwindow.h"
10 
11 namespace Mandelbrot {
12 
13 //------------------------------------------------------------------------
17 {
18  using Ptr = std::shared_ptr<ModelBinding>;
20  using ValuePtr = VSTGUI::Standalone::ValuePtr;
21  using ValueConverterPtr = VSTGUI::Standalone::ValueConverterPtr;
22 
23  static Ptr make (Model::Ptr model) { return std::make_shared<ModelBinding> (model); }
24 
25  ModelBinding (Model::Ptr model) : model (model)
26  {
27  using namespace VSTGUI::Standalone;
28  Value::performSingleStepEdit (*maxIterations.get (), model->getIterations ());
29 
30  values.emplace_back (maxIterations);
31  values.emplace_back (minX);
32  values.emplace_back (minY);
33  values.emplace_back (maxX);
34  values.emplace_back (maxY);
35  values.emplace_back (progressValue);
36  values.emplace_back (showParams);
37 
38  maxIterations->registerListener (this);
39  minX->registerListener (this);
40  minY->registerListener (this);
41  maxX->registerListener (this);
42  maxY->registerListener (this);
43 
44  model->registerListener (this);
45  }
46 
47  const ValueList& getValues () const override { return values; }
48 
49  void modelChanged (const Model& model) override
50  {
51  using namespace VSTGUI::Standalone;
52  Value::performSingleStepEdit (*maxIterations.get (), model.getIterations ());
53  Value::performSinglePlainEdit (*minX.get (), model.getMin ().x);
54  Value::performSinglePlainEdit (*minY.get (), model.getMin ().y);
55  Value::performSinglePlainEdit (*maxX.get (), model.getMax ().x);
56  Value::performSinglePlainEdit (*maxY.get (), model.getMax ().y);
57  }
58 
59  void onPerformEdit (IValue& value, IValue::Type newValue) override
60  {
61  using namespace VSTGUI::Standalone;
62  if (&value == maxIterations.get ())
63  {
64  auto step = Value::currentStepValue (*maxIterations.get ());
65  if (step != IStepValue::InvalidStep)
66  model->setIterations (step);
67  return;
68  }
69  auto min = model->getMin ();
70  auto max = model->getMax ();
71  if (&value == maxX.get ())
72  max.x = Value::currentPlainValue (value);
73  else if (&value == maxY.get ())
74  max.y = Value::currentPlainValue (value);
75  else if (&value == minX.get ())
76  min.x = Value::currentPlainValue (value);
77  else if (&value == minY.get ())
78  min.y = Value::currentPlainValue (value);
79  model->setMinMax (min, max);
80  }
81 
82  const ValuePtr& getProgressValue () const { return progressValue; }
83  const ValuePtr& getMaxIterationsValue () const { return maxIterations; }
84 private:
85  static constexpr auto numMaxIterations = 2048.;
86  ValueConverterPtr xConverter {VSTGUI::Standalone::Value::makeRangeConverter (-2.2, 1.2)};
87  ValueConverterPtr yConverter {VSTGUI::Standalone::Value::makeRangeConverter (-1.7, 1.7)};
88 
89  ValuePtr maxIterations {VSTGUI::Standalone::Value::makeStepValue ("max interations", numMaxIterations)};
90  ValuePtr minX {VSTGUI::Standalone::Value::make ("minX", 0., xConverter)};
91  ValuePtr minY {VSTGUI::Standalone::Value::make ("minY", 0., yConverter)};
92  ValuePtr maxX {VSTGUI::Standalone::Value::make ("maxX", 1., xConverter)};
93  ValuePtr maxY {VSTGUI::Standalone::Value::make ("maxY", 1., yConverter)};
94  ValuePtr progressValue {VSTGUI::Standalone::Value::make ("progress")};
95  ValuePtr showParams {VSTGUI::Standalone::Value::make ("showParams")};
96  ValueList values;
97 
98  Model::Ptr model;
99 };
100 }
Definition: modelbinding.h:14
ValueConverterPtr makeRangeConverter(IValue::Type minValue, IValue::Type maxValue)
Definition: value.cpp:526
ValuePtr makeStepValue(const UTF8String &id, IStepValue::StepType numSteps, IValue::Type initialValue=0., const ValueConverterPtr &valueConverter=nullptr)
Definition: value.cpp:480
Definition: mandelbrot.h:19
double Type
Definition: ivalue.h:24
Definition: AlertBoxDesign.cpp:11
Definition: valuelistener.h:18
ValuePtr make(const UTF8String &id, IValue::Type initialValue=0., const ValueConverterPtr &valueConverter=nullptr)
Definition: value.cpp:470
Definition: iuidescwindow.h:26
void onPerformEdit(IValue &value, IValue::Type newValue) override
Definition: modelbinding.h:59
Definition: ivalue.h:20
Definition: mandelbrot.h:25
Definition: mandelbrot.h:12