Browse Source

getLine(), handleResoruce request render, Smooth out scrolling/dead code removal, Input arrow key handling start, error pages

master
Odilitime 4 years ago
parent
commit
aae33ba5aa

+ 140
- 70
src/graphics/components/DocumentComponent.cpp View File

@@ -14,6 +14,42 @@
void deleteComponent(std::shared_ptr<Component> &component);
void deleteNode(std::shared_ptr<Node> node);

#include <utility>

// well we need to know how many chars in a lines
// we also need to know where a line starts (pos)
// lines start at 0 for first line
std::pair<size_t, size_t> getLine(std::string text, int findLine) {
//std::cout << "DocumentComponent.getLine [" << text << "] findLine: " << findLine << std::endl;
size_t pos = 0;
size_t lPos = 0;
pos = text.find("\r");
size_t line = 0;
while(pos != std::string::npos) {
lPos = pos;
pos = text.find("\r", lPos + 1);
if (line == findLine) {
//std::cout << "lPos: " << lPos << " pos: " << pos << " line: " << line << std::endl;
//std::cout << "DocumentComponent.getLine start " << (lPos + line + 1) << " end " << (pos == std::string::npos ? text.length() : (lPos + pos)) << std::endl;
return std::make_pair(lPos + line + 1, pos == std::string::npos ? text.length() : (lPos + pos));
}
line++;
}
//std::cout << "end lPos: " << lPos << " pos: " << pos << " line: " << line << std::endl;
//std::cout << "DocumentComponent.getLine result end of text" << findLine << std::endl;
return std::make_pair(lPos + line, text.size());
}

size_t getNumberOfLines(std::string text) {
size_t pos = text.find("\r");
size_t lines = 0;
while(pos != std::string::npos) {
lines++;
pos = text.find("\r", pos + 1);
}
return lines;
}

