TerrainProperties.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.Interface;
  15. using MobiusEditor.Model;
  16. using MobiusEditor.Utility;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.ComponentModel;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Windows.Forms;
  23. namespace MobiusEditor.Controls
  24. {
  25. public partial class TerrainProperties : UserControl
  26. {
  27. private bool isMockObject;
  28. public IGamePlugin Plugin { get; private set; }
  29. private Terrain terrain;
  30. public Terrain Terrain
  31. {
  32. get => terrain;
  33. set
  34. {
  35. if (terrain != value)
  36. {
  37. terrain = value;
  38. Rebind();
  39. }
  40. }
  41. }
  42. public TerrainProperties()
  43. {
  44. InitializeComponent();
  45. }
  46. public void Initialize(IGamePlugin plugin, bool isMockObject)
  47. {
  48. this.isMockObject = isMockObject;
  49. Plugin = plugin;
  50. plugin.Map.Triggers.CollectionChanged += Triggers_CollectionChanged;
  51. UpdateDataSource();
  52. Disposed += (sender, e) =>
  53. {
  54. Terrain = null;
  55. plugin.Map.Triggers.CollectionChanged -= Triggers_CollectionChanged;
  56. };
  57. }
  58. private void Triggers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  59. {
  60. UpdateDataSource();
  61. }
  62. private void UpdateDataSource()
  63. {
  64. triggerComboBox.DataSource = Trigger.None.Yield().Concat(Plugin.Map.Triggers.Select(t => t.Name).Distinct()).ToArray();
  65. }
  66. private void Rebind()
  67. {
  68. triggerComboBox.DataBindings.Clear();
  69. if (terrain == null)
  70. {
  71. return;
  72. }
  73. triggerComboBox.DataBindings.Add("SelectedItem", terrain, "Trigger");
  74. }
  75. private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
  76. {
  77. switch (e.PropertyName)
  78. {
  79. case "Type":
  80. {
  81. Rebind();
  82. }
  83. break;
  84. }
  85. if (!isMockObject)
  86. {
  87. Plugin.Dirty = true;
  88. }
  89. }
  90. private void comboBox_SelectedValueChanged(object sender, EventArgs e)
  91. {
  92. foreach (Binding binding in (sender as ComboBox).DataBindings)
  93. {
  94. binding.WriteValue();
  95. }
  96. }
  97. private void nud_ValueChanged(object sender, EventArgs e)
  98. {
  99. foreach (Binding binding in (sender as NumericUpDown).DataBindings)
  100. {
  101. binding.WriteValue();
  102. }
  103. }
  104. }
  105. public class TerrainPropertiesPopup : ToolStripDropDown
  106. {
  107. private readonly ToolStripControlHost host;
  108. public TerrainProperties TerrainProperties { get; private set; }
  109. public TerrainPropertiesPopup(IGamePlugin plugin, Terrain terrain)
  110. {
  111. TerrainProperties = new TerrainProperties();
  112. TerrainProperties.Initialize(plugin, false);
  113. TerrainProperties.Terrain = terrain;
  114. host = new ToolStripControlHost(TerrainProperties);
  115. Padding = Margin = host.Padding = host.Margin = Padding.Empty;
  116. MinimumSize = TerrainProperties.MinimumSize;
  117. TerrainProperties.MinimumSize = TerrainProperties.Size;
  118. MaximumSize = TerrainProperties.MaximumSize;
  119. TerrainProperties.MaximumSize = TerrainProperties.Size;
  120. Size = TerrainProperties.Size;
  121. Items.Add(host);
  122. TerrainProperties.Disposed += (sender, e) =>
  123. {
  124. TerrainProperties = null;
  125. Dispose(true);
  126. };
  127. }
  128. protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
  129. {
  130. base.OnClosed(e);
  131. TerrainProperties.Terrain = null;
  132. }
  133. }
  134. }