Skip to content

Commit

Permalink
StaticManager: add dynamic items
Browse files Browse the repository at this point in the history
  • Loading branch information
Imrglop committed Nov 30, 2024
1 parent b6ad268 commit d933124
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/api/manager/StaticManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ template <typename Base, typename ... Items>
class StaticManager {
protected:
std::tuple<Items...> items = { Items()... };
std::vector<std::shared_ptr<Base>> dynamicItems;
public:
StaticManager() : items() {
}
StaticManager(StaticManager<Base, Items...>&) = delete;
StaticManager(StaticManager<Base, Items...>&&) = delete;

template <typename T, typename ... Params>
void addDynamicItem(Params&&... args) {
dynamicItems.push_back(std::make_shared<T>(...args);
}

void forEach(std::function<void(Base&)> const& func) {
forEachImpl(func, items);
for (auto& item : dynamicItems) {
func(*item);
}
}

template <typename T>
Expand Down
2 changes: 2 additions & 0 deletions src/client/screen/script/JsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void JsScreen::onRender(Event& evG) {
}

void JsScreen::onKey(Event& evG) {
auto& ev = reinterpret_cast<KeyUpdateEvent&>(evG);
Event sEv{ L"key", {Chakra::MakeInt(ev.getKey()), ev.isDown() ? Chakra::GetTrue() : Chakra::GetFalse() }};
auto ret = dispatchEvent(sEv.name, sEv);
if (ret != JS_INVALID_REFERENCE) {
Chakra::Release(ret);
Expand Down
14 changes: 12 additions & 2 deletions src/client/script/objects/ClientScriptingObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ void ClientScriptingObject::initCommandManager() {
JS::JsAddRef(commandManager, nullptr);
}

void ClientScriptingObject::initScreenManager() {
JS::JsCreateObject(&this->commandManager);
JS::JsAddRef(commandManager, nullptr);
}

void ClientScriptingObject::initialize(JsContextRef ctx, JsValueRef parentObj) {
#if LATITE_DEBUG
Chakra::DefineFunc(object, testCallback, L"test", this);
Expand All @@ -101,9 +106,11 @@ void ClientScriptingObject::initialize(JsContextRef ctx, JsValueRef parentObj) {
Chakra::DefineFunc(object, showNotifCallback, XW("showNotification"));
Chakra::DefineFunc(object, getMmgrCallback, XW("getModuleManager"), this);
Chakra::DefineFunc(object, getCmgrCallback, XW("getCommandManager"), this);
Chakra::DefineFunc(object, getSmgrCallback, XW("getScreenManager"), this);

initModuleManager();
initCommandManager();
initScreenManager();

Chakra::DefineFunc(moduleManager, mmgrRegisterModuleCallback, XW("registerModule"));
Chakra::DefineFunc(moduleManager, mmgrGetModuleByName, XW("getModuleByName"));
Expand All @@ -115,8 +122,7 @@ void ClientScriptingObject::initialize(JsContextRef ctx, JsValueRef parentObj) {
}


JsValueRef ClientScriptingObject::mmgrRegisterModuleCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState)
{
JsValueRef ClientScriptingObject::mmgrRegisterModuleCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState) {
JsValueRef undefined;
JS::JsGetUndefinedValue(&undefined);
if (!Chakra::VerifyArgCount(argCount, 2)) return undefined;
Expand Down Expand Up @@ -335,3 +341,7 @@ JsValueRef ClientScriptingObject::cmgrDeregisterCommandCallback(JsValueRef calle
JsValueRef ClientScriptingObject::cmgrGetPrefixCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState){
return Chakra::MakeString(util::StrToWStr(Latite::getCommandManager().prefix));
}

JsValueRef ClientScriptingObject::smgrRegisterScreenCallback(JsValueRef callee, bool isConstructor, JsValueRef* arguments, unsigned short argCount, void* callbackState) {
return JsValueRef();
}
9 changes: 9 additions & 0 deletions src/client/script/objects/ClientScriptingObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ClientScriptingObject final : public ScriptingObject {
JsValueRef* arguments, unsigned short argCount, void* callbackState);
static JsValueRef CALLBACK getCmgrCallback(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount, void* callbackState);
static JsValueRef CALLBACK getSmgrCallback(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount, void* callbackState);


static JsValueRef CALLBACK testCallback(JsValueRef callee, bool isConstructor,
Expand All @@ -25,6 +27,7 @@ class ClientScriptingObject final : public ScriptingObject {

void initModuleManager();
void initCommandManager();
void initScreenManager();
void initialize(JsContextRef ctx, JsValueRef parentObj) override;

~ClientScriptingObject() {
Expand All @@ -35,6 +38,7 @@ class ClientScriptingObject final : public ScriptingObject {

JsValueRef moduleManager = JS_INVALID_REFERENCE;
JsValueRef commandManager = JS_INVALID_REFERENCE;
JsValueRef screenManager = JS_INVALID_REFERENCE;
private:
static JsValueRef CALLBACK mmgrRegisterModuleCallback(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount, void* callbackState);
Expand All @@ -55,4 +59,9 @@ class ClientScriptingObject final : public ScriptingObject {
JsValueRef* arguments, unsigned short argCount, void* callbackState);
static JsValueRef CALLBACK cmgrGetPrefixCallback(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount, void* callbackState);

// screen manager
static JsValueRef CALLBACK smgrRegisterScreenCallback(JsValueRef callee, bool isConstructor,
JsValueRef* arguments, unsigned short argCount, void* callbackState);

};

0 comments on commit d933124

Please sign in to comment.