diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/crepe/api/Button.cpp | 0 | ||||
| -rw-r--r-- | src/crepe/api/Button.h | 17 | ||||
| -rw-r--r-- | src/crepe/api/UiObject.h | 14 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 6 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.cpp | 52 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.h | 8 | 
6 files changed, 91 insertions, 6 deletions
| diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/crepe/api/Button.cpp diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h new file mode 100644 index 0000000..5035729 --- /dev/null +++ b/src/crepe/api/Button.h @@ -0,0 +1,17 @@ +#include <functional> + +#include "Component.h" +#include "api/EventHandler.h" +#include "api/UiObject.h" +namespace crepe { +class Button : public UiObject{ +public: +	~Button(){}; +	bool interactable = true; +	bool is_toggle = false; +	bool is_pressed = false; +	std::function<void()> on_click; +public: +virtual int get_instances_max() const { return 1; } +}; +} diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h new file mode 100644 index 0000000..f57f7ef --- /dev/null +++ b/src/crepe/api/UiObject.h @@ -0,0 +1,14 @@ +#include <functional> + +#include "Component.h" +#include "api/EventHandler.h" +namespace crepe { +class UiObject : public Component{ +public: +	~UiObject(){}; +	int width = 0; +	int height = 0; +public: +virtual int get_instances_max() const { return 1; } +}; +} diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d0977cb..43ef3f1 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -94,7 +94,6 @@ void SDLContext::handle_events(bool &running) {                  break;              case SDL_KEYDOWN: -                std::cout << "keyDown: " << event.key.keysym.sym << std::endl;                  event_manager.trigger_event<KeyPressEvent>(KeyPressEvent{                      .repeat = event.key.repeat,                      .key = this->sdl_to_keycode(event.key.keysym.scancode) @@ -157,7 +156,6 @@ void SDLContext::handle_events(bool &running) {  Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {      static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {          std::array<Keycode, SDL_NUM_SCANCODES> table{}; -		 // Default to NONE for unmapped keys          table.fill(Keycode::NONE);          table[SDL_SCANCODE_SPACE] = Keycode::SPACE; @@ -271,7 +269,7 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {  MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {      static const std::array<MouseButton, 8> MOUSE_BUTTON_LOOKUP_TABLE = [] {          std::array<MouseButton, 8> table{}; -        table.fill(MouseButton::NONE); // Default to NONE for unmapped buttons +        table.fill(MouseButton::NONE);          table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE;          table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE; @@ -287,7 +285,7 @@ MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {          return MouseButton::NONE;       } -    return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];  // Return mapped button +    return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];  }  void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }  void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index e69de29..b7a86f4 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -0,0 +1,52 @@ +#include "ComponentManager.h" +#include "../api/Button.h" +#include "../api/EventManager.h" +#include "../api/Transform.h" +#include "../api/Event.h" + +#include "system/InputSystem.h" + +using namespace crepe; + +InputSystem::InputSystem(ComponentManager &component_manager)  +    : System(component_manager) { +    auto &event_manager = EventManager::get_instance(); + +    event_manager.subscribe<MouseClickEvent>([this](const MouseClickEvent &event) { +        return this->handle_click(event); +    }); + +    event_manager.subscribe<MouseMoveEvent>([this](const MouseMoveEvent &event) { +        return this->handle_move(event); +    }); +} + +void InputSystem::update() { +} + +bool InputSystem::handle_click(const MouseClickEvent &event) { +    ComponentManager &mgr = this->component_manager; + +    std::vector<std::reference_wrapper<Button>> buttons = +        mgr.get_components_by_type<Button>(); +	 +	std::vector<std::reference_wrapper<Transform>> transforms = +        mgr.get_components_by_type<Transform>(); +    for (Button &button : buttons) { +		for(Transform& transform : transforms){ +			if(button.game_object_id != transform.game_object_id){continue;} +			if (!button.interactable) {break;} +            if (event.mouse_x >= transform.position.x && event.mouse_x <= transform.position.x + button.width && +                event.mouse_y >= transform.position.y && event.mouse_y <= transform.position.y + button.height) { +                button.on_click(); +            } +		} +    } +    return false; +} + +bool InputSystem::handle_move(const MouseMoveEvent &event) { + +    ComponentManager &mgr = this->component_manager; + +} diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 642035f..c50d928 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,14 +1,18 @@  #pragma once  #include "System.h" - +#include "../api/Event.h"  namespace crepe {  class InputSystem : public System {  public:  	using System::System; +	InputSystem(ComponentManager & component_manager);  	void update() override; -	void  +	bool handle_click(const MouseClickEvent &event); +	bool handle_move(const MouseMoveEvent &event); +	bool handle_key_press(const KeyPressEvent &event); +	bool handle_key_release(const KeyReleaseEvent &event);  };  } // namespace crepe |