CnC_Remastered_Collection

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

TypeComboBox.cs (3965B)


      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 System;
     18 using System.Collections.Generic;
     19 using System.ComponentModel;
     20 using System.Data;
     21 using System.Drawing;
     22 using System.Linq;
     23 using System.Windows.Forms;
     24 
     25 namespace MobiusEditor.Controls
     26 {
     27     public partial class TypeComboBox : ComboBox
     28     {
     29         [Category("Behavior")]
     30         public Image MissingThumbnail { get; set; } = SystemIcons.Error.ToBitmap();
     31 
     32         public IEnumerable<IBrowsableType> Types
     33         {
     34             get => Items.Cast<TypeItem<IBrowsableType>>().Select(t => t.Type);
     35             set
     36             {
     37                 DataSource = value.Select(t => new TypeItem<IBrowsableType>(t.DisplayName, t)).ToArray();
     38                 DropDownHeight = Math.Max(DropDownHeight, value.Max(t => (t.Thumbnail?.Height ?? MissingThumbnail.Height) * 3));
     39                 Invalidate();
     40             }
     41         }
     42 
     43         public IBrowsableType SelectedType => SelectedValue as IBrowsableType;
     44 
     45         public TypeComboBox()
     46         {
     47             InitializeComponent();
     48 
     49             DisplayMember = "Name";
     50             ValueMember = "Type";
     51         }
     52 
     53         protected override void OnMeasureItem(MeasureItemEventArgs e)
     54         {
     55             base.OnMeasureItem(e);
     56 
     57             var typeItem = Items[e.Index] as TypeItem<IBrowsableType>;
     58             if (typeItem?.Type != null)
     59             {
     60                 e.ItemHeight = typeItem.Type.Thumbnail?.Height ?? MissingThumbnail.Height;
     61             }
     62         }
     63 
     64         protected override void OnDrawItem(DrawItemEventArgs e)
     65         {
     66             base.OnDrawItem(e);
     67 
     68             e.DrawBackground();
     69 
     70             if ((e.Index >= 0) && (e.Index < Items.Count))
     71             {
     72                 var typeItem = Items[e.Index] as TypeItem<IBrowsableType>;
     73                 if (typeItem?.Type != null)
     74                 {
     75                     StringFormat stringFormat = new StringFormat
     76                     {
     77                         LineAlignment = StringAlignment.Center
     78                     };
     79 
     80                     var textColor = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? SystemBrushes.HighlightText : SystemBrushes.WindowText;
     81                     var textSize = e.Graphics.MeasureString(typeItem.Name, Font, e.Bounds.Width, stringFormat);
     82                     e.Graphics.DrawString(typeItem.Name, Font, textColor, e.Bounds, stringFormat);
     83 
     84                     if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.None)
     85                     {
     86                         var thumbnail = typeItem.Type.Thumbnail ?? MissingThumbnail;
     87                         var thumbnailWidth = (int)Math.Min(e.Bounds.Width - textSize.Width, thumbnail.Width);
     88                         var thumbnailSize = new Size(thumbnailWidth, thumbnailWidth * thumbnail.Height / thumbnail.Width);
     89                         var thumbnailBounds = new Rectangle(new Point(e.Bounds.Right - thumbnailSize.Width, e.Bounds.Top), thumbnailSize);
     90                         e.Graphics.DrawImage(thumbnail, thumbnailBounds);
     91                     }
     92                 }
     93             }
     94 
     95             e.DrawFocusRectangle();
     96         }
     97     }
     98 }