Changeset View
Changeset View
Standalone View
Standalone View
source/gui/CGUI.cpp
Show All 31 Lines | |||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/GameSetup/Config.h" | #include "ps/GameSetup/Config.h" | ||||
#include "ps/Globals.h" | #include "ps/Globals.h" | ||||
#include "ps/Hotkey.h" | #include "ps/Hotkey.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "ps/Pyrogenesis.h" | #include "ps/Pyrogenesis.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include "renderer/Renderer.h" | #include "renderer/Renderer.h" | ||||
#include "scriptinterface/ScriptContext.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include <string> | #include <string> | ||||
#include <unordered_set> | #include <unordered_set> | ||||
extern int g_yres; | extern int g_yres; | ||||
const double SELECT_DBLCLICK_RATE = 0.5; | const double SELECT_DBLCLICK_RATE = 0.5; | ||||
▲ Show 20 Lines • Show All 44 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; | ||||
ScriptInterface::Request rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
JS::RootedObject globalObj(rq.cx, rq.glob); | JS::RootedObject globalObj(rq.cx, rq.glob); | ||||
JS::RootedValue result(rq.cx); | JS::RootedValue result(rq.cx); | ||||
JS_CallFunctionValue(rq.cx, globalObj, m_GlobalHotkeys[hotkey][eventName], JS::HandleValueArray::empty(), &result); | if (!JS_CallFunctionValue(rq.cx, globalObj, m_GlobalHotkeys[hotkey][eventName], JS::HandleValueArray::empty(), &result)) | ||||
ScriptException::CatchPending(rq); | |||||
} | } | ||||
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) | ||||
{ | { | ||||
ScriptInterface::Request rq(*m_ScriptInterface); | ScriptRequest rq(*m_ScriptInterface); | ||||
if (hotkeyTag.empty()) | if (hotkeyTag.empty()) | ||||
{ | { | ||||
JS_ReportError(rq.cx, "Cannot assign a function to an empty hotkey identifier!"); | ScriptException::Raise(rq, "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(rq.cx, "Cannot assign a function to an unsupported event!"); | ScriptException::Raise(rq, "Cannot assign a function to an unsupported event!"); | ||||
return; | return; | ||||
} | } | ||||
if (!function.isObject() || !JS_ObjectIsFunction(rq.cx, &function.toObject())) | if (!function.isObject() || !JS_ObjectIsFunction(rq.cx, &function.toObject())) | ||||
{ | { | ||||
JS_ReportError(rq.cx, "Cannot assign non-function value to global hotkey '%s'", hotkeyTag.c_str()); | ScriptException::Raise(rq, "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(rq.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) | ||||
▲ Show 20 Lines • Show All 846 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator