Пример #1
0
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);
}
	box2px TreeItem::update(AlloyContext* context, const pixel2& offset) {
		NVGcontext* nvg = context->nvgContext;
		nvgTextAlign(nvg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
		nvgFontSize(nvg, fontSize);
		nvgFontFaceId(nvg, context->getFontHandle(FontType::Bold));
		spaceWidth = fontSize + PADDING * 2;
		float textWidth = nvgTextBounds(nvg, 0, 0, name.c_str(), nullptr, nullptr);
		nvgFontFaceId(nvg, context->getFontHandle(FontType::Icon));
		float iconWidth =
			(iconCodeString.length() == 0) ?
			0 :
			nvgTextBounds(nvg, 0, 0, iconCodeString.c_str(), nullptr,
				nullptr) + PADDING * 2;
		float th = (name.length() > 0) ? fontSize + PADDING * 2 : 0;
		selectionBounds = box2px(offset,
			pixel2(textWidth + iconWidth + spaceWidth + PADDING, th));
		bounds = selectionBounds;
		if (isExpanded()) {
			pixel2 pt = offset + pixel2((name.length() > 0) ? spaceWidth : 0, th);
			for (TreeItemPtr& item : children) {
				box2px cdims = item->update(context, pt);
				bounds.dimensions = aly::max(bounds.max(), cdims.max())
					- aly::min(bounds.min(), cdims.min());
				pt += pixel2(0.0f, cdims.dimensions.y);
			}
		}
		return bounds;
	}
Пример #3
0
void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, struct NVGcolor col)
{
	struct NVGpaint bg;
	char icon[8];
	float cornerRadius = 4.0f;
	float tw = 0, iw = 0;

	bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,isBlack(col)?16:32), nvgRGBA(0,0,0,isBlack(col)?16:32) );
	nvgBeginPath(vg);
	nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
	if (!isBlack(col) ) {
		nvgFillColor(vg, col);
		nvgFill(vg);
	}
	nvgFillPaint(vg, bg);
	nvgFill(vg);

	nvgBeginPath(vg);
	nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
	nvgStrokeColor(vg, nvgRGBA(0,0,0,48) );
	nvgStroke(vg);

	nvgFontSize(vg, 20.0f);
	nvgFontFace(vg, "sans-bold");
	tw = nvgTextBounds(vg, 0,0, text, NULL, NULL);
	if (preicon != 0) {
		nvgFontSize(vg, h*1.3f);
		nvgFontFace(vg, "icons");
		iw = nvgTextBounds(vg, 0,0, cpToUTF8(preicon,icon), NULL, NULL);
		iw += h*0.15f;
	}

	if (preicon != 0) {
		nvgFontSize(vg, h*1.3f);
		nvgFontFace(vg, "icons");
		nvgFillColor(vg, nvgRGBA(255,255,255,96) );
		nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
		nvgText(vg, x+w*0.5f-tw*0.5f-iw*0.75f, y+h*0.5f, cpToUTF8(preicon,icon), NULL);
	}

	nvgFontSize(vg, 20.0f);
	nvgFontFace(vg, "sans-bold");
	nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
	nvgFillColor(vg, nvgRGBA(0,0,0,160) );
	nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f-1,text, NULL);
	nvgFillColor(vg, nvgRGBA(255,255,255,160) );
	nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL);
}
Пример #4
0
Rect TextNode::generateBoundingBox()
{
    Rect boundingBox = Node::generateBoundingBox();

    if(mFontColor.isVisible())
    {
        NVGcontext& nanoVgContext = Application::getNanoVgContext();
        nvgSave(&nanoVgContext);

        float bounds[4];
        setupContext(nanoVgContext);

        for(const Text& text : mTexts)
        {
            const char* charArray = text.getString().getCharArray();
            float positionX = text.getPosition().getX();
            float positionY = text.getPosition().getY();
            if(isPositive(text.getBoxWidth()))
            {
                nvgTextBoxBounds(&nanoVgContext, positionX, positionY, text.getBoxWidth(),
                        charArray, nullptr, bounds);
            }
            else
            {
                nvgTextBounds(&nanoVgContext, positionX, positionY, charArray, nullptr, bounds);
            }

            boundingBox.join(Rect(Point(bounds[0], bounds[1]), Point(bounds[2], bounds[3])));
        }

        nvgRestore(&nanoVgContext);
    }

    return boundingBox;
}
Пример #5
0
	float NanoInk::contentSize(Dimension dim)
	{
		if(mImage)
		{
			return dim == DIM_X ? float(mImage->d_width) : float(mImage->d_height);
		}
		else if(skin().textColour().a() != 0.f)
		{
			float bounds[4];
			float height;

			this->setupText();
			nvgTextBounds(mCtx, 0.f, 0.f, mFrame.widget().label().c_str(), nullptr, bounds);
			nvgTextMetrics(mCtx, nullptr, nullptr, &height);

			return dim == DIM_X ? bounds[2] - bounds[0] : height;
		}
		else if(!skin().imageSkin().null())
		{
			if(skin().imageSkin().d_stretch == DIM_X)
				return skin().imageSkin().d_height;
			else if(skin().imageSkin().d_stretch == DIM_Y)
				return skin().imageSkin().d_width;
		}
		
		return 0.f;
	}
