Browse Source

setValue/getValue() wrapper, addChar/BackSpace cursor updates, updateCursor() does it's on textRaster size

master
Odilitime 4 years ago
parent
commit
3cca9dd391

+ 1
- 1
src/graphics/components/Component.cpp View File

@@ -428,7 +428,7 @@ void Component::printComponentTree(const std::shared_ptr<Component> &component,
std::cout << std::fixed << "X: " << static_cast<int>(butComponent->x) << " Y: " << static_cast<int>(butComponent->y) << " WIDTH: " << static_cast<int>(butComponent->width) << " HEIGHT: " << static_cast<int>(butComponent->height) << " INLINE: " << butComponent->isInline << " Bound: " << butComponent->boundToPage << " BUTTON: " << butComponent->value << std::endl;
} else
if (inputComponent) {
std::cout << std::fixed << "X: " << static_cast<int>(inputComponent->x) << " Y: " << static_cast<int>(inputComponent->y) << " WIDTH: " << static_cast<int>(inputComponent->width) << " HEIGHT: " << static_cast<int>(inputComponent->height) << " INLINE: " << inputComponent->isInline << " Bound: " << inputComponent->boundToPage << " INPUT: " << inputComponent->value << std::endl;
std::cout << std::fixed << "X: " << static_cast<int>(inputComponent->x) << " Y: " << static_cast<int>(inputComponent->y) << " WIDTH: " << static_cast<int>(inputComponent->width) << " HEIGHT: " << static_cast<int>(inputComponent->height) << " INLINE: " << inputComponent->isInline << " Bound: " << inputComponent->boundToPage << " INPUT: " << inputComponent->getValue() << std::endl;
} else {
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
if (textComponent) {

+ 113
- 5
src/graphics/components/InputComponent.cpp View File

@@ -1,10 +1,40 @@
#include "InputComponent.h"
#include "DocumentComponent.h"
#include <iostream>
#include "../text/TextRasterizerCache.h"
#include "../../scheduler.h"

#include "../opengl/Shader.h"

void TextBlock::addChar(char chr) {
if (chr==10 || chr==13) {
this->lineChars.push_back(this->currentLineChars);
this->currentLineChars = 0;
lines++;
}
this->text += std::string(&chr);
this->currentLineChars++;
}

void TextBlock::delLastChar() {
// get last char
char lastChar = this->text.at(this->text.size() - 1);
this->text = this->text.substr(0, this->text.size() - 2);
this->currentLineChars--;
if (lastChar == 10 || lastChar == 13) {
this->lineChars.pop_back();
this->currentLineChars = 0;
}
}

void insertCharAt(char chr, size_t pos) {
}

void delCharAt(size_t pos) {
}

extern TextRasterizerCache *rasterizerCache;
extern std::unique_ptr<Scheduler> scheduler;

@@ -44,6 +74,7 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
onFocus=[this]() {
this->focused = true;
//std::cout << "focus input" << std::endl;
// blink cursor
this->cursorTimer = scheduler->setInterval([this]() {
this->showCursor = !this->showCursor;
@@ -56,6 +87,7 @@ InputComponent::InputComponent(const float rawX, const float rawY, const float r
this->win->renderDirty = true;
};
onBlur=[this]() {
//std::cout << "blur input" << std::endl;
if (this->cursorTimer != nullptr) {
scheduler->clearInterval(this->cursorTimer);
}
@@ -277,29 +309,105 @@ void InputComponent::resize(const int passedWindowWidth, const int passedWindowH
}
}

std::string InputComponent::getValue() {
return this->value;
}

void InputComponent::setValue(std::string newValue) {
this->value = newValue;
if (!this->multiLine) {
this->cursorCharX = static_cast<int>(this->value.size() & INT_MAX);
}
// if we pasted in a middle of block
}

void InputComponent::addChar(char c) {
value+=c;
value += c;
//std::cout << "InputComponent::addChar - was cursor at " << this->cursorCharX << "," << this->cursorCharY << std::endl;
if (c == '\r') {
if (this->multiLine) {
this->cursorCharY++;
this->cursorCharX = 0;
}
} else {
this->cursorCharX++;
}
//std::cout << "InputComponent::addChar - at " << (int)x << "," << (int)y << std::endl;
updateText();
}

void InputComponent::backSpace() {
value=value.substr(0, value.length() - 1);
char last = '\0';
if (value.length()) {
last = value.at(value.length() - 1);
}
value = value.substr(0, value.length() - 1);
//std::cout << "InputComponent::backSpace - was cursor at " << this->cursorCharX << "," << this->cursorCharY << std::endl;
if (last == '\r') {
if (this->multiLine) {
this->cursorCharY--;
if (this->cursorCharY < 0) this->cursorCharY = 0;
std::pair<int, int> lineData = getLine(this->value, this->cursorCharY);
this->cursorCharX = lineData.second - lineData.first;
}
} else {
this->cursorCharX--;
if (this->cursorCharX < 0) this->cursorCharX = 0;
}
//std::cout << "InputComponent::backSpace - at " << (int)x << "," << (int)y << std::endl;
updateText();
}

void InputComponent::updateCursor() {
std::cout << "InputComponent::updateCursor - cursor at " << this->cursorCharX << "," << this->cursorCharY << std::endl;
//std::cout << "InputComponent::updateCursor - text[" << this->value << "]" << std::endl;
std::pair<int, int> lineData = getLine(this->value, this->cursorCharY);
std::cout << "InputComponent::updateCursor - line " << this->cursorCharY << " starts at " << lineData.first << std::endl;
size_t pos = lineData.first + this->cursorCharX;
//std::cout << "InputComponent::updateCursor - cx " << this->cursorCharX << " starts at value pos " << pos << std::endl;

if (pos < this->value.size()) {
//std::cout << "InputComponent::updateCursor - Cursor at " << this->cursorCharX << "," << this->cursorCharY << " " << this->value.at(pos) << std::endl;
}
const std::shared_ptr<TextRasterizer> textRasterizer = rasterizerCache->loadFont(12, false); // fontSize, bold
rasterizationRequest request;
request.text = value.substr(0, pos);
std::cout << "InputComponent::updateCursor - sizeText[" << request.text << "]" << std::endl;
request.startX = x;
request.availableWidth = windowWidth;
request.sourceStartX = 0;
request.sourceStartY = 0;
if (this->multiLine) {
request.noWrap = false;
} else {
request.noWrap = true;
}
std::unique_ptr<sizeResponse> textInfo = textRasterizer->size(request);
std::cout << "InputComponent::updateCursor - response at [" << textInfo->endingX << "," << textInfo->endingY << "]" << std::endl;
this->cursorStartAtX = textInfo->endingX;
this->cursorStartAtY = textInfo->endingY;
if (textInfo->height > this->height) {
this->cursorStartAtY += textInfo->height - this->height;
}

// this is texture shader coordinates now (not text shader coords)
//
cursorBox->x = x + this->cursorStartAtX;
if (boundToPage) {
cursorBox->y = windowHeight + y + this->cursorStartAtY;
cursorBox->y = this->windowHeight + y + this->cursorStartAtY;
} else {
// + this->cursorStartAtY
cursorBox->y = y + 5;
cursorBox->y = this->y + 5;
}
//std::cout << "placing cursor at " << (int)cursorBox->x << "," << (int)cursorBox->y << std::endl;
cursorBox->resize(windowWidth, windowHeight);
cursorBox->resize(this->windowWidth, this->windowHeight);
// if we move the cursor, redraw immediately
this->showCursor = true;
if (this->win) {
this->win->renderDirty = true;
}
}

void InputComponent::updateText() {

+ 22
- 1
src/graphics/components/InputComponent.h View File

@@ -9,6 +9,22 @@

class Window;

class TextBlock {
public:
// methods
void addChar(char chr);
void delLastChar();
void insertCharAt(char chr, size_t pos);
void delCharAt(size_t pos);
std::pair<size_t, size_t> getXYPos(size_t pos);
size_t getPosXY(size_t x, size_t y);
// properties
size_t lines;
std::vector<size_t> lineChars;
size_t currentLineChars = 0;
std::string text;
};

class InputComponent : public BoxComponent {
public:
//: BoxComponent(rawX, rawY, rawWidth, rawHeight, passedWindowWidth, passedWindowHeight) { }
@@ -17,11 +33,12 @@ public:
//using BoxComponent::BoxComponent;
void resize(const int passedWindowWidth, const int passedWindowHeight);
void render();
std::string getValue();
void setValue(std::string newValue);
void addChar(char c);
void backSpace();
void updateCursor();
void updateText();
std::string value="";
TextComponent *userInputText = nullptr;
BoxComponent *cursorBox = nullptr;
std::function<void(std::string value)> onEnter = nullptr;
@@ -33,8 +50,12 @@ public:
// store for cursor
int cursorStartAtX;
int cursorStartAtY;
int cursorCharX = 0;
int cursorCharY = 0;
TagNode *node = nullptr;
bool multiLine = false; // textarea control
protected:
std::string value="";
};

#endif

+ 1
- 1
src/graphics/elements/INPUTElement.cpp View File

@@ -30,7 +30,7 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
inputComponent->name = "textInput";
auto propIter2 = tagNode->properties.find("value");
if (propIter2 != tagNode->properties.end()) {
inputComponent->value = propIter2->second;
inputComponent->setValue(propIter2->second);
}
return std::move(inputComponent);
} else if (type == "submit") {

Loading…
Cancel
Save