Пример #1
0
        private void DrawPanel_MouseClick(object sender, MouseEventArgs e)
        {
            // If clicked location is beyond map limits --> exit method
            if (e.Location.X < mapDest.X || e.Location.X > mapDest.X + mapSrc1.Width || e.Location.Y < mapDest.Y || e.Location.Y > mapDest.Y + mapSrc1.Height)
            {
                return;
            }
            // Else you clicked within the map
            int clickedX = (int)e.Location.X - mapDest.X;
            int clickedY = (int)e.Location.Y - mapDest.Y;

            clickedXY     = PxToCoords(clickedX, clickedY, Map.Zoom);
            clickedXY[0] += mapStartXY[0];
            clickedXY[1] += mapStartXY[1];

            Debug.WriteLine($"clickedXY={clickedXY[0]},{clickedXY[1]}");

            // TODO: Make sure that edge black tiles are also ignored!

            //clickedXY = new int[] { (MapPanel_offset[0] + coords[0]) % (2 * Map.Xdim), MapPanel_offset[1] + coords[1] };  // Coordinates of clicked square

            if (e.Buttons == MouseButtons.Primary)  // Left button
            {
                // City clicked
                if (Game.GetCities.Any(city => city.X == clickedXY[0] && city.Y == clickedXY[1]))
                {
                    if (Map.ViewPieceMode)
                    {
                        Map.ActiveXY = clickedXY;
                    }
                    var cityPanel = new CityPanel(main, Game.GetCities.Find(city => city.X == clickedXY[0] && city.Y == clickedXY[1]), 658, 459); // For normal zoom
                    MainPanelLayout.Add(cityPanel, (drawPanel.Width / 2) - (cityPanel.Width / 2), (drawPanel.Height / 2) - (cityPanel.Height / 2));
                    MainPanel.Content = MainPanelLayout;
                }
                // Unit clicked
                else if (Game.GetUnits.Any(unit => unit.X == clickedXY[0] && unit.Y == clickedXY[1]))
                {
                    int clickedUnitIndex = Game.GetUnits.FindIndex(a => a.X == clickedXY[0] && a.Y == clickedXY[1]);
                    if (!Game.GetUnits[clickedUnitIndex].TurnEnded)
                    {
                        Game.SetActiveUnit(Game.GetUnits[clickedUnitIndex]);
                        Map.ViewPieceMode = false;
                        OnMapEvent?.Invoke(null, new MapEventArgs(MapEventType.SwitchViewMovePiece));
                        StartAnimation(AnimationType.Waiting);
                    }
                    else
                    {
                        //TODO: determine what happens if unit has ended turn...
                    }
                    MapViewChange(clickedXY);
                }
                // Something else clicked
                else
                {
                    if (Map.ViewPieceMode)
                    {
                        Map.ActiveXY = clickedXY;
                    }
                    MapViewChange(clickedXY);
                    OnMapEvent?.Invoke(null, new MapEventArgs(MapEventType.MapViewChanged));
                }
            }
            else    // Right click
            {
                Map.ViewPieceMode = true;
                Map.ActiveXY      = clickedXY;
                if (!Map.ViewPieceMode)
                {
                    OnMapEvent?.Invoke(null, new MapEventArgs(MapEventType.SwitchViewMovePiece));
                }
                else
                {
                    OnMapEvent?.Invoke(null, new MapEventArgs(MapEventType.MapViewChanged));
                }
                MapViewChange(clickedXY);
                StartAnimation(AnimationType.Waiting);
            }
        }