Skyline基本操作模式封装
skyline基本操作模式
项目中基于skyline的浏览器插件进行二次开发,基本的业务操作模式如下:
- 工具栏:点击工具栏某个功能,开启操作模式。
- onFrame:鼠标移动预选对象,在能够拾取或者选定操作的Fature对象上,改变渲染色彩。
- OnLButtonUp:左键单击选定对象,在onFrame渲染对象的基础上,选定某个对象,并用不同于OnFrame的渲染色彩,再次渲染。同时,执行业务操作,查询数据并弹出窗口或者其他。
- OnRButtonUp:右击结束当前操作模式,取消事件监听、取消对象渲染、移除业务窗口等。
其中涉及到很多重复性的代码:
- 事件绑定与取消绑定
/**
* 事件绑定
* @returns {}
*/
mouseModel.Attach = function () {
mouseModel.sgworld.AttachEvent("OnFrame", mouseModel.onPointSelectOnFrame);
mouseModel.sgworld.AttachEvent("OnLButtonUp", mouseModel.onPointSelectLButtonUp);
mouseModel.sgworld.AttachEvent("OnRButtonUp", mouseModel.onPointSelectRButtonUp);
mouseModel.sgworld.Window.SetInputMode(1);
}
/**
* 解除绑定
* @returns {}
*/
mouseModel.Detach = function() {
mouseModel.sgworld.DetachEvent("OnLButtonUp", mouseModel.onPointSelectLButtonUp);
mouseModel.sgworld.DetachEvent("OnRButtonUp", mouseModel.onPointSelectRButtonUp);
mouseModel.sgworld.DetachEvent("OnFrame", mouseModel.onPointSelectOnFrame);
mouseModel.sgworld.Window.SetInputMode(0);
}
- OnRButtonUp事件处理逻辑基本一致
mouseModel.onPointSelectRButtonUp = function () {
mouseModel.Detach();
if (mouseModel.lButtonFeature != null && mouseModel.lButtonFeature.Tint != null) {
mouseModel.lButtonFeature.Tint.SetAlpha(0);
mouseModel.lButtonFeature = null;
}
if (mouseModel.frameFeature != null && mouseModel.frameFeature.Tint != null) {
mouseModel.frameFeature.Tint.SetAlpha(0);
mouseModel.frameFeature = null;
}
//可能的业务处理逻辑
...
return true;
}
- OnFrame事件处理逻辑基本一致
mouseModel.onPointSelectOnFrame = function () {
var mouseInfo = mouseModel.sgworld.Window.GetMouseInfo();
var position = mouseModel.sgworld.Window.PixelToWorld(mouseInfo.X, mouseInfo.Y);
if (position.Type === 8192 || position.Type === 4 || position.Type === 1) {
try {
if (mouseModel.frameFeature != null && (mouseModel.frameFeature.ObjectType === 33 || mouseModel.frameFeature.ObjectType === 38)) {
mouseModel.frameFeature.Tint.SetAlpha(0);
}
//标记元素不再渲染
if (mouseModel.lButtonFeature == null || position.ObjectID !== mouseModel.lButtonFeature.ID) {
mouseModel.frameFeature = sgworld.Creator.GetObject(position.ObjectID);
mouseModel.frameFeature.Tint.abgrColor = 0x8f0000f0;
}
} catch (e) {
}
}
}
- OnLButtonUp事件Feature判断处理逻辑一致
mouseModel.onPointSelectLButtonUp = function () {
var mouseInfo = mouseModel.sgworld.Window.GetMouseInfo();
var position = mouseModel.sgworld.Window.PixelToWorld(mouseInfo.X, mouseInfo.Y);
if (position.Type === 8192 || position.Type === 4 || position.Type === 1 || position.Type === 1228
|| position.Type === 12288) {
try {
if (!mouseModel.frameFeature) return true;
if (mouseModel.lButtonFeature != null && (mouseModel.lButtonFeature.ObjectType === 33 || mouseModel.lButtonFeature.ObjectType === 38)) {
mouseModel.lButtonFeature.Tint.SetAlpha(0);
}
mouseModel.lButtonFeature = mouseModel.frameFeature;
mouseModel.frameFeature = null;
//设置16进制颜色值
mouseModel.lButtonFeature.Tint.abgrColor = 0x8f00f0f0;
//业务处理
...
} catch (e) {
return false;
}
}
return true;
}
操作模式封装
因此基于项目中已经存在的大量重复代码的共性,对这种操作模式进行封装,方便部分操作逻辑的统一,以后后续扩展的便捷。
function MouseMode(args) {
var mouseModel = {};
mouseModel.sgworld = args.sgworld;
//左键处理逻辑回调
mouseModel.lButtonUpCallback = args.lButtonUpCallback;
//右键处理逻辑回调
mouseModel.rButtonUpCallback = args.rButtonUpCallback;
//左键选定Feature
mouseModel.lButtonFeature = null;
//frame预选Feature
mouseModel.frameFeature = null;
/**
* 事件绑定
* @returns {}
*/
mouseModel.Attach = function () {
mouseModel.sgworld.AttachEvent("OnFrame", mouseModel.onPointSelectOnFrame);
mouseModel.sgworld.AttachEvent("OnLButtonUp", mouseModel.onPointSelectLButtonUp);
mouseModel.sgworld.AttachEvent("OnRButtonUp", mouseModel.onPointSelectRButtonUp);
mouseModel.sgworld.Window.SetInputMode(1);
}
/**
* 解除绑定
* @returns {}
*/
mouseModel.Detach = function() {
mouseModel.sgworld.DetachEvent("OnLButtonUp", mouseModel.onPointSelectLButtonUp);
mouseModel.sgworld.DetachEvent("OnRButtonUp", mouseModel.onPointSelectRButtonUp);
mouseModel.sgworld.DetachEvent("OnFrame", mouseModel.onPointSelectOnFrame);
mouseModel.sgworld.Window.SetInputMode(0);
}
/**
* 右键处理
* @returns {}
*/
mouseModel.onPointSelectRButtonUp = function () {
mouseModel.Detach();
if (mouseModel.lButtonFeature != null && mouseModel.lButtonFeature.Tint != null) {
mouseModel.lButtonFeature.Tint.SetAlpha(0);
mouseModel.lButtonFeature = null;
}
if (mouseModel.frameFeature != null && mouseModel.frameFeature.Tint != null) {
mouseModel.frameFeature.Tint.SetAlpha(0);
mouseModel.frameFeature = null;
}
//可能的业务回调
if (mouseModel.rButtonUpCallback) {
mouseModel.rButtonUpCallback();
}
return true;
}
/**
* 左键处理
* @returns {}
*/
mouseModel.onPointSelectLButtonUp = function () {
var mouseInfo = mouseModel.sgworld.Window.GetMouseInfo();
var position = mouseModel.sgworld.Window.PixelToWorld(mouseInfo.X, mouseInfo.Y);
if (position.Type === 8192 || position.Type === 4 || position.Type === 1 || position.Type === 1228
|| position.Type === 12288) {
try {
if (!mouseModel.frameFeature) return true;
if (mouseModel.lButtonFeature != null && (mouseModel.lButtonFeature.ObjectType === 33 || mouseModel.lButtonFeature.ObjectType === 38)) {
mouseModel.lButtonFeature.Tint.SetAlpha(0);
}
mouseModel.lButtonFeature = mouseModel.frameFeature;
mouseModel.frameFeature = null;
//设置16进制颜色值
mouseModel.lButtonFeature.Tint.abgrColor = 0x8f00f0f0;
//业务回调
if (mouseModel.lButtonUpCallback) {
mouseModel.lButtonUpCallback(mouseModel.sgworld, mouseModel.lButtonFeature, mouseModel.frameFeature);
}
} catch (e) {
return false;
}
}
return true;
}
mouseModel.onPointSelectOnFrame = function () {
var mouseInfo = mouseModel.sgworld.Window.GetMouseInfo();
var position = mouseModel.sgworld.Window.PixelToWorld(mouseInfo.X, mouseInfo.Y);
if (position.Type === 8192 || position.Type === 4 || position.Type === 1) {
try {
if (mouseModel.frameFeature != null && (mouseModel.frameFeature.ObjectType === 33 || mouseModel.frameFeature.ObjectType === 38)) {
mouseModel.frameFeature.Tint.SetAlpha(0);
}
//左键选定元素不再渲染
if (mouseModel.lButtonFeature == null || position.ObjectID !== mouseModel.lButtonFeature.ID) {
mouseModel.frameFeature = sgworld.Creator.GetObject(position.ObjectID);
mouseModel.frameFeature.Tint.abgrColor = 0x8f0000f0;
}
} catch (e) {
}
}
}
mouseModel.Attach();
return mouseModel;
}
使用方式
var mouseMode = new MouseMode({
sgworld: sgworld,
lButtonUpCallback: function (sgworld, feature, lastFeature) {
//业务处理代码
},
rButtonUpCallback: function () {
//业务处理代码
}
});
这样是不是简单了许多,这里统一了onFrame和LButtonUp时Feature渲染色彩,开放了LButtonUp和RButtonUp时业务处理逻辑。
其实,这个封装之前就写好了,但是奈何skyline调试的复杂性,有几个问题一直没有解决,今天再次翻出来,一一搞定,真的是时间是解决一切问题的利器呀,还是凉拌好。终于删除一大堆我讨厌的代码了!!!
Skyline基本操作模式封装的更多相关文章
- HeadFirst设计模式笔记:(六)命令模式 —— 封装调用
1.概念 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化与不变的因素. 在面向对象的程序 ...
- 【Android】策略模式封装百度地图路线规划模块
百度地图的Demo里有个路线规划的功能,但是,这个功能和Activity耦合性太高,所以需要单独抽离出路径规划功能,进行"解耦". 注:由于项目原因,本文只针对驾车路线规划进行封装 ...
- js原生设计模式——9外观模式封装
1.事件处理程序兼容性封装 <!DOCTYPE html><html lang="en"><head> <meta charset= ...
- js原生设计模式——3简单工厂模式\简单工厂模式封装简单对象
1.Factory基本写法 <!DOCTYPE html><html lang="en"><head> <meta charset= ...
- PO页面对象模式封装
PO的主要价值体现在对界面交互细节的封装,这样可以使测试案例可以更关注与业务而非界面细节,提高测试案例的可读性. 以传统的登陆页面为例實現PO模式,因为每个用例中都需要登陆. 其中需要使用Page ...
- Facade 门面模式 封装 MD
门面模式 简介 作用:封装系统功能,简化系统调用 门面模式要求一个系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行.门面模式提供一个高层次的接口,使得系统更易于使用. 门面模式的 ...
- 使用.net中的API网关模式封装微服务
在本文中,我们将了解如何使用API网关模式来封装微服务并抽象出底层实现细节,从而允许使用者拥有进入我们系统的一致入口点. 为了构建和测试我们的应用程序,我们需要: 1.Visual Studio 20 ...
- js原生设计模式——9外观模式封装2(小型代码库YJ)
<script type="text/javascript"> //小型代码库YJ封装 var YJ = { //根据id获取元素 ...
- Wpf+数据库代码封装+策略模式封装
运行界面: 数据库保存的题: 数据库封装代码: using System; using System.Collections.Generic; using System.Linq; using Sys ...
随机推荐
- Python爬虫入门教程 60-100 python识别验证码,阿里、腾讯、百度、聚合数据等大公司都这么干
常见验证码 之前的博客中已经解决了一些常见验证码的问题,但是验证码是层出不穷的,目前解决验证码除了通过常规手段解决以外,还可以通过人工智能领域的深度学习去解决 深度学习?! 无疑对爬虫coder提高了 ...
- 《前端之路》之 webpack 4.0+ 的应用构建
目录 一.版本 二.webpack 的主体概念 2-1.入口 2-1-1.单页面入口 2-1-2.多页面应用的入口 2-2.输出 2-3.loader 2-4.plugins 三.如何使用 3-1 关 ...
- TFS线上生成环境发布历程
继前文 TFS在项目中Devops落地进程(上) TFS在项目中DevOps落地进程(下) 自从之前将开发环境使用TFS进行了自动化之后,就享受在此成果中,其他后续进度就停顿了好一段时间. 毕竟在我们 ...
- 使用JS+Three.js+Echart开发商场室内地图客流信息统计功能
现在的商场管理者在管理商场的同时面临着一些无法避免的问题比如:人员监管不到位.效率低下.商场同质化严重,人流量少等.发现了这些问题作为开发人员的我们怎能视而不见,我们的责任就是发现问题解决问题,提供更 ...
- 图解 sql 事务隔离级别
sql 事务隔离级别有四种分种为: 一 Read Uncpommitted(未提交读) 二 Read Committed(提交读) 三 Repeated Read(可重复读) 四 Serializab ...
- 微服务容错限流Hystrix入门
为什么需要容错限流 复杂分布式系统通常有很多依赖,如果一个应用不能对来自依赖 故障进行隔离,那么应用本身就处在被拖垮的风险中.在一个高流量的网站中,某个单一后端一旦发生延迟,将会在数秒内导致 所有应用 ...
- 设计模式 | 抽象工厂模式(abstract factory)
定义: 提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. 结构:(书中图,侵删) 这个图相对来说有一点点复杂,其实就是在工厂方法模式的基础上做了一些扩展,工厂方法模式只用于生成一种 ...
- 5分钟解决google play上架App设置隐私政策声明问题
本文同步自javaexception 问题: 在我们的app上架到google play后,为了赚点小钱,就集成google ads,然而这会引发一个新的问题,那就是设置隐私政策声明的问题,通常我们会 ...
- 调研pwa和sw
概述 处于好奇,最近我调研了一下pwa和service worker,有些新的,记录下来,供以后开发时参考,相信对其他人也有用.pwa主要是通过service worker实现的,它主要包括桌面图标, ...
- dropload.js(上拉加载插件使用过程中遇到的坑)
dropload.js相关介绍和使用以及demo下载详见:https://github.com/ximan/dropload (原文出处) 之前因为项目需要一个上拉加载的效果,然后无意中看到了此插件, ...