Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Fully-Featured Event Self-Registration for Images & Texts #2209

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

Roland-J
Copy link
Contributor

@Roland-J Roland-J commented Jul 9, 2022

Adds the plumbing required for images to support image:register_event('left_click', function) and image:register_event('drag', function), potentially making images much more friendly to work with by lowering the technical barrier to utilizing click events for images. (This functionality could also be extended to images if this PR is found desirable)

Sample addon for demo testing:
image

--------------------------------------------------------------------------------------------------------------
-- 1. Nonddraggable-image event self(:)-registration demo
--------------------------------------------------------------------------------------------------------------

-- Setup an image (nothing new here)
local images = require('images')
local image2 = images.new()
image2:pos(50, 75)
image2:size(175, 23)
image2:fit(false)
image2:path(windower.addon_path .. 'Button002.png')
image2:draggable(false)
image2:show()

-- Self(:)-register a left-click event (NEW!)
image2:register_event('left_click', function(...)
    print('I\m not draggable and I was just left_clicked!')
end)



--------------------------------------------------------------------------------------------------------------
-- 2. Draggable-image event self(:)-registration demo
-- NOTE: We only fire the left_click event on release and only if there was no dragging
--------------------------------------------------------------------------------------------------------------

local image = images.new()
image:pos(50, 50)
image:size(175, 23)
image:fit(false)
image:path(windower.addon_path .. 'Button002.png')
image:draggable(true)
image:show()

-- Self(:)-register a left-click event (NEW!)
image:register_event('left_click', function(...)
    print('I\'m draggable and I\'ve been clicked and unclicked, without being dragged!')
end)

-- Self(:)-register a drag event! (NEW!)
image:register_event('drag', function(...)
    local curr_x, curr_y = image:pos()
    local last_x, last_y = image:last_pos() --new function: provides the pos_x and pos_y of previous drag's final position
    local x_delta, y_delta = curr_x - last_x, curr_y - last_y --how to get current drag's precise x/y deltas, can be used to move clustered elements
    print('I\'ve been dragged and my delta is ' .. x_delta .. '/' .. y_delta .. '!')
end)

Adds the plumbing required for images to support image:register_event('left_click', function) and image:register_event('drag', function), potentially making images much more friendly to work with.
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
Copy link
Contributor Author

@Roland-J Roland-J left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some clarifying comments

addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
addons/libs/images.lua Show resolved Hide resolved
Sets the clicked var to nil on left mouse release
@Roland-J
Copy link
Contributor Author

Roland-J commented Jul 9, 2022

@z16 Thank you for the feedback! I am trying to implement the changes but need some clarifications. Please see my follow up questions. Thanks!

@Roland-J
Copy link
Contributor Author

Roland-J commented Aug 6, 2022

@z16 Sorry for the delay, but I've implemented much of what was mentioned, plus a lot more. It can be found and demo'd in a fully functional form here: (feel free to re-use this code)
https://github.com/Roland-J/SmartSkillup/blob/main/libs/sms_images.lua
https://github.com/Roland-J/SmartSkillup/blob/main/libs/sms_texts.lua

It's a lot of changes - trying to flesh out the event self-registration feature as much as possible to lower the barrier to UI development and encourage more UI development - so there may be too many conflicts to event attempt it in the PR, so I figured I'd first mention it before merging it in. I'll take my next action according to the feedback.

Summary:

  1. Added hover/unhover event. (Only fires if said event has been registered for said element)
  2. Added scroll up/down events. (Only fires if said event(s) have been registered ffor said element)
  3. Added scroll click event. (Only fires if said event has been registered for said element)
  4. Added right click/release event. (Only fires if said event has been registered for said element)
  5. Added right drag. (Only occurs if its new flag is enabled.)
  6. Added right drag event. (Only fires if said event has been registered for said element)
  7. Added scroll drag. (Only occurs if its new flag is enabled.)
  8. Added scroll drag event. (Only fires if said event has been registered for said element)
  9. Differentiated left drag from right drag. (Renamed vars & func and provided alias vars and funcs for legacy support)
  10. Finished the drag tolerance feature. (Defaults to 0, drags only start once tolerance is exceeded)
  11. Refined event sinking with event checking - keep going to first event-registered element - to allow events to sink through a irrelevant hitbox or similar item into the topmost relevant item.
  12. Added event returning - return out on first relevant element - to avoid allowing for both the intended (topmost) element and unintended (lower) elements to trigger events; only the intended (topmost) one will trigger.

I've added some event variables to them. You can see what I'm doing with those variables in https://github.com/Roland-J/SmartSkillup/blob/main/libs/sms_ui.lua

@Roland-J Roland-J changed the title Implement image:register_event for click and drag Implement Fully-Featured Event Self-Registration for Images & Texts Aug 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants