Esempio n. 1
0
    private void UpdateBuildingsPosition()
    {
        OnlineMapsTileSetControl control = OnlineMapsTileSetControl.instance;

        Bounds bounds = new Bounds();

        double tlx, tly, brx, bry;

        api.GetTopLeftPosition(out tlx, out tly);
        api.GetBottomRightPosition(out brx, out bry);

        bounds.min = new Vector3((float)tlx, (float)bry);
        bounds.max = new Vector3((float)brx, (float)tly);

        List <string> unusedKeys = new List <string>();

        bool useElevation = control.useElevation;

        foreach (KeyValuePair <string, OnlineMapsBuildingBase> building in buildings)
        {
            if (!bounds.Intersects(building.Value.boundsCoords))
            {
                unusedKeys.Add(building.Key);
            }
            else
            {
                if (useElevation)
                {
                    Vector3 newPosition = control.GetWorldPositionWithElevation(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y, tlx, tly, brx, bry);
                    building.Value.transform.position = newPosition;
                }
                else
                {
                    Vector3 newPosition = control.GetWorldPosition(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y);
                    building.Value.transform.position = newPosition;
                }
            }
        }

        List <string> usedKeys    = new List <string>();
        List <string> destroyKeys = new List <string>();

        double px, py;

        api.GetPosition(out px, out py);
        api.projection.CoordinatesToTile(px, py, api.zoom, out px, out py);

        float maxDistance = Mathf.Sqrt(Mathf.Pow(api.width / 2 / OnlineMapsUtils.tileSize, 2) + Mathf.Pow(api.height / 2 / OnlineMapsUtils.tileSize, 2)) * 2;

        foreach (KeyValuePair <string, OnlineMapsBuildingBase> building in unusedBuildings)
        {
            OnlineMapsBuildingBase value = building.Value;
            if (bounds.Intersects(value.boundsCoords))
            {
                usedKeys.Add(building.Key);
                if (useElevation)
                {
                    Vector3 newPosition = control.GetWorldPositionWithElevation(building.Value.centerCoordinates, tlx, tly, brx, bry);
                    building.Value.transform.position = newPosition;
                }
                else
                {
                    Vector3 newPosition = control.GetWorldPosition(building.Value.centerCoordinates.x, building.Value.centerCoordinates.y);
                    building.Value.transform.position = newPosition;
                }
            }
            else
            {
                double bx, by;
                api.projection.CoordinatesToTile(value.centerCoordinates.x, value.centerCoordinates.y, api.zoom, out bx, out by);
                if (OnlineMapsUtils.Magnitude(0, 0, bx - px, by - py) > maxDistance)
                {
                    destroyKeys.Add(building.Key);
                }
            }
        }

        foreach (string key in unusedKeys)
        {
            OnlineMapsBuildingBase value = buildings[key];
            value.gameObject.SetActive(false);
            unusedBuildings.Add(key, value);
            buildings.Remove(key);
        }

        foreach (string key in usedKeys)
        {
            OnlineMapsBuildingBase value = unusedBuildings[key];
            if (maxActiveBuildings > 0 && buildings.Count >= maxActiveBuildings)
            {
                break;
            }
            if (OnShowBuilding != null && !OnShowBuilding(value))
            {
                continue;
            }
            value.gameObject.SetActive(true);
            buildings.Add(key, value);
            unusedBuildings.Remove(key);
        }

        foreach (string key in destroyKeys)
        {
            OnlineMapsBuildingBase value = unusedBuildings[key];
            if (OnBuildingDispose != null)
            {
                OnBuildingDispose(value);
            }
            OnlineMapsUtils.DestroyImmediate(value.gameObject);
            unusedBuildings.Remove(key);
        }

        if (destroyKeys.Count > 0)
        {
            OnlineMaps.instance.needGC = true;
        }
    }
