14#error This IControl only works with the Skia graphics backend
23#include "include/effects/SkRuntimeEffect.h"
26BEGIN_IGRAPHICS_NAMESPACE
47 mText.mAlign = EAlign::Near;
48 mText.mVAlign = EVAlign::Top;
69 SetShaderStr(shaderStr ? shaderStr :
73 uniform float2 uMouse;
74 uniform float2 uMouseBut;
76 half4 main(float2 fragCoord) {
77 float2 pos = uMouse.xy/uDim.xy;
78 return half4(pos.x, pos.y, 1, 1);
83 DBGMSG(
"%s\n", err.Get());
89 DrawShader(g, GetShaderBounds());
98 IRECT shaderBounds = GetShaderBounds();
100 mUniforms[kX] =
Clip(0.f, x - shaderBounds.L, shaderBounds.
W());
101 mUniforms[kY] =
Clip(0.f, y - shaderBounds.T, shaderBounds.
H());
102 mUniforms[kL] = (float) mod.L ? 1.f : 0.f;
103 mUniforms[kR] = (
float) mod.R ? 1.f : 0.f;
109 IRECT shaderBounds = GetShaderBounds();
111 mUniforms[kX] =
Clip(0.f, x - shaderBounds.L, shaderBounds.
W());
112 mUniforms[kY] =
Clip(0.f, y - shaderBounds.T, shaderBounds.
H());
120 IRECT shaderBounds = GetShaderBounds();
121 mUniforms[kX] =
Clip(0.f, x - shaderBounds.L, shaderBounds.
W());
122 mUniforms[kY] =
Clip(0.f, y - shaderBounds.T, shaderBounds.
H());
128 mUniforms[kWidth] = GetShaderBounds().
W();
129 mUniforms[kHeight] = GetShaderBounds().
H();
133 bool SetShaderStr(
const char* str, WDL_String& error)
135 mShaderStr = SkString(str);
137 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(mShaderStr);
141 error.Set(errorText.c_str());
147 auto inputs = SkData::MakeWithoutCopy(mUniforms.data(), mRTEffect->uniformSize());
148 auto shader = mRTEffect->makeShader(std::move(inputs),
nullptr, 0,
nullptr);
149 mPaint.setShader(std::move(shader));
156 virtual IRECT GetShaderBounds()
const
165 canvas->translate(r.L, r.T);
166 canvas->drawRect({ 0, 0, r.
W(), r.
H() }, mPaint);
173 sk_sp<SkRuntimeEffect> mRTEffect;
174 std::array<float, kNumUniforms> mUniforms {0.f};
177END_IGRAPHICS_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
The lowest level base class of an IGraphics context.
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
This control allows you to draw to the UI via a shader written using the Skia shading language,...
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 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.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void Draw(IGraphics &g) override
Draw the control to the graphics context.
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.