PDA

View Full Version : Capture global onMenuSelection().



StoryWeaver
March 30th, 2019, 14:18
Hi.

I'm trying to solve a particular lua issue for an extension I'm writing. I'm creating some global functionality that will add a radial menu item to many different windows.
In this stripped down implementation what the code does is it overrides the standard Interface.onWindowOpened function correctly, and adds the menu item to the various windows radial menus.

Later on I will filter what particular windows get the menu item by w.getClass().getText() filtering.

This part is all working so far.

However I am unable to capture the menu item selection as I've been unable to make a global override of onMenuSelection() for the windowinstance's the menu item gets added to.

I've tried a few different override attempts such as:
w.onMenuSelection = menuAction;
windowinstance.onMenuSelection = menuAction;
But none have worked so far.

This functionality will be applied to, to many different windows, to make it feasible to copy out and modify the individual xml files for each of those, as it would make it bad to maintain with official updates and make it more likely to clash with other extensions.



function onInit()
Interface.onWindowOpened = windowCheck;
end


function windowCheck(w)
if w == nil then return; end

w.registerMenuItem(Interface.getString("Test"), "Test", 5);
onMenuSelection = menuAction;
end


function menuAction(selection, subselection, subsubselection)
if selection == 5 then
-- do some things
end
end




What am I doing wrong here, how do I capture or register the capture of the override of onMenuSelection for the different window the function interacts with?

Also is there a super() function call I should make in the menuAction function to make sure the regular onMenuSelection functionality runs as well?

Trenloe
March 30th, 2019, 14:33
onMenuSelection (https://www.fantasygrounds.com/refdoc/windowinstance.xcp#onMenuSelection) is an event, not a handler - so any code using this event has to be in the script extending the control. I think you'll have to modify the script attached to each <windowclass> you want to receive the event for.

See the difference between events and handlers here: https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting#Using_Events

"To take advantage of events, a script block needs to be created extending the element and defining the event function"

StoryWeaver
March 31st, 2019, 00:36
Thanks Trenloe.

I've decreased the scope of the extension and have it working now.