Esempio n. 2
0
    private static void UpdateBuildingPosition(OnlineMapsBuildingBase building, OnlineMapsTileSetControl control, double tlx, double tly, double brx, double bry)
    {
        Vector3 newPosition = control.GetWorldPositionWithElevation(building.centerCoordinates.x, building.centerCoordinates.y, tlx, tly, brx, bry);

        building.transform.position = newPosition;
    }
    private void DrawTooltipForTileset(GUIStyle style)
    {
        OnlineMapsTileSetControl tsControl = OnlineMapsTileSetControl.instance;

        double tlx, tly, brx, bry;

        map.GetCorners(out tlx, out tly, out brx, out bry);
        if (brx < tlx)
        {
            brx += 360;
        }

        foreach (OnlineMapsMarker marker in OnlineMapsMarkerManager.instance)
        {
            if (string.IsNullOrEmpty(marker.label))
            {
                continue;
            }

            double mx, my;
            marker.GetPosition(out mx, out my);

            if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) || (mx - 360 > tlx && mx - 360 < brx)) && my < tly && my > bry))
            {
                continue;
            }

            if (marker.OnDrawTooltip != null)
            {
                marker.OnDrawTooltip(marker);
            }
            else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
            {
                OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
            }
            else
            {
                Vector3 p1 = tsControl.GetWorldPositionWithElevation(mx, my, tlx, tly, brx, bry);
                Vector3 p2 = p1 + new Vector3(0, 0, tsControl.sizeInScene.y / map.height * marker.height * marker.scale);

                Vector2 screenPoint1 = tsControl.activeCamera.WorldToScreenPoint(p1);
                Vector2 screenPoint2 = tsControl.activeCamera.WorldToScreenPoint(p2);

                float yOffset = (screenPoint1.y - screenPoint2.y) * map.transform.localScale.x - 10;

                OnGUITooltip(style, marker.label, screenPoint1 + new Vector2(0, yOffset));
            }
        }

        foreach (OnlineMapsMarker3D marker in OnlineMapsMarker3DManager.instance)
        {
            if (string.IsNullOrEmpty(marker.label))
            {
                continue;
            }

            double mx, my;
            marker.GetPosition(out mx, out my);

            if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) ||
                   (mx - 360 > tlx && mx - 360 < brx)) &&
                  my < tly && my > bry))
            {
                continue;
            }

            if (marker.OnDrawTooltip != null)
            {
                marker.OnDrawTooltip(marker);
            }
            else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
            {
                OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
            }
            else
            {
                Vector3 p1 = tsControl.GetWorldPositionWithElevation(mx, my, tlx, tly, brx, bry);
                Vector3 p2 = p1 + new Vector3(0, 0, tsControl.sizeInScene.y / map.height * marker.scale);

                Vector2 screenPoint1 = tsControl.activeCamera.WorldToScreenPoint(p1);
                Vector2 screenPoint2 = tsControl.activeCamera.WorldToScreenPoint(p2);

                float yOffset = (screenPoint1.y - screenPoint2.y) * map.transform.localScale.x - 10;

                OnGUITooltip(style, marker.label, screenPoint1 + new Vector2(0, yOffset));
            }
        }
    }
    private void DrawTooltips()
    {
        if (string.IsNullOrEmpty(tooltip) && map.showMarkerTooltip != OnlineMapsShowMarkerTooltip.always)
        {
            return;
        }

        GUIStyle style = new GUIStyle(tooltipStyle);

        if (OnPrepareTooltipStyle != null)
        {
            OnPrepareTooltipStyle(ref style);
        }

        if (!string.IsNullOrEmpty(tooltip))
        {
            Vector2 inputPosition = control.GetInputPosition();

            if (tooltipMarker != null)
            {
                if (tooltipMarker.OnDrawTooltip != null)
                {
                    tooltipMarker.OnDrawTooltip(tooltipMarker);
                }
                else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
                {
                    OnlineMapsMarkerBase.OnMarkerDrawTooltip(tooltipMarker);
                }
                else
                {
                    OnGUITooltip(style, tooltip, inputPosition);
                }
            }
            else if (tooltipDrawingElement != null)
            {
                if (tooltipDrawingElement.OnDrawTooltip != null)
                {
                    tooltipDrawingElement.OnDrawTooltip(tooltipDrawingElement);
                }
                else if (OnlineMapsDrawingElement.OnElementDrawTooltip != null)
                {
                    OnlineMapsDrawingElement.OnElementDrawTooltip(tooltipDrawingElement);
                }
                else
                {
                    OnGUITooltip(style, tooltip, inputPosition);
                }
            }
        }

        if (map.showMarkerTooltip == OnlineMapsShowMarkerTooltip.always)
        {
            if (OnlineMapsControlBase.instance is OnlineMapsTileSetControl)
            {
                OnlineMapsTileSetControl tsControl = OnlineMapsTileSetControl.instance;

                double tlx, tly, brx, bry;
                map.GetCorners(out tlx, out tly, out brx, out bry);
                if (brx < tlx)
                {
                    brx += 360;
                }

                foreach (OnlineMapsMarker marker in OnlineMapsMarkerManager.instance)
                {
                    if (string.IsNullOrEmpty(marker.label))
                    {
                        continue;
                    }

                    double mx, my;
                    marker.GetPosition(out mx, out my);

                    if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) || (mx - 360 > tlx && mx - 360 < brx)) && my < tly && my > bry))
                    {
                        continue;
                    }

                    if (marker.OnDrawTooltip != null)
                    {
                        marker.OnDrawTooltip(marker);
                    }
                    else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
                    {
                        OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
                    }
                    else
                    {
                        Vector3 p1 = tsControl.GetWorldPositionWithElevation(mx, my, tlx, tly, brx, bry);
                        Vector3 p2 = p1 + new Vector3(0, 0, tsControl.sizeInScene.y / map.height * marker.height * marker.scale);

                        Vector2 screenPoint1 = tsControl.activeCamera.WorldToScreenPoint(p1);
                        Vector2 screenPoint2 = tsControl.activeCamera.WorldToScreenPoint(p2);

                        float yOffset = (screenPoint1.y - screenPoint2.y) * map.transform.localScale.x - 10;

                        OnGUITooltip(style, marker.label, screenPoint1 + new Vector2(0, yOffset));
                    }
                }

                foreach (OnlineMapsMarker3D marker in OnlineMapsMarker3DManager.instance)
                {
                    if (string.IsNullOrEmpty(marker.label))
                    {
                        continue;
                    }

                    double mx, my;
                    marker.GetPosition(out mx, out my);

                    if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) ||
                           (mx - 360 > tlx && mx - 360 < brx)) &&
                          my < tly && my > bry))
                    {
                        continue;
                    }

                    if (marker.OnDrawTooltip != null)
                    {
                        marker.OnDrawTooltip(marker);
                    }
                    else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
                    {
                        OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
                    }
                    else
                    {
                        Vector3 p1 = tsControl.GetWorldPositionWithElevation(mx, my, tlx, tly, brx, bry);
                        Vector3 p2 = p1 + new Vector3(0, 0, tsControl.sizeInScene.y / map.height * marker.scale);

                        Vector2 screenPoint1 = tsControl.activeCamera.WorldToScreenPoint(p1);
                        Vector2 screenPoint2 = tsControl.activeCamera.WorldToScreenPoint(p2);

                        float yOffset = (screenPoint1.y - screenPoint2.y) * map.transform.localScale.x - 10;

                        OnGUITooltip(style, marker.label, screenPoint1 + new Vector2(0, yOffset));
                    }
                }
            }
            else
            {
                foreach (OnlineMapsMarker marker in OnlineMapsMarkerManager.instance)
                {
                    if (string.IsNullOrEmpty(marker.label))
                    {
                        continue;
                    }

                    Rect rect = marker.screenRect;

                    if (rect.xMax > 0 && rect.xMin < Screen.width && rect.yMax > 0 && rect.yMin < Screen.height)
                    {
                        if (marker.OnDrawTooltip != null)
                        {
                            marker.OnDrawTooltip(marker);
                        }
                        else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
                        {
                            OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
                        }
                        else
                        {
                            OnGUITooltip(style, marker.label, new Vector2(rect.x + rect.width / 2, rect.y + rect.height));
                        }
                    }
                }

                if (map.control is OnlineMapsControlBase3D)
                {
                    double tlx, tly, brx, bry;
                    map.GetCorners(out tlx, out tly, out brx, out bry);
                    if (brx < tlx)
                    {
                        brx += 360;
                    }

                    foreach (OnlineMapsMarker3D marker in OnlineMapsMarker3DManager.instance)
                    {
                        if (string.IsNullOrEmpty(marker.label))
                        {
                            continue;
                        }

                        double mx, my;
                        marker.GetPosition(out mx, out my);

                        if (!(((mx > tlx && mx < brx) || (mx + 360 > tlx && mx + 360 < brx) ||
                               (mx - 360 > tlx && mx - 360 < brx)) &&
                              my < tly && my > bry))
                        {
                            continue;
                        }

                        if (marker.OnDrawTooltip != null)
                        {
                            marker.OnDrawTooltip(marker);
                        }
                        else if (OnlineMapsMarkerBase.OnMarkerDrawTooltip != null)
                        {
                            OnlineMapsMarkerBase.OnMarkerDrawTooltip(marker);
                        }
                        else
                        {
                            double mx1, my1;
                            OnlineMapsControlBase3D.instance.GetPosition(mx, my, out mx1, out my1);

                            double px = (-mx1 / map.width + 0.5) * OnlineMapsControlBase3D.instance.cl.bounds.size.x;
                            double pz = (my1 / map.height - 0.5) * OnlineMapsControlBase3D.instance.cl.bounds.size.z;

                            Vector3 offset = map.transform.rotation * new Vector3((float)px, 0, (float)pz);
                            offset.Scale(map.transform.lossyScale);

                            Vector3 p1 = map.transform.position + offset;
                            Vector3 p2 = p1 + new Vector3(0, 0, OnlineMapsControlBase3D.instance.cl.bounds.size.z / map.height * marker.scale);

                            Vector2 screenPoint1 = OnlineMapsControlBase3D.instance.activeCamera.WorldToScreenPoint(p1);
                            Vector2 screenPoint2 = OnlineMapsControlBase3D.instance.activeCamera.WorldToScreenPoint(p2);

                            float yOffset = (screenPoint1.y - screenPoint2.y) * map.transform.localScale.x - 10;

                            OnGUITooltip(style, marker.label, screenPoint1 + new Vector2(0, yOffset));
                        }
                    }
                }
            }
        }
    }