some dom stuff
This commit is contained in:
parent
556b064e1d
commit
a4ac73529e
1
.clangd
1
.clangd
@ -19,6 +19,7 @@ Diagnostics:
|
||||
|
||||
CheckOptions:
|
||||
UnusedIncludes: Strict
|
||||
cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor: true
|
||||
|
||||
# typecase rules
|
||||
readability-identifier-naming.ClassCase: CamelCase
|
||||
|
@ -17,10 +17,10 @@ add_project_arguments(
|
||||
)
|
||||
|
||||
sources = []
|
||||
subdir('browser')
|
||||
subdir('src')
|
||||
|
||||
executable(
|
||||
'web-browser',
|
||||
'browser',
|
||||
sources,
|
||||
win_subsystem: 'console',
|
||||
include_directories: [],
|
||||
|
30
src/dom.cpp
Normal file
30
src/dom.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "dom.hpp"
|
||||
|
||||
namespace dom {
|
||||
|
||||
auto Box::size() const noexcept -> Size
|
||||
{
|
||||
auto new_size = Size {};
|
||||
for (const auto& child : children)
|
||||
new_size += child->size() + Size { 5, 5 };
|
||||
new_size += { 5, 5 };
|
||||
return new_size;
|
||||
}
|
||||
|
||||
auto Box::render(Renderer& renderer, Position position) const noexcept -> void
|
||||
{
|
||||
renderer.draw_rectangle(position, size(), color);
|
||||
auto child_position = position + Position { 5, 5 };
|
||||
for (const auto& child : children) {
|
||||
child->render(renderer, child_position);
|
||||
child_position.y += child->size().height + 5;
|
||||
}
|
||||
}
|
||||
|
||||
auto Rectangle::render(Renderer& renderer, Position position) const noexcept
|
||||
-> void
|
||||
{
|
||||
renderer.draw_rectangle(position, size(), color);
|
||||
}
|
||||
|
||||
}
|
92
src/dom.hpp
Normal file
92
src/dom.hpp
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace dom {
|
||||
|
||||
template <typename T> using Ref = std::shared_ptr<T>;
|
||||
|
||||
struct Position {
|
||||
auto operator+=(const Position& other) noexcept -> Position&
|
||||
{
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
return *this;
|
||||
}
|
||||
int x, y;
|
||||
};
|
||||
|
||||
auto operator+(const Position& a, const Position& b) noexcept -> Position
|
||||
{
|
||||
return { a.x + b.x, a.y + b.y };
|
||||
}
|
||||
|
||||
struct Size {
|
||||
auto operator+=(const Size& other) noexcept -> Size&
|
||||
{
|
||||
width += other.width;
|
||||
height += other.height;
|
||||
return *this;
|
||||
}
|
||||
friend auto operator+(const Size& a, const Size& b) noexcept -> Size;
|
||||
|
||||
int width, height;
|
||||
};
|
||||
|
||||
auto operator+(const Size& a, const Size& b) noexcept -> Size
|
||||
{
|
||||
return { a.width + b.width, a.height + b.height };
|
||||
}
|
||||
|
||||
struct Color {
|
||||
static const auto opaque = uint8_t { 255 };
|
||||
static const auto transparent = uint8_t { 0 };
|
||||
|
||||
uint8_t red, green, blue, alpha { opaque };
|
||||
};
|
||||
|
||||
struct Renderer {
|
||||
Renderer() = default;
|
||||
virtual ~Renderer() = default;
|
||||
virtual auto draw_rectangle(
|
||||
Position position, Size size, Color color) const noexcept -> void
|
||||
= 0;
|
||||
};
|
||||
|
||||
struct Element {
|
||||
Element() = default;
|
||||
virtual ~Element() = default;
|
||||
[[nodiscard]] virtual auto size() const noexcept -> Size = 0;
|
||||
virtual auto render(Renderer& renderer, Position position) const noexcept
|
||||
-> void
|
||||
= 0;
|
||||
};
|
||||
|
||||
struct Box final : public Element {
|
||||
[[nodiscard]] auto size() const noexcept -> Size override;
|
||||
auto render(Renderer& renderer, Position position) const noexcept
|
||||
-> void override;
|
||||
auto add_child(Ref<Element> element) noexcept
|
||||
{
|
||||
children.push_back(element);
|
||||
}
|
||||
|
||||
std::vector<Ref<Element>> children;
|
||||
Color color {};
|
||||
};
|
||||
|
||||
struct Rectangle final : public Element {
|
||||
Rectangle(Size size)
|
||||
: m_size { size }
|
||||
{ }
|
||||
[[nodiscard]] auto size() const noexcept -> Size override { return m_size; }
|
||||
auto render(Renderer& renderer, Position position) const noexcept
|
||||
-> void override;
|
||||
|
||||
Color color {};
|
||||
Size m_size { 50, 50 };
|
||||
};
|
||||
|
||||
}
|
13
src/main.cpp
13
src/main.cpp
@ -3,8 +3,7 @@
|
||||
#include "SDL_rect.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_video.h"
|
||||
#include "scriptlang/parser.hpp"
|
||||
#include "utils/all.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <SDL.h>
|
||||
#include <fmt/core.h>
|
||||
#include <memory>
|
||||
@ -70,16 +69,6 @@ private:
|
||||
auto main() -> int
|
||||
{
|
||||
|
||||
const auto* text = "{name: \"test\", value: [true, false, 123, \"bruh\"], "
|
||||
"int: 123, float: 3.14}";
|
||||
auto ast = scriptlang::Parser(text).parse_expression(true);
|
||||
if (!ast)
|
||||
fmt::print("parser error at {}:{}: {}\n\t\n",
|
||||
ast.unwrap_error().span.from.line,
|
||||
ast.unwrap_error().span.from.column, ast.unwrap_error().message);
|
||||
else
|
||||
fmt::print("ast = {}\n", ast.unwrap()->to_string());
|
||||
|
||||
// test
|
||||
fmt::print("browser: hello world!\n");
|
||||
auto gui = GUI::create().unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user