diff options
Diffstat (limited to 'src/crepe/api')
-rw-r--r-- | src/crepe/api/Button.cpp | 3 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 12 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 16 | ||||
-rw-r--r-- | src/crepe/api/Event.h | 98 | ||||
-rw-r--r-- | src/crepe/api/KeyCodes.h | 6 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.h | 4 | ||||
-rw-r--r-- | src/crepe/api/Script.cpp | 15 | ||||
-rw-r--r-- | src/crepe/api/Script.h | 18 | ||||
-rw-r--r-- | src/crepe/api/Text.cpp | 12 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 66 |
11 files changed, 202 insertions, 50 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 76f74f0..305922c 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -3,9 +3,8 @@ namespace crepe { Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::function<void()> & on_click, bool is_toggle) + const std::function<void()> & on_click) : 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 index 61b18d7..08f5dec 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -15,19 +15,11 @@ public: * \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); + const std::function<void()> & on_click); - /** - * \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. @@ -56,8 +48,6 @@ public: 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; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 8f84f06..e8d6f92 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -20,6 +20,7 @@ target_sources(crepe PUBLIC Button.cpp UIObject.cpp AI.cpp + Text.cpp Scene.cpp ) @@ -51,4 +52,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Button.h UIObject.h AI.h + Text.h ) diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index ca2d3f1..6b9e3ca 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -76,6 +76,22 @@ struct Config final { */ std::string root_pattern = ".crepe-root"; } asset; + //! Default font options + struct { + /** + * \brief Default font size + * + * Using the SDL_ttf library the font size needs to be set when loading the font. + * This config option is the font size at which all fonts will be loaded initially. + * + */ + unsigned int size = 16; + } font; + //! Configuration for click tolerance. + struct { + //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click. + int click_tolerance = 5; + } input; //! Audio system settings struct { diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index f2f3daf..73bf461 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -3,7 +3,8 @@ #include <string> -#include "KeyCodes.h" +#include "api/KeyCodes.h" +#include "types.h" namespace crepe { @@ -38,11 +39,8 @@ public: */ class MousePressEvent : 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; + //! mouse position in world coordinates (game units). + vec2 mouse_pos = {0, 0}; //! The mouse button that was pressed. MouseButton button = MouseButton::NONE; @@ -53,11 +51,8 @@ public: */ class MouseClickEvent : 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; + //! mouse position in world coordinates (game units). + vec2 mouse_pos = {0, 0}; //! The mouse button that was clicked. MouseButton button = MouseButton::NONE; @@ -68,11 +63,8 @@ public: */ class MouseReleaseEvent : 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; + //! mouse position in world coordinates (game units). + vec2 mouse_pos = {0, 0}; //! The mouse button that was released. MouseButton button = MouseButton::NONE; @@ -83,17 +75,10 @@ public: */ class MouseMoveEvent : 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; - - // Movement since last event in x - int delta_x = 0; - - // Movement since last event in y - int delta_y = 0; + //! mouse position in world coordinates (game units). + vec2 mouse_pos = {0, 0}; + //! The change in mouse position relative to the last position (in pixels). + ivec2 mouse_delta = {0, 0}; }; /** @@ -101,12 +86,8 @@ public: */ 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; - + //! mouse position in world coordinates (game units) when the scroll happened. + vec2 mouse_pos = {0, 0}; //! scroll direction (-1 = down, 1 = up) int scroll_direction = 0; //! scroll amount in y axis (from and away from the person). @@ -127,4 +108,55 @@ public: */ class ShutDownEvent : public Event {}; +/** + * \brief Event triggered to indicate the window is overlapped by another window. + * + * When two windows overlap the bottom window gets distorted and that window has to be redrawn. + */ +class WindowExposeEvent : public Event {}; + +/** + * \brief Event triggered to indicate the window is resized. + */ +class WindowResizeEvent : public Event { +public: + //! new window dimensions + ivec2 dimensions = {0, 0}; +}; + +/** + * \brief Event triggered to indicate the window is moved. + */ +class WindowMoveEvent : public Event { +public: + //! The change in position relative to the last position (in pixels). + ivec2 delta_move = {0, 0}; +}; + +/** + * \brief Event triggered to indicate the window is minimized. + */ +class WindowMinimizeEvent : public Event {}; + +/** + * \brief Event triggered to indicate the window is maximized + */ +class WindowMaximizeEvent : public Event {}; + +/** + * \brief Event triggered to indicate the window gained focus + * + * This event is triggered when the window receives focus, meaning it becomes the active window + * for user interaction. + */ +class WindowFocusGainEvent : public Event {}; + +/** + * \brief Event triggered to indicate the window lost focus + * + * This event is triggered when the window loses focus, meaning it is no longer the active window + * for user interaction. + */ +class WindowFocusLostEvent : public Event {}; + } // namespace crepe diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index fcfc080..1b9573a 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,5 +1,9 @@ #pragma once + +#include <unordered_map> + namespace crepe { + //! Enumeration for mouse button inputs, including standard and extended buttons. enum class MouseButton { NONE = 0, //!< No mouse button input. @@ -151,4 +155,6 @@ enum class Keycode { /// \} MENU = 348, //!< Menu key. }; +//! Typedef for keyboard state. +typedef std::unordered_map<Keycode, bool> keyboard_state_t; } // namespace crepe diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 40e6b38..1d23cbf 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -72,6 +72,8 @@ private: //! Global context Mediator mediator; + //! SDLContext instance + SDLContext sdl_context{mediator}; //! Component manager instance ComponentManager component_manager{mediator}; //! Scene manager instance @@ -84,8 +86,6 @@ private: ResourceManager resource_manager{mediator}; //! Save manager instance SaveManager save_manager{mediator}; - //! SDLContext instance - SDLContext sdl_context{mediator}; private: /** diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index 753a9e3..583c04f 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -1,7 +1,7 @@ #include <string> +#include "../facade/SDLContext.h" #include "../manager/SceneManager.h" - #include "Script.h" using namespace crepe; @@ -25,3 +25,16 @@ void Script::set_next_scene(const string & name) { } SaveManager & Script::get_save_manager() const { return this->mediator->save_manager; } + +const keyboard_state_t & Script::get_keyboard_state() const { + SDLContext & sdl_context = this->mediator->sdl_context; + return sdl_context.get_keyboard_state(); +} + +bool Script::get_key_state(Keycode key) const noexcept { + try { + return this->get_keyboard_state().at(key); + } catch (...) { + return false; + } +} diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..65306cd 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -2,6 +2,7 @@ #include <vector> +#include "../api/KeyCodes.h" #include "../manager/EventManager.h" #include "../manager/Mediator.h" #include "../system/CollisionSystem.h" @@ -134,7 +135,22 @@ protected: //! Retrieve SaveManager reference SaveManager & get_save_manager() const; - + /** + * \brief Utility function to retrieve the keyboard state + * \see SDLContext::get_keyboard_state + * + * \return current keyboard state map with Keycode as key and bool as value(true = pressed, false = not pressed) + * + */ + const keyboard_state_t & get_keyboard_state() const; + /** + * \brief Utility function to retrieve a single key state. + * \see SDLContext::get_keyboard_state + * + * \return Keycode state (true if pressed, false if not pressed). + * + */ + bool get_key_state(Keycode key) const noexcept; //! \} private: diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp new file mode 100644 index 0000000..54a4370 --- /dev/null +++ b/src/crepe/api/Text.cpp @@ -0,0 +1,12 @@ +#include "../facade/FontFacade.h" + +#include "Text.h" + +using namespace crepe; + +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & font_family, const Data & data, const std::string & text) + : UIObject(id, dimensions, offset), + text(text), + data(data), + font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h new file mode 100644 index 0000000..c30dc80 --- /dev/null +++ b/src/crepe/api/Text.h @@ -0,0 +1,66 @@ +#pragma once + +#include <optional> +#include <string> + +#include "../Component.h" + +#include "Asset.h" +#include "Color.h" +#include "UIObject.h" + +namespace crepe { +/** + * \brief Text UIObject component for displaying text + * + * This class can be used to display text on screen. By setting the font_family to a font already stored on the current device it will automatically be loaded in. + */ +class Text : public UIObject { +public: + //! Text data that does not have to be set in the constructor + struct Data { + /** + * \brief fontsize for text rendering + * + * \note this is not the actual font size that is loaded in. + * + * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. + * The default font size that is loaded is set in the Config. + * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. + */ + unsigned int font_size = 16; + + //! Layer sorting level of the text + const int sorting_in_layer = 0; + + //! Order within the sorting text + const int order_in_layer = 0; + + //! Label text color. + Color text_color = Color::BLACK; + }; + +public: + /** + * + * \param dimensions Width and height of the UIObject. + * \param offset Offset of the UIObject relative to its transform + * \param text The text to be displayed. + * \param font_family The font style name to be displayed. + * \param data Data struct containing extra text parameters. + * \param font Optional font asset that can be passed or left empty. + */ + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & font_family, const Data & data, const std::string & text = ""); + + //! Label text. + std::string text = ""; + //! font family name + std::string font_family = ""; + //! Font asset variable if this is not set, it will use the font_family to create an asset. + std::optional<Asset> font; + //! Data instance + Data data; +}; + +} // namespace crepe |