Skip to content

Instantly share code, notes, and snippets.

@DelusionalLogic
Created July 5, 2012 22:45
Show Gist options
  • Save DelusionalLogic/3056932 to your computer and use it in GitHub Desktop.
Save DelusionalLogic/3056932 to your computer and use it in GitHub Desktop.
guilib
-- User: Delusional Logic
-- Date: 06-07-12
-- Time: 00:00
cursor = {
x = 0,
y = 0,
}
---Creates a new window
function cursor:create()
local new_inst = {} -- the new instance
setmetatable( new_inst, { __index = cursor } ) -- all instances share the same metatable
return new_inst
end
function cursor:up()
self.y = self.y - 10
end
function cursor:down()
self.y = self.y + 10
end
function cursor:left()
self.x = self.x - 10
end
function cursor:right()
self.x = self.x + 10
end
function cursor:click(callback)
callback(self.x, self.y)
end
function cursor:draw()
local hori = "_"
local vert = "|"
DrawText(hori, 14, self.x - 3, self.y - 3, 0xFF00FF00)
DrawText(vert, 14, self.x - 3, self.y + 3, 0xFF00FF00)
end
-- User: Delusional Logic
-- Date: 05-07-12
-- Time: 21:44
function altDoFile(name)
dofile(debug.getinfo(1).source:sub(debug.getinfo(1).source:find(".*\\")):sub(2)..name)
end
altDoFile("libs/Guilib/utils.lua")
altDoFile("libs/Guilib/window.lua")
-- User: Delusional Logic
-- Date: 05-07-12
-- Time: 23:51
function notify(callbackTable)
for key, callback in pairs(callbackTable) do
callback()
end
end
-- User: Delusional Logic
-- Date: 05-07-12
-- Time: 22:28
window = {
x = 0,
y = 0,
width = 0,
height = 0,
components = {},
clickCallback = {}
}
---Creates a new window
function window:create()
local new_inst = {} -- the new instance
setmetatable( new_inst, { __index = window } ) -- all instances share the same metatable
return new_inst
end
---Sets the position of the window
--@param x The new x location
--@param y The new y location
function window:setPosition(x,y)
self.x = x
self.y = y
end
---Sets the height of the window
--@param height The new height
function window:setHeight(height)
self.heigth = height
end
---Sets the width of the window
--@param width The new width
function window:setWidth(width)
self.width = width
end
---Add a callback for clicks
--@param callback the function to be called
function window:addClickHandler(callback)
table.insert(self.clickCallback, callback)
end
---Notify the window of a click
--@param x The x coordinate of the mouse
--@param y The y coordinate of the mouse
function window:click(x, y)
if self:positionInWindow(x, y) then
notify(self.clickCallback)
end
for number, component in pairs(self.componenets) do
component:click(x, y)
end
end
---Draws the window
function window:draw()
local hori = "_"
local vert = "|"
DrawText(hori, 14, self.x, self.y - 7, 0xFFFF0000)
DrawText(hori, 14, self.x + (self.width - 7), self.y - 7, 0xFFFF0000)
DrawText(hori, 14, self.x, self.y + self.heigth - 7, 0xFFFF0000)
DrawText(hori, 14, self.x + (self.width - 7), self.y + self.heigth - 7, 0xFFFF0000)
DrawText(vert, 14, self.x, self.y, 0xFFFF0000)
DrawText(vert, 14, self.x + self.width, self.y, 0xFFFF0000)
DrawText(vert, 14, self.x, self.y + (self.heigth - 7), 0xFFFF0000)
DrawText(vert, 14, self.x + self.width, self.y + (self.heigth - 7), 0xFFFF0000)
for number,component in pairs(self.components) do
component:draw()
end
end
---Checks if position is inside window
--@param x The x position
--@param y The y position
function window:positionInWindow(x, y)
if (self.x < x and self.x + self.width > x) and (self.y+self.heigth > y and self.y < y) then
return true
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment