Skip to content

Commit

Permalink
Update to v0.6.0 + add micro version
Browse files Browse the repository at this point in the history
- removed attribute center 
- added attribure align as replace for center
- icon.size renamed to icon.extent and added option to define size of sprite (number of tiles) in both axis
- fixed bug in icon positioning
- added micro version
- renamed some attributes for the purpose of to make them unique in the 2-3 letter shortcut form (micro version).
- renamings:
	- icon.size > icon.extent
	- icon.scale > icon.measure
	- drag.enabled > drag.active
	- text.space > text.gap
	- text.display > text.print
	- text.spacing > text.height
	- :onHold > :onPress
	- :show > :view
  • Loading branch information
Crutiatix authored Apr 13, 2017
1 parent 7cd85b1 commit 0c3c375
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 75 deletions.
142 changes: 80 additions & 62 deletions ticuare.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
-- title: TICuare
-- author: Crutiatix
-- desc: UI library for TIC-80 v0.5.0
-- desc: UI library for TIC-80 v0.6.0
-- script: lua
-- input: mouse

-- Based on Uare (c) 2015 Ulysse Ramage
-- Credits: (c) 2015 Ulysse Ramage
-- Copyright (c) 2017 Crutiatix
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


ticuare = {name = "ticuare", elements = {}, z = 1, hz = nil}
ticuare.__index = ticuare
ticuare.mouse_events = {nothing=0,click=1,noclick=2,none=3}
ticuare.me = {nothing=0,click=1,noclick=2,none=3}
-- Private
local ticuareMt = {__index = ticuare}

local function inArea(x, y, x1, y1, x2, y2)
return x > x1 and x < x2 and y > y1 and y < y2
local function inArea(mx, my, x, y, w, h)
return mx > x and mx < x+w and my > y and my < y+h
end

local function mergeTables(t1, t2, overwrite)
Expand Down Expand Up @@ -55,7 +54,7 @@ local function copyTable(object)
end

-- multiline print
function ticuare.mlPrint(txt,x,y,c,ls,fix,fnt,key,sw) -- string; x,y position; color; line spacing; fixed letters size; use font func?; key color; space size
function ticuare.mlPrint(txt,x,y,c,ls,fix,fnt,key,sw) -- string; x,y position; color; line height; fixed letters size; use font func?; key color; gap size
local width, width_result, li = 0, 0, 0
for l in txt:gmatch("([^\n]+)") do
li = li+1
Expand All @@ -79,7 +78,8 @@ function ticuare.element(element_type, element_obj)

ticuareObj.hover, ticuareObj.click = false, false
ticuareObj.active = element_obj.active or true
ticuareObj.drag = element_obj.drag or {enabled = false}
ticuareObj.drag = element_obj.drag or {active = false}
ticuareObj.align = element_obj.align or {x=0,y=0}
ticuareObj.visible = element_obj.visible or true


Expand All @@ -100,27 +100,27 @@ function ticuare.newStyle(f) return f end

function ticuare.newGroup() local t = {type = "group", elements = {}} setmetatable(t, ticuare) return t end


--
-- Update
--

function ticuare:updateSelf(mouse_x, mouse_y, mouse_press, mouse_event)

local mouse_holding, mouse_over, element_in_focus, hovered, held
local me = ticuare.mouse_events
local mouse_holding, mouse_over, element_in_focus, hovered, held, align_x, align_y
local me, tempX, tempY = ticuare.me, self.x-(self.align.x==1 and self.w*.5 or (self.align.x==2 and self.w or 0)),
self.y-(self.align.y==1 and self.h*.5-1 or (self.align.y==2 and self.h-1 or 0))

mouse_holding = mouse_event ~= me.none and mouse_press or false

mouse_over = inArea(mouse_x, mouse_y, self.x, self.y, self.x+self.w, self.y+self.h)
if self.center then
mouse_over = inArea(mouse_x, mouse_y, self.x-self.w*.5, self.y-self.h*.5, self.x+self.w*.5, self.y+self.h*.5)
end
mouse_over = inArea(mouse_x, mouse_y, tempX, tempY, self.w, self.h)


element_in_focus = mouse_event ~= me.none and mouse_over or false

hovered, held = self.hover, self.hold

