mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Optimize the GUI event broadcast
Patch By: nani Differential Revision: D2638 This was SVN commit r24185.
This commit is contained in:
parent
10c3bcbf5f
commit
768c84aa46
3 changed files with 35 additions and 2 deletions
|
|
@ -278,12 +278,24 @@ void CGUI::TickObjects()
|
|||
|
||||
void CGUI::SendEventToAll(const CStr& eventName)
|
||||
{
|
||||
m_BaseObject.RecurseObject(nullptr, &IGUIObject::ScriptEvent, eventName);
|
||||
auto it = m_EventIGUIObjects.find(eventName);
|
||||
if (it == m_EventIGUIObjects.end())
|
||||
return;
|
||||
|
||||
std::set<IGUIObject*> copy = it->second;
|
||||
for (IGUIObject* pIGUIObject : copy)
|
||||
pIGUIObject->ScriptEvent(eventName);
|
||||
}
|
||||
|
||||
void CGUI::SendEventToAll(const CStr& eventName, const JS::HandleValueArray& paramData)
|
||||
{
|
||||
m_BaseObject.RecurseObject(nullptr, &IGUIObject::ScriptEvent, eventName, paramData);
|
||||
auto it = m_EventIGUIObjects.find(eventName);
|
||||
if (it == m_EventIGUIObjects.end())
|
||||
return;
|
||||
|
||||
std::set<IGUIObject*> copy = it->second;
|
||||
for (IGUIObject* pIGUIObject : copy)
|
||||
pIGUIObject->ScriptEvent(eventName, paramData);
|
||||
}
|
||||
|
||||
void CGUI::Draw()
|
||||
|
|
|
|||
|
|
@ -650,6 +650,12 @@ private:
|
|||
|
||||
// Icons
|
||||
std::map<CStr, const SGUIIcon> m_Icons;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Stores all the IGUIObject which listen to a given event.
|
||||
*/
|
||||
std::map<CStr, std::set<IGUIObject*>> m_EventIGUIObjects;
|
||||
};
|
||||
|
||||
#endif // INCLUDED_CGUI
|
||||
|
|
|
|||
|
|
@ -334,6 +334,12 @@ void IGUIObject::SetScriptHandler(const CStr& eventName, JS::HandleObject Functi
|
|||
JS_AddExtraGCRootsTracer(m_pGUI.GetScriptInterface()->GetJSRuntime(), Trace, this);
|
||||
|
||||
m_ScriptHandlers[eventName] = JS::Heap<JSObject*>(Function);
|
||||
|
||||
auto it = m_pGUI.m_EventIGUIObjects.find(eventName);
|
||||
if (it == m_pGUI.m_EventIGUIObjects.end())
|
||||
m_pGUI.m_EventIGUIObjects.emplace(eventName, std::set<IGUIObject*>{this});
|
||||
else
|
||||
it->second.insert(this);
|
||||
}
|
||||
|
||||
void IGUIObject::UnsetScriptHandler(const CStr& eventName)
|
||||
|
|
@ -347,6 +353,15 @@ void IGUIObject::UnsetScriptHandler(const CStr& eventName)
|
|||
|
||||
if (m_ScriptHandlers.empty())
|
||||
JS_RemoveExtraGCRootsTracer(m_pGUI.GetScriptInterface()->GetJSRuntime(), Trace, this);
|
||||
{
|
||||
auto it = m_pGUI.m_EventIGUIObjects.find(eventName);
|
||||
if (it != m_pGUI.m_EventIGUIObjects.end())
|
||||
{
|
||||
it->second.erase(this);
|
||||
if (!it->second.size())
|
||||
m_pGUI.m_EventIGUIObjects.erase(eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InReaction IGUIObject::SendEvent(EGUIMessageType type, const CStr& eventName)
|
||||
|
|
|
|||
Loading…
Reference in a new issue