This commit is contained in:
Paul Liverman III
2018-04-06 15:30:00 -07:00
parent f0a9da387a
commit a45aa2624e
101 changed files with 12392 additions and 77 deletions

8
LIB/init.lua Normal file
View File

@@ -0,0 +1,8 @@
local path = ...
if path:sub(-4) == "init" then
path = path:sub(1, -6)
if not (path) then
path = "."
end
end
return require(tostring(path) .. "/slab")

7
LIB/init.moon Normal file
View File

@@ -0,0 +1,7 @@
path = ...
if path\sub(-4) == "init"
path = path\sub 1, -6
path = "." unless path
return require "#{path}/slab"

180
LIB/slab.lua Normal file
View File

@@ -0,0 +1,180 @@
local path = (...):sub(1, -(("slab"):len() + 2))
local graphics
graphics = love.graphics
local slab = {
_VERSION = "0.1.0",
_DESCRIPTION = "GUI library for LÖVE.",
_URL = nil,
_LICENSE = "The MIT License (MIT)",
_AUTHOR = "Paul Liverman III"
}
slab.ui = {
x = 0,
y = 0,
w = graphics.getWidth(),
h = graphics.getHeight()
}
slab.hovered = false
slab.focused = false
slab.types = {
"menu",
"spinner",
"text"
}
slab.generic = require(tostring(path) .. "/types/generic")
local _list_0 = slab.types
for _index_0 = 1, #_list_0 do
local t = _list_0[_index_0]
slab[t] = require(tostring(path) .. "/types/" .. tostring(t))
end
slab.make = function(element, parent)
if element == nil then
element = { }
end
if parent == nil then
parent = slab.ui
end
local _list_1 = slab.types
for _index_0 = 1, #_list_1 do
local t = _list_1[_index_0]
if element[t] then
return slab[t](element, parent)
end
end
return slab.generic(element, parent)
end
slab.update = function(dt, element)
if element == nil then
element = slab.ui
end
if element.update then
element:update(dt)
end
for _index_0 = 1, #element do
local child = element[_index_0]
slab.update(dt, child)
end
end
slab.draw = function(element)
if element == nil then
element = slab.ui
end
if element == slab.ui then
graphics.push("all")
graphics.origin()
end
local drawChildren
if element.visible and element.draw then
drawChildren = element:draw()
end
if drawChildren ~= false then
for _index_0 = 1, #element do
local child = element[_index_0]
slab.draw(child)
end
end
if element == slab.ui then
return graphics.pop()
end
end
slab.mousemoved = function(x, y, dx, dy, element)
if element == nil then
element = slab.ui
end
local previous
if element == slab.ui then
previous = slab.hovered
end
if element.visible and element.hoverable and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h then
slab.hovered = element
for _index_0 = 1, #element do
local child = element[_index_0]
slab.mousemoved(x, y, dx, dy, child)
end
end
if element == slab.ui then
if slab.hovered ~= previous then
if previous and previous.hovered then
previous:hovered(false)
end
if slab.hovered.hovered then
slab.hovered:hovered(true)
end
end
if slab.focused and slab.focused.mousemoved then
return slab.focused:mousemoved(x - slab.focused.x, y - slab.focused.y, dx, dy)
end
end
end
slab.mousepressed = function(x, y, btn, element)
if element == nil then
element = screen.ui
end
if element == screen.ui then
if btn == "wd" then
return slab.wheelmoved(0, -1)
elseif btn == "wu" then
return slab.wheelmoved(0, 1)
end
end
local handled = false
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h then
for i = #element, 1, -1 do
handled = slab.mousepressed(x, y, btn, element[i])
if handled ~= false then
return handled
end
end
if element.mousepressed then
handled = element:mousepressed(btn, x, y)
end
end
return handled
end
slab.mousereleased = function(x, y, btn, element)
local clickHandled, mousereleaseHandled = false, false
if element then
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h then
for i = #element, 1, -1 do
clickHandled, mousereleaseHandled = slab.mousereleased(x, y, btn, element[i])
if clickHandled ~= false or mousereleaseHandled ~= false then
return clickHandled, mousereleaseHandled
end
end
if element.clicked then
clickHandled = element:clicked(btn, x - element.x, y - element.y)
end
if element.mousereleased then
mousereleaseHandled = element:mousereleased(btn, x - element.x, y - element.y)
end
if clickHandled then
slab.focused = element
end
end
else
do
element = slab.focused
if element then
if element.mousereleased then
mousereleaseHandled = element:mousereleased(btn, x - element.x, y - element.y)
end
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h then
if element.clicked then
clickHandled = element:clicked(btn, x - element.x, y - element.y)
end
end
if clickHandled ~= false or mousereleaseHandled ~= false then
return clickHandled, mousereleaseHandled
end
return slab.mousereleased(x, y, btn, slab.ui)
end
end
end
end
slab.wheelmoved = function(x, y)
if slab.hovered and slab.hovered.wheelmoved then
return slab.hovered:wheelmoved(x, y)
end
return false
end
return slab

