Skip to content

Commit

Permalink
Bump to version 1.0.44;
Browse files Browse the repository at this point in the history
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;
  • Loading branch information
onepiecefreak3 committed Nov 26, 2023
1 parent 45c9b2a commit c57c06f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 31 deletions.
32 changes: 20 additions & 12 deletions ImGui.Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -230,26 +236,26 @@ 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)
{
obj = _dragDropEvent;

// Try get drag drop event
if (_frameHandledDragDrop || _dragDropEvent.MousePosition == default)
if (_frameHandledDragDrop || _dragDropEvent.IsEmpty)
return false;

// Check if control contains dropped element
Expand Down Expand Up @@ -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;
Expand Down
41 changes: 28 additions & 13 deletions ImGui.Forms/Controls/TextBox.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Numerics;
using ImGui.Forms.Controls.Base;
using ImGui.Forms.Localization;
Expand All @@ -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;

/// <summary>
/// The text that was set or changed in this component.
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
Expand All @@ -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);
}
}

Expand Down
3 changes: 1 addition & 2 deletions ImGui.Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ImGui.Forms/ImGui.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Imgui.Forms</id>
<version>1.0.43</version>
<version>1.0.44</version>
<description>A WinForms-inspired object-oriented framework around Dear ImGui (https://github.com/ocornut/imgui)</description>

<authors>onepiecefreak</authors>
Expand Down
19 changes: 16 additions & 3 deletions ImGui.Forms/Modals/IO/InputBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,15 +21,18 @@ 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

var okButton = new Button { Text = Ok_, Width = ButtonWidth_ };
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;

Expand All @@ -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;
Expand Down Expand Up @@ -92,7 +105,7 @@ private void OkButton_Clicked(object sender, EventArgs e)
Close();
}

public static async Task<string> ShowAsync(string caption, string text, string preset = "", string placeHolder = "", int maxCharacters = 0)
public static async Task<string> 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();
Expand Down
9 changes: 9 additions & 0 deletions ImGui.Forms/Modals/Modal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit c57c06f

Please sign in to comment.