ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Event Chaining (https://www.esoui.com/forums/showthread.php?t=896)

Wukar 04/14/14 01:04 PM

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() {}))

Nogaruk 04/14/14 01:53 PM

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

Wukar 04/14/14 02:12 PM

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

Nogaruk 04/14/14 02:19 PM

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)

Wukar 04/14/14 06:09 PM

The EVENT_GLOBAL_MOUSE_UP did the trick, thanks ;)

Anyway, the event chaining problem still ain't solved (at least for me :p)

Kith 04/14/14 08:56 PM

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)

Cr4x 04/15/14 03:38 AM

Quote:

Originally Posted by Wukar (Post 4290)
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.

Vicster0 04/15/14 04:42 AM

Quote:

Originally Posted by Kith (Post 4372)
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? :)

Wukar 04/15/14 05:06 AM

I tried the PreHookHandler but unfortunately the inventory list data is updated afterwards

Wobin 04/15/14 06:45 AM

Quote:

Originally Posted by Vicster0 (Post 4400)
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.

Vicster0 04/15/14 09:34 AM

Quote:

Originally Posted by Wobin (Post 4416)
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.

Iyanga 04/15/14 02:21 PM

Quote:

Originally Posted by Wukar (Post 4406)
I tried the PreHookHandler but unfortunately the inventory list data is updated afterwards

Then...if they have a PreHookHandler...what about a PostHookHandler?

Vicster0 04/16/14 08:28 AM

Quote:

Originally Posted by Iyanga (Post 4510)
Then...if they have a PreHookHandler...what about a PostHookHandler?

Doesn't look like it...

Quote:

Originally Posted by Kith (Post 4372)
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)


Froali 04/16/14 09:09 AM

Quote:

Originally Posted by Iyanga (Post 4510)
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 ;)

Vicster0 04/16/14 09:34 AM

Quote:

Originally Posted by Froali (Post 4598)
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

Froali 04/16/14 09:40 AM

ooops, i think i was a bit to fast with hitting "reply" then ;)


All times are GMT -6. The time now is 09:20 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI