Skip to content
Snippets Groups Projects
Commit 29161daa authored by Odilitime's avatar Odilitime
Browse files

MultiComponent refactor, delete*, setDOM, move out printComponent

parent 33592a42
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,10 @@
#include "InputComponent.h"
#include <ctime>
DocumentComponent::DocumentComponent(const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int passedWindowWidth, const int passedWindowHeight) {
void deleteComponent(std::shared_ptr<Component> &component);
void deleteNode(std::shared_ptr<Node> node);
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;
windowWidth = passedWindowWidth;
......@@ -50,7 +53,8 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
}
};
onWheel=[this](int passedX, int passedY) {
//std::cout << "scroll yDelta: " << y << std::endl;
//std::cout << "DocumentComponent::DocumentComponent:::onWheel - scroll yDelta: " << y << std::endl;
//Component::printComponentTree(rootComponent, 0);
double pY = passedY / 10;
this->scrollY -= pY;
if (this->scrollY < 0) {
......@@ -63,7 +67,7 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
//std::cout << "root.y: " << static_cast<int>(rootComponent->y) << std::endl;
//std::cout << "windowSize: " << windowWidth << "," << windowHeight << std::endl;
// ajdust root position
// adjust root position
rootComponent->y = this->y + this->scrollY;
//std::cout << "now root.y: " << static_cast<int>(rootComponent->y) << std::endl;
......@@ -71,18 +75,20 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
rootComponent->windowWidth = windowWidth;
rootComponent->windowHeight = windowHeight;
//printComponentTree(rootComponent, 0);
//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
// don't need this - why not?
//this->renderDirty = true;
};
onMousedown=[this](int passedX, int passedY) {
......@@ -178,35 +184,43 @@ DocumentComponent::DocumentComponent(const float rawX, const float rawY, const f
};
}
void DocumentComponent::resize(const int passedWindowWidth, const int passedWindowHeight) {
std::cout << "DocumentComponent::resize - relaying out" << std::endl;
windowWidth = passedWindowWidth;
windowHeight = passedWindowHeight;
//this->printComponentTree(rootComponent, 0);
rootComponent->windowWidth = passedWindowWidth;
rootComponent->windowHeight = passedWindowHeight;
rootComponent->layout();
//this->printComponentTree(rootComponent, 0);
void deleteComponent(std::shared_ptr<Component> &component) {
// delete all my child first
for (std::shared_ptr<Component> child : component->children) {
deleteComponent(child);
}
component->parent=nullptr;
component->previous=nullptr;
component->children.clear();
// now delete self
}
void DocumentComponent::printComponentTree(const std::shared_ptr<Component> &component, int depth) {
for (int i = 0; i < depth; i++) {
std::cout << '\t';
}
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
if (textComponent) {
std::cout << std::fixed << "X: " << static_cast<int>(textComponent->x) << " Y: " << static_cast<int>(textComponent->y) << " WIDTH: " << static_cast<int>(textComponent->width) << " HEIGHT: " << static_cast<int>(textComponent->height) << " INLINE: " << textComponent->isInline << " TEXT: " << textComponent->text << std::endl;
void deleteNode(std::shared_ptr<Node> node) {
for (std::shared_ptr<Node> child : node->children) {
deleteNode(child);
}
else {
std::cout << std::fixed << "X: " << static_cast<int>(component->x) << " Y: " << static_cast<int>(component->y) << " WIDTH: " << static_cast<int>(component->width) << " HEIGHT: " << static_cast<int>(component->height) << " INLINE: " << component->isInline << std::endl;
node->parent=nullptr;
node->children.clear();
node->component=nullptr; // disassociate component
}
void DocumentComponent::setDOM(const std::shared_ptr<Node> rootNode) {
// reset rootComponent
if (rootComponent) {
deleteComponent(rootComponent);
}
for (std::shared_ptr<Component> child : component->children) {
printComponentTree(child, depth + 1);
if (domRootNode) {
deleteNode(domRootNode);
}
// reset scroll position ?
// new root component
rootComponent = std::make_shared<Component>();
rootComponent->name = "rootComponent of " + name;
rootComponent->y = y;
domRootNode = rootNode;
domDirty = true;
}
void DocumentComponent::render() {
......@@ -215,25 +229,25 @@ void DocumentComponent::render() {
const std::clock_t begin = clock();
createComponentTree(domRootNode, rootComponent);
const std::clock_t end = clock();
// root component here doesn't have any children...
std::cout << "built & laid out document components in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
//printComponentTree(rootComponent, 0);
//Component::printComponentTree(rootComponent, 0);
domDirty = false;
renderDirty = true;
//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))));
}
if (renderDirty) {
glUseProgram(win->textureProgram);
renderBoxComponents(rootComponent);
glUseProgram(win->fontProgram);
renderComponents(rootComponent);
}
//std::cout << "DocumentComponent::render - renderDirty" << std::endl;
glUseProgram(win->textureProgram);
renderBoxComponents(rootComponent);
glUseProgram(win->fontProgram);
renderComponents(rootComponent);
}
// 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, windowWidth, windowHeight);
//std::cout << "DocumentComponent::createComponentTree" << std::endl;
if (!component) {
//std::cout << "DocumentComponent::createComponentTree - no component" << std::endl;
return;
......@@ -249,65 +263,7 @@ void DocumentComponent::createComponentTree(const std::shared_ptr<Node> node, co
}
}
// draw this component and all it's children
void DocumentComponent::renderComponents(std::shared_ptr<Component> component) {
if (!component) {
std::cout << "DocumentComponent::renderComponents - got null passed" << std::endl;
return;
}
TextComponent *textComponent = dynamic_cast<TextComponent*>(component.get());
if (textComponent) {
textComponent->render();
}
// is this needed?
if (component->children.empty()) {
return;
}
for (std::shared_ptr<Component> &child : component->children) {
renderComponents(child);
}
}
void DocumentComponent::renderBoxComponents(std::shared_ptr<Component> component) {
if (!component) {
std::cout << "DocumentComponent::renderBoxComponents - got null passed" << std::endl;
return;
}
// render non-text components too
BoxComponent *boxComponent = dynamic_cast<BoxComponent*>(component.get());
//inputComponent->render();
if (boxComponent) {
/*
GLenum glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "Window::renderBoxComponents - box render start - not ok: " << glErr << std::endl;
}
*/
/*
glUseProgram(textureProgram);
glErr=glGetError();
if(glErr != GL_NO_ERROR) {
std::cout << "glUseProgram - not ok: " << glErr << std::endl;
}
*/
//std::cout << "Window::renderBoxComponents - Rendering at " << (int)boxComponent->x << "x" << (int)boxComponent->y << std::endl;
boxComponent->render();
}
/*
InputComponent *inputComponent = dynamic_cast<InputComponent*>(component.get());
if (inputComponent) {
glUseProgram(textureProgram);
inputComponent->render();
}
*/
// is this needed?
if (component->children.empty()) {
return;
}
for (std::shared_ptr<Component> &child : component->children) {
renderBoxComponents(child);
}
}
// used for picking
std::shared_ptr<Component> DocumentComponent::searchComponentTree(const std::shared_ptr<Component> &component, const int passedX, const int passedY) {
......
......@@ -9,29 +9,20 @@
#include "../../URL.h"
#include "../../networking/HTTPResponse.h"
class DocumentComponent : public Component {
#include "MultiComponent.h"
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 printComponentTree(const std::shared_ptr<Component> &component, int depth);
void render();
void createComponentTree(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent);
void renderComponents(std::shared_ptr<Component> component);
void renderBoxComponents(std::shared_ptr<Component> component);
std::shared_ptr<Component> searchComponentTree(const std::shared_ptr<Component> &component, const int x, const int y);
void navTo(const std::string url);
void resize(const int passedWindowWidth, const int passedWindowHeight);
void setDOM(const std::shared_ptr<Node> rootNode);
std::shared_ptr<Node> domRootNode = nullptr;
ComponentBuilder componentBuilder;
std::shared_ptr<Component> rootComponent = std::make_shared<Component>();
URL currentURL;
bool domDirty = false;
bool renderDirty = false;
// we'll need a way to pass events down and up
// also how do we handle scroll?
// we'll need searchComponentTree for picking
Window *win;
std::shared_ptr<Component> hoverComponent = nullptr;
std::shared_ptr<Component> focusedComponent = nullptr;
int scrollY = 0;
int scrollHeight = 0;
};
......@@ -39,6 +30,5 @@ public:
//bool setWindowContent(URL const& url);
//void handleRequest(const HTTPResponse &response);
extern const std::unique_ptr<Window> window;
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment