예제 #1
0
    private void ApplyLabelTexture()
    {
        Texture2D t = new Texture2D(OnlineMapsUtils.tileSize, OnlineMapsUtils.tileSize);

        t.LoadImage(labelData);
        labelData   = null;
        labelColors = t.GetPixels32();

        if (map.control.resultIsTexture)
        {
#if !UNITY_WEBGL
            if (map.renderInThread)
            {
                OnlineMapsThreadManager.AddThreadAction(MergeColors);
            }
            else
            {
                MergeColors();
            }
#else
            MergeColors();
#endif
            OnlineMapsUtils.Destroy(t);
        }
        else
        {
            _colors = texture.GetPixels32();
            MergeColors();
            t.SetPixels32(_colors);
            texture = t;
            _colors = null;
        }
    }
예제 #2
0
    private void ApplyLabelTexture()
    {
        Texture2D t = new Texture2D(OnlineMapsUtils.tileSize, OnlineMapsUtils.tileSize);

        t.LoadImage(labelData);
        labelData   = null;
        labelColors = t.GetPixels();

        if (api.target == OnlineMapsTarget.texture)
        {
#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(MergeColors);
#else
            MergeColors();
#endif
            Object.Destroy(t);
        }
        else
        {
            _colors = texture.GetPixels();
            MergeColors();
            t.SetPixels(_colors);
            texture = t;
            _colors = null;
        }
    }
예제 #3
0
        public void Add(OnlineMapsCache cache, OnlineMapsTile tile, byte[] bytes)
        {
#if ALLOW_FILECACHE
            StringBuilder filename      = cache.GetShortTilePath(tile);
            string        shortFilename = filename.ToString();
            if (Contains(shortFilename))
            {
                return;
            }

            string fullFilename = cache.GetFullTilePath(shortFilename);

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                FileInfo fileInfo = new FileInfo(fullFilename);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                File.WriteAllBytes(fullFilename, bytes);
            });

            AddItem(shortFilename, bytes.Length);
            _size += bytes.Length;
#endif
        }
예제 #4
0
    private void OnBuildingRequestComplete(string response)
    {
        Action action = () =>
        {
            Dictionary <string, OnlineMapsOSMNode> nodes;
            List <OnlineMapsOSMWay>      ways;
            List <OnlineMapsOSMRelation> relations;

            OnlineMapsOSMAPIQuery.ParseOSMResponse(response, out nodes, out ways, out relations);

            lock (newBuildingsData)
            {
                foreach (OnlineMapsOSMWay way in ways)
                {
                    newBuildingsData.Add(new OnlineMapsBuildingsNodeData(way, nodes));
                }
            }

            sendBuildingsReceived = true;
        };

#if !UNITY_WEBGL
        OnlineMapsThreadManager.AddThreadAction(action);
#else
        action();
#endif

        if (OnRequestComplete != null)
        {
            OnRequestComplete();
        }
    }
예제 #5
0
    /// <summary>
    /// Stops the current process map generation, clears all buffers and completely redraws the map.
    /// </summary>
    public void RedrawImmediately()
    {
        OnlineMapsThreadManager.Dispose();

        if (renderInThread)
        {
            if (_buffer != null)
            {
                _buffer.Dispose();
                _buffer = null;
            }

#if NETFX_CORE
            if (renderThread != null)
            {
                renderThread.Dispose();
            }
#endif
#if !UNITY_WEBGL
            renderThread = null;
#endif
        }
        else
        {
            StartBuffer();
        }

        Redraw();
    }
