KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
ui.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26#include "../utility/id.hpp"
27#include "../basic/cella.hpp"
28#include "../basic/rgba.hpp"
29#include "../basic/point.hpp"
30#include "../basic/upoint.hpp"
31
32#include <limits>
33#include <string>
34#include <vector>
35
46{
47public:
50 std::string m_name;
51 std::vector<ID<Widget>> m_widgets;
52
54 CellA m_background = CellA(' ', RGBA(0, 0, 0, 0), RGBA(0, 0, 0, 0));
55 uint8_t m_alpha = std::numeric_limits<uint8_t>::max();
56 RGBA m_frgba = RGBAColors::transparent;
57 RGBA m_brgba = RGBAColors::transparent;
58 std::vector<CellA> m_image;
59
60 UI(Engine& engine, UPoint resolution = UPoint(10, 10), std::string name = "");
61 virtual ~UI();
62
63 auto AddWidget(const ID<Widget>& widget) -> bool;
64 auto RemoveWidget(const ID<Widget>& widget) -> bool;
65 auto RemoveAllWidgets() -> bool;
66
67 void Resize(UPoint resolution);
68
69 void Render();
70 void Draw(Point position = Point(0, 0), UPoint start = UPoint(0, 0), UPoint end = UPoint(0, 0), uint8_t alpha = std::numeric_limits<uint8_t>::max());
72
73protected:
74 virtual auto OnTick() -> bool;
75
76 friend class KTech::Memory;
77
78private:
79 inline void RenderBackground();
80 inline void RenderSimple(Widget* widget, Texture& texture);
81 inline void RenderComplex(Widget* widget, Texture& texture);
82 inline void RenderForeground();
83};
Complete engine containing all engine components.
Definition engine.hpp:41
Engine component responsible for registering all world structures.
Definition memory.hpp:36
World structure that contains and renders Widgets.
Definition ui.hpp:46
auto AddWidget(const ID< Widget > &widget) -> bool
Add a Widget.
Definition ui.cpp:56
std::string m_name
String name.
Definition ui.hpp:50
uint8_t m_alpha
Opacity for all rendered Widgets.
Definition ui.hpp:55
UI(Engine &engine, UPoint resolution=UPoint(10, 10), std::string name="")
Construct a UI.
Definition ui.cpp:34
std::vector< ID< Widget > > m_widgets
Contained Widgets.
Definition ui.hpp:51
void Render()
Render all contained Widgets.
Definition ui.cpp:132
const ID< UI > m_id
Personal ID.
Definition ui.hpp:49
auto RemoveWidget(const ID< Widget > &widget) -> bool
Remove a Widget.
Definition ui.cpp:80
void Resize(UPoint resolution)
Resize the image's resolution.
Definition ui.cpp:123
auto RemoveAllWidgets() -> bool
Remove all contained Widget.
Definition ui.cpp:101
RGBA m_brgba
Background color added after rendering Widgets.
Definition ui.hpp:57
std::vector< CellA > m_image
CellA-based rendered image.
Definition ui.hpp:58
virtual auto OnTick() -> bool
Virtual function called once each tick.
Definition ui.cpp:218
UPoint m_res
Image's resolution.
Definition ui.hpp:53
CellA m_background
The background to render upon.
Definition ui.hpp:54
void RenderClearDrawPrint()
Shortcut for UI::Render(), Output::Clear(), UI::Draw() and Output::Print().
Definition ui.cpp:186
Engine & engine
Parent Engine.
Definition ui.hpp:48
RGBA m_frgba
Foreground color added after rendering Widgets.
Definition ui.hpp:56
void Draw(Point position=Point(0, 0), UPoint start=UPoint(0, 0), UPoint end=UPoint(0, 0), uint8_t alpha=std::numeric_limits< uint8_t >::max())
Draw the rendered image (UI::m_image) to Output so it can be printed to the terminal.
Definition ui.cpp:170
virtual ~UI()
Remove all Widgets from itself, and itself from Memory.
Definition ui.cpp:44
World structure that comprises Textures, behaves as a user interface element, and exists within UI.
Definition widget.hpp:45
Like Cell, but with RGBA foreground and background colors, instead of RGB.
Definition cella.hpp:32
Serializable world structure identifier.
Definition id.hpp:38
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
Like RGB, but also has an alpha channel representing transparency.
Definition rgba.hpp:30
A CellA-based sprite.
Definition texture.hpp:48
Unsigned 2D vector, mostly used to store sizes and 2D indexes.
Definition upoint.hpp:29