click drag drop mouseover mouseout all in an object

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Rob.m
Prole
Posts: 23
Joined: Mon Mar 11, 2013 1:57 am

click drag drop mouseover mouseout all in an object

Post by Rob.m »

Hi all and thanks everone for the help.

I have some rough code (I will clean it up) that has mouse events and draw in an object. The drawn object can be moved anywhere on the screen and the mouse event still work.

There is nothing more than main.lua so you can copy and paste it to try it.

The drop even is just a grid snap, click mouseover and mouseout are obvious.

One button is dragable dropable the other is not.

Still some bugs but I will get to them.

Any comments apreciated.

The object is at the end of the code. It will give an idea of the way this works.


Code: Select all

function love.load()
  objects = {}
  local button1 = newTextButtonToggle()
  local button2 = newTextButtonToggle()
  button1.x = 50
  button1.text = 'B1'
  button1.currenttext = 'B1'
  button2.x = 150
  button2.drag = nil
  button2.boxcolour = {0, 127, 0, 255}
  button2.altboxcolour = {0, 255, 0, 255}
  button2.currentboxcolour = {0, 127, 0, 255}
  objects = {button1, button2}
  mouse_dx = nil
  mouse_dy = nil
  mouse_drag = false
  mouse_ob = nil
end

function love.draw()
  local ob
  local mx = love.mouse.getX()
  local my = love.mouse.getY()
  for index = 1, #objects do
    ob = objects[index]
    if ob ~= mouse_ob then
      if ob.mouseovers then
        if ob.mouseisover then
          if not inZone(ob, mx, my) then -- mouseovers and mouseisover and mouse isn't actualy over
            ob.mouseisover = false
            if ob.mouseout then
              ob.mouseout(ob)
            end
          end
        else -- mouseovers and !mouseisover
          if inZone(ob, mx, my) then -- mouseovers and !mouseisover and mouse is actual over
            ob.mouseisover = true
            if ob.mouseover then
              ob.mouseover(ob)
            end
          end
        end
      end
      if ob.draw then
        ob.draw(ob)
      end
    end
  end
  if mouse_drag and mouse_ob.drag then
    mouse_ob.drag(mouse_ob)
  end
end


function love.update()
end


function love.mousepressed(x, y, button)
  if button == 'l' then
    mouse_dx = x
    mouse_dy = y
    local index
    local ob
    for index = 1, #objects do
      ob = objects[index]
      if ob.drag and inZone(ob, x, y) then
        mouse_drag = true
        mouse_ob = ob
        mouse_dx = ob.x - x
        mouse_dy = ob.y - y
      end
    end
  end
end

function love.mousereleased(x, y, button)
  if button == 'l' then
    if mouse_drag then
      if mouse_ob.drop then
        mouse_ob.drop(mouse_ob, x, y)
        mouse_drag = false
        mouse_ob = nil
      else
        mouse_drag = false
        mouse_ob = nil
      end
    end
    local index
    local ob
    for index = 1, #objects do
      ob = objects[index]
      if ob.click and inZone(ob, x, y) then 
        ob:click()
      end
    end
  end
end

function inZone(ob, x, y)
  if x > ob.x and x < ob.x + ob.width and y > ob.y and y < ob.y + ob.height then
    return true
  end
  return false
end

function newTextButtonToggle()
  return
    {
    type = 'TextButtonToggle',
    x = 0,
    y = 0,
    width = 64,
    height = 16,
    textyo = 1,
    toggled = false,
    textcolour = {192, 192, 192, 255},
    boxcolour = {127, 0, 0, 255},
    altboxcolour = {255, 0, 0, 255},
    currentboxcolour = {127, 0, 0, 255},
    text = 'text',
    alttext = 'ALT text',
    currenttext = 'text',
    mouseovers = true,
    mouseisover = false,
    click = 
      function (self)
        if self.toggled then
          self.currenttext = self.text
          self.toggled = false
        else
          self.currenttext = self.alttext
          self.toggled = true
        end
      end,
    draw = 
      function (self)
        love.graphics.setColor(self.textcolour)
        love.graphics.printf(self.currenttext, self.x, self.y + self.textyo, self.width, 'center')
        love.graphics.setColor(self.currentboxcolour)
        love.graphics.rectangle('line', self.x, self.y, self.width, self.height)
      end,
    drag = 
      function (self)
        self.x = love.mouse.getX() + mouse_dx
        self.y = love.mouse.getY() + mouse_dy
        self.draw(self)
      end,
    drop =
      function (self, x, y)
        self.x = math.floor(self.x / 16) * 16
        self.y = math.floor(self.y / 16) * 16
      end,
    mouseover = 
      function (self)
        self.currentboxcolour = self.altboxcolour
      end,
    mouseout = 
      function (self)
        self.currentboxcolour = self.boxcolour
      end
    }
  -- end of return object
end
Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests