diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Button.cpp | 11 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 89 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 7 | ||||
-rw-r--r-- | src/crepe/api/Event.h | 20 | ||||
-rw-r--r-- | src/crepe/api/Font.cpp | 45 | ||||
-rw-r--r-- | src/crepe/api/Font.h | 60 | ||||
-rw-r--r-- | src/crepe/api/KeyCodes.h | 21 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 42 | ||||
-rw-r--r-- | src/crepe/api/UiObject.cpp | 8 | ||||
-rw-r--r-- | src/crepe/api/UiObject.h | 33 |
11 files changed, 329 insertions, 13 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp new file mode 100644 index 0000000..d325014 --- /dev/null +++ b/src/crepe/api/Button.cpp @@ -0,0 +1,11 @@ +#include "Button.h" + +namespace crepe { + +Button::Button(game_object_id_t id, int width, int height, std::function<void()> on_click, + bool is_toggle) + : UiObject(id, width, height), + 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..0dfad5d --- /dev/null +++ b/src/crepe/api/Button.h @@ -0,0 +1,89 @@ +#pragma once + +#include <functional> + +#include "UiObject.h" + + +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, 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; + + /** + * \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_enter; + + /** + * \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_exit; + +private: + friend class InputSystem; + /** + * \brief Indicates whether the button is currently pressed. + * + * This state is true when the button is actively pressed and false otherwise. + */ + 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: + /** + * \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..2d86968 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -23,6 +23,9 @@ target_sources(crepe PUBLIC Asset.cpp EventHandler.cpp Script.cpp + Button.cpp + UiObject.cpp + Font.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -58,4 +61,8 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES LoopManager.h LoopTimer.h Asset.h + Button.h + UiObject.h + Font.h + Text.h ) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index b267e3e..91a30b5 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; + + // Movement since last event in x + int rel_x = 0; + + // Movement since last event 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/Font.cpp b/src/crepe/api/Font.cpp new file mode 100644 index 0000000..8db4b23 --- /dev/null +++ b/src/crepe/api/Font.cpp @@ -0,0 +1,45 @@ +#include <SDL2/SDL_ttf.h> + +#include "facade/SDLContext.h" +#include "util/Log.h" + +#include "Asset.h" +#include "Font.h" + +using namespace crepe; +using namespace std; + +Font::Font(const Asset &src, int size) : font_size(size) { + dbg_trace(); + this->load(src, size); +} + +Font::~Font() { + dbg_trace(); + this->font.reset(); +} + +void Font::load(const Asset &res, int size) { + SDLContext &ctx = SDLContext::get_instance(); + // Open the font using SDL's TTF_OpenFontRW, which supports loading from memory + SDL_RWops *rw_ops = SDL_RWFromFile(res.get_path().c_str(), "rb"); + if (!rw_ops) { + // dbg_log("Failed to create RWops for font: %s", SDL_GetError()); + return; + } + + TTF_Font *loaded_font = TTF_OpenFontRW(rw_ops, 1, size); // 1 indicates SDL should free the RWops + if (!loaded_font) { + // dbg_log("Failed to load font from asset: %s", TTF_GetError()); + return; + } + + // Wrap the TTF_Font with a unique_ptr for automatic cleanup + this->font = unique_ptr<TTF_Font, function<void(TTF_Font *)>>(loaded_font, [](TTF_Font *f) { + if (f) TTF_CloseFont(f); + }); +} + +int Font::get_size() const { + return this->font_size; +} diff --git a/src/crepe/api/Font.h b/src/crepe/api/Font.h new file mode 100644 index 0000000..012c271 --- /dev/null +++ b/src/crepe/api/Font.h @@ -0,0 +1,60 @@ +#pragma once + +#include <SDL2/SDL_ttf.h> +#include <functional> +#include <memory> + +#include "Asset.h" + +namespace crepe { + +class SDLContext; + +/** + * \class Font + * \brief Manages font loading and text rendering properties. + * + * The Font class is responsible for loading font resources and providing a way to render text + * with different styles and sizes. It can be used for text rendering in the game engine. + */ +class Font { + +public: + /** + * \brief Constructs a Font from an Asset resource. + * \param src Asset with font data to load. + * \param size The point size to render the font at. + */ + Font(const Asset &src, int size); + + /** + * \brief Destroys the Font instance, freeing associated resources. + */ + ~Font(); + + /** + * \brief Gets the size of the font. + * \return The point size of the font. + */ + int get_size() const; + +private: + /** + * \brief Loads the font from an Asset resource. + * \param res The Asset resource containing the font data. + * \param size The point size to render the font at. + */ + void load(const Asset &res, int size); + +private: + //! The font resource from the SDL_ttf library. + std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font; + + //! The size of the font in points. + int font_size; + + //! Grants SDLContext access to private members. + friend class SDLContext; +}; + +} // namespace crepe 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/Text.h b/src/crepe/api/Text.h new file mode 100644 index 0000000..6bb011c --- /dev/null +++ b/src/crepe/api/Text.h @@ -0,0 +1,42 @@ +#pragma once + +#include <string> + +#include "Color.h" +#include "UiObject.h" +#include "" +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 Text : 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. + */ + Text(game_object_id_t id, int width, int height); + + Color color = Color{0,0,0,0}; + std::string text = ""; + int size = 0; + const std::shared_ptr<Font> sprite_image; +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 10; } +}; + +} // namespace crepe 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 |