public override bool Update(GameTime gameTime, ref MouseState MouseState, ref MouseState OldMouseState) { Point AbsolutePosition = this.AbsolutePosition; Point AbsoluteSize = this.AbsoluteSize; HoveringOver = false; if (IsDisplayed && MouseState.Position.X >= AbsolutePosition.X && MouseState.Position.Y >= AbsolutePosition.Y && MouseState.Position.X < AbsolutePosition.X + AbsoluteSize.X && MouseState.Position.Y < AbsolutePosition.Y + AbsoluteSize.Y) { HoveringOver = true; if (OldMouseState.LeftButton == ButtonState.Released && MouseState.LeftButton == ButtonState.Pressed) { IsFocused = true; } } else if (IsDisplayed && OldMouseState.LeftButton == ButtonState.Released && MouseState.LeftButton == ButtonState.Pressed) { IsFocused = false; } // update textbox specific stuff bool SetCursorPositionToMouse = false; int TotalTextWidth = 0; int TotalCursorPositionOnText = 0; for (int i = 0; i < Text.Length; i++) { CharacterTexture CharacterToDraw = Font[Text[i]]; TotalTextWidth += CharacterToDraw.Texture.Width + CharacterToDraw.Spacing; if (CursorPosition > i) { TotalCursorPositionOnText += CharacterToDraw.Texture.Width + CharacterToDraw.Spacing; } if (IsFocused && OldMouseState.LeftButton == ButtonState.Released && MouseState.LeftButton == ButtonState.Pressed) { CursorPosition = SetCursorPositionToMouse? CursorPosition : Text.Length; if (SetCursorPositionToMouse == false && TotalTextWidth - CharacterToDraw.Texture.Width / 2 > MouseState.X - AbsolutePosition.X - 2 + Scroll) { CursorPosition = i; SetCursorPositionToMouse = true; } } } if (TotalCursorPositionOnText + 6 - Scroll > AbsoluteSize.X) { Scroll = TotalCursorPositionOnText + (AbsoluteSize.X / 2) - AbsoluteSize.X; } else if (TotalCursorPositionOnText - 6 - Scroll < 0) { Scroll = Math.Max(TotalCursorPositionOnText - (AbsoluteSize.X / 2), 0); } return(HoveringOver); }
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { Point ParentAbsolutePosition = Parent != null ? Parent.AbsolutePosition : new Point(); Point ParentAbsoluteSize = Parent != null ? Parent.AbsoluteSize : new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); int AbsoluteX = (int)(Position.Offset.X + (Position.Scale.X * ParentAbsoluteSize.X)) + ParentAbsolutePosition.X; int AbsoluteY = (int)(Position.Offset.Y + (Position.Scale.Y * ParentAbsoluteSize.Y)) + ParentAbsolutePosition.Y; int AbsoluteSizeX = (int)(Size.Offset.X + (Size.Scale.X * ParentAbsoluteSize.X)); int AbsoluteSizeY = (int)(Size.Offset.Y + (Size.Scale.Y * ParentAbsoluteSize.Y)); if (IsDisplayed) { for (int c = (ShadowEnabled ? 1 : 0); c >= 0; c--) { int TextXOffset = 0; for (int i = 0; i < Text.Length; i++) { // this code is kinda confusing but im not touching this thing again in 2000 years int XPosition = AbsoluteX + TextXOffset; CharacterTexture CharacterToDraw = Font[Text[i]]; //int RectLeft = Math.Min(CharacterToDraw.Texture.Width, Math.Max(0, AbsoluteX - XPosition + 3)); //int RectRight = Math.Min(CharacterToDraw.Texture.Bounds.Width - RectLeft, AbsoluteX + AbsoluteSizeX - XPosition - 3); /*spriteBatch.Draw(CharacterToDraw.Texture, new Rectangle(XPosition + RectLeft, AbsoluteY + 3, * RectRight, 8), * new Rectangle(RectLeft, 0, RectRight, CharacterToDraw.Texture.Bounds.Height), * TextColor * );*/ spriteBatch.Draw(CharacterToDraw.Texture, new Vector2(XPosition + c, AbsoluteY + c), (c == 0? TextColor : ShadowColor)); TextXOffset += CharacterToDraw.Texture.Width + CharacterToDraw.Spacing; } } } }
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { Point ParentAbsolutePosition = Parent != null ? Parent.AbsolutePosition : new Point(); Point ParentAbsoluteSize = Parent != null ? Parent.AbsoluteSize : new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); int AbsoluteX = (int)(Position.Offset.X + (Position.Scale.X * ParentAbsoluteSize.X)) + ParentAbsolutePosition.X; int AbsoluteY = (int)(Position.Offset.Y + (Position.Scale.Y * ParentAbsoluteSize.Y)) + ParentAbsolutePosition.Y; int AbsoluteSizeX = (int)(Size.Offset.X + (Size.Scale.X * ParentAbsoluteSize.X)); int AbsoluteSizeY = (int)(Size.Offset.Y + (Size.Scale.Y * ParentAbsoluteSize.Y)); if (IsDisplayed) { if (!IsFocused || (FocusedLeftEdge == null && FocusedCenter == null && FocusedRightEdge == null)) { spriteBatch.Draw(LeftEdge, new Vector2(AbsoluteX, AbsoluteY), Color.White); spriteBatch.Draw(Center, new Rectangle(AbsoluteX + LeftEdge.Width, AbsoluteY, Math.Max(AbsoluteSizeX - LeftEdge.Width - RightEdge.Width, 0), LeftEdge.Height), Center.Bounds, Color.White ); spriteBatch.Draw(RightEdge, new Vector2(AbsoluteX + AbsoluteSizeX - LeftEdge.Width, AbsoluteY), Color.White); } else { spriteBatch.Draw(FocusedLeftEdge, new Vector2(AbsoluteX, AbsoluteY), Color.White); spriteBatch.Draw(FocusedCenter, new Rectangle(AbsoluteX + LeftEdge.Width, AbsoluteY, Math.Max(AbsoluteSizeX - LeftEdge.Width - RightEdge.Width, 0), LeftEdge.Height), Center.Bounds, Color.White ); spriteBatch.Draw(FocusedRightEdge, new Vector2(AbsoluteX + AbsoluteSizeX - LeftEdge.Width, AbsoluteY), Color.White); } int TextXOffset = 0; int DrawCursorPosition = 0; string TextToDisplay = (Text == "" && !IsFocused)? PlaceHolderText : Text; Color TextColorToDisplay = (Text == "" && !IsFocused)? PlaceHolderTextColor : TextColor; for (int i = 0; i < TextToDisplay.Length; i++) { // this code is kinda confusing but im not touching this thing again in 2000 years int XPosition = AbsoluteX + 3 + TextXOffset - Scroll; CharacterTexture CharacterToDraw = Font[TextToDisplay[i]]; int RectLeft = Math.Min(CharacterToDraw.Texture.Width, Math.Max(0, AbsoluteX - XPosition + 3)); int RectRight = Math.Min(CharacterToDraw.Texture.Bounds.Width - RectLeft, AbsoluteX + AbsoluteSizeX - XPosition - 3); spriteBatch.Draw(CharacterToDraw.Texture, new Rectangle(XPosition + RectLeft, AbsoluteY + 3, RectRight, 8), new Rectangle(RectLeft, 0, RectRight, CharacterToDraw.Texture.Bounds.Height), TextColorToDisplay ); if (i == CursorPosition) { DrawCursorPosition = TextXOffset; } TextXOffset += CharacterToDraw.Texture.Width + CharacterToDraw.Spacing; } if (IsFocused && gameTime.TotalGameTime.TotalSeconds * 2 % 2 >= 1) { spriteBatch.Draw(PixelTexture, new Rectangle(AbsoluteX + 2 - Scroll + (CursorPosition > 0? (CursorPosition >= TextToDisplay.Length? TextXOffset : DrawCursorPosition) : 0), AbsoluteY + 3, 1, 8), PixelTexture.Bounds, Color.Black); } } }