/// <summary> /// Create a single-line text box. /// </summary> /// <param name="label">label</param> /// <param name="width">width</param> /// <param name="text">text</param> /// <param name="flags">filter flags</param> /// <param name="checker">custom checker per char</param> /// <returns>(modified) text</returns> public static string TextBox(string label, double width, string text, InputTextFlags flags, Func <char, bool> checker, LayoutOptions?options) { Window window = GetCurrentWindow(); if (window.SkipItems) { return(text); } var id = window.GetID(label); var container = window.RenderTree.CurrentContainer; Node node = container.GetNodeById(id); label = Utility.FindRenderedText(label); if (node == null) { //create node node = new Node(id, $"{nameof(TextBox)}<{label}>"); node.UseBoxModel = true; node.RuleSet.Replace(GUISkin.Current[GUIControlName.TextBox]); var size = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal); node.AttachLayoutEntry(size); } container.AppendChild(node); node.RuleSet.ApplyOptions(options); node.ActiveSelf = true; // rect Rect rect = window.GetRect(id); // interact InputTextContext context; text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context, flags, checker); var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal; // last item state window.TempData.LastItemState = state; // render if (flags.HaveFlag(InputTextFlags.Password)) { var dotText = new string('*', text.Length);//FIXME bad performance GUIAppearance.DrawTextBox(node, id, dotText, context, state); } else { GUIAppearance.DrawTextBox(node, id, text, context, state); } return(text); }
/// <summary> /// Create a single-line text box. /// </summary> /// <param name="label">label</param> /// <param name="width">width</param> /// <param name="text">text</param> /// <param name="flags">filter flags</param> /// <param name="checker">custom checker per char</param> /// <returns>(modified) text</returns> public static string TextBox(string label, double width, string text, InputTextFlags flags, Func <char, bool> checker, LayoutOptions?options) { Window window = GetCurrentWindow(); if (window.SkipItems) { return(text); } int id = window.GetID(label); // style apply var style = GUIStyle.Basic; style.Save(); style.ApplySkin(GUIControlName.TextBox); style.ApplyOption(options); // rect var height = style.GetLineHeight(); var size = new Size(width, height); Rect rect = window.GetRect(id, size); // interact InputTextContext context; text = GUIBehavior.TextBoxBehavior(id, rect, text, out bool hovered, out bool active, out context, flags, checker); // render var d = window.DrawList; var state = active ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal; if (flags.HaveFlag(InputTextFlags.Password)) { var dotText = new string('*', text.Length);//FIXME bad performance GUIAppearance.DrawTextBox(rect, id, dotText, context, state); } else { GUIAppearance.DrawTextBox(rect, id, text, context, state); } style.Restore(); return(text); }
private static char[] ApplyFilter(Queue <char> imeBuffer, InputTextFlags flags, Func <char, bool> checker) { if (checker != null) { List <char> list = new List <char>(); foreach (var c in imeBuffer) { if (checker(c)) { list.Add(c); } } return(list.ToArray()); } else if (flags == 0 || !CheckFlags(flags)) { return(imeBuffer.ToArray()); } else { List <char> list = new List <char>(); foreach (var c in imeBuffer) { bool pass = false; char charToAdd = c; if (flags.HaveFlag(InputTextFlags.CharsDecimal)) { if (c >= '0' && c <= '9' || c == '.') { pass = true; } } else if (flags.HaveFlag(InputTextFlags.CharsHexadecimal)) { if (flags.HaveFlag(InputTextFlags.CharsUppercase)) { if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F') { pass = true; } } else { if (c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f') { pass = true; } } } else if (flags.HaveFlag(InputTextFlags.CharsUppercase)) { if (c >= 'A' && c <= 'Z') { pass = true; } else if (c >= 'a' && c <= 'z') { charToAdd = char.ToUpper(c); pass = true; } } else if (flags.HaveFlag(InputTextFlags.CharsNoBlank)) { if (!char.IsWhiteSpace(c)) { pass = true; } } if (pass) { list.Add(charToAdd); } } return(list.ToArray()); } }