public Vector2 FindNonCollidingPosition(HUDElement element, Vector2 desiredPosition) { if (element.IsIgnoringCollisions) { return(desiredPosition); } Vector2 ogPosition = element.GetHUDComputedPosition(false); Vector2 bestPosition = desiredPosition; for (int i = 0; i < 10; i++) // <- lazy { Vector2?testPos = this.FindFirstCollisionSolvedPosition(element, bestPosition); if (!testPos.HasValue) { return(bestPosition); } if (bestPosition == desiredPosition) { bestPosition = testPos.Value; } else { break; // <- even more lazy } } return(ogPosition); }
//////////////// public void FindAndApplyDisplacements(HUDElement element) { if (!this.FindAndApplyDisplacements_If(element)) { element.RevertDisplacedPosition(); } }
//////////////// private void UpdateInteractionsForEditModeDrag_If(bool isEditMode, bool mouseLeft) { if (this.IsInteractingWithControls) { return; } if (this.IsDragLocked()) { return; } // HUDElement currDrag = HUDElementsLibAPI.GetDraggingElement(); if (currDrag == null || currDrag == this) { bool isInteracting = mouseLeft && isEditMode; if (isInteracting) { if (this.IsDraggingSinceLastTick || this.IsMouseHoveringEditableBox) { this.ApplyDrag(); } } else { this.DesiredDragPosition = null; } } }
//////////////// public static void AddWidget(HUDElement element) //string layerName = "Vanilla: Mouse Text" { var mymod = ModContent.GetInstance <HUDElementsLibMod>(); mymod.HUDManager?.LoadHUDElement(element); mymod.MyUI?.Append(element); mymod.MyUI?.Recalculate(); HUDElementsLibAPI.MessageAboutHUD_If(); }
public static void DrawEditModeControls_AnchorButtons_If( SpriteBatch sb, Rectangle area, float brightness, bool onRight, bool onBottom, Vector2 hoverPoint, ref bool isHoverRight, ref bool isHoverBottom) { if (!HUDElementsLibConfig.Instance.EnableAnchorsToggleControl) { return; } Rectangle rArea = HUDElement.GetRightAnchorButtonArea(area); Rectangle bArea = HUDElement.GetBottomAnchorButtonArea(area); Rectangle iconBgArea = HUDElement.GetAnchorButtonIconBgArea(area); Rectangle iconArea = HUDElement.GetAnchorButtonIconArea(area); isHoverRight = rArea.Contains(hoverPoint.ToPoint()); isHoverBottom = bArea.Contains(hoverPoint.ToPoint()); Color rColor = onRight ? Color.White : Color.White * (isHoverRight ? 0.6f : 0.4f) * brightness; Color bColor = onBottom ? Color.White : Color.White * (isHoverBottom ? 0.6f : 0.4f) * brightness; sb.Draw( texture: Main.magicPixel, destinationRectangle: iconBgArea, color: Color.White * brightness ); sb.Draw( texture: Main.itemTexture[ItemID.WallAnchor], destinationRectangle: iconArea, color: Color.White ); // sb.Draw( texture: Main.magicPixel, destinationRectangle: rArea, color: rColor ); sb.Draw( texture: Main.magicPixel, destinationRectangle: bArea, color: bColor ); }
private void DrawEditModeControls_If( SpriteBatch sb, out bool isHoverCollision, out bool isHoverReset, out bool isHoverAnchorRight, out bool isHoverAnchorBottom) { isHoverCollision = this.IsMouseHoveringEditableBox && this.IsCollisionToggleable(); isHoverReset = this.IsMouseHoveringEditableBox && !this.IsDragLocked(); isHoverAnchorRight = this.IsMouseHoveringEditableBox && this.IsAnchorsToggleable(); isHoverAnchorBottom = this.IsMouseHoveringEditableBox && this.IsAnchorsToggleable(); // Rectangle realArea = this.GetHUDComputedArea(false); float brightness = this.IsDraggingSinceLastTick ? 1f : 0.5f; /*ModLibsCore.Libraries.Debug.DebugLibraries.Print( * "edit_ctrls_1_"+realArea+" "+this.Name, * ", imh:"+this.IsMouseHoveringEditableBox +", hc:"+this.IsCollisionToggleable() +", hr:"+this.IsDragLocked() +", har:"+this.IsAnchorsToggleable() +", hab:"+this.IsAnchorsToggleable() * );*/ // HUDElement.DrawEditModeControls_If( sb: sb, realArea: realArea, brightness: brightness, collisionToggler: isHoverCollision ? !this.IsIgnoringCollisions : (bool?)null, resetButton: isHoverReset ? !this.IsDragLocked() : (bool?)null, anchorRightButton: isHoverAnchorRight ? this.IsRightAnchored() : (bool?)null, anchorBottomButton: isHoverAnchorBottom ? this.IsBottomAnchored() : (bool?)null, hoverPoint: Main.MouseScreen, isHoverCollisionToggle: ref isHoverCollision, isHoverResetButton: ref isHoverReset, isHoverAnchorRightToggle: ref isHoverAnchorRight, isHoverAnchorBottomToggle: ref isHoverAnchorBottom ); }
//// private void ClearInteractionsIfAny() { var mousePos = new Vector2((float)Main.mouseX, (float)Main.mouseY); // if (this._LastElementHover != null) { this._LastElementHover.MouseOut(new UIMouseEvent(this._LastElementHover, mousePos)); this._LastElementHover = null; } }
//////////////// public virtual Vector2 GetDisplacementDirection(HUDElement against) { Vector2 pos = this.GetHUDComputedPosition(false); Vector2 dim = this.GetHUDComputedDimensions(); Vector2 posMid = pos + (dim * 0.5f); float midX = Main.screenWidth / 2; float midY = Main.screenHeight / 2; posMid.X = midX - posMid.X; posMid.Y = midY - posMid.Y; return(Vector2.Normalize(posMid)); // by default, aim to screen center }
//////////////// public static Vector2?FindDisplacedPosition_If(Rectangle currentArea, HUDElement mover, HUDElement obstacle) { Rectangle obstacleArea = obstacle.GetHUDComputedArea(true); if (!currentArea.Intersects(obstacleArea)) { return(null); } Vector2 dir = obstacle.GetDisplacementDirection(mover) * 2f; // float fX = currentArea.X; float fY = currentArea.Y; void inc(ref Rectangle rect) { fX += dir.X; fY += dir.Y; rect.X = (int)Math.Round(fX); rect.Y = (int)Math.Round(fY); } // Rectangle testArea; for (testArea = currentArea; testArea.Intersects(obstacleArea); inc(ref testArea)) // Efficient! { if (testArea.Right <= 0) { return(null); } if (testArea.Bottom <= 0) { return(null); } if (testArea.Top >= (Main.screenHeight - 1)) { return(null); } if (testArea.Left >= (Main.screenWidth - 1)) { return(null); } } return(new Vector2(testArea.X, testArea.Y)); }
//////////////// private void DrawEditModeBoxes(SpriteBatch sb) { Rectangle area = this.GetHUDComputedArea(false); Color baseColor = this.IsDragLocked() ? Color.Red : Color.White; baseColor *= this.IsMouseHoveringEditableBox ? 1f : 0.8f; float brightness = this.IsDraggingSinceLastTick ? 1f : 0.5f; // //ModLibsCore.Libraries.Debug.DebugLibraries.Print( "edit_"+this.Name, area.ToString() ); HUDElement.DrawEditModeBox( sb: sb, area: area, color: baseColor * brightness, bodyOpacity: this.IsIgnoringCollisions ? 0.2f : 0.5f, pulses: !this.IsMouseHoveringEditableBox ); if (this.DisplacedPosition.HasValue) { Color displacedColor = Color.Yellow * 0.5f; displacedColor *= this.IsMouseHoveringEditableBox ? 1f : 0.65f; Rectangle displacedArea = this.GetHUDComputedArea(true); HUDElement.DrawEditModeBox( sb: sb, area: displacedArea, color: displacedColor, bodyOpacity: 0.5f, pulses: !this.IsMouseHoveringEditableBox ); } }
public static void DrawEditModeControls_ResetButton_If( SpriteBatch sb, Rectangle area, float brightness, bool on, Vector2 hoverPoint, ref bool isHovering) { if (!HUDElementsLibConfig.Instance.EnableResetButtonControl) { return; } var buttonArea = HUDElement.GetResetButtonArea(area); var buttonIconArea = HUDElement.GetResetButtonIconArea(area); isHovering = buttonArea.Contains(hoverPoint.ToPoint()); Color bgColor = on ? Color.White : Color.White * (isHovering ? 0.65f : 0.35f) * brightness; bgColor *= isHovering ? 1f : 0.75f; Color iconColor = on ? Color.White : Color.Red; iconColor *= 0.8f; iconColor *= isHovering ? 1f : 0.85f; sb.Draw( texture: Main.magicPixel, destinationRectangle: buttonArea, color: bgColor * brightness ); sb.Draw( texture: Main.itemTexture[ItemID.MagicMirror], destinationRectangle: buttonIconArea, color: iconColor ); }
public void LoadHUDElement(HUDElement element) { this.Elements[element.Name] = element; // if (this.SavedElementInfo.ContainsKey(element.Name)) { ElementInfo elemInfo = this.SavedElementInfo[element.Name]; this.SavedElementInfo.Remove(element.Name); // element.SetUncomputedPosition(elemInfo.ScreenPosition, false); if (elemInfo.IsIgnoringCollisions.HasValue) { element.IsIgnoringCollisions = elemInfo.IsIgnoringCollisions.Value; } element.Recalculate(); } }
private Vector2?FindFirstCollisionSolvedPosition(HUDElement element, Vector2 desiredPosition) { Rectangle currentArea = element.GetHUDComputedArea(false); Rectangle desiredArea = currentArea; desiredArea.X = (int)desiredPosition.X - 1; desiredArea.Y = (int)desiredPosition.Y - 1; desiredArea.Width += 2; desiredArea.Height += 2; foreach (HUDElement elem in this.Elements.Values) { if (elem == element) { continue; } if (!elem.IsEnabled()) { continue; } if (elem.IsIgnoringCollisions) { continue; } Rectangle obstacleArea = elem.GetHUDComputedArea(true); if (desiredArea.Intersects(obstacleArea)) { return(HUDElement.FindClosestNonCollidingPosition( currentArea: currentArea, desiredPosition: desiredPosition, obstacleArea: obstacleArea )); } } return(null); }
private bool FindAndApplyDisplacements_If(HUDElement element) { if (element.IsIgnoringCollisions) { return(false); } // bool isDisplaced = false; Rectangle currentArea = element.GetHUDComputedArea(false); for (int i = 0; i < 250; i++) { HUDElement obstacle = this.FindFirstCollision(element); if (obstacle == null) { break; } Vector2?displacedPos = HUDElement.FindDisplacedPosition_If(currentArea, element, obstacle); if (!displacedPos.HasValue) { break; } isDisplaced = true; element.SetDisplacedPosition(displacedPos.Value); } //if( element.Name == "PKE Meter" || element.Name == "PKEMeter" ) { //ModContent.GetInstance<HUDElementsLibMod>().Logger.Info( element.Name+" displaced? "+isDisplaced // +" basepos: "+element.GetPositionOnHUD(true) // +" displacepos: "+element.GetPositionOnHUD(false) ); //} return(isDisplaced); }
//////////////// public HUDElement FindFirstCollision(HUDElement element) { if (element.IsDragLocked()) { return(null); } if (element.IsIgnoringCollisions) { return(null); } Rectangle currentArea = element.GetHUDComputedArea(false); foreach (HUDElement obstacle in this.Elements.Values) { if (obstacle == element) { continue; } if (!obstacle.IsEnabled()) { continue; } if (obstacle.IsIgnoringCollisions) { continue; } Rectangle obstacleArea = obstacle.GetHUDComputedArea(true); if (currentArea.Intersects(obstacleArea)) { return(obstacle); } } return(null); }
//////////////// private void UpdateInteractionsForEditModeControls_If(bool isEditMode, bool mouseLeft) { if (this.IsInteractingWithControls) { bool isInteracting = mouseLeft && isEditMode; //&& this.IsMouseHovering_Custom; this.IsInteractingWithControls = isInteracting; Main.LocalPlayer.mouseInterface |= isInteracting; // Repeatably locks control for this element, if needed //if( Main.LocalPlayer.mouseInterface ) { // Main.NewText( "HUD_UpdIntForCtrlIf 1" ); //} return; // Only the first tick of interaction matters } // if (!isEditMode) { return; } if (!mouseLeft) { return; } if (!this.IsMouseHoveringEditableBox) { return; } if (HUDElementsLibAPI.GetDraggingElement() != null) { return; } // Point mouse = Main.MouseScreen.ToPoint(); Rectangle area = this.GetHUDComputedArea(false); Rectangle toggler = HUDElement.GetCollisionTogglerArea(area); Rectangle reset = HUDElement.GetResetButtonArea(area); Rectangle anchorR = HUDElement.GetRightAnchorButtonArea(area); Rectangle anchorB = HUDElement.GetBottomAnchorButtonArea(area); bool pressed = false; if (toggler.Contains(mouse) && this.IsCollisionToggleable()) { pressed = this.ApplyCollisionsToggleControlPress_If(); } else if (reset.Contains(mouse) && !this.IsDragLocked()) { pressed = true; this.ResetPositionToDefault(); } else if (this.IsAnchorsToggleable()) { if (anchorR.Contains(mouse)) { pressed = this.ApplyRightAnchorToggleControlPress_If(); } else if (anchorB.Contains(mouse)) { pressed = this.ApplyBottomAnchorToggleControlPress_If(); } } this.IsInteractingWithControls = pressed; Main.LocalPlayer.mouseInterface |= pressed; //if( Main.LocalPlayer.mouseInterface ) { // Main.NewText( "HUD_UpdIntForCtrlIf 2" ); //} }
//////////////// private bool UpdateInteractionsWithinEntireUI_If(HUDElement elem) { if (!elem.IsEnabled()) { return(false); } if (!elem.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { return(false); } GameTime time = Main._drawInterfaceGameTime; if (time == null) { return(false); } this._ClickDisabledMillisecondsRemaining = Math.Max( 0.0d, this._ClickDisabledMillisecondsRemaining - time.ElapsedGameTime.TotalMilliseconds ); if (this._ClickDisabledMillisecondsRemaining > 0.0d) { return(true); } // var mousePos = new Vector2((float)Main.mouseX, (float)Main.mouseY); bool mouseLeftDown = Main.mouseLeft; //&& Main.hasFocus; bool mouseRightDown = Main.mouseRight; //&& Main.hasFocus; bool mouseMiddleDown = Main.mouseMiddle; //&& Main.hasFocus; bool mouseXButton1Down = Main.mouseXButton1; //&& Main.hasFocus; bool mouseXButton2Down = Main.mouseXButton2; //&& Main.hasFocus; // if (elem != this._LastElementHover) { if (this._LastElementHover != null) { this._LastElementHover.MouseOut(new UIMouseEvent(this._LastElementHover, mousePos)); } elem.MouseOver(new UIMouseEvent(elem, mousePos)); this._LastElementHover = elem; } // //ModLibsCore.Libraries.Debug.DebugLibraries.Print( "hud_interact_"+elem.Name, "mld:"+mouseLeftDown+", wmld:"+this._WasMouseLeftDown+", led:"+this._LastElementDown ); if (mouseLeftDown && !this._WasMouseLeftDown) { this._LastElementLeftDown = elem; elem.MouseDown(new UIMouseEvent(elem, mousePos)); double milliSinceLastMouseDown = time.TotalGameTime.TotalMilliseconds - this._LastMouseDownMilliseconds; if (this._LastElementLeftClicked == elem && milliSinceLastMouseDown < 500.0) { elem.DoubleClick(new UIMouseEvent(elem, mousePos)); this._LastElementLeftClicked = null; } this._LastMouseDownMilliseconds = time.TotalGameTime.TotalMilliseconds; } else if (this._LastElementLeftDown != null) { if (this._LastElementLeftDown.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { this._LastElementLeftDown.Click(new UIMouseEvent(this._LastElementLeftDown, mousePos)); this._LastElementLeftClicked = this._LastElementLeftDown; } this._LastElementLeftDown.MouseUp(new UIMouseEvent(this._LastElementLeftDown, mousePos)); this._LastElementLeftDown = null; } // // tModLoader added functionality, right, middle, extra button 1 & 2 click Events if (mouseRightDown && !this._WasMouseRightDown) { this._LastElementRightDown = elem; elem.RightMouseDown(new UIMouseEvent(elem, mousePos)); double milliSinceLastMouseRightDown = time.TotalGameTime.TotalMilliseconds - this._LastMouseRightDownMilliseconds; if (this._LastElementRightClicked == elem && milliSinceLastMouseRightDown < 500.0) { elem.RightDoubleClick(new UIMouseEvent(elem, mousePos)); this._LastElementRightClicked = null; } this._LastMouseRightDownMilliseconds = time.TotalGameTime.TotalMilliseconds; } else if (this._LastElementRightDown != null) { if (this._LastElementRightDown.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { this._LastElementRightDown.RightClick(new UIMouseEvent(this._LastElementRightDown, mousePos)); this._LastElementRightClicked = this._LastElementRightDown; } this._LastElementRightDown.RightMouseUp(new UIMouseEvent(this._LastElementRightDown, mousePos)); this._LastElementRightDown = null; } // if (mouseMiddleDown && !this._WasMouseMiddleDown) { this._LastElementMiddleDown = elem; elem.MiddleMouseDown(new UIMouseEvent(elem, mousePos)); double milliSinceLastMouseMiddleDown = time.TotalGameTime.TotalMilliseconds - this._LastMouseMiddleDownMilliseconds; if (this._LastElementMiddleClicked == elem && milliSinceLastMouseMiddleDown < 500.0) { elem.MiddleDoubleClick(new UIMouseEvent(elem, mousePos)); this._LastElementMiddleClicked = null; } this._LastMouseMiddleDownMilliseconds = time.TotalGameTime.TotalMilliseconds; } else if (this._LastElementMiddleDown != null) { if (this._LastElementMiddleDown.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { this._LastElementMiddleDown.MiddleClick(new UIMouseEvent(this._LastElementMiddleDown, mousePos)); this._LastElementMiddleClicked = this._LastElementMiddleDown; } this._LastElementMiddleDown.MiddleMouseUp(new UIMouseEvent(this._LastElementMiddleDown, mousePos)); this._LastElementMiddleDown = null; } // if (mouseXButton1Down && !this._WasMouseXButton1Down) { this._LastElementXButton1Down = elem; elem.XButton1MouseDown(new UIMouseEvent(elem, mousePos)); double milliSinceLastX1Down = time.TotalGameTime.TotalMilliseconds - this._LastMouseXButton1DownMilliseconds; if (this._LastElementXButton1Clicked == elem && milliSinceLastX1Down < 500.0) { elem.XButton1DoubleClick(new UIMouseEvent(elem, mousePos)); this._LastElementXButton1Clicked = null; } this._LastMouseXButton1DownMilliseconds = time.TotalGameTime.TotalMilliseconds; } else if (this._LastElementXButton1Down != null) { if (this._LastElementXButton1Down.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { this._LastElementXButton1Down.XButton1Click(new UIMouseEvent(this._LastElementXButton1Down, mousePos)); this._LastElementXButton1Clicked = this._LastElementXButton1Down; } this._LastElementXButton1Down.XButton1MouseUp(new UIMouseEvent(this._LastElementXButton1Down, mousePos)); this._LastElementXButton1Down = null; } // if (mouseXButton2Down && !this._WasMouseXButton2Down) { this._LastElementXButton2Down = elem; elem.XButton2MouseDown(new UIMouseEvent(elem, mousePos)); double millisSinceLastX2Down = time.TotalGameTime.TotalMilliseconds - this._LastMouseXButton2DownMilliseconds; if (this._LastElementXButton2Clicked == elem && millisSinceLastX2Down < 500.0) { elem.XButton2DoubleClick(new UIMouseEvent(elem, mousePos)); this._LastElementXButton2Clicked = null; } this._LastMouseXButton2DownMilliseconds = time.TotalGameTime.TotalMilliseconds; } else if (this._LastElementXButton2Down != null) { if (this._LastElementXButton2Down.GetHUDComputedArea(true).Contains(Main.mouseX, Main.mouseY)) { this._LastElementXButton2Down.XButton2Click(new UIMouseEvent(this._LastElementXButton2Down, mousePos)); this._LastElementXButton2Clicked = this._LastElementXButton2Down; } this._LastElementXButton2Down.XButton2MouseUp(new UIMouseEvent(this._LastElementXButton2Down, mousePos)); this._LastElementXButton2Down = null; } // if (PlayerInput.ScrollWheelDeltaForUI != 0) { elem.ScrollWheel(new UIScrollWheelEvent(elem, mousePos, PlayerInput.ScrollWheelDeltaForUI)); // PlayerInput.ScrollWheelDeltaForUI = 0; Moved after ModHooks.UpdateUI(gameTime); } // this._WasMouseLeftDown = mouseLeftDown; this._WasMouseRightDown = mouseRightDown; this._WasMouseMiddleDown = mouseMiddleDown; this._WasMouseXButton1Down = mouseXButton1Down; this._WasMouseXButton2Down = mouseXButton2Down; return(true); }
//// public static void DrawEditModeControls_If( SpriteBatch sb, Rectangle realArea, float brightness, bool?collisionToggler, bool?resetButton, bool?anchorRightButton, bool?anchorBottomButton, Vector2 hoverPoint, ref bool isHoverCollisionToggle, ref bool isHoverResetButton, ref bool isHoverAnchorRightToggle, ref bool isHoverAnchorBottomToggle) { if (!realArea.Contains(hoverPoint.ToPoint())) { return; } /*ModLibsCore.Libraries.Debug.DebugLibraries.Print( * "edit_ctrls_2_"+realArea.ToString(), * "b:"+brightness +", ct:"+collisionToggler +", ar:"+resetButton +", ab:"+anchorBottomButton +", hct:"+isHoverCollisionToggle +", hrb:"+isHoverResetButton +", har:"+isHoverAnchorRightToggle +", hab:"+isHoverAnchorBottomToggle * );*/ if (collisionToggler.HasValue) { HUDElement.DrawEditModeControls_CollisionToggler_If( sb: sb, area: realArea, brightness: brightness, on: collisionToggler.Value, hoverPoint: hoverPoint, isHovering: ref isHoverCollisionToggle ); } if (resetButton.HasValue) { HUDElement.DrawEditModeControls_ResetButton_If( sb: sb, area: realArea, brightness: brightness, on: resetButton.Value, hoverPoint: hoverPoint, isHovering: ref isHoverResetButton ); } if (anchorRightButton.HasValue && anchorBottomButton.HasValue) { HUDElement.DrawEditModeControls_AnchorButtons_If( sb: sb, area: realArea, brightness: brightness, onRight: anchorRightButton.Value, onBottom: anchorBottomButton.Value, hoverPoint: hoverPoint, isHoverRight: ref isHoverAnchorRightToggle, isHoverBottom: ref isHoverAnchorBottomToggle ); } }