Skip to content

Instantly share code, notes, and snippets.

@klutch
Created September 26, 2013 04:04
Show Gist options
  • Save klutch/6709748 to your computer and use it in GitHub Desktop.
Save klutch/6709748 to your computer and use it in GitHub Desktop.
Some UI snippets. Warning: contains very ugly code.
abstract public class Screen : ITranslatable
{
protected ScreenSystem _screenSystem;
protected SpriteBatch _spriteBatch;
protected GamePadState _newGamepadState;
protected GamePadState _oldGamepadState;
protected KeyboardState _newKeyState;
protected KeyboardState _oldKeyState;
protected MouseState _newMouseState;
protected MouseState _oldMouseState;
protected ScreenType _screenType;
protected float _translationX;
protected float _translationY;
protected List<Transition> _transitions;
protected List<Transition> _transitionsToRemove;
private Action _onFinished;
protected bool _skipUpdate = true;
public ScreenType screenType { get { return _screenType; } set { _screenType = value; } }
public ScreenSystem screenSystem { get { return _screenSystem; } }
public KeyboardState newKeyState { get { return _newKeyState; } }
public KeyboardState oldKeyState { get { return _oldKeyState; } }
public MouseState newMouseState { get { return _newMouseState; } }
public MouseState oldMouseState { get { return _oldMouseState; } }
public float translationX { get { return _translationX; } set { _translationX = value; } }
public float translationY { get { return _translationY; } set { _translationY = value; } }
public Screen screen { get { return this; } }
public bool skipUpdate { get { return _skipUpdate; } set { _skipUpdate = value; } }
public Screen(ScreenSystem screenSystem, ScreenType screenType)
{
_screenSystem = screenSystem;
_spriteBatch = screenSystem.spriteBatch;
_screenType = screenType;
_transitions = new List<Transition>();
_transitionsToRemove = new List<Transition>();
}
virtual public void applyIntroTransitions()
{
}
virtual public void applyOutroTransitions(Action onFinished = null)
{
_onFinished = onFinished;
}
virtual public void update()
{
_oldGamepadState = _newGamepadState;
_oldKeyState = _newKeyState;
_oldMouseState = _newMouseState;
_newGamepadState = GamePad.GetState(PlayerIndex.One);
_newKeyState = Keyboard.GetState();
_newMouseState = Mouse.GetState();
// Remove finished transitions
for (int i = 0; i < _transitionsToRemove.Count; i++)
{
_transitions.Remove(_transitionsToRemove[i]);
}
_transitionsToRemove.Clear();
// Attempt to call onFinished callback
if (_transitions.Count == 0 && _onFinished != null)
{
_onFinished();
_onFinished = null;
}
// Update transitions
for (int i = 0; i < _transitions.Count; i++)
{
Transition transition = _transitions[i];
// Break if this is a queued transition that's not ready to be processed.
if (transition.queued && i != 0)
{
break;
}
// Handle starting/finished transitions
if (transition.finished)
{
transition.end();
_transitionsToRemove.Add(transition);
continue;
}
else if (transition.starting)
{
transition.begin();
}
// Update transitions
if ((transition.queued && i == 0) || !transition.queued)
{
transition.update();
}
}
}
virtual public void draw()
{
}
public class TextureButton : ITranslatable, IAlphaFadable
{
protected Screen _screen;
protected SpriteBatch _spriteBatch;
private Texture2D _selectedTexture;
private Texture2D _deselectedTexture;
private bool _selected;
private UIAlignment _alignment;
private Texture2D _pixel;
private int _xOffset;
private int _yOffset;
private Rectangle _localHitBox;
private Action _onActivate;
private Action _onMouseOver;
private Action _onMouseOut;
protected float _translationX;
protected float _translationY;
protected float _alpha = 1f;
public bool selected { get { return _selected; } }
public float translationX { get { return _translationX; } set { _translationX = value; } }
public float translationY { get { return _translationY; } set { _translationY = value; } }
public Screen screen { get { return _screen; } }
public float alpha { get { return _alpha; } set { _alpha = value; } }
public int x
{
get
{
if (_alignment == UIAlignment.TopLeft || _alignment == UIAlignment.MiddleLeft || _alignment == UIAlignment.BottomLeft)
return _xOffset;
else if (_alignment == UIAlignment.TopCenter || _alignment == UIAlignment.MiddleCenter || _alignment == UIAlignment.BottomCenter)
return _xOffset + (int)(_spriteBatch.GraphicsDevice.Viewport.Width / 2f);
else if (_alignment == UIAlignment.TopRight || _alignment == UIAlignment.MiddleRight || _alignment == UIAlignment.BottomRight)
return _xOffset + _spriteBatch.GraphicsDevice.Viewport.Width;
return _xOffset;
}
}
public int y
{
get
{
if (_alignment == UIAlignment.TopLeft || _alignment == UIAlignment.TopCenter || _alignment == UIAlignment.TopRight)
return _yOffset;
else if (_alignment == UIAlignment.MiddleLeft || _alignment == UIAlignment.MiddleCenter || _alignment == UIAlignment.MiddleRight)
return _yOffset + (int)(_spriteBatch.GraphicsDevice.Viewport.Height / 2f);
else if (_alignment == UIAlignment.BottomLeft || _alignment == UIAlignment.BottomCenter || _alignment == UIAlignment.BottomRight)
return _yOffset + _spriteBatch.GraphicsDevice.Viewport.Height;
return _yOffset;
}
}
public TextureButton(Screen screen, SpriteBatch spriteBatch, UIAlignment alignment, int x, int y, Texture2D selectedTexture, Texture2D deselectedTexture, Rectangle localHitBox, Action onActivate) :
this(screen, spriteBatch, alignment, x, y, selectedTexture, deselectedTexture, localHitBox, onActivate, null, null)
{
_onMouseOver = () => { select(); };
_onMouseOut = () => { deselect(); };
}
public TextureButton(Screen screen, SpriteBatch spriteBatch, UIAlignment alignment, int x, int y, Texture2D selectedTexture, Texture2D deselectedTexture, Rectangle localHitBox, Action onActivate, Action onMouseOver, Action onMouseOut)
{
_screen = screen;
_spriteBatch = spriteBatch;
_alignment = alignment;
_xOffset = x;
_yOffset = y;
_selectedTexture = selectedTexture;
_deselectedTexture = deselectedTexture;
_localHitBox = localHitBox;
_onActivate = onActivate;
_onMouseOver = onMouseOver;
_onMouseOut = onMouseOut;
_pixel = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
_pixel.SetData<Color>(new[] { Color.White });
}
public void activate()
{
_onActivate();
}
virtual public void select()
{
_selected = true;
}
virtual public void deselect()
{
_selected = false;
}
public void mouseOut()
{
_onMouseOut();
}
public void mouseOver()
{
_onMouseOver();
}
public bool hitTest(Vector2 point)
{
Rectangle pointRect = new Rectangle((int)point.X, (int)point.Y, 1, 1);
Rectangle screenHitBox = new Rectangle(_localHitBox.X + x, _localHitBox.Y + y, _localHitBox.Width, _localHitBox.Height);
return screenHitBox.Intersects(pointRect);
}
virtual public void draw()
{
Texture2D texture = _selected ? _selectedTexture : _deselectedTexture;
_spriteBatch.Draw(
texture,
new Rectangle(x + (int)_screen.translationX + (int)_translationX, y + (int)_screen.translationY + (int)_translationY, texture.Width, texture.Height),
texture.Bounds,
Color.White * _alpha,
0f,
Vector2.Zero,
SpriteEffects.None,
0f);
//Rectangle hitBox = new Rectangle(_x, _y, _hitBoxWidth, _hitBoxHeight);
//_spriteBatch.Draw(_pixel, hitBox, hitBox, Color.Green * 0.5f, 0f, new Vector2(_hitBoxOffsetX, _hitBoxOffsetY), SpriteEffects.None, 0f);
}
}
public class AlphaFadeTransition : Transition
{
private IAlphaFadable _target;
private float _from;
private float _to;
private float _d;
public AlphaFadeTransition(IAlphaFadable obj, float from, float to, bool queue = true, float speed = 0.05f, Action onBegin = null, Action onEnd = null)
: base(obj, queue, speed, onBegin, onEnd)
{
_target = obj;
_from = from;
_to = to;
_d = (_to - _from) * speed;
}
public override void begin()
{
_target.alpha = _from;
base.begin();
}
public override void end()
{
_target.alpha = _to;
base.end();
}
public override void update()
{
_progress += _speed;
_target.alpha += _d;
}
public override void draw()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment