CnC_Remastered_Collection

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

TemplateType.cs (4293B)


      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 MobiusEditor.Interface;
     16 using MobiusEditor.Utility;
     17 using System;
     18 using System.Drawing;
     19 
     20 namespace MobiusEditor.Model
     21 {
     22     [Flags]
     23     public enum TemplateTypeFlag
     24     {
     25         None    = 0,
     26         Clear   = (1 << 1),
     27         Water   = (1 << 2),
     28         OreMine = (1 << 3),
     29     }
     30 
     31     public class TemplateType : IBrowsableType
     32     {
     33         public ushort ID { get; private set; }
     34 
     35         public string Name { get; private set; }
     36 
     37         public string DisplayName => Name;
     38 
     39         public int IconWidth { get; private set; }
     40 
     41         public int IconHeight { get; private set; }
     42 
     43         public Size IconSize => new Size(IconWidth, IconHeight);
     44 
     45         public int NumIcons => IconWidth * IconHeight;
     46 
     47         public bool[,] IconMask { get; set; }
     48 
     49         public Image Thumbnail { get; set; }
     50 
     51         public TheaterType[] Theaters { get; private set; }
     52 
     53         public TemplateTypeFlag Flag { get; private set; }
     54 
     55         public TemplateType(ushort id, string name, int iconWidth, int iconHeight, TheaterType[] theaters, TemplateTypeFlag flag)
     56         {
     57             ID = id;
     58             Name = name;
     59             IconWidth = iconWidth;
     60             IconHeight = iconHeight;
     61             Theaters = theaters;
     62             Flag = flag;
     63         }
     64 
     65         public TemplateType(ushort id, string name, int iconWidth, int iconHeight, TheaterType[] theaters)
     66             : this(id, name, iconWidth, iconHeight, theaters, TemplateTypeFlag.None)
     67         {
     68         }
     69 
     70         public override bool Equals(object obj)
     71         {
     72             if (obj is TemplateType)
     73             {
     74                 return this == obj;
     75             }
     76             else if (obj is byte)
     77             {
     78                 return ID == (byte)obj;
     79             }
     80             else if (obj is ushort)
     81             {
     82                 return ID == (ushort)obj;
     83             }
     84             else if (obj is string)
     85             {
     86                 return string.Equals(Name, obj as string, StringComparison.OrdinalIgnoreCase);
     87             }
     88 
     89             return base.Equals(obj);
     90         }
     91 
     92         public override int GetHashCode()
     93         {
     94             return ID.GetHashCode();
     95         }
     96 
     97         public override string ToString()
     98         {
     99             return Name;
    100         }
    101 
    102         public void Init(TheaterType theater)
    103         {
    104             var size = new Size(Globals.OriginalTileWidth / 4, Globals.OriginalTileWidth / 4);
    105             var iconSize = Math.Max(IconWidth, IconHeight);
    106             var thumbnail = new Bitmap(iconSize * size.Width, iconSize * size.Height);
    107             var mask = new bool[IconWidth, IconHeight];
    108             Array.Clear(mask, 0, mask.Length);
    109 
    110             bool found = false;
    111             using (var g = Graphics.FromImage(thumbnail))
    112             {
    113                 g.Clear(Color.Transparent);
    114 
    115                 int icon = 0;
    116                 for (var y = 0; y < IconHeight; ++y)
    117                 {
    118                     for (var x = 0; x < IconWidth; ++x, ++icon)
    119                     {
    120                         if (Globals.TheTilesetManager.GetTileData(theater.Tilesets, Name, icon, out Tile tile))
    121                         {
    122                             g.DrawImage(tile.Image, x * size.Width, y * size.Height, size.Width, size.Height);
    123                             found = mask[x, y] = true;
    124                         }
    125                     }
    126                 }
    127             }
    128 
    129             Thumbnail = found ? thumbnail : null;
    130             IconMask = mask;
    131         }
    132     }
    133 }