Skip to content

Commit

Permalink
Begin to add Canvas and rework elements
Browse files Browse the repository at this point in the history
  • Loading branch information
capnkenny committed Feb 4, 2023
1 parent 27ff830 commit d5db0a6
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 17 deletions.
11 changes: 9 additions & 2 deletions include/NovelRT/UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
class GlfwVulkanUIProvider final : public UI::IUIProvider
{
private:
std::list<std::shared_ptr<ImGuiTextbox>> _textboxes;
std::list<std::shared_ptr<ImGuiButton>> _buttons;
std::list<std::shared_ptr<IUIElement>> _elements;
//std::list<std::shared_ptr<ImGuiTextbox>> _textboxes;
//std::list<std::shared_ptr<ImGuiButton>> _buttons;
NovelRT::Maths::GeoVector2F _windowSize;

void Render();
Expand Down Expand Up @@ -48,6 +49,12 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale,
NovelRT::Graphics::RGBAColour backgroundColour) final;


std::shared_ptr<IUICanvas> CreateCanvas(const std::string& identifier,
NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale,
NovelRT::Graphics::RGBAColour colour) final;
};
}

Expand Down
30 changes: 30 additions & 0 deletions include/NovelRT/UI/DearImGui/ImGuiCanvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
// for more information.

#ifndef NOVELRT_UI_DEARIMGUI_IMGUICANVAS_H
#define NOVELRT_UI_DEARIMGUI_IMGUICANVAS_H

#ifndef NOVELRT_UI_DEARIMGUI_H
#error NovelRT does not support including types explicitly by default. Please include UI::DearImGui.h instead for the UI::DearImGui namespace subset.
#endif

namespace NovelRT::UI::DearImGui
{
class ImGuiCanvas : public IUICanvas, public ImGuiCommon
{
public:
ImGuiCanvas() noexcept;

ImGuiCanvas(const std::string& identifier,
NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale,
NovelRT::Graphics::RGBAColour backgroundColour,
NovelRT::Maths::GeoVector2F screenSize) noexcept;

void Render(std::shared_ptr<IUIProvider> provider, NovelRT::Maths::GeoVector2F windowSize) final;

void AddElement(std::shared_ptr<IUIElement> element);
};
}

#endif
2 changes: 2 additions & 0 deletions include/NovelRT/UI/DearImGui/UI.DearImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NovelRT::UI::DearImGui
{
class ImGuiTextbox;
class ImGuiButton;
class ImGuiCanvas;
class ImGuiCommon;
}

Expand All @@ -30,6 +31,7 @@ namespace NovelRT::UI::DearImGui

#include "ImGuiCommon.h"
#include "ImGuiButton.h"
#include "ImGuiCanvas.h"
#include "ImGuiTextbox.h"

// clang-format on
Expand Down
39 changes: 39 additions & 0 deletions include/NovelRT/UI/IUICanvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
// for more information.

#ifndef NOVELRT_UI_IUICANVAS_H
#define NOVELRT_UI_IUICANVAS_H

#ifndef NOVELRT_UI_H
#error NovelRT does not support including types explicitly by default. Please include UI.h instead for the Windowing namespace subset.
#endif

namespace NovelRT::UI
{
class IUICanvas : public IUIElement
{
protected:
NovelRT::Graphics::RGBAColour _colour;
std::list<std::shared_ptr<IUIElement>> _elements;

public:
IUICanvas(const std::string& identifier, UIElementState state, NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour colour) noexcept :
IUIElement(identifier, state, position, scale), _colour(colour), _elements(std::list<std::shared_ptr<IUIElement>>()){}

virtual void AddElement(std::shared_ptr<IUIElement> element) = 0;

[[nodiscard]] inline NovelRT::Graphics::RGBAColour& Colour()
{
return _colour;
}

[[nodiscard]] inline const NovelRT::Graphics::RGBAColour& Colour() const
{
return _colour;
}
};
}

#endif // NOVELRT_UI_IUICANVAS_H
4 changes: 3 additions & 1 deletion include/NovelRT/UI/IUIProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace NovelRT::UI
protected:
LoggingService _logger;
bool _editorMode;
//std::list<IUIElement> _elements;

public:
Utilities::Event<std::reference_wrapper<IUIProvider>> RenderEvent;
Expand All @@ -35,6 +34,9 @@ namespace NovelRT::UI
virtual std::shared_ptr<IUIButton> CreateButton(const std::string& identifier,
NovelRT::Maths::GeoVector2F position, NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour backgroundColour) = 0;

virtual std::shared_ptr<IUICanvas> CreateCanvas(const std::string& identifier,
NovelRT::Maths::GeoVector2F position, NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour colour) = 0;

