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

lae界面开发工具入门之介绍九--<lua脚本组件篇>

2016-07-03 00:00 627 查看
摘要: lae是使用c++开发的一个工具平台,采用组件模式,所见即所得、机制简单、跨平台、布局自由、内存透明、lua逻辑纯粹、自定义控件简单等优点,可以应用于PC工具软件界面,APP开发、游戏界面开发,很方便集成第三方代码,也可嵌入任何c++工程里。这里主要介绍关于lua脚本组件功能。

1、lua脚本组件,可以处理自己订阅的全局消息,或者本窗口所有接受的事件。下图可以接收OnClickItem全局事件,OnMouseEnterItem全局事件,其他事件OnLoad,OnLClickDown,OnLClickUp,OnMouseMove等自己可接收基本事件。



比如垂直滚动条控件



--



--down button



--up button



--slider



--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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lae 界面 APP 开发 lua c++ 逻辑