예제 #6
0
    private void OnDestroy()
    {
        OnlineMapsThreadManager.Dispose();

        if (_buffer != null)
        {
            _buffer.Dispose();
            _buffer = null;
        }
#if NETFX_CORE
        if (renderThread != null)
        {
            renderThread.Dispose();
        }
#endif
#if !UNITY_WEBGL
        renderThread = null;
#endif
        _control = null;

        if (defaultColors != null && texture != null)
        {
            if (texture.width * texture.height == defaultColors.Length)
            {
                texture.SetPixels(defaultColors);
                texture.Apply();
            }
        }
    }
    public void LoadBuildingsFromOSM(string osmData)
    {
        Action action = () =>
        {
            Dictionary <string, OnlineMapsOSMNode> nodes;
            Dictionary <string, OnlineMapsOSMWay>  ways;
            List <OnlineMapsOSMRelation>           relations;

            OnlineMapsOSMAPIQuery.ParseOSMResponseFast(osmData, out nodes, out ways, out relations);

            lock (newBuildingsData)
            {
                MoveRelationsToWays(relations, ways, nodes);
            }

            sendBuildingsReceived = true;
        };

#if !UNITY_WEBGL
        if (map.renderInThread)
        {
            OnlineMapsThreadManager.AddThreadAction(action);
        }
        else
        {
            action();
        }
#else
        action();
#endif
    }
예제 #8
0
    private void OnDisable()
    {
        OnlineMapsThreadManager.Dispose();

        if (_buffer != null)
        {
            _buffer.Dispose();
            _buffer = null;
        }

#if NETFX_CORE
        if (renderThread != null)
        {
            renderThread.Dispose();
        }
#endif
#if !UNITY_WEBGL
        renderThread = null;
#endif

        if (tileManager != null)
        {
            tileManager.Dispose();
            tileManager = null;
        }

        _control = null;

        if (_instance == this)
        {
            _instance = null;
        }
    }
예제 #9
0
    /// <summary>
    /// Disposes of thread manager.
    /// </summary>
    public static void Dispose()
    {
#if !UNITY_WEBGL
        if (instance != null)
        {
            instance.isAlive = false;
            instance.thread  = null;
            instance         = null;
        }
#endif
    }
예제 #10
0
    private void OnBuildingRequestSuccess(OnlineMapsTextWebService request)
    {
        osmRequest = null;

        string response = request.response;

        if (response.Length < 300)
        {
            if (OnRequestFailed != null)
            {
                OnRequestFailed();
            }
            return;
        }

        Action action = () =>
        {
            Dictionary <string, OnlineMapsOSMNode> nodes;
            Dictionary <string, OnlineMapsOSMWay>  ways;
            List <OnlineMapsOSMRelation>           relations;

            OnlineMapsOSMAPIQuery.ParseOSMResponseFast(response, out nodes, out ways, out relations);

            lock (newBuildingsData)
            {
                MoveRelationsToWays(relations, ways, nodes);
            }

            sendBuildingsReceived = true;
        };

#if !UNITY_WEBGL
        if (map.renderInThread)
        {
            OnlineMapsThreadManager.AddThreadAction(action);
        }
        else
        {
            action();
        }
#else
        action();
#endif

        if (OnRequestComplete != null)
        {
            OnRequestComplete();
        }
    }
예제 #11
0
    /// <summary>
    /// Adds action queued for execution in a separate thread.
    /// </summary>
    /// <param name="action">Action to be executed.</param>
    public static void AddThreadAction(Action action)
    {
#if !UNITY_WEBGL
        if (instance == null)
        {
            instance = new OnlineMapsThreadManager(action);
        }
        else
        {
            instance.Add(action);
        }
#else
        throw new Exception("AddThreadAction not supported for WebGL.");
#endif
    }
예제 #12
0
    private OnlineMapsThreadManager(Action action)
    {
#if !UNITY_WEBGL
        instance      = this;
        threadActions = new List <Action>();
        Add(action);

        isAlive = true;

#if !NETFX_CORE
        thread = new Thread(StartNextAction);
#else
        thread = new OnlineMapsThreadWINRT(StartNextAction);
#endif
        thread.Start();
#endif
    }
예제 #13
0
    private void Update()
    {
        OnlineMapsThreadManager.ExecuteMainThreadActions();

        if (OnUpdateBefore != null)
        {
            OnUpdateBefore();
        }

        CheckBaseProps();
        tileManager.StartDownloading();

        if (OnUpdateLate != null)
        {
            OnUpdateLate();
        }
    }
예제 #14
0
        public void Add(OnlineMapsCache cache, string key, byte[] bytes)
        {
#if ALLOW_FILECACHE
            int hash = key.GetHashCode();

            int index = -1;
            string path = "";

            for (int i = 0; i < count; i++)
            {
                CustomCacheItem item = items[i];
                if (item.hash == hash && item.key == key)
                {
                    size -= item.size;
                    item.size = bytes.Length;
                    size += bytes.Length;

                    for (int j = i + 1; j < count; j++) items[j - 1] = items[j];
                    items[count - 1] = item;
                    path = item.GetFullPath(cache);
                    index = i;
                    break;
                }
            }

            if (index == -1)
            {
                CustomCacheItem item = AddItem(key, bytes.Length);
                path = item.GetFullPath(cache);
                size += bytes.Length;
            }

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                FileInfo fileInfo = new FileInfo(path);
                if (!Directory.Exists(fileInfo.DirectoryName)) Directory.CreateDirectory(fileInfo.DirectoryName);
                File.WriteAllBytes(path, bytes);
#if !UNITY_WEBGL
            });
#endif
#endif
        }
예제 #15
0
        public override void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int unloadSize = Mathf.RoundToInt(cache.maxCustomCacheSize * cache.customCacheUnloadRate);

            int s = 0;
            int countUnload = 0;

            while (countUnload < items.Length && s < unloadSize)
            {
                s += items[countUnload].size;
                countUnload++;
            }

            string[] unloadFiles = new string[countUnload];

            for (int i = 0; i < countUnload; i++)
            {
                CustomCacheItem item = items[i];
                size -= item.size;
                string fullFilename = item.GetFullPath(cache);
                unloadFiles[i] = fullFilename;
                items[i] = null;
            }

            for (int i = countUnload; i < count; i++) items[i - countUnload] = items[i];

            count -= countUnload;

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                for (int i = 0; i < countUnload; i++)
                {
                    string fn = unloadFiles[i];
                    if (File.Exists(fn)) File.Delete(fn);
                }
#if !UNITY_WEBGL
        });
#endif
#endif
        }
예제 #16
0
    private void OnDestroy()
    {
        OnlineMapsThreadManager.Dispose();

        if (_buffer != null)
        {
            _buffer.Dispose();
            _buffer = null;
        }
#if NETFX_CORE
        if (renderThread != null)
        {
            renderThread.Dispose();
        }
#endif
#if !UNITY_WEBGL
        renderThread = null;
#endif
        if (tileManager != null)
        {
            tileManager.Dispose();
            tileManager = null;
        }

        _control = null;

        if (defaultColors != null && texture != null)
        {
            if (texture.width * texture.height == defaultColors.Length)
            {
                texture.SetPixels(defaultColors);
                texture.Apply();
            }
        }

        OnChangePosition = null;
        OnChangeZoom     = null;
        OnMapUpdated     = null;
        OnUpdateBefore   = null;
        OnUpdateLate     = null;
    }
예제 #17
0
    private bool TryLoadFromFileCache(OnlineMapsTile tile)
    {
#if ALLOW_FILECACHE
        if (fileCacheAtlas == null)
        {
            LoadFileCacheAtlas();
        }

        StringBuilder filename      = GetShortTilePath(tile);
        string        shortFilename = filename.ToString();
        if (fileCacheAtlas.Contains(shortFilename))
        {
            string fullTilePath = GetFullTilePath(shortFilename);
            if (!File.Exists(fullTilePath))
            {
                return(false);
            }

            tile.status = OnlineMapsTileStatus.loading;

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                byte[] bytes = File.ReadAllBytes(fullTilePath);

                OnlineMapsThreadManager.AddMainThreadAction(() =>
                {
                    LoadTile(tile, bytes);
                    AddMemoryCacheItem(tile);
                    if (OnLoadedFromFileCache != null)
                    {
                        OnLoadedFromFileCache(tile);
                    }
                });
            });

            return(true);
        }
#endif
        return(false);
    }
