CnC_Remastered_Collection

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

TerrainProperties.cs (4900B)


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