self.hover = element_in_focus or (self.drag.enabled and ticuare.draging_obj and ticuare.draging_obj.obj == self)
self.hover = element_in_focus or (self.drag.active and ticuare.draging_obj and ticuare.draging_obj.obj == self)

self.hold = ((mouse_event == me.click and element_in_focus) and true) or
(mouse_holding and self.hold) or ((element_in_focus and mouse_event ~= me.noclick and self.hold))
Expand All @@ -131,8 +131,8 @@ function ticuare:updateSelf(mouse_x, mouse_y, mouse_press, mouse_event)
self.onCleanRelease()
elseif ((mouse_event == me.noclick and element_in_focus and held) or (self.hold and not element_in_focus)) and self.onRelease then --released (or mouse has left element, still holding temporarly)
self.onRelease()
elseif self.hold and self.onHold then --holding
self.onHold()
elseif self.hold and self.onPress then --holding
self.onPress()
elseif not hovered and self.hover and self.onStartHover then --started hovering
self.onStartHover()
elseif self.hover and self.onHover then --hovering
Expand All @@ -141,9 +141,9 @@ function ticuare:updateSelf(mouse_x, mouse_y, mouse_press, mouse_event)
self.onReleaseHover()
end

if self.hold and (not element_in_focus or self.drag.enabled) and not ticuare.draging_obj then
self.hold = self.drag.enabled
ticuare.draging_obj = {obj = self, d = {x = self.x-mouse_x, y = self.y-mouse_y}} -- save what and where is element holded
if self.hold and (not element_in_focus or self.drag.active) and not ticuare.draging_obj then
self.hold = self.drag.active
ticuare.draging_obj = {obj = self, d = {x = tempX-mouse_x, y = tempY-mouse_y}} -- save what and where is element holded
elseif not self.hold and element_in_focus and (ticuare.draging_obj and ticuare.draging_obj.obj == self) then
self.hold = true
ticuare.draging_obj = nil
Expand All @@ -153,7 +153,7 @@ function ticuare:updateSelf(mouse_x, mouse_y, mouse_press, mouse_event)


-- DRAGGING
if ticuare.draging_obj and ticuare.draging_obj.obj == self and self.drag.enabled then
if ticuare.draging_obj and ticuare.draging_obj.obj == self and self.drag.active then
self.x = (not self.drag.fixed or not self.drag.fixed.x) and mouse_x + ticuare.draging_obj.d.x or self.x
self.y = (not self.drag.fixed or not self.drag.fixed.y) and mouse_y + ticuare.draging_obj.d.y or self.y

Expand Down Expand Up @@ -205,8 +205,9 @@ function ticuare:drawSelf()
sprite, tempX, tempY, text_width, text_height, text_x, text_y,
sprite_offset, text_offset, text_shadow_offset, shadow_offset
local shadow, border, text, icon = self.shadow, self.border, self.text, self.icon
tempX, tempY = self.x, self.y
if self.center then tempX, tempY = self.x-self.w*.5, self.y-self.h*.5 end

tempX = self.x-(self.align.x==1 and self.w*.5-1 or (self.align.x==2 and self.w-1 or 0))
tempY = self.y-(self.align.y==1 and self.h*.5-1 or (self.align.y==2 and self.h-1 or 0))


if shadow and shadow.colors then
Expand Down Expand Up @@ -234,17 +235,19 @@ function ticuare:drawSelf()
sprite_offset = icon.offset or {x=0,y=0}

icon.key = icon.key or -1
icon.scale = icon.scale or 1
icon.measure = icon.measure or 1
icon.flip = icon.flip or 0
icon.rotate = icon.rotate or 0
icon.size = icon.size or 1
for x=1,icon.size do
for y=1,icon.size do
icon.extent = icon.extent or {x=1,y=1}
icon.align = icon.align or {x=0,y=0}
for x=1,icon.extent.x do
for y=1,icon.extent.y do
spr(sprite+(x-1)+((y-1)*16),
(tempX+(self.center and 0 or self.w*.5)+sprite_offset.x-4),
(tempY+(self.center and 0 or self.h*.5)+sprite_offset.y-4),

(tempX+(icon.align.x==1 and self.w*.5 or (icon.align.x==2 and self.w or 0))+sprite_offset.x+((x-1)*icon.measure)*8),
(tempY+(icon.align.y==1 and self.h*.5 or (icon.align.y==2 and self.h or 0))+sprite_offset.y+((y-1)*icon.measure)*8),
icon.key,
icon.scale,
icon.measure,
icon.flip,
icon.rotate)
end
Expand All @@ -253,11 +256,11 @@ function ticuare:drawSelf()
end

--draw text
if text and text.display and text.colors[1] then
if text and text.print and text.colors[1] then
text.colors[1] = text.colors[1] or 14
text.space = text.space or 5
text.gap = text.gap or 5
text.key = text.key or -1
text.spacing = text.spacing or (text.font and 8 or 6)
text.height = text.height or (text.font and 8 or 6)
text.fixed = text.fixed or false


Expand All @@ -282,20 +285,36 @@ function ticuare:drawSelf()
end
-- initiate required vars
text_offset = text.offset or {x = 0, y = 0}
text_width, text_height = ticuare.mlPrint(text.display,300,300, -1, text.spacing, text.fixed, text.font, text.key, text.space)
text_x = self.x-(self.center and (self.w*0.5) or 0)+(text.center and (self.w*.5)-(text_width*.5) or 0)+text_offset.x+(text.center and 0 or border.width)
text_y = self.y-(self.center and (self.h*0.5) or 0)+(text.center and (self.h*.5)-(text_height*.5) or 0)+text_offset.y+(text.center and 0 or border.width)
text_width, text_height = ticuare.mlPrint(text.print,0,200, -1, text.height, text.fixed, text.font, text.key, text.gap)
text.align = text.align or {x=0,y=0}

if text.align.x == 1 then
text_x = tempX+((self.w*.5)-(text_width*.5))+text_offset.x
elseif text.align.x == 2 then
text_x = tempX+((self.w)-(text_width))+text_offset.x-border.width
else
text_x = tempX+text_offset.x+border.width
end

if text.align.y == 1 then
text_y = tempY+((self.h*.5)-(text_height*.5))+text_offset.y
elseif text.align.y == 2 then
text_y = tempY+((self.h)-(text_height))+text_offset.y-border.width
else
text_y = tempY+text_offset.y+border.width
end

-- drawing text and text shadow
if text.shadow and text_shadow_color then
ticuare.mlPrint(text.display, text_x+text_shadow_offset.x, text_y+text_shadow_offset.y, text_shadow_color, text.spacing, text.fixed, text.font, text.key, text.space)
ticuare.mlPrint(text.display, text_x, text_y, text_color, text.spacing, text.fixed, text.font, text.key, text.space)
ticuare.mlPrint(text.print, text_x+text_shadow_offset.x, text_y+text_shadow_offset.y, text_shadow_color, text.height, text.fixed, text.font, text.key, text.gap)
ticuare.mlPrint(text.print, text_x, text_y, text_color, text.height, text.fixed, text.font, text.key, text.gap)
else
ticuare.mlPrint(text.display, text_x, text_y, text_color, text.spacing, text.fixed, text.font, text.key, text.space)
ticuare.mlPrint(text.print, text_x, text_y, text_color, text.height, text.fixed, text.font, text.key, text.gap)
end
end
-- drawing element content
if self.content and self.drawContent then
if self.content.wrap and clip then clip(self.x+border.width, self.y+border.width, self.w-(2*border.width), self.h-(2*border.width)) end
if self.content.wrap and clip then clip(tempX+border.width, tempY+border.width, self.w-(2*border.width), self.h-(2*border.width)) end
self:renderContent()
if self.content.wrap and clip then clip() end
end
Expand All @@ -307,14 +326,11 @@ end
--

function ticuare:renderContent()
local tx, ty = self.x, self.y
if self.center then tx, ty = self.x-self.w*.5, self.y-self.h*.5 end
local border = self.border.width and self.border.width+1 or 1
local tx, ty = self.x-(self.align.x==1 and self.w*.5 or (self.align.x==2 and self.w or 0)), self.y-(self.align.y==1 and self.h*.5-1 or (self.align.y==2 and self.h-1 or 0))
local border = self.border.width and self.border.width or 1
local offsetX = tx-(self.content.scroll.x or 0)*(self.content.w-self.w) + border
local offsetY = ty-(self.content.scroll.y or 0)*(self.content.h-self.h) + border
self.drawContent(self,offsetX,offsetY)


