/// <summary> /// Gamestate pattern : Draw the widget. /// </summary> /// <param name="gameTime">Elasped time since last draw</param> /// <param name="spriteBatch">The sprite batch</param> public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { // If the widget is invisible, don't render it nor it's children if (_visible) { // Retreive the used skin YnSkin skin = YnGui.GetSkin(_skinName); // Draw the borders if needed if (_hasBorders) { DrawBorders(gameTime, spriteBatch, skin); } // Draw the background id needed if (_hasBackground) { DrawBackground(gameTime, spriteBatch, skin); } // Draw the component itself DrawWidget(gameTime, spriteBatch, skin); // Draw the child components DrawChildren(gameTime, spriteBatch); } }
/// <summary> /// Set up the cursor size. /// </summary> /// <param name="skin">The skin to use</param> protected override void ApplySkin(YnSkin skin) { _cursor.Width = Height; _cursor.Height = Height; _cursor.Move(-_cursor.Width / 2, 0); UpdateCursor(); }
/// <summary> /// Draw the checked square. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The skin to use</param> protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Draw the square only if the box is checked if (_checked) { // Draw a square with padding (Width /5) int padding = Width / 5; Texture2D fg = GetSkin().BackgroundDefault; Rectangle source = new Rectangle(0, 0, fg.Width, fg.Height); Rectangle dest = new Rectangle((int)ScreenPosition.X + padding, (int)ScreenPosition.Y + padding, Width - padding * 2, Height - padding * 2); spriteBatch.Draw(fg, dest, source, Color.White); } }
/// <summary> /// Computes the text size and relative coordinates according to the alignment defined. /// </summary> /// <param name="skin">The skin</param> protected override void ApplySkin(YnSkin skin) { // If a custom font is currently in use, take it to measure the text SpriteFont font = (_useCustomColor && _customFont != null) ? _customFont : skin.FontDefault; // Measure the text to define widget's width & height Vector2 size = font.MeasureString(Text); _textWidth = (int)(size.X * _scale.X); _textHeight = (int)(size.Y * _scale.Y); // if (_textAlign != YnTextAlign.None) { // An alignment is defined, recompute coordinates ComputeTextAlignment(); } else { // No alignment, the widget size is initialized with the text size Width = _textWidth; Height = _textHeight; } }
/// <summary> /// Performs skin related computing on the widget bounds. /// </summary> /// <param name="skin">The new skin</param> protected virtual void ApplySkin(YnSkin skin) { // Override if needed }
/// <summary> /// See documentation in YnWidget. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The skin used to render the widget</param> protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // The label does not react to user input so we simply get the default text color Color color = (_useCustomColor) ? _textColor : skin.TextColorDefault; SpriteFont font = (_customFont == null) ? skin.FontDefault : _customFont; // Draw the text at the aligned position spriteBatch.DrawString(font, _text, ScreenPosition + _textPosition, color, _rotation, Vector2.Zero, _scale, SpriteEffects.None, 1.0f); }
/// <summary> /// Renders the widget itself. This method must be overriden in subclasses to define specific /// rendering. /// </summary> /// <param name="gameTime">Elasped time since last draw</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The rendering skin used</param> protected abstract void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin);
/// <summary> /// As YnPanels are juste containers, this method does nothing. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Nothing here, just draw the children in super class }
/// <summary> /// Draw the background. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The skin to use</param> protected override void DrawBackground(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Get the clicked background as background. Texture2D bg = skin.BackgroundClicked; // Create source and destination rectangles for rendering Rectangle source = new Rectangle(0, 0, bg.Width, bg.Height); Rectangle dest = new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y, Width, Height); // Render spriteBatch.Draw(bg, dest, source, Color.White); }
/// <summary> /// Draw the widget. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The skin to use</param> protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { Texture2D fg = skin.BackgroundDefault; Rectangle source; Rectangle dest; if (_maxValue != 0) { if (_orientation == YnOrientation.Vertical) { int height = (int)(Height * _currentValue / _maxValue); source = new Rectangle(0, 0, fg.Width, fg.Height); dest = new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y + Height - height, Width, height); } else { int width = (int)(Width * _currentValue / _maxValue); source = new Rectangle(0, 0, fg.Width, fg.Height); dest = new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y, width, Height); } // Draw the bar spriteBatch.Draw(fg, dest, source, Color.White); } }
protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // TODO }
/// <summary> /// Apply the skin to the inner label. /// </summary> /// <param name="skin">The skin</param> protected override void ApplySkin(YnSkin skin) { _label.SkinName = _skinName; _label.ApplySkin(); }
/// <summary> /// Computes the text size and relative coordinates according to the alignment defined. /// </summary> /// <param name="skin">The skin</param> protected override void ApplySkin(YnSkin skin) { // If a custom font is currently in use, take it to measure the text SpriteFont font = (_useCustomColor && _customFont != null) ? _customFont : skin.FontDefault; // Measure the text to define widget's width & height Vector2 size = font.MeasureString(Text); _textWidth = (int)( size.X * _scale.X); _textHeight = (int)( size.Y * _scale.Y); // if (_textAlign != YnTextAlign.None) { // An alignment is defined, recompute coordinates ComputeTextAlignment(); } else { // No alignment, the widget size is initialized with the text size Width = _textWidth; Height = _textHeight; } }
/// <summary> /// Draw the background. /// </summary> /// <param name="gameTime">The game time</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The skin to use</param> protected override void DrawBackground(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Always draw the clicked background Texture2D background = GetSkin().BackgroundClicked; Rectangle source = new Rectangle(0, 0, background.Width, background.Height); Rectangle dest = new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y, Width, Height); spriteBatch.Draw(background, dest, source, Color.White); }
/// <summary> /// Draw the background rail. As the rail does not fill the entire widget bounds /// it cannot be handled by the DrawBackground method. /// </summary> /// <param name="gameTime"></param> /// <param name="spriteBatch"></param> /// <param name="skin"></param> protected override void DrawWidget(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Background rail rectangle int bgHeight = Height / 8; Rectangle rect = new Rectangle( (int)ScreenPosition.X, (int)ScreenPosition.Y + Height / 2 - bgHeight / 2, Width, bgHeight ); Texture2D tex = GetSkin().BackgroundClicked; spriteBatch.Draw(tex, rect, Color.White); }
/// <summary> /// Renders the widget borders. This method may be overridden to customise the way the borders are /// drawn. /// </summary> /// <param name="gameTime">Elasped time since last draw</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The rendering skin used</param> protected virtual void DrawBorders(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Used vars Vector2 pos = Vector2.Zero; Rectangle source = Rectangle.Empty; Rectangle dest = Rectangle.Empty; YnBorder border; // Get the appropriate border definition according to the widget's state (if it's animated) if (Clicked && _animated) { // Clicked state border = skin.BorderClicked; } else if (Hovered && _animated) { // Hovered state border = skin.BorderHover; } else { // Default state border = skin.BorderDefault; } // Draw corners // Top left pos.X = ScreenPosition.X - border.TopLeft.Width; pos.Y = ScreenPosition.Y - border.TopLeft.Height; spriteBatch.Draw(border.TopLeft, pos, Color.White); // top right pos.X = ScreenPosition.X + Width; pos.Y = ScreenPosition.Y - border.TopRight.Height; spriteBatch.Draw(border.TopRight, pos, Color.White); // Bottom right pos.X = ScreenPosition.X + Width; pos.Y = ScreenPosition.Y + Height; spriteBatch.Draw(border.BottomRight, pos, Color.White); // Bottom left pos.X = ScreenPosition.X - border.BottomLeft.Width; pos.Y = ScreenPosition.Y + Height; spriteBatch.Draw(border.BottomLeft, pos, Color.White); // Draw borders // Top source = border.Top.Bounds; dest = new Rectangle( (int)ScreenPosition.X, (int)ScreenPosition.Y - border.Top.Height, Width, border.Top.Height ); spriteBatch.Draw(border.Top, dest, source, Color.White); // Right source = border.Right.Bounds; dest = new Rectangle( (int)ScreenPosition.X + Width, (int)ScreenPosition.Y, border.Right.Width, Height ); spriteBatch.Draw(border.Right, dest, source, Color.White); // Bottom source = border.Bottom.Bounds; dest = new Rectangle( (int)ScreenPosition.X, (int)ScreenPosition.Y + Height, Width, border.Bottom.Height ); spriteBatch.Draw(border.Bottom, dest, source, Color.White); // Left source = border.Left.Bounds; dest = new Rectangle( (int)ScreenPosition.X - border.Left.Width, (int)ScreenPosition.Y, border.Left.Width, Height ); spriteBatch.Draw(border.Left, dest, source, Color.White); }
/// <summary> /// Renders the widget background. This method may be overridden to customise the way the background is /// drawn. /// </summary> /// <param name="gameTime">Elasped time since last draw</param> /// <param name="spriteBatch">The sprite batch</param> /// <param name="skin">The rendering skin used</param> protected virtual void DrawBackground(GameTime gameTime, SpriteBatch spriteBatch, YnSkin skin) { // Get the appropriate background according to the current widget's state (if it's animated) Texture2D background; if (Clicked && _animated) { // Clicked state background = skin.BackgroundClicked; } else if (Hovered && _animated) { // Hovered state background = skin.BackgroundHover; } else { // Default state background = skin.BackgroundDefault; } Rectangle source = new Rectangle(0, 0, background.Width, background.Height); Rectangle dest = new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y, Width, Height); spriteBatch.Draw(background, dest, source, Color.White); }