Skip to content

Commit

Permalink
feat: Add main window for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrno committed Nov 19, 2024
1 parent 985c799 commit 1307ddb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 56 deletions.
55 changes: 32 additions & 23 deletions main/include/modules/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,74 @@ class ModuleInterface
// Module API (declarations needs to be here)
public:
virtual ~ModuleInterface() {}

// Main functions
virtual void execute() {};
virtual void destroy() {};
virtual void render() {};

void Start();
void Stop();

// Hooks
void OnInputEvent();
void OnOutputEvent();


template<typename T>
void AddArg(const std::string& key, T value);

// Misc
void AddLogo(const uint8_t* hexa, size_t size);
void AddLogo(const std::string& relative_path);

// Functions of the modules (gives the Vortex abstraction/features)
void AddFunction(std::function<void()> foo, const std::string& name);
void AddFunction(std::function<void(ArgumentValues&)> foo, const std::string& name);
void AddFunction(std::function<void(ReturnValues&)> foo, const std::string& name);
void AddFunction(std::function<void(ArgumentValues&, ReturnValues&)> foo, const std::string& name);
void ExecuteFunction(const std::string& name);
void ExecuteFunction(const std::string& name, ArgumentValues& args);
void ExecuteFunction(const std::string& name, ReturnValues& ret);
void ExecuteFunction(const std::string& name, ArgumentValues& args, ReturnValues& ret);

// IMPORTANT : These AddOutputEvent declaration are in reality "Handlers" of output events triggered by other compenents.
// Remember:
// - A output event is triggered via VortexMaker::ExecuteOutputEvent() by the current component to all concerned extern components with the output event added.
// - A input event is triggered via VortexMaker::ExecuteInputEvent() by a extern component to the current component with the input component added.
// Output Events
// A output event is triggered via VortexMaker::ExecuteOutputEvent() by the current component to all concerned extern components with the output event added.
void AddOutputEvent(std::function<void()> foo, const std::string& name);
void AddOutputEvent(std::function<void(ArgumentValues&)> foo, const std::string& name);
void AddOutputEvent(std::function<void(ReturnValues&)> foo, const std::string& name);
void AddOutputEvent(std::function<void(ArgumentValues&, ReturnValues&)> foo, const std::string& name);
void AddOutputEvent(const ModuleOutputEvent& event);

// Input Events
// A input event is triggered via VortexMaker::ExecuteInputEvent() by a extern component to the current component with the input component added.
void AddInputEvent(std::function<void()> foo, const std::string& name);
void AddInputEvent(std::function<void(ArgumentValues&)> foo, const std::string& name);
void AddInputEvent(std::function<void(ReturnValues&)> foo, const std::string& name);
void AddInputEvent(std::function<void(ArgumentValues&, ReturnValues&)> foo, const std::string& name);
void AddInputEvent(const ModuleInputEvent& event);

// GUI stuffs
void SetMainWindow(const std::shared_ptr<Cherry::AppWindow>& win);
void AddWindow(const std::shared_ptr<Cherry::AppWindow>& win);

// Content Browser stuffs
void AddContentBrowserItemHandler();
void AddContentBrowserItemIdentifier();

// Logs & Metrics
void LogInfo(const std::string& message);
void LogWarning(const std::string& message);
void LogError(const std::string& message);
void LogFatal(const std::string& message);

// TODO : AddParameterToFunction
// TODO : AddParameterToInputEvent
// TODO : AddParameterToOutputEvent
// TODO : AddReturnToFunction
// TODO : AddReturnToInputEvent
// TODO : AddReturnToOutputEvent

// TODO : AddSpawnableWindow
// TODO : AddContentBrowserItemHandler
// TODO : AddContentBrowserItemIdentifier
// TODO : AddCommonUtility
// TODO : AddMainWindow

//void AddFunction(void (*item)(), const std::string& name, Parameters params);
std::shared_ptr<ModuleInterface> GetInterface();

void ExecuteFunction(const std::string& name);
void ExecuteFunction(const std::string& name, ArgumentValues& args);
void ExecuteFunction(const std::string& name, ReturnValues& ret);
void ExecuteFunction(const std::string& name, ArgumentValues& args, ReturnValues& ret);

void ExecuteOutputEvent(const std::string& name, ArgumentValues& args, ReturnValues& ret);
void ExecuteInputEvent(const std::string& name, ArgumentValues& args, ReturnValues& ret);
Expand All @@ -97,16 +111,10 @@ class ModuleInterface
template<typename T>
void AddModuleItemParam(const void *item, std::pair<std::string, T> parameter);

void AddModuleOutputEvent(const ModuleOutputEvent& event);
void AddModuleInputEvent(const ModuleInputEvent& event);
void AddModuleRenderInstance(const std::shared_ptr<ModuleRenderInstance> &event);
void AddModuleFunction(const ModuleFunction& event);
std::vector<std::shared_ptr<ModuleRenderInstance>> GetModuleRenderInstances();

void LogInfo(const std::string& message);
void LogWarning(const std::string& message);
void LogError(const std::string& message);
void LogFatal(const std::string& message);

void CallOutputEvent(const std::string &event_name, ArgumentValues& args, ReturnValues& ret);
void CallInputEvent(const std::string &module_name, const std::string &event_name, ArgumentValues& args, ReturnValues& ret);
Expand Down Expand Up @@ -138,6 +146,7 @@ class ModuleInterface
size_t m_logo_size;

std::string m_state = "unknow";
std::shared_ptr<Cherry::AppWindow> m_main_window;

