wisemapping-open-source/mindplot/src/main/javascript/ScreenManager.js

141 lines
4.4 KiB
JavaScript
Raw Normal View History

2010-11-20 23:43:54 +01:00
/*
* Copyright [2011] [wisemapping]
2010-11-20 23:43:54 +01:00
*
2011-01-24 01:03:12 +01:00
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
2010-11-20 23:43:54 +01:00
*
* http://www.wisemapping.org/license
2010-11-20 23:43:54 +01:00
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2010-11-20 23:43:54 +01:00
*/
2009-06-07 20:59:43 +02:00
mindplot.ScreenManager = function(width, height, divElement)
{
this._divContainer = divElement;
this._offset = {x:0,y:0};
};
mindplot.ScreenManager.prototype.setScale = function(scale)
{
$assert($defined(scale), 'Screen scale can not be null');
2009-06-07 20:59:43 +02:00
this._workspaceScale = scale;
};
2010-11-20 23:43:54 +01:00
mindplot.ScreenManager.prototype.addEventListener=function(event, listener){
2011-03-24 19:00:51 +01:00
$(this._divContainer).addEvent(event, listener);
2010-11-20 23:43:54 +01:00
};
mindplot.ScreenManager.prototype.removeEventListener=function(event, listener){
2011-03-24 19:00:51 +01:00
$(this._divContainer).removeEvent(event, listener);
2010-11-20 23:43:54 +01:00
};
2009-06-07 20:59:43 +02:00
mindplot.ScreenManager.prototype.getWorkspaceElementPosition = function(e)
{
// Retrive current element position.
var elementPosition = e.getPosition();
var x = elementPosition.x;
var y = elementPosition.y;
// Add workspace offset.
x = x - this._offset.x;
y = y - this._offset.y;
// Scale coordinate in order to be relative to the workspace. That's coordSize/size;
x = x / this._workspaceScale;
y = y / this._workspaceScale;
// Subtract div position.
/* var containerElem = this.getContainer();
var containerPosition = core.Utils.workOutDivElementPosition(containerElem);
x = x + containerPosition.x;
y = y + containerPosition.y;*/
// Remove decimal part..
return {x:x,y:y};
};
mindplot.ScreenManager.prototype.getWorkspaceIconPosition = function(e)
{
// Retrive current icon position.
var image = e.getImage();
var elementPosition = image.getPosition();
var imageSize = e.getSize();
//Add group offset
var iconGroup = e.getGroup();
var group = iconGroup.getNativeElement();
var coordOrigin=group.getCoordOrigin();
var groupSize = group.getSize();
var coordSize = group.getCoordSize();
var scale={x:coordSize.width/parseInt(groupSize.width), y:coordSize.height/parseInt(groupSize.height)};
var x = (elementPosition.x - coordOrigin.x-(parseInt(imageSize.width)/2))/scale.x;
var y = (elementPosition.y - coordOrigin.y-(parseInt(imageSize.height)/2))/scale.y;
//Retrieve iconGroup Position
var groupPosition = iconGroup.getPosition();
x = x + groupPosition.x;
y = y + groupPosition.y;
//Retrieve topic Position
var topic = iconGroup.getTopic();
var topicPosition = this.getWorkspaceElementPosition(topic);
topicPosition.x = topicPosition.x - (parseInt(topic.getSize().width)/2);
// Remove decimal part..
return {x:x+topicPosition.x,y:y+topicPosition.y};
};
mindplot.ScreenManager.prototype.getWorkspaceMousePosition = function(e)
{
// Retrive current mouse position.
var mousePosition = this._getMousePosition(e);
var x = mousePosition.x;
var y = mousePosition.y;
// Subtract div position.
var containerElem = this.getContainer();
var containerPosition = core.Utils.workOutDivElementPosition(containerElem);
x = x - containerPosition.x;
y = y - containerPosition.y;
// Scale coordinate in order to be relative to the workspace. That's coordSize/size;
x = x * this._workspaceScale;
y = y * this._workspaceScale;
// Add workspace offset.
x = x + this._offset.x;
y = y + this._offset.y;
// Remove decimal part..
return new core.Point(x, y);
};
/**
* http://www.howtocreate.co.uk/tutorials/javascript/eventinfo
*/
mindplot.ScreenManager.prototype._getMousePosition = function(event)
{
return core.Utils.getMousePosition(event);
};
mindplot.ScreenManager.prototype.getContainer = function()
{
return this._divContainer;
};
mindplot.ScreenManager.prototype.setOffset = function(x, y)
{
this._offset.x = x;
this._offset.y = y;
};