end

function ticuare:setContent(f)
Expand Down Expand Up @@ -350,7 +366,7 @@ end
function ticuare.update(mouse_x, mouse_y, mouse_press)

if mouse_x and mouse_y then
local me = ticuare.mouse_events
local me, elements = ticuare.me, ticuare.elements
local mouse_event, focused, updateQueue, elemt = me.nothing, false, {}, nil

if ticuare.click and not mouse_press then
Expand All @@ -363,7 +379,7 @@ function ticuare.update(mouse_x, mouse_y, mouse_press)
ticuare.draging_obj = nil
end
--update every element/window first...
for i = 1, #ticuare.elements do table.insert(updateQueue, ticuare.elements[i]) end
for i = 1, #elements do table.insert(updateQueue, elements[i]) end

table.sort(updateQueue, function(a, b) return a.z > b.z end)

Expand All @@ -376,9 +392,9 @@ function ticuare.update(mouse_x, mouse_y, mouse_press)
end
end
--...then update their anchors
for i = #ticuare.elements, 1, -1 do
if ticuare.elements[i] then
ticuare.elements[i]:updateTrack()
for i = #elements, 1, -1 do
if elements[i] then
elements[i]:updateTrack()
end
end
end
Expand Down Expand Up @@ -453,7 +469,7 @@ function ticuare:getActive() if self.active ~= nil then return self.active end e

--Visible

function ticuare:setVisible(bool) --l is for lerp
function ticuare:setVisible(bool)

if self.type == "group" then
for i = 1, #self.elements do
Expand All @@ -465,9 +481,9 @@ function ticuare:setVisible(bool) --l is for lerp

end

function ticuare:show(l) return self:setVisible(true, l) end
function ticuare:view() return self:setVisible(true) end

function ticuare:hide(l) return self:setVisible(false, l) end
function ticuare:hide() return self:setVisible(false) end

function ticuare:getVisible() if self.visible ~= nil then return self.visible end end

Expand All @@ -478,21 +494,25 @@ function ticuare:setDragBounds(bounds)
end

function ticuare:setHorizontalRange(n)
self.x = self.drag.bounds.x[1] + (self.drag.bounds.x[2]-self.drag.bounds.x[1])*n
local bounds = self.drag.bounds
self.x = bounds.x[1] + (bounds.x[2]-bounds.x[1])*n
end

function ticuare:setVerticalRange(n)
self.y = self.drag.bounds.y[1] + (self.drag.bounds.y[2]-self.drag.bounds.y[1])*n
local bounds = self.drag.bounds
self.y = bounds.y[1] + (bounds.y[2]-bounds.y[1])*n
end

function ticuare:getHorizontalRange()
assert(self.drag.bounds and self.drag.bounds.x and self.drag.bounds.x and self.drag.bounds.x[1] and self.drag.bounds.x[2], "Element must have 2 horizontal boundaries")
return (self.x-self.drag.bounds.x[1]) / (self.drag.bounds.x[2]-self.drag.bounds.x[1])
local bounds = self.drag.bounds
assert(bounds and bounds.x and #bounds.x==2, "X bounds error!")
return (self.x-bounds.x[1]) / (bounds.x[2]-bounds.x[1])
end

function ticuare:getVerticalRange()
assert(self.drag.bounds and self.drag.bounds.y and self.drag.bounds.y and self.drag.bounds.y[1] and self.drag.bounds.y[2], "Element must have 2 vertical boundaries")
return (self.y-self.drag.bounds.y[1]) / (self.drag.bounds.y[2]-self.drag.bounds.y[1])
local bounds = self.drag.bounds
assert(bounds and bounds.y and #bounds.y==2, "Y bounds error!")
return (self.y-bounds.y[1]) / (bounds.y[2]-bounds.y[1])
end

--Z-Index
Expand Down Expand Up @@ -528,8 +548,6 @@ function ticuare:remove()
end
end

function ticuare.clear()
function ticuare.empty()
for i = 1, #ticuare.elements do ticuare.elements[i] = nil end
end

--return ticuare
Loading

0 comments on commit 0c3c375

Please sign in to comment.