예제 #18
0
        public void Save(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder builder = cache.GetFileCacheFolder();
            builder.Append("/").Append(atlasName);
            string filename = builder.ToString();

            FileInfo fileInfo = new FileInfo(filename);
            if (!Directory.Exists(fileInfo.DirectoryName)) Directory.CreateDirectory(fileInfo.DirectoryName);

            T[] itemsCopy = new T[items.Length];
            items.CopyTo(itemsCopy, 0);

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                FileStream stream = new FileStream(filename, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(stream);

                writer.Write((byte)'T');
                writer.Write((byte)'C');
                writer.Write(ATLAS_VERSION);

                writer.Write(size);

                for (int i = 0; i < count; i++)
                {
                    T item = itemsCopy[i];
                    writer.Write(item.key);
                    writer.Write(item.size);
                    writer.Write(item.time);
                }

                writer.Close();
#if !UNITY_WEBGL
        });
#endif
#endif
        }
예제 #19
0
        public void Save(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder builder = cache.GetFileCacheFolder();
            builder.Append("/").Append(atlasFilename);
            string filename = builder.ToString();

            FileInfo fileInfo = new FileInfo(filename);
            if (!Directory.Exists(fileInfo.DirectoryName))
            {
                Directory.CreateDirectory(fileInfo.DirectoryName);
            }

            FileCacheItem[] itemsCopy = new FileCacheItem[items.Length];
            items.CopyTo(itemsCopy, 0);

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                FileStream stream   = new FileStream(filename, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(stream);

                writer.Write((byte)'T');
                writer.Write((byte)'C');
                writer.Write(ATLAS_VERSION);

                writer.Write(_size);

                for (int i = 0; i < count; i++)
                {
                    FileCacheItem item = itemsCopy[i];
                    writer.Write(item.filename);
                    writer.Write(item.size);
                    writer.Write(item.time);
                }

                (writer as IDisposable).Dispose();
            });
#endif
        }
예제 #20
0
    private void OnDisable()
    {
        OnlineMapsThreadManager.Dispose();

        if (_buffer != null)
        {
            _buffer.Dispose();
            _buffer = null;
        }

#if NETFX_CORE
        if (renderThread != null)
        {
            renderThread.Dispose();
        }
#endif
#if !UNITY_WEBGL
        renderThread = null;
#endif

        _control         = null;
        OnChangePosition = null;
        OnChangeZoom     = null;
        OnMapUpdated     = null;
        OnMapUpdated     = null;
        OnUpdateBefore   = null;
        OnUpdateLate     = null;
        OnlineMapsTile.OnGetResourcesPath        = null;
        OnlineMapsTile.OnTileDownloaded          = null;
        OnlineMapsTile.OnTrafficDownloaded       = null;
        OnlineMapsMarkerBase.OnMarkerDrawTooltip = null;

        if (_instance == this)
        {
            _instance = null;
        }
    }