std::vector<std::shared_ptr<ModuleFunction>> m_functions;
std::vector<std::shared_ptr<ModuleOutputEvent>> m_output_events;
Expand Down
76 changes: 46 additions & 30 deletions main/src/modules/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void ModuleInterface::AddModuleFunction(const ModuleFunction &function)
*
* @param event The ModuleOutputEvent to add.
*/
void ModuleInterface::AddModuleOutputEvent(const ModuleOutputEvent &event)
void ModuleInterface::AddOutputEvent(const ModuleOutputEvent &event)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleOutputEvent> p_event = std::make_shared<ModuleOutputEvent>(event);
Expand All @@ -34,8 +34,28 @@ void ModuleInterface::AddModuleOutputEvent(const ModuleOutputEvent &event)
this->m_output_events.push_back(p_event);
}

void ModuleInterface::AddModuleInputEvent(const ModuleInputEvent &event)
void ModuleInterface::AddInputEvent(const ModuleInputEvent &event)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(event);

// Add the shared_ptr to the list of output events
this->m_input_events.push_back(p_event);
}

void ModuleInterface::SetMainWindow(const std::shared_ptr<Cherry::AppWindow> &win)
{
// Remove potential old main window
for(auto& window : Cherry::Application::Get().m_AppWindows)
{
if(m_main_window->m_IdName == window->m_IdName)
{
Cherry::DeleteAppWindow(window);
}
}

// Add/Set the new one
Cherry::AddAppWindow(win);
}

/**
Expand All @@ -51,30 +71,6 @@ void ModuleInterface::AddModuleRenderInstance(const std::shared_ptr<ModuleRender
this->m_render_instances.push_back(renderInstance);
}

/**
* @brief Adds an argument to the ModuleInterface's argument container.
*
* This function adds a key-value pair to the ModuleInterface's argument container.
* If the container does not exist, it creates a new one and adds the argument.
*
* @tparam T The type of the argument value.
* @param key The key for the argument.
* @param value The value of the argument.
*/
template <typename T>
void ModuleInterface::AddArg(const std::string &key, T value)
{
if (this->m_args == nullptr)
{
// Create a new argument container if it doesn't exist
std::shared_ptr<hArgs> args = std::make_shared<hArgs>();
this->m_args = args;
}

// Add the argument to the container
this->m_args->add<T>(key.c_str(), value);
}

/**
* @brief Sets the logo for the ModuleInterface.
*
Expand Down Expand Up @@ -132,18 +128,38 @@ void ModuleInterface::AddOutputEvent(std::function<void(ArgumentValues &, Return

void ModuleInterface::AddInputEvent(std::function<void()> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(foo, name);

// Add the shared_ptr to the list of output events
this->m_input_events.push_back(p_event);
}

void ModuleInterface::AddInputEvent(std::function<void(ArgumentValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(foo, name);

// Add the shared_ptr to the list of output events
this->m_input_events.push_back(p_event);
}

void ModuleInterface::AddInputEvent(std::function<void(ReturnValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(foo, name);

// Add the shared_ptr to the list of output events
this->m_input_events.push_back(p_event);
}

void ModuleInterface::AddInputEvent(std::function<void(ArgumentValues &, ReturnValues &)> foo, const std::string &name)
{
// Create a shared_ptr to the ModuleOutputEvent
std::shared_ptr<ModuleInputEvent> p_event = std::make_shared<ModuleInputEvent>(foo, name);

// Add the shared_ptr to the list of output events
this->m_input_events.push_back(p_event);
}

/**
Expand Down Expand Up @@ -637,13 +653,13 @@ void ModuleInterface::Stop()
}
}

void ModuleInterface::CallOutputEvent(const std::string &event_name, ArgumentValues& args, ReturnValues& ret)
{
void ModuleInterface::CallOutputEvent(const std::string &event_name, ArgumentValues &args, ReturnValues &ret)
{
VortexMaker::CallOutputEvent(event_name, args, ret, this->m_name);
}

void ModuleInterface::CallInputEvent(const std::string &module_name, const std::string &event_name, ArgumentValues& args, ReturnValues& ret)
{
void ModuleInterface::CallInputEvent(const std::string &module_name, const std::string &event_name, ArgumentValues &args, ReturnValues &ret)
{
VortexMaker::CallInputEvent(module_name, event_name, args, ret, this->m_name);
}

Expand Down
6 changes: 3 additions & 3 deletions main/src/vortex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,18 @@ VORTEX_API void VortexMaker::CallOutputEvent(const std::string &event_name, Argu
if (output_event->m_function)
{
// Trigger a information trigger in the input event
output_event->trigger_happening(origin + ":call_input_event", HappeningState::INFO, "Calling module input event \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" from \"" + origin + "\"");
output_event->trigger_happening(origin + ":call_output_event", HappeningState::INFO, "Calling module output event \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" from \"" + origin + "\"");

// Call the corresponding event function with the provided arguments
output_event->m_function(args, ret);

// Trigger a information trigger in the input event
output_event->trigger_happening(origin + ":call_input_event", HappeningState::INFO, "Input event \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" called succefully from \"" + origin + "\" !");
output_event->trigger_happening(origin + ":call_output_event", HappeningState::INFO, "Output event \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" called succefully from \"" + origin + "\" !");
}
else
{
// Trigger a information trigger in the input event
output_event->trigger_happening(origin + ":call_input_event", HappeningState::INFO, "Trying to call \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" from \"" + origin + "\" but it not exist !");
output_event->trigger_happening(origin + ":call_output_event", HappeningState::INFO, "Trying to call \"" + output_event->m_name + "\" of module \"" + em->m_name + "\" from \"" + origin + "\" but it not exist !");
}
}
}
Expand Down

0 comments on commit 1307ddb

Please sign in to comment.