[[nodiscard]] inline bool& EditorMode() noexcept
{
return _editorMode;
Expand Down
2 changes: 2 additions & 0 deletions include/NovelRT/UI/UI.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace NovelRT::UI
{
enum class UIElementState : uint32_t;
class IUIElement;
class IUICanvas;
class IUITextbox;
class IUIProvider;
class IUIButton;
Expand All @@ -33,6 +34,7 @@ namespace NovelRT::UI

#include "UIElementState.h"
#include "IUIElement.h"
#include "IUICanvas.h"
#include "IUIButton.h"
#include "IUITextbox.h"
#include "IUIProvider.h"
Expand Down
1 change: 1 addition & 0 deletions src/NovelRT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set(CORE_SOURCES

UI/DearImGui/ImGuiTextbox.cpp
UI/DearImGui/ImGuiButton.cpp
UI/DearImGui/ImGuiCanvas.cpp
UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.cpp
UI/DearImGui/GlfwVulkan/GlfwVulkanUIPluginProvider.cpp

Expand Down
36 changes: 22 additions & 14 deletions src/NovelRT/UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace NovelRT::UI::DearImGui::GlfwVulkan
{
GlfwVulkanUIProvider::GlfwVulkanUIProvider() noexcept
: _isInitialised(false), _initInfo(), _textboxes(std::list<std::shared_ptr<ImGuiTextbox>>()), _windowSize()
: _isInitialised(false), _initInfo(), _elements(std::list<std::shared_ptr<IUIElement>>()), _windowSize(),
RenderEvent(NovelRT::Utilities::Event<std::shared_ptr<IUIProvider>, NovelRT::Maths::GeoVector2F>())
{
_editorMode = EngineConfig::EnableEditorMode();
}
Expand Down Expand Up @@ -176,15 +177,10 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan

void GlfwVulkanUIProvider::Render()
{
for (auto&& x : _textboxes)
for (auto&& x : _elements)
{
x->Render(this->shared_from_this(), _windowSize);
}

for (auto&& button : _buttons)
{
button->Render(shared_from_this(), _windowSize);
}
}

void GlfwVulkanUIProvider::End(std::shared_ptr<NovelRT::Graphics::GraphicsContext> context)
Expand Down Expand Up @@ -217,22 +213,34 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
float fontSize,
NovelRT::Graphics::RGBAColour backgroundColour)
{
// auto boxPtr = _elements.emplace_back(
// std::make_shared<ImGuiTextbox>(identifier, text, wordWrap, position, scale, fontSize, backgroundColour,
// _windowSize)); // TODO: This looks VEEEERY WRONG???

auto boxPtr = _textboxes.emplace_back(
std::make_shared<ImGuiTextbox>(identifier, text, wordWrap, position, scale, fontSize, backgroundColour,
_windowSize)); // TODO: This looks VEEEERY WRONG???

return std::dynamic_pointer_cast<IUITextbox>(boxPtr);
// return std::dynamic_pointer_cast<IUITextbox>(boxPtr);
return std::make_shared<ImGuiTextbox>(identifier, text, wordWrap, position, scale, fontSize, backgroundColour, _windowSize);
}

std::shared_ptr<IUIButton> GlfwVulkanUIProvider::CreateButton(const std::string& identifier,
NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale,
NovelRT::Graphics::RGBAColour backgroundColour)
{
auto buttonPtr = _buttons.emplace_back(std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize));
//auto buttonPtr = _elements.emplace_back(std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize));

//return std::dynamic_pointer_cast<IUIButton>(buttonPtr);
return std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize);
}

std::shared_ptr<IUICanvas> GlfwVulkanUIProvider::CreateCanvas(const std::string& identifier,
NovelRT::Maths::GeoVector2F position,
NovelRT::Maths::GeoVector2F scale,
NovelRT::Graphics::RGBAColour colour)
{
//auto canvasPtr = _elements.emplace_back(std::make_shared<ImGuiCanvas>(identifier, position, scale, colour, _windowSize));

return std::dynamic_pointer_cast<IUIButton>(buttonPtr);
//return std::dynamic_pointer_cast<IUICanvas>(canvasPtr);
return std::make_shared<ImGuiCanvas>(identifier, position, scale, colour, _windowSize);
}

ImGuiKey GlfwVulkanUIProvider::GlfwToImGuiKey(int32_t key)
Expand Down
45 changes: 45 additions & 0 deletions src/NovelRT/UI/DearImGui/ImGuiCanvas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
// for more information.

#include <NovelRT/UI/DearImGui/UI.DearImGui.h>

namespace NovelRT::UI::DearImGui
{
ImGuiCanvas::ImGuiCanvas() noexcept : ImGuiCanvas("", NovelRT::Maths::GeoVector2F::Zero(), NovelRT::Maths::GeoVector2F::One(), NovelRT::Graphics::RGBAColour(0,0,0,255), NovelRT::Maths::GeoVector2F::Zero()){}

ImGuiCanvas::ImGuiCanvas(const std::string& identifier, NovelRT::Maths::GeoVector2F position, NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour Colour, NovelRT::Maths::GeoVector2F screenSize) noexcept : IUICanvas(identifier, UIElementState::Hidden, position, scale, Colour), ImGuiCommon(screenSize, position + (screenSize / 2)){}

void ImGuiCanvas::Render(std::shared_ptr<IUIProvider> provider, NovelRT::Maths::GeoVector2F windowSize)
{
if (_state != UIElementState::Shown)
{
return;
}
if (windowSize != _screenSize)
{
_screenSize = windowSize;
_translatedPosition = _position + (windowSize / 2);
}

ImGui::PushStyleColor(ImGuiCol_WindowBg, _colour);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(100, 100));
ImGui::Begin(_identifier.c_str(), nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
ImGui::SetWindowPos(_translatedPosition);
ImGui::SetWindowSize(_scale);
ImGui::PopStyleVar();
ImGui::PopStyleColor();

for(auto&& element : _elements)
{
element->Render(provider, windowSize);
}

ImGui::End();

}

void ImGuiCanvas::AddElement(std::shared_ptr<IUIElement> element)
{
_elements.emplace_back(element);
}
}

0 comments on commit d5db0a6

Please sign in to comment.