Skip to content

Commit d5db0a6

Browse files
committed
Begin to add Canvas and rework elements
1 parent 27ff830 commit d5db0a6

File tree

9 files changed

+153
-17
lines changed

9 files changed

+153
-17
lines changed

include/NovelRT/UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
1313
class GlfwVulkanUIProvider final : public UI::IUIProvider
1414
{
1515
private:
16-
std::list<std::shared_ptr<ImGuiTextbox>> _textboxes;
17-
std::list<std::shared_ptr<ImGuiButton>> _buttons;
16+
std::list<std::shared_ptr<IUIElement>> _elements;
17+
//std::list<std::shared_ptr<ImGuiTextbox>> _textboxes;
18+
//std::list<std::shared_ptr<ImGuiButton>> _buttons;
1819
NovelRT::Maths::GeoVector2F _windowSize;
1920

2021
void Render();
@@ -48,6 +49,12 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
4849
NovelRT::Maths::GeoVector2F position,
4950
NovelRT::Maths::GeoVector2F scale,
5051
NovelRT::Graphics::RGBAColour backgroundColour) final;
52+
53+
54+
std::shared_ptr<IUICanvas> CreateCanvas(const std::string& identifier,
55+
NovelRT::Maths::GeoVector2F position,
56+
NovelRT::Maths::GeoVector2F scale,
57+
NovelRT::Graphics::RGBAColour colour) final;
5158
};
5259
}
5360

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
2+
// for more information.
3+
4+
#ifndef NOVELRT_UI_DEARIMGUI_IMGUICANVAS_H
5+
#define NOVELRT_UI_DEARIMGUI_IMGUICANVAS_H
6+
7+
#ifndef NOVELRT_UI_DEARIMGUI_H
8+
#error NovelRT does not support including types explicitly by default. Please include UI::DearImGui.h instead for the UI::DearImGui namespace subset.
9+
#endif
10+
11+
namespace NovelRT::UI::DearImGui
12+
{
13+
class ImGuiCanvas : public IUICanvas, public ImGuiCommon
14+
{
15+
public:
16+
ImGuiCanvas() noexcept;
17+
18+
ImGuiCanvas(const std::string& identifier,
19+
NovelRT::Maths::GeoVector2F position,
20+
NovelRT::Maths::GeoVector2F scale,
21+
NovelRT::Graphics::RGBAColour backgroundColour,
22+
NovelRT::Maths::GeoVector2F screenSize) noexcept;
23+
24+
void Render(std::shared_ptr<IUIProvider> provider, NovelRT::Maths::GeoVector2F windowSize) final;
25+
26+
void AddElement(std::shared_ptr<IUIElement> element);
27+
};
28+
}
29+
30+
#endif

include/NovelRT/UI/DearImGui/UI.DearImGui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace NovelRT::UI::DearImGui
1111
{
1212
class ImGuiTextbox;
1313
class ImGuiButton;
14+
class ImGuiCanvas;
1415
class ImGuiCommon;
1516
}
1617

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

3132
#include "ImGuiCommon.h"
3233
#include "ImGuiButton.h"
34+
#include "ImGuiCanvas.h"
3335
#include "ImGuiTextbox.h"
3436

3537
// clang-format on

include/NovelRT/UI/IUICanvas.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
3+
// for more information.
4+
5+
#ifndef NOVELRT_UI_IUICANVAS_H
6+
#define NOVELRT_UI_IUICANVAS_H
7+
8+
#ifndef NOVELRT_UI_H
9+
#error NovelRT does not support including types explicitly by default. Please include UI.h instead for the Windowing namespace subset.
10+
#endif
11+
12+
namespace NovelRT::UI
13+
{
14+
class IUICanvas : public IUIElement
15+
{
16+
protected:
17+
NovelRT::Graphics::RGBAColour _colour;
18+
std::list<std::shared_ptr<IUIElement>> _elements;
19+
20+
public:
21+
IUICanvas(const std::string& identifier, UIElementState state, NovelRT::Maths::GeoVector2F position,
22+
NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour colour) noexcept :
23+
IUIElement(identifier, state, position, scale), _colour(colour), _elements(std::list<std::shared_ptr<IUIElement>>()){}
24+
25+
virtual void AddElement(std::shared_ptr<IUIElement> element) = 0;
26+
27+
[[nodiscard]] inline NovelRT::Graphics::RGBAColour& Colour()
28+
{
29+
return _colour;
30+
}
31+
32+
[[nodiscard]] inline const NovelRT::Graphics::RGBAColour& Colour() const
33+
{
34+
return _colour;
35+
}
36+
};
37+
}
38+
39+
#endif // NOVELRT_UI_IUICANVAS_H

include/NovelRT/UI/IUIProvider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace NovelRT::UI
1515
protected:
1616
LoggingService _logger;
1717
bool _editorMode;
18-
//std::list<IUIElement> _elements;
1918

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

