Brunot
Loading...
Searching...
No Matches
Menu.h
Go to the documentation of this file.
1
16// ____ __ __ __
17// /\__ _\/\ \ /\ \/\ \
18// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
19// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
20// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
21// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
22// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
23
24
25#pragma once
26#include "Button.h"
27#include "Framework/Component.h"
28#include "Component/Transform.h"
29
30enum class menuType
31{
34};
35
40class Menu : public Component
41{
42public:
43#pragma region static_functions
44 static auto bindLuaTypes(sol::state& lua) -> void;
45 static auto getEnum() -> ComponentTypeEnum
46 {
47 return cMenu;
48 }
49
50#pragma endregion
51
52 Menu(float gapSize, int widthSize, int heightSize, menuType typeMenu);
53
55 : Component(cMenu, "Menu")
56 {
57 }
58
59 // If you have messages, you will need to un-default the copy and move constructor and call linkMessages in them
60 Menu(const Menu&) = default;
61 auto operator=(const Menu&) -> Menu& = default;
62 ~Menu() override = default;
63 Menu(Menu&& other) noexcept = default;
64 auto operator=(Menu&& other) noexcept -> Menu& = default;
65
66#pragma region overriden_functions
67
68 auto clone() const -> std::unique_ptr<GameObject> override { return std::make_unique<Menu>(*this); }
69
70 auto componentShowMenu() -> void override;
71 // for debug ImGUI
72 auto onUpdate(float dt) -> void override;
73
74 auto endWindow() -> void override {} // for debug ImGUI
75 auto onRender() -> void override;
76 auto onEnterEngine() -> void override;
77 auto load(const Stream& stream) -> void override;
78 auto save(Stream& stream) const -> void override;
79
80 friend auto to_json(json& j, const Menu& m) -> void;
81
82 friend auto from_json(const json& j, Menu& m) -> void;
83
84
85#pragma endregion
86
87#pragma region Menu_functions
88
89#pragma endregion
90
94 auto width() const -> int;
95
99 auto sizeWidth() const -> int;
100
104 auto height() const -> int;
105
107 auto setOrigin(Vector4D newOrigin) -> void;
108
110 auto setOrigin(float x, float y) -> void;
111
112
114 auto setOriginX(float x) -> void;
116 auto setOriginY(float y) -> void;
117
119 auto moveOrigin(float x, float y) -> void;
120
124 auto createEmptyButton() -> std::unique_ptr<Entity>;
125
129 auto getTranslationOfIndex(int x, int y) -> Point2D;
130
131
136 auto pushEntityHorizontal(Entity* entity) -> void;
137
144 auto pushEntity(Entity* entity) -> bool;
145
151 auto makeAndPush(const std::string& name) -> void;
152
156 auto removeEntity(int x, int y) -> void;
157
161 auto removeEntity(Entity* entity) -> void;
162
166 auto getAboveEntity(Entity* entity) -> Entity*;
170 auto getBelowEntity(Entity* entity) -> Entity*;
174 auto getLeftEntity(Entity* entity) -> Entity*;
178 auto getRightEntity(Entity* entity) -> Entity*;
179
183 auto getMenuElementPosition(Entity* entity) const -> std::pair<int, int>;
184
188 auto updateMenuChildren() -> void;
189
193 auto compactMenu() -> void;
194
198 auto horizontalCompactMenu() -> void;
199
207 auto at(int x, int y) -> Entity*;
208
212 auto getButtonRow(int x) const -> std::vector<Entity*>;
213
217 auto getButtonColumn(int y) const -> std::vector<Entity*>;
218
222 auto clear() const -> void;
223
224
225 auto fillMenu() -> void;
226
230 auto getChildScale() const -> Vector4D;
231
235 auto setGapBetweenObjects(float newGap) -> void;
236
237 auto drawDebugLines(AffineMatrix transform) -> void;
238
240 auto getChildTranslation(Entity* child) const -> Vector4D;
241
242
243#pragma region messaging_functions
244 auto linkMessages() -> void;
245
246#pragma endregion
247
248private:
249
250#pragma region helper_struct
251
252 // Struct that contains a bunch of information for calculating where children should be positioned
254 {
255 // Scale of children inside the menu
258
259 // Position for where children start out
261 // How far the children should be moved right to travel one space
263 // How far the children should be moved up to travel one space
265 };
266
267#pragma endregion
268
269#pragma region member_variables
270
272 float gapBetweenObjects = 0.01f;
277
278 Vector4D originPoint{0.f, 0.f, 0, 1};
279 Vector4D menuScale{1.f, 1.f, 0.f, 0.f};
280
285
286
288 std::vector<std::vector<Entity*>> buttons;
289
290#pragma endregion
291
292#pragma region helper_functions
293
297 auto returnWrappedMenuPosition(int x, int y) -> Point2D;
301 auto returnDefaultMenuPosition(int x, int y) -> Point2D;
302
306 auto returnButtonIndexWrapped(int x, int y) -> Entity*;
310 auto returnButtonIndexDefault(int x, int y) -> Entity*;
311
316 auto getButtonPosition(Entity* ent) -> std::pair<int, int>;
317
323
329
333 auto repositionItems(const std::vector<Entity*>& ent, int column) -> void;
334
339 auto repositionHorizontalItems(const std::vector<Entity*>& ent, int column) -> void;
340
341#pragma endregion
342
343#pragma region static_variables
344
345#pragma endregion
346
347};
sys::Json Stream
Definition AudioObject.h:20
A class that can be "activated" to broadcast a message.
The base class for components, holding all of their shared All components should inherit from this.
nlohmann::json json
Definition Json.cpp:19
menuType
Definition Menu.h:31
@ mWrapped
Definition Menu.h:33
@ mDefault
Definition Menu.h:32
Vector4D Point2D
Definition Transform.h:22
ComponentTypeEnum
This enum lists out every type of component we have.
Definition Component.h:39
Component(ComponentTypeEnum type, const char *typeName)
Definition Component.cpp:72
Definition Entity.h:41
friend Entity
Definition GameObject.h:90
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24
A menu that organizes its parents' children (in the node) either horizontally or vertically.
Definition Menu.h:41
auto setGapBetweenObjects(float newGap) -> void
Manually sets the gaps between objects.
Definition Menu.cpp:720
auto linkMessages() -> void
Definition Menu.cpp:522
auto getRightEntity(Entity *entity) -> Entity *
When given an entity, it will return the one right in the menu.
Definition Menu.cpp:475
menuType type
How to tell if the menu wraps.
Definition Menu.h:282
auto getMenuElementPosition(Entity *entity) const -> std::pair< int, int >
Returns the index of the entity in the menu.
Definition Menu.cpp:481
static auto getEnum() -> ComponentTypeEnum
Definition Menu.h:45
auto returnButtonIndexWrapped(int x, int y) -> Entity *
Returns the menu position while accounting for wrapped logic, intended as a helper function for getBu...
Definition Menu.cpp:580
auto updateMenuChildren() -> void
Resizes menu in memory, allocates new items, and then calls compact menu.
Definition Menu.cpp:491
auto createEmptyButton() -> std::unique_ptr< Entity >
Creates an empty button, intended for filling the board.
Definition Menu.cpp:886
auto at(int x, int y) -> Entity *
takes in a position, and gets the entity in the position, blocking overflow by locking to edges,...
Definition Menu.cpp:633
auto setOriginX(float x) -> void
Sets just the X of the origin point.
Definition Menu.cpp:865
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Menu.h:68
std::vector< std::vector< Entity * > > buttons
The managed items/entities for the menu.
Definition Menu.h:288
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Menu.cpp:235
int maxItemsWide
How wide menu grid is (how many objects it can hold horizontally).
Definition Menu.h:274
auto repositionHorizontalItems(const std::vector< Entity * > &ent, int column) -> void
Visually arranges the entities in a given column, intended as a helper function for compactMenu.
Definition Menu.cpp:819
auto pushEntity(Entity *entity) -> bool
A universal push function that automatically fills in empty spaces The Entity should already be a chi...
Definition Menu.cpp:357
auto getButtonColumn(int y) const -> std::vector< Entity * >
Returns every item in a given column.
Definition Menu.cpp:659
static auto bindLuaTypes(sol::state &lua) -> void
Definition Menu.cpp:50
auto returnButtonIndexDefault(int x, int y) -> Entity *
Returns the menu position while accounting for default logic, intended as a helper function for getBu...
Definition Menu.cpp:605
auto returnWrappedMenuPosition(int x, int y) -> Point2D
Returns the menu position while accounting for wrapped logic, intended as a helper function for retur...
Definition Menu.cpp:526
auto operator=(Menu &&other) noexcept -> Menu &=default
Menu(Menu &&other) noexcept=default
auto getButtonPosition(Entity *ent) -> std::pair< int, int >
takes in the entity and returns the index (x and y) of where it is in the menu
Definition Menu.cpp:733
auto compactMenu() -> void
Visually shrinks the menu, intended for when an element gets removed.
Definition Menu.cpp:248
auto getAboveEntity(Entity *entity) -> Entity *
When given an entity, it will return the one above in the menu.
Definition Menu.cpp:457
Vector4D menuScale
Definition Menu.h:279
auto moveOrigin(float x, float y) -> void
Adjusts the current x and y of the origin point.
Definition Menu.cpp:878
auto componentShowMenu() -> void override
Definition Menu.cpp:85
auto getChildTranslation(Entity *child) const -> Vector4D
Returns where a child inside the menu should be positioned.
Definition Menu.cpp:725
Vector4D originPoint
Definition Menu.h:278
PositioningInformation posInfo
Used to keep track of where items in the menu should be put in the game.
Definition Menu.h:284
auto getButtonRow(int x) const -> std::vector< Entity * >
Returns every item in a given row.
Definition Menu.cpp:643
friend auto from_json(const json &j, Menu &m) -> void
Definition Menu.cpp:917
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Menu.cpp:240
auto returnDefaultMenuPosition(int x, int y) -> Point2D
Returns the menu position while accounting for default logic, intended as a helper function for retur...
Definition Menu.cpp:552
int maxItemsHeight
How tall the menu grid is (how many objects it can hold vertically).
Definition Menu.h:276
auto sizeWidth() const -> int
Returns the current width of the menu.
Definition Menu.cpp:840
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Menu.cpp:198
auto makeAndPush(const std::string &name) -> void
Definition Menu.cpp:408
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Menu.h:74
auto getLeftEntity(Entity *entity) -> Entity *
When given an entity, it will return the one left in the menu.
Definition Menu.cpp:469
~Menu() override=default
friend auto to_json(json &j, const Menu &m) -> void
Definition Menu.cpp:904
auto getBelowEntity(Entity *entity) -> Entity *
When given an entity, it will return the one below in the menu.
Definition Menu.cpp:463
auto repositionItems(const std::vector< Entity * > &ent, int column) -> void
Visually arranges the entities in a given column, intended as a helper function for compactMenu.
Definition Menu.cpp:802
auto getChildScale() const -> Vector4D
Returns what the scale of a child inside the menu should be.
Definition Menu.cpp:715
auto removeEntity(int x, int y) -> void
Removes a specific entity at given index.
Definition Menu.cpp:424
auto getTranslationOfIndex(int x, int y) -> Point2D
Returns the translation of an item at given index.
Definition Menu.cpp:330
auto drawDebugLines(AffineMatrix transform) -> void
Definition Menu.cpp:203
auto setOrigin(Vector4D newOrigin) -> void
Changes the origin point to a new value.
Definition Menu.cpp:850
auto clear() const -> void
Deletes all entities inside of the grid.
Definition Menu.cpp:664
auto onUpdate(float dt) -> void override
called once every frame.
Definition Menu.cpp:183
auto height() const -> int
Returns the height of the menu.
Definition Menu.cpp:845
auto pushEntityHorizontal(Entity *entity) -> void
Pushes an item to the first row of a menu, intended for the cards.
Definition Menu.cpp:341
auto fillMenu() -> void
Definition Menu.cpp:679
Menu(const Menu &)=default
void recalculatePositioning()
Recalculates where children inside the menu should be positioned + scaled in the game.
Definition Menu.cpp:751
float gapBetweenObjects
How much visual space is put between objects in the menu.
Definition Menu.h:272
auto operator=(const Menu &) -> Menu &=default
Menu()
Definition Menu.h:54
auto setOriginY(float y) -> void
Sets just the Y of the origin point.
Definition Menu.cpp:871
Menu(float gapSize, int widthSize, int heightSize, menuType typeMenu)
Definition Menu.cpp:76
void recalculateHorizontalPositioning()
Recalculates where children inside the menu should be positioned + scaled in the game.
Definition Menu.cpp:778
auto width() const -> int
Returns the maximum width of the menu.
Definition Menu.cpp:835
auto horizontalCompactMenu() -> void
Visually shrinks the menu, intended for when an element gets removed from a player hand.
Definition Menu.cpp:289
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition Menu.cpp:227
Definition AffineMatrix.h:30
Definition Menu.h:254
Vector4D right
Definition Menu.h:262
float objectWidth
Definition Menu.h:256
Vector4D up
Definition Menu.h:264
float objectHeight
Definition Menu.h:257
Point2D start
Definition Menu.h:260
Definition Vector4D.h:28