TypeComboBox.cs 3.9 KB

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