예제 #21
0
    private void OnBuildingRequestComplete(string response)
    {
        osmRequest = null;

        Action action = () =>
        {
            Dictionary <string, OnlineMapsOSMNode> nodes;
            List <OnlineMapsOSMWay>      ways;
            List <OnlineMapsOSMRelation> relations;

            OnlineMapsOSMAPIQuery.ParseOSMResponseFast(response, out nodes, out ways, out relations);

            lock (newBuildingsData)
            {
                List <string> waysInRelation = new List <string>();
                foreach (OnlineMapsOSMRelation relation in relations)
                {
                    if (relation.members.Count == 0)
                    {
                        continue;
                    }

                    OnlineMapsOSMWay way      = new OnlineMapsOSMWay();
                    List <string>    nodeRefs = new List <string>();

                    List <OnlineMapsOSMRelationMember> members = relation.members.Where(m => m.type == "way" && m.role == "outer").ToList();
                    if (members.Count == 0)
                    {
                        continue;
                    }

                    OnlineMapsOSMWay relationWay = GetWayByID(ways, members[0].reference);
                    if (relationWay == null)
                    {
                        continue;
                    }

                    nodeRefs.AddRange(relationWay.nodeRefs);
                    members.RemoveAt(0);

                    while (members.Count > 0)
                    {
                        string lastRef = nodeRefs[nodeRefs.Count - 1];

                        int memberIndex = -1;
                        for (int i = 0; i < members.Count; i++)
                        {
                            OnlineMapsOSMRelationMember member = members[i];
                            OnlineMapsOSMWay            w      = GetWayByID(ways, member.reference);
                            if (w.nodeRefs[0] == lastRef)
                            {
                                nodeRefs.AddRange(w.nodeRefs.Skip(1));
                                memberIndex = i;
                                break;
                            }
                            if (w.nodeRefs[w.nodeRefs.Count - 1] == lastRef)
                            {
                                List <string> refs = w.nodeRefs;
                                refs.Reverse();
                                nodeRefs.AddRange(refs.Skip(1));
                                memberIndex = i;
                                break;
                            }
                        }

                        if (memberIndex != -1)
                        {
                            members.RemoveAt(memberIndex);
                        }
                        else
                        {
                            break;
                        }
                    }

                    waysInRelation.AddRange(relation.members.Select(m => m.reference));
                    way.nodeRefs = nodeRefs;
                    way.id       = relation.id;
                    way.tags     = relation.tags;
                    newBuildingsData.Enqueue(new OnlineMapsBuildingsNodeData(way, nodes));
                }

                ways.RemoveAll(delegate(OnlineMapsOSMWay w)
                {
                    if (waysInRelation.Contains(w.id))
                    {
                        w.Dispose();
                        return(true);
                    }
                    return(false);
                });

                foreach (OnlineMapsOSMWay way in ways)
                {
                    newBuildingsData.Enqueue(new OnlineMapsBuildingsNodeData(way, nodes));
                }
            }

            sendBuildingsReceived = true;
        };

#if !UNITY_WEBGL
        if (map.renderInThread)
        {
            OnlineMapsThreadManager.AddThreadAction(action);
        }
        else
        {
            action();
        }
#else
        action();
#endif

        if (OnRequestComplete != null)
        {
            OnRequestComplete();
        }
    }
예제 #22
0
        public void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int countUnload = Mathf.RoundToInt(count * cache.fileCacheUnloadRate);
            if (countUnload <= 0)
            {
                throw new Exception("Can not unload a negative number of items. Check fileCacheUnloadRate.");
            }
            if (count < countUnload)
            {
                countUnload = count;
            }

            long[]   unloadTimes   = new long[countUnload];
            int[]    unloadIndices = new int[countUnload];
            string[] unloadFiles   = new string[countUnload];
            int      c             = 0;

            for (int i = 0; i < count; i++)
            {
                long t = items[i].time;
                if (c == 0)
                {
                    unloadIndices[0] = 0;
                    unloadTimes[0]   = t;
                    c++;
                }
                else
                {
                    int index  = c;
                    int index2 = index - 1;

                    while (index2 >= 0)
                    {
                        if (unloadTimes[index2] < t)
                        {
                            break;
                        }

                        index2--;
                        index--;
                    }

                    if (index < countUnload)
                    {
                        for (int j = countUnload - 1; j > index; j--)
                        {
                            unloadIndices[j] = unloadIndices[j - 1];
                            unloadTimes[j]   = unloadTimes[j - 1];
                        }
                        unloadIndices[index] = i;
                        unloadTimes[index]   = t;
                        if (c < countUnload)
                        {
                            c++;
                        }
                    }
                }
            }

            for (int i = 0; i < countUnload; i++)
            {
                int index = unloadIndices[i];
                _size -= items[index].size;
                string fullFilename = cache.GetFullTilePath(items[index].filename);
                unloadFiles[i] = fullFilename;
                items[index]   = null;
            }

            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                if (items[i] == null)
                {
                    offset++;
                }
                else if (offset > 0)
                {
                    items[i - offset] = items[i];
                }
            }

            count -= countUnload;

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                for (int i = 0; i < countUnload; i++)
                {
                    string fn = unloadFiles[i];
                    if (File.Exists(fn))
                    {
                        File.Delete(fn);
                    }
                }
            });
#endif
        }