ivec2 Button::preferredSize(NVGcontext * ctx) const { int fontSize = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize; nvgFontSize(ctx, fontSize); nvgFontFace(ctx, "sans-bold"); float tw = nvgTextBounds(ctx, 0, 0, mCaption.c_str(), nullptr, nullptr); float iw = 0.0f, ih = fontSize; if (mIcon) { if (nvgIsFontIcon(mIcon)) { ih *= 1.5f; nvgFontFace(ctx, "icons"); nvgFontSize(ctx, ih); iw = nvgTextBounds(ctx, 0, 0, utf8(mIcon).data(), nullptr, nullptr) + mSize.y * 0.15f; } else { int w, h; ih *= 0.9f; nvgImageSize(ctx, mIcon, &w, &h); iw = w * ih / h; } } return ivec2((int)(tw + iw) + 20, fontSize + 10); }
ImageGlyph::ImageGlyph(const std::string& file, AlloyContext* context, bool mipmap) : Glyph(GetFileNameWithoutExtension(file), GlyphType::Image, 0, 0), file( file) { handle = nvgCreateImage(context->nvgContext, file.c_str(), (mipmap) ? NVG_IMAGE_GENERATE_MIPMAPS : 0); int w, h; nvgImageSize(context->nvgContext, handle, &w, &h); width = (pixel) w; height = (pixel) h; }
void NanoImage::_updateSize() { int w=0, h=0; nvgImageSize(fHandle.context, fHandle.imageId, &w, &h); if (w < 0) w = 0; if (h < 0) h = 0; fSize.setSize(static_cast<uint>(w), static_cast<uint>(h)); }
//void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h); JNIEXPORT void JNICALL Java_firststep_internal_NVG_imageSize (JNIEnv *e, jclass c, jlong ctx, jint image, jintArray dims) { int w, h; nvgImageSize((NVGcontext*)ctx, image, &w, &h); jint *dimsArray = (*e)->GetIntArrayElements(e, dims, 0); dimsArray[0] = w; dimsArray[1] = h; (*e)->ReleaseIntArrayElements(e, dims, dimsArray, 0); }
Image& NanoInk::fetchImage(Image& image, bool tile) { if(image.d_index == 0) { image.d_index = nvgCreateImage(mCtx, (mLayer.target().window().resourcePath() + "interface/uisprites/" + image.d_name + ".png").c_str(), tile ? (NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY) : 0); nvgImageSize(mCtx, image.d_index, &image.d_width, &image.d_height); #ifdef NANO_ATLAS ImageRect& rect = mLayer.target().window().atlas().findSpriteRect(image.d_name + ".png"); image.d_left = rect.x; image.d_top = rect.y; #endif } return image; }
void other_begin(BATB& batb) { auto* nvg = batb.gl.nvg_context; //glClearColor( 0, 0, 0.3, 0 ); auto image_path = file::static_data( "batb/openforest-512x256.png" ); image = nvgCreateImage( nvg, image_path.c_str(), 0 ); int w, h; nvgImageSize( nvg, image, &w, &h ); image_wth = w; image_hth = h; }
void ImagePanel::draw (NVGcontext * ctx) { ivec2 grid = gridSize(); for (size_t i = 0; i < mImages.size(); ++i) { ivec2 p = mPos + ivec2 (mMargin) + ivec2 ((int)i % grid.x, (int)i / grid.x) * (mThumbSize + mSpacing); int imgw, imgh; nvgImageSize (ctx, mImages[i].first, &imgw, &imgh); float iw, ih, ix, iy; if (imgw < imgh) { iw = mThumbSize; ih = iw * (float)imgh / (float)imgw; ix = 0; iy = - (ih - mThumbSize) * 0.5f; } else { ih = mThumbSize; iw = ih * (float)imgw / (float)imgh; ix = - (iw - mThumbSize) * 0.5f; iy = 0; } NVGpaint imgPaint = nvgImagePattern ( ctx, p.x + ix, p.y + iy, iw, ih, 0, mImages[i].first, mMouseIndex == (int)i ? 1.0 : 0.7); nvgBeginPath (ctx); nvgRoundedRect (ctx, p.x, p.y, mThumbSize, mThumbSize, 5); nvgFillPaint (ctx, imgPaint); nvgFill (ctx); NVGpaint shadowPaint = nvgBoxGradient (ctx, p.x - 1, p.y, mThumbSize + 2, mThumbSize + 2, 5, 3, nvgRGBA (0, 0, 0, 128), nvgRGBA (0, 0, 0, 0)); nvgBeginPath (ctx); nvgRect (ctx, p.x - 5, p.y - 5, mThumbSize + 10, mThumbSize + 10); nvgRoundedRect (ctx, p.x, p.y, mThumbSize, mThumbSize, 6); nvgPathWinding (ctx, NVG_HOLE); nvgFillPaint (ctx, shadowPaint); nvgFill (ctx); nvgBeginPath (ctx); nvgRoundedRect (ctx, p.x + 0.5f, p.y + 0.5f, mThumbSize - 1, mThumbSize - 1, 4 - 0.5f); nvgStrokeWidth (ctx, 1.0f); nvgStrokeColor (ctx, nvgRGBA (255, 255, 255, 80)); nvgStroke (ctx); } }
Vector2i TextBox::preferredSize(NVGcontext *ctx) const { Vector2i size(0, fontSize() * 1.4f); float uw = 0; if (mUnitsImage > 0) { int w, h; nvgImageSize(ctx, mUnitsImage, &w, &h); float uh = size(1) * 0.4f; uw = w * uh / h; } else if (!mUnits.empty()) { uw = nvgTextBounds(ctx, 0, 0, mUnits.c_str(), nullptr, nullptr); } float ts = nvgTextBounds(ctx, 0, 0, mValue.c_str(), nullptr, nullptr); size(0) = size(1) + ts + uw; return size; }
bool t2Image::loadImage(string path) { img = nvgCreateImage(t2GetContext(), path.c_str(), 0); if(!img) { bLoaded = false; t2PrintError("图片读取失败,可能是路径有误"); return false; } else { bLoaded = true; nvgImageSize(t2GetContext(), img, &width, &height); return true; } }
void renderPattern(NVGcontext* vg, NVGLUframebuffer* fb, float t, float pxRatio) { int winWidth, winHeight; int fboWidth, fboHeight; int pw, ph, x, y; float s = 20.0f; float sr = (cosf(t)+1)*0.5f; float r = s * 0.6f * (0.2f + 0.8f * sr); if (fb == NULL) return; nvgImageSize(vg, fb->image, &fboWidth, &fboHeight); winWidth = (int)(fboWidth / pxRatio); winHeight = (int)(fboHeight / pxRatio); // Draw some stuff to an FBO as a test nvgluBindFramebuffer(fb); glViewport(0, 0, fboWidth, fboHeight); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); pw = (int)ceilf(winWidth / s); ph = (int)ceilf(winHeight / s); nvgBeginPath(vg); for (y = 0; y < ph; y++) { for (x = 0; x < pw; x++) { float cx = (x+0.5f) * s; float cy = (y+0.5f) * s; nvgCircle(vg, cx,cy, r); } } nvgFillColor(vg, nvgRGBA(220,160,0,200)); nvgFill(vg); nvgEndFrame(vg); nvgluBindFramebuffer(NULL); }
void Button::draw(NVGcontext *ctx) { Widget::draw(ctx); NVGcolor gradTop = mTheme->mButtonGradientTopUnfocused; NVGcolor gradBot = mTheme->mButtonGradientBotUnfocused; if (mPushed) { gradTop = mTheme->mButtonGradientTopPushed; gradBot = mTheme->mButtonGradientBotPushed; } else if (mMouseFocus && mEnabled) { gradTop = mTheme->mButtonGradientTopFocused; gradBot = mTheme->mButtonGradientBotFocused; } nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x() + 1, mPos.y() + 1.0f, mSize.x() - 2, mSize.y() - 2, mTheme->mButtonCornerRadius - 1); if(mBackgroundColor.w() != 0) { nvgFillColor(ctx, mBackgroundColor); nvgFill(ctx); if(mPushed) { gradTop.a = gradBot.a = 0.8f; } else { double v = 1-mBackgroundColor.w(); gradTop.a = gradBot.a = mEnabled ? v : 0.5f; gradTop.a = gradBot.a = (mMouseFocus && mEnabled) ? gradTop.a+0.2 : gradTop.a; } } NVGpaint bg = nvgLinearGradient(ctx, mPos.x(), mPos.y(), mPos.x(), mPos.y() + mSize.y(), gradTop, gradBot); nvgFillPaint(ctx, bg); nvgFill(ctx); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x() + 0.5f, mPos.y() + (mPushed ? 0.5f : 1.5f), mSize.x() - 1, mSize.y() - 1 - (mPushed ? 0.0f : 1.0f), mTheme->mButtonCornerRadius); nvgStrokeColor(ctx, mTheme->mBorderLight); nvgStroke(ctx); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x() + 0.5f, mPos.y() + 0.5f, mSize.x() - 1, mSize.y() - 2, mTheme->mButtonCornerRadius); nvgStrokeColor(ctx, mTheme->mBorderDark); nvgStroke(ctx); nvgFontSize(ctx, mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize); nvgFontFace(ctx, "sans-bold"); float tw = nvgTextBounds(ctx, 0,0, mCaption.c_str(), nullptr, nullptr); Vector2f center = mPos.cast<float>() + mSize.cast<float>() * 0.5f; Vector2f textPos(center.x() - tw * 0.5f, center.y() - 1); NVGcolor textColor = mTextColor.w() == 0 ? mTheme->mTextColor : mTextColor; if (!mEnabled) textColor = mTheme->mDisabledTextColor; if (mIcon) { auto icon = utf8(mIcon); float iw, ih = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize; if (nvgIsFontIcon(mIcon)) { ih *= 1.5f; nvgFontSize(ctx, ih); nvgFontFace(ctx, "icons"); iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr); } else { int w, h; ih *= 0.9f; nvgImageSize(ctx, mIcon, &w, &h); iw = w * ih / h; } if (mCaption != "") iw += mSize.y() * 0.15f; nvgFillColor(ctx, textColor); nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); Vector2f iconPos = center; iconPos.y() -= 1; if (mIconPosition == LeftCentered) { iconPos.x() -= (tw + iw) * 0.5f; textPos.x() += iw * 0.5f; } else if (mIconPosition == RightCentered) { textPos.x() -= iw * 0.5f; iconPos.x() += tw * 0.5f; } else if (mIconPosition == Left) { iconPos.x() = mPos.x() + 8; } else if (mIconPosition == Right) { iconPos.x() = mPos.x() + mSize.x() - iw - 8; } if (nvgIsFontIcon(mIcon)) { nvgText(ctx, iconPos.x(), iconPos.y()+1, icon.data(), nullptr); } else { NVGpaint imgPaint = nvgImagePattern(ctx, iconPos.x(), iconPos.y() - ih/2, iw, ih, 0, mIcon, mEnabled ? 0.5f : 0.25f); nvgFillPaint(ctx, imgPaint); nvgFill(ctx); } } nvgFontSize(ctx, mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize); nvgFontFace(ctx, "sans-bold"); nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); nvgFillColor(ctx, mTheme->mTextColorShadow); nvgText(ctx, textPos.x(), textPos.y(), mCaption.c_str(), nullptr); nvgFillColor(ctx, textColor); nvgText(ctx, textPos.x(), textPos.y()+1, mCaption.c_str(), nullptr); }
void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t) { float cornerRadius = 3.0f; struct NVGpaint shadowPaint, imgPaint, fadePaint; float ix,iy,iw,ih; float thumb = 60.0f; float arry = 30.5f; int imgw, imgh; float stackh = (nimages/2) * (thumb+10) + 10; int i; float u = (1+cosf(t*0.5f) )*0.5f; float scrollh; nvgSave(vg); // nvgClearState(vg); // Drop shadow shadowPaint = nvgBoxGradient(vg, x,y+4, w,h, cornerRadius*2, 20, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) ); nvgBeginPath(vg); nvgRect(vg, x-10,y-10, w+20,h+30); nvgRoundedRect(vg, x,y, w,h, cornerRadius); nvgPathWinding(vg, NVG_HOLE); nvgFillPaint(vg, shadowPaint); nvgFill(vg); // Window nvgBeginPath(vg); nvgRoundedRect(vg, x,y, w,h, cornerRadius); nvgMoveTo(vg, x-10,y+arry); nvgLineTo(vg, x+1,y+arry-11); nvgLineTo(vg, x+1,y+arry+11); nvgFillColor(vg, nvgRGBA(200,200,200,255) ); nvgFill(vg); nvgSave(vg); nvgScissor(vg, x,y,w,h); nvgTranslate(vg, 0, -(stackh - h)*u); for (i = 0; i < nimages; i++) { float tx, ty; tx = x+10; ty = y+10; tx += (i%2) * (thumb+10); ty += (i/2) * (thumb+10); nvgImageSize(vg, images[i], &imgw, &imgh); if (imgw < imgh) { iw = thumb; ih = iw * (float)imgh/(float)imgw; ix = 0; iy = -(ih-thumb)*0.5f; } else { ih = thumb; iw = ih * (float)imgw/(float)imgh; ix = -(iw-thumb)*0.5f; iy = 0; } imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], 0); nvgBeginPath(vg); nvgRoundedRect(vg, tx,ty, thumb,thumb, 5); nvgFillPaint(vg, imgPaint); nvgFill(vg); shadowPaint = nvgBoxGradient(vg, tx-1,ty, thumb+2,thumb+2, 5, 3, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) ); nvgBeginPath(vg); nvgRect(vg, tx-5,ty-5, thumb+10,thumb+10); nvgRoundedRect(vg, tx,ty, thumb,thumb, 6); nvgPathWinding(vg, NVG_HOLE); nvgFillPaint(vg, shadowPaint); nvgFill(vg); nvgBeginPath(vg); nvgRoundedRect(vg, tx+0.5f,ty+0.5f, thumb-1,thumb-1, 4-0.5f); nvgStrokeWidth(vg,1.0f); nvgStrokeColor(vg, nvgRGBA(255,255,255,192) ); nvgStroke(vg); } nvgRestore(vg); // Hide fades fadePaint = nvgLinearGradient(vg, x,y,x,y+6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0) ); nvgBeginPath(vg); nvgRect(vg, x+4,y,w-8,6); nvgFillPaint(vg, fadePaint); nvgFill(vg); fadePaint = nvgLinearGradient(vg, x,y+h,x,y+h-6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0) ); nvgBeginPath(vg); nvgRect(vg, x+4,y+h-6,w-8,6); nvgFillPaint(vg, fadePaint); nvgFill(vg); // Scroll bar shadowPaint = nvgBoxGradient(vg, x+w-12+1,y+4+1, 8,h-8, 3,4, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,92) ); nvgBeginPath(vg); nvgRoundedRect(vg, x+w-12,y+4, 8,h-8, 3); nvgFillPaint(vg, shadowPaint); // nvgFillColor(vg, nvgRGBA(255,0,0,128) ); nvgFill(vg); scrollh = (h/stackh) * (h-8); shadowPaint = nvgBoxGradient(vg, x+w-12-1,y+4+(h-8-scrollh)*u-1, 8,scrollh, 3,4, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255) ); nvgBeginPath(vg); nvgRoundedRect(vg, x+w-12+1,y+4+1 + (h-8-scrollh)*u, 8-2,scrollh-2, 2); nvgFillPaint(vg, shadowPaint); // nvgFillColor(vg, nvgRGBA(0,0,0,128) ); nvgFill(vg); nvgRestore(vg); }
void TextBox::draw(NVGcontext* ctx) { Widget::draw(ctx); NVGpaint bg = nvgBoxGradient(ctx, mPos.x() + 1, mPos.y() + 1 + 1.0f, mSize.x() - 2, mSize.y() - 2, 3, 4, Color(255, 32), Color(32, 32)); NVGpaint fg1 = nvgBoxGradient(ctx, mPos.x() + 1, mPos.y() + 1 + 1.0f, mSize.x() - 2, mSize.y() - 2, 3, 4, Color(150, 32), Color(32, 32)); NVGpaint fg2 = nvgBoxGradient(ctx, mPos.x() + 1, mPos.y() + 1 + 1.0f, mSize.x() - 2, mSize.y() - 2, 3, 4, nvgRGBA(255, 0, 0, 100), nvgRGBA(255, 0, 0, 50)); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x() + 1, mPos.y() + 1 + 1.0f, mSize.x() - 2, mSize.y() - 2, 3); if(mEditable && focused()) mValidFormat ? nvgFillPaint(ctx, fg1) : nvgFillPaint(ctx, fg2); else nvgFillPaint(ctx, bg); nvgFill(ctx); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x() + 0.5f, mPos.y() + 0.5f, mSize.x() - 1, mSize.y() - 1, 2.5f); nvgStrokeColor(ctx, Color(0, 48)); nvgStroke(ctx); nvgFontSize(ctx, fontSize()); nvgFontFace(ctx, "sans"); Vector2i drawPos(mPos.x(), mPos.y() + mSize.y() * 0.5f + 1); float xSpacing = mSize.y() * 0.3f; float unitWidth = 0; if (mUnitsImage > 0) { int w, h; nvgImageSize(ctx, mUnitsImage, &w, &h); float unitHeight = mSize.y() * 0.4f; unitWidth = w * unitHeight / h; NVGpaint imgPaint = nvgImagePattern( ctx, mPos.x() + mSize.x() - xSpacing - unitWidth, drawPos.y() - unitHeight * 0.5f, unitWidth, unitHeight, 0, mUnitsImage, mEnabled ? 0.7f : 0.35f); nvgBeginPath(ctx); nvgRect(ctx, mPos.x() + mSize.x() - xSpacing - unitWidth, drawPos.y() - unitHeight * 0.5f, unitWidth, unitHeight); nvgFillPaint(ctx, imgPaint); nvgFill(ctx); unitWidth += 2; } else if (!mUnits.empty()) { unitWidth = nvgTextBounds(ctx, 0, 0, mUnits.c_str(), nullptr, nullptr); nvgFillColor(ctx, Color(255, mEnabled ? 64 : 32)); nvgTextAlign(ctx, NVG_ALIGN_RIGHT | NVG_ALIGN_MIDDLE); nvgText(ctx, mPos.x() + mSize.x() - xSpacing, drawPos.y(), mUnits.c_str(), nullptr); unitWidth += 2; } switch (mAlignment) { case Alignment::Left: nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); drawPos.x() += xSpacing; break; case Alignment::Right: nvgTextAlign(ctx, NVG_ALIGN_RIGHT | NVG_ALIGN_MIDDLE); drawPos.x() += mSize.x() - unitWidth - xSpacing; break; case Alignment::Center: nvgTextAlign(ctx, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE); drawPos.x() += mSize.x() * 0.5f; break; } nvgFontSize(ctx, fontSize()); nvgFillColor(ctx, mEnabled ? mTheme->mTextColor : mTheme->mDisabledTextColor); // clip visible text area float clipX = mPos.x() + xSpacing - 1.0f; float clipY = mPos.y() + 1.0f; float clipWidth = mSize.x() - unitWidth - 2 * xSpacing + 2.0f; float clipHeight = mSize.y() - 3.0f; nvgScissor(ctx, clipX, clipY, clipWidth, clipHeight); Vector2i oldDrawPos(drawPos); drawPos.x() += mTextOffset; if (mCommitted) { nvgText(ctx, drawPos.x(), drawPos.y(), mValue.c_str(), nullptr); } else { const int maxGlyphs = 1024; NVGglyphPosition glyphs[maxGlyphs]; float textBound[4]; nvgTextBounds(ctx, drawPos.x(), drawPos.y(), mValueTemp.c_str(), nullptr, textBound); float lineh = textBound[3] - textBound[1]; // find cursor positions int nglyphs = nvgTextGlyphPositions(ctx, drawPos.x(), drawPos.y(), mValueTemp.c_str(), nullptr, glyphs, maxGlyphs); updateCursor(ctx, textBound[2], glyphs, nglyphs); // compute text offset int prevCPos = mCursorPos > 0 ? mCursorPos - 1 : 0; int nextCPos = mCursorPos < nglyphs ? mCursorPos + 1 : nglyphs; float prevCX = cursorIndex2Position(prevCPos, textBound[2], glyphs, nglyphs); float nextCX = cursorIndex2Position(nextCPos, textBound[2], glyphs, nglyphs); if (nextCX > clipX + clipWidth) mTextOffset -= nextCX - (clipX + clipWidth) + 1; if (prevCX < clipX) mTextOffset += clipX - prevCX + 1; drawPos.x() = oldDrawPos.x() + mTextOffset; // draw text with offset nvgText(ctx, drawPos.x(), drawPos.y(), mValueTemp.c_str(), nullptr); nvgTextBounds(ctx, drawPos.x(), drawPos.y(), mValueTemp.c_str(), nullptr, textBound); // recompute cursor positions nglyphs = nvgTextGlyphPositions(ctx, drawPos.x(), drawPos.y(), mValueTemp.c_str(), nullptr, glyphs, maxGlyphs); if (mCursorPos > -1) { if (mSelectionPos > -1) { float caretx = cursorIndex2Position(mCursorPos, textBound[2], glyphs, nglyphs); float selx = cursorIndex2Position(mSelectionPos, textBound[2], glyphs, nglyphs); if (caretx > selx) std::swap(caretx, selx); // draw selection nvgBeginPath(ctx); nvgFillColor(ctx, nvgRGBA(255, 255, 255, 80)); nvgRect(ctx, caretx, drawPos.y() - lineh * 0.5f, selx - caretx, lineh); nvgFill(ctx); } float caretx = cursorIndex2Position(mCursorPos, textBound[2], glyphs, nglyphs); // draw cursor nvgBeginPath(ctx); nvgMoveTo(ctx, caretx, drawPos.y() - lineh * 0.5f); nvgLineTo(ctx, caretx, drawPos.y() + lineh * 0.5f); nvgStrokeColor(ctx, nvgRGBA(255, 192, 0, 255)); nvgStrokeWidth(ctx, 1.0f); nvgStroke(ctx); } } nvgResetScissor(ctx); }
void Button::draw(NVGcontext * ctx) { Widget::draw(ctx); NVGcolor gradTop = mTheme->mButtonGradientTopUnfocused; NVGcolor gradBot = mTheme->mButtonGradientBotUnfocused; if (mPushed) { gradTop = mTheme->mButtonGradientTopPushed; gradBot = mTheme->mButtonGradientBotPushed; } else if (mMouseFocus && mEnabled) { gradTop = mTheme->mButtonGradientTopFocused; gradBot = mTheme->mButtonGradientBotFocused; } nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x + 1, mPos.y + 1.0f, mSize.x - 2, mSize.y - 2, mTheme->mButtonCornerRadius - 1); if (mBackgroundColor.a != 0) { nvgFillColor(ctx, Colour(mBackgroundColor.r, mBackgroundColor.g, mBackgroundColor.b, 1.f)); nvgFill(ctx); if (mPushed) gradTop.a = gradBot.a = 0.8f; else { double v = 1 - mBackgroundColor.a; gradTop.a = gradBot.a = mEnabled ? v : v * .5f + .5f; } } NVGpaint bg = nvgLinearGradient(ctx, mPos.x, mPos.y, mPos.x, mPos.y + mSize.y, gradTop, gradBot); nvgFillPaint(ctx, bg); nvgFill(ctx); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x + 0.5f, mPos.y + (mPushed ? 0.5f : 1.5f), mSize.x - 1, mSize.y - 1 - (mPushed ? 0.0f : 1.0f), mTheme->mButtonCornerRadius); nvgStrokeColor(ctx, mTheme->mBorderLight); nvgStroke(ctx); nvgBeginPath(ctx); nvgRoundedRect(ctx, mPos.x + 0.5f, mPos.y + 0.5f, mSize.x - 1, mSize.y - 2, mTheme->mButtonCornerRadius); nvgStrokeColor(ctx, mTheme->mBorderDark); nvgStroke(ctx); int fontSize = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize; nvgFontSize(ctx, fontSize); nvgFontFace(ctx, "sans-bold"); float tw = nvgTextBounds(ctx, 0, 0, mCaption.c_str(), nullptr, nullptr); vec2 center = vec2((float)mPos.x, (float)mPos.y) + vec2((float)mSize.x, (float)mSize.y) * 0.5f; vec2 textPos(center.x - tw * 0.5f, center.y - 1); NVGcolor textColor = mTextColor.a == 0 ? mTheme->mTextColor : mTextColor; if (!mEnabled) textColor = mTheme->mDisabledTextColor; if (mIcon) { auto icon = utf8(mIcon); float iw, ih = fontSize; if (nvgIsFontIcon(mIcon)) { ih *= 1.5f; nvgFontSize(ctx, ih); nvgFontFace(ctx, "icons"); iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr); } else { int w, h; ih *= 0.9f; nvgImageSize(ctx, mIcon, &w, &h); iw = w * ih / h; } if (mCaption != "") iw += mSize.y * 0.15f; nvgFillColor(ctx, textColor); nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); vec2 iconPos = center; iconPos.y -= 1; if (mIconPosition == IconPosition::LeftCentered) { iconPos.x -= (tw + iw) * 0.5f; textPos.x += iw * 0.5f; } else if (mIconPosition == IconPosition::RightCentered) { textPos.x -= iw * 0.5f; iconPos.x += tw * 0.5f; } else if (mIconPosition == IconPosition::Left) iconPos.x = mPos.x + 8; else if (mIconPosition == IconPosition::Right) iconPos.x = mPos.x + mSize.x - iw - 8; if (nvgIsFontIcon(mIcon)) nvgText(ctx, iconPos.x, iconPos.y + 1, icon.data(), nullptr); else { NVGpaint imgPaint = nvgImagePattern(ctx, iconPos.x, iconPos.y - ih / 2, iw, ih, 0, mIcon, mEnabled ? 0.5f : 0.25f); nvgFillPaint(ctx, imgPaint); nvgFill(ctx); } } nvgFontSize(ctx, fontSize); nvgFontFace(ctx, "sans-bold"); nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); nvgFillColor(ctx, mTheme->mTextColorShadow); nvgText(ctx, textPos.x, textPos.y, mCaption.c_str(), nullptr); nvgFillColor(ctx, textColor); nvgText(ctx, textPos.x, textPos.y + 1, mCaption.c_str(), nullptr); }
static void draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height) { const struct zr_command *cmd; glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glEnable(GL_TEXTURE_2D); nvgBeginFrame(nvg, width, height, ((float)width/(float)height)); zr_foreach_command(cmd, queue) { switch (cmd->type) { case ZR_COMMAND_NOP: break; case ZR_COMMAND_SCISSOR: { const struct zr_command_scissor *s = zr_command(scissor, cmd); nvgScissor(nvg, s->x, s->y, s->w, s->h); } break; case ZR_COMMAND_LINE: { const struct zr_command_line *l = zr_command(line, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, l->begin.x, l->begin.y); nvgLineTo(nvg, l->end.x, l->end.y); nvgFillColor(nvg, nvgRGBA(l->color.r, l->color.g, l->color.b, l->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CURVE: { const struct zr_command_curve *q = zr_command(curve, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, q->begin.x, q->begin.y); nvgBezierTo(nvg, q->ctrl[0].x, q->ctrl[0].y, q->ctrl[1].x, q->ctrl[1].y, q->end.x, q->end.y); nvgStrokeColor(nvg, nvgRGBA(q->color.r, q->color.g, q->color.b, q->color.a)); nvgStrokeWidth(nvg, 3); nvgStroke(nvg); } break; case ZR_COMMAND_RECT: { const struct zr_command_rect *r = zr_command(rect, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, r->x, r->y, r->w, r->h, r->rounding); nvgFillColor(nvg, nvgRGBA(r->color.r, r->color.g, r->color.b, r->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CIRCLE: { const struct zr_command_circle *c = zr_command(circle, cmd); nvgBeginPath(nvg); nvgCircle(nvg, c->x + (c->w/2.0f), c->y + c->w/2.0f, c->w/2.0f); nvgFillColor(nvg, nvgRGBA(c->color.r, c->color.g, c->color.b, c->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TRIANGLE: { const struct zr_command_triangle *t = zr_command(triangle, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, t->a.x, t->a.y); nvgLineTo(nvg, t->b.x, t->b.y); nvgLineTo(nvg, t->c.x, t->c.y); nvgLineTo(nvg, t->a.x, t->a.y); nvgFillColor(nvg, nvgRGBA(t->color.r, t->color.g, t->color.b, t->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TEXT: { const struct zr_command_text *t = zr_command(text, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, t->x, t->y, t->w, t->h, 0); nvgFillColor(nvg, nvgRGBA(t->background.r, t->background.g, t->background.b, t->background.a)); nvgFill(nvg); nvgBeginPath(nvg); nvgFillColor(nvg, nvgRGBA(t->foreground.r, t->foreground.g, t->foreground.b, t->foreground.a)); nvgFontSize(nvg, (float)t->height); nvgTextAlign(nvg, NVG_ALIGN_MIDDLE); nvgText(nvg, t->x, t->y + t->h * 0.5f, t->string, &t->string[t->length]); nvgFill(nvg); } break; case ZR_COMMAND_IMAGE: { int w, h; NVGpaint imgpaint; const struct zr_command_image *i = zr_command(image, cmd); nvgImageSize(nvg, i->img.handle.id, &w, &h); nvgBeginPath(nvg); nvgRect(nvg, i->x, i->y, i->w, i->h); nvgFillPaint(nvg, nvgImagePattern(nvg, i->x, i->y, i->w, i->h, 0, i->img.handle.id, 1)); nvgFill(nvg); } break; case ZR_COMMAND_ARC: default: break; } } zr_command_queue_clear(queue); nvgResetScissor(nvg); nvgEndFrame(nvg); glPopAttrib(); }