public void OnTick() { if (Mouse.ButtonPressed(System.Windows.Forms.MouseButtons.Left)) { if (pointer.MouseOver) { moving = true; } } else { moving = false; } if (moving) { if (this.PositionType == UIPositionType.Global) { Vector2D pos = new Vector2D(Mouse.GamePosition.X - this.GetGlobalPosition().X, pointer.Position.Y); if (pos.X < -this.GetRealSize().Width / 2) { pos.X = -this.GetRealSize().Width / 2; } if (pos.X > this.GetRealSize().Width / 2) { pos.X = this.GetRealSize().Width / 2; } pointer.SetPosition(pos); float tmpValue = (pos.X + this.GetRealSize().Width / 2) / this.GetRealSize().Width; float valuesOffset = this.MaxValue - this.MinValue; this.Value = this.MinValue + tmpValue * valuesOffset; } else { Vector2D pos = new Vector2D(Mouse.Position.X - this.GetGlobalPosition().X, pointer.Position.Y); if (pos.X < -this.GetRealSize().Width / 2) { pos.X = -this.GetRealSize().Width / 2; } if (pos.X > this.GetRealSize().Width / 2) { pos.X = this.GetRealSize().Width / 2; } pointer.SetPosition(pos); float tmpValue = (pos.X + this.GetRealSize().Width / 2) / this.GetRealSize().Width; float valuesOffset = this.MaxValue - this.MinValue; this.Value = this.MinValue + tmpValue * valuesOffset; } } }
public override void Update() { if (Mouse.ButtonPressed(MouseButtons.Left)) { var isFocused = IsMouseOver(); if (isFocused != IsFocused) { IsFocused = isFocused; FocusChanged?.Invoke(this, new FocusChangedEventArgs(IsFocused)); } if (IsFocused && Font.HitTest(Mouse.Position - Position, Text, ActualWidth, out int i)) { _cursorPos = i; } } if (!IsFocused) { return; } //Toggle cursor every half second _cursorTick += GameTime.UnscaledDeltaTime; if (_cursorTick >= .5f) { _showCursor = !_showCursor; _cursorTick = 0; } //Cursor navigation //Always show cursor when navigating if (_cursorPos > 0 && Keyboard.KeyPressed(Keys.Left)) { _cursorPos--; _cursorTick = 0; _showCursor = true; HandleInputEvent(Keys.Left); } else if (_cursorPos < _text.Length && Keyboard.KeyPressed(Keys.Right)) { _cursorPos++; _cursorTick = 0; _showCursor = true; HandleInputEvent(Keys.Right); } if (Keyboard.KeyPressed(Keys.Enter)) { Enter?.Invoke(this, new EventArgs()); HandleInputEvent(Keys.Enter); } //Text deletion if (Keyboard.KeyPressed(Keys.Backspace) && _cursorPos > 0) { _text = _text.Remove(_cursorPos - 1, 1); _cursorPos--; HandleInputEvent(Keys.Backspace); } if (Keyboard.KeyPressed(Keys.Delete) && _cursorPos < _text.Length) { _text = _text.Remove(_cursorPos, 1); HandleInputEvent(Keys.Delete); } //Text input var keys = Enum.GetValues(typeof(Keys)); for (int i = 0; i < keys.Length; i++) { var k = (Keys)keys.GetValue(i); if (Keyboard.KeyPressed(k)) { var text = Keyboard.ToUnicode(k); if (text != "\b") { _text = _text.Insert(_cursorPos, text); _cursorPos += text.Length; } HandleInputEvent(k); } } }