-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,76 @@ | ||
#include "pch.h" | ||
#include "JsScreen.h" | ||
#include <client/event/impl/RendererInitEvent.h> | ||
#include <client/event/impl/RenderGameEvent.h> | ||
#include "sdk/common/client/gui/controls/VisualTree.h" | ||
#include "sdk/common/client/gui/controls/UIControl.h" | ||
|
||
JsScreen::JsScreen(JsValueRef object, JsValueRef renderFunc) : renderFunc(renderFunc) { | ||
JS::JsGetCurrentContext(&this->ctx); | ||
|
||
Chakra::GetStringProperty(object, L"name"); | ||
key = Chakra::GetIntProperty(object, L"key"); | ||
|
||
|
||
Eventing::get().listen<RenderLayerEvent>(this, (EventListenerFunc)&JsScreen::onRender, 1, true); | ||
Eventing::get().listen<KeyUpdateEvent>(this, (EventListenerFunc)&JsScreen::onKey, 1); | ||
Eventing::get().listen<ClickEvent>(this, (EventListenerFunc)&JsScreen::onClick, 1); | ||
|
||
this->eventListeners[L"enable"] = {}; | ||
this->eventListeners[L"disable"] = {}; | ||
this->eventListeners[L"render"] = {}; | ||
this->eventListeners[L"key"] = {}; | ||
this->eventListeners[L"click"] = {}; | ||
} | ||
|
||
void JsScreen::onRender(Event& evG) { | ||
auto& ev = reinterpret_cast<RenderLayerEvent&>(evG); | ||
auto view = ev.getScreenView(); | ||
if (view->visualTree->rootControl->name == "debug_screen") { | ||
Chakra::SetContext(ctx); | ||
|
||
Event ev{ L"render", {} }; | ||
auto ret = dispatchEvent(ev.name, ev); | ||
if (ret != JS_INVALID_REFERENCE) { | ||
Chakra::Release(ret); | ||
} | ||
} | ||
} | ||
|
||
void JsScreen::onKey(Event& evG) { | ||
auto ret = dispatchEvent(sEv.name, sEv); | ||
if (ret != JS_INVALID_REFERENCE) { | ||
Chakra::Release(ret); | ||
} | ||
} | ||
|
||
void JsScreen::onRender(Event& ev) | ||
{ | ||
void JsScreen::onClick(Event& evG) { | ||
auto& ev = reinterpret_cast<ClickEvent&>(evG); | ||
Event sEv{ L"click", {Chakra::MakeInt(ev.getMouseButton()), Chakra::MakeInt(ev.getWheelDelta()), ev.isDown() ? Chakra::GetTrue() : Chakra::GetFalse() } }; | ||
auto ret = dispatchEvent(sEv.name, sEv); | ||
if (ret != JS_INVALID_REFERENCE) { | ||
Chakra::Release(ret); | ||
} | ||
} | ||
|
||
void JsScreen::onEnable(bool ignoreAnims) { | ||
Latite::get().queueForClientThread([this, ignoreAnims]() { | ||
Chakra::SetContext(ctx); | ||
|
||
Event ev{ L"enable", { (ignoreAnims ? Chakra::GetTrue() : Chakra::GetFalse()) }}; | ||
auto ret = dispatchEvent(ev.name, ev); | ||
if (ret != JS_INVALID_REFERENCE) { | ||
Chakra::Release(ret); | ||
} | ||
}); | ||
} | ||
|
||
void JsScreen::onDisable() { | ||
Latite::get().queueForClientThread([this]() { | ||
Chakra::SetContext(ctx); | ||
Event ev{ L"disable", { } }; | ||
auto ret = dispatchEvent(ev.name, ev); | ||
if (ret != JS_INVALID_REFERENCE) { | ||
Chakra::Release(ret); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
#pragma once | ||
#include "../JsWrapperClass.h" | ||
#include "util/ChakraUtil.h" | ||
#include "client/screen/script/JsScreen.h" | ||
|
||
class JsScreenClass : public JsWrapperClass<JsScreen> { | ||
protected: | ||
static JsValueRef CALLBACK jsConstructor(JsValueRef callee, bool isConstructor, | ||
JsValueRef* arguments, unsigned short argCount, void* callbackState) { | ||
auto thi = reinterpret_cast<JsScreenClass*>(callbackState); | ||
if (!Chakra::VerifyArgCount(argCount, 2)) return JS_INVALID_REFERENCE; | ||
if (!Chakra::VerifyParameters({ {arguments[1], JsString}, {arguments[2], JsFunction} })) return JS_INVALID_REFERENCE; | ||
|
||
auto screen = new JsScreen(arguments[0], arguments[2]); | ||
JsValueRef obj = thi->construct(screen, false); | ||
return obj; | ||
} | ||
|
||
static JsValueRef CALLBACK toStringCallback(JsValueRef callee, bool isConstructor, | ||
JsValueRef* arguments, unsigned short argCount, void* callbackState) { | ||
auto thi = reinterpret_cast<JsScreenClass*>(callbackState); | ||
auto scn = Get(arguments[0]); | ||
std::string add = std::format("{} ({})", util::WStrToStr(thi->name), scn->getName()); | ||
return Chakra::MakeString(L"[object " + util::StrToWStr(add) + L"]"); | ||
} | ||
|
||
static JsValueRef CALLBACK isActiveCallback(JsValueRef callee, bool isConstructor, | ||
JsValueRef* arguments, unsigned short argCount, void* callbackState) { | ||
|
||
return Get(arguments[0])->isActive() ? Chakra::GetTrue() : Chakra::GetFalse(); | ||
} | ||
public: | ||
inline static const wchar_t* class_name = L"Screen"; | ||
|
||
JsScreenClass(class JsScript* owner) : JsWrapperClass(owner, class_name) { | ||
createConstructor(jsConstructor, this); | ||
} | ||
|
||
/// <summary> | ||
/// Constructs a JS screen. | ||
/// </summary> | ||
/// <param name="set">The C++ screen to create from.</param> | ||
/// <param name="finalize">Whether to destroy the C++ screen when the object goes out of scope or not.</param> | ||
/// <returns></returns> | ||
JsValueRef construct(JsScreen* screen, bool finalize) { | ||
auto obj = __super::construct(screen, finalize); | ||
|
||
Chakra::SetPropertyString(obj, L"name", util::StrToWStr(screen->getName()), true); | ||
return obj; | ||
} | ||
|
||
void prepareFunctions() override { | ||
Chakra::SetPropertyString(prototype, L"name", L"", true); | ||
Chakra::DefineFunc(prototype, isActiveCallback, L"isActive", this); | ||
}; | ||
}; |