add: Widget registry
This commit is contained in:
22
frame/src/widgets/WidgetRegistry.cpp
Normal file
22
frame/src/widgets/WidgetRegistry.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "WidgetRegistry.hpp"
|
||||
|
||||
namespace frame::widgets
|
||||
{
|
||||
|
||||
WidgetRegistry::WidgetRegistry()
|
||||
: IService("WidgetRegistry")
|
||||
{
|
||||
}
|
||||
|
||||
Widget::shared_ptr WidgetRegistry::Create(std::string_view name) const
|
||||
{
|
||||
auto it = mWidgets.find(std::string{name});
|
||||
if(it == mWidgets.end())
|
||||
{
|
||||
fmt::print("[ERROR] Widget {} not found!\n", name);
|
||||
return nullptr;
|
||||
}
|
||||
return it->second();
|
||||
}
|
||||
|
||||
} // namespace frame::widgets
|
||||
52
frame/src/widgets/WidgetRegistry.hpp
Normal file
52
frame/src/widgets/WidgetRegistry.hpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include "../ServiceLocator.hpp"
|
||||
#include "Widget.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#define REGISTER_WIDGET(TYPE, NAME) \
|
||||
static bool registered = \
|
||||
frame::Service::get<frame::widgets::WidgetRegistry>()->Register<TYPE>( \
|
||||
NAME)
|
||||
|
||||
namespace frame::widgets
|
||||
{
|
||||
|
||||
class WidgetRegistry : public IService
|
||||
{
|
||||
|
||||
using Creator = std::function<Widget::shared_ptr()>;
|
||||
|
||||
std::unordered_map<std::string, Creator> mWidgets;
|
||||
|
||||
public:
|
||||
WidgetRegistry();
|
||||
|
||||
template<class WIDGET>
|
||||
bool Register(std::string_view name);
|
||||
|
||||
Widget::shared_ptr Create(std::string_view name) const;
|
||||
};
|
||||
|
||||
template<class WIDGET>
|
||||
bool WidgetRegistry::Register(std::string_view name)
|
||||
{
|
||||
auto const name_string = std::string{name};
|
||||
if(mWidgets.find(name_string) != mWidgets.end())
|
||||
{
|
||||
fmt::print("Widget {} already registered!\n", name);
|
||||
return true;
|
||||
}
|
||||
|
||||
mWidgets[name_string] = []() -> Widget::shared_ptr {
|
||||
return WIDGET::Create();
|
||||
};
|
||||
|
||||
fmt::print("Widget {} registered!\n", name);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace frame::widgets
|
||||
@@ -5,6 +5,14 @@
|
||||
|
||||
constexpr double pi = 3.14159;
|
||||
|
||||
#include "../WidgetRegistry.hpp"
|
||||
/*
|
||||
static bool registerd =
|
||||
frame::Service::get<frame::widgets::WidgetRegistry>()
|
||||
->Register<frame::widgets::AnalogClock>("AnalogClock");*/
|
||||
|
||||
REGISTER_WIDGET(frame::widgets::AnalogClock, "AnalogClock");
|
||||
|
||||
namespace frame::widgets
|
||||
{
|
||||
|
||||
@@ -72,8 +80,8 @@ namespace frame::widgets
|
||||
auto const min_rad = (min_steps * c_time->tm_min - 90) * pi / 180.f;
|
||||
|
||||
rt.DrawLine(center,
|
||||
{center.x + int32_t(std::cos(min_rad) * radius),
|
||||
center.y + int32_t(std::sin(min_rad) * radius)});
|
||||
{center.x + int32_t(std::cos(min_rad) * radius * 0.8),
|
||||
center.y + int32_t(std::sin(min_rad) * radius * 0.8)});
|
||||
|
||||
// Render Pointer Hour
|
||||
|
||||
|
||||
Reference in New Issue
Block a user