lua小巧高效,能与C自然的结合与交互,所以常被用于写游戏逻辑,不过lua并非天生为游戏而设计,它也可以用来写GUI。
wxLua是一个把wxWidgets库和lua绑定的项目,可以方便的使用lua写GUI。
这是wxLua在sourceforge的站点http://wxlua.sourceforge.net/。
下载wxLua并解压后,在wxLua的samples目录下有一个例子叫editor.wx.lua,运行后是这样的。
这是一个用lua编写的lua语言编辑器,我们考虑把它改进一下,为它加上函数列表显示,并且点击函数名就可以快速的定位到某个函数。就像这样:

-- 声明菜单ID
local ID_PALEXT = NewID()
local ID_FUNC_LIST = NewID()
-- 声明函数列表窗口与控件的ID
local ID_FUNC_LISTCTRL = NewID()
local ID_FUNC_WINDOW = NewID()
-- 创建菜单
My_ExtMenu = wx.wxCreateMenu
{
{
ID_PALEXT, "&Function List", ""
}
}
-- 加入菜单条
menuBar:Append(My_ExtMenu, "&MyExtension")
-- 设置菜单事件
frame:Connect(ID_PALEXT, wx.wxEVT_COMMAND_MENU_SELECTED, CreateFunctionListWindow)
-- 创建窗口
function CreateFunctionListWindow()
local width = 213
-- 建立窗口
FuncListWindow = wx.wxFrame(frame, ID_FUNC_WINDOW, "wxLua FunctionList Window",
wx.wxDefaultPosition, wx.wxSize(width, 400))
-- 建立列表控件
funcListCtrl = wx.wxListCtrl(FuncListWindow, ID_FUNC_LISTCTRL,
wx.wxDefaultPosition, wx.wxDefaultSize,
wx.wxLC_REPORT + wx.wxLC_EDIT_LABELS)
-- 建立列表栏目
local info = wx.wxListItem()
info:SetMask(wx.wxLIST_MASK_TEXT + wx.wxLIST_MASK_WIDTH)
info:SetText("Line")
info:SetWidth(40)
funcListCtrl:InsertColumn(0, info)
info:SetText("FuncName")
info:SetWidth(160)
funcListCtrl:InsertColumn(1, info)
FuncListWindow:CentreOnParent()
ConfigRestoreFramePosition(FuncListWindow, "FuncList")
FuncListWindow:Show(true)
local allFunc = FindAllFunction()
for i,v in ipairs(allFunc) do
local row = funcListCtrl:InsertStringItem(funcListCtrl:GetItemCount(), "Line")
funcListCtrl:SetStringItem(row, 0, v[1])
funcListCtrl:SetStringItem(row, 1, v[2])
end
local function FindSelectedItem()
local count = funcListCtrl:GetSelectedItemCount()
if count > 0 then
for idx = 0, funcListCtrl:GetItemCount() - 1 do
if funcListCtrl:GetItemState(idx, wx.wxLIST_STATE_FOCUSED) ~= 0 then
return idx
end
end
end
return -1
end
-- 列表项被点击事件,点击后光标跳到函数所在行
FuncListWindow:Connect(ID_FUNC_LISTCTRL, wx.wxEVT_COMMAND_LIST_ITEM_SELECTED,
function (event)
local row = FindSelectedItem()
if row >= 0 then
local iLine = funcListCtrl:GetItemText(row)
local editor = GetEditor()
editor:GotoLine( tonumber(iLine) + 2 )
end
end)
end
-- 简易的词法分析函数,找出所有的function和所在行
function FindAllFunction()
local function IsFuncName(char)
return (char > 47 and char < 58) or (char > 64 and char < 91) or (char > 96 and char < 123) or char == string.byte('_')
end
local editor = GetEditor()
local lenFind = string.len("function")
local AllFunc = {}
local FuncNum = 1
local iLine = 1
local nLength = editor:GetLength()
for iPos = 1, nLength do
local cChar = editor:GetCharAt(iPos)
if cChar == 10 then
iLine = iLine + 1
end
if cChar == 102 and (IsFuncName(editor:GetCharAt(iPos-1)) == false or iPos == 1) then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 117 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 110 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 99 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 116 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 105 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 111 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 110 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
if cChar == 32 or cChar == 9 then
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
while cChar == 32 or cChar == 9 do
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
end
if (cChar > 64 and cChar < 91) or (cChar > 96 and cChar < 123) or (cChar == string.byte('_')) then
local funcName = ""
funcName = funcName .. string.char(cChar)
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
while IsFuncName(cChar) do
funcName = funcName .. string.char(cChar)
iPos = iPos + 1
cChar = editor:GetCharAt(iPos)
end
AllFunc[FuncNum] = { iLine, funcName }
FuncNum = FuncNum + 1
end
end
end
end
end
end
end
end
end
end
end
return AllFunc
end
好了,这样就给这个lua编辑器加上了列出所有function并快速定位的功能,如图:
>>>完整代码下载<<<