Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created August 21, 2012 12:38
Show Gist options
  • Save demonixis/3415069 to your computer and use it in GitHub Desktop.
Save demonixis/3415069 to your computer and use it in GitHub Desktop.
Transition on MenuItem with YnTransition
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Yna;
using Yna.Display;
using Yna.Display.Effect;
namespace SpaceGame.States.Menu
{
public class MenuItem : YnGroup
{
public const int ItemWidth = 350;
public const int ItemHeight = 75;
public const int BorderSize = 5;
private static Color BackgroundColor = Color.White;
private static Color ForegroundColor = new Color(0, 108, 165);
private YnSprite _foreground;
private YnSprite _background;
private YnText _contentText;
private bool _selected;
private int _itemPosition;
// Transition
private YnTransition _transition;
public bool Selected
{
get { return _selected; }
set { _selected = value; }
}
public int ItemPosition
{
get { return _itemPosition; }
}
public void SetSelected(bool selected)
{
_selected = selected;
if (_selected)
{
_contentText.Color = Color.AntiqueWhite;
_transition.StartTransitionOn();
}
else
{
_contentText.Color = Color.White;
_transition.StartTransitionOff();
}
}
public MenuItem(string text, int x, int y, int position, bool selected = false)
{
_itemPosition = position;
_selected = selected;
Rectangle = new Rectangle(x, y, ItemWidth, ItemHeight);
_background = new YnSprite(Rectangle, BackgroundColor);
_background.Alpha = 0.0f;
//_background.Visible = false;
Add(_background);
_foreground = new YnSprite(new Rectangle(x, y, ItemWidth - BorderSize, ItemHeight - BorderSize), ForegroundColor);
Add(_foreground);
_contentText = new YnText("Fonts/Menu", Vector2.Zero, text);
_contentText.Color = Color.White;
Add(_contentText);
_transition = new YnTransition(350.0f, 250.0f);
}
public override void Initialize()
{
base.Initialize();
_foreground.Position = new Vector2(
(int)(_background.X + _background.Width / 2 - _foreground.Width / 2),
(int)(_background.Y + _background.Height / 2 - _foreground.Height / 2));
CenterText();
}
private void CenterText()
{
_contentText.Position = new Vector2(
(_background.X + _background.Width / 2) - _contentText.ScaledWidth / 2,
(_background.Y + _background.Height / 2) - _contentText.ScaledHeight / 2);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
// Mise à jour de la transition
_transition.Update(gameTime);
if (_transition.TransitionState == TransitionState.TransitionOn)
_background.Alpha = _transition.Alpha;
else if (_transition.TransitionState == TransitionState.TransitionOff)
_background.Alpha = _transition.Alpha;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment