bones脚本篇 - 模拟一个按钮
来源:互联网 发布:重庆网络推广 编辑:程序博客网 时间:2024/05/21 22:29
本篇使用image标签来模拟1个按钮的行为,要模拟按钮就要响应事件,除root以外每一个内置标签都可以响应事件。 bones的事件模型类似于dom的事件模型,事件在传递的过程中分为3个阶段 capturing target bublling,
以鼠标事件为例 a,b,c三个节点 a是b的父 b是c的父 ,当鼠标在c的节点时
事件会先传递给a 然后b 此时是capturing阶段 再传给c 此时是target阶段 再传个b然后是a此时是bublling阶段 具体的描述参照dom文档
支持的事件类型参照bones.h中BonesHandler接口
一:如何模拟一个按钮
通常按钮有4个状态 普通状态 鼠标进入的高亮状态 左键点击的 按下状态 以及禁用disable状态
按钮的xml文件如下:
<bones> <head> <link type ="pixmap" name ="common" file ="../res/common.png"></link> <link type ="pixmap" name ="highlight" file ="../res/highlight.png"></link> <link type ="pixmap" name ="press" file ="../res/press.png"></link> <link type ="pixmap" name ="disable" file ="../res/disable.png"></link> <link type ="script" module ="test" file="test.lua"></link> <style> .red { color:#ffff0000; } .pic { loc:20px 20px; size:256px 256px; } </style> </head> <body> <root id ="main" class ="red" > <image class ="pic"> <notify name ="onCreate" module ="test" func ="onCreate"></notify> <notify name ="onHitTest" module ="test" func ="onHitTest"></notify> <event name ="onMouseDown" phase ="target" module ="test" func ="onMouseDown"></event> <event name ="onMouseUp" phase ="target" module ="test" func ="onMouseUp"></event> <event name ="onMouseMove" phase ="target" module ="test" func ="onMouseMove"></event> <event name ="onMouseLeave" phase ="target" module ="test" func ="onMouseLeave"></event> <event name ="onMouseEnter" phase ="target" module ="test" func ="onMouseEnter"></event> </image> </root> </body></bones>
test.lua:
local mod = {}--control statelocal common = 1local highlight = 2local press = 3local disable = 4--buttonlocal function isEnable(self) return self.enable_endlocal function stateChanged(self) local state = common; if self:isEnable() ~= true then state = disable; elseif self.down_ then if self.hover_ then state = press; else state = highlight; end elseif self.hover_ then state = highlight; end if state ~= self.state_ then self.state_ = state; if self.state_ == common then self:setContent("common") elseif self.state_ == highlight then self:setContent("highlight") elseif self.state_ == press then self:setContent("press") elseif self.state_ == disable then self:setContent("disable") end endendfunction mod.onCreate(self)--public methodself.isEnable = isEnable--private memberself.enable_ = trueself.hover_ = falseself.down_ = falseself.state_ = disablestateChanged(self)endfunction mod.onHitTest(self, x, y) return self:isTransparent(x, y)endfunction mod.onMouseEnter(self, e)endfunction mod.onMouseMove(self, e) self.hover_ = self:contains(e:getLoc()) stateChanged(self);endfunction mod.onMouseDown(self, e) if self:isEnable() and e:isLeftMouse() then if not self.down_ then self.down_ = true stateChanged(self) end endendfunction mod.onMouseUp(self, e) local notify = false if self:isEnable() and e:isLeftMouse() then if self.down_ then self.down_ = false if self.hover_ then notify = true end stateChanged(self) end if notify then print("click") end endendfunction mod.onMouseLeave(self, e) self.hover_ = false self.down_ = false stateChanged(self)endreturn mod;
这里我们链接了4幅图片 并响应了mouse事件,具体如何响应参照lua代码, 这里用到了一个新的通知onHitTest 这个通知是用来自定义鼠标响应区域的,当鼠标在标签上的时候 onHitTest如果返回true则响应鼠标否则交给其他标签处理,这里通过isTransparent来看位图像素是否为透明 如果透明则不处理
效果图:
本篇代码下载:
http://blog.csdn.net/dalixux/article/details/48830721
0 0
- bones脚本篇 - 模拟一个按钮
- bones脚本篇 - 给按钮增加响应焦点的外观
- bones脚本篇 - 实现一个简单的列表
- bones脚本篇 - 扩展标签
- bones脚本篇 - 内置标签root
- bones脚本篇 - 内置标签image
- bones脚本篇 - 内置标签scroller
- SpriteKit在游戏中模拟一个按钮
- 一个不错的CSS模拟的按钮效果
- 在TWebbrowser 中模拟点击一个图片提交按钮
- 在TWebbrowser 中模拟点击一个图片提交按钮
- 在TWebbrowser 中模拟点击一个图片提交按钮
- 在TWebbrowser 中模拟点击一个提交按钮
- 使用脚本使一个form可以有多个普通按钮
- bones ui
- 模拟点击网页按钮
- WPF模拟点击按钮
- 模拟短信发送按钮
- 汉诺塔问题,Python
- Selection和Range对象
- markDown的简单使用
- 如何使用strace+pstack利器分析程序性能
- 【前端学习】javascript正则表达式和字符串RegExp and String(二)
- bones脚本篇 - 模拟一个按钮
- Android通话记录过滤和监听
- 查看动态链接库依赖关系的命令
- HDU 1787 GCD Again (欧拉函数)
- IOS CGContextRef 写图形跟文字
- [mysql实战] mysql基本操作 下
- android四大组件之Service个人总结
- Unity3d 性能优化篇
- UIViewController的生命周期及iOS程序执行顺序