MCJack123 / PrimeUI

A collection of UI component primitives for ComputerCraft.
26 stars 7 forks source link

Fix click and scroll for moving elements #14

Closed ascpial closed 1 month ago

ascpial commented 1 month ago

When clickable elements are movable (for example when in a scrollbox), their "hitbox" don't move when the element move. Moreover, when the origin of the element is out of the screen, it becomes impossible to click them.

Here is a minimal demo of the issue:

local PrimeUI = require("init")

PrimeUI.clear()

local function draw_box(display,x,y,w,h,color)
    local color    = ("%x"):format(math.log(color,2))
    local line_str = color:rep(w)

    for line_y=y,y+h - 1 do
        display.setCursorPos(x,line_y)
        display.blit(line_str,line_str,line_str)
    end
end

local win = term.current()

local w, h = win.getSize()

local scroll = PrimeUI.scrollBox(win, 1, math.floor(h/2), w, math.floor(h/2-1), h, true)

PrimeUI.clickRegion(scroll, 1, 1, w, math.floor(h/2), function ()
  draw_box(scroll, 1, 1, w, math.floor(h/2), colors.gray)
  sleep(0.5)
  draw_box(scroll, 1, 1, w, math.floor(h/2), colors.lightGray)
end)

draw_box(scroll, 1, 1, w, math.floor(h/2), colors.lightGray)

PrimeUI.run()

This is opinionated, it increases the size of util.lua and idk if you will like it the way I did it.

I also fixed a minor issue in init.lua.

Documentation is missing, this is why this PR is marked as draft.

MCJack123 commented 1 month ago

Fixing this issue requires some more complex logic that's out of scope for PrimeUI. One of the design goals is for the code to be clearly understandable with little to no "hidden" logic, and another is to avoid any elements changing the behavior of others, which includes creating a view hierarchy (this is a primitive toolbox, not a full UI library!). I already hate that getWindowPos has to exist, and I wish I could remove it entirely, but any sort of nesting would break otherwise; so expanding it any more just doesn't sit right with me. Feel free to keep it for your own use (and even share it with others!), but I don't plan on merging this.