How to correctly use love.mousepressed ?

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
User avatar
ErtYwek
Prole
Posts: 6
Joined: Sat Nov 03, 2018 3:59 pm
Contact:

How to correctly use love.mousepressed ?

Post by ErtYwek »

Hey! I have problem with function love.mousepressed(x,y,mousebutton,istouch). I'm trying to make a simple button library and as you can see below I've made something already. But unfortunately, I stuck on trying to let buttons change the color after mouse click until the mouse button is not released. Before clicking button already change color (like a mouseover).
Image
This is how i implement my buttons:

Code: Select all

ial_butt("-","rect","5","Help",topbar_txt_size,186,0,n,n,
      n,n,n,n,n,n,n,n)--TBM tree5
Full* code of library:

Code: Select all

ialdatatable={  }--[[ ["ial_butt_gC1"]=0, ["somedata"]=0 ]]
ialtabulator={  }
--ial_butt_run=0

do
  local ial_itemamount=0
function ial_butt(ial_bstyle, ial_bconfig, ial_bid, ial_btxt, ial_btxtsz, ial_bx,ial_by, ial_btx, ial_bty, ial_bwidth, ial_bheight,
  ial_bclr, ial_bclr2, ial_bclr3, ial_btclr, ial_btclr2, ial_btclr3)           --[[ial_butt]]
  mousex, mousey = love.mouse.getPosition( ) --[[doesn't work if outside of function ial_butt]]

  --ial_butt_run=ial_butt_run+1

  ial_window_hasmousefocus = love.window.hasMouseFocus( )
  ial_window_haskeyboardfocus = love.window.hasFocus( )
  ial_window_isvisible = love.window.isVisible( )--[[visible equals not minimized and/or hidden]]
  --print(mousex, mousey)--[[delete that later]]
  if ial_bstyle=="btest" then
    return print("ial_butt function btest")
  elseif ial_bstyle=="-" then                                                --[[basic theme]]
    if ial_bconfig=="rect" then                                      --[[basic RECTANGLE button]]
      do
        local ial_mouseover=false
        local ial_mouseclicked=false
      if ial_bwidth==nil or ial_bwidth==n then --[[button width  all the nils begin here]]

        ial_bxsize=string.len(ial_btxt) --[[THERE IS PROBLEM WITH EURO SYMBOL €]]
        if (ial_btxtsz==1) then

        elseif (ial_btxtsz==17) then
          ial_bwidth=(ial_bxsize*10)+3
        end
      end
      if ial_bheight==nil or ial_bheight==n then --[[button height]]
        if (ial_btxtsz==1) then

        elseif (ial_btxtsz==17) then
          ial_bheight=ial_btxtsz+3
        end
      end
      if ial_btx==nil or ial_btx==n then ial_btx=ial_bx+1 end
      if ial_bty==nil or ial_bty==n then ial_bty=ial_by-1 end
      if ial_btxtsz==nil or ial_btxtsz==n then ial_btxtsz=17 end
      if ial_bclr==nil or ial_bclr==n then ial_bclr="Grey50" end
      if ial_bclr2==nil or ial_bclr2==n then ial_bclr2="Grey45" end
      if ial_bclr3==nil or ial_bclr3==n then ial_bclr3="Grey10" end
      if ial_btclr==nil or ial_btclr==n then ial_btclr="Grey25" end
      if ial_btclr2==nil or ial_btclr2==n then ial_btclr2="Grey20" end
      if ial_btclr3==nil or ial_btclr3==n then ial_btclr3="black" end
                                                         --[[nils end here]]

      if ( mousex>=ial_bx and mousex<=(ial_bx+ial_bwidth) and mousey>=ial_by and mousey<=(ial_by+ial_bheight) ) then
        ial_mouseover=true
        ial_butt_cc=ial_bclr2
        ial_butt_txtcc=ial_btclr2
      else
        ial_mouseover=false
      end

      if ial_mouseover==true and ial_window_hasmousefocus==true and ial_window_haskeyboardfocus==true and ial_mouseclicked==false then
        ial_butt_cc=ial_bclr2
        ial_butt_txtcc=ial_btclr2
      elseif ial_mouseover==true and ial_mouseclicked==true then
        print("test")
        ial_butt_cc=ial_bclr3
        ial_butt_txtcc=ial_btclr3
      else
        ial_mouseover=false
        ial_butt_cc=ial_bclr
        ial_butt_txtcc=ial_btclr
      end

      if ( mousex>=ial_bx and mousex<=(ial_bx+ial_bwidth) and mousey>=ial_by and mousey<=(ial_by+ial_bheight) ) then
        function love.mousepressed(x,y,mousebutton,istouch)
          if ( mousex>=ial_bx and mousex<=(ial_bx+ial_bwidth) and mousey>=ial_by and mousey<=(ial_by+ial_bheight) ) then
            if ial_mouseover==true then
              ial_butt_cc=ial_bclr3
              ial_butt_txtcc=ial_btclr3
              ial_mouseclicked=true
              print(ial_bx)
            end
          end
        end
      end

      --ial_itemamount=ial_itemamount+1
      --ialtabulator[ial_itemamount]="iaitem"..ial_itemamount

      ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"]=love.graphics.getColor( )
      --ial_b_am=print(ial_butt_run)
      ial_b_gsc=love.graphics.setColor( ial_html_cn(ial_butt_cc,1) )
      ial_b_gr=love.graphics.rectangle("fill", ial_bx, ial_by, ial_bwidth, ial_bheight )--[[button]]
      ial_b_gsc2=love.graphics.setColor( ial_html_cn(ial_butt_txtcc,1) )
      ial_b_gnt=love.graphics.newText( ial_fnt("dj_sans",ial_btxtsz), ial_btxt )
      ial_b_dt=love.graphics.draw(ial_b_gnt,ial_btx,ial_bty)--[[text]]

      ial_b_gscend=love.graphics.setColor( ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"] )
      ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"]=nil,nil,nil,nil
      end
      return ial_b_gnt,ial_b_gsc,ial_b_gr,ial_b_gsc2,ial_b_dt
    elseif ial_bconfig=="cc" then                                      --[[basic CIRCLE button]]
      do
        local ial_mouseover=false
        local ial_mouseclicked=false
        local ial_mousereleased=true
        ial_testvar_glob="q"
      if ial_bwidth==nil or ial_bwidth==n then --[[button width  all the nils begin here]]
        ial_bxsize=string.len(ial_btxt) --[[THERE IS PROBLEM WITH EURO SYMBOL €]]
        if (ial_btxtsz==1) then

        elseif (ial_btxtsz==17) then
          ial_bwidth=(ial_bxsize*5)+1
        end
      end
      if ial_bheight==nil or ial_bheight==n then ial_bheight=9999--[[angles]] end
      if ial_btx==nil or ial_btx==n then ial_btx=ial_bx-30 end
      if ial_bty==nil or ial_bty==n then ial_bty=ial_by-12 end
      if ial_btxtsz==nil or ial_btxtsz==n then ial_btxtsz=17 end
      if ial_bclr==nil or ial_bclr==n then ial_bclr="Grey50" end
      if ial_btclr==nil or ial_btclr==n then ial_btclr="Grey25" end
      if ial_bclr2==nil or ial_bclr2==n then ial_bclr2="Grey45" end--[[wiem czemu ale nie wiem dlaczego losowy przycisk a nie ostatni SPRAWDZIĆ!!]]
      if ial_bclr3==nil or ial_bclr3==n then ial_bclr3="Grey10" end
      if ial_btclr2==nil or ial_btclr2==n then ial_btclr2="Grey20" end
      if ial_btclr3==nil or ial_btclr3==n then ial_btclr3="Black" end
                                                         --[[nils end here]]

      if ial_distanceBe(ial_bx, ial_by, love.mouse.getX(), love.mouse.getY()) < ial_bwidth then
        ial_mouseover=true
        --[[if ial_mouseover==true and ial_window_hasmousefocus==true and ial_window_haskeyboardfocus==true and ial_mouseclicked==false then
          ial_butt_cc=ial_bclr2
          ial_butt_txtcc=ial_btclr2
        end
        if ial_mouseover==true and ial_window_hasmousefocus==true and ial_window_haskeyboardfocus==true and ial_mouseclicked==true then
          ial_butt_cc=ial_bclr3
          ial_butt_txtcc=ial_btclr3
        end]]
      else
        ial_mouseover=false
        --[[ial_butt_cc=ial_bclr
        ial_butt_txtcc=ial_btclr]]
      end

      if ial_distanceBe(ial_bx,ial_by,love.mouse.getX(),love.mouse.getY()) < ial_bwidth then
        function love.mousepressed(x,y,mousebutton,istouch)
          if ial_distanceBe(ial_bx,ial_by,love.mouse.getX(),love.mouse.getY()) < ial_bwidth then
            if ial_mouseover==true then
              ial_mouseclicked=true
              ial_mousereleased=false
              ial_testvar_glob="w"
              ial_butt_cc=ial_bclr3
              ial_butt_txtcc=ial_btclr3
              print(ial_bx)
              print(ial_testvar_glob)
              print(ial_mouseclicked,ial_mousereleased)
              print(ial_butt_cc,ial_butt_txtcc)
            end
          end
        end
      end

      function love.mousereleased(mouserx, mousery, button)
        ial_mouseclicked=false
        ial_mousereleased=true


        print(ial_mousereleased)
      end

      if ial_mouseover==true and ial_window_hasmousefocus==true and ial_window_haskeyboardfocus==true and ial_mouseclicked==false then
        ial_butt_cc=ial_bclr2
        ial_butt_txtcc=ial_btclr2
      end

      if ial_mouseover==false and ial_mouseclicked==false then
        ial_butt_cc=ial_bclr
        ial_butt_txtcc=ial_btclr
      end

      -- put there data about table
      ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"]=love.graphics.getColor( )
      --ial_b_am=print(ial_butt_run)
      ial_b_gsc=love.graphics.setColor( ial_html_cn(ial_butt_cc,1) )
      ial_b_gr=love.graphics.circle("fill", ial_bx, ial_by, ial_bwidth, ial_bheight)--[[button]]
      ial_b_gsc2=love.graphics.setColor( ial_html_cn(ial_butt_txtcc,1) )
      ial_b_gnt=love.graphics.newText( ial_fnt("dj_sans",ial_btxtsz), ial_btxt )
      ial_b_dt=love.graphics.draw(ial_b_gnt,ial_btx,ial_bty)--[[text]]

      ial_b_gscend=love.graphics.setColor( ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"] )
      ialdatatable["ial_butt_gC1"],ialdatatable["ial_butt_gC2"],ialdatatable["ial_butt_gC3"],ialdatatable["ial_butt_gC4"]=nil,nil,nil,nil
      end
      return ial_b_gnt,ial_b_gsc,ial_b_gr,ial_b_gsc2,ial_b_dt
    end

  elseif ial_bstyle=="bmard" then
    return print("ial_butt function bmard")
  end
end


function ial_distanceBe(ial_x1,ial_y1,ial_x2,ial_y2)
  return math.sqrt((ial_y2-ial_y1)^2+(ial_x2-ial_x1)^2)
end

function ial_fnt(f_fnt_name,f_fnt_size)                                                                 --[[ial_fnt]]
  if f_fnt_name=="dj_sans" then--[[monospaced fonts under SIL Open Font License 1.1]]
    return love.graphics.newFont("/libraries/InterActiveLibrary/dejavu_sans_mono_book.ttf", f_fnt_size)
  elseif f_fnt_name=="GNU_unifont" then
    return love.graphics.newFont("/libraries/InterActiveLibrary/GNU_unifont_13.0.02.ttf", f_fnt_size)
  end
end

end --[["do" before ial_butt function end]]

* below there is a function providing colors but it works as I want and I'm sure it doesn't cause any problems.
Tell me if you need .lua files to help me.
/EDIT Thank you, but I explored Uare scripts and I understand completely nothing from them, they are too professional for me. Even more creepy is that there is no love.mousepressed nor mousereleased in his library so how it can work?
Last edited by ErtYwek on Wed May 27, 2020 12:36 pm, edited 1 time in total.
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: How to correctly use love.mousepressed ?

Post by hoistbypetard »

It might help you to take a look at Uare. One of the buttons on the button sample there has exactly the behavior you want, and the whole thing is short enough that you can likely wrap your mind around it quickly and see what you're doing differently.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 41 guests