Exemple #1
0
void TextEdit::Draw(UIContext &dc) {
	dc.PushScissor(bounds_);
	dc.SetFontStyle(dc.theme->uiFont);
	dc.FillRect(HasFocus() ? UI::Drawable(0x80000000) : UI::Drawable(0x30000000), bounds_);

	float textX = bounds_.x;
	float w, h;
	// Hack to find the caret position. Might want to find a better way...
	dc.MeasureTextCount(dc.theme->uiFont, text_.c_str(), caret_, &w, &h, ALIGN_VCENTER | ALIGN_LEFT);
	float caretX = w;

	if (caretX > bounds_.w) {
		// Scroll text to the left if the caret won't fit. Not ideal but looks better than not scrolling it.
		textX -= caretX - bounds_.w;
	}

	caretX += textX;

	Bounds textBounds = bounds_;
	textBounds.x = textX;

	if (text_.empty()) {
		if (placeholderText_.size()) {
			dc.DrawTextRect(placeholderText_.c_str(), bounds_, 0x50FFFFFF, ALIGN_CENTER);
		}
	} else {
		dc.DrawTextRect(text_.c_str(), textBounds, 0xFFFFFFFF, ALIGN_VCENTER | ALIGN_LEFT);
	}

	dc.FillRect(UI::Drawable(0xFFFFFFFF), Bounds(caretX - 1, bounds_.y + 2, 3, bounds_.h - 4));
	dc.PopScissor();
}