37+
virtual std::shared_ptr<IUICanvas> CreateCanvas(const std::string& identifier,
38+
NovelRT::Maths::GeoVector2F position, NovelRT::Maths::GeoVector2F scale, NovelRT::Graphics::RGBAColour colour) = 0;
39+
3840
[[nodiscard]] inline bool& EditorMode() noexcept
3941
{
4042
return _editorMode;

include/NovelRT/UI/UI.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace NovelRT::UI
2121
{
2222
enum class UIElementState : uint32_t;
2323
class IUIElement;
24+
class IUICanvas;
2425
class IUITextbox;
2526
class IUIProvider;
2627
class IUIButton;
@@ -33,6 +34,7 @@ namespace NovelRT::UI
3334

3435
#include "UIElementState.h"
3536
#include "IUIElement.h"
37+
#include "IUICanvas.h"
3638
#include "IUIButton.h"
3739
#include "IUITextbox.h"
3840
#include "IUIProvider.h"

src/NovelRT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ set(CORE_SOURCES
7878

7979
UI/DearImGui/ImGuiTextbox.cpp
8080
UI/DearImGui/ImGuiButton.cpp
81+
UI/DearImGui/ImGuiCanvas.cpp
8182
UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.cpp
8283
UI/DearImGui/GlfwVulkan/GlfwVulkanUIPluginProvider.cpp
8384

src/NovelRT/UI/DearImGui/GlfwVulkan/GlfwVulkanUIProvider.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace NovelRT::UI::DearImGui::GlfwVulkan
77
{
88
GlfwVulkanUIProvider::GlfwVulkanUIProvider() noexcept
9-
: _isInitialised(false), _initInfo(), _textboxes(std::list<std::shared_ptr<ImGuiTextbox>>()), _windowSize()
9+
: _isInitialised(false), _initInfo(), _elements(std::list<std::shared_ptr<IUIElement>>()), _windowSize(),
10+
RenderEvent(NovelRT::Utilities::Event<std::shared_ptr<IUIProvider>, NovelRT::Maths::GeoVector2F>())
1011
{
1112
_editorMode = EngineConfig::EnableEditorMode();
1213
}
@@ -176,15 +177,10 @@ namespace NovelRT::UI::DearImGui::GlfwVulkan
176177

177178
void GlfwVulkanUIProvider::Render()
178179
{
179-
for (auto&& x : _textboxes)
180+
for (auto&& x : _elements)
180181
{
181182
x->Render(this->shared_from_this(), _windowSize);
182183
}
183-
184-
for (auto&& button : _buttons)
185-
{
186-
button->Render(shared_from_this(), _windowSize);
187-
}
188184
}
189185

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

221-
auto boxPtr = _textboxes.emplace_back(
222-
std::make_shared<ImGuiTextbox>(identifier, text, wordWrap, position, scale, fontSize, backgroundColour,
223-
_windowSize)); // TODO: This looks VEEEERY WRONG???
224-
225-
return std::dynamic_pointer_cast<IUITextbox>(boxPtr);
220+
// return std::dynamic_pointer_cast<IUITextbox>(boxPtr);
221+
return std::make_shared<ImGuiTextbox>(identifier, text, wordWrap, position, scale, fontSize, backgroundColour, _windowSize);
226222
}
227223

228224
std::shared_ptr<IUIButton> GlfwVulkanUIProvider::CreateButton(const std::string& identifier,
229225
NovelRT::Maths::GeoVector2F position,
230226
NovelRT::Maths::GeoVector2F scale,
231227
NovelRT::Graphics::RGBAColour backgroundColour)
232228
{
233-
auto buttonPtr = _buttons.emplace_back(std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize));
229+
//auto buttonPtr = _elements.emplace_back(std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize));
230+
231+
//return std::dynamic_pointer_cast<IUIButton>(buttonPtr);
232+
return std::make_shared<ImGuiButton>(identifier, position, scale, backgroundColour, _windowSize);
233+
}
234+
235+
std::shared_ptr<IUICanvas> GlfwVulkanUIProvider::CreateCanvas(const std::string& identifier,
236+
NovelRT::Maths::GeoVector2F position,
237+
NovelRT::Maths::GeoVector2F scale,
238+
NovelRT::Graphics::RGBAColour colour)
239+
{
240+
//auto canvasPtr = _elements.emplace_back(std::make_shared<ImGuiCanvas>(identifier, position, scale, colour, _windowSize));
234241

235-
return std::dynamic_pointer_cast<IUIButton>(buttonPtr);
242+
//return std::dynamic_pointer_cast<IUICanvas>(canvasPtr);
243+
return std::make_shared<ImGuiCanvas>(identifier, position, scale, colour, _windowSize);
236244
}
237245

238246
ImGuiKey GlfwVulkanUIProvider::GlfwToImGuiKey(int32_t key)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
2+
// for more information.
3+
4+
#include <NovelRT/UI/DearImGui/UI.DearImGui.h>
5+
6+
namespace NovelRT::UI::DearImGui
7+
{
8+
ImGuiCanvas::ImGuiCanvas() noexcept : ImGuiCanvas("", NovelRT::Maths::GeoVector2F::Zero(), NovelRT::Maths::GeoVector2F::One(), NovelRT::Graphics::RGBAColour(0,0,0,255), NovelRT::Maths::GeoVector2F::Zero()){}
9+
10+
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)){}
11+
12+
void ImGuiCanvas::Render(std::shared_ptr<IUIProvider> provider, NovelRT::Maths::GeoVector2F windowSize)
13+
{
14+
if (_state != UIElementState::Shown)
15+
{
16+
return;
17+
}
18+
if (windowSize != _screenSize)
19+
{
20+
_screenSize = windowSize;
21+
_translatedPosition = _position + (windowSize / 2);
22+
}
23+
24+
ImGui::PushStyleColor(ImGuiCol_WindowBg, _colour);
25+
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(100, 100));
26+
ImGui::Begin(_identifier.c_str(), nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
27+
ImGui::SetWindowPos(_translatedPosition);
28+
ImGui::SetWindowSize(_scale);
29+
ImGui::PopStyleVar();
30+
ImGui::PopStyleColor();
31+
32+
for(auto&& element : _elements)
33+
{
34+
element->Render(provider, windowSize);
35+
}
36+
37+
ImGui::End();
38+
39+
}
40+
41+
void ImGuiCanvas::AddElement(std::shared_ptr<IUIElement> element)
42+
{
43+
_elements.emplace_back(element);
44+
}
45+
}

0 commit comments

Comments
 (0)