108
LIB/slab.moon Normal file
View File

@@ -0,0 +1,108 @@
path = (...)\sub 1, -("slab"\len! + 2)
import graphics from love
slab = {
_VERSION: "0.1.0"
_DESCRIPTION: "GUI library for LÖVE."
_URL: nil
_LICENSE: "The MIT License (MIT)"
_AUTHOR: "Paul Liverman III"
}
slab.ui = { x: 0, y: 0, w: graphics.getWidth!, h: graphics.getHeight! }
slab.hovered = false
slab.focused = false
slab.types = { "menu", "spinner", "text" }
slab.generic = require "#{path}/types/generic"
for t in *slab.types
slab[t] = require "#{path}/types/#{t}"
-- slab[t]\load slab if slab[t].load
slab.make = (element={}, parent=slab.ui) ->
for t in *slab.types
return slab[t](element, parent) if element[t]
return slab.generic element, parent
slab.update = (dt, element=slab.ui) ->
element\update dt if element.update
for child in *element
slab.update dt, child
slab.draw = (element=slab.ui) ->
if element == slab.ui
graphics.push "all"
graphics.origin!
local drawChildren
drawChildren = element\draw! if element.visible and element.draw
if drawChildren != false
for child in *element
slab.draw child
if element == slab.ui
graphics.pop!
slab.mousemoved = (x, y, dx, dy, element=slab.ui) ->
local previous
if element == slab.ui
previous = slab.hovered
if element.visible and element.hoverable and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h
slab.hovered = element
for child in *element
slab.mousemoved x, y, dx, dy, child
if element == slab.ui
if slab.hovered != previous
previous\hovered false if previous and previous.hovered
slab.hovered\hovered true if slab.hovered.hovered
if slab.focused and slab.focused.mousemoved
return slab.focused\mousemoved x - slab.focused.x, y - slab.focused.y, dx, dy
slab.mousepressed = (x, y, btn, element=screen.ui) ->
if element == screen.ui
if btn == "wd"
return slab.wheelmoved 0, -1
elseif btn == "wu"
return slab.wheelmoved 0, 1
handled = false
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h
for i = #element, 1, -1
handled = slab.mousepressed x, y, btn, element[i]
return handled if handled != false
if element.mousepressed
handled = element\mousepressed btn, x, y
return handled
slab.mousereleased = (x, y, btn, element) ->
clickHandled, mousereleaseHandled = false, false
if element
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h
for i = #element, 1, -1
clickHandled, mousereleaseHandled = slab.mousereleased x, y, btn, element[i]
if clickHandled != false or mousereleaseHandled != false
return clickHandled, mousereleaseHandled
clickHandled = element\clicked btn, x - element.x, y - element.y if element.clicked
mousereleaseHandled = element\mousereleased btn, x - element.x, y - element.y if element.mousereleased
slab.focused = element if clickHandled
elseif element = slab.focused
mousereleaseHandled = element\mousereleased btn, x - element.x, y - element.y if element.mousereleased
if element.visible and x >= element.x and x <= element.x + element.w and y >= element.y and y <= element.y + element.h
clickHandled = element\clicked btn, x - element.x, y - element.y if element.clicked
if clickHandled != false or mousereleaseHandled != false
return clickHandled, mousereleaseHandled
slab.mousereleased x, y, btn, slab.ui
slab.wheelmoved = (x, y) ->
return slab.hovered\wheelmoved x, y if slab.hovered and slab.hovered.wheelmoved
return false
return slab

122
LIB/types/generic.lua Normal file
View File

