public void HandleMouseEvent(GameTime gameTime) { bool mouseLeftDown = Main.mouseLeft && Main.hasFocus; bool mouseRightDown = Main.mouseRight && Main.hasFocus; // 响应鼠标事件的时候一定是从后往前,前端的窗口一定是第一个响应鼠标事件的 // 如果有多个前端窗口,那么优先响应当前焦点窗口 UIElement hoverElement = null; int sz = uiRunningStack.Count; for (int i = sz - 1; i >= 0; i--) { var state = uiRunningStack[i]; if (state.IsActive) { var element = state.ElementAt(Main.MouseScreen); if (element != state) { hoverElement = element; if (!_wasMouseLeftDown && mouseLeftDown) { _currentFocus = state; _currentFocus.TimeGetFocus = _timer; } break; } } } // if (hoverElement != null) Main.NewText(hoverElement.ToString()); // 鼠标点击移动事件 if (hoverElement != null) { _tooltip = hoverElement.Tooltip; Main.LocalPlayer.mouseInterface = true; Main.LocalPlayer.releaseUseItem = true; } if (hoverElement != null && hoverElement != _previousHoverElement) { hoverElement.MouseEnter(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); } if (_previousHoverElement != null && hoverElement != _previousHoverElement) { _previousHoverElement.MouseOut(new UIMouseEvent(_previousHoverElement, gameTime.TotalGameTime, Main.MouseScreen)); } if (!_wasMouseLeftDown && mouseLeftDown) { if (hoverElement == null || _lastFocusElement != hoverElement) { _lastFocusElement?.UnFocus(new UIActionEvent(_lastLeftDownElement, gameTime.TotalGameTime)); } } // 鼠标左键 if (!_wasMouseLeftDown && mouseLeftDown && hoverElement != null) { hoverElement.MouseLeftDown(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); hoverElement.FocusOn(new UIActionEvent(hoverElement, gameTime.TotalGameTime)); hoverElement.DragStart(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); _lastLeftDownElement = hoverElement; _lastFocusElement = hoverElement; } if (_wasMouseLeftDown && Main.mouseLeftRelease) { _lastLeftDownElement?.MouseLeftUp(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); _lastLeftDownElement?.DragEnd(new UIDragEndEvent(_lastLeftDownElement, hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); if (_wasMouseLeftDown && Main.mouseLeftRelease && hoverElement != null && _lastLeftDownElement == hoverElement) { if (gameTime.TotalGameTime.TotalMilliseconds - _lastLeftClickTime > 200) { hoverElement.MouseLeftClick(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); } else { hoverElement.MouseDoubleClick(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); } _lastLeftClickElement = hoverElement; _lastLeftClickTime = gameTime.TotalGameTime.TotalMilliseconds; } _lastLeftDownElement = null; } // 鼠标右键 if (!_wasMouseRightDown && mouseRightDown && hoverElement != null) { hoverElement.MouseRightDown(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); _lastRightDownElement = hoverElement; } if (_wasMouseRightDown && Main.mouseRightRelease) { _lastRightDownElement?.MouseRightUp(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); if (hoverElement != null && _lastRightDownElement == hoverElement) { hoverElement.MouseRightClick(new UIMouseEvent(hoverElement, gameTime.TotalGameTime, Main.MouseScreen)); _lastRightClickElement = hoverElement; } _lastRightDownElement = null; } // 滚轮 if (PlayerInput.ScrollWheelDeltaForUI != 0) { hoverElement?.ScrollWheel(new UIScrollWheelEvent(hoverElement, gameTime.TotalGameTime, PlayerInput.ScrollWheelDeltaForUI)); } _previousHoverElement = hoverElement; _wasMouseLeftDown = Main.mouseLeft; _wasMouseRightDown = Main.mouseRight; }
public void Remove(UIState state) { uiRunningStack.Remove(state); }
public void AddState(UIState state) { UIStateMachine.Add(state); }