ObjectProperties.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Specialized;
  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 ObjectProperties : UserControl
  26. {
  27. private bool isMockObject;
  28. public IGamePlugin Plugin { get; private set; }
  29. private INotifyPropertyChanged obj;
  30. public INotifyPropertyChanged Object
  31. {
  32. get => obj;
  33. set
  34. {
  35. if (obj != value)
  36. {
  37. if (obj != null)
  38. {
  39. obj.PropertyChanged -= Obj_PropertyChanged;
  40. }
  41. obj = value;
  42. if (obj != null)
  43. {
  44. obj.PropertyChanged += Obj_PropertyChanged;
  45. }
  46. Rebind();
  47. }
  48. }
  49. }
  50. public ObjectProperties()
  51. {
  52. InitializeComponent();
  53. }
  54. public void Initialize(IGamePlugin plugin, bool isMockObject)
  55. {
  56. this.isMockObject = isMockObject;
  57. Plugin = plugin;
  58. plugin.Map.Triggers.CollectionChanged += Triggers_CollectionChanged;
  59. houseComboBox.DataSource = plugin.Map.Houses.Select(t => new TypeItem<HouseType>(t.Type.Name, t.Type)).ToArray();
  60. missionComboBox.DataSource = plugin.Map.MissionTypes;
  61. UpdateDataSource();
  62. Disposed += (sender, e) =>
  63. {
  64. Object = null;
  65. plugin.Map.Triggers.CollectionChanged -= Triggers_CollectionChanged;
  66. };
  67. }
  68. private void Triggers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  69. {
  70. UpdateDataSource();
  71. }
  72. private void UpdateDataSource()
  73. {
  74. triggerComboBox.DataSource = Trigger.None.Yield().Concat(Plugin.Map.Triggers.Select(t => t.Name).Distinct()).ToArray();
  75. }
  76. private void Rebind()
  77. {
  78. houseComboBox.DataBindings.Clear();
  79. strengthNud.DataBindings.Clear();
  80. directionComboBox.DataBindings.Clear();
  81. missionComboBox.DataBindings.Clear();
  82. triggerComboBox.DataBindings.Clear();
  83. basePriorityNud.DataBindings.Clear();
  84. prebuiltCheckBox.DataBindings.Clear();
  85. sellableCheckBox.DataBindings.Clear();
  86. rebuildCheckBox.DataBindings.Clear();
  87. if (obj == null)
  88. {
  89. return;
  90. }
  91. switch (obj)
  92. {
  93. case Infantry infantry:
  94. {
  95. houseComboBox.Enabled = true;
  96. directionComboBox.DataSource = Plugin.Map.DirectionTypes
  97. .Where(t => t.Facing != FacingType.None)
  98. .Select(t => new TypeItem<DirectionType>(t.Name, t)).ToArray();
  99. missionComboBox.DataBindings.Add("SelectedItem", obj, "Mission");
  100. missionLabel.Visible = missionComboBox.Visible = true;
  101. basePriorityLabel.Visible = basePriorityNud.Visible = false;
  102. prebuiltCheckBox.Visible = false;
  103. sellableCheckBox.Visible = false;
  104. rebuildCheckBox.Visible = false;
  105. }
  106. break;
  107. case Unit unit:
  108. {
  109. houseComboBox.Enabled = true;
  110. directionComboBox.DataSource = Plugin.Map.DirectionTypes.Select(t => new TypeItem<DirectionType>(t.Name, t)).ToArray();
  111. missionComboBox.DataBindings.Add("SelectedItem", obj, "Mission");
  112. missionLabel.Visible = missionComboBox.Visible = true;
  113. basePriorityLabel.Visible = basePriorityNud.Visible = false;
  114. prebuiltCheckBox.Visible = false;
  115. sellableCheckBox.Visible = false;
  116. rebuildCheckBox.Visible = false;
  117. }
  118. break;
  119. case Building building:
  120. {
  121. houseComboBox.Enabled = building.IsPrebuilt;
  122. directionComboBox.DataSource = Plugin.Map.DirectionTypes.Select(t => new TypeItem<DirectionType>(t.Name, t)).ToArray();
  123. directionComboBox.Visible = (building.Type != null) && building.Type.HasTurret;
  124. missionLabel.Visible = missionComboBox.Visible = false;
  125. basePriorityLabel.Visible = basePriorityNud.Visible = true;
  126. prebuiltCheckBox.Visible = true;
  127. prebuiltCheckBox.Enabled = building.BasePriority >= 0;
  128. basePriorityNud.DataBindings.Add("Value", obj, "BasePriority");
  129. prebuiltCheckBox.DataBindings.Add("Checked", obj, "IsPrebuilt");
  130. switch (Plugin.GameType)
  131. {
  132. case GameType.TiberianDawn:
  133. {
  134. sellableCheckBox.Visible = false;
  135. rebuildCheckBox.Visible = false;
  136. } break;
  137. case GameType.RedAlert:
  138. {
  139. sellableCheckBox.DataBindings.Add("Checked", obj, "Sellable");
  140. rebuildCheckBox.DataBindings.Add("Checked", obj, "Rebuild");
  141. sellableCheckBox.Visible = true;
  142. rebuildCheckBox.Visible = true;
  143. } break;
  144. }
  145. }
  146. break;
  147. }
  148. houseComboBox.DataBindings.Add("SelectedValue", obj, "House");
  149. strengthNud.DataBindings.Add("Value", obj, "Strength");
  150. directionComboBox.DataBindings.Add("SelectedValue", obj, "Direction");
  151. triggerComboBox.DataBindings.Add("SelectedItem", obj, "Trigger");
  152. }
  153. private void Obj_PropertyChanged(object sender, PropertyChangedEventArgs e)
  154. {
  155. switch (e.PropertyName)
  156. {
  157. case "Type":
  158. {
  159. Rebind();
  160. }
  161. break;
  162. case "BasePriority":
  163. {
  164. if (obj is Building building)
  165. {
  166. prebuiltCheckBox.Enabled = building.BasePriority >= 0;
  167. }
  168. }
  169. break;
  170. case "IsPrebuilt":
  171. {
  172. if (obj is Building building)
  173. {
  174. if (!building.IsPrebuilt)
  175. {
  176. var basePlayer = Plugin.Map.HouseTypes.Where(h => h.Equals(Plugin.Map.BasicSection.BasePlayer)).FirstOrDefault() ?? Plugin.Map.HouseTypes.First();
  177. building.House = basePlayer;
  178. }
  179. houseComboBox.Enabled = building.IsPrebuilt;
  180. }
  181. } break;
  182. }
  183. if (!isMockObject)
  184. {
  185. Plugin.Dirty = true;
  186. }
  187. }
  188. private void comboBox_SelectedValueChanged(object sender, EventArgs e)
  189. {
  190. foreach (Binding binding in (sender as ComboBox).DataBindings)
  191. {
  192. binding.WriteValue();
  193. }
  194. }
  195. private void nud_ValueChanged(object sender, EventArgs e)
  196. {
  197. foreach (Binding binding in (sender as NumericUpDown).DataBindings)
  198. {
  199. binding.WriteValue();
  200. }
  201. }
  202. private void checkBox_CheckedChanged(object sender, EventArgs e)
  203. {
  204. foreach (Binding binding in (sender as CheckBox).DataBindings)
  205. {
  206. binding.WriteValue();
  207. }
  208. }
  209. }
  210. public class ObjectPropertiesPopup : ToolStripDropDown
  211. {
  212. private readonly ToolStripControlHost host;
  213. public ObjectProperties ObjectProperties { get; private set; }
  214. public ObjectPropertiesPopup(IGamePlugin plugin, INotifyPropertyChanged obj)
  215. {
  216. ObjectProperties = new ObjectProperties();
  217. ObjectProperties.Initialize(plugin, false);
  218. ObjectProperties.Object = obj;
  219. host = new ToolStripControlHost(ObjectProperties);
  220. Padding = Margin = host.Padding = host.Margin = Padding.Empty;
  221. MinimumSize = ObjectProperties.MinimumSize;
  222. ObjectProperties.MinimumSize = ObjectProperties.Size;
  223. MaximumSize = ObjectProperties.MaximumSize;
  224. ObjectProperties.MaximumSize = ObjectProperties.Size;
  225. Size = ObjectProperties.Size;
  226. Items.Add(host);
  227. ObjectProperties.Disposed += (sender, e) =>
  228. {
  229. ObjectProperties = null;
  230. Dispose(true);
  231. };
  232. }
  233. protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
  234. {
  235. base.OnClosed(e);
  236. ObjectProperties.Object = null;
  237. }
  238. }
  239. }