2016-08-21 05:29:16 +00:00
|
|
|
--- A generic element every element must inherit from.
|
2016-08-21 07:49:16 +00:00
|
|
|
--- @classmod element
|
2016-08-21 05:29:16 +00:00
|
|
|
--- @copyright Paul Liverman III (2016)
|
|
|
|
--- @license The MIT License (MIT)
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
import graphics from love
|
2016-11-25 03:37:53 +00:00
|
|
|
import floor from math
|
2016-10-31 05:19:24 +00:00
|
|
|
|
2016-08-21 05:29:16 +00:00
|
|
|
class element
|
2016-08-23 01:19:58 +00:00
|
|
|
--- Constructor expects nothing, or a data table describing it.
|
2016-09-08 04:42:19 +00:00
|
|
|
--- @tparam ?Element|false parent The parent element.
|
|
|
|
--- @tparam table data[opt] The data (state) for this element.
|
|
|
|
--- @treturn element self
|
2016-08-23 01:19:58 +00:00
|
|
|
new: (@parent, @data={}) =>
|
2016-11-25 03:37:53 +00:00
|
|
|
if type(@data) != "table"
|
2016-09-08 03:53:22 +00:00
|
|
|
@data = {}
|
2016-08-21 05:29:16 +00:00
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
@data.parent = false unless @data.parent
|
2016-09-08 04:42:19 +00:00
|
|
|
@data.child = {} unless @data.child
|
|
|
|
@data.x = 0 unless @data.x
|
|
|
|
@data.y = 0 unless @data.y
|
|
|
|
@data.w = 0 unless @data.w
|
|
|
|
@data.h = 0 unless @data.h
|
|
|
|
@data.update = false if @data.update == nil
|
|
|
|
@data.draw = true if @data.draw == nil
|
2016-10-31 06:40:40 +00:00
|
|
|
@data.type = "element" unless @data.type
|
2016-11-25 03:37:53 +00:00
|
|
|
@data.align = true if (@data.align == nil) and @parent
|
|
|
|
@data.vertical = "top" unless @data.vertical
|
|
|
|
@data.horizontal = "left" unless @data.horizontal
|
2016-09-08 04:42:19 +00:00
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
@child = {}
|
|
|
|
|
2016-11-25 03:37:53 +00:00
|
|
|
@align!
|
|
|
|
|
2016-10-31 05:19:24 +00:00
|
|
|
--- Slightly modified from pop.debugDraw
|
|
|
|
--- @see pop.debugDraw
|
|
|
|
debugDraw: =>
|
|
|
|
graphics.setLineWidth 1
|
|
|
|
graphics.setColor 0, 20, 0, 100
|
|
|
|
graphics.rectangle "fill", @data.x, @data.y, @data.w, @data.h
|
|
|
|
graphics.setColor 150, 255, 150, 150
|
|
|
|
graphics.rectangle "line", @data.x, @data.y, @data.w, @data.h
|
|
|
|
graphics.setColor 200, 255, 200, 255
|
|
|
|
graphics.print "e", @data.x, @data.y
|
|
|
|
|
2017-01-10 20:57:02 +00:00
|
|
|
return @
|
|
|
|
|
2016-11-25 03:37:53 +00:00
|
|
|
--- @todo doc me
|
|
|
|
align: (horizontal, vertical, toPixel=true) =>
|
|
|
|
unless @data.align return false
|
|
|
|
|
|
|
|
@data.horizontal = horizontal if horizontal
|
|
|
|
@data.vertical = vertical if vertical
|
|
|
|
|
|
|
|
@data.x = @parent.data.x
|
|
|
|
@data.y = @parent.data.y
|
|
|
|
|
|
|
|
switch @data.horizontal
|
|
|
|
when "center"
|
|
|
|
@data.x += (@parent.data.w - @data.w) / 2
|
|
|
|
when "right"
|
|
|
|
@data.x += @parent.data.w - @data.w
|
|
|
|
|
|
|
|
switch @data.vertical
|
|
|
|
when "center"
|
|
|
|
@data.y += (@parent.data.h - @data.h) / 2
|
2016-11-25 05:32:04 +00:00
|
|
|
when "bottom"
|
2016-11-25 03:37:53 +00:00
|
|
|
@data.y += @parent.data.h - @data.h
|
|
|
|
|
|
|
|
if toPixel
|
|
|
|
@data.x = floor @data.x
|
|
|
|
@data.y = floor @data.y
|
|
|
|
|
|
|
|
return @
|
|
|
|
|
2017-01-10 20:57:02 +00:00
|
|
|
--- Sets an element's width/height. Fixes alignment if needed.
|
2016-09-08 04:42:19 +00:00
|
|
|
--- @tparam integer w[opt] Width.
|
|
|
|
--- @tparam integer h[opt] Height.
|
|
|
|
--- @treturn element self
|
|
|
|
setSize: (w, h) =>
|
|
|
|
if w
|
|
|
|
@data.w = w
|
|
|
|
if h
|
|
|
|
@data.h = h
|
|
|
|
|
2017-01-10 20:57:02 +00:00
|
|
|
@align!
|
|
|
|
|
2016-09-08 04:42:19 +00:00
|
|
|
return @
|
|
|
|
|
|
|
|
--- Returns an element's width and height.
|
|
|
|
--- @treturn integer Width.
|
|
|
|
--- @treturn integer Height.
|
|
|
|
getSize: =>
|
|
|
|
return @data.w, @data.h
|
|
|
|
|
|
|
|
--- Sets an element's width.
|
|
|
|
--- @tparam integer w Width.
|
|
|
|
--- @treturn element self
|
|
|
|
setWidth: (w) =>
|
|
|
|
@data.w = w
|
|
|
|
return @
|
|
|
|
|
|
|
|
--- Returns an element's width.
|
|
|
|
--- @treturn integer Width.
|
|
|
|
getWidth: =>
|
|
|
|
return @data.w
|
|
|
|
|
|
|
|
--- Sets an element's height.
|
|
|
|
--- @tparam integer h Height.
|
|
|
|
--- @treturn element self
|
|
|
|
setHeight: (h) =>
|
|
|
|
@data.h = h
|
|
|
|
return @
|
|
|
|
|
|
|
|
--- Returns an element's height.
|
|
|
|
--- @treturn integer Height.
|
|
|
|
getHeight: =>
|
|
|
|
return @data.h
|