iPlug2 - C++ Audio Plug-in Framework
IGraphicsEditorDelegate.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
12 #include "IGraphics.h"
13 #include "IControl.h"
14 
15 using namespace iplug;
16 using namespace igraphics;
17 
18 IGEditorDelegate::IGEditorDelegate(int nParams)
19 : IEditorDelegate(nParams)
20 {
21 }
22 
23 IGEditorDelegate::~IGEditorDelegate()
24 {
25 }
26 
27 void* IGEditorDelegate::OpenWindow(void* pParent)
28 {
29  if(!mGraphics)
30  {
31  mGraphics = std::unique_ptr<IGraphics>(CreateGraphics());
32  if (mLastWidth && mLastHeight && mLastScale)
33  GetUI()->Resize(mLastWidth, mLastHeight, mLastScale);
34  }
35 
36  if(mGraphics)
37  return mGraphics->OpenWindow(pParent);
38  else
39  return nullptr;
40 }
41 
43 {
44  if (!mClosing)
45  {
46  mClosing = true;
48 
49  if (mGraphics)
50  {
51  mLastWidth = mGraphics->Width();
52  mLastHeight = mGraphics->Height();
53  mLastScale = mGraphics->GetDrawScale();
54  mGraphics->CloseWindow();
55  mGraphics = nullptr;
56  }
57 
58  mClosing = false;
59  }
60 }
61 
63 {
64  if (GetUI())
65  mGraphics->SetScreenScale(scale);
66 }
67 
68 void IGEditorDelegate::SendControlValueFromDelegate(int ctrlTag, double normalizedValue)
69 {
70  if(!mGraphics)
71  return;
72 
73  IControl* pControl = mGraphics->GetControlWithTag(ctrlTag);
74 
75  assert(pControl);
76 
77  if(pControl)
78  {
79  pControl->SetValueFromDelegate(normalizedValue);
80  }
81 }
82 
83 void IGEditorDelegate::SendControlMsgFromDelegate(int ctrlTag, int msgTag, int dataSize, const void* pData)
84 {
85  if(!mGraphics)
86  return;
87 
88  IControl* pControl = mGraphics->GetControlWithTag(ctrlTag);
89 
90  assert(pControl);
91 
92  if(pControl)
93  {
94  pControl->OnMsgFromDelegate(msgTag, dataSize, pData);
95  }
96 }
97 
98 void IGEditorDelegate::SendParameterValueFromDelegate(int paramIdx, double value, bool normalized)
99 {
100  if(mGraphics)
101  {
102  if (!normalized)
103  value = GetParam(paramIdx)->ToNormalized(value);
104 
105  for (int c = 0; c < mGraphics->NControls(); c++)
106  {
107  IControl* pControl = mGraphics->GetControl(c);
108 
109  int nVals = pControl->NVals();
110 
111  for(int v = 0; v < nVals; v++)
112  {
113  if (pControl->GetParamIdx(v) == paramIdx)
114  {
115  pControl->SetValueFromDelegate(value, v);
116  // Could be more than one, don't break until we check them all.
117  }
118  }
119 
120  }
121  }
122 
123  IEditorDelegate::SendParameterValueFromDelegate(paramIdx, value, normalized);
124 }
125 
127 {
128  if(mGraphics)
129  {
130  for (auto c = 0; c < mGraphics->NControls(); c++) // TODO: could keep a map
131  {
132  IControl* pControl = mGraphics->GetControl(c);
133 
134  if (pControl->GetWantsMidi())
135  {
136  pControl->OnMidi(msg);
137  }
138  }
139  }
140 
142 }
143 
145 {
146  bool savedOK = true;
147 
148  int width = mGraphics ? mGraphics->Width() : mLastWidth;
149  int height = mGraphics ? mGraphics->Height() : mLastHeight;
150  float scale = mGraphics ? mGraphics->GetDrawScale() : mLastScale;
151 
152  savedOK &= data.Put(&width);
153  savedOK &= data.Put(&height);
154  savedOK &= data.Put(&scale);
155 
156  return savedOK;
157 }
158 
160 {
161  int width = 0;
162  int height = 0;
163  float scale = 0.f;
164 
165  startPos = data.Get(&width, startPos);
166  startPos = data.Get(&height, startPos);
167  startPos = data.Get(&scale, startPos);
168 
169  if (GetUI())
170  {
171  if (width && height && scale)
172  GetUI()->Resize(width, height, scale);
173  }
174  else
175  {
176  mLastWidth = width;
177  mLastHeight = height;
178  mLastScale = scale;
179  }
180 
181  return startPos;
182 }
183 
185 {
186  return SerializeEditorSize(chunk);
187 }
188 
190 {
191  return UnserializeEditorSize(chunk, startPos);
192 }
193 
195 {
196  IGraphics* pGraphics = GetUI();
197 
198  if (pGraphics)
199  {
200  float x, y;
201  pGraphics->GetMouseLocation(x, y);
202  return pGraphics->OnKeyDown(x, y, key);
203  }
204  else
205  return false;
206 }
207 
209 {
210  IGraphics* pGraphics = GetUI();
211 
212  if (pGraphics)
213  {
214  float x, y;
215  pGraphics->GetMouseLocation(x, y);
216  return pGraphics->OnKeyUp(x, y, key);
217  }
218  else
219  return false;
220 }
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
bool GetWantsMidi() const
Definition: IControl.h:417
Encapsulates a MIDI message and provides helper functions.
Definition: IPlugMidi.h:30
bool OnKeyDown(const IKeyPress &key) override
KeyDown handler, in order to get keystrokes from certain hosts/plugin formats that send key press mes...
int UnserializeEditorState(const IByteChunk &chunk, int startPos) override
Unserializes editor state (such as scale).
virtual void SetValueFromDelegate(double value, int valIdx=0)
Set the control&#39;s value from the delegate This method is called from the class implementing the IEdit...
Definition: IControl.cpp:157
int Get(T *pDst, int startPos) const
Get arbitary typed data from the IByteChunk.
Definition: IPlugStructs.h:181
bool SerializeEditorState(IByteChunk &chunk) const override
Serializes the editor state (such as scale) into a binary chunk.
void CloseWindow() final
If you are not using IGraphics you can if you need to free resources etc when the window closes...
int GetParamIdx(int valIdx=0) const
Get the index of a parameter that the control is linked to Normaly controls are either linked to a si...
Definition: IControl.cpp:107
This file contains the base IControl implementation, along with some base classes for specific types ...
int Put(const T *pVal)
Copies arbitary typed data into the IByteChunk.
Definition: IPlugStructs.h:170
void SetScreenScale(float scale) final
Can be used by a host API to inform the editor of screen scale changes.
bool OnKeyDown(float x, float y, const IKeyPress &key)
Definition: IGraphics.cpp:1223
Manages a block of memory, for plug-in settings store/recall.
Definition: IPlugStructs.h:111
The lowest level base class of an IGraphics context.
An editor delegate base class for a SOMETHING that uses IGraphics for it&#39;s UI.
void * OpenWindow(void *pHandle) final
If you are not using IGraphics, you can implement this method to attach to the native parent view e...
bool OnKeyUp(float x, float y, const IKeyPress &key)
Definition: IGraphics.cpp:1251
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
void SendControlValueFromDelegate(int ctrlTag, double normalizedValue) override
SendControlValueFromDelegate (Abbreviation: SCVFD) WARNING: should not be called on the realtime audi...
virtual void OnMidi(const IMidiMsg &msg)
Implement to receive MIDI messages sent to the control if mWantsMidi == true, see IEditorDelegate:Sen...
Definition: IControl.h:160
This pure virtual interface delegates communication in both directions between a UI editor and someth...
void SendControlMsgFromDelegate(int ctrlTag, int msgTag, int dataSize=0, const void *pData=nullptr) override
SendControlMsgFromDelegate (Abbreviation: SCMFD) WARNING: should not be called on the realtime audio ...
int NVals() const
Definition: IControl.h:233
virtual void SendMidiMsgFromDelegate(const IMidiMsg &msg)
SendMidiMsgFromDelegate (Abbreviation: SMMFD) WARNING: should not be called on the realtime audio thr...
int UnserializeEditorSize(const IByteChunk &chunk, int startPos)
Unserializes the size and scale of the IGraphics.
virtual void CloseWindow()
If you are not using IGraphics you can if you need to free resources etc when the window closes...
virtual void SendParameterValueFromDelegate(int paramIdx, double value, bool normalized)
SendParameterValueFromDelegate (Abbreviation: SPVFD) WARNING: should not be called on the realtime au...
virtual void GetMouseLocation(float &x, float &y) const =0
Get the x, y position of the mouse cursor.
virtual void OnMsgFromDelegate(int msgTag, int dataSize, const void *pData)
Implement to receive messages sent to the control, see IEditorDelegate:SendControlMsgFromDelegate() ...
Definition: IControl.h:157
void SendParameterValueFromDelegate(int paramIdx, double value, bool normalized) override
SendParameterValueFromDelegate (Abbreviation: SPVFD) WARNING: should not be called on the realtime au...
bool SerializeEditorSize(IByteChunk &data) const
Serializes the size and scale of the IGraphics.
bool OnKeyUp(const IKeyPress &key) override
KeyDown handler, in order to get keystrokes from certain hosts/plugin formats that send key press mes...
void SendMidiMsgFromDelegate(const IMidiMsg &msg) override
SendMidiMsgFromDelegate (Abbreviation: SMMFD) WARNING: should not be called on the realtime audio thr...
Used for key press info, such as ASCII representation, virtual key (mapped to win32 codes) and modifi...
Definition: IPlugStructs.h:612