aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-27 14:25:32 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-27 14:25:32 +0100
commit7070f22d86057eafca3b82321d4146958c14a33e (patch)
tree9355ce7728a5ab9a9dbf964461b854591e7018d9 /src/crepe/api
parentad4de3ee60f699595bfd1950f88e5417fa29a0d5 (diff)
pr feedback
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Button.cpp7
-rw-r--r--src/crepe/api/Button.h52
-rw-r--r--src/crepe/api/Event.h7
-rw-r--r--src/crepe/api/UiObject.cpp2
-rw-r--r--src/crepe/api/UiObject.h2
5 files changed, 50 insertions, 20 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
index 547c0fc..077a5e7 100644
--- a/src/crepe/api/Button.cpp
+++ b/src/crepe/api/Button.cpp
@@ -1,5 +1,8 @@
#include "Button.h"
-using namespace crepe;
+namespace crepe {
-Button::Button(game_object_id_t id) : UiObject(id) {}
+Button::Button(game_object_id_t id, int width, int height, bool is_toggle, std::function<void()> on_click)
+ : UiObject(id, width, height), is_toggle(is_toggle), is_pressed(false), hover(false), on_click(on_click) {}
+
+} // namespace crepe
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
index 0056238..df6f1e0 100644
--- a/src/crepe/api/Button.h
+++ b/src/crepe/api/Button.h
@@ -1,7 +1,6 @@
#pragma once
#include <functional>
-
#include "UiObject.h"
namespace crepe {
@@ -9,34 +8,59 @@ namespace crepe {
/**
* \class Button
* \brief Represents a clickable UI button, derived from the UiObject class.
+ *
+ * This class provides functionality for a button in the UI, including toggle state,
+ * click handling, and mouse hover detection. A callback function can be provided to
+ * handle button clicks.
*/
class Button : public UiObject {
public:
/**
- * \brief Constructs a Button with the specified game object ID.
+ * \brief Constructs a Button with the specified game object ID and dimensions.
+ *
* \param id The unique ID of the game object associated with this button.
+ * \param width The width of the button.
+ * \param height The height of the button.
+ * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false.
+ * \param on_click callback function that will be invoked when the button is clicked.
*/
- Button(game_object_id_t id);
-
- //! Indicates if the button is interactable (can be clicked).
- bool interactable = true;
+ Button(game_object_id_t id, int width, int height, bool is_toggle = false, std::function<void()> on_click = nullptr);
- //! Indicates if the button is a toggle button (can be pressed and released).
- bool is_toggle = false;
+ /**
+ * \brief Indicates if the button is a toggle button (can be pressed and released).
+ *
+ * A toggle button allows for a pressed/released state, whereas a regular button
+ * typically only has an on-click state.
+ */
+ bool is_toggle;
- //! Indicates whether the button is currently pressed.
- bool is_pressed = false;
+ /**
+ * \brief Indicates whether the button is currently pressed.
+ *
+ * This state is true when the button is actively pressed and false otherwise.
+ */
+ bool is_pressed;
- //! Indicates whether the mouse is currently hovering over the button.
- bool hover = false;
+ /**
+ * \brief Indicates whether the mouse is currently hovering over the button.
+ *
+ * This is set to true when the mouse is over the button and false otherwise.
+ */
+ bool hover;
- //! The callback function to be executed when the button is clicked.
+ /**
+ * \brief The callback function to be executed when the button is clicked.
+ *
+ * This function is invoked whenever the button is clicked. It can be set to any
+ * function that matches the signature `void()`. Defaults to nullptr.
+ */
std::function<void()> on_click;
public:
/**
* \brief Retrieves the maximum number of instances allowed for this button type.
- * \return Always returns 1, as only a single instance is allowed.
+ *
+ * \return Always returns 1, as only a single instance of this type is allowed.
*/
virtual int get_instances_max() const override { return 1; }
};
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index b13abc1..a7d5511 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -88,10 +88,12 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int mouse_y = 0;
+
// Relative movement in x
- int rel_x;
+ int rel_x = 0;
+
// Relative movement in y
- int rel_y;
+ int rel_y = 0;
};
/**
@@ -104,6 +106,7 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int scroll_y = 0;
+
//! scroll direction (-1 = down, 1 = up)
int direction = 0;
};
diff --git a/src/crepe/api/UiObject.cpp b/src/crepe/api/UiObject.cpp
index 1c11fc3..987fc06 100644
--- a/src/crepe/api/UiObject.cpp
+++ b/src/crepe/api/UiObject.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-UiObject::UiObject(game_object_id_t id) : Component(id){};
+UiObject::UiObject(game_object_id_t id,int width,int height) : Component(id),width(width),height(height){};
diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h
index 7bd1c2e..6b0323e 100644
--- a/src/crepe/api/UiObject.h
+++ b/src/crepe/api/UiObject.h
@@ -14,7 +14,7 @@ public:
* \brief Constructs a UiObject with the specified game object ID.
* \param id The unique ID of the game object associated with this UI object.
*/
- UiObject(game_object_id_t id);
+ UiObject(game_object_id_t id,int width,int height);
//! The width of the UI object.
int width = 0;