Thread Tools Display Modes
04/14/14, 01:04 PM   #1
Wukar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 27
Event Chaining

Hi,

i try to attach actions to already defined ui event, for example inventory tab clicks, without killing the already assigned actions. I saw a solution inside the InventoryMod (by Cr4x) but it looks cumbersome and may fail once another addon uses its own event storage.

The PreHookHandler, however, is not suitable in this scenario.

lua Code:
  1. -- that kills the default behavior
  2. ZO_PlayerInventoryTabsButton7:SetHandler("OnMouseUp", function() {}))
  Reply With Quote
04/14/14, 01:53 PM   #2
Nogaruk
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 15
To your example, I could be wrong, but I think the problem is you're not giving it anything to do.

-- SetHandler(string handlerName, function functionRef)

When I've used handlers before I haven't had any issues, though, it is possible the ones I've needed didn't already use the event I did. I would think the way you would want to write it though, would be:

Code:
ZO_PlayerInventoryTabsButton7:SetHandler("OnMouseUp", function(self) addonName:SomeFunction() end)
If that still breaks it, you could try seeing if registering into the main mouse event will work. You'll of course have to check against what the mouse was interacting with.

EVENT_GLOBAL_MOUSE_UP (integer button, bool ctrl, bool alt, bool shift, bool command)
I believe it would be:
Code:
EVENT_MANAGER:RegisterForEvent(addonName .. "_OnMouseUp", EVENT_GLOBAL_MOUSE_UP , function(...) self:OnMouseUpHandler(...) end)

function addonName:OnMouseUpHandler(event, button, ctrl, alt, shift, command)
end
Another choice would be to change what event you want to handle. For example you could try:
Code:
ZO_PlayerInventoryTabsButton7:SetHandler("OnClicked", function(self) addonName:SomeFunction() end)
edit: forgot an example

Last edited by Nogaruk : 04/14/14 at 01:58 PM.
  Reply With Quote
04/14/14, 02:12 PM   #3
Wukar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 27
Thanks for the examples. The OnMouseUp, kills the default operation, OnClicked is ignored. Maybe there's just something wrong with my event handler assignment:

lua Code:
  1. local function BBS_GetActiveTabs(tabControl)
  2.     local children = {}
  3.     local counter = tabControl:GetNumChildren()
  4.     for i=1, counter do
  5.         local child = tabControl:GetChild(i)
  6.        
  7.         if (child:GetType() == CT_CONTROL) then
  8.             -- add a handler for each control
  9.             -- TODO: add one handler to the parent and handle tab changes
  10.             child:SetHandler("OnClicked", function(self)
  11.                 BBS_SearchBoxOnTextChanged(bbs.editbox)
  12.             end)
  13.             children[i] = child
  14.         end
  15.     end
  16.     return children
  17. end
  Reply With Quote
04/14/14, 02:19 PM   #4
Nogaruk
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 15
I believe the "OnClicked" will only work if it is a CT_BUTTON. Beyond that, you'll probably have to wait for someone more experienced to see this. I have not needed to use handlers that required parameters to be passed along yet, so not sure if there's anything else wrong with that. Otherwise the code looks right.

Edit: also, I'm not sure if the syntax is correct unless you're calling a global function, but it might need addonName or self for

Code:
child:SetHandler("OnClicked", function(self) addonName:BBS_SearchBoxOnTextChanged(bbs.editbox) end)

Last edited by Nogaruk : 04/14/14 at 02:25 PM.
  Reply With Quote
04/14/14, 06:09 PM   #5
Wukar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 27
The EVENT_GLOBAL_MOUSE_UP did the trick, thanks

Anyway, the event chaining problem still ain't solved (at least for me )
  Reply With Quote
04/14/14, 08:56 PM   #6
Kith
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 49
You could try using the ZO built in prehook. As far as I know there is no posthook though so it'll run before the default code does
Lua Code:
  1. ZO_PreHookHandler(ZO_PlayerInventoryTabsButton7, 'OnMouseUp', function(...)
  2.     -- do your stuff here, the ... is the variables passed
  3. end)
  Reply With Quote
04/15/14, 03:38 AM   #7
Cr4x
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 11
Originally Posted by Wukar View Post
Hi,
... and may fail once another addon uses its own event storage.
Each addon should make sure not to override any existing handlers, wether it is using a event storage or not.
  Reply With Quote
04/15/14, 04:42 AM   #8
Vicster0
 
