diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/crepe/api/Button.h | 1 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 2 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.h | 5 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.cpp | 43 | ||||
| -rw-r--r-- | src/crepe/system/InputSystem.h | 33 | ||||
| -rw-r--r-- | src/test/InputTest.cpp | 31 | 
6 files changed, 64 insertions, 51 deletions
| diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 0dfad5d..290bb2f 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -4,7 +4,6 @@  #include "UiObject.h" -  namespace crepe {  /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 03b0cdc..a657373 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -16,8 +16,8 @@  #include <stdexcept>  #include "../api/Camera.h" -#include "../api/EventManager.h"  #include "../api/Config.h" +#include "../api/EventManager.h"  #include "../api/Sprite.h"  #include "../api/Texture.h"  #include "../util/Log.h" diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index a56232f..2228950 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -12,13 +12,12 @@  #include <utility>  #include "api/Camera.h" +#include "api/Color.h"  #include "api/Event.h"  #include "api/KeyCodes.h"  #include "api/Sprite.h" -#include "api/Transform.h" -#include "api/Camera.h" -#include "api/Color.h"  #include "api/Texture.h" +#include "api/Transform.h"  #include "types.h"  namespace crepe { diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 1cd3974..0cf5af1 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -6,7 +6,6 @@  using namespace crepe; -  void InputSystem::update() {  	ComponentManager & mgr = this->component_manager;  	EventManager & event_mgr = EventManager::get_instance(); @@ -21,11 +20,14 @@ void InputSystem::update() {  		break;  	}  	if (!curr_cam_ref) return; -	Camera& current_cam = curr_cam_ref; -	RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(current_cam.game_object_id); -	Transform& cam_transform = transform_vec.front().get(); -	int camera_origin_x = cam_transform.position.x + current_cam.offset.x - (current_cam.viewport_size.x / 2); -	int camera_origin_y = cam_transform.position.y + current_cam.offset.y - (current_cam.viewport_size.y / 2); +	Camera & current_cam = curr_cam_ref; +	RefVector<Transform> transform_vec +		= mgr.get_components_by_id<Transform>(current_cam.game_object_id); +	Transform & cam_transform = transform_vec.front().get(); +	int camera_origin_x +		= cam_transform.position.x + current_cam.offset.x - (current_cam.viewport_size.x / 2); +	int camera_origin_y +		= cam_transform.position.y + current_cam.offset.y - (current_cam.viewport_size.y / 2);  	for (const SDLContext::EventData & event : event_list) {  		int world_mouse_x = event.mouse_position.first + camera_origin_x;  		int world_mouse_y = event.mouse_position.second + camera_origin_y; @@ -98,7 +100,8 @@ void InputSystem::update() {  	}  } -void InputSystem::handle_move(const SDLContext::EventData & event_data, const int& world_mouse_x, const int& world_mouse_y) { +void InputSystem::handle_move(const SDLContext::EventData & event_data, +							  const int & world_mouse_x, const int & world_mouse_y) {  	ComponentManager & mgr = this->component_manager;  	RefVector<Button> buttons = mgr.get_components_by_type<Button>(); @@ -110,7 +113,8 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data, const in  		if (!transform) continue;  		bool was_hovering = button.hover; -		if (button.active && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) { +		if (button.active +			&& is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {  			button.hover = true;  			if (!was_hovering && button.on_enter) {  				button.on_enter(); @@ -125,7 +129,8 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data, const in  	}  } -void InputSystem::handle_click(const MouseButton& mouse_button, const int& world_mouse_x, const int& world_mouse_y) { +void InputSystem::handle_click(const MouseButton & mouse_button, const int & world_mouse_x, +							   const int & world_mouse_y) {  	ComponentManager & mgr = this->component_manager;  	RefVector<Button> buttons = mgr.get_components_by_type<Button>(); @@ -135,21 +140,21 @@ void InputSystem::handle_click(const MouseButton& mouse_button, const int& world  			= mgr.get_components_by_id<Transform>(button.game_object_id);  		OptionalRef<Transform> transform(transform_vec.front().get()); -		if (button.active && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) { +		if (button.active +			&& is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {  			handle_button_press(button);  		}  	}  } -bool InputSystem::is_mouse_inside_button( -    const int& mouse_x, const int& mouse_y,  -    const Button & button, const Transform & transform) { -    int half_width = button.width / 2; -    int half_height = button.height / 2; -    return mouse_x >= transform.position.x - half_width -        && mouse_x <= transform.position.x + half_width -        && mouse_y >= transform.position.y - half_height -        && mouse_y <= transform.position.y + half_height; +bool InputSystem::is_mouse_inside_button(const int & mouse_x, const int & mouse_y, +										 const Button & button, const Transform & transform) { +	int half_width = button.width / 2; +	int half_height = button.height / 2; +	return mouse_x >= transform.position.x - half_width +		   && mouse_x <= transform.position.x + half_width +		   && mouse_y >= transform.position.y - half_height +		   && mouse_y <= transform.position.y + half_height;  }  void InputSystem::handle_button_press(Button & button) { diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 4244d91..53f2c4e 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -21,26 +21,26 @@ class Transform;   */  class InputSystem : public System {  public: -    using System::System; +	using System::System; -    /** +	/**       * \brief Updates the system, processing all input events.       * This method processes all events and triggers corresponding actions.       */ -    void update() override; +	void update() override;  private: -    	//! Reference to the currently active camera. -    	OptionalRef<Camera> curr_cam_ref; +	//! Reference to the currently active camera. +	OptionalRef<Camera> curr_cam_ref; -    	//! Stores the last position of the mouse when the button was pressed. -    	std::pair<int, int> last_mouse_down_position{INFINITY, INFINITY}; +	//! Stores the last position of the mouse when the button was pressed. +	std::pair<int, int> last_mouse_down_position{INFINITY, INFINITY}; -    	//! Stores the last mouse button pressed. -    	MouseButton last_mouse_button = MouseButton::NONE; +	//! Stores the last mouse button pressed. +	MouseButton last_mouse_button = MouseButton::NONE;  	// -    	//! The tolerance in game units for detecting a mouse click. -    	const int click_tolerance = 5; +	//! The tolerance in game units for detecting a mouse click. +	const int click_tolerance = 5;  	/**  	* \brief Handles the mouse click event. @@ -50,7 +50,8 @@ private:  	*  	* This method processes the mouse click event and triggers the corresponding button action.  	*/ -	void handle_click(const MouseButton& mouse_button, const int& world_mouse_x, const int& world_mouse_y); +	void handle_click(const MouseButton & mouse_button, const int & world_mouse_x, +					  const int & world_mouse_y);  	/**  	* \brief Handles the mouse movement event. @@ -60,7 +61,8 @@ private:  	*  	* This method processes the mouse movement event and updates the button hover state.  	*/ -	void handle_move(const SDLContext::EventData& event_data, const int& world_mouse_x, const int& world_mouse_y); +	void handle_move(const SDLContext::EventData & event_data, const int & world_mouse_x, +					 const int & world_mouse_y);  	/**  	* \brief Checks if the mouse position is inside the bounds of the button. @@ -70,7 +72,8 @@ private:  	* \param transform The transform component of the button.  	* \return True if the mouse is inside the button, false otherwise.  	*/ -	bool is_mouse_inside_button(const int& world_mouse_x, const int& world_mouse_y, const Button& button, const Transform& transform); +	bool is_mouse_inside_button(const int & world_mouse_x, const int & world_mouse_y, +								const Button & button, const Transform & transform);  	/**  	* \brief Handles the button press event, calling the on_click callback if necessary. @@ -78,7 +81,7 @@ private:  	*  	* This method triggers the on_click action for the button when it is pressed.  	*/ -	void handle_button_press(Button& button); +	void handle_button_press(Button & button);  };  } // namespace crepe diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 790dbff..32111c0 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -8,11 +8,11 @@  #include <SDL2/SDL_keycode.h>  #include <crepe/ComponentManager.h>  #include <crepe/api/Button.h> +#include <crepe/api/Camera.h>  #include <crepe/api/GameObject.h>  #include <crepe/api/Metadata.h>  #include <crepe/api/Transform.h>  #include <crepe/api/Vector2.h> -#include <crepe/api/Camera.h>  #include <gmock/gmock.h>  using namespace std; @@ -26,10 +26,9 @@ public:  	EventManager & event_manager = EventManager::get_instance();  	//GameObject camera; +  protected: -	void SetUp() override {  -		event_manager.clear();  -	} +	void SetUp() override { event_manager.clear(); }  	void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) {  		SDL_Event event; @@ -54,7 +53,8 @@ protected:  TEST_F(InputTest, MouseDown) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool mouse_triggered = false;  	EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) { @@ -83,7 +83,8 @@ TEST_F(InputTest, MouseDown) {  TEST_F(InputTest, MouseUp) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool function_triggered = false;  	EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { @@ -110,7 +111,8 @@ TEST_F(InputTest, MouseUp) {  TEST_F(InputTest, MouseMove) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool function_triggered = false;  	EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) { @@ -139,7 +141,8 @@ TEST_F(InputTest, MouseMove) {  TEST_F(InputTest, KeyDown) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool function_triggered = false; @@ -169,7 +172,8 @@ TEST_F(InputTest, KeyDown) {  TEST_F(InputTest, KeyUp) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool function_triggered = false;  	EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) { @@ -192,7 +196,8 @@ TEST_F(InputTest, KeyUp) {  TEST_F(InputTest, MouseClick) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	bool on_click_triggered = false;  	EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) { @@ -212,7 +217,8 @@ TEST_F(InputTest, MouseClick) {  TEST_F(InputTest, testButtonClick) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);  	bool button_clicked = false; @@ -237,7 +243,8 @@ TEST_F(InputTest, testButtonClick) {  TEST_F(InputTest, testButtonHover) {  	GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); -	auto& camera = obj.add_component<Camera>(Color::BLACK,ivec2{0,0},vec2{500,500},0.0,vec2{0,0}); +	auto & camera = obj.add_component<Camera>(Color::BLACK, ivec2{0, 0}, vec2{500, 500}, 0.0, +											  vec2{0, 0});  	camera.active = true;  	GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);  	bool button_clicked = false; |