CnC_Remastered_Collection

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

BasicSection.cs (6859B)


      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.Utility;
     16 using System;
     17 using System.Collections.Generic;
     18 using System.ComponentModel;
     19 using System.Globalization;
     20 using System.Runtime.CompilerServices;
     21 
     22 namespace MobiusEditor.Model
     23 {
     24     public class BooleanTypeConverter : 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 bool boolean) || !CanConvertTo(context, destinationType))
     39             {
     40                 return null;
     41             }
     42 
     43             return boolean ? "1" : "0";
     44         }
     45 
     46         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
     47         {
     48             if (!(value is string str) || !CanConvertFrom(context, value?.GetType()))
     49             {
     50                 return null;
     51             }
     52 
     53             var first = (str.Length > 0) ? str.ToUpper()[0] : 0;
     54             return (first == 'T') || (first == 'Y') || (first == '1');
     55         }
     56     }
     57 
     58     public class PercentageTypeConverter : TypeConverter
     59     {
     60         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
     61         {
     62             return (context is MapContext) && (sourceType == typeof(string));
     63         }
     64 
     65         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
     66         {
     67             return (context is MapContext) && (destinationType == typeof(string));
     68         }
     69 
     70         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
     71         {
     72             if (!(value is int percent) || !CanConvertTo(context, destinationType))
     73             {
     74                 return null;
     75             }
     76 
     77             var mapContext = context as MapContext;
     78             return mapContext.FractionalPercentages ? (percent / 100M).ToString("D2") : percent.ToString();
     79         }
     80 
     81         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
     82         {
     83             if (!(value is string str) || !CanConvertFrom(context, value?.GetType()))
     84             {
     85                 return null;
     86             }
     87 
     88             var mapContext = context as MapContext;
     89             if (mapContext.FractionalPercentages && str.Contains("."))
     90             {
     91                 if (!decimal.TryParse(str, out decimal percent))
     92                 {
     93                     return null;
     94                 }
     95                 return (int)(percent * 100);
     96             }
     97             else
     98             {
     99                 if (!int.TryParse(str, out int percent))
    100                 {
    101                     return null;
    102                 }
    103                 return percent;
    104             }
    105         }
    106     }
    107 
    108     public class BasicSection : INotifyPropertyChanged
    109     {
    110         public event PropertyChangedEventHandler PropertyChanged;
    111 
    112         private string name;
    113         [DefaultValue(null)]
    114         public string Name { get => name; set => SetField(ref name, value); }
    115 
    116         private int carryOverCap;
    117         [TypeConverter(typeof(PercentageTypeConverter))]
    118         [DefaultValue(-1)]
    119         public int CarryOverCap { get => carryOverCap; set => SetField(ref carryOverCap, value); }
    120 
    121         private int carryOverMoney;
    122         [TypeConverter(typeof(PercentageTypeConverter))]
    123         [DefaultValue(100)]
    124         public int CarryOverMoney { get => carryOverMoney; set => SetField(ref carryOverMoney, value); }
    125 
    126         private string intro;
    127         [DefaultValue(null)]
    128         public string Intro { get => intro; set => SetField(ref intro, value); }
    129 
    130         private string theme;
    131         [DefaultValue("No Theme")]
    132         public string Theme { get => theme; set => SetField(ref theme, value); }
    133 
    134         private int percent;
    135         [TypeConverter(typeof(PercentageTypeConverter))]
    136         [DefaultValue(100)]
    137         public int Percent { get => percent; set => SetField(ref percent, value); }
    138 
    139         public string player;
    140         [DefaultValue(null)]
    141         public string Player { get => player; set => SetField(ref player, value); }
    142 
    143         private string action;
    144         [DefaultValue("x")]
    145         public string Action { get => action; set => SetField(ref action, value); }
    146 
    147         private string lose;
    148         [DefaultValue("x")]
    149         public string Lose { get => lose; set => SetField(ref lose, value); }
    150 
    151         private string win;
    152         [DefaultValue("x")]
    153         public string Win { get => win; set => SetField(ref win, value); }
    154 
    155         private string brief;
    156         [DefaultValue("x")]
    157         public string Brief { get => brief; set => SetField(ref brief, value); }
    158 
    159         private string author;
    160         [DefaultValue(null)]
    161         public string Author { get => author; set => SetField(ref author, value); }
    162 
    163         private string basePlayer;
    164         [NonSerializedINIKey]
    165         [DefaultValue(null)]
    166         public string BasePlayer { get => basePlayer; set => SetField(ref basePlayer, value); }
    167 
    168         private bool soloMission;
    169         [DefaultValue(false)]
    170         public bool SoloMission { get => soloMission; set => SetField(ref soloMission, value); }
    171 
    172         protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    173         {
    174             if (EqualityComparer<T>.Default.Equals(field, value))
    175             {
    176                 return false;
    177             }
    178             field = value;
    179             OnPropertyChanged(propertyName);
    180             return true;
    181         }
    182 
    183         protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    184     }
    185 }