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.h67
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Event.h22
-rw-r--r--src/crepe/api/EventHandler.h58
-rw-r--r--src/crepe/api/IKeyListener.h22
-rw-r--r--src/crepe/api/IMouseListener.h42
-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.h25
11 files changed, 212 insertions, 72 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
new file mode 100644
index 0000000..76f74f0
--- /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..26e7526
--- /dev/null
+++ b/src/crepe/api/Button.h
@@ -0,0 +1,67 @@
+#pragma once
+
+#include <functional>
+
+#include "UIObject.h"
+
+namespace crepe {
+
+//! 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;
+ // TODO: create separate toggle button class
+ /**
+ * \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;
+ //! Indicates whether the toggle button is pressed
+ bool is_pressed = false;
+ //! Indicates whether the mouse is currently hovering over the button
+ bool hover = false;
+
+public:
+};
+
+} // namespace crepe
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 0808612..b186e0c 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -19,6 +19,8 @@ target_sources(crepe PUBLIC
Asset.cpp
EventHandler.cpp
Script.cpp
+ Button.cpp
+ UIObject.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -47,4 +49,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/EventHandler.h b/src/crepe/api/EventHandler.h
index ef659fd..7bdd9a3 100644
--- a/src/crepe/api/EventHandler.h
+++ b/src/crepe/api/EventHandler.h
@@ -29,29 +29,29 @@ using EventHandler = std::function<bool(const EventType & e)>;
class IEventHandlerWrapper {
public:
/**
- * \brief Virtual destructor for IEventHandlerWrapper.
- */
+ * \brief Virtual destructor for IEventHandlerWrapper.
+ */
virtual ~IEventHandlerWrapper() = default;
/**
- * \brief Executes the handler with the given event.
- *
- * This method calls the `call()` method of the derived class, passing the event to the handler.
- *
- * \param e The event to be processed.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief Executes the handler with the given event.
+ *
+ * This method calls the `call()` method of the derived class, passing the event to the handler.
+ *
+ * \param e The event to be processed.
+ * \return A boolean value indicating whether the event is handled.
+ */
bool exec(const Event & e);
private:
/**
- * \brief The method responsible for handling the event.
- *
- * This method is implemented by derived classes to process the event.
- *
- * \param e The event to be processed.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief The method responsible for handling the event.
+ *
+ * This method is implemented by derived classes to process the event.
+ *
+ * \param e The event to be processed.
+ * \return A boolean value indicating whether the event is handled.
+ */
virtual bool call(const Event & e) = 0;
};
@@ -69,23 +69,23 @@ template <typename EventType>
class EventHandlerWrapper : public IEventHandlerWrapper {
public:
/**
- * \brief Constructs an EventHandlerWrapper with a given handler.
- *
- * The constructor takes an event handler function and stores it in the wrapper.
- *
- * \param handler The event handler function.
- */
+ * \brief Constructs an EventHandlerWrapper with a given handler.
+ *
+ * The constructor takes an event handler function and stores it in the wrapper.
+ *
+ * \param handler The event handler function.
+ */
explicit EventHandlerWrapper(const EventHandler<EventType> & handler);
private:
/**
- * \brief Calls the stored event handler with the event.
- *
- * This method casts the event to the appropriate type and calls the handler.
- *
- * \param e The event to be handled.
- * \return A boolean value indicating whether the event is handled.
- */
+ * \brief Calls the stored event handler with the event.
+ *
+ * This method casts the event to the appropriate type and calls the handler.
+ *
+ * \param e The event to be handled.
+ * \return A boolean value indicating whether the event is handled.
+ */
bool call(const Event & e) override;
//! The event handler function.
EventHandler<EventType> handler;
diff --git a/src/crepe/api/IKeyListener.h b/src/crepe/api/IKeyListener.h
index 6ded107..180a0a6 100644
--- a/src/crepe/api/IKeyListener.h
+++ b/src/crepe/api/IKeyListener.h
@@ -14,9 +14,9 @@ namespace crepe {
class IKeyListener {
public:
/**
- * \brief Constructs an IKeyListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
+ * \brief Constructs an IKeyListener with a specified channel.
+ * \param channel The channel ID for event handling.
+ */
IKeyListener(event_channel_t channel = EventManager::CHANNEL_ALL);
virtual ~IKeyListener();
IKeyListener(const IKeyListener &) = delete;
@@ -25,17 +25,17 @@ public:
IKeyListener(IKeyListener &&) = delete;
/**
- * \brief Pure virtual function to handle key press events.
- * \param event The key press event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Pure virtual function to handle key press events.
+ * \param event The key press event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_key_pressed(const KeyPressEvent & event) = 0;
/**
- * \brief Pure virtual function to handle key release events.
- * \param event The key release event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Pure virtual function to handle key release events.
+ * \param event The key release event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_key_released(const KeyReleaseEvent & event) = 0;
private:
diff --git a/src/crepe/api/IMouseListener.h b/src/crepe/api/IMouseListener.h
index 9e4fdf7..e19897d 100644
--- a/src/crepe/api/IMouseListener.h
+++ b/src/crepe/api/IMouseListener.h
@@ -14,9 +14,9 @@ namespace crepe {
class IMouseListener {
public:
/**
- * \brief Constructs an IMouseListener with a specified channel.
- * \param channel The channel ID for event handling.
- */
+ * \brief Constructs an IMouseListener with a specified channel.
+ * \param channel The channel ID for event handling.
+ */
IMouseListener(event_channel_t channel = EventManager::CHANNEL_ALL);
virtual ~IMouseListener();
IMouseListener & operator=(const IMouseListener &) = delete;
@@ -25,36 +25,36 @@ public:
IMouseListener(IMouseListener &&) = delete;
/**
- * \brief Move assignment operator (deleted).
- */
+ * \brief Move assignment operator (deleted).
+ */
IMouseListener & operator=(IMouseListener &&) = delete;
/**
- * \brief Handles a mouse click event.
- * \param event The mouse click event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse click event.
+ * \param event The mouse click event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_clicked(const MouseClickEvent & event) = 0;
/**
- * \brief Handles a mouse press event.
- * \param event The mouse press event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse press event.
+ * \param event The mouse press event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_pressed(const MousePressEvent & event) = 0;
/**
- * \brief Handles a mouse release event.
- * \param event The mouse release event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse release event.
+ * \param event The mouse release event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_released(const MouseReleaseEvent & event) = 0;
/**
- * \brief Handles a mouse move event.
- * \param event The mouse move event to handle.
- * \return True if the event was handled, false otherwise.
- */
+ * \brief Handles a mouse move event.
+ * \param event The mouse move event to handle.
+ * \return True if the event was handled, false otherwise.
+ */
virtual bool on_mouse_moved(const MouseMoveEvent & event) = 0;
private:
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..d239b89
--- /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..f7f4fba
--- /dev/null
+++ b/src/crepe/api/UIObject.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "../Component.h"
+
+namespace crepe {
+
+/**
+ * \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