Skip to content

Commit

Permalink
Core widget logic
Browse files Browse the repository at this point in the history
Also changes to lambda! IMPORTANT
You now have -> (no return) and => (return)
  • Loading branch information
jimbauwens committed Mar 13, 2015
1 parent 18144eb commit de4b179
Show file tree
Hide file tree
Showing 21 changed files with 1,895 additions and 132 deletions.
90 changes: 81 additions & 9 deletions ETK/etk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,94 @@
----------------------------------

etk = {}
local etk = etk

--include "helpers.lua"
--include "graphics.lua"
--include "screenmanager.lua"
--include "eventmanager.lua"
--include "widgetmanager.lua"

do
local etk = etk

--include "helpers.lua"
--include "graphics.lua"
--include "screenmanager.lua"
--include "viewmanager.lua"
--include "eventmanager.lua"
--include "widgetmanager.lua"
end

do
local myView = View()
local Button = etk.Widgets.Button
local Input = etk.Widgets.Input
local myView = etk.View()


local box1 = Box(
Position {
top = "50px",
left = "100px"
},
Dimension ("100px", "10%"),
"Hello world")

local box2 = Box(
Position {
top = "50px",
left = "2px",
alignment = {
{ref=box1, side=Position.Sides.Right}
}
},
Dimension ("50px", "10%"),
"Hello!")

local box3 = Box(
Position {
top = "2px",
left = "0",
alignment = {
{ref=box2, side=Position.Sides.Bottom},
{ref=box2, side=Position.Sides.Left}
}
},
Dimension ("50px", "20%"),
"Yolo")

local button1 = Button {
position = Position { bottom = "2px", right = "2px" },
text = "Button1"
}

local button2 = Button {
position = Position { bottom = "2px", right = "2px", alignment = {{ref=button1, side=Position.Sides.Left}}},
text = "Button2"
}

local input1 = Input {
position = Position { top = "2px", left = "2px" },
value = "1"
}
input1.disabled = true

local input2 = Input {
position = Position { top = "2px", left = "2px", alignment = {{ref=input1, side=Position.Sides.Bottom}}},
value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
input2.dimension.width = Input.defaultStyle.defaultWidth * 2

myView:addChildren(box1, box2, box3, button1, button2, input1, input2)


function button2:charIn(char)
self.text = self.text .. char

self.parent:invalidate()
end

button2.onAction = λ -> input1.value++;


function myView:draw(gc, x, y, width, height)
Logger.Log("in myView draw")
gc:drawString(string.format("%d, %d, %d, %d", x, y, width, height), 10, 10, "top")
end



etk.RootScreen:pushScreen(myView)
end
2 changes: 2 additions & 0 deletions ETK/eventmanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ do
end

on.paint = function(gc)
gc:smartClipRect("set", 0, 0, eg.viewPortWidth, eg.viewPortHeight)

eventLinker.__index(on, "paint")(gc)

eg.dimensionsChanged = false
Expand Down
19 changes: 11 additions & 8 deletions ETK/graphics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ do
eg.needsFullRedraw = true
eg.dimensionsChanged = true

eg.isColor = platform.isColorDisplay()

eg.viewPortWidth = 318
eg.viewPortHeight = 212

Expand All @@ -40,14 +42,15 @@ do

local clipRectData = {}
local clipRects = 0
local old = clipRectData[clipRects]

local gc_clipRect = function (gc, what, x, y, w, h)
if what == "set" then
clipRects = clipRects + 1
clipRectData[clipRects] = {x, y, w, h}

elseif what == "subset" and old then
elseif what == "subset" and clipRects > 0 then
local old = clipRectData[clipRects]

x = old[1] < x and x or old[1]
y = old[2] < y and y or old[2]
h = old[2] + old[4] > y + h and h or old[2] + old[4] - y
Expand All @@ -58,18 +61,19 @@ do
clipRects = clipRects + 1
clipRectData[clipRects] = {x, y, w, h}

elseif what == "restore" and old then
elseif what == "restore" and clipRects > 0 then
what = "set"
x, y, w, h = old[1], old[2], old[3], old[4]

clipRectData[clipRects] = nil
clipRects = clipRects - 1

local old = clipRectData[clipRects]
x, y, w, h = old[1], old[2], old[3], old[4]

elseif what == "restore" then
what = "reset"

end

gc:clipRect(what, x, y, w, h)
end

Expand All @@ -81,7 +85,7 @@ do
platform.withGC = function (func, ...)
local gc = platform.gc()
gc:begin()
func(..., gc)
func(..., gc) -- BUG: if you have a parameter after ..., you will only select the first parameter from the list, <- func({...}[1], gc)
end
end

Expand All @@ -92,7 +96,6 @@ do
local addToGC = function (name, func)
local gcMeta = platform.withGC(getmetatable)
gcMeta[name] = func
-- It's that simple!
end

------------------------
Expand Down
61 changes: 55 additions & 6 deletions ETK/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end

do UnitCalculator = {}
UnitCalculator.GetAbsoluteValue = function (value, referenceValue)
local numberValue, unit = string.match(tostring(value), "([%d.]+)(.*)")
local numberValue, unit = string.match(tostring(value), "([-%d.]+)(.*)")

local number = tonumber(numberValue)

Expand Down Expand Up @@ -75,9 +75,21 @@ do Dimension = class()

return self.cachedWidth, self.cachedHeight
else
self.cachedWidth = parentWidth
self.cachedHeight = parentHeight

return parentWidth, parentHeight
end
end

function Dimension:getCachedDimension()
return self.cachedWidth or 0, self.cachedHeight or 0
end

function Dimension:invalidate()
self.cachedWidth = nil
self.cachedHeight = nil
end
end

do Position = class()
Expand Down Expand Up @@ -109,6 +121,14 @@ do Position = class()
local originX = parentX
local originY = parentY

if self.right then
originX = originX + parentWidth
end

if self.bottom then
originY = originY + parentHeight
end

for _, alignment in ipairs(self.alignment) do
local side = alignment.side
local ref = alignment.ref
Expand All @@ -129,15 +149,15 @@ do Position = class()
end

if self.left then
x = originX + self.left
x = originX + UnitCalculator.GetAbsoluteValue(self.left, parentWidth)
elseif self.right then
x = originX - self.right - width
x = originX - UnitCalculator.GetAbsoluteValue(self.right, parentWidth) - width
end

if self.top then
y = originY + self.top
y = originY + UnitCalculator.GetAbsoluteValue(self.top, parentHeight)
elseif self.bottom then
y = originY - self.bottom - height
y = originY - UnitCalculator.GetAbsoluteValue(self.bottom, parentHeight) - height
end

self.cachedX = x
Expand All @@ -146,5 +166,34 @@ do Position = class()

return self.cachedX, self.cachedY
end

function Position:invalidate()
self.cachedX = nil
self.cachedY = nil
end

function Position:getCachedPosition()
return self.cachedX or 0, self.cachedY or 0
end

end

-----------
-- Color --
-----------

local function unpackColor(col)
return col[1] or 0, col[2] or 0, col[3] or 0
end

-------------------
-- Event calling --
-------------------

local CallEvent = function(object, event, ...)
local handler = object[event]

if handler then
return handler, handler(object, ...)
end
end
Loading

0 comments on commit de4b179

Please sign in to comment.