BasicSection.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using MobiusEditor.Utility;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Globalization;
  19. using System.Runtime.CompilerServices;
  20. namespace MobiusEditor.Model
  21. {
  22. public class BooleanTypeConverter : TypeConverter
  23. {
  24. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  25. {
  26. return (context is MapContext) && (sourceType == typeof(string));
  27. }
  28. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  29. {
  30. return (context is MapContext) && (destinationType == typeof(string));
  31. }
  32. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  33. {
  34. if (!(value is bool boolean) || !CanConvertTo(context, destinationType))
  35. {
  36. return null;
  37. }
  38. return boolean ? "1" : "0";
  39. }
  40. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  41. {
  42. if (!(value is string str) || !CanConvertFrom(context, value?.GetType()))
  43. {
  44. return null;
  45. }
  46. var first = (str.Length > 0) ? str.ToUpper()[0] : 0;
  47. return (first == 'T') || (first == 'Y') || (first == '1');
  48. }
  49. }
  50. public class PercentageTypeConverter : TypeConverter
  51. {
  52. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  53. {
  54. return (context is MapContext) && (sourceType == typeof(string));
  55. }
  56. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  57. {
  58. return (context is MapContext) && (destinationType == typeof(string));
  59. }
  60. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  61. {
  62. if (!(value is int percent) || !CanConvertTo(context, destinationType))
  63. {
  64. return null;
  65. }
  66. var mapContext = context as MapContext;
  67. return mapContext.FractionalPercentages ? (percent / 100M).ToString("D2") : percent.ToString();
  68. }
  69. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  70. {
  71. if (!(value is string str) || !CanConvertFrom(context, value?.GetType()))
  72. {
  73. return null;
  74. }
  75. var mapContext = context as MapContext;
  76. if (mapContext.FractionalPercentages && str.Contains("."))
  77. {
  78. if (!decimal.TryParse(str, out decimal percent))
  79. {
  80. return null;
  81. }
  82. return (int)(percent * 100);
  83. }
  84. else
  85. {
  86. if (!int.TryParse(str, out int percent))
  87. {
  88. return null;
  89. }
  90. return percent;
  91. }
  92. }
  93. }
  94. public class BasicSection : INotifyPropertyChanged
  95. {
  96. public event PropertyChangedEventHandler PropertyChanged;
  97. private string name;
  98. [DefaultValue(null)]
  99. public string Name { get => name; set => SetField(ref name, value); }
  100. private int carryOverCap;
  101. [TypeConverter(typeof(PercentageTypeConverter))]
  102. [DefaultValue(-1)]
  103. public int CarryOverCap { get => carryOverCap; set => SetField(ref carryOverCap, value); }
  104. private int carryOverMoney;
  105. [TypeConverter(typeof(PercentageTypeConverter))]
  106. [DefaultValue(100)]
  107. public int CarryOverMoney { get => carryOverMoney; set => SetField(ref carryOverMoney, value); }
  108. private string intro;
  109. [DefaultValue(null)]
  110. public string Intro { get => intro; set => SetField(ref intro, value); }
  111. private string theme;
  112. [DefaultValue("No Theme")]
  113. public string Theme { get => theme; set => SetField(ref theme, value); }
  114. private int percent;
  115. [TypeConverter(typeof(PercentageTypeConverter))]
  116. [DefaultValue(100)]
  117. public int Percent { get => percent; set => SetField(ref percent, value); }
  118. public string player;
  119. [DefaultValue(null)]
  120. public string Player { get => player; set => SetField(ref player, value); }
  121. private string action;
  122. [DefaultValue("x")]
  123. public string Action { get => action; set => SetField(ref action, value); }
  124. private string lose;
  125. [DefaultValue("x")]
  126. public string Lose { get => lose; set => SetField(ref lose, value); }
  127. private string win;
  128. [DefaultValue("x")]
  129. public string Win { get => win; set => SetField(ref win, value); }
  130. private string brief;
  131. [DefaultValue("x")]
  132. public string Brief { get => brief; set => SetField(ref brief, value); }
  133. private string author;
  134. [DefaultValue(null)]
  135. public string Author { get => author; set => SetField(ref author, value); }
  136. private string basePlayer;
  137. [NonSerializedINIKey]
  138. [DefaultValue(null)]
  139. public string BasePlayer { get => basePlayer; set => SetField(ref basePlayer, value); }
  140. private bool soloMission;
  141. [DefaultValue(false)]
  142. public bool SoloMission { get => soloMission; set => SetField(ref soloMission, value); }
  143. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  144. {
  145. if (EqualityComparer<T>.Default.Equals(field, value))
  146. {
  147. return false;
  148. }
  149. field = value;
  150. OnPropertyChanged(propertyName);
  151. return true;
  152. }
  153. protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  154. }
  155. }