MapSection.cs 3.5 KB

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