DocumentComponent::DocumentComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) : MultiComponent(rawX, rawY, rawWidth, rawHeight, passedWindowWidth, passedWindowHeight) {
//std::cout << "DocumentComponent::DocumentComponent" << std::endl;

@@ -62,68 +98,12 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
};
onWheel=[this](int passedX, int passedY) {
//std::cout << "DocumentComponent::DocumentComponent:::onWheel - scroll yDelta: " << passedY << std::endl;
//Component::printComponentTree(rootComponent, 0);
//double pY = passedY * 0.1;
/*
this->scrollY -= pY;
if (this->scrollY < 0) {
this->scrollY = 0;
} else if (this->scrollY > this->scrollHeight) {
this->scrollY = this->scrollHeight;
}
*/
//std::cout << "scroll pos: " << scrollY << "/" << scrollHeight << std::endl;
//std::cout << "y: " << static_cast<int>(this->y) << " - " << this->scrollY << std::endl;
//std::cout << "root.y: " << static_cast<int>(rootComponent->y) << std::endl;
//std::cout << "windowSize: " << windowWidth << "," << windowHeight << std::endl;
// new system
//std::cout << "DocumentComponent::DocumentComponent:::onWheel - old position: " << this->transformMatrix[13] << " adjustment:" << (-pY*0.1) << std::endl;
//this->transformMatrix[13] += -pY * 0.01;
this->scrollY(passedY * 0.1);
/*
// 2.0 is one screen height
// we draw from 0 downwards (y+), so can't scroll past our starting draw point
if (this->transformMatrix[13] < 2) {
this->transformMatrix[13] = 2;
}
// calculate scroll max by calculating how many screens are in the rootComponent's Height
if (this->transformMatrix[13] > std::max( (this->rootComponent->height - this->rootComponent->y) / this->windowHeight * 2.0f, 2.0f)) {
this->transformMatrix[13] = std::max( (this->rootComponent->height - this->rootComponent->y) / this->windowHeight * 2.0f, 2.0f);
}
this->transformMatrixDirty = true;
*/
/*
// adjust root position
rootComponent->y = this->y + this->scrollY;
//std::cout << "now root.y: " << static_cast<int>(rootComponent->y) << std::endl;
// reset root size
rootComponent->windowWidth = windowWidth;
rootComponent->windowHeight = windowHeight;
//Component::printComponentTree(rootComponent, 0);
// this takes so long, we may want to delay until the input is adjusted
const std::clock_t begin = clock();
rootComponent->layout(); // need to update the vertices
//renderDirty = true;
// should we mark win->renderDirty = true?
const std::clock_t end = clock();
std::cout << "Scrolled document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
*/

//rootComponent->y = this->y - this->scrollY;
//std::cout << "after root.y: " << static_cast<int>(rootComponent->y) << std::endl;
// don't need this - why not?
//this->renderDirty = true;
this->scrollY(passedY);
};
onMousedown=[this](int passedX, int passedY) {
//std::cout << "document left press" << std::endl;
if (this->hoverComponent) {
// if we're hovering over a component that's not focused
if (this->focusedComponent != this->hoverComponent) {
// blur old component
if (this->focusedComponent) {
@@ -136,6 +116,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
this->hoverComponent->onFocus();
}
}
// set hover component as focused
this->focusedComponent = this->hoverComponent;
if (this->focusedComponent->onMousedown) {
//std::cout << "click event" << std::endl;
@@ -181,14 +162,77 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
if (action == 0 || action == 2) {
// key up
// it's always uppercase...
if (key == 259) {
if (key == GLFW_KEY_BACKSPACE) {
focusedInputComponent->backSpace();
} else if (key == 257) {
} else if (key == GLFW_KEY_UP) {
/*
std::pair<int, int> lineData = getLine(focusedInputComponent->value, 0);
std::cout << "line 0 is " << focusedInputComponent->value.substr(lineData.first, lineData.second) << " start: " << lineData.first << " end: " << lineData.second << std::endl;
lineData = getLine(focusedInputComponent->value, 1);
std::cout << "line 1 is " << focusedInputComponent->value.substr(lineData.first, lineData.second) << " start: " << lineData.first << " end: " << lineData.second << std::endl;
*/
if (focusedInputComponent->multiLine) {
//std::pair<int, int> lineData = getLine(focusedInputComponent->value, 0);
focusedInputComponent->cursorCharY--;
if (focusedInputComponent->cursorCharY < 0) {
focusedInputComponent->cursorCharY = 0;
focusedInputComponent->cursorCharX = 0;
}
} else {
focusedInputComponent->cursorCharX = 0;
}
focusedInputComponent->updateCursor();
} else if (key == GLFW_KEY_DOWN) {
if (focusedInputComponent->multiLine) {
//std::pair<int, int> lineData = getLine(focusedInputComponent->value, 0);
focusedInputComponent->cursorCharY++;
size_t lines = getNumberOfLines(focusedInputComponent->getValue());
if (focusedInputComponent->cursorCharY > lines) {
focusedInputComponent->cursorCharY = lines;
focusedInputComponent->cursorCharX = focusedInputComponent->getValue().length();
}
} else {
focusedInputComponent->cursorCharX = static_cast<size_t>(focusedInputComponent->getValue().size());
}
focusedInputComponent->updateCursor();
} else if (key == GLFW_KEY_LEFT) {
focusedInputComponent->cursorCharX--;
if (focusedInputComponent->cursorCharX < 0) {
if (focusedInputComponent->multiLine) {
focusedInputComponent->cursorCharY--;
if (focusedInputComponent->cursorCharY < 0) {
focusedInputComponent->cursorCharY = 0;
// if was already at first line, no need to adjust x
} else {
std::pair<int, int> lineData = getLine(focusedInputComponent->getValue(), focusedInputComponent->cursorCharY);
// adjust Y
focusedInputComponent->cursorCharX = lineData.second; // end of this line
}
}
focusedInputComponent->cursorCharX = 0;
}
focusedInputComponent->updateCursor();
} else if (key == GLFW_KEY_RIGHT) {
focusedInputComponent->cursorCharX++;
if (focusedInputComponent->cursorCharX > focusedInputComponent->getValue().size()) {
if (focusedInputComponent->multiLine) {
focusedInputComponent->cursorCharY++;
size_t lines = getNumberOfLines(focusedInputComponent->getValue());
if (focusedInputComponent->cursorCharY > lines) {
focusedInputComponent->cursorCharY = lines;
} else {;
focusedInputComponent->cursorCharX = 0;
}
} else {
focusedInputComponent->cursorCharX = focusedInputComponent->getValue().size();
}
}
focusedInputComponent->updateCursor();
} else if (key == GLFW_KEY_ENTER) {
//std::cout << "DocumentComponent::onKeyUp - enter!" << std::endl;
if (focusedInputComponent->multiLine) {
// harfbuzz or freetype2 (something?) doesn't like \n
focusedInputComponent->value += "\r";
focusedInputComponent->updateText();
// harfbuzz or freetype2 (something?) doesn't like \n //focusedInputComponent->value += "\r";
focusedInputComponent->addChar('\r');
} else {
std::cout << "should submit form!" << std::endl;
}
@@ -255,10 +299,12 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
}

void DocumentComponent::scrollY(int py) {
this->transformMatrix[13] -= py;
this->textureTransformMatrix[13] -= py;
// we're going to reduce here, so we can get the full signal all the way here
this->transformMatrix[13] -= py * 0.01;
this->textureTransformMatrix[13] -= py * 0.01;
//std::cout << "transformMatrix : " << this->transformMatrix[13] << std::endl;
//std::cout << "textureTransformMatrix: " << this->textureTransformMatrix[13] << std::endl;

// check bounds
if (this->transformMatrix[13] < 2) {
this->transformMatrix[13] = 2;
@@ -317,7 +363,7 @@ void DocumentComponent::setDOM(const std::shared_ptr<Node> rootNode) {
rootComponent->y = y;
domRootNode = rootNode;
printNode(domRootNode, 0);
//std::cout << "DocumentComponent::setDOM - printing nodes" << endl; printNode(domRootNode, 0);
domDirty = true;
}

@@ -340,6 +386,9 @@ void DocumentComponent::render() {
transformMatrix[13]=std::max((rootComponent->height)/(windowHeight)*2.0f, 2.0f);
transformMatrixDirty = true;
}
// after we load in the document, allow scroll to work
this->updateMouse();
}
// we have to do this each time because window resets it
@@ -461,7 +510,25 @@ void DocumentComponent::navTo(const std::string url) {
void DocumentComponent::handleResource(WebResource &res, std::string url) {
if (res.resourceType == ResourceType::INVALID) {
logError() << "DocumentComponent::handleResource - Invalid resource type: " << res.raw << std::endl;
return;

std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
tagNode->tag="p";
// bind tag to root
tagNode->parent = rootNode;
rootNode->children.push_back(tagNode);
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
textNode->text = "Invalid Resource Type, HTTP Result Status: " + res.raw;
// bind text to tag
textNode->parent = tagNode;
tagNode->children.push_back(textNode);
// send NodeTree to window
//this->win->setDOM(rootNode);
this->setDOM(rootNode);
}
//std::cout << "body: " << res.raw << std::endl;
@@ -473,7 +540,7 @@ void DocumentComponent::handleResource(WebResource &res, std::string url) {
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "INPUTElement.handleResource - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
logDebug() << "DocumentComponent::handleResource - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// send NodeTree to window
//this->win->setDOM(rootNode);
@@ -486,7 +553,7 @@ void DocumentComponent::handleResource(WebResource &res, std::string url) {
}
this->setDOM(rootNode);
} else if (res.resourceType == ResourceType::TXT) {
std::cout << "INPUTElement.handleResource - Rendering text document" << std::endl;
std::cout << "DocumentComponent::handleResource - Rendering text document" << std::endl;
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
tagNode->tag="p";
@@ -506,6 +573,9 @@ void DocumentComponent::handleResource(WebResource &res, std::string url) {
//this->win->setDOM(rootNode);
this->setDOM(rootNode);
} else {
std::cout << "INPUTElement.handleResource - I don't know how to render non-html files" << std::endl;
std::cout << "DocumentComponent::handleResource - I don't know how to render non-html files" << std::endl;
}
if (this->win) {
this->win->renderDirty = true;
}
}

+ 2
- 0
src/graphics/components/DocumentComponent.h View File

@@ -10,6 +10,8 @@
#include "../../networking/HTTPResponse.h"
#include "../../WebResource.h"

std::pair<size_t, size_t> getLine(std::string text, int findLine);

// document is scrollable until multicomponent
class DocumentComponent : public MultiComponent {
public:

Loading…
Cancel
Save