From c57c06f6eeff04f530e7fc670ae96065c9889699 Mon Sep 17 00:00:00 2001 From: onepiecefreak3 Date: Sun, 26 Nov 2023 14:30:51 +0100 Subject: [PATCH] Bump to version 1.0.44; Textboxes visually show when disabled or marked as read-only; Use LocalizedString's for InputBox; Add OkAction and CancelAction on Modals; Fix DragDropEvent not firing for MainForm in border areas or the menu bar; --- ImGui.Forms/Application.cs | 32 +++++++++++++++--------- ImGui.Forms/Controls/TextBox.cs | 41 +++++++++++++++++++++---------- ImGui.Forms/Form.cs | 3 +-- ImGui.Forms/ImGui.Forms.nuspec | 2 +- ImGui.Forms/Modals/IO/InputBox.cs | 19 +++++++++++--- ImGui.Forms/Modals/Modal.cs | 9 +++++++ 6 files changed, 75 insertions(+), 31 deletions(-) diff --git a/ImGui.Forms/Application.cs b/ImGui.Forms/Application.cs index a88bed2..05f8f63 100644 --- a/ImGui.Forms/Application.cs +++ b/ImGui.Forms/Application.cs @@ -3,6 +3,7 @@ using System.Numerics; using ImGui.Forms.Factories; using ImGui.Forms.Localization; +using ImGui.Forms.Models; using ImGui.Forms.Models.IO; using ImGui.Forms.Support.Veldrid.ImGui; using Veldrid; @@ -125,10 +126,7 @@ private void CreateApplication(Form form) private bool UpdateFrame(CommandList cl) { - _dragDropEvent = default; - _keyUpCommand = default; - _keyDownCommand = default; - _frameHandledDragDrop = false; + UpdateApplicationEvents(); ImageFactory.FreeTextures(); IdFactory.FreeIds(); @@ -159,6 +157,14 @@ private bool UpdateFrame(CommandList cl) return true; } + private void UpdateApplicationEvents() + { + _dragDropEvent = default; + _keyUpCommand = default; + _keyDownCommand = default; + _frameHandledDragDrop = false; + } + #region Window events private void Window_Shown() @@ -230,18 +236,18 @@ private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) UnhandledException?.Invoke(this, e.ExceptionObject as Exception); } - internal bool TryGetKeyUpCommand(out KeyCommand keyUp) + internal bool TryGetKeyDownCommand(out KeyCommand keyDown) { - keyUp = _keyUpCommand; + keyDown = _keyDownCommand; - return _keyUpCommand != default; + return _keyDownCommand != default; } - internal bool TryGetKeyDownCommand(out KeyCommand keyDown) + internal bool TryGetKeyUpCommand(out KeyCommand keyUp) { - keyDown = _keyDownCommand; + keyUp = _keyUpCommand; - return _keyDownCommand != default; + return _keyUpCommand != default; } internal bool TryGetDragDrop(Veldrid.Rectangle controlRect, out DragDropEventEx obj) @@ -249,7 +255,7 @@ internal bool TryGetDragDrop(Veldrid.Rectangle controlRect, out DragDropEventEx obj = _dragDropEvent; // Try get drag drop event - if (_frameHandledDragDrop || _dragDropEvent.MousePosition == default) + if (_frameHandledDragDrop || _dragDropEvent.IsEmpty) return false; // Check if control contains dropped element @@ -277,11 +283,13 @@ public ExecutionContext(Form mainForm, GraphicsDevice gd, Sdl2Window window) } } - struct DragDropEventEx + readonly struct DragDropEventEx { public DragDropEvent Event { get; } public Vector2 MousePosition { get; } + public bool IsEmpty => MousePosition == default && Event.File == null; + public DragDropEventEx(DragDropEvent evt, Vector2 mousePos) { Event = evt; diff --git a/ImGui.Forms/Controls/TextBox.cs b/ImGui.Forms/Controls/TextBox.cs index eb8fc8d..3cb0bc3 100644 --- a/ImGui.Forms/Controls/TextBox.cs +++ b/ImGui.Forms/Controls/TextBox.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Numerics; using ImGui.Forms.Controls.Base; using ImGui.Forms.Localization; @@ -12,7 +13,7 @@ namespace ImGui.Forms.Controls public class TextBox : Component { private bool _activePreviousFrame; - private string _text=string.Empty; + private string _text = string.Empty; /// /// The text that was set or changed in this component. @@ -89,9 +90,13 @@ public override Size GetSize() protected override void UpdateInternal(Rectangle contentRect) { + bool isMasked = IsMasked; + bool isReadonly = IsReadOnly; + bool enabled = Enabled; + var flags = ImGuiInputTextFlags.None; - if (IsMasked) flags |= ImGuiInputTextFlags.Password; - if (IsReadOnly) flags |= ImGuiInputTextFlags.ReadOnly; + if (isMasked) flags |= ImGuiInputTextFlags.Password; + if (isReadonly || !enabled) flags |= ImGuiInputTextFlags.ReadOnly; switch (AllowedCharacters) { @@ -106,22 +111,32 @@ protected override void UpdateInternal(Rectangle contentRect) ImGuiNET.ImGui.SetNextItemWidth(contentRect.Width); + if (isReadonly || !enabled) + { + ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBg, 0xFF666666); + ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBgActive, 0xFF666666); + ImGuiNET.ImGui.PushStyleColor(ImGuiCol.FrameBgHovered, 0xFF666666); + } + if (!string.IsNullOrEmpty(Placeholder)) { if (ImGuiNET.ImGui.InputTextWithHint($"##{Id}", Placeholder, ref _text, MaxCharacters, flags)) OnTextChanged(); - - return; } + else + { + if (ImGuiNET.ImGui.InputText($"##{Id}", ref _text, MaxCharacters, flags)) + OnTextChanged(); - if (ImGuiNET.ImGui.InputText($"##{Id}", ref _text, MaxCharacters, flags)) - OnTextChanged(); + // Check if InputText is active and lost focus + if (!ImGuiNET.ImGui.IsItemActive() && _activePreviousFrame) + OnFocusLost(); - // Check if InputText is active and lost focus - if (!ImGuiNET.ImGui.IsItemActive() && _activePreviousFrame) - OnFocusLost(); + _activePreviousFrame = ImGuiNET.ImGui.IsItemActive(); + } - _activePreviousFrame = ImGuiNET.ImGui.IsItemActive(); + if (isReadonly || !enabled) + ImGuiNET.ImGui.PopStyleColor(3); } protected override void ApplyStyles() @@ -142,12 +157,12 @@ protected override void RemoveStyles() private void OnTextChanged() { - TextChanged?.Invoke(this, new EventArgs()); + TextChanged?.Invoke(this, EventArgs.Empty); } private void OnFocusLost() { - FocusLost?.Invoke(this, new EventArgs()); + FocusLost?.Invoke(this, EventArgs.Empty); } } diff --git a/ImGui.Forms/Form.cs b/ImGui.Forms/Form.cs index 02adbaa..3aa716d 100644 --- a/ImGui.Forms/Form.cs +++ b/ImGui.Forms/Form.cs @@ -9,7 +9,6 @@ using ImGui.Forms.Extensions; using ImGui.Forms.Localization; using ImGui.Forms.Modals; -using ImGui.Forms.Models; using ImGui.Forms.Resources; using ImGuiNET; using Veldrid.Sdl2; @@ -134,7 +133,7 @@ public void Update() // Handle Drag and Drop after rendering if (AllowDragDrop) - if (Application.Instance.TryGetDragDrop(contentRect, out var dragDrop)) + if (Application.Instance.TryGetDragDrop(new Veldrid.Rectangle(0, 0, (int)Size.X, (int)Size.Y), out var dragDrop)) OnDragDrop(dragDrop.Event); // End window diff --git a/ImGui.Forms/ImGui.Forms.nuspec b/ImGui.Forms/ImGui.Forms.nuspec index d45281f..6bd16b2 100644 --- a/ImGui.Forms/ImGui.Forms.nuspec +++ b/ImGui.Forms/ImGui.Forms.nuspec @@ -2,7 +2,7 @@ Imgui.Forms - 1.0.43 + 1.0.44 A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui) onepiecefreak diff --git a/ImGui.Forms/Modals/IO/InputBox.cs b/ImGui.Forms/Modals/IO/InputBox.cs index 36207c7..dc6fa99 100644 --- a/ImGui.Forms/Modals/IO/InputBox.cs +++ b/ImGui.Forms/Modals/IO/InputBox.cs @@ -3,7 +3,10 @@ using System.Threading.Tasks; using ImGui.Forms.Controls; using ImGui.Forms.Controls.Layouts; +using ImGui.Forms.Localization; using ImGui.Forms.Models; +using ImGui.Forms.Models.IO; +using Veldrid; namespace ImGui.Forms.Modals.IO { @@ -18,7 +21,7 @@ public class InputBox : Modal public string Input { get; private set; } - private InputBox(string caption, string text, string preset, string placeHolder, int maxCharacters = 0) + private InputBox(LocalizedString caption, LocalizedString text, string preset, LocalizedString? placeHolder, int maxCharacters) { #region Controls @@ -26,7 +29,10 @@ private InputBox(string caption, string text, string preset, string placeHolder, var cancelButton = new Button { Text = Cancel_, Width = ButtonWidth_ }; var label = new Label { Text = text }; - _textBox = new TextBox { Placeholder = placeHolder }; + + _textBox = new TextBox(); + if (placeHolder != null) + _textBox.Placeholder = placeHolder.Value; if (maxCharacters >= 0) _textBox.MaxCharacters = (uint)maxCharacters; @@ -41,6 +47,13 @@ private InputBox(string caption, string text, string preset, string placeHolder, #endregion + #region Keys + + OkAction = new KeyCommand(ModifierKeys.None, Key.Enter); + CancelAction = new KeyCommand(ModifierKeys.None, Key.Escape); + + #endregion + _textBox.Text = preset; Result = DialogResult.Cancel; @@ -92,7 +105,7 @@ private void OkButton_Clicked(object sender, EventArgs e) Close(); } - public static async Task ShowAsync(string caption, string text, string preset = "", string placeHolder = "", int maxCharacters = 0) + public static async Task ShowAsync(LocalizedString caption, LocalizedString text, string preset = "", LocalizedString? placeHolder = null, int maxCharacters = -1) { var inputBox = new InputBox(caption, text, preset, placeHolder, maxCharacters); await inputBox.ShowAsync(); diff --git a/ImGui.Forms/Modals/Modal.cs b/ImGui.Forms/Modals/Modal.cs index a0cd517..738b821 100644 --- a/ImGui.Forms/Modals/Modal.cs +++ b/ImGui.Forms/Modals/Modal.cs @@ -4,6 +4,7 @@ using ImGui.Forms.Controls.Base; using ImGui.Forms.Localization; using ImGui.Forms.Models; +using ImGui.Forms.Models.IO; using ImGui.Forms.Resources; using ImGuiNET; using Veldrid; @@ -23,6 +24,9 @@ public class Modal : Component public bool BlockFormClosing { get; private set; } + public KeyCommand OkAction { get; set; } + public KeyCommand CancelAction { get; set; } + protected DialogResult Result { get; set; } public Vector2 Size { get; set; } = new Vector2(200, 80); @@ -50,6 +54,11 @@ protected override async void UpdateInternal(Rectangle contentRect) // Create content of popup Content?.Update(new Rectangle(contentRect.X, contentRect.Y, contentRect.Width, contentRect.Height)); + if (IsKeyDown(OkAction)) + Close(DialogResult.Ok); + else if (IsKeyDown(CancelAction)) + Close(DialogResult.Cancel); + // Create content of child modal DrawModal(ChildModal);