Beispiel #1
0
        /// <summary>
        /// Called on POINTUPDATED_ACK, whenever a point changes
        /// in some other running client. Updates local points respectively
        /// </summary>
        /// <param name="packet"></param>
        private void OnPointUpdatedAck(PointUpdatedAck packet)
        {
            foreach (var polygon in _polygons.Polygons)
            {
                if (!(polygon.Tag is PolyZone zone))
                {
                    continue;
                }
                if (int.Parse(zone.Id) != packet.ZoneId)
                {
                    continue;
                }

                if (packet.Added)
                {
                    var pointLatLng = new GMap.NET.PointLatLng(packet.Lat, packet.Lng);
                    polygon.Points.Insert(packet.Index, pointLatLng);
                    zone.Geometry.Insert(packet.Index, pointLatLng.ToGeometry(packet.PointId));
                }
                else if (packet.Removed)
                {
                    polygon.Points.RemoveAt(packet.Index);
                    zone.Geometry.RemoveAt(packet.Index);
                }
                else
                {
                    var i  = zone.Geometry.FindIndex(x => x.Id == packet.PointId);
                    var pt = new GMap.NET.PointLatLng(packet.Lat, packet.Lng);
                    polygon.Points[i] = pt;
                    zone.Geometry[i]  = pt.ToGeometry(packet.PointId);
                }

                UpdatePolygonLocalPosition(polygon);
                Refresh();
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets called when the cursor moves on the map
        /// </summary>
        /// <param name="e">Mouse event arguments</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (!_readOnly)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (Equals(_currentRectMarker, null))
                    {
                        _pointer.Position = FromLocalToLatLng(e.X, e.Y);

                        // Handles polygon dragging on the map
                        if (CurrentPolygon != null && CurrentPolygon.IsMouseOver)
                        {
                            for (var i = 0; i < CurrentPolygon.Points.Count; i++)
                            {
                                var pnew = new GMap.NET.PointLatLng(
                                    CurrentPolygon.Points[i].Lat + _pointer.Position.Lat - _previousMouseLocation.Lat,
                                    CurrentPolygon.Points[i].Lng + _pointer.Position.Lng - _previousMouseLocation.Lng
                                    );
                                CurrentPolygon.Points[i] = pnew;

                                var zone = (PolyZone)CurrentPolygon.Tag;
                                var id   = zone.Geometry[i].Id;

                                zone.Geometry[i] = pnew.ToGeometry(id);
                            }

                            UpdatePolygonLocalPosition(CurrentPolygon);
                        }
                    }
                    // Handles dragging a point of a polygon
                    else if (CurrentPolygon != null)
                    {
                        var pnew = FromLocalToLatLng(e.X, e.Y);

                        var pIndex = (int?)_currentRectMarker.Tag;

                        if (pIndex.HasValue && pIndex < CurrentPolygon.Points.Count)
                        {
                            CurrentPolygon.Points[pIndex.Value] = pnew;
                            var zone = (PolyZone)CurrentPolygon.Tag;

                            // TODO: Find a workaround for waiting for a server response in this
                            // non-asynch method. Currently it causes the gui to hang up
                            if (_isWaitingForResponse)
                            {
                                while (_isWaitingForResponse)
                                {
                                    Thread.Sleep(300);
                                }
                            }

                            var id = zone.Geometry[pIndex.Value].Id;

                            zone.Geometry[pIndex.Value] = pnew.ToGeometry(id);
                            UpdatePolygonLocalPosition(CurrentPolygon);

                            _pointer.Position = pnew;
                            CurrentPolygon.PointsHasChanged();
                            _currentRectMarker.Position = pnew;

                            // TODO: Remove real-time update
                            // It is very inefficent
                            //await Task.Run(() => { Sql.Instance.UpdatePoint(zone.Geometry[pIndex.Value]); });
                        }
                    }
                }
                else if (IsDrawingPolygon)
                {
                    // Handles dragging a point of a NEW polygon on the map
                    _currentNewRectMaker.Position = FromLocalToLatLng(e.X, e.Y);

                    _currentDrawingPolygon.Points[_currentDrawingPolygon.Points.Count - 1] =
                        _currentNewRectMaker.Position;
                    UpdatePolygonLocalPosition(_currentDrawingPolygon);
                }

                _previousMouseLocation = FromLocalToLatLng(e.X, e.Y);
            }
            else if (e.Button == MouseButtons.Left)
            {
                _pointer.Position = FromLocalToLatLng(e.X, e.Y);
            }

            base.OnMouseMove(e);
        }