click on topmost ui element?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

click on topmost ui element?

Post by Shadowblitz16 »

does anybody know how I can click on the topmost ui element only?
currently I have this button but when I click on it when another button is behind it both buttons get pressed

Code: Select all


buttons = {}

function buttons:Create(text, x, y, w, h, callback)
    local inst    = {}
    inst.state    = 0;
    inst.text     = text;
    inst.x        = x;
    inst.y        = y;
    inst.w        = w;
    inst.h        = h;
    inst.callback = callback or nil;

    table.insert(buttons, inst);
    return inst;
end

function buttons:Update()
    for i,v in ipairs(buttons) do 
        v.state = 0;
        if love.mouse.getX() >= v.x     and
           love.mouse.getY() >= v.y     and
           love.mouse.getX() <= v.x+v.w and
           love.mouse.getY() <= v.y+v.h and 
           love.mouse.isDown(1) then
              v.state = 1;
              if v.callback ~= nil then 
                v.callback() 
            end
        end
    end
end

function buttons:Render()
    for i,v in ipairs(buttons) do 

        if v.state == 0 then
            love.graphics.setColor(255,255,255)
            love.graphics.rectangle("fill", v.x+0, v.y+0, v.w-0, v.h-0)

            love.graphics.setColor(48,48,48)
            love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-1, v.h-1)

            love.graphics.setColor(96,96,96)
            love.graphics.rectangle("fill", v.x+2, v.y+2, v.w-2, v.h-2)

            love.graphics.setColor(192,192,192)
            love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-2, v.h-2)
        else
            love.graphics.setColor(48,48,48)
            love.graphics.rectangle("fill", v.x+0, v.y+0, v.w-0, v.h-0)

            love.graphics.setColor(96,96,96)
            love.graphics.rectangle("fill", v.x+1, v.y+1, v.w-2, v.h-2)

            love.graphics.setColor(192,192,192)
            love.graphics.rectangle("fill", v.x+2, v.y+2, v.w-4, v.h-4)
        end

        love.graphics.setColor(0,0,0)
        love.graphics.printf(v.text, v.x, v.y+v.h/4, v.w-2, "center")
    end
end
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: click on topmost ui element?

Post by zorg »

Code: Select all

function buttons:Update()
    for i,v in ipairs(buttons) do 
        v.state = 0;
        if love.mouse.getX() >= v.x     and
           love.mouse.getY() >= v.y     and
           love.mouse.getX() <= v.x+v.w and
           love.mouse.getY() <= v.y+v.h and 
           love.mouse.isDown(1) then
              v.state = 1;
              if v.callback ~= nil then 
                v.callback() 
              end
              return -- don't trigger any other element, if one already was.
          end
    end
end
On the other hand, querying each frame might not be the most performant of things; löve gives you callback functions for a reason; love.mousepressed is exactly for this kind of thing.

Also, your code doesn't really do any z-reordering either (e.g. with windows, clicking on one's toolbar or something would make that be the topmost one) so the above solution will always depend on the button creation order, in which one gets triggered.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: click on topmost ui element?

Post by Shadowblitz16 »

Edit: this doesn't work it seems to work on the bottom most
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: click on topmost ui element?

Post by zorg »

As i said...
easy solution: Iterate in reverse.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: click on topmost ui element?

Post by Shadowblitz16 »

oh ok sorry thankyou
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 201 guests