您的位置:首页 > 编程语言 > Lua

100行lua代码实现简单目录浏览器

2016-06-27 12:55 495 查看
摘要: 界面部分用lae实现,无需写代码,100行lua代码实现简单目录浏览器。





lae下载地址:

https://github.com/ouloba/laetool.git

lae下载地址(国内):https://pan.baidu.com/s/1ckMy0Q

相关视频:

http://www.tudou.com/listplay/aly7NDWz_sQ/AaqZ81jIt-k.html

教程:

lae界面开发工具入门介绍之一<新建工程篇>

lae界面开发工具入门之介绍二--<渲染组件篇>

lae界面开发工具入门之介绍三--<布局篇>

lae界面开发工具入门之介绍四--<秘籍篇-拷贝粘贴>

lae界面开发工具入门之介绍五--<秘籍篇-杂项>

lae界面开发工具入门之介绍六--<秘籍篇-状态篇>

关于lae工具问题单独介绍!

--lua代码

lua编辑器 
csdn下载

github下载地址,csdn没有更新版本功能,这里改为git,可实时获得最新的版本
下载地址:


--目录浏览器下载地址

https://github.com/ouloba/folder-explorer.git

--辅助接口
LXZDoFile("LXZHelper.lua");
LXZDoFile("serial.lua");

--每帧调用,root窗口status中IsActive设置为true,即可触发OnUpdate事件。
local function OnUpdate(window, msg, sender)
UpdateWindow();
end

--更新目录子目录或者文件列表
local function UpdateDirectry(dir)
local root = HelperGetRoot();

--set current dir.
lfs.chdir(dir);
HelperSetWindowText(root:GetLXZWindow("directry"), dir);

--
local items = root:GetLXZWindow("folders:area:items"); --目录文件容器
local item = root:GetLXZWindow("folders:item"); --目录文件项
local path = lfs.currentdir();

--清除容器中内容
items:ClearChilds();

--遍历该目录下的子目录文件
local cnt = 0;
for file in lfs.dir(lfs.currentdir()) do
local wnd = item:Clone(); --克隆一个目录文件项"folders:item"
wnd:Show();                      --显示
HelperSetWindowText(wnd:GetChild("text"), file); --设置目录或者文件名
items:AddChild(wnd);   --加入items容器中

local f = path.."\\"..file;
local attr = lfs.attributes(f);
if attr and attr.mode=="directory" then
wnd:GetChild("icon"):SetState(0); --通过0状态设置目录图标
else
wnd:GetChild("icon"):SetState(1);--通过1状态设置文件名图标
end
cnt=cnt+1;
end

--如果无法访问该目录,则添加"."与".."
if cnt==0 then
local wnd = item:Clone();
wnd:Show();
HelperSetWindowText(wnd:GetChild("text"), ".");
items:AddChild(wnd);

local wnd = item:Clone();
wnd:Show();
HelperSetWindowText(wnd:GetChild("text"), "..");
items:AddChild(wnd);
end

--垂直滚动条适应内容大小。
local msg = CLXZMessage:new_local();
local wnd = root:GetLXZWindow("folders:vertical slider");
wnd:ProcMessage("OnReset", msg, wnd);

end

--点击目录或者文件项
local function OnClickItem(window, msg, sender)
local file=HelperGetWindowText(sender:GetChild("text"));
local path = lfs.currentdir();

local f = path.."\\"..file;
local attr,err = lfs.attributes (f)
if attr== nil then
LXZMessageBox("error:"..err);
return;
end

-- LXZMessageBox("type(attr)"..type(attr).."f:"..f)
assert (type(attr) == "table");
if attr.mode == "directory" then --如果是目录
UpdateDirectry(f);
end

end

--ui加载时触发该事件
local function OnLoad(window, msg, sender)
local root = HelperGetRoot();

--set default.
local default_dir = "c:\\";
HelperSetWindowText(root:GetLXZWindow("directry"), default_dir);

--set folder list.
UpdateDirectry(default_dir);
end

--事件与接口绑定
local event_callback = {}
event_callback ["OnUpdate"] = OnUpdate;
event_callback ["OnLoad"] = OnLoad;
event_callback ["OnClickItem"] = OnClickItem;

--事件分发器
function main_dispacher(window, cmd, msg, sender)
--- LXZAPI_OutputDebugStr("cmd 1:"..cmd);
if(event_callback[cmd] ~= nil) then
--  LXZAPI_OutputDebugStr("cmd 2:"..cmd);
event_callback[cmd](window, msg, sender);
end
end




增加权限、创建时间、修改时间等

--增加lua代码

if attr then
HelperSetWindowText(wnd:GetChild("access time"), os.date("%c", attr.access) );
HelperSetWindowText(wnd:GetChild("modify time"), os.date("%c", attr.modification));
HelperSetWindowText(wnd:GetChild("change time"), os.date("%c", attr.change));
HelperSetWindowText(wnd:GetChild("permissions"), attr.permissions);
end

--界面修改如下





--增加浏览图片的功能



--修改lua代码,增加事件

--获取扩展名
function getextension(filename)
return filename:match(".+%.(%w+)$")
end

--鼠标进入
local function OnMouseEnterItem(window, msg, sender)
local file=HelperGetWindowText(sender:GetChild("text"));
local path = lfs.currentdir();

local f = path.."\\"..file;
local attr,err = lfs.attributes (f)
if attr== nil then
LXZMessageBox("error:"..err);
return;
end

local root = HelperGetRoot();
assert (type(attr) == "table");
local ext = getextension(file);

LXZAPI_OutputDebugStr("OnMouseEnterItem:"..f.." mode:"..attr.mode);

if attr.mode == "file" and (ext=="png" or ext=="PNG") then --如果是图片文件
LXZAPI_OutputDebugStr("OnMouseEnterItem:"..f.." ext:"..ext.." mode:"..attr.mode);
local wnd = root:GetLXZWindow ("folders:show picture");
HelperSetWindowPictureFile(wnd,f);
wnd:Show();
HelperCoroutine(function(thread)
AddWndUpdateFunc(wnd, EffectFaceOut, {from=255, End=200,step=3, old=255, hide=true}, thread);
coroutine.yield();
local texture = ILXZTexture:GetTexture(f);
if texture then
texture:RemoveTexture();
end
end);
end
end

表现如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: