KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
widget.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/point.hpp"
28#include "../world/texture.hpp"
29#include "../engine/input/input.hpp"
30#include "../engine/input/callbackgroup.hpp"
31
45{
46public:
48 struct ChildWidget
49 {
50 ID<Widget> widget;
51 bool oldSelected;
52 bool oldShown;
53 inline ChildWidget(ID<Widget> widget, bool currentSelected, bool currentShown)
54 : widget(widget), oldSelected(currentSelected), oldShown(currentShown) {}
55 };
56
59 std::string m_name;
61 ID<Widget> m_parentWidget = nullID<Widget>;
62 std::vector<ChildWidget> m_childWidgets;
63 bool m_selected = false;
64 bool m_shown = true;
65
67 std::vector<Texture> m_textures = {};
69
70 Widget(Engine& engine, Point position = Point(0, 0), std::string name = "");
71 Widget(Engine& engine, const ID<UI>& parentUI, Point position = Point(0, 0), std::string name = "");
72 virtual ~Widget();
73
74 auto AddWidget(const ID<Widget>& widget) -> bool;
75 auto RemoveWidget(const ID<Widget>& widget) -> bool;
76 auto RemoveAllWidgets() -> bool;
77
78 auto EnterWidget(const ID<Widget>& widget) -> bool;
79 auto EnterUI(const ID<UI>& ui) -> bool;
80 auto LeaveWidget() -> bool;
81 auto LeaveUI() -> bool;
82
83 void Select();
84 void Deselect();
85 void Show();
86 void Hide();
87
88protected:
89 virtual auto OnTick() -> bool;
90 virtual void OnSelect();
91 virtual void OnDeselect();
92 virtual void OnShow();
93 virtual void OnHide();
94
95 friend class KTech::Memory;
96};
Complete engine containing all engine components.
Definition engine.hpp:41
Input callbacks creator and manager.
Definition callbackgroup.hpp:33
Engine component responsible for registering all world structures.
Definition memory.hpp:36
World structure that comprises Textures, behaves as a user interface element, and exists within UI.
Definition widget.hpp:45
std::vector< ChildWidget > m_childWidgets
Undocumented because it's planned to change (see GitHub issue #106).
Definition widget.hpp:62
auto RemoveAllWidgets() -> bool
Undocumented due to planned changes (see GitHub issue #106).
Definition widget.cpp:115
Engine & engine
Parent Engine.
Definition widget.hpp:57
auto EnterWidget(const ID< Widget > &widget) -> bool
Undocumented due to planned changes (see GitHub issue #106).
Definition widget.cpp:135
std::vector< Texture > m_textures
Comprising Textures.
Definition widget.hpp:67
Point m_pos
World position.
Definition widget.hpp:66
void Deselect()
Disable input callbacks, and call Widget::OnDisable().
Definition widget.cpp:214
ID< UI > m_parentUI
The UI containing this Widget.
Definition widget.hpp:60
auto AddWidget(const ID< Widget > &widget) -> bool
Undocumented due to planned changes (see GitHub issue #106).
Definition widget.cpp:70
std::string m_name
String name.
Definition widget.hpp:59
virtual void OnDeselect()
Virtual function called by Widget::Deselect().
Definition widget.cpp:298
virtual ~Widget()
Leave parent UI (if in one) and remove itself from Memory.
Definition widget.cpp:59
auto RemoveWidget(const ID< Widget > &widget) -> bool
Undocumented due to planned changes (see GitHub issue #106).
Definition widget.cpp:95
Input::CallbackGroup m_callbackGroup
Group of all input callbacks, which are enabled and disabled in correspondence to Widget::m_selected.
Definition widget.hpp:68
virtual auto OnTick() -> bool
Virtual function called once each tick.
Definition widget.cpp:274
virtual void OnShow()
Virtual function called by Widget::Show().
Definition widget.cpp:307
ID< Widget > m_parentWidget
Undocumented because it's planned to change (see GitHub issue #106).
Definition widget.hpp:61
Widget(Engine &engine, Point position=Point(0, 0), std::string name="")
Construct a Widget.
Definition widget.cpp:34
auto LeaveWidget() -> bool
Undocumented due to planned changes (see GitHub issue #106).
Definition widget.cpp:147
void Select()
Enable input callbacks, and call Widget::OnSelect().
Definition widget.cpp:193
bool m_selected
true: player input reaches the Widget. false: player input doesn't.
Definition widget.hpp:63
auto EnterUI(const ID< UI > &ui) -> bool
Enter a UI.
Definition widget.cpp:163
const ID< Widget > m_id
Personal ID.
Definition widget.hpp:58
virtual void OnSelect()
Virtual function called by Widget::Select().
Definition widget.cpp:287
void Hide()
Don't let UI render this Widget, and call Widget::OnHide().
Definition widget.cpp:253
virtual void OnHide()
Virtual function called by Widget::Hide().
Definition widget.cpp:316
bool m_shown
true: will be rendered by UI. false: will be ignored by UI.
Definition widget.hpp:64
auto LeaveUI() -> bool
Leave the parent UI.
Definition widget.cpp:176
void Show()
Let UI render this Widget, and call Widget::OnShow().
Definition widget.cpp:233
Serializable world structure identifier.
Definition id.hpp:38
2D vector, mostly used to store positions and directions.
Definition point.hpp:30