Browse Source

textureTransformMatrix for boxcomponent hwaccel scrolling, scrollY refactor, extract button label, button hover fix

master
Odilitime 5 years ago
parent
commit
c63ce64201
  1. 122
      src/graphics/components/DocumentComponent.cpp
  2. 11
      src/graphics/components/DocumentComponent.h

122
src/graphics/components/DocumentComponent.cpp

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#include <iostream>
#include "../../Log.h"
#include "InputComponent.h"
#include "ButtonComponent.h"
#include "TabbedComponent.h"
#include <ctime>
@ -50,7 +51,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -50,7 +51,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
// this could communicate the cursor to use
this->hoverComponent->onMousemove(passedX, passedY);
}
if (this->hoverComponent->onClick) {
if (this->hoverComponent->onClick || typeOfComponent(this->hoverComponent) == "button") {
glfwSetCursor(this->win->window, this->win->cursorHand);
} else {
glfwSetCursor(this->win->window, this->win->cursorIbeam);
@ -60,15 +61,17 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -60,15 +61,17 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
}
};
onWheel=[this](int passedX, int passedY) {
//std::cout << "DocumentComponent::DocumentComponent:::onWheel - scroll yDelta: " << y << std::endl;
//std::cout << "DocumentComponent::DocumentComponent:::onWheel - scroll yDelta: " << passedY << std::endl;
//Component::printComponentTree(rootComponent, 0);
double pY = passedY / 10.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;
@ -76,8 +79,10 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -76,8 +79,10 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
// 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->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) {
@ -89,6 +94,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -89,6 +94,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
}
this->transformMatrixDirty = true;
*/
/*
// adjust root position
rootComponent->y = this->y + this->scrollY;
@ -165,17 +171,18 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -165,17 +171,18 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
}
}
};
onKeyup=[this](int key, int scancode, int action, int mods) {
onKeyUp=[this](int key, int scancode, int action, int mods) {
//std::cout << "DocumentComponent::DocumentComponent:onKeyup" << typeOfComponent(this->focusedComponent) << std::endl;
InputComponent *inputComponent = dynamic_cast<InputComponent*>(this->focusedComponent.get());
if (inputComponent) {
InputComponent *focusedInputComponent = dynamic_cast<InputComponent*>(this->focusedComponent.get());
if (focusedInputComponent) {
//std::cout << "inputComponent is focused, key pressed " << key << " action: " <<action << std::endl;
// FIXME: not going to get repeat events here...
// action 1 is down, 0 is up, 2 is a repeat
if (action == 0 || action == 2) {
// key up
// it's always uppercase...
if (key == 259) {
inputComponent->backSpace();
focusedInputComponent->backSpace();
} else if (key == 257) {
std::cout << "enter!" << std::endl;
} else {
@ -201,12 +208,65 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f @@ -201,12 +208,65 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
key += 'a' - 'A';
}
}
inputComponent->addChar(key);
focusedInputComponent->addChar(key);
} // otherwise I think it's some weird control char
}
}
}
};
onKeyPress=[this](int key, int scancode, int action, int mods) {
InputComponent *focusedInputComponent = dynamic_cast<InputComponent*>(this->focusedComponent.get());
if (focusedInputComponent) {
return;
}
std::cout << "DocumentComponent onKeyPresss - no input component focused" << std::endl;
int yOffsetScroll = 1;
if (key == GLFW_KEY_PAGE_UP && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
std::cout << "PgUp is/was pressed. Scrolling down." << std::endl;
this->scrollY(-yOffsetScroll*0.1);
}
if (key == GLFW_KEY_PAGE_DOWN && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
std::cout << "PgDn is/was pressed. Scrolling up." << std::endl;
this->scrollY(yOffsetScroll*0.1);
}
// FIXME Scrolling with this can scroll past boundary whereas the same doesn't happen with
// scrolling wheel
if (key == GLFW_KEY_J && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
std::cout << "J is/was pressed. Scrolling down." << std::endl;
this->scrollY(-yOffsetScroll*0.1);
}
// FIXME Scrolling with this can scroll past boundary whereas the same doesn't happen with
// scrolling wheel
if (key == GLFW_KEY_K && (action == GLFW_PRESS || action == GLFW_REPEAT)) {
std::cout << "K is/was pressed. Scrolling up." << std::endl;
this->scrollY(yOffsetScroll*0.1);
}
};
}
void DocumentComponent::scrollY(int y) {
this->transformMatrix[13] -= y;
this->textureTransformMatrix[13] -= y;
//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;
}
if (this->textureTransformMatrix[13] < 0) {
this->textureTransformMatrix[13] = 0;
}
// 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);
}
if (this->textureTransformMatrix[13] > std::max( (this->rootComponent->height - this->rootComponent->y) / this->windowHeight * 2.0f, 2.0f) - 2.0) {
this->textureTransformMatrix[13] = std::max( (this->rootComponent->height - this->rootComponent->y) / this->windowHeight * 2.0f, 2.0f) - 2.0;
}
this->transformMatrixDirty = true;
}
void deleteComponent(std::shared_ptr<Component> &component) {
@ -250,6 +310,7 @@ void DocumentComponent::setDOM(const std::shared_ptr<Node> rootNode) { @@ -250,6 +310,7 @@ void DocumentComponent::setDOM(const std::shared_ptr<Node> rootNode) {
rootComponent->y = y;
domRootNode = rootNode;
//printNode(domRootNode, 0);
domDirty = true;
}
@ -265,7 +326,7 @@ void DocumentComponent::render() { @@ -265,7 +326,7 @@ void DocumentComponent::render() {
//Component::printComponentTree(rootComponent, 0);
domDirty = false;
//std::cout << "root Height: " << static_cast<int>(rootComponent->height) << " window Height: " << windowHeight << " y " << static_cast<int>(this->y) << std::endl;
scrollHeight = std::max(0, static_cast<int>(rootComponent->height - (windowHeight + (this->y * 2))));
//scrollHeight = std::max(0, static_cast<int>(rootComponent->height - (windowHeight + (this->y * 2))));
// recalculate scroll max by calculating how many screens are in the rootComponent's Height
if (transformMatrix[13]>std::max((rootComponent->height)/(windowHeight)*2.0f, 2.0f)) {
@ -274,8 +335,7 @@ void DocumentComponent::render() { @@ -274,8 +335,7 @@ void DocumentComponent::render() {
}
}
// we have to do this each time
// because window resets it
// we have to do this each time because window resets it
//if (transformMatrixDirty) {
//const std::clock_t begin = clock();
@ -285,31 +345,50 @@ void DocumentComponent::render() { @@ -285,31 +345,50 @@ void DocumentComponent::render() {
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
//const std::clock_t end = clock();
//std::cout << "Updated font matrix in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
Shader *textureShader = this->win->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
GLint transformLocation2 = textureShader->uniform("transform");
glUniformMatrix4fv(transformLocation2, 1, GL_FALSE, transformMatrix);
Shader *textureShader = this->win->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
transformMatrixDirty = false;
//}
//std::cout << "DocumentComponent::render - renderDirty" << std::endl;
textureShader->bind();
GLint transformLocation2 = textureShader->uniform("transform");
glUniformMatrix4fv(transformLocation2, 1, GL_FALSE, textureTransformMatrix);
//std::cout << "DocumentComponent::render - start Box components" << std::endl;
renderBoxComponents(rootComponent);
//std::cout << "DocumentComponent::render - end Box components" << std::endl;
textureShader->release();
fontShader->bind();
//std::cout << "DocumentComponent::render - start components" << std::endl;
renderComponents(rootComponent);
//std::cout << "DocumentComponent::render - end components" << std::endl;
fontShader->release();
}
// create this component and all it's children
void DocumentComponent::createComponentTree(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent) {
std::shared_ptr<Component> component = componentBuilder.build(node, parentComponent, this->win);
std::shared_ptr<Component> component = componentBuilder.build(node, parentComponent, this->win, this);
//std::cout << "DocumentComponent::createComponentTree" << std::endl;
if (!component) {
//std::cout << "DocumentComponent::createComponentTree - no component" << std::endl;
return;
}
// don't build text node of button component because it'll mess with the picking
if (typeOfComponent(component) == "button") {
TextNode *textNode = dynamic_cast<TextNode*>(node->children.front().get());
if (textNode) {
//std::cout << "Button text: " << textNode->text << std::endl;
ButtonComponent *buttonComponent = dynamic_cast<ButtonComponent*>(component.get());
if (buttonComponent) {
buttonComponent->value = textNode->text;
buttonComponent->updateText();
}
}
return;
}
if (node==domRootNode) {
// if this is the root node
component->reqWidth = windowWidth;
@ -325,18 +404,18 @@ void DocumentComponent::createComponentTree(const std::shared_ptr<Node> node, co @@ -325,18 +404,18 @@ void DocumentComponent::createComponentTree(const std::shared_ptr<Node> node, co
// used for picking
std::shared_ptr<Component> DocumentComponent::searchComponentTree(const std::shared_ptr<Component> &component, const int passedX, const int passedY) {
// only get hits on leaves (and not to hit the rootComponent every time)
if (component->children.empty()) {
//std::cout << "DocumentComponent::searchComponentTree - component at " << static_cast<int>(component->x) << "," << static_cast<int>(component->y) << " size " << static_cast<int>(component->width) << "," << static_cast<int>(component->height) << std::endl;
//std::cout << "DocumentComponent::searchComponentTree - y search: " << static_cast<int>(-component->y) << "<" << static_cast<int>(passedY) << "<" << static_cast<int>(-component->y + component->height) << std::endl;
if (-component->y < passedY && -component->y + component->height > passedY) {
//std::cout << "DocumentComponent::searchComponentTree - x search: " << static_cast<int>(component->x) << "<" << static_cast<int>(passedX) << "<" << static_cast<int>(component->x + component->width) << std::endl;
if (component->x < passedX && component->x + component->width > passedX) {
//std::cout << "DocumentComponent::searchComponentTree - hit " << typeOfComponent(component) << std::endl;
//std::cout << "DocumentComponent::searchComponentTree - hit " << typeOfComponent(component) << " name: " << component->name << std::endl;
return component;
}
}
}
else {
} else {
for (std::shared_ptr<Component> child : component->children) {
std::shared_ptr<Component> found = searchComponentTree(child, passedX, passedY);
if (found) {
@ -380,6 +459,7 @@ void DocumentComponent::navTo(const std::string url) { @@ -380,6 +459,7 @@ void DocumentComponent::navTo(const std::string url) {
// send NodeTree to window
//this->win->setDOM(rootNode);
//printNode(rootNode, 0);
// we need a way to communicate with our tabComponent
// maybe an event is best

11
src/graphics/components/DocumentComponent.h

@ -14,6 +14,7 @@ class DocumentComponent : public MultiComponent { @@ -14,6 +14,7 @@ class DocumentComponent : public MultiComponent {
public:
DocumentComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight);
void render();
void scrollY(int y);
void createComponentTree(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent);
std::shared_ptr<Component> searchComponentTree(const std::shared_ptr<Component> &component, const int x, const int y);
void navTo(const std::string url);
@ -23,9 +24,15 @@ public: @@ -23,9 +24,15 @@ public:
URL currentURL;
bool domDirty = false;
//
int scrollY = 0;
int scrollHeight = 0;
//int scrollY = 0;
//int scrollHeight = 0;
float textureTransformMatrix[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
float transformMatrix[16] = {
1, 0, 0, 0,
0, 1, 0, 0,

Loading…
Cancel
Save