textbox: 'editable' property
This commit is contained in:
@@ -388,7 +388,7 @@ void TextBox::draw(const GfxContext* pctx, Assets* assets) {
|
||||
lcoord.y -= 2;
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
if (int((Window::time() - caretLastMove) * 2) % 2 == 0) {
|
||||
if (editable && int((Window::time() - caretLastMove) * 2) % 2 == 0) {
|
||||
uint line = label->getLineByTextIndex(caret);
|
||||
uint lcaret = caret - label->getTextLineOffset(line);
|
||||
batch->setColor(glm::vec4(1.0f));
|
||||
@@ -426,7 +426,7 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
batch->texture(nullptr);
|
||||
|
||||
if (valid) {
|
||||
if (isFocused()) {
|
||||
if (isFocused() && !multiline) {
|
||||
batch->setColor(focusedColor);
|
||||
} else if (hover && !multiline) {
|
||||
batch->setColor(hoverColor);
|
||||
@@ -442,6 +442,17 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
input = supplier();
|
||||
}
|
||||
|
||||
if (isFocused() && multiline) {
|
||||
batch->setColor(glm::vec4(1, 1, 1, 0.1f));
|
||||
glm::vec2 lcoord = label->calcCoord();
|
||||
lcoord.y -= 4;
|
||||
uint line = label->getLineByTextIndex(caret);
|
||||
int lineY = label->getLineYOffset(line);
|
||||
int lineHeight = font->getLineHeight() * label->getLineInterval();
|
||||
batch->rect(lcoord.x, lcoord.y+lineY, label->getSize().x, 1);
|
||||
batch->rect(lcoord.x, lcoord.y+lineY+lineHeight, label->getSize().x, 1);
|
||||
}
|
||||
|
||||
label->setColor(glm::vec4(input.empty() ? 0.5f : 1.0f));
|
||||
label->setText(getText());
|
||||
if (multiline && font) {
|
||||
@@ -465,8 +476,8 @@ void TextBox::paste(const std::wstring& text) {
|
||||
auto left = input.substr(0, caret);
|
||||
auto right = input.substr(caret);
|
||||
input = left + text + right;
|
||||
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
|
||||
}
|
||||
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
|
||||
// refresh label lines configuration for correct setCaret work
|
||||
label->setText(input);
|
||||
|
||||
@@ -560,6 +571,14 @@ bool TextBox::isMultiline() const {
|
||||
return multiline;
|
||||
}
|
||||
|
||||
void TextBox::setEditable(bool editable) {
|
||||
this->editable = editable;
|
||||
}
|
||||
|
||||
bool TextBox::isEditable() const {
|
||||
return editable;
|
||||
}
|
||||
|
||||
void TextBox::setOnEditStart(runnable oneditstart) {
|
||||
onEditStart = oneditstart;
|
||||
}
|
||||
@@ -621,9 +640,7 @@ void TextBox::resetMaxLocalCaret() {
|
||||
maxLocalCaret = caret - label->getTextLineOffset(label->getLineByTextIndex(caret));
|
||||
}
|
||||
|
||||
|
||||
// TODO: refactor
|
||||
void TextBox::keyPressed(keycode key) {
|
||||
void TextBox::performEditingKeyboardEvents(keycode key) {
|
||||
bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT);
|
||||
bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
|
||||
uint previousCaret = caret;
|
||||
@@ -726,6 +743,12 @@ void TextBox::keyPressed(keycode key) {
|
||||
resetSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextBox::keyPressed(keycode key) {
|
||||
if (editable) {
|
||||
performEditingKeyboardEvents(key);
|
||||
}
|
||||
if (Events::pressed(keycode::LEFT_CONTROL)) {
|
||||
// Copy selected text to clipboard
|
||||
if (key == keycode::C || key == keycode::X) {
|
||||
@@ -733,12 +756,12 @@ void TextBox::keyPressed(keycode key) {
|
||||
if (!text.empty()) {
|
||||
Window::setClipboardText(text.c_str());
|
||||
}
|
||||
if (key == keycode::X) {
|
||||
if (editable && key == keycode::X) {
|
||||
eraseSelected();
|
||||
}
|
||||
}
|
||||
// Paste text from clipboard
|
||||
if (key == keycode::V) {
|
||||
if (key == keycode::V && editable) {
|
||||
const char* text = Window::getClipboardText();
|
||||
if (text) {
|
||||
paste(util::str2wstr_utf8(text));
|
||||
|
||||
Reference in New Issue
Block a user