public void InitializeLayout(int ResizeIteration) { int WindowWidth = Game1.viewport.Size.Width; int WindowHeight = Game1.viewport.Size.Height; Vector2 InstructionSize = InstructionFont.MeasureString(InstructionText) * InstructionScale; Vector2 DefaultButtonTextSize = ButtonFont.MeasureString(DefaultButtonText) * ButtonFontScale; Vector2 CloseButtonTextSize = ButtonFont.MeasureString(CloseButtonText) * ButtonFontScale; int Width = Max(PreviewSize, (int)InstructionSize.X, TextureWidth) + Padding * 2; int Height = Padding + PreviewSize + Spacing + (int)InstructionSize.Y + Spacing + TextureHeight + Spacing + (int)Math.Max(DefaultButtonTextSize.Y + ButtonPadding * 2, CloseButtonTextSize.Y + ButtonPadding * 2) + Padding; this.Bounds = new Rectangle((WindowWidth - Width) / 2, (WindowHeight - Height) / 2, Width, Height); this.PreviewDestination = new Rectangle(Bounds.X + (Bounds.Width - PreviewSize) / 2, Bounds.Y + Padding, PreviewSize, PreviewSize); this.InstructionDestination = new Vector2(Bounds.X + (Bounds.Width - (int)InstructionSize.X) / 2, PreviewDestination.Bottom + Spacing); this.TextureDestination = new Rectangle(Bounds.X + (Bounds.Width - TextureWidth) / 2, (int)(InstructionDestination.Y + InstructionSize.Y + Spacing), TextureWidth, TextureHeight); this.DefaultButtonDestination = new Rectangle(TextureDestination.X, TextureDestination.Bottom + Spacing, (int)DefaultButtonTextSize.X + ButtonPadding * 2 * 2, (int)DefaultButtonTextSize.Y + ButtonPadding * 2); this.DefaultButtonTextDestination = new Vector2(DefaultButtonDestination.X + ButtonPadding * 2, DefaultButtonDestination.Y + ButtonPadding + 2); int CloseButtonWidth = (int)CloseButtonTextSize.X + ButtonPadding * 2 * 2; this.CloseButtonDestination = new Rectangle(TextureDestination.Right - CloseButtonWidth, TextureDestination.Bottom + Spacing, CloseButtonWidth, (int)CloseButtonTextSize.Y + ButtonPadding * 2); this.CloseButtonTextDestination = new Vector2(CloseButtonDestination.X + ButtonPadding * 2, CloseButtonDestination.Y + ButtonPadding + 2); }
public virtual void Update(MouseState mouse, KeyboardState keyboard) { Hitbox = new Rectangle((int)Position.X, (int)Position.Y, TexLeft.Width + TexMid.Width * BtnWidth + TexRight.Width, TexMid.Height); HitboxMouse = new Rectangle(mouse.X, mouse.Y, 1, 1); BtnColor = HasFocus ? new Color(255, 255, 255, 255) : new Color(200, 200, 200, 255); if (HitboxMouse.Intersects(Hitbox)) { if (mouse.LeftButton == ButtonState.Pressed) { HasFocus = true; } } else { if (mouse.LeftButton == ButtonState.Pressed) { HasFocus = false; } } NamePosition = Position + new Vector2(0, -TextboxNameFont.MeasureString(TextboxName).Y); TextPosition = Position + new Vector2(Hitbox.Width / 2, Hitbox.Height / 2) - ButtonFont.MeasureString(Text) / 2; }
private void UpdateText(KeyboardState keyboard) { if (HasFocus) { PressedKeys = (IList <Keys>)keyboard.GetPressedKeys(); foreach (Keys k in PressedKeys) { if (!LastPressedKeys.Contains(k)) { if (Text.Length < MaxTextLength) { switch (k) { case Keys.OemMinus: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '_'; } else { Text += '-'; } break; case Keys.D1: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '!'; } else { Text += '1'; } break; case Keys.D2: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '"'; } else { Text += '2'; } break; case Keys.D3: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '§'; } else { Text += '3'; } break; case Keys.D4: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '$'; } else { Text += '4'; } break; case Keys.D5: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '%'; } else { Text += '5'; } break; case Keys.D6: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '&'; } else { Text += '6'; } break; case Keys.D7: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '/'; } else { Text += '7'; } break; case Keys.D8: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '('; } else { Text += '8'; } break; case Keys.D9: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += ')'; } else { Text += '9'; } break; case Keys.D0: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += '='; } else { Text += '0'; } break; case Keys.OemSemicolon: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += ';'; } else { Text += ','; } break; case Keys.NumPad0: Text += "0"; break; case Keys.NumPad1: Text += "1"; break; case Keys.NumPad2: Text += "2"; break; case Keys.NumPad3: Text += "3"; break; case Keys.NumPad4: Text += "4"; break; case Keys.NumPad5: Text += "5"; break; case Keys.NumPad6: Text += "6"; break; case Keys.NumPad7: Text += "7"; break; case Keys.NumPad8: Text += "8"; break; case Keys.NumPad9: Text += "9"; break; case Keys.OemPeriod: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += ":"; } else { Text += '.'; } break; case Keys.Back: if (Text.Length > 0) { Text = Text.Remove(Text.Length - 1); } break; case Keys.LeftShift: break; case Keys.RightShift: break; case Keys.V: if (PressedKeys.Contains(Keys.LeftControl) || PressedKeys.Contains(Keys.RightControl)) { break; //todo } else { goto default; } case Keys.RightControl: case Keys.LeftControl: break; default: if (PressedKeys.Contains(Keys.LeftShift) || PressedKeys.Contains(Keys.RightShift)) { Text += char.ToUpper((char)k); } else { Text += char.ToLower((char)k); } break; } } } } LastPressedKeys = PressedKeys; } TextPosition = Position + new Vector2(Hitbox.Width / 2, Hitbox.Height / 2) - ButtonFont.MeasureString(Text) / 2; NamePosition = Position + new Vector2(0, -TextboxNameFont.MeasureString(TextboxName).Y); }