OneNightDuel/ONDClient/GUI/TextBoxUIElement.cs

41 lines
1.3 KiB
C#
Raw Normal View History

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;
public class TextBoxUIElement : TextUIElement {
public bool Focused{ get; set; } = false;
public Action OnFocused{ get; set; } = () => { };
public Action OnUnfocused{ get; set; } = () => { };
public TextBoxUIElement(SpriteFont font, Point corner1, Point corner2) : base(corner1, corner2, font) {
Core.Instance.Window.TextInput += TextInputHandler;
InputManager.AddListener(InputManager.MouseButton.LEFT, () => {
if (!IsWithinBounds(InputManager.MouseState.Position)){
Focused = false;
OnUnfocused();
}
},
InputTiming.PRESS, new InputListenerHook(true));
Pressable = true;
OnMousePress = () => {
Focused = true;
OnFocused();
};
}
public void TextInputHandler(object sender, TextInputEventArgs e) {
if (!Focused) return;
if (e.Character == '\b') {
if (Text.Length > 0) Text = Text[..^1];
return;
}
if(Font.Characters.Contains(e.Character))
Text += e.Character;
}
}