reorganize some stuff

This commit is contained in:
Eduardo Bart
2012-01-04 23:28:29 -02:00
parent a92af44eb6
commit 30ce5e2ba9
69 changed files with 143 additions and 131 deletions

151
modules/core_lib/const.lua Normal file
View File

@@ -0,0 +1,151 @@
AnchorNone = 0
AnchorTop = 1
AnchorBottom = 2
AnchorLeft = 3
AnchorRight = 4
AnchorVerticalCenter = 5
AnchorHorizontalCenter = 6
LogDebug = 0
LogInfo = 1
LogWarning = 2
LogError = 3
LogFatal = 4
ActiveFocusReason = 2
KeyUnknown = 0
KeyEscape = 1
KeyTab = 2
KeyBackspace = 3
KeyReturn = 4
KeyEnter = 5
KeyInsert = 6
KeyDelete = 7
KeyPause = 8
KeyPrintScreen = 9
KeyHome = 10
KeyEnd = 11
KeyPageUp = 12
KeyPageDown = 13
KeyUp = 14
KeyDown = 15
KeyLeft = 16
KeyRight = 17
KeyNumLock = 18
KeyScrollLock = 19
KeyCapsLock = 20
KeyCtrl = 21
KeyShift = 22
KeyAlt = 23
KeyAltGr = 24
KeyMeta = 25
KeyMenu = 26
KeySpace = 32 -- ' '
KeyExclamation = 33 -- !
KeyQuote = 34 -- "
KeyNumberSign = 35 -- #
KeyDollar = 36 -- $
KeyPercent = 37 -- %
KeyAmpersand = 38 -- &
KeyApostrophe = 39 -- '
KeyLeftParen = 40 -- (
KeyRightParen = 41 -- )
KeyAsterisk = 42 -- *
KeyPlus = 43 -- +
KeyComma = 44 -- ,
KeyMinus = 45 -- -
KeyPeriod = 46 -- .
KeySlash = 47 -- /
Key0 = 48 -- 0
Key1 = 49 -- 1
Key2 = 50 -- 2
Key3 = 51 -- 3
Key4 = 52 -- 4
Key5 = 53 -- 5
Key6 = 54 -- 6
Key7 = 55 -- 7
Key8 = 56 -- 8
Key9 = 57 -- 9
KeyColon = 58 -- :
KeySemicolon = 59 -- ;
KeyLess = 60 -- <
KeyEqual = 61 -- =
KeyGreater = 62 -- >
KeyQuestion = 63 -- ?
KeyAtSign = 64 -- @
KeyA = 65 -- a
KeyB = 66 -- b
KeyC = 67 -- c
KeyD = 68 -- d
KeyE = 69 -- e
KeyF = 70 -- f
KeyG = 71 -- g
KeyH = 72 -- h
KeyI = 73 -- i
KeyJ = 74 -- j
KeyK = 75 -- k
KeyL = 76 -- l
KeyM = 77 -- m
KeyN = 78 -- n
KeyO = 79 -- o
KeyP = 80 -- p
KeyQ = 81 -- q
KeyR = 82 -- r
KeyS = 83 -- s
KeyT = 84 -- t
KeyU = 85 -- u
KeyV = 86 -- v
KeyW = 87 -- w
KeyX = 88 -- x
KeyY = 89 -- y
KeyZ = 90 -- z
KeyLeftBracket = 91 -- [
KeyBackslash = 92 -- '\'
KeyRightBracket = 93 -- ]
KeyCaret = 94 -- ^
KeyUnderscore = 95 -- _
KeyGrave = 96 -- `
KeyLeftCurly = 123 -- {
KeyBar = 124 -- |
KeyRightCurly = 125 -- }
KeyTilde = 126 -- ~
KeyF1 = 128
KeyF2 = 129
KeyF3 = 130
KeyF4 = 131
KeyF5 = 132
KeyF6 = 134
KeyF7 = 135
KeyF8 = 136
KeyF9 = 137
KeyF10 = 138
KeyF11 = 139
KeyF12 = 140
KeyboardNoModifier = 0
KeyboardCtrlModifier = 1
KeyboardAltModifier = 2
KeyboardShiftModifier = 4
MouseNoButton = 0
MouseLeftButton = 1
MouseRightButton = 2
MouseMidButton = 3
AlignNone = 0
AlignLeft = 1
AlignRight = 2
AlignTop = 4
AlignBottom = 8
AlignHorizontalCenter = 16
AlignVerticalCenter = 32
AlignTopLeft = 5
AlignTopRight = 6
AlignBottomLeft = 9
AlignBottomRight = 10
AlignLeftCenter = 33
AlignRightCenter = 34
AlignTopCenter = 20
AlignBottomCenter = 24
AlignCenter = 48

View File

@@ -0,0 +1,19 @@
Module
name: core_lib
description: Contains core lua classes, functions and constants used by other modules
author: OTClient team
website: https://github.com/edubart/otclient
onLoad: |
require 'ext/table'
require 'ext/string'
require 'math/point'
require 'math/size'
require 'math/color'
require 'math/rect'
require 'const'
require 'util'
require 'globals'
require 'dispatcher'
require 'widget'
require 'effects'

View File

@@ -0,0 +1,33 @@
local eventId = 0
local eventList = {}
function scheduleEvent(func, delay)
eventId = eventId + 1
local id = eventId
local function proxyFunc()
if eventList[id] then
if eventList[id].active then
func()
end
eventList[id] = nil
end
end
eventList[id] = { func = proxyFunc, active = true }
if delay and delay > 0 then
g_dispatcher.scheduleEvent(proxyFunc, delay)
else
g_dispatcher.addEvent(proxyFunc, false)
end
return id
end
function addEvent(func)
return scheduleEvent(func, 0)
end
function removeEvent(id)
if id and eventList[id] then
eventList[id].active = false
return true
end
end

View File

@@ -0,0 +1,26 @@
Effects = {}
function Effects.fadeIn(widget, time, elapsed)
if not elapsed then elapsed = 0 end
if not time then time = 250 end
widget:setOpacity(math.min((255*elapsed)/time, 255))
if elapsed < time then
scheduleEvent(function()
Effects.fadeIn(widget, time, elapsed + 30)
end, 30)
end
end
function Effects.fadeOut(widget, time, elapsed)
if not elapsed then elapsed = 0 end
if not time then time = 250 end
widget:setOpacity((255*(time - elapsed))/time)
if elapsed < time then
scheduleEvent(function()
Effects.fadeOut(widget, time, elapsed + 30)
end, 30)
else
widget:destroy()
end
end

View File

@@ -0,0 +1,26 @@
function string:split(sep)
local t = { }
local function helper(word)
table.insert(t, word)
return ""
end
if not self:gsub("%w+", helper):find("%S") then
return t
end
end
function string:starts(start)
return self:sub(1, #start) == start
end
function string:trim()
return self:match('^%s*(.*%S)') or ''
end
function toboolean(str)
str = str:trim()
if str == '1' or str == 'true' then
return true
end
return false
end

View File

@@ -0,0 +1,34 @@
function table.dump(t, depth)
if not depth then depth = 0 end
for k,v in pairs(t) do
str = (' '):rep(depth * 2) .. k .. ': '
if type(v) ~= "table" then
print(str .. tostring(v))
else
print(str)
table.dump(v, depth+1)
end
end
end
function table.copy(t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
function table.selective_copy(t, keys)
local res = { }
for i,v in ipairs(keys) do
res[v] = t[v]
end
return res
end
function table.merge(t, src)
for k,v in pairs(src) do
t[k] = v
end
end

View File

@@ -0,0 +1,73 @@
rootWidget = g_ui.getRootWidget()
function importStyle(otui)
g_ui.importStyle(resolvepath(otui, 2))
end
function importFont(otfont)
g_fonts.importFont(resolvepath(otfont, 2))
end
function setDefaultFont(font)
g_fonts.setDefaultFont(font)
end
function displayUI(arg1, options)
local widget
local parent
if options then parent = options.parent end
parent = parent or rootWidget
-- display otui files
if type(arg1) == 'string' then
local otuiFilePath = resolvepath(arg1, 2)
widget = g_ui.loadUI(otuiFilePath, parent)
-- display already loaded widgets
else
widget = arg1
if parent:hasChild(widget) then
widget:focus()
widget:show()
else
parent:addChild(widget)
widget:show()
end
end
-- apply display options
if widget and options then
for option,value in pairs(options) do
if option == 'locked' and value then
widget:lock()
elseif option == 'visible' then
widget:setVisible(value)
elseif option == 'x' then
widget:setX(value)
elseif option == 'y' then
widget:setY(value)
end
end
end
return widget
end
function createWidget(style, parent)
local className = g_ui.getStyleClass(style)
if className == "" then
error('could not find style ' .. style)
return
end
local class = _G[className]
if not class then
error('could not find widget class ' .. class)
return
end
local widget = class.create()
if parent then
parent:addChild(widget)
end
widget:setStyle(style)
return widget
end

View File

@@ -0,0 +1,6 @@
Hotkeys = {}
function Hotkeys.bindKey(keyDesc, func)
end

View File

@@ -0,0 +1 @@
Color = {}

View File

@@ -0,0 +1 @@
Point = {}

View File

@@ -0,0 +1 @@
Rect = {}

View File

@@ -0,0 +1 @@
Size = {}

70
modules/core_lib/util.lua Normal file
View File

@@ -0,0 +1,70 @@
function print(...)
local msg = ""
for i,v in ipairs(arg) do
msg = msg .. tostring(v) .. "\t"
end
Logger.log(LogInfo, msg)
end
function fatal(msg)
Logger.log(LogFatal, msg)
end
function connect(object, signalsAndSlots, pushFront)
for signal,slot in pairs(signalsAndSlots) do
if not object[signal] then
object[signal] = slot
elseif type(object[signal]) == 'function' then
object[signal] = { object[signal], slot }
elseif type(object[signal]) == 'table' then
if pushFront then
table.insert(object[signal], 1, slot)
else
table.insert(object[signal], #object[signal]+1, slot)
end
end
end
end
function extends(base)
local derived = {}
function derived.internalCreate()
local instance = base.create()
for k,v in pairs(derived) do
instance[k] = v
end
return instance
end
derived.create = derived.internalCreate
return derived
end
function newenv()
local env = { }
setmetatable(env, { __index = _G} )
return env
end
function getfsrcpath(depth)
depth = depth or 2
local info = debug.getinfo(1+depth, "Sn")
local path
if info.short_src then
path = info.short_src:match("(.*)/.*")
end
if not path then
path = '/'
elseif path:sub(0, 1) ~= '/' then
path = '/' .. path
end
return path
end
function resolvepath(filePath, depth)
depth = depth or 1
if filePath:sub(0, 1) ~= '/' then
return getfsrcpath(depth+1) .. '/' .. filePath
else
return filePath
end
end

View File

@@ -0,0 +1,19 @@
function UIWidget:setMargin(...)
local params = {...}
if #params == 1 then
self:setMarginTop(params[1])
self:setMarginRight(params[1])
self:setMarginBottom(params[1])
self:setMarginLeft(params[1])
elseif #params == 2 then
self:setMarginTop(params[1])
self:setMarginRight(params[2])
self:setMarginBottom(params[1])
self:setMarginLeft(params[2])
elseif #params == 4 then
self:setMarginTop(params[1])
self:setMarginRight(params[2])
self:setMarginBottom(params[3])
self:setMarginLeft(params[4])
end
end