Browse Source

deprecate block and p element infavor of div, build now takes win instead of size, uses new element render api, remove window global

master
Odilitime 5 years ago
parent
commit
32247ac066
  1. 62
      src/graphics/components/ComponentBuilder.cpp
  2. 7
      src/graphics/components/ComponentBuilder.h

62
src/graphics/components/ComponentBuilder.cpp

@ -2,12 +2,10 @@ @@ -2,12 +2,10 @@
#include <iostream>
#include "../elements/AElement.h"
#include "../elements/BLOCKQUOTEElement.h"
#include "../elements/H1Element.h"
#include "../elements/H2Element.h"
#include "../elements/H3Element.h"
#include "../elements/LIElement.h"
#include "../elements/PElement.h"
#include "../elements/SPANElement.h"
#include "../elements/DIVElement.h"
#include "../elements/STRONGElement.h"
@ -18,12 +16,12 @@ std::shared_ptr<Component> searchComponentTree(const std::shared_ptr<Component> @@ -18,12 +16,12 @@ std::shared_ptr<Component> searchComponentTree(const std::shared_ptr<Component>
const std::unordered_map<std::string, std::shared_ptr<Element>> ComponentBuilder::elementMap {
{"a", std::make_shared<AElement>()},
{"blockquote", std::make_shared<BLOCKQUOTEElement>()},
{"blockquote", std::make_shared<DIVElement>()},
{"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>()},
{"p", std::make_shared<DIVElement>()},
{"span", std::make_shared<SPANElement>()},
{"aside", std::make_shared<SPANElement>()},
{"div", std::make_shared<DIVElement>()},
@ -34,8 +32,7 @@ const std::unordered_map<std::string, std::shared_ptr<Element>> ComponentBuilder @@ -34,8 +32,7 @@ const std::unordered_map<std::string, std::shared_ptr<Element>> ComponentBuilder
{"i", std::make_shared<SPANElement>()}
};
// FIXME: pass the component it's attached too
std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent, int windowWidth, int windowHeight) {
std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent, const std::shared_ptr<Window> win) {
std::shared_ptr<Component> component;
std::string tag;
if (node == nullptr) {
@ -68,7 +65,11 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n @@ -68,7 +65,11 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n
//std::tie(x,y) = getPos(parentComponent, isInline, windowWidth);
//auto [x, y]=getPos(parentComponent, isInline);
component = element->renderer(node, 0, 0, windowWidth, windowHeight); // doesn't always make a component
//component = element->renderer(node, 0, 0, windowWidth, windowHeight); // doesn't always make a component
ElementRenderRequest request;
request.node = node;
request.parentComponent = parentComponent;
component = element->renderer(request); // doesn't always make a component
} else {
//std::cout << "Unknown tag: " << tag << std::endl;
}
@ -81,70 +82,73 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n @@ -81,70 +82,73 @@ std::shared_ptr<Component> ComponentBuilder::build(const std::shared_ptr<Node> n
if (textComponent) {
//std::cout << "compositing [" << textComponent->text << "]" << std::endl;
if (!textComponent->onMousedown) {
textComponent->onMousedown = [textComponent](int x, int y) {
if (window->selectionList.size()) {
textComponent->onMousedown = [textComponent, win](int x, int y) {
if (win->selectionList.size()) {
//std::cout << "unselecting text" << std::endl;
for(std::vector<std::shared_ptr<Component>>::iterator it = window->selectionList.begin(); it != window->selectionList.end(); ++it) {
for(std::vector<std::shared_ptr<Component>>::iterator it = win->selectionList.begin(); it != win->selectionList.end(); ++it) {
TextComponent *selectedTextComponent = dynamic_cast<TextComponent*>(it->get());
if (selectedTextComponent) {
selectedTextComponent->textSelected = false;
selectedTextComponent->updateHighlight();
}
}
window->selectionList.clear();
win->selectionList.clear();
}
//std::cout << "TextSelection starting at " << x << "," << y << std::endl;
// 0 to -640 (windowHeight)
// so if y=640 , f(y)=-640
//int ny = -y;
//std::cout << "TextSelection adjusted " << x << "," << ny << std::endl;
window->highlightStartX = x;
window->highlightStartY = y;
win->highlightStartX = x;
win->highlightStartY = y;
//std::cout << "Component at " << static_cast<int>(textComponent->x) << "," << static_cast<int>(textComponent->y) << std::endl;
};
// mouseover to update selection renderer
textComponent->onMouseup = [textComponent, parentComponent](int x, int y) {
textComponent->onMouseup = [textComponent, parentComponent, win](int x, int y) {
//std::cout << "TextSelection ending at " << x << "," << y << std::endl;
//int ny = -y;
//std::cout << "TextSelection adjusted " << x << "," << ny << std::endl;
// find all components in that range
//std::cout << "TextSelection started at " << window->highlightStartX << "," << window->highlightStartY << std::endl;
int minX = std::min(window->highlightStartX, x);
int maxX = std::max(window->highlightStartX, x);
int minY = std::min(window->highlightStartY, y);
int maxY = std::max(window->highlightStartY, y);
//std::cout << "TextSelection started at " << win->highlightStartX << "," << win->highlightStartY << std::endl;
int minX = std::min(win->highlightStartX, x);
int maxX = std::max(win->highlightStartX, x);
int minY = std::min(win->highlightStartY, y);
int maxY = std::max(win->highlightStartY, y);
// FIXME: find root component and search there
// can't use win because we maybe in a sub component
for(int cx = minX; cx < maxX; ++cx) {
for(int cy = minY; cy < maxY; ++cy) {
//std::cout << "textComponent inside " << cx << "," << cy << std::endl;
std::shared_ptr<Component> cp = searchComponentTree(parentComponent, cx, cy);
TextComponent *selectedTextComponent = dynamic_cast<TextComponent*>(cp.get());
if (selectedTextComponent) {
std::vector<std::shared_ptr<Component>>::iterator it = std::find(window->selectionList.begin(), window->selectionList.end(), cp);
if (it == window->selectionList.end()) {
std::vector<std::shared_ptr<Component>>::iterator it = std::find(win->selectionList.begin(), win->selectionList.end(), cp);
if (it == win->selectionList.end()) {
selectedTextComponent->textSelected = true;
selectedTextComponent->updateHighlight();
window->renderDirty = true;
window->selectionList.push_back(cp);
win->renderDirty = true;
win->selectionList.push_back(cp);
}
}
}
}
//std::cout << "selected " << window->selectionList.size() << " components" << std::endl;
//std::cout << "selected " << win->selectionList.size() << " components" << std::endl;
};
}
}
InputComponent *inputComponent = dynamic_cast<InputComponent*>(component.get());
if (inputComponent) {
//if (inputComponent) {
// any input set up we need to do?
// boundToPage defaults to true for components, InputComponent doesn't have it overridded
//std::cout << "Just built an inputComponent" << std::endl;
inputComponent->win = window.get();
}
//inputComponent->win = win;
//}
//std::cout << "composting component, initial: " << (int)component->width << "x" << (int)component->height << std::endl;
// set our available dimensions
component->windowWidth = windowWidth;
component->windowHeight = windowHeight;
component->win = win;
component->windowWidth = win->windowWidth;
component->windowHeight = win->windowHeight;
// set our type
component->isInline = isInline;

7
src/graphics/components/ComponentBuilder.h

@ -5,18 +5,21 @@ @@ -5,18 +5,21 @@
#include <memory>
#include <unordered_map>
#include "Component.h"
#include "../elements/Element.h"
//#include "../elements/Element.h"
#include "../../html/TagNode.h"
#include "../../html/TextNode.h"
#include "../../html/Node.h"
class Element;
typedef std::unordered_map<std::string, std::function<std::unique_ptr<Component>(const Node &node, int y, int windowWidth, int windowHeight)>> ElementRendererMap;
class ComponentBuilder {
private:
const static std::unordered_map<std::string, std::shared_ptr<Element>> elementMap;
public:
std::shared_ptr<Component> build(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent, int windowWidth, int windowHeight);
// FIXME: consider passing the documentComponent we're bound too
std::shared_ptr<Component> build(const std::shared_ptr<Node> node, const std::shared_ptr<Component> &parentComponent, const std::shared_ptr<Window> win);
};
// getComponentType

Loading…
Cancel
Save