From 6335bcdc934fd9eee6157562e561134ec15e5ea8 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 6 May 2026 16:42:38 +0200 Subject: [PATCH] Add in-built editors for `DateTime` and `TimeSpan` --- .../CustomEditors/Editors/TimeSpanEditor.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Source/Editor/CustomEditors/Editors/TimeSpanEditor.cs diff --git a/Source/Editor/CustomEditors/Editors/TimeSpanEditor.cs b/Source/Editor/CustomEditors/Editors/TimeSpanEditor.cs new file mode 100644 index 000000000..ec42527e3 --- /dev/null +++ b/Source/Editor/CustomEditors/Editors/TimeSpanEditor.cs @@ -0,0 +1,90 @@ +// Copyright (c) Wojciech Figat. All rights reserved. + +using FlaxEngine; +using FlaxEngine.GUI; +using System; + +namespace FlaxEditor.CustomEditors.Editors +{ + /// + /// Default implementation of the inspector used to edit TimeSpan value type properties. + /// + [CustomEditor(typeof(TimeSpan)), DefaultEditor] + class TimeSpanEditor : CustomEditor + { + private TextBox _textBox; + private bool _isRefreshing; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + if (HasDifferentTypes) + return; + + _textBox = layout.Custom().CustomControl; + _textBox.EditEnd += OnEditEnd; + } + + private void OnEditEnd() + { + if (_isRefreshing) + return; + if (TimeSpan.TryParse(_textBox.Text, out var timeSpan)) + SetValue(timeSpan); + } + + /// + public override void Refresh() + { + base.Refresh(); + + _isRefreshing = true; + _textBox.Text = HasDifferentValues ? "Multiple Values" : ((TimeSpan)Values[0]).ToString("g"); + _isRefreshing = false; + } + } + + /// + /// Default implementation of the inspector used to edit DateTime value type properties. + /// + [CustomEditor(typeof(DateTime)), DefaultEditor] + class DateTimeEditor : CustomEditor + { + private TextBox _textBox; + private bool _isRefreshing; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + if (HasDifferentTypes) + return; + + _textBox = layout.Custom().CustomControl; + _textBox.EditEnd += OnEditEnd; + } + + private void OnEditEnd() + { + if (_isRefreshing) + return; + if (DateTime.TryParse(_textBox.Text, out var timeSpan)) + SetValue(timeSpan); + } + + /// + public override void Refresh() + { + base.Refresh(); + + _isRefreshing = true; + _textBox.Text = HasDifferentValues ? "Multiple Values" : ((DateTime)Values[0]).ToString("g"); + _isRefreshing = false; + } + } +}