iPlug2 - C++ Audio Plug-in Framework
TestSVGControl.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 "nanosvg.h"
20 
23 class TestSVGControl : public IControl
24 {
25 public:
26  TestSVGControl(const IRECT& bounds, const ISVG& svg)
27  : IControl(bounds)
28  , mSVG(svg)
29  {
30  SetTooltip("TestSVGControl - Click or Drag 'n drop here to load a new SVG.");
31  }
32 
33  void Draw(IGraphics& g) override
34  {
35  g.DrawDottedRect(COLOR_BLACK, mRECT);
36  g.FillRect(mMouseIsOver ? COLOR_TRANSLUCENT : COLOR_TRANSPARENT, mRECT);
37 
38 #if 1
39  if (!g.CheckLayer(mLayer))
40  {
41  g.StartLayer(this, mRECT);
42  g.DrawSVG(mSVG, mRECT);
43  mLayer = g.EndLayer();
44  }
45 
46  g.DrawLayer(mLayer);
47 #else
48  g.DrawSVG(mSVG, mRECT);
49 #endif
50  }
51 
52  void OnMouseDown(float x, float y, const IMouseMod& mod) override
53  {
54  WDL_String file;
55  WDL_String path;
56 
57  GetUI()->PromptForFile(file, path, EFileAction::Open, "svg");
58 
59  if(file.GetLength())
60  SetSVG(GetUI()->LoadSVG(file.Get()));
61 
62  SetDirty(false);
63  }
64 
65  void OnDrop(const char* str) override
66  {
67  SetSVG(GetUI()->LoadSVG(str));
68  SetDirty(false);
69  }
70 
71  void SetSVG(const ISVG& svg)
72  {
73  mSVG = svg;
74  mLayer->Invalidate();
75  }
76 
77 private:
78  ILayerPtr mLayer;
79  ISVG mSVG;
80 };
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 DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f, float dashLen=2.f)
Draw a dotted rectangle to the graphics context.
Definition: IGraphics.cpp:2517
Used to manage mouse modifiers i.e.
bool mMouseIsOver
if mGraphics::mHandleMouseOver = true, this will be true when the mouse is over control.
Definition: IControl.h:545
void SetTooltip(const char *str)
Set a tooltip for the control.
Definition: IControl.h:210
User-facing SVG abstraction that you use to manage SVG data ISVG doesn't actually own the image data...
This file contains the base IControl implementation, along with some base classes for specific types ...
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
Control to test drawing SVGs.
virtual void PromptForFile(WDL_String &fileName, WDL_String &path, EFileAction action=EFileAction::Open, const char *extensions=0)=0
Create a platform file prompt dialog to choose a file/directory path for opening/saving a file/direct...
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:2009
virtual void DrawSVG(const ISVG &svg, const IRECT &bounds, const IBlend *pBlend=0)
Draw an SVG image to the graphics context.
Definition: IGraphics.cpp:2762
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
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:2547
void DrawLayer(const ILayerPtr &layer, const IBlend *pBlend=nullptr)
Draw a layer to the main IGraphics context.
Definition: IGraphics.cpp:2022
virtual ISVG LoadSVG(const char *fileNameOrResID, const char *units="px", float dpi=72.f)
Load an SVG from disk or from windows resource.
Definition: IGraphics.cpp:1593
IGraphics * GetUI()
Definition: IControl.h:452
void Draw(IGraphics &g) override
Draw the control to the graphics context.
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.
void OnDrop(const char *str) override
Implement to do something when something was drag &#39;n dropped onto this control.
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:196