aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Button.cpp13
-rw-r--r--src/crepe/api/Button.h69
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Event.h20
-rw-r--r--src/crepe/api/KeyCodes.h21
-rw-r--r--src/crepe/api/LoopManager.cpp6
-rw-r--r--src/crepe/api/UiObject.cpp8
-rw-r--r--src/crepe/api/UiObject.h33
8 files changed, 161 insertions, 13 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
new file mode 100644
index 0000000..c0ff5a8
--- /dev/null
+++ b/src/crepe/api/Button.cpp
@@ -0,0 +1,13 @@
+#include "Button.h"
+
+namespace crepe {
+
+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
new file mode 100644
index 0000000..2fa94ae
--- /dev/null
+++ b/src/crepe/api/Button.h
@@ -0,0 +1,69 @@
+#pragma once
+
+#include "UiObject.h"
+#include <functional>
+
+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 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, int width, int height, bool is_toggle = false,
+ std::function<void()> on_click = nullptr);
+
+ /**
+ * \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;
+
+ /**
+ * \brief Indicates whether the button is currently pressed.
+ *
+ * This state is true when the button is actively pressed and false otherwise.
+ */
+ bool is_pressed;
+
+ /**
+ * \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;
+
+ /**
+ * \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 of this type is allowed.
+ */
+ virtual int get_instances_max() const override { return 1; }
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 50c51ed..aeb451d 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -23,6 +23,8 @@ target_sources(crepe PUBLIC
Asset.cpp
EventHandler.cpp
Script.cpp
+ Button.cpp
+ UiObject.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -58,4 +60,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
LoopManager.h
LoopTimer.h
Asset.h
+ Button.h
+ UiObject.h
)
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index b267e3e..a7d5511 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -88,9 +88,29 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int mouse_y = 0;
+
+ // Relative movement in x
+ int rel_x = 0;
+
+ // Relative movement in y
+ int rel_y = 0;
};
/**
+ * \brief Event triggered when the mouse is moved.
+ */
+class MouseScrollEvent : public Event {
+public:
+ //! X-coordinate of the mouse position at the time of the event.
+ int scroll_x = 0;
+
+ //! 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;
+};
+/**
* \brief Event triggered during a collision between objects.
*/
class CollisionEvent : public Event {};
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index 9e173e0..fcfc080 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -1,5 +1,5 @@
#pragma once
-
+namespace crepe {
//! Enumeration for mouse button inputs, including standard and extended buttons.
enum class MouseButton {
NONE = 0, //!< No mouse button input.
@@ -85,9 +85,9 @@ enum class Keycode {
PRINT_SCREEN = 283, //!< Print Screen key.
PAUSE = 284, //!< Pause key.
/**
- * \name Function keys (F1-F25).
- * \{
- */
+ * \name Function keys (F1-F25).
+ * \{
+ */
F1 = 290,
F2 = 291,
F3 = 292,
@@ -115,9 +115,9 @@ enum class Keycode {
F25 = 314,
/// \}
/**
- * \name Keypad digits and operators.
- * \{
- */
+ * \name Keypad digits and operators.
+ * \{
+ */
KP0 = 320,
KP1 = 321,
KP2 = 322,
@@ -137,9 +137,9 @@ enum class Keycode {
KP_EQUAL = 336,
/// \}
/**
- * \name Modifier keys.
- * \{
- */
+ * \name Modifier keys.
+ * \{
+ */
LEFT_SHIFT = 340,
LEFT_CONTROL = 341,
LEFT_ALT = 342,
@@ -151,3 +151,4 @@ enum class Keycode {
/// \}
MENU = 348, //!< Menu key.
};
+} // namespace crepe
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 7edf4d1..b343250 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -2,6 +2,7 @@
#include "../system/AnimatorSystem.h"
#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
#include "../system/ParticleSystem.h"
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
@@ -20,11 +21,10 @@ LoopManager::LoopManager() {
this->load_system<PhysicsSystem>();
this->load_system<RenderSystem>();
this->load_system<ScriptSystem>();
+ this->load_system<InputSystem>();
}
-void LoopManager::process_input() {
- SDLContext::get_instance().handle_events(this->game_running);
-}
+void LoopManager::process_input() { this->get_system<InputSystem>().update(); }
void LoopManager::start() {
this->setup();
diff --git a/src/crepe/api/UiObject.cpp b/src/crepe/api/UiObject.cpp
new file mode 100644
index 0000000..7859a90
--- /dev/null
+++ b/src/crepe/api/UiObject.cpp
@@ -0,0 +1,8 @@
+#include "UiObject.h"
+
+using namespace crepe;
+
+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
new file mode 100644
index 0000000..c056877
--- /dev/null
+++ b/src/crepe/api/UiObject.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "../Component.h"
+
+namespace crepe {
+
+/**
+ * @class UiObject
+ * \brief Represents a UI object in the game, derived from the Component class.
+ */
+class UiObject : public Component {
+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, int width, int height);
+
+ //! The width of the UI object.
+ int width = 0;
+
+ //! The height of the UI object.
+ int height = 0;
+
+public:
+ /**
+ * \brief Retrieves the maximum number of instances allowed for this UI object type.
+ * /return Always returns 1, as only a single instance is allowed.
+ */
+ virtual int get_instances_max() const override { return 1; }
+};
+
+} // namespace crepe