CnC_Remastered_Collection

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

MapSection.cs (3583B)


      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.ComponentModel;
     18 using System.Globalization;
     19 using System.Linq;
     20 using System.Runtime.CompilerServices;
     21 
     22 namespace MobiusEditor.Model
     23 {
     24     public class TheaterTypeConverter : TypeConverter
     25     {
     26         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
     27         {
     28             return (context is MapContext) && (sourceType == typeof(string));
     29         }
     30 
     31         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
     32         {
     33             return (context is MapContext) && (destinationType == typeof(string));
     34         }
     35 
     36         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
     37         {
     38             if (!(value is TheaterType) || !CanConvertTo(context, destinationType))
     39             {
     40                 return null;
     41             }
     42 
     43             return (value as TheaterType)?.Name;
     44         }
     45 
     46         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
     47         {
     48             if (!CanConvertFrom(context, value?.GetType()))
     49             {
     50                 return null;
     51             }
     52 
     53             var map = (context as MapContext).Map;
     54             return map.TheaterTypes.Where(t => t.Equals(value)).FirstOrDefault() ?? map.TheaterTypes.First();
     55         }
     56     }
     57 
     58     public class MapSection : INotifyPropertyChanged
     59     {
     60         public event PropertyChangedEventHandler PropertyChanged;
     61 
     62         private int x;
     63         [DefaultValue(0)]
     64         public int X { get => x; set => SetField(ref x, value); }
     65 
     66         private int y;
     67         [DefaultValue(0)]
     68         public int Y { get => y; set => SetField(ref y, value); }
     69 
     70         private int width;
     71         [DefaultValue(0)]
     72         public int Width { get => width; set => SetField(ref width, value); }
     73 
     74         private int height;
     75         [DefaultValue(0)]
     76         public int Height { get => height; set => SetField(ref height, value); }
     77 
     78         private TheaterType theater;
     79         [TypeConverter(typeof(TheaterTypeConverter))]
     80         [DefaultValue(null)]
     81         public TheaterType Theater { get => theater; set => SetField(ref theater, value); }
     82 
     83         protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
     84         {
     85             if (EqualityComparer<T>.Default.Equals(field, value))
     86             {
     87                 return false;
     88             }
     89             field = value;
     90             OnPropertyChanged(propertyName);
     91             return true;
     92         }
     93 
     94         protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     95     }
     96 }