aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/Button.cpp11
-rw-r--r--src/crepe/api/Button.h79
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Event.h22
-rw-r--r--src/crepe/api/KeyCodes.h21
-rw-r--r--src/crepe/api/LoopManager.cpp4
-rw-r--r--src/crepe/api/UIObject.cpp8
-rw-r--r--src/crepe/api/UIObject.h26
8 files changed, 164 insertions, 11 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
new file mode 100644
index 0000000..313ec4c
--- /dev/null
+++ b/src/crepe/api/Button.cpp
@@ -0,0 +1,11 @@
+#include "Button.h"
+
+namespace crepe {
+
+Button::Button(game_object_id_t id, const vec2& dimensions, const vec2& offset,
+ const std::function<void()>& on_click, bool is_toggle)
+ : UIObject(id, dimensions, offset),
+ is_toggle(is_toggle),
+ 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..baee71f
--- /dev/null
+++ b/src/crepe/api/Button.h
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <functional>
+
+#include "UIObject.h"
+
+namespace crepe {
+
+/**
+ * \brief Represents a clickable UI button, derived from the UiObject class.
+ *
+ */
+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 Dimensions The width and height of the UIObject
+ * \param offset The offset relative this GameObjects Transform
+ * \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, const vec2 & dimensions, const vec2 & offset, const std::function<void()>& on_click,
+ 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 = false;
+
+ /**
+ * \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()`.
+ */
+ std::function<void()> on_click = nullptr;
+
+ /**
+ * \brief Callback function to be executed when the mouse enters the button's boundaries.
+ *
+ * This function is triggered when the mouse cursor moves over the button, allowing
+ * custom actions like visual effects, highlighting, or sound effects.
+ */
+ std::function<void()> on_mouse_enter = nullptr;
+
+ /**
+ * \brief Callback function to be executed when the mouse exits the button's boundaries.
+ *
+ * This function is triggered when the mouse cursor moves out of the button's area,
+ * allowing custom actions like resetting visual effects or playing exit-related effects.
+ */
+ std::function<void()> on_mouse_exit = nullptr;
+
+private:
+ //! friend relation for is_pressed and hover variables
+ friend class InputSystem;
+ /**
+ * \brief Indicates whether the toggle button is pressed
+ *
+ * This state indicates if the toggle button is pressed or not
+ */
+ bool is_pressed = 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 = false;
+
+public:
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 7da9dca..ac8f301 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -20,6 +20,8 @@ target_sources(crepe PUBLIC
Asset.cpp
EventHandler.cpp
Script.cpp
+ Button.cpp
+ UIObject.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -50,4 +52,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..6298118 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -88,9 +88,31 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int mouse_y = 0;
+
+ // Movement since last event in x
+ int delta_x = 0;
+
+ // Movement since last event in y
+ int delta_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 mouse_x = 0;
+
+ //! Y-coordinate of the mouse position at the time of the event.
+ int mouse_y = 0;
+
+ //! scroll direction (-1 = down, 1 = up)
+ int scroll_direction = 0;
+ //! scroll amount in y axis (from and away from the person).
+ float scroll_delta = 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 731cfb7..dc2d9e0 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -1,5 +1,6 @@
#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,9 +21,10 @@ LoopManager::LoopManager() {
this->load_system<PhysicsSystem>();
this->load_system<RenderSystem>();
this->load_system<ScriptSystem>();
+ this->load_system<InputSystem>();
}
-void LoopManager::process_input() { this->sdl_context.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..784328a
--- /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, const vec2 & dimensions, const vec2 &offset)
+ : Component(id),
+ dimensions(dimensions),
+ offset(offset) {}
diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h
new file mode 100644
index 0000000..614794d
--- /dev/null
+++ b/src/crepe/api/UIObject.h
@@ -0,0 +1,26 @@
+#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.
+ * \param dimensions width and height of the UIObject
+ * \param offset Offset relative to the GameObject Transform
+ */
+ UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset);
+ //! Width and height of the UIObject
+ vec2 dimensions;
+ //! Position offset relative to this GameObjects Transform
+ vec2 offset;
+};
+
+} // namespace crepe