Implementation
Pedro Gimeno edited this page 5 years ago

Copy Gspot.lua into your project directory, and include the module in the usual way : gui = require('Gspot')

Gspot.lua returns an instance with a constructor. To create more instances use mainmenu = gui()


IMPORTANT: LÖVE 11.x introduces a compatibility-breaking change, namely the range of colour components is 0-1 instead of 0-255. Gspöt is able to work with both ranges for its style definitions. If you want Gspöt to work with a 0-255 range no matter the version (e.g. if you're writing a program that you want to work with both LÖVE 0.10.x and 11.x), you can use this code in your main.lua:

gui = require('Gspot'):setComponentMax(255)

That only needs to be done at the top of main.lua; everywhere else you can just use:

gui = require('Gspot')

This modifies the internal class, therefore you can't mix code using the 0-255 range and code using the 0-1 range.


Normal usage

The following is a basic main.lua for use with LÖVE 0.10.x and LÖVE 11.x.

gui = require('Gspot')

love.load = function()
  local button = gui:button('Hello', {128, 128, 128, 16}) -- or {0.5, 0.5, 0.5, 0.063} for 11.x
  button.click = function(this) print('Hello!') end
end
love.update = function(dt)
  gui:update(dt)
end
love.draw = function()
  gui:draw()
end

love.keypressed = function(key, code, isrepeat)
  gui:keypress(key)
end
love.textinput = function(key)
  gui:textinput(key)
end
love.mousepressed = function(x, y, button)
  gui:mousepress(x, y, button)
end
love.mousereleased = function(x, y, button)
  gui:mouserelease(x, y, button)
end
love.wheelmoved = function(x, y)
  gui:mousewheel(x, y)
end

The following is a basic main.lua for use with LÖVE 0.9.x:

gui = require('Gspot')

love.load = function()
  local button = gui:button('Hello', {128, 128, 128, 16})
  button.click = function(this) print('Hello!') end
end
love.update = function(dt)
  gui:update(dt)
end
love.draw = function()
  gui:draw()
end

love.keypressed = function(key, isrepeat)
  gui:keypress(key)
end
love.textinput = function(key)
  gui:textinput(key)
end
love.mousepressed = function(x, y, button)
  gui:mousepress(x, y, button)
end
love.mousereleased = function(x, y, button)
  gui:mouserelease(x, y, button)
end

And this is a cross-version compatible version that works in 0.9.x to 11.x:

gui = require('Gspot'):setComponentMax(255)

love.load = function()
  local button = gui:button('Hello', {128, 128, 128, 16})
  button.click = function(this) print('Hello!') end
end
love.update = function(dt)
  gui:update(dt)
end
love.draw = function()
  gui:draw()
end

love.keypressed = function(key, code, isrepeat)
  gui:keypress(key)
end
love.textinput = function(key)
  gui:textinput(key)
end
love.mousepressed = function(x, y, button)
  gui:mousepress(x, y, button)
end
love.mousereleased = function(x, y, button)
  gui:mouserelease(x, y, button)
end
love.wheelmoved = function(x, y)
  gui:mousewheel(x, y)
end

The basic convention for constructing an element is:

Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent)

Some element constructors accept an additional parameter, as follows.

Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent, variable value)

The following exceptions adhere to convention, but accept different types as their 4th (6th) parameter.

  • text, typetext and button elements, which are constructed as follows.
  • Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent,
                  nil or bool autosize)
    

  • image and imgbutton elements, which are constructed as follows.
  • Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent,
                  string path or love.image img)
    

  • scrollbar elements, which are constructed as follows.
  • Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent,
                  nil or table or Gspot.scrollvalues values)
    

  • input elements are constructed with three optional extra parameters instead of one, as follows.
  • Gspot:element(string label, nil or table or Gspot.pos pos, nil or Gspot.element parent,
                  string value, nil or bool ispassword, nil or string passwordchar)
    


See Elements for a complete list of predefined element constructors.