lae界面开发工具入门之介绍九--<lua脚本组件篇>
2016-07-03 00:00
627 查看
摘要: lae是使用c++开发的一个工具平台,采用组件模式,所见即所得、机制简单、跨平台、布局自由、内存透明、lua逻辑纯粹、自定义控件简单等优点,可以应用于PC工具软件界面,APP开发、游戏界面开发,很方便集成第三方代码,也可嵌入任何c++工程里。这里主要介绍关于lua脚本组件功能。
1、lua脚本组件,可以处理自己订阅的全局消息,或者本窗口所有接受的事件。下图可以接收OnClickItem全局事件,OnMouseEnterItem全局事件,其他事件OnLoad,OnLClickDown,OnLClickUp,OnMouseMove等自己可接收基本事件。
![](http://static.oschina.net/uploads/space/2016/0703/101954_uvmk_1030910.png)
比如垂直滚动条控件
![](http://static.oschina.net/uploads/space/2016/0703/103050_8CmU_1030910.png)
--
![](http://static.oschina.net/uploads/space/2016/0703/103908_XDQK_1030910.png)
--down button
![](http://static.oschina.net/uploads/space/2016/0703/104007_82lU_1030910.png)
--up button
![](http://static.oschina.net/uploads/space/2016/0703/104136_sOLD_1030910.png)
--slider
![](http://static.oschina.net/uploads/space/2016/0703/104215_Kqia_1030910.png)
--lua代码
--父窗口为约束窗口,窗口在父窗口里移动。
local function HelperContrain(parent, child,offset, IsHorization)
local parentRc = LXZRect:new_local();
local childRc = LXZRect:new_local();
parent:GetRect(parentRc);
child:GetRect(childRc);
local pt = LXZPoint:new_local();
local parent_pt=parentRc:TopLeft();
local child_pt=childRc:TopLeft();
--reset to origin position.
if IsHorization then
pt.x = parent_pt.x-child_pt.x;
else
pt.y = parent_pt.y-child_pt.y;
end
childRc:OffsetPoint(pt);
if IsHorization then
pt.x = offset;
pt.y = 0;
else
pt.y=offset
pt.x = 0;
end
childRc:OffsetPoint(pt);
if IsHorization then
pt.y = 0;
if parentRc:Width()>=childRc:Width() then
if offset>0 then
if childRc.right> parentRc.right then
pt.x = parentRc.right-childRc.right;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("1 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
else
if childRc.left<parentRc.left then
pt.x = parentRc.left-childRc.left;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("2 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
end
else
if offset>0 then
if childRc.left> parentRc.left then
pt.x = parentRc.left-childRc.left;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("3 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
else
if childRc.right<parentRc.right then
pt.x = parentRc.right-childRc.right;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("4 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
end
end
child:SetHotPos(childRc:Center(),true);
return;
end
pt.x = 0;
if parentRc:Height()>=childRc:Height() then
if offset>0 then
if childRc.bottom> parentRc.bottom then
pt.y = parentRc.bottom-childRc.bottom;
childRc:OffsetPoint(pt);
end
else
if childRc.top<parentRc.top then
pt.y = parentRc.top-childRc.top;
childRc:OffsetPoint(pt);
end
end
else
if offset>0 then
if childRc.top> parentRc.top then
pt.y = parentRc.top-childRc.top;
childRc:OffsetPoint(pt);
end
else
if childRc.bottom<parentRc.bottom then
pt.y = parentRc.bottom-childRc.bottom;
childRc:OffsetPoint(pt);
end
end
end
child:SetHotPos(childRc:Center(),true);
end
--通过滑块位置控制窗口移动
local function VerticalScrollBySliderPosition(window, wnd)
local pt = window:GetChild("slider"):GetPos();
local size = HelperGetSliderHeight( wnd:GetParent(), wnd);
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
local stepmove = pt.y*size/len;
HelperContrain(wnd:GetParent(), wnd, -stepmove, false);
end
local function VerticalNormalizePosition(window, pt)
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
if pt.y<0 then
pt.y=0;
elseif pt.y>len then
pt.y=len;
end
window:GetChild("slider"):SetPos(pt);
end
--点击向上按钮
local function OnUp(window, msg, sender)
local cfg = window:GetCfg();
local step = cfg:GetInt("step");
local count = cfg:GetInt("count");
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local size=window:GetHeight()-window:GetChild("slider"):GetHeight();
local offset = step*size/count;
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y-offset;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
HelperPerioProc("OnUp", OnUp, window, msg, sender);
end
--点击向下按钮
local function OnDown(window, msg, sender)
local cfg = window:GetCfg();
local step = cfg:GetInt("step");
local count = cfg:GetInt("count");
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local size=window:GetHeight()-window:GetChild("slider"):GetHeight();
local offset = step*size/count;
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y+offset;
LXZAPI_OutputDebugStr("OnDown step:"..step.." count:"..count.." offset:"..offset.." size:"..size);
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
HelperPerioProc("OnDown", OnDown, window, msg, sender);
end
--鼠标滚动
local function OnVertMouseWheel(window, msg, sender)
local x = msg:int();
local y = msg:int();
local delta = msg:int();
local corecfg = ICGuiGetLXZCoreCfg();
if corecfg.IsClickDown==true then
LXZAPI_OutputDebugStr("OnVertMouseWheel 0");
return;
end
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil or wnd:IsVisible()==false then
LXZAPI_OutputDebugStr("OnVertMouseWheel 1");
return;
end
local rc = wnd:GetParent():GetRect();
if rc:IsIncludePoint(x,y)==false then
LXZAPI_OutputDebugStr("OnVertMouseWheel 2");
return;
end
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y-delta;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
end
--滑条移动
local function OnVerticalSliderMove(window, msg, sender)
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
VerticalScrollBySliderPosition(window, wnd);
end
--点击滚动条
local function OnClickVerticalBar(window, msg, sender)
local x = msg:int();
local y = msg:int();
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local rc = window:GetRect();
local size = HelperGetSliderHeight( wnd:GetParent(), wnd);
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
local stepmove = (y-rc.top)*size/len;
local pt = window:GetChild("slider"):GetPos();
pt.y = y-rc.top-window:GetChild("slider"):GetHeight()/2;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
end
--滚动条加载
local function OnVerticalLoad(window, msg, sender)
local cfg = window:GetCfg();
local page = cfg:GetInt("page");
if page==0 then
cfg:SetInt("page", -1, 10);
cfg:SetInt("step", -1, 5);
cfg:SetInt("count",-1, 100);
end
end
local event_callback = {}
event_callback ["OnLoad"] = OnVerticalLoad;
event_callback ["OnUp"] = OnUp;
event_callback ["OnDown"] = OnDown;
event_callback ["OnVertMouseWheel"] =OnVertMouseWheel;
event_callback ["OnVerticalSliderMove"] = OnVerticalSliderMove;
event_callback ["OnClickVerticalBar"] = OnClickVerticalBar;
event_callback ["OnReset"] = OnVerticalSliderMove;
function vertical_scrollbar_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
1、lua脚本组件,可以处理自己订阅的全局消息,或者本窗口所有接受的事件。下图可以接收OnClickItem全局事件,OnMouseEnterItem全局事件,其他事件OnLoad,OnLClickDown,OnLClickUp,OnMouseMove等自己可接收基本事件。
![](http://static.oschina.net/uploads/space/2016/0703/101954_uvmk_1030910.png)
比如垂直滚动条控件
![](http://static.oschina.net/uploads/space/2016/0703/103050_8CmU_1030910.png)
--
![](http://static.oschina.net/uploads/space/2016/0703/103908_XDQK_1030910.png)
--down button
![](http://static.oschina.net/uploads/space/2016/0703/104007_82lU_1030910.png)
--up button
![](http://static.oschina.net/uploads/space/2016/0703/104136_sOLD_1030910.png)
--slider
![](http://static.oschina.net/uploads/space/2016/0703/104215_Kqia_1030910.png)
--lua代码
--父窗口为约束窗口,窗口在父窗口里移动。
local function HelperContrain(parent, child,offset, IsHorization)
local parentRc = LXZRect:new_local();
local childRc = LXZRect:new_local();
parent:GetRect(parentRc);
child:GetRect(childRc);
local pt = LXZPoint:new_local();
local parent_pt=parentRc:TopLeft();
local child_pt=childRc:TopLeft();
--reset to origin position.
if IsHorization then
pt.x = parent_pt.x-child_pt.x;
else
pt.y = parent_pt.y-child_pt.y;
end
childRc:OffsetPoint(pt);
if IsHorization then
pt.x = offset;
pt.y = 0;
else
pt.y=offset
pt.x = 0;
end
childRc:OffsetPoint(pt);
if IsHorization then
pt.y = 0;
if parentRc:Width()>=childRc:Width() then
if offset>0 then
if childRc.right> parentRc.right then
pt.x = parentRc.right-childRc.right;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("1 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
else
if childRc.left<parentRc.left then
pt.x = parentRc.left-childRc.left;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("2 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
end
else
if offset>0 then
if childRc.left> parentRc.left then
pt.x = parentRc.left-childRc.left;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("3 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
else
if childRc.right<parentRc.right then
pt.x = parentRc.right-childRc.right;
childRc:OffsetPoint(pt);
LXZAPI_OutputDebugStr("4 offset x:"..pt.x.." y:"..pt.y.." offset:"..offset.." ("..childRc.left..","..childRc.top..","..childRc.right..","..childRc.bottom..")");
end
end
end
child:SetHotPos(childRc:Center(),true);
return;
end
pt.x = 0;
if parentRc:Height()>=childRc:Height() then
if offset>0 then
if childRc.bottom> parentRc.bottom then
pt.y = parentRc.bottom-childRc.bottom;
childRc:OffsetPoint(pt);
end
else
if childRc.top<parentRc.top then
pt.y = parentRc.top-childRc.top;
childRc:OffsetPoint(pt);
end
end
else
if offset>0 then
if childRc.top> parentRc.top then
pt.y = parentRc.top-childRc.top;
childRc:OffsetPoint(pt);
end
else
if childRc.bottom<parentRc.bottom then
pt.y = parentRc.bottom-childRc.bottom;
childRc:OffsetPoint(pt);
end
end
end
child:SetHotPos(childRc:Center(),true);
end
--通过滑块位置控制窗口移动
local function VerticalScrollBySliderPosition(window, wnd)
local pt = window:GetChild("slider"):GetPos();
local size = HelperGetSliderHeight( wnd:GetParent(), wnd);
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
local stepmove = pt.y*size/len;
HelperContrain(wnd:GetParent(), wnd, -stepmove, false);
end
local function VerticalNormalizePosition(window, pt)
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
if pt.y<0 then
pt.y=0;
elseif pt.y>len then
pt.y=len;
end
window:GetChild("slider"):SetPos(pt);
end
--点击向上按钮
local function OnUp(window, msg, sender)
local cfg = window:GetCfg();
local step = cfg:GetInt("step");
local count = cfg:GetInt("count");
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local size=window:GetHeight()-window:GetChild("slider"):GetHeight();
local offset = step*size/count;
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y-offset;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
HelperPerioProc("OnUp", OnUp, window, msg, sender);
end
--点击向下按钮
local function OnDown(window, msg, sender)
local cfg = window:GetCfg();
local step = cfg:GetInt("step");
local count = cfg:GetInt("count");
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local size=window:GetHeight()-window:GetChild("slider"):GetHeight();
local offset = step*size/count;
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y+offset;
LXZAPI_OutputDebugStr("OnDown step:"..step.." count:"..count.." offset:"..offset.." size:"..size);
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
HelperPerioProc("OnDown", OnDown, window, msg, sender);
end
--鼠标滚动
local function OnVertMouseWheel(window, msg, sender)
local x = msg:int();
local y = msg:int();
local delta = msg:int();
local corecfg = ICGuiGetLXZCoreCfg();
if corecfg.IsClickDown==true then
LXZAPI_OutputDebugStr("OnVertMouseWheel 0");
return;
end
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil or wnd:IsVisible()==false then
LXZAPI_OutputDebugStr("OnVertMouseWheel 1");
return;
end
local rc = wnd:GetParent():GetRect();
if rc:IsIncludePoint(x,y)==false then
LXZAPI_OutputDebugStr("OnVertMouseWheel 2");
return;
end
local pt = window:GetChild("slider"):GetPos();
pt.y = pt.y-delta;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
end
--滑条移动
local function OnVerticalSliderMove(window, msg, sender)
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
VerticalScrollBySliderPosition(window, wnd);
end
--点击滚动条
local function OnClickVerticalBar(window, msg, sender)
local x = msg:int();
local y = msg:int();
local wnd = window:GetParent():GetLXZWindow(window:GetAddString());
if wnd == nil then
return;
end
local rc = window:GetRect();
local size = HelperGetSliderHeight( wnd:GetParent(), wnd);
local len=window:GetHeight()-window:GetChild("slider"):GetHeight();
local stepmove = (y-rc.top)*size/len;
local pt = window:GetChild("slider"):GetPos();
pt.y = y-rc.top-window:GetChild("slider"):GetHeight()/2;
VerticalNormalizePosition(window,pt);
VerticalScrollBySliderPosition(window, wnd);
end
--滚动条加载
local function OnVerticalLoad(window, msg, sender)
local cfg = window:GetCfg();
local page = cfg:GetInt("page");
if page==0 then
cfg:SetInt("page", -1, 10);
cfg:SetInt("step", -1, 5);
cfg:SetInt("count",-1, 100);
end
end
local event_callback = {}
event_callback ["OnLoad"] = OnVerticalLoad;
event_callback ["OnUp"] = OnUp;
event_callback ["OnDown"] = OnDown;
event_callback ["OnVertMouseWheel"] =OnVertMouseWheel;
event_callback ["OnVerticalSliderMove"] = OnVerticalSliderMove;
event_callback ["OnClickVerticalBar"] = OnClickVerticalBar;
event_callback ["OnReset"] = OnVerticalSliderMove;
function vertical_scrollbar_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
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 个人信息安全报告发布:有 APP 每分钟调用位置权限 1468 次
- 关于指针的一些事情
- 下载量超过一亿的流行应用被发现含有恶意模块
- 如何成为一名专家级的开发人员
- c++ primer 第五版 笔记前言
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- gitinspector+jenkins 开发代码统计CI
- share_ptr的几个注意点
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记