-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce actual ComboBox with text input nad proposals;
- Loading branch information
1 parent
3fc5872
commit 3ae0e05
Showing
3 changed files
with
214 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using ImGui.Forms.Controls.Base; | ||
using ImGui.Forms.Localization; | ||
using ImGui.Forms.Models; | ||
using ImGui.Forms.Resources; | ||
using Veldrid; | ||
|
||
namespace ImGui.Forms.Controls | ||
{ | ||
public class DropDownBox<TItem> : Component | ||
{ | ||
public IList<ComboBoxItem<TItem>> Items { get; } = new List<ComboBoxItem<TItem>>(); | ||
|
||
public ComboBoxItem<TItem> SelectedItem { get; set; } | ||
|
||
public SizeValue Width { get; set; } = SizeValue.Content; | ||
|
||
#region Events | ||
|
||
public event EventHandler SelectedItemChanged; | ||
|
||
#endregion | ||
|
||
public override Size GetSize() | ||
{ | ||
var maxWidth = Items.Select(x => FontResource.GetCurrentLineWidth(x.Name)).DefaultIfEmpty(0).Max() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.X * 2; | ||
var arrowWidth = 20; | ||
|
||
SizeValue width = Width.IsContentAligned ? maxWidth + arrowWidth : Width; | ||
var height = FontResource.GetCurrentLineHeight() + (int)ImGuiNET.ImGui.GetStyle().ItemInnerSpacing.Y * 2; | ||
|
||
return new Size(width, height); | ||
} | ||
|
||
protected override void UpdateInternal(Rectangle contentRect) | ||
{ | ||
ImGuiNET.ImGui.SetNextItemWidth(contentRect.Width); | ||
|
||
var selectedName = SelectedItem?.Name ?? string.Empty; | ||
if (ImGuiNET.ImGui.BeginCombo($"##combo{Id}", selectedName)) | ||
{ | ||
for (var i = 0; i < Items.Count; i++) | ||
{ | ||
if (ImGuiNET.ImGui.Selectable(Items[i].Name, SelectedItem == Items[i])) | ||
{ | ||
var hasChanged = SelectedItem != Items[i]; | ||
SelectedItem = Items[i]; | ||
|
||
if (hasChanged) | ||
OnSelectedItemChanged(); | ||
} | ||
} | ||
|
||
ImGuiNET.ImGui.EndCombo(); | ||
} | ||
} | ||
|
||
private void OnSelectedItemChanged() | ||
{ | ||
SelectedItemChanged?.Invoke(this, new EventArgs()); | ||
} | ||
} | ||
|
||
public class ComboBoxItem<TItem> | ||
{ | ||
public TItem Content { get; } | ||
|
||
public LocalizedString Name { get; } | ||
|
||
public ComboBoxItem(TItem content, LocalizedString name = default) | ||
{ | ||
Content = content; | ||
Name = name.IsEmpty ? (LocalizedString)content.ToString() : name; | ||
} | ||
|
||
public static implicit operator ComboBoxItem<TItem>(TItem o) => new(o); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters