/*---------------- FILE HEADER --------------------------------------- This file is part of Geoide. Copyright (C) 2005-2006 by: IDgis B.V. http://www.idgis.nl This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Contact: Herman Assink IDgis bv P.O. Box 15 7450 AA Holten The Netherlands E-Mail: herman.assink@idgis.nl * @version 1.4.0 * @author IDgis team * ------------------------------------------------------------------------*/ import nl.idgis.giclient.geoma.Geometry; import nl.idgis.giclient.geoma.LineSegment; import nl.idgis.giclient.geoma.LineString; import nl.idgis.giclient.geoma.NullGeometry; import nl.idgis.giclient.geoma.Point; import nl.idgis.giclient.geoma.Polygon; import nl.idgis.giclient.gui.mapviewer.GeometryLayer; import nl.idgis.giclient.gis.GIS; import nl.idgis.giclient.gis.ZoomListener; import nl.idgis.giclient.util.Pixel; import nl.idgis.giclient.config.Colors; import nl.idgis.giclient.geoma.Envelope; import nl.idgis.giclient.gui.mapviewer.MapViewer; import nl.idgis.giclient.gui.mapviewer.MapIcon; class nl.idgis.giclient.gui.mapviewer.MapGeometry extends MovieClip implements ZoomListener { static var NORMAL:Number = 0; static var ACTIVE:Number = 1; static var SELECTION:Number = 2; static var BUFFER:Number = 3; private var geometry:Geometry = null; // Set by init object. private var annotation:String = null; // Set by init object. private var type:Number = -1; // Set by init object. private var color:Number = -1; // Set by init object. private var labelText:String = null; // Set by init object. private var gism:GIS = null; // Set by init object. private var geometryLayer:GeometryLayer = null; // Set by init object. private var mapGeometries:Array = null; private var numMapGeometries:Number = 0; private var movingPoint:Boolean = false; var fromPixel:Pixel; var toPixel:Pixel; private var mapIcon:MapIcon = null; private var textFormat:TextFormat = null; private var haloTextFormat:TextFormat = null; function onLoad():Void { gism.gisEventDispatcher.addZoomListener(this); if (type == ACTIVE) { setMapGeometries(); } textFormat = new TextFormat(); textFormat.font = "_sans"; textFormat.size = 12; textFormat.bold = true; textFormat.color = Colors.editTextColor; haloTextFormat = new TextFormat(); haloTextFormat.font = "_sans"; haloTextFormat.size = 12; haloTextFormat.bold = true; haloTextFormat.color = Colors.currentTextColor; draw(); } function onZoom():Void { draw(); } function setMapIcon(mapIcon:MapIcon) { this.mapIcon = mapIcon; } function onChangeGeometry(eventType:String):Void { if(mapIcon != null) { mapIcon.onChangeGeometry(); } if (eventType == null) { draw(); drawSubs(); // TODO } else if (eventType == "ADDREMOVE") { removeMapGeometries(); setMapGeometries(); } // if (geometry == null) { // this.removeMovieClip(); // TODO notify geometrylayer // } } // TODO EVENT MODEL function drawSubs():Void { for (var i:String in mapGeometries) { MapGeometry(mapGeometries[i]).draw(); MapGeometry(mapGeometries[i]).drawSubs(); } } function getGeometry():Geometry { return geometry; } function setType(type:Number):Void { if (this.type == type) { return; } if ((this.type == SELECTION) || (type == SELECTION) || (this.type == BUFFER) || (type == BUFFER)){ // EXCEPTION return; } this.type = type; if (type == ACTIVE) { // From NORMAL to ACTIVE. setMapGeometries(); } else { // From ACTIVE to NORMAL. removeMapGeometries(); } draw(); } function setColor(color:Number):Void { if (this.color == color) { return; } this.color = color; for (var i:String in mapGeometries) { MapGeometry(mapGeometries[i]).setColor(color); } draw(); } function setAnnotationText(annotation:String):Void { if (this.annotation == annotation) { return; } this.annotation = annotation; setAnnotation(); } function setLabelText(labelText:String):Void { if (this.labelText == labelText) { return; } this.labelText = labelText; setLabel(); } private function setMapGeometries():Void { mapGeometries = new Array(); var geometries:Array = geometry.getGeometries(); for (var i:Number = 0; i < geometries.length; i++) { addMapGeometry(Geometry(geometries[i]), geometries.length - 1 - i); } } private function addMapGeometry(geometry:Geometry, depth:Number):Void { if (getMapGeometry(geometry) != null) { // EXCEPTION return; } var mapGeometry:MapGeometry = null; var initObject:Object = new Object(); initObject["geometry"] = geometry; initObject["type"] = type; initObject["color"] = color; if (this instanceof nl.idgis.giclient.gui.mapviewer.MapGeometryCollection) { initObject["labelText"] = labelText; } else { initObject["labelText"] = null; } initObject["gism"] = gism; initObject["geometryLayer"] = geometryLayer; if (geometry instanceof Point) { mapGeometry = MapGeometry(attachMovie("MapPoint", "mapPoint" + numMapGeometries, depth + 1, initObject)); } else if (geometry instanceof LineSegment) { mapGeometry = MapGeometry(attachMovie("MapLineSegment", "mapLineSegment" + numMapGeometries, depth + 1, initObject)); } else if (geometry instanceof LineString) { mapGeometry = MapGeometry(attachMovie("MapLineString", "mapLineString" + numMapGeometries, depth + 1, initObject)); } else if (geometry instanceof Polygon) { mapGeometry = MapGeometry(attachMovie("MapPolygon", "mapPolygon" + numMapGeometries, depth + 1, initObject)); } mapGeometries.push(mapGeometry); numMapGeometries++; } function removeMapGeometries():Void { for (var i:String in mapGeometries) { MapGeometry(mapGeometries[i]).removeMovieClip(); } mapGeometries = new Array(); } function removeMapGeometry(geometry:Geometry):Void { var mapGeometry:MapGeometry = null; for (var i:Number = 0; i < mapGeometries.length; i++) { mapGeometry = MapGeometry(mapGeometries[i]); if (mapGeometry.getGeometry() == geometry) { mapGeometries.splice(i, 1); return; } } } function getMapGeometries():Array { return mapGeometries; } function getMapGeometry(geometry:Geometry):MapGeometry { var mapGeometry:MapGeometry = null; for (var i:String in mapGeometries) { mapGeometry = MapGeometry(mapGeometries[i]); if (mapGeometry.getGeometry() == geometry) { return mapGeometry; } } return null; } function performEvent(eventObject:Object):MapGeometry { return null; } private function passEvent(eventObject):MapGeometry { var pixel:Pixel = Pixel(eventObject["pixel"]); var x:Number = pixel.getX(); var y:Number = pixel.getY(); var mapGeometry:MapGeometry = null; for (var i:Number = 0; i < mapGeometries.length; i++) { mapGeometry = MapGeometry(mapGeometries[i]); //var pixel:Pixel = Pixel(eventObject["pixel"]); //var x:Number = pixel.getX(); //var y:Number = pixel.getY(); if (mapGeometry.hitTest(x, y, true)) { //(use overridden hittest for MapPolygon) mapGeometry = mapGeometry.performEvent(eventObject); break; // Pass to first-found only. } else { mapGeometry = null; } } if ((mapGeometry == null) && (this.hitTest(x, y, true))) { mapGeometry = this; } return mapGeometry; } private function draw():Void { if ((geometry == null) || (geometry instanceof NullGeometry)) { return; } var mapViewer:MapViewer = geometryLayer.getMapViewer(); var envelope:Envelope = mapViewer.ruler.getGIS().getCurrentCentreScale().toEnvelope(Stage.width, Stage.height, mapViewer.ruler.getGIS().getCoordPixFactor()); var clip:Geometry = null; //if ((geometry instanceof LineSegment) || (geometry instanceof LineString)) { // clip = geometry.clip(envelope); //} else { clip = geometry; //} if (clip instanceof NullGeometry) { return; } /* var envelope:Envelope = geometry.getEnvelope(); var minPixel:Pixel = mapViewer.point2Pixel(envelope.getMinX(), envelope.getMinY()); var maxPixel:Pixel = mapViewer.point2Pixel(envelope.getMaxX(), envelope.getMaxY()); var centerPixel:Pixel = new Pixel(mapViewer.mapWidth / 2, mapViewer.mapHeight / 2); // 1638 omdat totale maximum lengte = 2^16 in twips; vanaf centerpunt dus 2^15 in twips; 20 twips is 1 pixel dus 32768 / 20 if ((minPixel.getX() < centerPixel.getX() - 7000) || (minPixel.getY() < centerPixel.getY() - 7000) || (maxPixel.getX() > centerPixel.getX() + 7000) || (maxPixel.getY() > centerPixel.getY() + 7000)) { clear(); this["idName"].removeMovieClip(); throw new Error("The geometry is too big to draw and needs to be clipped."); return; }*/ if (type == NORMAL) { drawAsNormal(clip, Colors.editColor,Colors.editFillOpacity,1); } else if (type == ACTIVE) { geometryLayer.moveMapGeometryFrontward(this); drawAsActive(clip); } else if (type == SELECTION) { if(color != null){ drawAsNormal(clip, color,Colors.selectionFillOpacity,1); } else { drawAsNormal(clip, Colors.selectionColor,Colors.selectionFillOpacity,1); } } else {//BUFFER drawAsNormal(clip, Colors.selectionGeometryColor,Colors.selectionGeometryFillOpacity,1); } setLabel(); setAnnotation(); } private function drawAsNormal(geometry:Geometry, color:Number,fillOpacity:Number,lineThickNess:Number):Void { clear(); } private function drawAsActive(geometry:Geometry):Void { clear(); } private function setAnnotation():Void { if(this.annotation == null) { return; } var centerPoint:Point = geometry.getCenterPoint(); var centerPixel:Pixel = geometryLayer.getMapViewer().point2Pixel(centerPoint.getX(), centerPoint.getY()); var x:Number = centerPixel.getX(); var y:Number = centerPixel.getY() + 15; addText("annotationText", this.annotation, x, y); } private function addText(name:String, text:String, x:Number, y:Number):Void { var textFieldWidth:Number = textFormat.getTextExtent(text).textFieldWidth; var textFieldHeight:Number = textFormat.getTextExtent(text).textFieldHeight; x = x - (textFieldWidth / 2); y = y; // - (textFieldHeight / 2); (position text above point if (this[name] != null) { TextField(this[name]).removeTextField(); } var textField:TextField = this.createTextField(name, this.getNextHighestDepth(), x, y, textFieldWidth, textFieldHeight); textField._x = x; textField._y = y; textField._visible = true; textField.setNewTextFormat(textFormat); textField.multiline = true; textField.text = text; } private function setLabel():Void { if ((labelText == null) || (labelText == "")) { return; } var centerPoint:Point = geometry.getCenterPoint(); var centerPixel:Pixel = geometryLayer.getMapViewer().point2Pixel(centerPoint.getX(), centerPoint.getY()); var x:Number = centerPixel.getX(); var y:Number = centerPixel.getY(); addText("idText", labelText, x, y); /*var centerPoint:Point = geometry.getCenterPoint(); var centerPixel:Pixel = geometryLayer.getMapViewer().point2Pixel(centerPoint.getX(), centerPoint.getY()); var x:Number = centerPixel.getX(); var y:Number = centerPixel.getY(); var textFormat:TextFormat = new TextFormat(); textFormat.font = "_sans"; textFormat.size = 12; textFormat.bold = true; //textFormat.color = color; textFormat.color = Colors.editTextColor; var textFieldWidth:Number = textFormat.getTextExtent(labelText).textFieldWidth; var textFieldHeight:Number = textFormat.getTextExtent(labelText).textFieldHeight; x = x - (textFieldWidth / 2); y = y; // - (textFieldHeight / 2); (position text above point //if (this["idName"] == null) { createTextField("idName", 0, x, y, textFieldWidth, textFieldHeight); this["idName"]._x = x; this["idName"]._y = y; this["idName"]._visible = true; this["idName"].setNewTextFormat(textFormat); this["idName"].text = labelText;*/ } }