項目中基於skyline的瀏覽器插件進行二次開發,基本的業務操作模式基本上是:點擊功能按鈕開啟操作模式,onFrame預選Feature,onLButtonUp選定Feature並執行業務操作,OnRButtonUp結束操作模式等。這裡封裝了這個基本操作模式,避免代碼重覆,便捷功能擴展和統一。 ...
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調試的複雜性,有幾個問題一直沒有解決,今天再次翻出來,一一搞定,真的是時間是解決一切問題的利器呀,還是涼拌好。終於刪除一大堆我討厭的代碼了!!!