/* ----------------------------- * Main method *-----------------------------*/ public bool DrawMap(Rect area) { bool r = false; GUI.BeginGroup(area); area = new Rect(Vector2.zero, area.size); // update the pixel absolute to relative convert variable PATR = -(centerPixel - (area.size / 2f).ToVector2d() - area.position.ToVector2d()); var mousePos = Event.current.mousePosition.ToVector2d(); var delta = new Vector2d(Event.current.delta.x, Event.current.delta.y); if (Event.current.type != EventType.Layout) { GeoMousePosition = GM.MetersToLatLon(GM.PixelsToMeters(RelativeToAbsolute(mousePos), Zoom)); } switch (Event.current.type) { case EventType.Repaint: { // Draw the tiles DrawTiles(area); // Draw the GeoShapes DrawGeometries(area); // Draw the Resources DrawResources(area); if (selectedGeometry != null && selectedGeometry.Points.Count > 0) { var pixels = PixelsToRelative(LatLonToPixels(selectedGeometry.Points)).ConvertAll(p => p.ToVector2()); var v2mousepos = mousePos.ToVector2(); // Find the closest index var min = pixels.Min(p => (p - v2mousepos).magnitude); var closest = pixels.FindIndex(p => (p - v2mousepos).magnitude == min); // Fix the previous and next var prev = closest == 0 ? pixels.Count - 1 : closest - 1; var next = (closest + 1) % pixels.Count; // Calculate the normal to both adjacent axis to closest point var c = pixels[closest]; var v1 = (pixels[closest] - pixels[prev]).normalized; var v2 = (pixels[closest] - pixels[next]).normalized; var closestNormal = (v1 + v2).normalized; var convex = Vector3.Cross(v1, v2).z > 0; var mouseVector = (v2mousepos - c); var left = Vector3.Cross(closestNormal, mouseVector).z > 0; Handles.DrawLine(pixels[closest], v2mousepos); if ((left && convex) || (!left && !convex)) { Handles.DrawLine(pixels[prev], v2mousepos); } else { Handles.DrawLine(pixels[next], v2mousepos); } } } break; case EventType.ScrollWheel: { // Changezoom Zoom += Mathf.FloorToInt(-Event.current.delta.y / 3f); Event.current.Use(); } break; case EventType.MouseDrag: { // MoveLatLon or center var if (area.Contains(Event.current.mousePosition)) { if (selectedGeometry != null) { var pixels = LatLonToPixels(selectedGeometry.Points); // If there's a point, move the point if (selectedPoint != -1) { pixels[selectedPoint] += delta; } // Otherwise, move the pixel else { pixels = pixels.ConvertAll(p => p + delta); } // Then update the points selectedGeometry.Points = PixelsToLatLon(pixels); } else { Center = GM.MetersToLatLon(GM.PixelsToMeters(centerPixel - delta, Zoom)); } Event.current.Use(); } } break; case EventType.MouseDown: { selectedGeometry = Geometries.Find(g => { List <Vector2d> points = PixelsToRelative(LatLonToPixels(g.Points)) .ConvertAll(p => p - Event.current.mousePosition.ToVector2d()); selectedPoint = points.FindIndex(p => p.magnitude < SelectPointDistance); var marginMeters = (GM.PixelsToMeters(-PATR, Zoom) - GM.PixelsToMeters(-PATR + new Vector2d(SelectPointDistance, 0), Zoom)).magnitude; var i = g.InsideMargin(GeoMousePosition, (float)marginMeters); return(i || selectedPoint != -1); }); if (area.Contains(Event.current.mousePosition)) { GUI.FocusControl(null); r = true; } } break; } GUI.EndGroup(); return(r); }