ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   General Authoring Discussion (https://www.esoui.com/forums/forumdisplay.php?f=174)
-   -   Event Help Needed (https://www.esoui.com/forums/showthread.php?t=1627)

zgrssd 05/27/14 04:08 PM

Quote:

Originally Posted by Garkin (Post 8575)
I believe that event manager works the same way as callback object. It uses registry with structure similar to:
Lua Code:
  1. eventRegistry = {
  2.    [event1] = {
  3.       [identifier1] = callback1,
  4.       [identifier2] = callback2,
  5.    },
  6.    [event2] = {
  7.       [identifier1] = callback3,
  8.    },
  9. }

It does not work this way. There is no identifier for your "Registered Callback" the same way you have to specify one for your Event Registering - you have (<Event>, <Callback>) instead of
(<ID of Event handler registation>, <Event>, <Callback>). Maybe RegsiterCallback returns some value (like an int index).
Does anybody know how one could unregister a callback from a Callback Manager? Anything RegisterCallback Returns? Maybe you just give the callback again during unregister?

zgrssd 05/27/14 04:24 PM

Quote:

Originally Posted by lyravega (Post 8552)
What I really don't understand about creating custom events is, what do you watch? On examples, all custom events that I've seen are linked to "OnSomethingHappened" stuff. Since I am not working with UI (at all), I cannot grasp what else you can use instead of "OnSomethingHappened" stuff.

And don't bother, I won't be able to till I see some other examples, I learn better with dissecting stuff and inspecting them bit by bit :)

Events allow other code (that you do not need to know about in advance) to react to something happening in your code.
They do not know what we write in our OnUIUpdate Event Handlers. And still our Handlers are able to react to the event (specificalyl the Update/Draw phase of the UI).

A second aspect is that Events do not need to follow the usual rules for declaration. You can "Fire" an event in code, long before you have any Callback defined (much less registerd). With normal function calls you need to have the code you call defined before you call it*. Also you can register an arbitrary amount of different Callback functions to one Event.

*Wich can get you into a Egg and Chicken Paradox if A depends on B, B on C and C on A. And yes, I managed to programm myself into one of those.

Garkin 05/27/14 04:45 PM

Quote:

Originally Posted by zgrssd (Post 8635)
It does not work this way. There is no identifier for your "Registered Callback" the same way you have to specify one for your Event Registering - you have (<Event>, <Callback>) instead of
(<ID of Event handler registation>, <Event>, <Callback>). Maybe RegsiterCallback returns some value (like an int index).

That was comment for event manager, callback manager has a slightly different structure of registry.
Lua Code:
  1. object.callbackRegistry = {
  2.    [eventName] = {
  3.       [1] = callback,
  4.       [2] = argument,
  5.       [3] = isDeleted
  6.    }
  7. }
Quote:

Originally Posted by zgrssd (Post 8635)
Does anybody know how one could unregister a callback from a Callback Manager? Anything RegisterCallback Returns? Maybe you just give the callback again during unregister?

Lua Code:
  1. object:UnregisterCallback(eventName, callback)

zgrssd 05/27/14 05:00 PM

I rewrote the wiki explanation for Custom events. Based on the parts I did not understand properly when trying to learn it. I hope it is now easier to understand and use.
http://wiki.esoui.com/AddOn_Quick_Qu...s.22_in_Lua.3F

Also, regarding wanting an example. Here is a small lib I wrote (in part to learn Event Programming). On every UI update it checks if the Camera Heading has been changed (wich can be important to identify what target is under the cursor).
First I set up a dummy UI Element to intercept the UIUpdate Event. This was copied from exterminatus group Leader. There is no other way (like an Event Manager event) to get notified of a new Frame.
Code:

<GuiXml>
    <Controls>
        <TopLevelControl name="UI_UpdateInterceptor" movable="false">
            <Dimensions x="58" y="58" />
            <Anchor point="CENTER" />
                       
                        <OnUpdate>
                LibHeadingChange.onUIUpdate()
            </OnUpdate>
                       
        </TopLevelControl>
    </Controls>
</GuiXml>

Code:

--Background variable to store the last heading the Event was raised against
local SavedHeading = nil
--A table containing the Events String Name
local CustomEventNames = { HeadingChanged = "CUSTOM_EVENT_CAMERA_HEADING_CHANGED" }

--The OnUpdate handler of the XML Dummy Element
local function onUIUpdate()
        local currentHeading = GetPlayerCameraHeading()
       
        --If the heading has changed since you last used it, raise the Custom Event in the Global CALLBACK_MANAGER instance
        if (currentHeading ~= SavedHeading) then
                CALLBACK_MANAGER:FireCallbacks(CustomEventNames.HeadingChanged, SavedHeading, currentHeading)
                SavedHeading = currentHeading
        end
end

--Explicit exposure of local function globally under the choosen namespace
--Especially needed for the UI Element that need it#s handler
LibHeadingChange = {
        onUIUpdate = onUIUpdate,
        eventHeadingChanged = CustomEventNames.HeadingChanged
}

return LibHeadingChange

Now if somebody wanted to do anything if the heading changed (and not on every other Update Event) he would just have to do this:
Code:

--Set up handler for Custom event
local CustomEventExampleHandeler (oldHeading, newHeading)
    --Code to do something with those two parameters, or even just one of them
end

--registering the Callback
CALLBACK_MANAGER:RegisterCallback(LibHeadingChange.eventHeadingChanged, CustomEventExampleHandeler)


lyravega 05/27/14 05:09 PM

Quote:

Originally Posted by zgrssd (Post 8637)
*snip*

I know about event basics, just not creating custom events. That may be because I do not deal with UI, just options menu, and it is with the help of LAM - stuff that do not require custom events so to speak.

zgrssd 05/28/14 03:06 AM

Quote:

Originally Posted by lyravega (Post 8643)
I know about event basics, just not creating custom events. That may be because I do not deal with UI, just options menu, and it is with the help of LAM - stuff that do not require custom events so to speak.

Custom Events are by far not limited to UI stuff. They can be especialyl usefull for the non-UI side. I am currently working on adapting the core of the MVVM pattern (Change notification on properties) for Lua/Addon programming.
Propably need to delve into Metatables and simualted classes next before being totally there, but I can cobble something together right now. Propably will post it over in the getset-pattern thread however.

lyravega 05/28/14 09:04 PM

Quote:

Originally Posted by zgrssd (Post 8661)
Custom Events are by far not limited to UI stuff. They can be especialyl usefull for the non-UI side..

If you read my post slowly, you'll notice that I am saying "That may be because I do not deal with ... stuff that do not require custom events".


All times are GMT -6. The time now is 04:16 AM.

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