iPlug2 - C++ Audio Plug-in Framework
TestMultiTouchControl.h
Go to the documentation of this file.
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 
11 #pragma once
12 
18 #include "IControl.h"
19 #include <map>
20 
23 class TestMTControl : public IControl
24  , public IMultiTouchControlBase
25 {
26 public:
27  TestMTControl(IRECT bounds)
28  : IControl(bounds)
29  {
30  SetWantsMultiTouch(true);
31  }
32 
33  void Draw(IGraphics& g) override
34  {
35  std::vector<ITouchID> touches;
36  g.GetTouches(this, touches);
37  WDL_String str;
38  str.SetFormatted(32, "NUM TOUCHES: %i", static_cast<int>(touches.size()));
39  g.DrawText(IText(20), str.Get(), mRECT);
40 
41  g.DrawRect(COLOR_BLACK, mRECT);
42 
43  if (g.CheckLayer(mLayer))
44  {
45  g.ResumeLayer(mLayer);
46 
47  WDL_String str;
48 
49  for (auto& touchPair : mTrackedTouches)
50  {
51  TrackedTouch* pTouch = &touchPair.second;
52  int t = pTouch->index;
53  float dim = pTouch->radius > 0.f ? pTouch->radius : 50.f;
54  IRECT r {pTouch->x-dim,pTouch->y-dim,pTouch->x+dim, pTouch->y+dim};
55  g.FillEllipse(GetRainbow(t), r);
56  g.DrawEllipse(COLOR_BLACK, r);
57  Milliseconds duration = std::chrono::high_resolution_clock::now() - pTouch->startTime;
58  str.SetFormatted(32, "%i: %i", t, static_cast<int>(duration.count()));
59  g.DrawText(IText(20.f), str.Get(), r);
60  }
61  }
62  else
63  {
64  g.StartLayer(this, mRECT);
65  }
66 
67  mLayer = g.EndLayer();
68  g.DrawLayer(mLayer);
69  }
70 
71  void OnMouseDown(float x, float y, const IMouseMod& mod) override
72  {
73  AddTouch(mod.touchID, x, y, mod.touchRadius);
74  }
75 
76  void OnMouseUp(float x, float y, const IMouseMod& mod) override
77  {
78  ReleaseTouch(mod.touchID);
79 
80  if(NTrackedTouches() == 0)
81  {
82  mLayer->Invalidate();
83  }
84 
85  SetDirty(true);
86  }
87 
88  void OnTouchCancelled(float x, float y, const IMouseMod& mod) override
89  {
90  OnMouseUp(x, y, mod);
91  }
92 
93  void OnMouseDrag(float x, float y, float dx, float dy, const IMouseMod& mod) override
94  {
95  UpdateTouch(mod.touchID, x, y, mod.touchRadius);
96  SetDirty(true);
97  }
98 
99  bool IsDirty() override
100  {
101  if(NTrackedTouches())
102  return true;
103  else
104  return IControl::IsDirty();
105  }
106 
107 public:
108  ILayerPtr mLayer;
109 };
void OnMouseDrag(float x, float y, float dx, float dy, const IMouseMod &mod) override
Implement this method to respond to a mouse drag event on this control.
void SetWantsMultiTouch(bool enable=true)
Specify whether this control supports multiple touches.
Definition: IControl.h:420
void StartLayer(IControl *pOwner, const IRECT &r, bool cacheable=false)
Create an IGraphics layer.
Definition: IGraphics.cpp:1954
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
virtual void DrawRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw a rectangle to the graphics context.
Definition: IGraphics.cpp:2475
void ResumeLayer(ILayerPtr &layer)
If a layer already exists, continue drawing to it.
Definition: IGraphics.cpp:1964
Used to manage mouse modifiers i.e.
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:631
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
This file contains the base IControl implementation, along with some base classes for specific types ...
void GetTouches(IControl *pControl, std::vector< ITouchID > &touchesOnThisControl) const
Populate a vector with the touchIDs active on pControl.
Definition: IGraphics.h:1355
IText is used to manage font and text/text entry style for a piece of text on the UI...
bool CheckLayer(const ILayerPtr &layer)
Test to see if a layer needs drawing, for instance if the control&#39;s bounds were changed.
Definition: IGraphics.cpp:2009
virtual bool IsDirty()
Called at each display refresh by the IGraphics draw loop, after IControl::Animate(), to determine if the control is marked as dirty.
Definition: IControl.cpp:229
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
void OnTouchCancelled(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a touch cancel event on this control.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
bool IsDirty() override
Called at each display refresh by the IGraphics draw loop, after IControl::Animate(), to determine if the control is marked as dirty.
virtual void FillEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2591
virtual void DrawEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2526
Control to test multi touch.
void DrawLayer(const ILayerPtr &layer, const IBlend *pBlend=nullptr)
Draw a layer to the main IGraphics context.
Definition: IGraphics.cpp:2022
A base class for controls that can do do multitouch.
Definition: IControl.h:1099
void OnMouseUp(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse up event on this control.
ILayerPtr EndLayer()
End an IGraphics layer.
Definition: IGraphics.cpp:1977
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:196