Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/gui/CGUI.cpp
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | if (ev->ev.type == SDL_HOTKEYDOWN || ev->ev.type == SDL_HOTKEYPRESS || ev->ev.type == SDL_HOTKEYUP) | ||||
const char* hotkey = static_cast<const char*>(ev->ev.user.data1); | const char* hotkey = static_cast<const char*>(ev->ev.user.data1); | ||||
const CStr& eventName = ev->ev.type == SDL_HOTKEYPRESS ? EventNamePress : ev->ev.type == SDL_HOTKEYDOWN ? EventNameKeyDown : EventNameRelease; | const CStr& eventName = ev->ev.type == SDL_HOTKEYPRESS ? EventNamePress : ev->ev.type == SDL_HOTKEYDOWN ? EventNameKeyDown : EventNameRelease; | ||||
if (m_GlobalHotkeys.find(hotkey) != m_GlobalHotkeys.end() && m_GlobalHotkeys[hotkey].find(eventName) != m_GlobalHotkeys[hotkey].end()) | if (m_GlobalHotkeys.find(hotkey) != m_GlobalHotkeys.end() && m_GlobalHotkeys[hotkey].find(eventName) != m_GlobalHotkeys[hotkey].end()) | ||||
{ | { | ||||
ret = IN_HANDLED; | ret = IN_HANDLED; | ||||
JSContext* cx = m_ScriptInterface->GetContext(); | ScriptInterface::Request rq(*m_ScriptInterface); | ||||
JSAutoRequest rq(cx); | JS::RootedObject globalObj(rq.cx, &GetGlobalObject().toObject()); | ||||
JS::RootedObject globalObj(cx, &GetGlobalObject().toObject()); | JS::RootedValue result(rq.cx); | ||||
JS::RootedValue result(cx); | JS_CallFunctionValue(rq.cx, globalObj, m_GlobalHotkeys[hotkey][eventName], JS::HandleValueArray::empty(), &result); | ||||
JS_CallFunctionValue(cx, globalObj, m_GlobalHotkeys[hotkey][eventName], JS::HandleValueArray::empty(), &result); | |||||
} | } | ||||
std::map<CStr, std::vector<IGUIObject*> >::iterator it = m_HotkeyObjects.find(hotkey); | std::map<CStr, std::vector<IGUIObject*> >::iterator it = m_HotkeyObjects.find(hotkey); | ||||
if (it != m_HotkeyObjects.end()) | if (it != m_HotkeyObjects.end()) | ||||
for (IGUIObject* const& obj : it->second) | for (IGUIObject* const& obj : it->second) | ||||
{ | { | ||||
if (ev->ev.type == SDL_HOTKEYPRESS) | if (ev->ev.type == SDL_HOTKEYPRESS) | ||||
ret = obj->SendEvent(GUIM_PRESSED, EventNamePress); | ret = obj->SendEvent(GUIM_PRESSED, EventNamePress); | ||||
▲ Show 20 Lines • Show All 289 Lines • ▼ Show 20 Lines | std::remove_if( | ||||
assignment.end(), | assignment.end(), | ||||
[&pObject](const IGUIObject* hotkeyObject) | [&pObject](const IGUIObject* hotkeyObject) | ||||
{ return pObject == hotkeyObject; }), | { return pObject == hotkeyObject; }), | ||||
assignment.end()); | assignment.end()); | ||||
} | } | ||||
void CGUI::SetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName, JS::HandleValue function) | void CGUI::SetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName, JS::HandleValue function) | ||||
{ | { | ||||
JSContext* cx = m_ScriptInterface->GetContext(); | ScriptInterface::Request rq(*m_ScriptInterface); | ||||
JSAutoRequest rq(cx); | |||||
if (hotkeyTag.empty()) | if (hotkeyTag.empty()) | ||||
{ | { | ||||
JS_ReportError(cx, "Cannot assign a function to an empty hotkey identifier!"); | JS_ReportError(rq.cx, "Cannot assign a function to an empty hotkey identifier!"); | ||||
return; | return; | ||||
} | } | ||||
// Only support "Press", "Keydown" and "Release" events. | // Only support "Press", "Keydown" and "Release" events. | ||||
if (eventName != EventNamePress && eventName != EventNameKeyDown && eventName != EventNameRelease) | if (eventName != EventNamePress && eventName != EventNameKeyDown && eventName != EventNameRelease) | ||||
{ | { | ||||
JS_ReportError(cx, "Cannot assign a function to an unsupported event!"); | JS_ReportError(rq.cx, "Cannot assign a function to an unsupported event!"); | ||||
return; | return; | ||||
} | } | ||||
if (!function.isObject() || !JS_ObjectIsFunction(cx, &function.toObject())) | if (!function.isObject() || !JS_ObjectIsFunction(rq.cx, &function.toObject())) | ||||
{ | { | ||||
JS_ReportError(cx, "Cannot assign non-function value to global hotkey '%s'", hotkeyTag.c_str()); | JS_ReportError(rq.cx, "Cannot assign non-function value to global hotkey '%s'", hotkeyTag.c_str()); | ||||
return; | return; | ||||
} | } | ||||
UnsetGlobalHotkey(hotkeyTag, eventName); | UnsetGlobalHotkey(hotkeyTag, eventName); | ||||
m_GlobalHotkeys[hotkeyTag][eventName].init(cx, function); | m_GlobalHotkeys[hotkeyTag][eventName].init(rq.cx, function); | ||||
} | } | ||||
void CGUI::UnsetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName) | void CGUI::UnsetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName) | ||||
{ | { | ||||
std::map<CStr, std::map<CStr, JS::PersistentRootedValue>>::iterator it = m_GlobalHotkeys.find(hotkeyTag); | std::map<CStr, std::map<CStr, JS::PersistentRootedValue>>::iterator it = m_GlobalHotkeys.find(hotkeyTag); | ||||
if (it == m_GlobalHotkeys.end()) | if (it == m_GlobalHotkeys.end()) | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 841 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator