aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-20 12:17:14 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-20 12:17:14 +0100
commit18c7e528f3df31d62cd05c2cc34be92be83d5367 (patch)
tree800cc0fc41750c31a95de09dc2fc2207d0ed7469 /src
parent03aea832aa0bc2edba2cc5ab4d9f8eba42d355be (diff)
button now using channel
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Script.cpp15
-rw-r--r--src/crepe/api/Script.h38
-rw-r--r--src/crepe/system/InputSystem.cpp8
-rw-r--r--src/example/FontExample.cpp55
4 files changed, 56 insertions, 60 deletions
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
index b147252..06b535f 100644
--- a/src/crepe/api/Script.cpp
+++ b/src/crepe/api/Script.cpp
@@ -20,6 +20,21 @@ void Script::subscribe(const EventHandler<CollisionEvent> & callback) {
this->subscribe_internal(callback, this->game_object_id);
}
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback) {
+ this->subscribe_internal(callback, this->game_object_id);
+}
+
void Script::set_next_scene(const string & name) {
SceneManager & mgr = this->mediator->scene_manager;
mgr.set_next_scene(name);
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h
index 5f68928..bbee920 100644
--- a/src/crepe/api/Script.h
+++ b/src/crepe/api/Script.h
@@ -8,6 +8,7 @@
#include "../manager/Mediator.h"
#include "../manager/ReplayManager.h"
#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
#include "../types.h"
#include "../util/Log.h"
#include "../util/OptionalRef.h"
@@ -270,7 +271,42 @@ void Script::subscribe(const EventHandler<CollisionEvent> & callback);
template <>
void Script::subscribe(const EventHandler<CollisionEvent> & callback, event_channel_t)
= delete;
-
+/**
+ * \brief Subscribe to ButtonPressEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonPressEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonPressEvent> & callback, event_channel_t)
+ = delete;
+/**
+ * \brief Subscribe to ButtonExitEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonExitEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonExitEvent> & callback, event_channel_t)
+ = delete;
+/**
+ * \brief Subscribe to ButtonEnterEvent for the current GameObject
+ *
+ * This is a template specialization for Script::subscribe which automatically sets the event
+ * channel so the callback handler is only called for ButtonEnterEvent events that apply to the
+ * current GameObject the parent BehaviorScript is attached to.
+ */
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback);
+template <>
+void Script::subscribe(const EventHandler<ButtonEnterEvent> & callback, event_channel_t)
+ = delete;
} // namespace crepe
#include "Script.hpp"
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index 858a645..49ffeac 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -171,12 +171,12 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p
if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
button.hover = true;
if (!was_hovering) {
- event_mgr.trigger_event<ButtonEnterEvent>(metadata);
+ event_mgr.trigger_event<ButtonEnterEvent>(metadata,metadata.game_object_id);
}
} else {
button.hover = false;
if (was_hovering) {
- event_mgr.trigger_event<ButtonExitEvent>(metadata);
+ event_mgr.trigger_event<ButtonExitEvent>(metadata,metadata.game_object_id);
}
}
}
@@ -196,7 +196,7 @@ void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mo
const Transform & transform
= mgr.get_components_by_id<Transform>(button.game_object_id).front();
if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
- event_mgr.trigger_event<ButtonPressEvent>(metadata);
+ event_mgr.trigger_event<ButtonPressEvent>(metadata,metadata.game_object_id);
}
}
}
@@ -209,7 +209,7 @@ bool InputSystem::is_mouse_inside_button(const vec2 & mouse_pos, const Button &
actual_pos += cam_transform.position;
}
vec2 half_dimensions = button.dimensions / 2;
-
+
return mouse_pos.x >= actual_pos.x - half_dimensions.x
&& mouse_pos.x <= actual_pos.x + half_dimensions.x
&& mouse_pos.y >= actual_pos.y - half_dimensions.y
diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp
deleted file mode 100644
index 6a334b1..0000000
--- a/src/example/FontExample.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-#include <SDL2/SDL_ttf.h>
-#include <chrono>
-#include <crepe/api/Camera.h>
-#include <crepe/api/Config.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/LoopManager.h>
-#include <crepe/api/Scene.h>
-#include <crepe/api/Script.h>
-#include <crepe/api/Text.h>
-#include <crepe/facade/Font.h>
-#include <crepe/facade/SDLContext.h>
-#include <crepe/manager/EventManager.h>
-#include <crepe/manager/Mediator.h>
-#include <crepe/manager/ResourceManager.h>
-#include <exception>
-#include <iostream>
-#include <memory>
-using namespace crepe;
-using namespace std;
-using namespace std::chrono;
-class TestScript : public Script {
-public:
- steady_clock::time_point start_time;
- virtual void init() override { start_time = steady_clock::now(); }
- virtual void update() override {
- auto now = steady_clock::now();
- auto elapsed = duration_cast<seconds>(now - start_time).count();
-
- if (elapsed >= 5) {
- Mediator & med = mediator;
- EventManager & event_mgr = med.event_manager;
- event_mgr.trigger_event<ShutDownEvent>();
- }
- }
-};
-class TestScene : public Scene {
-public:
- void load_scene() override {
- GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1);
- text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "OpenSymbol",
- Text::Data{});
- text_object.add_component<BehaviorScript>().set_script<TestScript>();
- text_object.add_component<Camera>(ivec2{300, 300}, vec2{100, 100}, Camera::Data{});
- }
- std::string get_name() const override { return "hey"; }
-};
-int main() {
- // Config& config = Config::get_instance();
- // config.log.level = Log::Level::TRACE;
- LoopManager engine;
- engine.add_scene<TestScene>();
- engine.start();
-
- return 0;
-}