Created
October 31, 2020 13:54
-
-
Save olilarkin/50fce764d48df17fc07b3121843fbcb3 to your computer and use it in GitHub Desktop.
IPlug2 NvgText
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "IPlugEffect.h" | |
#include "IPlug_include_in_plug_src.h" | |
#include "IControls.h" | |
class NVGTextDemo : public IControl { | |
public: | |
float mMouseX, mMouseY; | |
NVGTextDemo(const IRECT& bounds) | |
: IControl(bounds, kNoParameter) {} | |
void Draw(IGraphics& g) override | |
{ | |
NVGcontext* ctx = (NVGcontext*) g.GetDrawContext(); | |
drawParagraph(ctx, mRECT.L, mRECT.T, mRECT.W(), mRECT.H(), mMouseX, mMouseY); | |
} | |
void OnMouseOver(float x, float y, const IMouseMod &mod) override | |
{ | |
mMouseX = x; | |
mMouseY = y; | |
SetDirty(false); | |
} | |
void drawParagraph(NVGcontext* vg, float x, float y, float width, float height, float mx, float my) | |
{ | |
NVGtextRow rows[3]; | |
NVGglyphPosition glyphs[100]; | |
const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.🎉"; | |
const char* start; | |
const char* end; | |
int nrows, i, nglyphs, j, lnum = 0; | |
float lineh; | |
float caretx, px; | |
float bounds[4]; | |
float a; | |
float gx,gy; | |
int gutter = 0; | |
NVG_NOTUSED(height); | |
nvgSave(vg); | |
nvgFontSize(vg, 18.0f); | |
nvgFontFace(vg, "Roboto-Regular"); | |
nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP); | |
nvgTextMetrics(vg, NULL, NULL, &lineh); | |
// The text break API can be used to fill a large buffer of rows, | |
// or to iterate over the text just few lines (or just one) at a time. | |
// The "next" variable of the last returned item tells where to continue. | |
start = text; | |
end = text + strlen(text); | |
while ((nrows = nvgTextBreakLines(vg, start, end, width, rows, 3))) { | |
for (i = 0; i < nrows; i++) { | |
NVGtextRow* row = &rows[i]; | |
int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh); | |
nvgBeginPath(vg); | |
nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:16)); | |
nvgRect(vg, x, y, row->width, lineh); | |
nvgFill(vg); | |
nvgFillColor(vg, nvgRGBA(255,255,255,255)); | |
nvgText(vg, x, y, row->start, row->end); | |
if (hit) { | |
caretx = (mx < x+row->width/2) ? x : x+row->width; | |
px = x; | |
nglyphs = nvgTextGlyphPositions(vg, x, y, row->start, row->end, glyphs, 100); | |
for (j = 0; j < nglyphs; j++) { | |
float x0 = glyphs[j].x; | |
float x1 = (j+1 < nglyphs) ? glyphs[j+1].x : x+row->width; | |
float gx = x0 * 0.3f + x1 * 0.7f; | |
if (mx >= px && mx < gx) | |
caretx = glyphs[j].x; | |
px = gx; | |
} | |
nvgBeginPath(vg); | |
nvgFillColor(vg, nvgRGBA(255,192,0,255)); | |
nvgRect(vg, caretx, y, 1, lineh); | |
nvgFill(vg); | |
gutter = lnum+1; | |
gx = x - 10; | |
gy = y + lineh/2; | |
} | |
lnum++; | |
y += lineh; | |
} | |
// Keep going... | |
start = rows[nrows-1].next; | |
} | |
if (gutter) { | |
char txt[16]; | |
snprintf(txt, sizeof(txt), "%d", gutter); | |
nvgFontSize(vg, 13.0f); | |
nvgTextAlign(vg, NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE); | |
nvgTextBounds(vg, gx,gy, txt, NULL, bounds); | |
nvgBeginPath(vg); | |
nvgFillColor(vg, nvgRGBA(255,192,0,255)); | |
nvgRoundedRect(vg, (int)bounds[0]-4,(int)bounds[1]-2, (int)(bounds[2]-bounds[0])+8, (int)(bounds[3]-bounds[1])+4, ((int)(bounds[3]-bounds[1])+4)/2-1); | |
nvgFill(vg); | |
nvgFillColor(vg, nvgRGBA(32,32,32,255)); | |
nvgText(vg, gx,gy, txt, NULL); | |
} | |
y += 20.0f; | |
nvgFontSize(vg, 13.0f); | |
nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP); | |
nvgTextLineHeight(vg, 1.2f); | |
nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds); | |
// Fade the tooltip out when close to it. | |
gx = fabsf((mx - (bounds[0]+bounds[2])*0.5f) / (bounds[0] - bounds[2])); | |
gy = fabsf((my - (bounds[1]+bounds[3])*0.5f) / (bounds[1] - bounds[3])); | |
a = std::fmaxf(gx, gy) - 0.5f; | |
a = Clip(a, 0.f, 1.f); | |
nvgGlobalAlpha(vg, a); | |
nvgBeginPath(vg); | |
nvgFillColor(vg, nvgRGBA(220,220,220,255)); | |
nvgRoundedRect(vg, bounds[0]-2,bounds[1]-2, (int)(bounds[2]-bounds[0])+4, (int)(bounds[3]-bounds[1])+4, 3); | |
px = (int)((bounds[2]+bounds[0])/2); | |
nvgMoveTo(vg, px,bounds[1] - 10); | |
nvgLineTo(vg, px+7,bounds[1]+1); | |
nvgLineTo(vg, px-7,bounds[1]+1); | |
nvgFill(vg); | |
nvgFillColor(vg, nvgRGBA(0,0,0,220)); | |
nvgTextBox(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL); | |
nvgRestore(vg); | |
} | |
}; | |
IPlugEffect::IPlugEffect(const InstanceInfo& info) | |
: Plugin(info, MakeConfig(kNumParams, kNumPresets)) | |
{ | |
GetParam(kGain)->InitDouble("Gain", 0., 0., 100.0, 0.01, "%"); | |
mMakeGraphicsFunc = [&]() { | |
return MakeGraphics(*this, PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS, GetScaleForScreen(PLUG_HEIGHT)); | |
}; | |
mLayoutFunc = [&](IGraphics* pGraphics) { | |
pGraphics->AttachCornerResizer(EUIResizerMode::Scale, false); | |
pGraphics->AttachPanelBackground(COLOR_GRAY); | |
pGraphics->LoadFont("Roboto-Regular", ROBOTO_FN); | |
pGraphics->EnableMouseOver(true); | |
const IRECT b = pGraphics->GetBounds(); | |
pGraphics->AttachControl(new NVGTextDemo(b.GetCentredInside(300, 300))); | |
}; | |
} | |
#if IPLUG_DSP | |
void IPlugEffect::ProcessBlock(sample** inputs, sample** outputs, int nFrames) | |
{ | |
const double gain = GetParam(kGain)->Value() / 100.; | |
const int nChans = NOutChansConnected(); | |
for (int s = 0; s < nFrames; s++) { | |
for (int c = 0; c < nChans; c++) { | |
outputs[c][s] = inputs[c][s] * gain; | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment