cmdfx 0.3.2
Lightweight game engine for your terminal
Loading...
Searching...
No Matches
button.hpp
Go to the documentation of this file.
1
11#pragma once
12
13extern "C" {
14#include "cmdfx/core/events.h"
15#include "cmdfx/ui/button.h"
16#include <stdlib.h>
17}
18
21#include <memory>
22#include <vector>
23
24namespace CmdFX
25{
26
35class Button {
36 private:
37 CmdFX_Button* button;
38
39 public:
40 Button(CmdFX_Button* btn) : button(btn) {
41 }
42 Button(Sprite& sprite, CmdFX_ButtonCallback callback) {
43 button = Button_create(sprite.getSprite(), callback);
44 }
45 Button(
46 int width, int height, char c, char* ansi, int z,
48 ) {
49 button = Button_createFilled(width, height, c, ansi, z, callback);
50 }
51 ~Button() {
52 if (button) {
53 Button_free(button);
54 }
55 }
56
57 CmdFX_Button* getButton() const {
58 return button;
59 }
60
66 return button->sprite;
67 }
68
69 int draw(int x, int y) {
70 return Button_draw(x, y, button);
71 }
72
73 int remove() {
74 return Button_remove(button);
75 }
76
77 bool isHidden() const {
78 return Button_isHidden(button) != 0;
79 }
80
81 int hide() {
82 return Button_hide(button);
83 }
84
85 int show() {
86 return Button_show(button);
87 }
88
89 int setData(char** data, char*** ansi) {
90 return Button_setData(button, data, ansi);
91 }
92
102 std::vector<std::string> data,
103 std::vector<std::vector<std::string>> ansi
104 ) {
105 return Button_setData(button, to2DArray(data), to3DArray(ansi));
106 }
107
108 int moveTo(int x, int y) {
109 return Button_moveTo(button, x, y);
110 }
111
112 int moveBy(int dx, int dy) {
113 return Button_moveBy(button, dx, dy);
114 }
115};
116
117namespace Canvas
118{
124std::vector<std::unique_ptr<Button>> getRegisteredButtons() {
127
128 std::vector<std::unique_ptr<Button>> buttonList;
129 buttonList.reserve(count);
130
131 for (int i = 0; i < count; i++) {
132 if (buttons[i] != nullptr) {
133 buttonList.emplace_back(std::make_unique<Button>(buttons[i]));
134 }
135 }
136
137 return buttonList;
138}
139
140int getRegisteredButtonsCount() {
142}
143
144std::vector<std::unique_ptr<Button>> getAllButtonsAt(int x, int y) {
145 CmdFX_Button** buttons = Canvas_getAllButtonsAt(x, y);
146 if (buttons == nullptr) return {};
147
148 std::vector<std::unique_ptr<Button>> buttonList;
149 for (int i = 0; buttons[i] != nullptr; i++) {
150 buttonList.emplace_back(std::make_unique<Button>(buttons[i]));
151 }
152
153 free(buttons);
154 return buttonList;
155}
156
157std::unique_ptr<Button> getButtonAt(int x, int y) {
158 CmdFX_Button* button = Canvas_getButtonAt(x, y);
159 if (button == nullptr) return nullptr;
160
161 return std::make_unique<Button>(button);
162}
163} // namespace Canvas
164
165} // namespace CmdFX
C++ Extensions for the Builder API.
Button declarations for the CmdFX UI Library.
CmdFX_Button ** Canvas_getAllButtonsAt(int x, int y)
Gets all buttons at the given position.
int Button_isHidden(CmdFX_Button *button)
Checks if a button is hidden.
CmdFX_Button * Canvas_getButtonAt(int x, int y)
Gets the button at the given position.
CmdFX_Button ** Canvas_getRegisteredButtons()
Gets an array of registered buttons.
int Button_show(CmdFX_Button *button)
Shows a button in the UI manager if it is already hidden.
int Button_moveTo(CmdFX_Button *button, int x, int y)
Moves the button to the given position.
void Button_free(CmdFX_Button *button)
Frees the memory associated with a button.
int Canvas_getRegisteredButtonsCount()
Gets the number of registered buttons.
struct CmdFX_Button CmdFX_Button
Represents a CmdFX button.
void(* CmdFX_ButtonCallback)(void *button, CmdFX_MouseEvent *event, unsigned long long time)
The callback function to be called when the button is clicked.
Definition button.h:33
CmdFX_Button * Button_create(CmdFX_Sprite *sprite, CmdFX_ButtonCallback callback)
Creates a new button.
int Button_hide(CmdFX_Button *button)
Hides a button from the UI manager.
int Button_draw(int x, int y, CmdFX_Button *button)
Draws the button on the screen.
CmdFX_Button * Button_createFilled(int width, int height, char c, char *ansi, int z, CmdFX_ButtonCallback callback)
Creates a new filled button.
int Button_remove(CmdFX_Button *button)
Removes a button from the UI manager.
int Button_moveBy(CmdFX_Button *button, int dx, int dy)
Moves the button by the given offset.
int Button_setData(CmdFX_Button *button, char **data, char ***ansi)
Sets the data for a button.
int setData(std::vector< std::string > data, std::vector< std::vector< std::string > > ansi)
Sets the data for the button using vectors.
Definition button.hpp:101
CmdFX_Sprite * getSprite() const
Gets the value of the button's sprite.
Definition button.hpp:65
A C++ wrapper around a CmdFX_Sprite struct.
Definition sprites.hpp:33
CmdFX_Sprite * getSprite()
Get the Sprite object associated with this class.
Definition sprites.hpp:60
Events API for CmdFX.
C++ wrapper for the CmdFX canvas.
Definition canvas.hpp:25
std::vector< std::unique_ptr< Button > > getRegisteredButtons()
Gets all registered buttons in the UI manager as a vector of unique_ptr<Button>.
Definition button.hpp:124
Primary namespace for CmdFX.
Definition cmdfx.hpp:26
char ** to2DArray(std::vector< std::string > string)
Converts a 1D vector of strings to a 2D array of characters.
Definition builder.hpp:64
char *** to3DArray(std::vector< std::vector< std::string > > string)
Converts a 2D vector of strings to a 3D array of characters.
Definition builder.hpp:80
C++ Extensions for the CmdFX Sprites API.
Represents a CmdFX button.
Definition button.h:45
Represents a sprite that can be drawn to the terminal.
Definition sprites.h:31