KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
frame.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#include "../ktech.hpp"
24
28class Frame : public KTech::Widget
29{
30public:
43 KTech::Point position,
44 KTech::UPoint size,
45 KTech::RGBA unselected = KTech::RGBAColors::gray,
46 KTech::RGBA selected = KTech::RGBAColors::white)
47 : Widget(engine, ui, position), m_unselectedRGBA(unselected), m_selectedRGBA(selected)
48 {
49 // Texture
50 SetSize(size);
51 }
52
59 {
60 KTech::RGBA tempRGBA = m_selected ? m_selectedRGBA : m_unselectedRGBA;
61 m_textures.resize(TEXTURES_SIZE);
62 m_textures[ti_topLeftCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(0, 0));
63 m_textures[ti_topRightCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(size.x - 1, 0));
64 m_textures[ti_bottomLeftCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(0, size.y - 1));
65 m_textures[ti_bottomRightCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(size.x - 1, size.y - 1));
66 m_textures[ti_topFrame].Simple(KTech::UPoint(size.x - 2, 1), KTech::CellA('-', tempRGBA), KTech::Point(1, 0));
67 m_textures[ti_leftFrame].Simple(KTech::UPoint(1, size.y - 2), KTech::CellA('|', tempRGBA), KTech::Point(0, 1));
68 m_textures[ti_bottomFrame].Simple(KTech::UPoint(size.x - 2, 1), KTech::CellA('-', tempRGBA), KTech::Point(1, size.y - 1));
69 m_textures[ti_rightFrame].Simple(KTech::UPoint(1, size.y - 2), KTech::CellA('|', tempRGBA), KTech::Point(size.x - 1, 1));
70 }
71
72private:
73 enum TextureIndex : size_t
74 {
75 ti_topLeftCorner,
76 ti_topRightCorner,
77 ti_bottomLeftCorner,
78 ti_bottomRightCorner,
79 ti_topFrame,
80 ti_leftFrame,
81 ti_bottomFrame,
82 ti_rightFrame,
83 TEXTURES_SIZE
84 };
85
86 KTech::RGBA m_unselectedRGBA, m_selectedRGBA;
87
88 void OnSelect() override
89 {
90 RenderSelected();
91 }
92
93 void OnDeselect() override
94 {
95 RenderUnselected();
96 }
97
98 void RenderSelected()
99 {
100 for (KTech::Texture& texture : m_textures)
101 {
102 texture.Transform([&](KTech::CellA& cell){ cell.f = m_selectedRGBA; });
103 }
104 }
105
106 void RenderUnselected()
107 {
108 for (KTech::Texture& texture : m_textures)
109 {
110 texture.Transform([&](KTech::CellA& cell){ cell.f = m_unselectedRGBA; });
111 }
112 }
113};
Widget that looks like a frame.
Definition frame.hpp:29
Frame(KTech::Engine &engine, KTech::ID< KTech::UI > ui, KTech::Point position, KTech::UPoint size, KTech::RGBA unselected=KTech::RGBAColors::gray, KTech::RGBA selected=KTech::RGBAColors::white)
Construct a Frame.
Definition frame.hpp:41
void SetSize(KTech::UPoint size)
Resize the frame.
Definition frame.hpp:58
Complete engine containing all engine components.
Definition engine.hpp:41
World structure that comprises Textures, behaves as a user interface element, and exists within UI.
Definition widget.hpp:45
Engine & engine
Parent Engine.
Definition widget.hpp:57
std::vector< Texture > m_textures
Comprising Textures.
Definition widget.hpp:67
Widget(Engine &engine, Point position=Point(0, 0), std::string name="")
Construct a Widget.
Definition widget.cpp:34
bool m_selected
true: player input reaches the Widget. false: player input doesn't.
Definition widget.hpp:63
Like Cell, but with RGBA foreground and background colors, instead of RGB.
Definition cella.hpp:32
RGBA f
Foreground (character) color.
Definition cella.hpp:33
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
uint32_t y
Y axis.
Definition upoint.hpp:31
uint32_t x
X axis.
Definition upoint.hpp:30