Browse Source

Elements can now be more than just a render function

pull/1/head
gyroninja 5 years ago
parent
commit
6aaaff4384
  1. 1
      src/graphics/components/Component.h
  2. 53
      src/graphics/components/ComponentBuilder.cpp
  3. 8
      src/graphics/components/ComponentBuilder.h
  4. 2
      src/graphics/elements/AElement.cpp
  5. 8
      src/graphics/elements/AElement.h
  6. 2
      src/graphics/elements/BLOCKQUOTEElement.cpp
  7. 7
      src/graphics/elements/BLOCKQUOTEElement.h
  8. 4
      src/graphics/elements/Element.cpp
  9. 16
      src/graphics/elements/Element.h
  10. 2
      src/graphics/elements/H1Element.cpp
  11. 7
      src/graphics/elements/H1Element.h
  12. 2
      src/graphics/elements/H2Element.cpp
  13. 7
      src/graphics/elements/H2Element.h
  14. 2
      src/graphics/elements/H3Element.cpp
  15. 7
      src/graphics/elements/H3Element.h
  16. 2
      src/graphics/elements/LIElement.cpp
  17. 7
      src/graphics/elements/LIElement.h
  18. 4
      src/graphics/elements/PElement.cpp
  19. 7
      src/graphics/elements/PElement.h
  20. 5
      src/graphics/elements/SPANElement.cpp
  21. 8
      src/graphics/elements/SPANElement.h
  22. 13
      src/graphics/opengl/Window.cpp
  23. 2
      src/graphics/opengl/Window.h
  24. 5
      src/html/Node.h

1
src/graphics/components/Component.h

@ -8,6 +8,7 @@ public: @@ -8,6 +8,7 @@ public:
float y;
float height;
float width;
bool isInline = false;
};
#endif

53
src/graphics/components/ComponentBuilder.cpp

@ -1,42 +1,49 @@ @@ -1,42 +1,49 @@
#include "ComponentBuilder.h"
#include <iostream>
const ElementRendererMap ComponentBuilder::tagRenderers {
};
//const ElementRendererMap ComponentBuilder::tagRenderers {
//};
//
//const ElementRendererMap ComponentBuilder::textRenderers {
//};
const ElementRendererMap ComponentBuilder::textRenderers {
{"a", &AElement::render},
{"blockquote", &BLOCKQUOTEElement::render},
{"h1", &H1Element::render},
{"h2", &H2Element::render},
{"h3", &H3Element::render},
{"li", &LIElement::render},
{"p", &PElement::render},
{"span", &SPANElement::render}
const std::unordered_map<std::string, std::shared_ptr<Element>> ComponentBuilder::elementMap {
{"a", std::make_shared<AElement>()},
{"blockquote", std::make_shared<BLOCKQUOTEElement>()},
{"h1", std::make_shared<H1Element>()},
{"h2", std::make_shared<H2Element>()},
{"h3", std::make_shared<H3Element>()},
{"li", std::make_shared<LIElement>()},
{"p", std::make_shared<PElement>()},
{"span", std::make_shared<SPANElement>()}
};
std::unique_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> node, const std::unique_ptr<Component> &parentComponent, int windowWidth, int windowHeight) {
std::unique_ptr<Component> component;
std::string tag;
if (node->nodeType == NodeType::TAG) {
TagNode *tagNode = dynamic_cast<TagNode*>(node.get());
if (tagNode) {
ElementRendererMap::const_iterator tagRenderer = tagRenderers.find(tagNode->tag);
if (tagRenderer != tagRenderers.end()) {
component = tagRenderer->second(*tagNode, y, windowWidth, windowHeight);
}
tag = tagNode->tag;
}
}
else if (node->nodeType == NodeType::TEXT) {
TagNode *tagNode = dynamic_cast<TagNode*>(node->parent.get());
if (tagNode) {
ElementRendererMap::const_iterator textRenderer = textRenderers.find(tagNode->tag);
if (textRenderer != textRenderers.end()) {
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
component = textRenderer->second(*textNode, y, windowWidth, windowHeight);
}
}
tag = tagNode->tag;
}
}
std::unordered_map<std::string, std::shared_ptr<Element>>::const_iterator elementPair = elementMap.find(tag);
if (elementPair != elementMap.end()) {
std::shared_ptr<Element> element = elementPair->second;
int y = parentComponent != nullptr ? (parentComponent->y + (!element->isInline ? 0 : parentComponent->y)) : 0;
y += 500;
std::cout << tag << std::endl;
std::cout << y << std::endl;
component = element->renderer(*node.get(), y, windowWidth, windowHeight);
}
return component;
}

