iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
TestKeyboardControl.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
20const char* vk_to_string(int vk_code)
21{
22 switch(vk_code)
23 {
24 case kVK_HELP: return "VK_HELP";
25 case kVK_BACK: return "VK_BACK";
26 case kVK_TAB: return "VK_TAB";
27 case kVK_CLEAR: return "VK_CLEAR";
28 case kVK_RETURN: return "VK_RETURN";
29 case kVK_SHIFT: return "VK_SHIFT";
30 case kVK_CONTROL: return "VK_CONTROL";
31 case kVK_MENU: return "VK_MENU";
32 case kVK_PAUSE: return "VK_PAUSE";
33 case kVK_CAPITAL: return "VK_CAPITAL";
34 case kVK_ESCAPE: return "VK_ESCAPE";
35 case kVK_SPACE: return "VK_SPACE";
36 case kVK_PRIOR: return "VK_PAGE_UP";
37 case kVK_NEXT: return "VK_PAGE_DOWN";
38 case kVK_END: return "VK_END";
39 case kVK_HOME: return "VK_HOME";
40 case kVK_LEFT: return "VK_LEFT";
41 case kVK_UP: return "VK_UP";
42 case kVK_RIGHT: return "VK_RIGHT";
43 case kVK_DOWN: return "VK_DOWN";
44 case kVK_SELECT: return "VK_SELECT";
45 case kVK_PRINT: return "VK_PRINT";
46 case kVK_INSERT: return "VK_INSERT";
47 case kVK_DELETE: return "VK_DELETE";
48 case kVK_NUMPAD0: return "VK_NUMPAD0";
49 case kVK_NUMPAD1: return "VK_NUMPAD1";
50 case kVK_NUMPAD2: return "VK_NUMPAD2";
51 case kVK_NUMPAD3: return "VK_NUMPAD3";
52 case kVK_NUMPAD4: return "VK_NUMPAD4";
53 case kVK_NUMPAD5: return "VK_NUMPAD5";
54 case kVK_NUMPAD6: return "VK_NUMPAD6";
55 case kVK_NUMPAD7: return "VK_NUMPAD7";
56 case kVK_NUMPAD8: return "VK_NUMPAD8";
57 case kVK_NUMPAD9: return "VK_NUMPAD9";
58 case kVK_MULTIPLY: return "VK_MULTIPLY";
59 case kVK_ADD: return "VK_ADD";
60 case kVK_SEPARATOR: return "VK_SEPARATOR";
61 case kVK_SUBTRACT: return "VK_SUBTRACT";
62 case kVK_DECIMAL: return "VK_DECIMAL";
63 case kVK_DIVIDE: return "VK_DIVIDE";
64 case kVK_F1: return "VK_F1";
65 case kVK_F2: return "VK_F2";
66 case kVK_F3: return "VK_F3";
67 case kVK_F4: return "VK_F4";
68 case kVK_F5: return "VK_F5";
69 case kVK_F6: return "VK_F6";
70 case kVK_F7: return "VK_F7";
71 case kVK_F8: return "VK_F8";
72 case kVK_F9: return "VK_F9";
73 case kVK_F10: return "VK_F10";
74 case kVK_F11: return "VK_F11";
75 case kVK_F12: return "VK_F12";
76 case kVK_F13: return "VK_F13";
77 case kVK_F14: return "VK_F14";
78 case kVK_F15: return "VK_F15";
79 case kVK_F16: return "VK_F16";
80 case kVK_F17: return "VK_F17";
81 case kVK_F18: return "VK_F18";
82 case kVK_F19: return "VK_F19";
83 case kVK_F20: return "VK_F20";
84 case kVK_F21: return "VK_F21";
85 case kVK_F22: return "VK_F22";
86 case kVK_F23: return "VK_F23";
87 case kVK_F24: return "VK_F24";
88 case kVK_NUMLOCK: return "VK_NUMLOCK";
89 case kVK_SCROLL: return "VK_SCROLL";
90 case kVK_RETURN|0x8000: return "ENTER";
91 default: return "Unknown VK code";
92 }
93}
94
98{
99public:
100 TestKeyboardControl(const IRECT& rect)
101 : IControl(rect)
102 {
103 mX = rect.MW();
104 mY = rect.MH();
105 mStr.Set("Press a key...");
106 SetTooltip("TestKeyboardControl");
107 }
108
109 void Draw(IGraphics& g) override
110 {
111 g.FillRect(COLOR_BLACK, mRECT);
112
113 if (g.CheckLayer(mLayer))
114 {
115 g.ResumeLayer(mLayer);
116
117 if(mNewText)
118 {
119 g.DrawText(IText(static_cast<float>((rand() % 30) + 10), COLOR_WHITE), mStr.Get(), mX, mY);
120 mNewText = false;
121 }
122
124 {
125 g.FillRect(COLOR_BLACK, mRECT, &BLEND_05);
126 }
127 }
128 else
129 {
130 g.StartLayer(this, mRECT);
131 g.DrawText(IText(20, COLOR_WHITE), mStr.Get(), mX, mY);
132 }
133
134 mLayer = g.EndLayer();
135
136 g.DrawLayer(mLayer);
137 }
138
139 void OnMouseDown(float x, float y, const IMouseMod& mod) override
140 {
141 mStr.SetFormatted(64, "MouseDown: L:%i, R:%i, A:%i, C:%i. S:%i", mod.L, mod.R, mod.A, mod.C, mod.S);
142 StrUpdated(x, y);
143 }
144
145 void OnMouseUp(float x, float y, const IMouseMod& mod) override
146 {
147 mStr.SetFormatted(64, "MouseUp: L:%i, R:%i, A:%i, C:%i. S:%i", mod.L, mod.R, mod.A, mod.C, mod.S);
148 StrUpdated(x, y);
149 }
150
151 bool OnKeyDown(float x, float y, const IKeyPress& key) override
152 {
153 mStr.Set(vk_to_string(key.VK));
154
155 if(strcmp(mStr.Get(),"Unknown VK code")==0)
156 {
157 mStr.Set(key.utf8);
158 }
159
160 StrUpdated(x, y);
161
162 return true;
163 }
164
165private:
166 void StrUpdated(float x, float y)
167 {
168 mNewText = true;
170 StartAnimation(5000);
171 mX = x;
172 mY = y;
173
174 SetDirty(false);
175 }
176
177 bool mNewText = false;
178 float mX = 0.;
179 float mY = 0.;
180 WDL_String mStr;
181 ILayerPtr mLayer;
182};
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
void StartAnimation(int duration)
Definition: IControl.cpp:425
IControl * SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:216
IAnimationFunction GetAnimationFunction()
Get the control's animation function, if it exists.
Definition: IControl.h:500
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
void SetAnimation(IAnimationFunction func)
Set the animation function.
Definition: IControl.h:492
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
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:670
void ResumeLayer(ILayerPtr &layer)
If a layer already exists, continue drawing to it.
Definition: IGraphics.cpp:1987
bool CheckLayer(const ILayerPtr &layer)
Test to see if a layer needs drawing, for instance if the control's bounds were changed.
Definition: IGraphics.cpp:2032
void DrawLayer(const ILayerPtr &layer, const IBlend *pBlend=nullptr)
Draw a layer to the main IGraphics context.
Definition: IGraphics.cpp:2045
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2569
void StartLayer(IControl *pOwner, const IRECT &r, bool cacheable=false)
Create an IGraphics layer.
Definition: IGraphics.cpp:1977
ILayerPtr EndLayer()
End an IGraphics layer.
Definition: IGraphics.cpp:2000
Control to test keyboard input.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnMouseUp(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse up event on this control.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
bool OnKeyDown(float x, float y, const IKeyPress &key) override
Implement this method to respond to a key down event on this control.
BEGIN_IPLUG_NAMESPACE BEGIN_IGRAPHICS_NAMESPACE void DefaultAnimationFunc(IControl *pCaller)
An animation function that just calls the caller control's OnEndAnimation() method at the end of the ...
Definition: IControl.cpp:21
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
Used for key press info, such as ASCII representation, virtual key (mapped to win32 codes) and modifi...
Definition: IPlugStructs.h:616
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
float MH() const
float MW() const
IText is used to manage font and text/text entry style for a piece of text on the UI,...