Vicster0's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 82
Originally Posted by Kith View Post
You could try using the ZO built in prehook. As far as I know there is no posthook though so it'll run before the default code does
Lua Code:
  1. ZO_PreHookHandler(ZO_PlayerInventoryTabsButton7, 'OnMouseUp', function(...)
  2.     -- do your stuff here, the ... is the variables passed
  3. end)
I had no idea there was a built in PreHook in the API. I just made them myself where necessary and store the old event handler separately in case the player turns off the addon. The only problem I have run into here is if the original event handler eventually runs protected/private code somewhere in the stack. Does anyone know if the ZO PreHook accounts for that?
  Reply With Quote
04/15/14, 05:06 AM   #9
Wukar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 27
I tried the PreHookHandler but unfortunately the inventory list data is updated afterwards
  Reply With Quote
04/15/14, 06:45 AM   #10
Wobin
 
Wobin's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 78
Originally Posted by Vicster0 View Post
I had no idea there was a built in PreHook in the API. I just made them myself where necessary and store the old event handler separately in case the player turns off the addon. The only problem I have run into here is if the original event handler eventually runs protected/private code somewhere in the stack. Does anyone know if the ZO PreHook accounts for that?
It doesn't, unfortunately.

You're safer trying to hook closer to the actual source.
  Reply With Quote
04/15/14, 09:34 AM   #11
Vicster0
 
Vicster0's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 82
Originally Posted by Wobin View Post
It doesn't, unfortunately.

You're safer trying to hook closer to the actual source.
Good to know.

I believe I was hooking as close as possible; I configured the prehook for the OnMouseOver handler for the generic ZO_InventorySlot. The problem is that somewhere in ZO's code, where they bind commands to objects for keybind interactions (in this case, pressing 'e' to use an item in the inventory or transfer to and from the bank), the original mouseover handler can call, and in this case, does call, a private/protected function. This would be fine as it's their handler, but once you localize that original handler (for use in a prehooked function, for instance) it becomes part of 'your addon's scope' and thus unsafe code meaning it can no longer run that private/protected function. Or, at least, this is how I am understanding it....

Please do correct me if I am mistaken or missing something!


Lua Code:
  1. INVENTORYSLOT_ORIGINAL_MOUSE_ENTER_HANDLER = ZO_InventorySlot_OnMouseEnter;
  2.  
  3. --OnMouseEnter preHook
  4. ZO_InventorySlot_OnMouseEnter = function( ... )
  5.         -- { my code } --
  6.         INVENTORYSLOT_ORIGINAL_MOUSE_ENTER_HANDLER( ... );
  7. end


I've since changed this code to use an existing mouseover event that I have hooked into as the calling function and utilize the moc() functionality instead, alleviating the issue with the private function.
  Reply With Quote
04/15/14, 02:21 PM   #12
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
Originally Posted by Wukar View Post
I tried the PreHookHandler but unfortunately the inventory list data is updated afterwards
Then...if they have a PreHookHandler...what about a PostHookHandler?
  Reply With Quote
04/16/14, 08:28 AM   #13
Vicster0
 
Vicster0's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 82
Originally Posted by Iyanga View Post
Then...if they have a PreHookHandler...what about a PostHookHandler?
Doesn't look like it...

Originally Posted by Kith View Post
You could try using the ZO built in prehook. As far as I know there is no posthook though so it'll run before the default code does
Lua Code:
  1. ZO_PreHookHandler(ZO_PlayerInventoryTabsButton7, 'OnMouseUp', function(...)
  2.     -- do your stuff here, the ... is the variables passed
  3. end)
  Reply With Quote
04/16/14, 09:09 AM   #14
Froali
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 13
Originally Posted by Iyanga View Post
Then...if they have a PreHookHandler...what about a PostHookHandler?
There is no PostHookHandler provided by ZOS, but look at this thread:
http://www.esoui.com/forums/showthread.php?t=564

I'm using this solution in my AddOn, but when hooking into some Functions i encountered a problem with "unsafe function call" errors, where hooking into other functions works without problems. So be warned
  Reply With Quote
04/16/14, 09:34 AM   #15
Vicster0
 
Vicster0's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 82
Originally Posted by Froali View Post
There is no PostHookHandler provided by ZOS, but look at this thread:
http://www.esoui.com/forums/showthread.php?t=564

I'm using this solution in my AddOn, but when hooking into some Functions i encountered a problem with "unsafe function call" errors, where hooking into other functions works without problems. So be warned
Yup, that's exactly what I said above. :P
  Reply With Quote
04/16/14, 09:40 AM   #16
Froali
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 13
ooops, i think i was a bit to fast with hitting "reply" then
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Event Chaining


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off