Skip to content

Commit

Permalink
Refactor widget into scene nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Jun 9, 2024
1 parent 2008fa2 commit 541ddcb
Show file tree
Hide file tree
Showing 21 changed files with 63 additions and 249 deletions.
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"name": "msvc",
"inherits": "default",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build-msvc/",
"binaryDir": "${sourceDir}/build/",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
Expand Down
2 changes: 1 addition & 1 deletion app/aspire/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ target_sources(${PROJECT_NAME} PRIVATE

target_link_libraries(${PROJECT_NAME} PRIVATE
aspire-core
aspire-widget
aspire-scene
)
4 changes: 2 additions & 2 deletions app/aspire/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <aspire/core/Kernel.h>
#include <aspire/widget/Window.h>
#include <aspire/scene/Window.h>

auto main() -> int
{
Expand All @@ -10,7 +10,7 @@ auto main() -> int

aspire::core::Kernel kernel;

auto window = std::make_unique<aspire::widget::Window>();
auto window = std::make_unique<aspire::scene::Window>();
window->setX(defaultX);
window->setY(defaultY);
window->setWidth(defaultWidth);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace aspire::widget
namespace aspire::scene
{
struct Color
{
Expand Down
42 changes: 0 additions & 42 deletions include/aspire/scene/Node.h

This file was deleted.

16 changes: 8 additions & 8 deletions include/aspire/widget/Window.h → include/aspire/scene/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include <aspire/core/Pimpl.h>
#include <aspire/core/Service.h>
#include <aspire/widget/Color.h>
#include <aspire/widget/Widget.h>
#include <aspire/widget/export.hxx>
#include <aspire/scene/Color.h>
#include <aspire/scene/Node.h>
#include <aspire/scene/export.hxx>

namespace aspire::widget
namespace aspire::scene
{
/// @brief This class defines an application window.
class ASPIRE_WIDGET_EXPORT Window : public aspire::core::Service
class ASPIRE_SCENE_EXPORT Window : public aspire::core::Service
{
public:
Window();
Expand Down Expand Up @@ -39,8 +39,8 @@ namespace aspire::widget
auto setColor(Color x) noexcept -> void;
[[nodiscard]] auto getColor() const noexcept -> Color;

auto setWidget(std::unique_ptr<Widget> x) -> void;
[[nodiscard]] auto getWidget() const -> Widget*;
auto setRootNode(std::unique_ptr<Node> x) -> void;
[[nodiscard]] auto getRootNode() const -> Node*;

auto event(aspire::core::Event* x) -> void override;

Expand All @@ -49,7 +49,7 @@ namespace aspire::widget
protected:
auto onStartup() -> void override;
auto update() -> void;
auto update(Widget& x) -> void;
auto update(Node& x) -> void;

private:
struct Impl;
Expand Down
7 changes: 0 additions & 7 deletions include/aspire/widget/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <aspire/core/Object.h>
#include <aspire/core/Pimpl.h>
#include <aspire/scene/Node.h>
#include <aspire/widget/export.hxx>

namespace aspire::widget
Expand Down Expand Up @@ -33,12 +32,6 @@ namespace aspire::widget

[[nodiscard]] auto childWidgets() const noexcept -> std::vector<Widget*>;

/// @brief This function is only save to call on the render thread and provides a way to synchronize the state of the widget with a scene
/// node.
/// @param x The scene node that corresponds to this widget. Nullptr if it doesn't exist.
/// @return The root node to represent this widget in the scene.
virtual auto update(aspire::scene::Node* x) -> std::unique_ptr<aspire::scene::Node>;

private:
struct Impl;
Pimpl<Impl> pimpl;
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
add_subdirectory(core)
add_subdirectory(scene)
add_subdirectory(widget)
10 changes: 0 additions & 10 deletions src/scene/CMakeLists.txt

This file was deleted.

41 changes: 0 additions & 41 deletions src/scene/Node.cpp

This file was deleted.

31 changes: 12 additions & 19 deletions src/widget/Window.cpp → src/scene/Window.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#include <aspire/widget/Window.h>
#include <aspire/scene/Window.h>

#include <aspire/core/Kernel.h>
#include <aspire/core/PimplImpl.h>
#include <SFML/Graphics.hpp>
#include "BatchRenderer.h"

using aspire::core::Event;
using aspire::widget::Window;
using aspire::scene::Window;

struct Window::Impl
{
sf::RenderWindow renderer;
BatchRenderer batchRenderer{};
sf::Color clearColor;

std::unique_ptr<Widget> widget;
std::unique_ptr<Node> rootNode;
aspire::core::Kernel* kernel{};

std::string title;
Expand Down Expand Up @@ -68,14 +66,14 @@ auto Window::getWidth() const noexcept -> int
return this->pimpl->width;
}

auto Window::setWidget(std::unique_ptr<Widget> x) -> void
auto Window::setRootNode(std::unique_ptr<Node> x) -> void
{
this->pimpl->widget = std::move(x);
this->pimpl->rootNode = std::move(x);
}

auto Window::getWidget() const -> Widget*
auto Window::getRootNode() const -> Node*
{
return this->pimpl->widget.get();
return this->pimpl->rootNode.get();
}

auto Window::setTitle(std::string_view x) -> void
Expand Down Expand Up @@ -146,7 +144,6 @@ auto Window::frame() -> void
this->update();

this->pimpl->renderer.clear(this->pimpl->clearColor);
this->pimpl->renderer.draw(this->pimpl->batchRenderer);
this->pimpl->renderer.display();
}

Expand Down Expand Up @@ -174,22 +171,18 @@ auto Window::update() -> void
this->pimpl->clearColor.b = static_cast<sf::Uint8>(this->pimpl->color.b * maxUint8);
this->pimpl->clearColor.a = static_cast<sf::Uint8>(this->pimpl->color.a * maxUint8);

if(this->pimpl->widget == nullptr)
if(this->pimpl->rootNode == nullptr)
{
return;
}

this->update(*this->pimpl->widget);

// I'll have an updated list of nodes.
// Sort and add to batch renderer.
// this->pimpl->batchRenderer.add(node);
this->update(*this->pimpl->rootNode);
}

auto Window::update(Widget& x) -> void
auto Window::update(Node& x) -> void
{
for(auto* widget : x.childWidgets())
for(auto* node : x.childNodes())
{
this->update(*widget);
this->update(*node);
}
}
2 changes: 2 additions & 0 deletions src/scene/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ project_add_executable(${PROJECT_NAME})

target_sources(${PROJECT_NAME} PRIVATE
Node.test.cpp
Window.test.cpp
)

enable_testing()

find_package(GTest CONFIG REQUIRED)

target_link_libraries(${PROJECT_NAME} PRIVATE
aspire-core
aspire-scene
GTest::gtest_main
)
Expand Down
38 changes: 21 additions & 17 deletions src/scene/test/Node.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@

using aspire::scene::Node;

TEST(Node, addChild)
TEST(Node, x)
{
Node node;

node.addChild(std::make_unique<Node>());
EXPECT_FALSE(node.getChildren().empty());
Node Node;
Node.setX(std::numeric_limits<int>::max());
EXPECT_EQ(Node.getX(), std::numeric_limits<int>::max());
}

TEST(Node, remove)
TEST(Node, y)
{
Node node;

auto child = std::make_unique<Node>();
auto* childNode = child.get();
node.addChild(std::move(child));
EXPECT_FALSE(node.getChildren().empty());
EXPECT_EQ(childNode->getParent(), &node);
Node Node;
Node.setY(std::numeric_limits<int>::max());
EXPECT_EQ(Node.getY(), std::numeric_limits<int>::max());
}

child = childNode->remove();
ASSERT_NE(child, nullptr);
EXPECT_EQ(child->getParent(), nullptr);
EXPECT_TRUE(node.getChildren().empty());
TEST(Node, width)
{
Node Node;
Node.setWidth(std::numeric_limits<int>::max());
EXPECT_EQ(Node.getWidth(), std::numeric_limits<int>::max());
}

TEST(Node, height)
{
Node Node;
Node.setHeight(std::numeric_limits<int>::max());
EXPECT_EQ(Node.getHeight(), std::numeric_limits<int>::max());
}
15 changes: 15 additions & 0 deletions src/scene/test/Window.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <aspire/scene/Window.h>
#include <gtest/gtest.h>

using aspire::scene::Node;
using aspire::scene::Window;

TEST(Window, node)
{
Window window;

auto node = std::make_unique<Node>();
auto* n = node.get();
window.setRootNode(std::move(node));
EXPECT_EQ(window.getRootNode(), n);
}
10 changes: 0 additions & 10 deletions src/widget/BatchRenderer.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions src/widget/BatchRenderer.h

This file was deleted.

1 change: 0 additions & 1 deletion src/widget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ find_package(SFML COMPONENTS graphics CONFIG REQUIRED)

target_link_libraries(${PROJECT_NAME} PRIVATE
aspire-core
aspire-scene
sfml-graphics
)

Expand Down
Loading

0 comments on commit 541ddcb

Please sign in to comment.