8
src/graphics/components/ComponentBuilder.h

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include <memory>
#include <unordered_map>
#include "Component.h"
#include "../elements/Element.h"
#include "../elements/AElement.h"
#include "../elements/BLOCKQUOTEElement.h"
#include "../elements/H1Element.h"
@ -21,10 +22,11 @@ typedef std::unordered_map<std::string, std::function<std::unique_ptr<Component> @@ -21,10 +22,11 @@ typedef std::unordered_map<std::string, std::function<std::unique_ptr<Component>
class ComponentBuilder {
private:
const static ElementRendererMap tagRenderers;
const static ElementRendererMap textRenderers;
// const static ElementRendererMap tagRenderers;
// const static ElementRendererMap textRenderers;
const static std::unordered_map<std::string, std::shared_ptr<Element>> elementMap;
public:
std::unique_ptr<Component> build(const std::shared_ptr<Node> node, int y, int windowWidth, int windowHeight);
std::unique_ptr<Component> build(const std::shared_ptr<Node> node, const std::unique_ptr<Component> &parentComponent, int windowWidth, int windowHeight);
};
#endif

2
src/graphics/elements/AElement.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "AElement.h"
std::unique_ptr<Component> AElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> AElement::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x00FFFF, windowWidth, windowHeight);
}

8
src/graphics/elements/AElement.h

@ -1,13 +1,15 @@ @@ -1,13 +1,15 @@
#ifndef AELEMENT_H
#define AELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class AElement {
class AElement : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
bool isInline = true;
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

2
src/graphics/elements/BLOCKQUOTEElement.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "BLOCKQUOTEElement.h"
std::unique_ptr<Component> BLOCKQUOTEElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> BLOCKQUOTEElement::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

7
src/graphics/elements/BLOCKQUOTEElement.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef BLOCKQUOTEELEMENT_H
#define BLOCKQUOTEELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class BLOCKQUOTEElement {
class BLOCKQUOTEElement : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

4
src/graphics/elements/Element.cpp

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
#include "Element.h"
Element::~Element() {
}

16
src/graphics/elements/Element.h

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
#ifndef ELEMENT_H
#define ELEMENT_H
#include <functional>
#include <memory>
#include "../components/Component.h"
#include "../../html/Node.h"
class Element {
public:
bool isInline = false;
virtual ~Element();
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif

2
src/graphics/elements/H1Element.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "H1Element.h"
std::unique_ptr<Component> H1Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> H1Element::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 24, true, 0x000000FF, windowWidth, windowHeight);
}

7
src/graphics/elements/H1Element.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef H1ELEMENT_H
#define H1ELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H1Element {
class H1Element : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

2
src/graphics/elements/H2Element.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "H2Element.h"
std::unique_ptr<Component> H2Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> H2Element::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 18, true, 0x000000FF, windowWidth, windowHeight);
}

7
src/graphics/elements/H2Element.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef H2ELEMENT_H
#define H2ELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H2Element {
class H2Element : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

2
src/graphics/elements/H3Element.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "H3Element.h"
std::unique_ptr<Component> H3Element::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> H3Element::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 14, true, 0x000000FF, windowWidth, windowHeight); // Should be 14.04pt
}

