Browse Source

onBeforeLoad(), navTo flushed out, new componentBuilder prototype, remove window global

master
Odilitime 5 years ago
parent
commit
6ee0425d88
  1. 70
      src/graphics/components/DocumentComponent.cpp
  2. 8
      src/graphics/components/DocumentComponent.h

70
src/graphics/components/DocumentComponent.cpp

@ -3,9 +3,12 @@ @@ -3,9 +3,12 @@
#include <iostream>
#include "../../Log.h"
#include "InputComponent.h"
#include "TabbedComponent.h"
#include <ctime>
#include "../opengl/Shader.h"
#include "../../WebResource.h"
#include "../../html/HTMLParser.h"
void deleteComponent(std::shared_ptr<Component> &component);
void deleteNode(std::shared_ptr<Node> node);
@ -276,13 +279,13 @@ void DocumentComponent::render() { @@ -276,13 +279,13 @@ void DocumentComponent::render() {
//if (transformMatrixDirty) {
//const std::clock_t begin = clock();
Shader *fontShader = window->shaderLoader.getShader(VertexShader("FontShader.vert"),
Shader *fontShader = this->win->shaderLoader.getShader(VertexShader("FontShader.vert"),
FragmentShader("FontShader.frag"));
GLint transformLocation = fontShader->uniform("transform");
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 = window->shaderLoader.getShader(VertexShader("TextureShader.vert"),
Shader *textureShader = this->win->shaderLoader.getShader(VertexShader("TextureShader.vert"),
FragmentShader("TextureShader.frag"));
GLint transformLocation2 = textureShader->uniform("transform");
glUniformMatrix4fv(transformLocation2, 1, GL_FALSE, transformMatrix);
@ -301,7 +304,7 @@ void DocumentComponent::render() { @@ -301,7 +304,7 @@ void DocumentComponent::render() {
// 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::shared_ptr<Component> component = componentBuilder.build(node, parentComponent, this->win);
//std::cout << "DocumentComponent::createComponentTree" << std::endl;
if (!component) {
//std::cout << "DocumentComponent::createComponentTree - no component" << std::endl;
@ -348,7 +351,64 @@ std::shared_ptr<Component> DocumentComponent::searchComponentTree(const std::sha @@ -348,7 +351,64 @@ std::shared_ptr<Component> DocumentComponent::searchComponentTree(const std::sha
// preparation for multiple HTML documents
void DocumentComponent::navTo(const std::string url) {
logDebug() << "DocumentComponent::navTo(" << url << ")" << std::endl;
currentURL = currentURL.merge(URL(url));
this->currentURL = currentURL.merge(URL(url));
logDebug() << "DocumentComponent::navTo - go to: " << currentURL << std::endl;
setWindowContent(currentURL);
//setWindowContent(currentURL);
logDebug() << "main::setWindowContent - " << url << std::endl;
// integrity check
TabbedComponent *tabComponent = dynamic_cast<TabbedComponent*>(this->win->tabComponent.get());
if (!tabComponent->tabs.size()) {
std::cout << "DocumentComponent::navTo - There's a document when there's not tabs" << std::endl;
}
// download URL
WebResource res = getWebResource(url);
if (res.resourceType == ResourceType::INVALID) {
logError() << "Invalid resource type: " << res.raw << std::endl;
return;
}
// parse HTML
if (res.resourceType == ResourceType::HTML) {
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "main::setWindowContent - 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);
// we need a way to communicate with our tabComponent
// maybe an event is best
if (this->onBeforeLoad) {
this->onBeforeLoad(url);
}
this->setDOM(rootNode);
} else if (res.resourceType == ResourceType::TXT) {
std::cout << "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";
// bind tag to root
tagNode->parent = rootNode;
rootNode->children.push_back(tagNode);
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
textNode->text = 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);
} else {
std::cout << "setWindowContent() - I don't know how to render non-html files" << std::endl;
}
}

8
src/graphics/components/DocumentComponent.h

@ -3,14 +3,12 @@ @@ -3,14 +3,12 @@
#include <GL/glew.h>
#include "Component.h"
#include "MultiComponent.h"
#include "ComponentBuilder.h"
#include "../opengl/Window.h"
#include "../../html/Node.h"
#include "../../URL.h"
#include "../../networking/HTTPResponse.h"
#include "MultiComponent.h"
// document is scrollable until multicomponent
class DocumentComponent : public MultiComponent {
public:
@ -35,10 +33,8 @@ public: @@ -35,10 +33,8 @@ public:
0, 0, 0, 1
};
bool transformMatrixDirty = true;
std::function<void(std::string url)> onBeforeLoad = nullptr;
};
//bool setWindowContent(URL const& url);
//void handleRequest(const HTTPResponse &response);
#endif

Loading…
Cancel
Save