cmdfx 0.2.1
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 <stdlib.h>
15 #include "cmdfx/core/events.h"
16 #include "cmdfx/ui/button.h"
17}
18
21#include <vector>
22#include <memory>
23
24namespace CmdFX {
25
34 class Button {
35 private:
36 CmdFX_Button* button;
37
38 public:
39 Button(CmdFX_Button* btn) : button(btn) {}
40 Button(Sprite& sprite, CmdFX_ButtonCallback callback) {
41 button = Button_create(sprite.getSprite(), callback);
42 }
43 Button(int width, int height, char c, char* ansi, int z, CmdFX_ButtonCallback callback) {
44 button = Button_createFilled(width, height, c, ansi, z, callback);
45 }
46 ~Button() {
47 if (button) { Button_free(button); }
48 }
49
50 CmdFX_Button* getButton() const {
51 return button;
52 }
53
59 return button->sprite;
60 }
61
62 int draw(int x, int y) {
63 return Button_draw(x, y, button);
64 }
65
66 int remove() {
67 return Button_remove(button);
68 }
69
70 bool isHidden() const {
71 return Button_isHidden(button) != 0;
72 }
73
74 int hide() {
75 return Button_hide(button);
76 }
77
78 int show() {
79 return Button_show(button);
80 }
81
82 int setData(char** data, char*** ansi) {
83 return Button_setData(button, data, ansi);
84 }
85
92 int setData(std::vector<std::string> data, std::vector<std::vector<std::string>> ansi) {
93 return Button_setData(button, to2DArray(data), to3DArray(ansi));
94 }
95
96 int moveTo(int x, int y) {
97 return Button_moveTo(button, x, y);
98 }
99
100 int moveBy(int dx, int dy) {
101 return Button_moveBy(button, dx, dy);
102 }
103 };
104
105 namespace Canvas {
110 std::vector<std::unique_ptr<Button>> getRegisteredButtons() {
113
114 std::vector<std::unique_ptr<Button>> buttonList;
115 buttonList.reserve(count);
116
117 for (int i = 0; i < count; i++) {
118 if (buttons[i] != nullptr) {
119 buttonList.emplace_back(std::make_unique<Button>(buttons[i]));
120 }
121 }
122
123 return buttonList;
124 }
125
126 int getRegisteredButtonsCount() {
128 }
129
130 std::vector<std::unique_ptr<Button>> getAllButtonsAt(int x, int y) {
131 CmdFX_Button** buttons = Canvas_getAllButtonsAt(x, y);
132 if (buttons == nullptr) return {};
133
134 std::vector<std::unique_ptr<Button>> buttonList;
135 for (int i = 0; buttons[i] != nullptr; i++) {
136 buttonList.emplace_back(std::make_unique<Button>(buttons[i]));
137 }
138
139 free(buttons);
140 return buttonList;
141 }
142
143 std::unique_ptr<Button> getButtonAt(int x, int y) {
144 CmdFX_Button* button = Canvas_getButtonAt(x, y);
145 if (button == nullptr) return nullptr;
146
147 return std::make_unique<Button>(button);
148 }
149 }
150
151}
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:92
CmdFX_Sprite * getSprite() const
Gets the value of the button's sprite.
Definition button.hpp:58
A C++ wrapper around a CmdFX_Sprite struct.
Definition sprites.hpp:32
CmdFX_Sprite * getSprite()
Get the Sprite object associated with this class.
Definition sprites.hpp:53
Events API for CmdFX.
C++ wrapper for the CmdFX canvas.
Definition canvas.hpp:23
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:110
Primary namespace for CmdFX.
Definition cmdfx.hpp:22
char ** to2DArray(std::vector< std::string > string)
Converts a 1D vector of strings to a 2D array of characters.
Definition builder.hpp:39
char *** to3DArray(std::vector< std::vector< std::string > > string)
Converts a 2D vector of strings to a 3D array of characters.
Definition builder.hpp:55
C++ Extensions for the CmdFX Sprites API.
Represents a CmdFX button.
Definition button.h:42
Represents a sprite that can be drawn to the terminal.
Definition sprites.h:30