Skip to content

Commit

Permalink
event: updated event, added event source for better filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
rxdu committed Nov 6, 2024
1 parent 4b3eb84 commit 3a1feb4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 26 deletions.
Binary file added docs/screenshots/quickviz-v0.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
namespace quickviz {
class AsyncEventEmitter {
public:
template <typename EventType, typename... Args>
void Emit(const std::string& event_name, Args... args) {
auto event = std::make_shared<EventType>(event_name, args...);
template <typename EventT, typename... Args>
void Emit(EventSource type, const std::string& event_name, Args... args) {
auto event = std::make_shared<EventT>(type, event_name, args...);
AsyncEventDispatcher::GetInstance().Dispatch(event);
}
};
Expand Down
17 changes: 15 additions & 2 deletions src/imview/include/imview/component/event/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@
#include <iostream>

namespace quickviz {
enum class EventSource : int {
kNone = 0,
kKeyboard,
kMouse,
kMouseButton,
kUiElement,
kApplicaton,
kCustomEvent
};

class BaseEvent {
public:
virtual ~BaseEvent() = default;
virtual EventSource GetSource() const = 0;
virtual std::string GetName() const = 0;
};

template <typename... Args>
class Event : public BaseEvent {
public:
// Constructor to create an event with given arguments
Event(std::string name, Args... args)
: name_(name), data_(std::make_tuple(args...)) {}
Event(EventSource type, const std::string& name, Args... args)
: type_(type), name_(name), data_(std::make_tuple(args...)) {}

// Get the stored data
EventSource GetSource() const override { return type_; }
std::string GetName() const override { return name_; }
const std::tuple<Args...>& GetData() const { return data_; }

Expand All @@ -39,6 +51,7 @@ class Event : public BaseEvent {
}

private:
EventSource type_;
std::string name_;
std::tuple<Args...> data_;

Expand Down
6 changes: 3 additions & 3 deletions src/imview/include/imview/component/event/event_emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
namespace quickviz {
class EventEmitter {
public:
template <typename EventType, typename... Args>
void Emit(const std::string& event_name, Args... args) {
auto event = std::make_shared<EventType>(event_name, args...);
template <typename EventT, typename... Args>
void Emit(EventSource type, const std::string& event_name, Args... args) {
auto event = std::make_shared<EventT>(type, event_name, args...);
EventDispatcher::GetInstance().Dispatch(event);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CameraController {
public:
CameraController(Camera& camera);

void Reset();
void SetMode(Mode mode);
void ProcessKeyboard(CameraMovement direction, float delta_time);
void ProcessMouseMovement(float x_offset, float y_offset);
Expand Down
19 changes: 11 additions & 8 deletions src/imview/src/component/opengl/camera_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace quickviz {
CameraController::CameraController(Camera& camera) : camera_(camera) {}

void CameraController::Reset() { camera_.Reset(); }

void CameraController::SetMode(CameraController::Mode mode) {
mode_ = mode;
if (mode == Mode::kTopDown) {
Expand All @@ -26,15 +28,16 @@ void CameraController::ProcessKeyboard(
if (mode_ == Mode::kTopDown) {
float velocity = camera_.GetMovementSpeed() * delta_time;
glm::vec3 position = camera_.GetPosition();
// Move only along X and Z axes
if (direction == CameraMovement::kForward) position.z -= velocity;
if (direction == CameraMovement::kBackward) position.z += velocity;
if (direction == CameraMovement::kLeft) position.x -= velocity;
if (direction == CameraMovement::kRight) position.x += velocity;

camera_.SetPosition(position); // Update position without changing height
if (direction == CameraMovement::kUp) position.y -= velocity;
if (direction == CameraMovement::kDown) position.y += velocity;
if (direction == CameraMovement::kForward) position.x -= velocity;
if (direction == CameraMovement::kBackward) position.x += velocity;
if (direction == CameraMovement::kLeft) position.z += velocity;
if (direction == CameraMovement::kRight) position.z -= velocity;
camera_.SetPosition(position);
} else {
camera_.ProcessKeyboard(direction, delta_time);
}
camera_.ProcessKeyboard(direction, delta_time);
}

void CameraController::ProcessMouseMovement(float x_offset, float y_offset) {
Expand Down
11 changes: 6 additions & 5 deletions src/imview/test/test_async_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
using namespace quickviz;

int main(int argc, char* argv[]) {
Event<int, double, std::string> event("test_event", 42, 3.14, "hello");
Event<int, double, std::string> event(EventSource::kApplicaton, "test_event",
42, 3.14, "hello");
event.Print();

auto name = event.GetName();
Expand All @@ -38,10 +39,10 @@ int main(int argc, char* argv[]) {
});

AsyncEventEmitter emitter;
emitter.Emit<Event<int, double, std::string>>("test_event", 42, 3.14,
"hello");
emitter.Emit<Event<int, double, std::string>>("test_event", 21, 6.28,
"hello again");
emitter.Emit<Event<int, double, std::string>>(
EventSource::kApplicaton, "test_event", 42, 3.14, "hello");
emitter.Emit<Event<int, double, std::string>>(
EventSource::kApplicaton, "test_event", 21, 6.28, "hello again");

// AsyncEventDispatcher::GetInstance().HandleEvents();
std::thread handler_thread(&AsyncEventDispatcher::HandleEvents,
Expand Down
4 changes: 2 additions & 2 deletions src/imview/test/test_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void ProcessInput(GLFWwindow* window) {
deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
camera.Reset();
camera_controller.Reset();
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ int main(int argc, char* argv[]) {
int height = 1080;
Window win("Test Window", width, height);

camera_controller.SetMode(CameraController::Mode::kTopDown);
camera_controller.SetMode(CameraController::Mode::kOrbit);
glfwSetCursorPosCallback(win.GetWindowObject(), MouseCallback);
glfwSetScrollCallback(win.GetWindowObject(), ScrollCallback);
glfwSetInputMode(win.GetWindowObject(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
Expand Down
7 changes: 4 additions & 3 deletions src/imview/test/test_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
using namespace quickviz;

int main(int argc, char* argv[]) {
Event<int, double, std::string> event("test_event", 42, 3.14, "hello");
Event<int, double, std::string> event(EventSource::kApplicaton, "test_event",
42, 3.14, "hello");
event.Print();

auto name = event.GetName();
Expand All @@ -39,8 +40,8 @@ int main(int argc, char* argv[]) {
});

EventEmitter emitter;
emitter.Emit<Event<int, double, std::string>>("test_event", 42, 3.14,
"hello");
emitter.Emit<Event<int, double, std::string>>(
EventSource::kApplicaton, "test_event", 42, 3.14, "hello");

return 0;
}

0 comments on commit 3a1feb4

Please sign in to comment.