Commit 7c53a4a7 authored by Johannes Braun's avatar Johannes Braun
Browse files

Implemented new messages

parent 83d27b80
#include <util/messages.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cassert> #include <cassert>
class MyObserver : public msg::Observer template<>
struct prop
{ {
public:
void onNotify(msg::id_type id, msg::message_type msg) override
{
assert(msg.type() == typeid(std::string));
std::cout << "MyObserver received: id=" << id << ", Payload=\"" << std::any_cast<std::string>(msg) << "\"" << '\n';
}
}; };
int main() int main()
{ {
// A general message handler.
msg::SharedHandler messages;
// Two kinds of observers, one in class form and one with a lambda notifier.
MyObserver my_observer;
msg::Observer lambda_observer([](msg::id_type id, msg::message_type msg)
{
assert(msg.type() == typeid(std::string));
std::cout << "Lambda observer received: id=" << id << ", Payload=\"" << std::any_cast<std::string>(msg) << "\"" << '\n';
});
// Register both for some arbitrary IDs
messages->addObserver(&my_observer, 0, 2, 244);
messages->addObserver(&lambda_observer, 0, 5, 2);
// Scope testing for releasing the observer's registration on destruction.
{
auto scope_observer = std::make_unique<msg::Observer>([](msg::id_type id, msg::message_type msg) {
assert(msg.type() == typeid(std::string));
std::cout << "Scope observer received: id=" << id << ", Payload=\"" << std::any_cast<std::string>(msg) << "\"" << '\n';
});
messages->addObserver(scope_observer.get(), 0, 5, 2);
// publish a message to see it working.
messages.id(0).push(std::make_any<std::string>("This will be received by all receivers including the scoped observer."));
// On leaving the scope, scope_observer will be destroyed and removed from the handler registration list.
}
// Some more messages for the remaining Observers
messages.id(5).push(std::make_any<std::string>("This will only be received by lambda_observer."));
messages.id(2).push(std::make_any<std::string>("This will be received by both my_observer and lambda_observer."));
messages.id(244).push(std::make_any<std::string>("This will only be received by my_observer."));
system("pause"); system("pause");
return 0; return 0;
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <imgui/imgui_glfw.h> #include <imgui/imgui_glfw.h>
#include <core/message_tags.h> #include <core/message_tags.h>
#include <core/state.h>
namespace glare::component namespace glare::component
{ {
size_t PlayerController::s_current_id = 0; size_t PlayerController::s_current_id = 0;
...@@ -17,7 +19,7 @@ namespace glare::component ...@@ -17,7 +19,7 @@ namespace glare::component
core::Context::current().callbacks().addKeyActionCallback("player_controller" + std::to_string(m_id), std::bind(&PlayerController::onKeyAction, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); core::Context::current().callbacks().addKeyActionCallback("player_controller" + std::to_string(m_id), std::bind(&PlayerController::onKeyAction, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
core::Context::current().callbacks().addMouseButtonCallback("player_controller" + std::to_string(m_id), std::bind(&PlayerController::onMouseButtonAction, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); core::Context::current().callbacks().addMouseButtonCallback("player_controller" + std::to_string(m_id), std::bind(&PlayerController::onMouseButtonAction, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
registerForMessage(tags::camera); core::Context::current().messages()->addObserver(this, tags::camera);
} }
PlayerController::~PlayerController() PlayerController::~PlayerController()
...@@ -25,7 +27,7 @@ namespace glare::component ...@@ -25,7 +27,7 @@ namespace glare::component
try { try {
core::Context::current().callbacks().removeKeyActionCallback("player_controller" + std::to_string(m_id)); core::Context::current().callbacks().removeKeyActionCallback("player_controller" + std::to_string(m_id));
core::Context::current().callbacks().removeMouseButtonCallback("player_controller" + std::to_string(m_id)); core::Context::current().callbacks().removeMouseButtonCallback("player_controller" + std::to_string(m_id));
unregisterForMessage(tags::camera); core::Context::current().messages()->removeObserver(this, tags::camera);
} }
catch (...) catch (...)
{ {
...@@ -37,9 +39,9 @@ namespace glare::component ...@@ -37,9 +39,9 @@ namespace glare::component
return core::Context::current().camera()->owner() == owner(); return core::Context::current().camera()->owner() == owner();
} }
void PlayerController::handle(messaging::message_t &message) void PlayerController::onNotify(msg::id_type id, msg::message_type message)
{ {
switch (message.name) switch (id)
{ {
case tags::camera: case tags::camera:
{ {
......
#ifndef __PLAYERCONTROLLER_H #ifndef __PLAYERCONTROLLER_H
#define __PLAYERCONTROLLER_H #define __PLAYERCONTROLLER_H
#include <util/messaging.h>
#include <core/graph/component.h> #include <core/graph/component.h>
#include <core/control.h> #include <core/control.h>
#include <util/messages.h>
namespace glare::component namespace glare::component
{ {
class PlayerController : public core::SceneComponent, public messaging::Receiver class PlayerController : public core::SceneComponent, public msg::Observer
{ {
public: public:
PlayerController(); PlayerController();
~PlayerController(); ~PlayerController();
void handle(messaging::message_t &message) override; void onNotify(msg::id_type id, msg::message_type message) override;
void onKeyAction(controls::Key key, controls::ButtonAction action, controls::KeyMods mods); void onKeyAction(controls::Key key, controls::ButtonAction action, controls::KeyMods mods);
void onMouseButtonAction(controls::MouseButton button, controls::ButtonAction action, controls::KeyMods mods); void onMouseButtonAction(controls::MouseButton button, controls::ButtonAction action, controls::KeyMods mods);
......
#include "scene_node.h" #include "scene_node.h"
#include <core/message_tags.h> #include <core/message_tags.h>
#include <core/state.h>
#include <imgui/imgui_glfw.h> #include <imgui/imgui_glfw.h>
namespace glare::core namespace glare::core
...@@ -19,13 +19,13 @@ namespace glare::core ...@@ -19,13 +19,13 @@ namespace glare::core
void SceneNode::onAttached(std::shared_ptr<SceneNode> node) void SceneNode::onAttached(std::shared_ptr<SceneNode> node)
{ {
node->start(); node->start();
messaging::Handler::getInstance().submit(tags::nodes, 1); Context::current().messages().id(tags::nodes).push();
} }
void SceneNode::onDetached(std::shared_ptr<SceneNode> node) void SceneNode::onDetached(std::shared_ptr<SceneNode> node)
{ {
node->end(); node->end();
messaging::Handler::getInstance().submit(tags::nodes, 1); Context::current().messages().id(tags::nodes).push();
} }
void SceneNode::onDestroy() void SceneNode::onDestroy()
...@@ -83,7 +83,7 @@ namespace glare::core ...@@ -83,7 +83,7 @@ namespace glare::core
if (m_cached_transform != transform) if (m_cached_transform != transform)
{ {
m_cached_transform = transform; m_cached_transform = transform;
messaging::Handler::getInstance().submit(tags::graph_transform, transform); Context::current().messages().id(tags::graph_transform).push(std::make_any<math::Transform>(transform));
notifyTransform(); notifyTransform();
} }
} }
......
#ifndef INCLUDE_MESSAGE_TAGS_H #ifndef INCLUDE_MESSAGE_TAGS_H
#define INCLUDE_MESSAGE_TAGS_H #define INCLUDE_MESSAGE_TAGS_H
#include <util/messaging.h> #include <util/messages.h>
namespace tags namespace tags
{ {
constexpr uint32_t graph_transform = messaging::tag("GRTR"); constexpr msg::id_type graph_transform = msg::make_id("GRTR");
constexpr uint32_t mesh_transform = messaging::tag("M TR"); constexpr msg::id_type mesh_transform = msg::make_id("M TR");
constexpr uint32_t nodes = messaging::tag("NODE"); constexpr msg::id_type nodes = msg::make_id("NODE");
constexpr uint32_t camera = messaging::tag("CAMR"); constexpr msg::id_type camera = msg::make_id("CAMR");
constexpr uint32_t light = messaging::tag("LGHT"); constexpr msg::id_type light = msg::make_id("LGHT");
constexpr uint32_t scene = messaging::tag("SCEN"); constexpr msg::id_type scene = msg::make_id("SCEN");
constexpr uint32_t material = messaging::tag("MATR"); constexpr msg::id_type material = msg::make_id("MATR");
} }
#endif // !INCLUDE_MESSAGE_TAGS_H #endif // !INCLUDE_MESSAGE_TAGS_H
...@@ -30,14 +30,14 @@ namespace glare::core ...@@ -30,14 +30,14 @@ namespace glare::core
{ {
ImGui::PushID(owner()->name().c_str()); ImGui::PushID(owner()->name().c_str());
if (ImGui::DragFloat("Focus Distance", &m_focus_distance, 0.05f, 0.05f, 10000.f)) if (ImGui::DragFloat("Focus Distance", &m_focus_distance, 0.05f, 0.05f, 10000.f))
messaging::Handler::getInstance().submit(tags::camera, 1); Context::current().messages().id(tags::camera).push();
if (ImGui::DragFloat("DOF Size", &m_dof_size, 0.05f, 0.05f, 10000.f)) if (ImGui::DragFloat("DOF Size", &m_dof_size, 0.05f, 0.05f, 10000.f))
messaging::Handler::getInstance().submit(tags::camera, 1); Context::current().messages().id(tags::camera).push();
if (ImGui::Button("Make Default")) if (ImGui::Button("Make Default"))
{ {
core::Context::current().switchCamera(std::static_pointer_cast<Camera>(shared_from_this())); core::Context::current().switchCamera(std::static_pointer_cast<Camera>(shared_from_this()));
messaging::Handler::getInstance().submit(tags::camera, 1); Context::current().messages().id(tags::camera).push();
} }
ImGui::PopID(); ImGui::PopID();
} }
...@@ -87,7 +87,7 @@ namespace glare::core ...@@ -87,7 +87,7 @@ namespace glare::core
void Camera::onTransform() void Camera::onTransform()
{ {
messaging::Handler::getInstance().submit(tags::camera, 1); Context::current().messages().id(tags::camera).push();
} }
void Camera::setProjection(Projection projection) void Camera::setProjection(Projection projection)
......
#include "light.h" #include "light.h"
#include <imgui/imgui_glfw.h> #include <imgui/imgui_glfw.h>
#include <util/messaging.h> #include <util/messages.h>
#include <core/message_tags.h> #include <core/message_tags.h>
#include <core/numeric/geometry.h> #include <core/numeric/geometry.h>
#include "core/state.h" #include "core/state.h"
...@@ -20,7 +20,7 @@ namespace glare::core ...@@ -20,7 +20,7 @@ namespace glare::core
{ {
// Light has been removed // Light has been removed
if (core::Context::hasCurrentContext() && !core::Context::current().window().isClosing()) if (core::Context::hasCurrentContext() && !core::Context::current().window().isClosing())
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, nullptr }); Context::current().messages().id(tags::light).push(std::make_any<LightUpdate>(m_id, nullptr));
} }
void LightComponent::setParameters(const LightParameters& data) void LightComponent::setParameters(const LightParameters& data)
...@@ -108,7 +108,7 @@ namespace glare::core ...@@ -108,7 +108,7 @@ namespace glare::core
void LightComponent::onTransform() void LightComponent::onTransform()
{ {
// Light just has changed // Light just has changed
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, this }); Context::current().messages().id(tags::light).push(std::make_any<LightUpdate>(m_id, this));
} }
void LightComponent::onDebugGui() void LightComponent::onDebugGui()
...@@ -189,7 +189,7 @@ namespace glare::core ...@@ -189,7 +189,7 @@ namespace glare::core
if (changed) if (changed)
{ {
Log_Info << m_color.r << ", " << m_color.g << ", " << m_color.b; Log_Info << m_color.r << ", " << m_color.g << ", " << m_color.b;
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, this }); Context::current().messages().id(tags::light).push(std::make_any<LightUpdate>(m_id, this));
} }
} }
...@@ -206,7 +206,7 @@ namespace glare::core ...@@ -206,7 +206,7 @@ namespace glare::core
m_id = current_id++; m_id = current_id++;
// Light has been added // Light has been added
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, this }); Context::current().messages().id(tags::light).push(std::make_any<LightUpdate>(m_id, this));
} }
core::Context::current().lights().submit(buildLightShadow()); core::Context::current().lights().submit(buildLightShadow());
......
...@@ -190,6 +190,8 @@ namespace glare::core ...@@ -190,6 +190,8 @@ namespace glare::core
struct LightUpdate struct LightUpdate
{ {
LightUpdate(uint64_t id, LightComponent* ptr) : light_id(id), ptr(ptr) {}
uint64_t light_id; uint64_t light_id;
LightComponent* ptr; LightComponent* ptr;
}; };
......
#include "material.h" #include "material.h"
#include <imgui/imgui_glfw.h> #include <imgui/imgui_glfw.h>
#include <util/messaging.h> #include <util/messages.h>
#include <core/state.h> #include <core/state.h>
#include <core/message_tags.h> #include <core/message_tags.h>
...@@ -58,8 +58,8 @@ namespace glare::core ...@@ -58,8 +58,8 @@ namespace glare::core
ImGui::Spacing(); ImGui::Spacing();
changed |= ImGui::DragFloat("IOR", &index_of_refraction, 0.001f, 0.1f, 10.f); changed |= ImGui::DragFloat("IOR", &index_of_refraction, 0.001f, 0.1f, 10.f);
if (changed) if (changed)
messaging::Handler::getInstance().submit(tags::material, 1); Context::current().messages().id(tags::material).push();
ImGui::EndPopup(); ImGui::EndPopup();
} }
ImGui::PopID(); ImGui::PopID();
......
...@@ -63,7 +63,7 @@ namespace glare::core ...@@ -63,7 +63,7 @@ namespace glare::core
m_drawable->destroy(); m_drawable->destroy();
SceneComponent::onDestroy(); SceneComponent::onDestroy();
messaging::Handler::getInstance().submit(tags::nodes, 1); Context::current().messages().id(tags::nodes).push();
} }
void BatchRenderer::onStart() void BatchRenderer::onStart()
......
...@@ -63,10 +63,6 @@ namespace glare::core ...@@ -63,10 +63,6 @@ namespace glare::core
m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[3]", "sampleAmbient"); m_texture_renderer->shader().bindSubroutine(gl::ShaderType::eFragment, "u_light_sample[3]", "sampleAmbient");
} }
void GBuffer::handle(messaging::message_t &message)
{
}
void GBuffer::record(std::function<void()> render_function) const void GBuffer::record(std::function<void()> render_function) const
{ {
......
...@@ -10,13 +10,11 @@ ...@@ -10,13 +10,11 @@
#include <core/res/texture2d_multisample.h> #include <core/res/texture2d_multisample.h>
#include <core/objects/light.h> #include <core/objects/light.h>
#include <util/messaging.h>
#include "texture_renderer.h" #include "texture_renderer.h"
namespace glare::core namespace glare::core
{ {
class GBuffer : public messaging::Receiver class GBuffer
{ {
public: public:
GBuffer(unsigned int width, unsigned int height, unsigned samples); GBuffer(unsigned int width, unsigned int height, unsigned samples);
...@@ -29,8 +27,6 @@ namespace glare::core ...@@ -29,8 +27,6 @@ namespace glare::core
void activate() const; void activate() const;
void deactivate() const; void deactivate() const;
void handle(messaging::message_t &message) override;
void draw() const; void draw() const;
void setSkybox(std::shared_ptr<Skybox> skybox); void setSkybox(std::shared_ptr<Skybox> skybox);
......
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
#include <core/objects/material.h> #include <core/objects/material.h>
#include "batch_drawable.h" #include "batch_drawable.h"
#include <util/messaging.h> #include <util/messages.h>
#include <core/state.h>
#include <core/message_tags.h> #include <core/message_tags.h>
namespace glare::core namespace glare::core
...@@ -36,7 +37,7 @@ namespace glare::core ...@@ -36,7 +37,7 @@ namespace glare::core
void onTransform() override void onTransform() override
{ {
messaging::Handler::getInstance().submit(tags::mesh_transform, 1); Context::current().messages().id(tags::mesh_transform).push();
} }
}; };
} }
......
...@@ -192,13 +192,18 @@ namespace glare::core ...@@ -192,13 +192,18 @@ namespace glare::core
return m_batch_light_renderer; return m_batch_light_renderer;
} }
msg::SharedHandler& Context::messages()
{
return m_messages;
}
void Context::destroy() void Context::destroy()
{ {
m_shall_be_destroyed = true; m_shall_be_destroyed = true;
if (m_current_context == this) if (m_current_context == this)
m_current_context = nullptr; m_current_context = nullptr;
m_contexts.erase(id()); m_contexts.erase(id());
messaging::Handler::getInstance().lock(); m_messages->lock();
m_window.close(); m_window.close();
//m_graph_root->clearChildren(); //m_graph_root->clearChildren();
//messaging::Handler::getInstance().unlock(); //messaging::Handler::getInstance().unlock();
...@@ -229,7 +234,7 @@ namespace glare::core ...@@ -229,7 +234,7 @@ namespace glare::core
if (m_main_camera) { if (m_main_camera) {
// If camera is adaptive, the width and height will be updated, otherwise, it should stay the same size // If camera is adaptive, the width and height will be updated, otherwise, it should stay the same size
m_main_camera->update(); m_main_camera->update();
messaging::Handler::getInstance().submit(messaging::make_message(tags::camera, 1)); m_messages.id(tags::camera).push();
} }
m_gbuffer.updateSize(width, height); m_gbuffer.updateSize(width, height);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "rendering/batch_render_list.h" #include "rendering/batch_render_list.h"
#include <util/files.h> #include <util/files.h>
#include <util/messages.h>
namespace glare::core namespace glare::core
{ {
...@@ -108,6 +109,7 @@ namespace glare::core ...@@ -108,6 +109,7 @@ namespace glare::core
BatchLightRenderer& lights(); BatchLightRenderer& lights();
AnimationManager& animations(); AnimationManager& animations();
Window& window(); Window& window();
msg::SharedHandler& messages();
std::shared_ptr<SceneNode> graph() const; std::shared_ptr<SceneNode> graph() const;
std::shared_ptr<Skybox> skybox() const; std::shared_ptr<Skybox> skybox() const;
std::shared_ptr<Camera> camera() const; std::shared_ptr<Camera> camera() const;
...@@ -143,6 +145,7 @@ namespace glare::core ...@@ -143,6 +145,7 @@ namespace glare::core
BatchRenderList m_batch_render_list; BatchRenderList m_batch_render_list;
BatchLightRenderer m_batch_light_renderer; BatchLightRenderer m_batch_light_renderer;
Window m_window; Window m_window;
msg::SharedHandler m_messages;
}; };
template<typename... Args> template<typename... Args>
......
...@@ -21,17 +21,14 @@ namespace glare::raytrace ...@@ -21,17 +21,14 @@ namespace glare::raytrace
// And register tags and evaluators for receiving and process scene changes // And register tags and evaluators for receiving and process scene changes
registerForMessage(tags::camera); core::Context::current().messages()->addObserver(this, tags::camera, tags::light, tags::mesh_transform, tags::nodes, tags::material);
m_evaluators.emplace(tags::camera, TagEvaluator(DirtyFlags::eCamera)); m_evaluators.emplace(tags::camera, TagEvaluator(DirtyFlags::eCamera));
registerForMessage(tags::light);
m_evaluators.emplace(tags::light, TagEvaluator(DirtyFlags::eLight)); m_evaluators.emplace(tags::light, TagEvaluator(DirtyFlags::eLight));