Browse Source

cursorStartAt/multiLine support, better blink start/stop events, better updateText/win detection, positioning fixes

master
Odilitime 5 years ago
parent
commit
99da7bbf44
  1. 121
      src/graphics/components/InputComponent.cpp
  2. 4
      src/graphics/components/InputComponent.h

121
src/graphics/components/InputComponent.cpp

@ -30,7 +30,8 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r @@ -30,7 +30,8 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
lastRenderedWindowHeight = windowHeight;
// const float rawX, const float rawY, const float rawWidth, const float rawHeight, const unsigned int hexColor, const int passedWindowWidth, const int passedWindowHeight
this->cursorBox = new BoxComponent(x, y, 2, rawHeight, 0x000000FF, windowWidth, windowHeight);
// we only want it one line high (esp for text area)
this->cursorBox = new BoxComponent(x, y, 2, 13, 0x000000FF, windowWidth, windowHeight);
this->cursorBox->boundToPage = this->boundToPage;
this->cursorBox->x = x;
@ -39,16 +40,25 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r @@ -39,16 +40,25 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
this->cursorBox->y = this->windowHeight + y - 13;
}
this->cursorBox->resize(this->windowWidth, this->windowHeight);
// can't updateText because no win set yet
// don't seem to need it either
//this->updateText();
this->updateText();
onFocus=[this]() {
this->focused = true;
// blink cursor
this->cursorTimer = scheduler->setInterval([this]() {
this->showCursor = !this->showCursor;
//std::cout << "showCursor " << this->showCursor << std::endl;
if (this->win) {
this->win->renderDirty = true;
}
}, 500);
this->updateCursor();
this->win->renderDirty = true;
};
onBlur=[this]() {
if (this->cursorTimer != nullptr) {
scheduler->clearInterval(this->cursorTimer);
}
this->focused = false;
this->win->renderDirty = true;
};
@ -184,18 +194,6 @@ void InputComponent::render() { @@ -184,18 +194,6 @@ void InputComponent::render() {
if (focused) {
//std::cout << "Rendering cursor" << std::endl;
// blink cursor
if (cursorTimer != nullptr) {
scheduler->clearInterval(cursorTimer);
}
cursorTimer = scheduler->setInterval([this]() {
this->showCursor = !this->showCursor;
//std::cout << "showCursor " << this->showCursor << std::endl;
if (this->win) {
this->win->renderDirty = true;
}
}, 500);
// render it if we need to
if (showCursor) {
cursorBox->render();
@ -292,11 +290,13 @@ void InputComponent::backSpace() { @@ -292,11 +290,13 @@ void InputComponent::backSpace() {
void InputComponent::updateCursor() {
// this is texture shader coordinates now (not text shader coords)
cursorBox->x = x + this->estWidth;
//
cursorBox->x = x + this->cursorStartAtX;
if (boundToPage) {
cursorBox->y = windowHeight + y - 13;
cursorBox->y = windowHeight + y + this->cursorStartAtY;
} else {
cursorBox->y = y;
// + this->cursorStartAtY
cursorBox->y = y + 5;
}
//std::cout << "placing cursor at " << (int)cursorBox->x << "," << (int)cursorBox->y << std::endl;
cursorBox->resize(windowWidth, windowHeight);
@ -314,56 +314,75 @@ void InputComponent::updateText() { @@ -314,56 +314,75 @@ void InputComponent::updateText() {
}
}
// maybe tie the fontsize to height
if (userInputText) {
delete userInputText;
}
userInputText=new TextComponent(value, 0, 0, 12, false, 0x000000FF, windowWidth, windowHeight);
if (!userInputText->win && this->win) {
userInputText->win = this->win;
}
//std::cout << "placing userInputText at " << static_cast<int>(x) << "," << static_cast<int>(y) << std::endl;
userInputText->x = x;
if (y < 0) {
userInputText->y = y;
} else {
userInputText->y = y - windowHeight + 16;
}
userInputText->boundToPage = this->boundToPage;
//std::cout << "placed userInputText at " << static_cast<int>(x) << "," << static_cast<int>(y - windowHeight) << std::endl;
// 125 pixels width
// but first we need to know how wide the text is
const std::shared_ptr<TextRasterizer> textRasterizer=rasterizerCache->loadFont(12, false); // fontSize, bold
const std::shared_ptr<TextRasterizer> textRasterizer = rasterizerCache->loadFont(12, false); // fontSize, bold
rasterizationRequest request;
request.text = value;
request.startX = x;
request.availableWidth = windowWidth;
request.sourceStartX = 0;
request.sourceStartY = 0;
request.noWrap = true;
std::unique_ptr<std::pair<int, int>> textInfo = textRasterizer->size(request);
if (this->multiLine) {
request.noWrap = false;
} else {
request.noWrap = true;
}
std::unique_ptr<sizeResponse> textInfo = textRasterizer->size(request);
if (textInfo.get() == nullptr) {
std::cout << "InputComponent::updateText - couldn't estimate value[" << value << "] size" << std::endl;
std::cout << "InputComponent::updateText - couldn't estimate value[" << this->value << "] size" << std::endl;
return;
}
//int textWidth = textInfo->first;
this->estWidth = std::get<0>(*textInfo.get());
//int estHeight = std::get<1>(*textInfo.get());
userInputText->rasterStartX = 0;
userInputText->rasterStartY = 0;
//std::cout << "InputComponent::updateText - estWidth: " << estWidth << " width: " << static_cast<int>(width) << std::endl;
if (estWidth > width) {
//std::cout << "scrolling text" << std::endl;
userInputText->rasterStartX = estWidth - width;
int estWidth = textInfo->width;
int estHeight = textInfo->height;
this->cursorStartAtX = textInfo->endingX;
this->cursorStartAtY = textInfo->endingY;
if (estHeight > this->height) {
this->cursorStartAtY += estHeight - this->height;
}
this->updateCursor();
userInputText->noWrap = true;
// why does changing the width mess shit up?
//std::cout << "InputComponent::updateText - our width: " << static_cast<int>(width) << " windowWidth: " << windowWidth << std::endl;
userInputText->resize(windowWidth, windowHeight, width); // need to make sure there's a texture
if (this->win) {
// maybe tie the fontsize to height
if (this->userInputText) {
delete this->userInputText;
}
this->userInputText = new TextComponent(value, 0, 0, 12, false, 0x000000FF, this->windowWidth, this->windowHeight);
if (!userInputText->win && this->win) {
userInputText->win = this->win;
}
//std::cout << "placing userInputText at " << static_cast<int>(x) << "," << static_cast<int>(y) << std::endl;
userInputText->x = this->x;
//std::cout << "InputComponent::updateText - y: " << static_cast<int>(this->y) << " boundToPage: " << this->boundToPage << std::endl;
//if (this->y < 0) {
if (this->boundToPage) {
userInputText->y = this->y;
} else {
userInputText->y = this->y - this->windowHeight + 16;
}
userInputText->boundToPage = this->boundToPage;
userInputText->rasterStartX = 0;
userInputText->rasterStartY = 0;
//std::cout << "InputComponent::updateText - estWidth: " << estWidth << " width: " << static_cast<int>(this->width) << " eX: " << textInfo->endingX << std::endl;
if (estWidth > this->width) {
//std::cout << "scrolling text" << std::endl;
userInputText->rasterStartX = estWidth - this->width;
}
//std::cout << "InputComponent::updateText - estHeight: " << estHeight << " height: " << static_cast<int>(this->height) << " eY: " << textInfo->endingY << std::endl;
if (this->multiLine && estHeight > this->height) {
//std::cout << "scrolling text" << std::endl;
userInputText->rasterStartY = estHeight - this->height; // start at one widget height from the bottom
}
//std::cout << "InputComponent::updateText - textHeight: " << estHeight << " inputHeight: " << static_cast<int>(height) << " eY: " << textInfo->endingY << " y0max:" << textInfo->y0max << " rasterStartY: " << userInputText->rasterStartY << " lines: " << textInfo->lines << std::endl;
// wrap if multiLine, otherwise don't
userInputText->noWrap = !this->multiLine;
userInputText->resize(this->windowWidth, this->windowHeight, this->width); // need to make sure there's a texture
this->win->renderDirty = true;
} // else it was called from cstr, and window isn't set up yet but no worrites, render will be dirty
}

4
src/graphics/components/InputComponent.h

@ -31,8 +31,10 @@ public: @@ -31,8 +31,10 @@ public:
bool showCursor = true;
std::shared_ptr<timer_handle> cursorTimer = nullptr;
// store for cursor
int estWidth;
int cursorStartAtX;
int cursorStartAtY;
TagNode *node = nullptr;
bool multiLine = false; // textarea control
};
#endif

Loading…
Cancel
Save