@@ -0,0 +1,122 @@
local path = (...):sub(1, -(("generic"):len() + 2))
local floor
floor = math.floor
local generic
do
local _class_0
local _base_0 = {
draw = function(self)
graphics.setColor(self.color)
if self.background then
return graphics.draw(self.background, self.x, self.y, 0, self.w / self.backgroundWidth, self.h / self.backgroundHeight)
else
return graphics.rectangle("fill", self.x, self.y, self.w, self.h)
end
end,
_align = function(self)
local _exp_0 = self.align
if "top-left" == _exp_0 or "left" == _exp_0 or "bottom-left" == _exp_0 then
self.x = self.parent.x
elseif "top" == _exp_0 or "center" == _exp_0 or "bottom" == _exp_0 then
self.x = self.parent.x + self.parent.w / 2 - self.w / 2
elseif "top-right" == _exp_0 or "right" == _exp_0 or "bottom-right" == _exp_0 then
self.x = self.parent.x + self.parent.w - self.w
end
local _exp_1 = self.align
if "top-left" == _exp_1 or "top" == _exp_1 or "top-right" == _exp_1 then
self.y = self.parent.y
elseif "left" == _exp_1 or "center" == _exp_1 or "right" == _exp_1 then
self.y = self.parent.y + self.parent.h / 2 - self.h / 2
elseif "bottom-left" == _exp_1 or "bottom" == _exp_1 or "bottom-right" == _exp_1 then
self.y = self.parent.y + self.parent.h - self.h
end
for _index_0 = 1, #self do
local child = self[_index_0]
child:_align()
end
end
}
_base_0.__index = _base_0
_class_0 = setmetatable({
__init = function(self, element, parent)
if element == nil then
element = { }
end
if not (parent) then
error("No parent element!")
end
self.parent = parent
for k, v in pairs(element) do
self[k] = v
end
if self.visible == nil then
self.visible = true
end
if self.hovered then
self.hoverable = true
end
if self.hoverable == nil then
self.hoverable = false
end
if self.width then
if self.width <= 1 then
self.w = floor(parent.w * self.width)
else
self.w = floor(self.width)
end
else
self.width = 0
self.w = 0
end
if self.height then
if self.height <= 1 then
self.h = floor(parent.h * self.height)
else
self.h = floor(self.height)
end
else
self.height = 0
self.h = 0
end
if "table" == type(self.background) then
if not (self.color) then
self.color = self.background
end
self.background = nil
else
if not (self.color) then
self.color = {
255,
255,
255,
255
}
end
end
if self.background then
self.backgroundWidth = self.background:getWidth()
self.backgroundHeight = self.background:getHeight()
self.aspectRatio = self.backgroundWidth / self.backgroundHeight
end
if #self > 0 then
local slab = require(tostring(path:sub(1, -7)) .. "/slab")
for i = 1, #self do
self[i] = slab.make(self[i], self)
end
end
return self:_align()
end,
__base = _base_0,
__name = "generic"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
generic = _class_0
return _class_0
end

88
LIB/types/generic.moon Normal file
View File

@@ -0,0 +1,88 @@
path = (...)\sub 1, -("generic"\len! + 2)
import floor from math
-- local slab
class generic
-- @load: (lib) =>
-- print "LOAD", self, lib
-- slab = lib
new: (element={}, parent) =>
error "No parent element!" unless parent
@parent = parent
for k,v in pairs element
@[k] = v
@visible = true if @visible == nil
@hoverable = true if @hovered
@hoverable = false if @hoverable == nil
if @width
if @width <= 1
@w = floor parent.w * @width
else
@w = floor(@width)
else
@width = 0
@w = 0
if @height
if @height <= 1
@h = floor parent.h * @height
else
@h = floor(@height)
else
@height = 0
@h = 0
if "table" == type @background
@color = @background unless @color
@background = nil
else
@color = {255, 255, 255, 255} unless @color
if @background
@backgroundWidth = @background\getWidth!
@backgroundHeight = @background\getHeight!
@aspectRatio = @backgroundWidth / @backgroundHeight
if #@ > 0
slab = require "#{path\sub 1, -7}/slab"
for i = 1, #@
@[i] = slab.make(@[i], @)
@_align!
draw: =>
graphics.setColor @color
if @background
graphics.draw @background, @x, @y, 0, @w / @backgroundWidth, @h / @backgroundHeight
else
graphics.rectangle "fill", @x, @y, @w, @h
_align: =>
switch @align
when "top-left", "left", "bottom-left"
-- align left
@x = @parent.x
when "top", "center", "bottom"
-- align center
@x = @parent.x + @parent.w / 2 - @w / 2
when "top-right", "right", "bottom-right"
-- align right
@x = @parent.x + @parent.w - @w
switch @align
when "top-left", "top", "top-right"
-- align top
@y = @parent.y
when "left", "center", "right"
-- align center
@y = @parent.y + @parent.h / 2 - @h / 2
when "bottom-left", "bottom", "bottom-right"
-- align bottom
@y = @parent.y + @parent.h - @h
for child in *@
child\_align!

72
LIB/types/menu.lua Normal file
View File

@@ -0,0 +1,72 @@
local path = (...):sub(1, -(("menu"):len() + 2))
local generic = require(tostring(path) .. "/generic")
local insert
insert = table.insert
local menu
do
local _class_0
local _parent_0 = generic
local _base_0 = { }
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, element, parent)
if element == nil then
element = { }
end
_class_0.__parent.__init(self, element, parent)
for _index_0 = 1, #self do
local child = self[_index_0]
if self.menu.width then
if not (child.width) then
child.width = self.menu.width
end
end
if self.menu.height then
if not (child.height) then
child.height = self.menu.height
end
end
if self.menu.align then
if not (child.align) then
child.align = self.menu.align
end
end
end
if #self.menu > 0 then
local slab = require(tostring(path:sub(1, -7)) .. "/slab")
local _list_0 = self.menu
for _index_0 = 1, #_list_0 do
local child = _list_0[_index_0]
insert(self, slab.make(child, self))
end
end
end,
__base = _base_0,
__name = "menu",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
menu = _class_0
return _class_0
end

21
LIB/types/menu.moon Normal file
View File

@@ -0,0 +1,21 @@
path = (...)\sub 1, -("menu"\len! + 2)
generic = require "#{path}/generic"
import insert from table
class menu extends generic
new: (element={}, parent) =>
super element, parent
for child in *@
if @menu.width
child.width = @menu.width unless child.width
if @menu.height
child.height = @menu.height unless child.height
if @menu.align
child.align = @menu.align unless child.align
if #@menu > 0
slab = require "#{path\sub 1, -7}/slab"
for child in *@menu
insert(@, slab.make child, @)

61
LIB/types/spinner.lua Normal file
View File

@@ -0,0 +1,61 @@
local path = (...):sub(1, -(("spinner"):len() + 2))
local generic = require(tostring(path) .. "/generic")
local graphics
graphics = love.graphics
local pi, min
do
local _obj_0 = math
pi, min = _obj_0.pi, _obj_0.min
end
local tau = pi * 2
local half_pi = pi / 2
local spinner
do
local _class_0
local _parent_0 = generic
local _base_0 = {
draw = function(self)
return graphics.arc("line", self.x + self.w / 2, self.y + self.h / 2, min(self.w, self.h), self.offset, self.offset + self.value * tau)
end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, element, parent)
if element == nil then
element = { }
end
_class_0.__parent.__init(self, element, parent)
if not (self.offset) then
self.offset = 0
end
self.value = 0.1
end,
__base = _base_0,
__name = "spinner",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
spinner = _class_0
return _class_0
end

21
LIB/types/spinner.moon Normal file
View File

@@ -0,0 +1,21 @@
path = (...)\sub 1, -("spinner"\len! + 2)
generic = require "#{path}/generic"
import graphics from love
import pi, min from math
tau = pi * 2
half_pi = pi / 2
class spinner extends generic
new: (element={}, parent) =>
super element, parent
-- @offset = -half_pi
@offset = 0 unless @offset
@value = 0.1 -- temporary
-- slab = require "#{path\sub 1, -7}/slab"
draw: =>
-- TODO should be setting color!
graphics.arc "line", @x + @w / 2, @y + @h / 2, min(@w, @h), @offset, @offset + @value * tau

48
LIB/types/text.lua Normal file
View File

@@ -0,0 +1,48 @@
local path = (...):sub(1, -(("text"):len() + 2))
local generic = require(tostring(path) .. "/generic")
local graphics
graphics = love.graphics
local text
do
local _class_0
local _parent_0 = generic
local _base_0 = {
draw = function(self) end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, element, parent)
if element == nil then
element = { }
end
return _class_0.__parent.__init(self, element, parent)
end,
__base = _base_0,
__name = "text",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
text = _class_0
return _class_0
end

15
LIB/types/text.moon Normal file
View File

@@ -0,0 +1,15 @@
path = (...)\sub 1, -("text"\len! + 2)
generic = require "#{path}/generic"
import graphics from love
class text extends generic
new: (element={}, parent) =>
super element, parent
-- @font = graphics.newFont 14
-- slab = require "#{path\sub 1, -7}/slab"
draw: =>
-- TODO