CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

CellMetrics.cs (4694B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // The Command & Conquer Map Editor and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // The Command & Conquer Map Editor and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 using System;
     16 using System.Collections.Generic;
     17 using System.Drawing;
     18 using System.Linq;
     19 using System.Text;
     20 using System.Threading.Tasks;
     21 
     22 namespace MobiusEditor.Model
     23 {
     24     public class CellMetrics
     25     {
     26         public static readonly FacingType[] AdjacentFacings = new FacingType[] {
     27             FacingType.North, FacingType.NorthEast, FacingType.East, FacingType.SouthEast, FacingType.South, FacingType.SouthWest, FacingType.West, FacingType.NorthWest
     28         };
     29 
     30         public int Width { get; private set; }
     31 
     32         public int Height { get; private set; }
     33 
     34         public Point TopLeft => Point.Empty;
     35 
     36         public Size Size => new Size(Width, Height);
     37 
     38         public Rectangle Bounds => new Rectangle(TopLeft, Size);
     39 
     40         public int Length => Width * Height;
     41 
     42         public bool Contains(Point location) => ((location.X >= 0) && (location.X < Width) && (location.Y >= 0) && (location.Y < Height));
     43 
     44         public bool Contains(int cell) => ((cell >= 0) && (cell < Length));
     45 
     46         public CellMetrics(int width, int height)
     47         {
     48             Width = width;
     49             Height = height;
     50         }
     51 
     52         public CellMetrics(Size size)
     53             : this(size.Width, size.Height)
     54         {
     55         }
     56 
     57         public bool GetCell(Point location, out int cell)
     58         {
     59             cell = (location.Y * Width) + location.X;
     60             return Contains(location);
     61         }
     62 
     63         public bool GetLocation(int cell, out Point location)
     64         {
     65             location = new Point(cell % Width, cell / Width);
     66             return Contains(cell);
     67         }
     68 
     69         public bool Adjacent(Point location, FacingType facing, out Point adjacent)
     70         {
     71             adjacent = location;
     72             switch (facing)
     73             {
     74                 case FacingType.North:
     75                     adjacent.Y--;
     76                     break;
     77                 case FacingType.NorthEast:
     78                     adjacent.X++;
     79                     adjacent.Y--;
     80                     break;
     81                 case FacingType.East:
     82                     adjacent.X++;
     83                     break;
     84                 case FacingType.SouthEast:
     85                     adjacent.X++;
     86                     adjacent.Y++;
     87                     break;
     88                 case FacingType.South:
     89                     adjacent.Y++;
     90                     break;
     91                 case FacingType.SouthWest:
     92                     adjacent.X--;
     93                     adjacent.Y++;
     94                     break;
     95                 case FacingType.West:
     96                     adjacent.X--;
     97                     break;
     98                 case FacingType.NorthWest:
     99                     adjacent.X--;
    100                     adjacent.Y--;
    101                     break;
    102             }
    103 
    104             return Contains(adjacent);
    105         }
    106 
    107         public bool Adjacent(int cell, FacingType facing, out int adjacent)
    108         {
    109             if (!GetLocation(cell, out Point location) || !Adjacent(location, facing, out Point adjacentPoint))
    110             {
    111                 adjacent = -1;
    112                 return false;
    113             }
    114             else
    115             {
    116                 return GetCell(adjacentPoint, out adjacent);
    117             }
    118         }
    119 
    120         public void Clip(ref Point location)
    121         {
    122             location.X = Math.Max(0, Math.Min(Width - 1, location.X));
    123             location.Y = Math.Max(0, Math.Min(Height - 1, location.Y));
    124         }
    125 
    126         public void Clip(ref Point location, Size margin)
    127         {
    128             Clip(ref location, margin, margin);
    129         }
    130 
    131         public void Clip(ref Point location, Size topLeftMargin, Size bottomRightMargin)
    132         {
    133             location.X = Math.Max(topLeftMargin.Width, Math.Min(Width - bottomRightMargin.Width - 1, location.X));
    134             location.Y = Math.Max(topLeftMargin.Height, Math.Min(Height - bottomRightMargin.Height - 1, location.Y));
    135         }
    136     }
    137 }