7
src/graphics/elements/H3Element.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef H3ELEMENT_H
#define H3ELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class H3Element {
class H3Element : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

2
src/graphics/elements/LIElement.cpp

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
#include "LIElement.h"
std::unique_ptr<Component> LIElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> LIElement::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>("" + static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

7
src/graphics/elements/LIElement.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef LIELEMENT_H
#define LIELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class LIElement {
class LIElement : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

4
src/graphics/elements/PElement.cpp

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
#include "PElement.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
std::unique_ptr<Component> PElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> PElement::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
}

7
src/graphics/elements/PElement.h

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
#ifndef PELEMENT_H
#define PELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class PElement {
class PElement : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

5
src/graphics/elements/SPANElement.cpp

@ -1,8 +1,9 @@ @@ -1,8 +1,9 @@
#include "SPANElement.h"
std::unique_ptr<Component> SPANElement::render(const Node &node, int y, int windowWidth, int windowHeight) {
std::unique_ptr<Component> SPANElement::renderer(const Node &node, int y, int windowWidth, int windowHeight) {
if (node.parent->children.size() == 1) {
return std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
std::unique_ptr<Component> component = std::make_unique<TextComponent>(static_cast<const TextNode&>(node).text, 0, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return component;
}
return nullptr;
}

8
src/graphics/elements/SPANElement.h

@ -1,13 +1,15 @@ @@ -1,13 +1,15 @@
#ifndef SPANELEMENT_H
#define SPANELEMENT_H
#include "Element.h"
#include "../components/Component.h"
#include "../components/TextComponent.h"
#include "../../html/TextNode.h"
class SPANElement {
class SPANElement : public Element {
public:
static std::unique_ptr<Component> render(const Node &node, int y, int windowWidth, int windowHeight);
bool isInline = true;
virtual std::unique_ptr<Component> renderer(const Node &node, int y, int windowWidth, int windowHeight);
};
#endif
#endif

13
src/graphics/opengl/Window.cpp

@ -157,7 +157,7 @@ GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentSh @@ -157,7 +157,7 @@ GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentSh
void Window::render() {
if (domDirty) {
const std::clock_t begin = clock();
drawNode(domRootNode);
drawNode(domRootNode, nullptr);
const std::clock_t end = clock();
std::cout << "Parsed dom in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
@ -188,13 +188,12 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) { @@ -188,13 +188,12 @@ void Window::setDOM(const std::shared_ptr<Node> rootNode) {
domDirty = true;
}
void Window::drawNode(const std::shared_ptr<Node> node) {
std::unique_ptr<Component> component = componentBuilder.build(node, y, windowWidth, windowHeight);
void Window::drawNode(const std::shared_ptr<Node> node, const std::unique_ptr<Component> &parentComponent) {
std::unique_ptr<Component> component = componentBuilder.build(node, parentComponent, windowWidth, windowHeight);
for (std::shared_ptr<Node> child : node->children) {
drawNode(child, component);
}
if (component) {
y -= component->height;
components.push_back(std::move(component));
}
for (std::shared_ptr<Node> child : node->children) {
drawNode(child);
}
}

2
src/graphics/opengl/Window.h

@ -25,7 +25,7 @@ public: @@ -25,7 +25,7 @@ public:
GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const;
void render();
void setDOM(const std::shared_ptr<Node> rootNode);
void drawNode(const std::shared_ptr<Node> rootNode);
void drawNode(const std::shared_ptr<Node> rootNode, const std::unique_ptr<Component> &parentComponent);
void repositionNode(const std::shared_ptr<Node> rootNode);
float transformMatrix[16] = {
1, 0, 0, 0,

5
src/html/Node.h

@ -21,9 +21,4 @@ public: @@ -21,9 +21,4 @@ public:
std::shared_ptr<Component> component;
};
struct Element {
std::string tag;
std::function<std::unique_ptr<Component>(const Node &node, int y, int windowWidth, int windowHeight)> render;
};
#endif

Loading…
Cancel
Save