Пример #6
0
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;
}
	void TreeItem::draw(ExpandTree* tree, AlloyContext* context,
		const pixel2& offset) {
		box2px bounds = getBounds();
		NVGcontext* nvg = context->nvgContext;
		nvgFontFaceId(nvg, context->getFontHandle(FontType::Icon));
		float spaceWidth = fontSize + PADDING * 2;
		float iconWidth = 0;
		static const std::string rightArrow = CodePointToUTF8(0xf0da);
		static const std::string downArrow = CodePointToUTF8(0xf0d7);
		pixel2 pt = bounds.position + offset;
		bool selected = (tree->getSelectedItem() == this)&&!tree->isOverArrow();
		nvgFontSize(nvg, fontSize);

		if (iconCodeString.length() > 0) {
			iconWidth = nvgTextBounds(nvg, 0, 0, iconCodeString.c_str(), nullptr,
				nullptr) + PADDING * 2;

			if (children.size() > 0 || onExpand) {
				nvgTextAlign(nvg, NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
				if (tree->isOverArrow()) {
					nvgFillColor(nvg, context->theme.LIGHTEST);
				}
				else {
					nvgFillColor(nvg, context->theme.LIGHTER);
				}
				nvgText(nvg, pt.x + spaceWidth * 0.5f, pt.y + PADDING,
					(expanded) ? downArrow.c_str() : rightArrow.c_str(),
					nullptr);
			}
			if (selected) {
				nvgFillColor(nvg, context->theme.LIGHTEST);
			}
			else {
				nvgFillColor(nvg, context->theme.LIGHTER);
			}
			nvgTextAlign(nvg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
			nvgText(nvg, pt.x + spaceWidth, pt.y + PADDING, iconCodeString.c_str(),
				nullptr);
		}
		if (name.length() > 0) {
			if (selected) {
				nvgFillColor(nvg, context->theme.LIGHTEST);
			}
			else {
				nvgFillColor(nvg, context->theme.LIGHTER);
			}
			nvgTextAlign(nvg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
			nvgFontFaceId(nvg, context->getFontHandle(FontType::Bold));
			nvgText(nvg, pt.x + iconWidth + spaceWidth, pt.y + PADDING,
				name.c_str(), nullptr);
		}
		if (expanded) {
			for (TreeItemPtr& item : children) {
				item->draw(tree, context, offset);
			}
		}
	}
AwesomeGlyph::AwesomeGlyph(int codePoint, AlloyContext* context,
		const FontStyle& style, pixel height) :
		Glyph(CodePointToUTF8(codePoint), GlyphType::Awesome, 0, height), codePoint(
				codePoint), style(style) {
	NVGcontext* nvg = context->nvgContext;
	nvgFontSize(nvg, height);
	nvgFontFaceId(nvg, context->getFontHandle(FontType::Icon));
	width = nvgTextBounds(nvg, 0, 0, name.c_str(), nullptr, nullptr);

}
Пример #9
0
static gui_size
font_get_width(gui_handle handle, const gui_char *text, gui_size len)
{
    gui_size width;
    float bounds[4];
    NVGcontext *ctx = (NVGcontext*)handle.ptr;
    nvgTextBounds(ctx, 0, 0, text, &text[len], bounds);
    width = (gui_size)(bounds[2] - bounds[0]);
    return width;
}
Пример #10
0
Vector2i CheckBox::preferredSize(NVGcontext *ctx) {
    if (mFixedSize != Vector2i(0))
        return mFixedSize;
    nvgFontSize(ctx, fontSize());
    nvgFontFace(ctx, "sans");
    return Vector2i(
        nvgTextBounds(ctx, 0, 0, mCaption.c_str(), nullptr, nullptr) +
            1.7f * fontSize(),
        fontSize() * 1.3f);
}
Пример #11
0
/* =================================================================
 *
 *                          APP
 *
 * ================================================================= */
static size_t
font_get_width(zr_handle handle, float height, const char *text, size_t len)
{
    size_t width;
    float bounds[4];
    NVGcontext *ctx = (NVGcontext*)handle.ptr;
    nvgFontSize(ctx, (float)height);
    nvgTextBounds(ctx, 0, 0, text, &text[len], bounds);
    width = (size_t)(bounds[2] - bounds[0]);
    return width;
}
Пример #12
0
Vector2i Window::preferredSize(NVGcontext *ctx) const {
    Vector2i result = Widget::preferredSize(ctx);

    nvgFontSize(ctx, 18.0f);
    nvgFontFace(ctx, "sans-bold");
    float bounds[4];
    nvgTextBounds(ctx, 0, 0, mTitle.c_str(), nullptr, bounds);

    return result.cwiseMax(Vector2i(
        bounds[2]-bounds[0] + 20, bounds[3]-bounds[1]
    ));
}
Пример #13
0
const QRectF QNanoPainter::textBoundingBox(const QString &text, float x, float y, float maxWidth)
{
    _checkAlignPixelsText(&x, &y);
    _checkFont();
    float bounds[4];
    if (maxWidth < 0) {
        nvgTextBounds(nvgCtx(), x, y, text.toUtf8().constData(), nullptr, bounds);
    } else {
        nvgTextBoxBounds(nvgCtx(), x, y, maxWidth, text.toUtf8().constData(), nullptr, bounds);
    }
    return QRectF(bounds[0], bounds[1], bounds[2]-bounds[0], bounds[3]-bounds[1]);
}
Пример #14
0
void Caret::updatePosFromIdx(NVGcontext *ctx) {
    
    float lineh;
    nvgTextMetrics(ctx, nullptr, nullptr, &lineh);
    const int maxGlyphs = 1024;
    NVGglyphPosition glyphs[maxGlyphs];
    const std::string& textLine = mConsole->mBuffer[mIdx.y()];
    float textBound[4];

    nvgTextBounds(ctx, 0, 0, textLine.c_str(), nullptr, textBound);
    int nglyphs = nvgTextGlyphPositions(ctx, 0, 0, textLine.c_str(),
            nullptr, glyphs, maxGlyphs);

    //update carete position now
    mPos.y() = mIdx.y() * lineh;
    mPos.x() = cursorIndex2Position(mIdx.x(), textBound[2], glyphs, nglyphs);
}
Пример #15
0
Vector2i Label::preferredSize(NVGcontext *ctx) const {
    if (mCaption == "")
        return Vector2i::Zero();
    nvgFontFace(ctx, mFont.c_str());
    nvgFontSize(ctx, fontSize());
    if (mFixedSize.x() > 0) {
        float bounds[4];
        nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
        nvgTextBoxBounds(ctx, mPos.x(), mPos.y(), mFixedSize.x(), mCaption.c_str(), nullptr, bounds);
        return Vector2i(
            mFixedSize.x(), (bounds[3]-bounds[1])
        );
    } else {
        nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
        return Vector2i(
            nvgTextBounds(ctx, 0, 0, mCaption.c_str(), nullptr, nullptr),
            mTheme->mStandardFontSize
        );
    }
}
Пример #16
0
void PopupButton::draw(NVGcontext* ctx) {
    if (!mEnabled && mPushed)
        mPushed = false;

    mPopup->setVisible(mPushed);
    Button::draw(ctx);

    auto icon = utf8(ENTYPO_ICON_CHEVRON_SMALL_RIGHT);
    NVGcolor textColor = mTextColor.w() == 0 ? mTheme->mTextColor : mTextColor;
    nvgFontSize(ctx, mTheme->mButtonFontSize * 1.5f);
    nvgFontFace(ctx, "icons");
    nvgFillColor(ctx, mEnabled ? textColor : mTheme->mDisabledTextColor);
    nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);

    float iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr);
    Vector2f iconPos(
        mPos.x() + mSize.x() - iw - 8, mPos.y() + mSize.y() * 0.5f -1);

    nvgText(ctx, iconPos.x(), iconPos.y(), icon.data(), nullptr);
}
Пример #17
0
void drawEditBoxNum(struct NVGcontext* vg,
		const char* text, const char* units, float x, float y, float w, float h)
{
	float uw;

	drawEditBoxBase(vg, x,y, w,h);

	uw = nvgTextBounds(vg, 0,0, units, NULL, NULL);

	nvgFontSize(vg, 18.0f);
	nvgFontFace(vg, "sans");
	nvgFillColor(vg, nvgRGBA(255,255,255,64) );
	nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
	nvgText(vg, x+w-h*0.3f,y+h*0.5f,units, NULL);

	nvgFontSize(vg, 20.0f);
	nvgFontFace(vg, "sans");
	nvgFillColor(vg, nvgRGBA(255,255,255,128) );
	nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
	nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL);
}
Пример #18
0
void PopupButton::draw (NVGcontext * ctx)
{
   if (!mEnabled && mPushed)
      mPushed = false;
   mPopup->setVisible (mPushed);
   Button::draw (ctx);
   if (mChevronIcon)
   {
      auto icon = utf8 (mChevronIcon);
      NVGcolor textColor =
         mTextColor.a == 0 ? mTheme->mTextColor : mTextColor;
      nvgFontSize (ctx, (mFontSize < 0 ? mTheme->mButtonFontSize : mFontSize) * 1.5f);
      nvgFontFace (ctx, "icons");
      nvgFillColor (ctx, mEnabled ? textColor : mTheme->mDisabledTextColor);
      nvgTextAlign (ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
      float iw = nvgTextBounds (ctx, 0, 0, icon.data(), nullptr, nullptr);
      vec2 iconPos (mPos.x + mSize.x - iw - 8,
                        mPos.y + mSize.y * 0.5f - 1);
      nvgText (ctx, iconPos.x, iconPos.y, icon.data(), nullptr);
   }
}
Пример #19
0
void Caret::onClick(NVGcontext *ctx, float lineh, Vector2i clickPos){
    if (clickPos.x() > -1) {
        mIdx.y() = (int) clickPos.y()/lineh;
        //std::cout << "mPos = " << mConsole->mPos.x() << "," << mConsole->mPos.y() << std::endl;

        if(mIdx.y() < (int)mConsole->mBuffer.size()){
            //std::cout << mBuffer.at(mCursorPosY)<< std::endl;
            double time = glfwGetTime();
            if (time - mLastClick < 0.25) {
                /* Double-click: select a line */
                int linew = mConsole->mSize.x();
                mSelectionIdx = Vector2i(0,(int) clickPos.y()/lineh); 
                mSelectionPos = Vector2i(0,mSelectionIdx.y()*lineh);
                mIdx = Vector2i(mConsole->mBuffer[mSelectionIdx.y()].size(), mSelectionIdx.y());
                mPos = Vector2i(linew, mSelectionPos.y());
                mSelectionState = true;
                std::cout << "Sel : " << mSelectionPos.x() << "," << mSelectionPos.y() << std::endl;
                std::cout << "Pos : " << mPos.x() << "," << mPos.y() << std::endl;
            }
            else { 
                const int maxGlyphs = 1024;
                NVGglyphPosition glyphs[maxGlyphs];
                const std::string& textLine = mConsole->mBuffer[mIdx.y()];
                float textBound[4];

                nvgTextBounds(ctx, 0, 0, textLine.c_str(), nullptr, textBound);
                int nglyphs = nvgTextGlyphPositions(ctx, 0, 0, textLine.c_str(),
                        nullptr, glyphs, maxGlyphs);

                mIdx.x() = position2CursorIndex(clickPos.x(), textBound[2], glyphs, nglyphs);
         
                //update carete position now
                mPos.y() = mIdx.y() * lineh;
                mPos.x() = cursorIndex2Position(mIdx.x(), textBound[2], glyphs, nglyphs);
            }
            mLastClick = time;
        }
    }
}
Пример #20
0
		float NVGRenderer::textBounds(float x, float y, const char* string, const char* end, float* bounds)
		{
			return nvgTextBounds(m_context, x, y, string, end, bounds);
		}
Пример #21
0
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);
}
Пример #22
0
void Button::draw(NVGcontext *ctx) {
    GUIObject::draw(ctx);


    NVGcolor gradTop = Theme.mButtonGradientTopUnfocused;
    NVGcolor gradBot = Theme.mButtonGradientBotUnfocused;

    if (mPushed) {
        gradTop = Theme.mButtonGradientTopPushed;
        gradBot = Theme.mButtonGradientBotPushed;
    } else if (mMouseFocus && mEnabled) {
        gradTop = Theme.mButtonGradientTopFocused;
        gradBot = Theme.mButtonGradientBotFocused;
    }
    nvgBeginPath(ctx);

    nvgRoundedRect(ctx, mPosition.x + 1, mPosition.y + 1, mSize.x - 2,
                   mSize.y - 2, Theme.mButtonCornerRadius);

    if (mBackgroundColor.a != 0) {
        nvgFillColor(ctx, Color(mBackgroundColor.r, mBackgroundColor.g, mBackgroundColor.b, 1.f));
        nvgFill(ctx);
        if (mPushed) {
            gradTop.a = gradBot.a = 0.8f;
        } else {
            double v = 1 - mBackgroundColor.a/2;
            gradTop.a = gradBot.a = mEnabled ? v : v * .5f + 0.5f;
        }
    }

    NVGpaint bg = nvgLinearGradient(ctx, mPosition.x, mPosition.y, mPosition.x,
                                    mPosition.y + mSize.y, gradTop, gradBot);
    nvgFillPaint(ctx, bg);
    nvgFill(ctx);

    // nvgBeginPath(ctx);
    // nvgRoundedRect(ctx, mPosition.x + 0.5f, mPosition.y + (mPushed ? 0.5f : 1.5f), mSize.x - 1,
    //                mSize.y - 1 - (mPushed ? 0.0f : 1.0f), Theme.mButtonCornerRadius);
    // nvgStrokeColor(ctx, Theme.mBorderLight);
    // nvgStroke(ctx);

    // nvgBeginPath(ctx);
    // nvgRoundedRect(ctx, mPosition.x + 0.5f, mPosition.y + 0.5f, mSize.x - 1,
    //                mSize.y - 2, Theme.mButtonCornerRadius);
    // nvgStrokeColor(ctx, Theme.mBorderDark);
    // nvgStroke(ctx);

    int fontSize = mFontSize == -1 ? Theme.mButtonFontSize : mFontSize;
    nvgFontSize(ctx, fontSize);
    nvgFontFace(ctx, "sans-bold");
    float tw = nvgTextBounds(ctx, 0,0, mCaption.c_str(), nullptr, nullptr);

    vec2 center = (vec2)mPosition + ((vec2)mSize) * 0.5f;
    vec2 textPos(center.x - 0.5f * tw, center.y - 1);
    NVGcolor textColor =
        mTextColor.a == 0 ? Theme.mTextColor : mTextColor;
    // if (!mEnabled)
    //     textColor = mDisabledTextColor;

    if (mIcon) {
        auto icon = utf8(mIcon);

        float iw, ih = fontSize;
        ih *= 1.5f;
        nvgFontSize(ctx, ih);
        nvgFontFace(ctx, "icons");
        iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr);
        
        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 = mPosition.x + 8;
        } else if (mIconPosition == IconPosition::Right) {
            iconPos.x = mPosition.x + mSize.x - iw - 8;
        }

        nvgText(ctx, iconPos.x, iconPos.y+1, icon.data(), nullptr);
    }

    nvgFontSize(ctx, fontSize);
    nvgFontFace(ctx, "sans-bold");
    nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
    // nvgFillColor(ctx, Theme.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);
}
Пример #23
0
void OGUITextInput::draw(int x, int y, int w)
{
    int h = this->height;
    OGUIWidget::draw(x, y, w);
    OGUIArea* area= (OGUIArea*)(this->area);
    NVGcontext* vg= (NVGcontext*)(area->window->vg);

    NVGpaint bg;
    // Edit
    bg = nvgBoxGradient(vg, x+1,y+1+1.5f, w-2,h-2, 3,4, nvgRGBA(255,255,255,32), nvgRGBA(32,32,32,32));
    nvgBeginPath(vg);
    nvgRoundedRect(vg, x+1,y+1, w-2,h-2, 4-1);
    nvgFillPaint(vg, bg);
    nvgFill(vg);

    nvgBeginPath(vg);
    nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, 4-0.5f);
    nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
    nvgStroke(vg);

    nvgFontSize(vg, 20.0f*area->window->font_scale);
    nvgFontFace(vg, "sans");
    if(value.compare(label)==0)
        nvgFillColor(vg, nvgRGBA(255,255,255,64));
    else
        nvgFillColor(vg, nvgRGBA(255,255,255,128));
    nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
    nvgText(vg, x+h*0.3f,y+h*0.5f, value.c_str(), NULL);

    if(area->window->mouse_x > x &&
       area->window->mouse_x < x+w &&
       area->window->mouse_y > y &&
       area->window->mouse_y < y + h
            ) {
        area->window->setCursor(TEXT_CURSOR);
        if (area->window->mouse_state == GLFW_PRESS)
        {
            is_focus_= true;
            area->window->setKeyFocus(this);
            if(value.compare(label)==0)
                value="";
        }
    }else{
        area->window->setCursor(DEFAULT_CURSOR);
        if (area->window->mouse_state == GLFW_PRESS)
        {
            if(value.compare("")==0)
                value= string(label);
            is_focus_= false;
        }
    }

    if(is_focus_){
        float char_pos= nvgTextBounds(vg, x, y, value.c_str(), NULL, NULL);
        nvgBeginPath(vg);
        animation_alpha_+=10;
        animation_alpha_=animation_alpha_%255;
        nvgFillColor(vg, nvgRGBA(255,192,0,animation_alpha_));
        nvgRect(vg, x+h*0.3f+char_pos, y+6, 1, 20);
        nvgFill(vg);
    }

}
Пример #24
0
void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
{
	struct NVGtextRow rows[3];
	struct 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 gx = 0.0f, gy = 0.0f;
	int gutter = 0;
	NVG_NOTUSED(height);

	nvgSave(vg);

	nvgFontSize(vg, 18.0f);
	nvgFontFace(vg, "sans");
	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);
	for (nrows = nvgTextBreakLines(vg, start, end, width, rows, 3); 0 != nrows; nrows = nvgTextBreakLines(vg, start, end, width, rows, 3) )
	{
		for (i = 0; i < nrows; i++) {
			struct 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:8) );
			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 tgx = x0 * 0.3f + x1 * 0.7f;
					if (mx >= px && mx < tgx)
						caretx = glyphs[j].x;
					px = tgx;
				}
				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];
		bx::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
			, bx::fround(bounds[0])-4.0f
			, bx::fround(bounds[1])-2.0f
			, bx::fround(bounds[2]-bounds[0])+8.0f
			, bx::fround(bounds[3]-bounds[1])+4.0f
			, (bx::fround(bounds[3]-bounds[1])+4.0f)/2.0f-1.0f
			);
		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);
	nvgBeginPath(vg);
	nvgFillColor(vg, nvgRGBA(220,220,220,255) );
	nvgRoundedRect(vg
		, bx::fround(bounds[0]-2.0f)
		, bx::fround(bounds[1]-2.0f)
		, bx::fround(bounds[2]-bounds[0])+4.0f
		, bx::fround(bounds[3]-bounds[1])+4.0f
		, 3.0f
		);
	px = float( (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);
}
Пример #25
0
void drawTextCenter(NVGcontext* vg, char* text, int x, int y) {
	float bounds[4];
	nvgTextBounds(vg, 0, 0, text, NULL, bounds);
	int dx = x - (bounds[2] - bounds[0]) / 2, dy = y - (bounds[3] + bounds[1]) / 2;
	nvgText(vg, dx, dy, text, NULL);
}
Пример #26
0
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);
}
Пример #27
0
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);
}
Пример #28
0
 float getTextWidth(Skin::SkinClass::Attributes &skinClass, const std::string& text) {
   nvgFontSize(m_vg, skinClass.textSize);
   return nvgTextBounds(m_vg, 0.f, 0.f, text.c_str(), 0, 0);
 }