Skip to content

Commit

Permalink
Merge branch 'master' into add-icon
Browse files Browse the repository at this point in the history
  • Loading branch information
yvan674 authored Jun 16, 2018
2 parents 77f5d11 + e4de529 commit e55703c
Show file tree
Hide file tree
Showing 14 changed files with 991 additions and 172 deletions.
6 changes: 3 additions & 3 deletions Singularity/Singularity/Libraries/Animations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ internal static class Animations
/// <returns>The current value of the easing animation</returns>
public static double Easing(float startValue,
float endValue,
int startTime,
int duration,
double startTime,
double duration,
GameTime gameTime)
{
// range is a separate var to make it easier to read.
var range = endValue - startValue;

// x is a separate var to make it easier to read as well.
var x = gameTime.TotalGameTime.TotalMilliseconds - startTime;
return startValue + range * -(Math.Cos(x * Math.PI / duration) / 2 + 0.5d);
return startValue + range * (-Math.Cos(x * Math.PI / duration) / 2 + 0.5d);
}
}
}
33 changes: 28 additions & 5 deletions Singularity/Singularity/Screen/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Singularity.Input;
using Singularity.Libraries;
using Singularity.Property;


Expand Down Expand Up @@ -38,6 +39,12 @@ class Button : IWindowItem

private Rectangle mBounds;
private bool mClicked;
private bool mWithBorder;

/// <summary>
/// Opacity of the button useful for transitions or transparent buttons
/// </summary>
public float Opacity { private get; set; }

// these events are sent out when they occur to an
// instance of a button
Expand All @@ -60,7 +67,7 @@ class Button : IWindowItem
/// <param name="scale"> scale of the texture</param>
/// <param name="buttonTexture"></param>
/// <param name="position"></param>
public Button(float scale, Texture2D buttonTexture, Vector2 position)
public Button(float scale, Texture2D buttonTexture, Vector2 position, bool withBorder)
{
mIsText = false;
mScale = scale;
Expand All @@ -70,6 +77,7 @@ public Button(float scale, Texture2D buttonTexture, Vector2 position)
mHeight = mButtonTexture.Height;
mColor = Color.White;
CreateRectangularBounds();

}

/// <summary>
Expand All @@ -90,6 +98,18 @@ public Button(string buttonText, SpriteFont font, Vector2 position)
CreateRectangularBounds();
}

public Button(string buttonText, SpriteFont font, Vector2 position, Color color)
{
mIsText = true;
mButtonText = buttonText;
mFont = font;
mPosition = position;
mWidth = (int)mFont.MeasureString(mButtonText).X;
mHeight = (int)mFont.MeasureString(mButtonText).Y;
mColor = color;
CreateRectangularBounds();
}


/// <summary>
/// Creates the bounding box that the button is contained in
Expand All @@ -111,7 +131,6 @@ protected virtual void OnButtonReleased()

}


/// <summary>
/// Sends out event that mouse is hovering over the button
/// </summary>
Expand Down Expand Up @@ -145,12 +164,16 @@ public void Draw(SpriteBatch spriteBatch)
spriteBatch.Draw(mButtonTexture,
mPosition,
null,
mColor,
mColor * Opacity,
0f,
new Vector2(0, 0),
mScale,
SpriteEffects.None,
0f);
if (mWithBorder)
{
spriteBatch.DrawRectangle(new Vector2(mPosition.X - 1, mPosition.Y - 1), new Vector2(mButtonTexture.Width + 1, mButtonTexture.Height + 1), Color.White, 1);
}

}

Expand All @@ -160,7 +183,7 @@ public void Draw(SpriteBatch spriteBatch)
spriteBatch.DrawString(mFont,
origin: Vector2.Zero,
position: mPosition,
color: mColor,
color: mColor * Opacity,
text: mButtonText,
rotation: 0f,
scale: 1f,
Expand Down Expand Up @@ -222,4 +245,4 @@ public void Update(GameTime gametime)
}
}
}
}
}
24 changes: 24 additions & 0 deletions Singularity/Singularity/Screen/ITransitionableMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Xna.Framework;

namespace Singularity.Screen
{
/// <inheritdoc cref = "IScreen" />
/// <summary>
/// Any menu screen that has transitions should implement this interface
/// </summary>
internal interface ITransitionableMenu : IScreen
{
/// <summary>
/// Gives the current state of the transition of this screen
/// </summary>
bool TransitionRunning { get; }

/// <summary>
/// Method called to tell a screen to transition and which screen it should transition to.
/// </summary>
/// <param name="originScreen"></param>
/// <param name="targetScreen"></param>
/// <param name="gameTime">GameTime when the transition is called indicating transition start time</param>
void TransitionTo(EScreen originScreen, EScreen targetScreen, GameTime gameTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Singularity.Screen.ScreenClasses
{
/// <inheritdoc cref="IScreen"/>
/// <inheritdoc cref="ITransitionableMenu"/>
/// <summary>
/// Used to show achievements that the player has earned.
/// It shows achievements already earned in a list format, not
Expand All @@ -18,7 +18,7 @@ namespace Singularity.Screen.ScreenClasses
/// and their description. If an achievement has not been earned yet,
/// the achievement texture is blacked out.
/// </summary>
class AchievementsScreen : IScreen
internal sealed class AchievementsScreen : ITransitionableMenu
{
/// <summary>
/// Updates the contents of the screen.
Expand Down Expand Up @@ -65,5 +65,11 @@ public bool DrawLower()
{
throw new NotImplementedException();
}

public bool TransitionRunning { get; }
public void TransitionTo(EScreen originScreen, EScreen targetScreen, GameTime gameTime)
{
throw new NotImplementedException();
}
}
}
136 changes: 120 additions & 16 deletions Singularity/Singularity/Screen/ScreenClasses/GameModeSelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

namespace Singularity.Screen.ScreenClasses
{
/// <inheritdoc cref="IScreen"/>
/// <inheritdoc cref="ITransitionableMenu"/>
/// <summary>
/// Shown after the "New Game" button on the main
/// menu has been clicked. It shows the option to either
/// play a new campaign or a new skirmish. It uses two text buttons
/// and a back button.
/// </summary>

class GameModeSelectScreen : IScreen
internal sealed class GameModeSelectScreen : ITransitionableMenu
{

private string mStoryString;
private string mFreePlayString;
private string mBackString;
private string mWindowTitleString;
private readonly string mStoryString;
private readonly string mFreePlayString;
private readonly string mBackString;
private readonly string mWindowTitleString;

private List<Button> mButtonList;
private readonly List<Button> mButtonList;

private SpriteFont mLibSans36;
private SpriteFont mLibSans20;
Expand All @@ -35,19 +35,39 @@ class GameModeSelectScreen : IScreen
private Button mFreePlayButton;
private Button mBackButton;

private Vector2 mMenuBoxPosition;
// Transition variables
private readonly Vector2 mMenuBoxPosition;
private float mMenuOpacity;
private double mTransitionStartTime;
private double mTransitionDuration;
private EScreen mTargetScreen;
public bool TransitionRunning { get; private set; }

// Selector Triangle
private Texture2D mSelectorTriangle;
private float mButtonVerticalCenter;
private Vector2 mSelectorPosition;

// Layout Variables
private float mButtonTopPadding;
private float mButtonLeftPadding;


public GameModeSelectScreen(Vector2 screenResolution)
{
mMenuBoxPosition = new Vector2(screenResolution.X / 2 - 204, screenResolution.Y / 4);

mStoryString = "Campaign Mode";
mFreePlayString = "Skirmish";
mBackString = "Back";
mWindowTitleString = "New Game";

mButtonList = new List<Button>(3);
mButtonLeftPadding = mMenuBoxPosition.X + 60;
mButtonTopPadding = mMenuBoxPosition.Y + 90;

mMenuBoxPosition = new Vector2(screenResolution.X / 2 - 150, screenResolution.Y / 2 - 175);
mButtonList = new List<Button>(3);

mMenuOpacity = 0;
}

/// <summary>
Expand All @@ -57,9 +77,48 @@ public GameModeSelectScreen(Vector2 screenResolution)
/// that take place over time </param>
public void Update(GameTime gametime)
{
if (TransitionRunning)
{
Transition(gametime);
}

foreach (Button button in mButtonList)
{
button.Update(gametime);
button.Opacity = mMenuOpacity;
}
}

/// <summary>
/// Code that actually does the transition
/// </summary>
/// <param name="gameTime">Current gameTime</param>
private void Transition(GameTime gameTime)
{
switch (mTargetScreen)
{
case EScreen.GameModeSelectScreen:
if (gameTime.TotalGameTime.TotalMilliseconds >= mTransitionStartTime + mTransitionDuration)
{
TransitionRunning = false;
mMenuOpacity = 1f;
}

mMenuOpacity =
(float)Animations.Easing(0, 1f, mTransitionStartTime, mTransitionDuration, gameTime);
break;
case EScreen.MainMenuScreen:
if (gameTime.TotalGameTime.TotalMilliseconds >= mTransitionStartTime + mTransitionDuration)
{
TransitionRunning = false;
mMenuOpacity = 0f;
}

mMenuOpacity =
(float)Animations.Easing(1, 0f, mTransitionStartTime, mTransitionDuration, gameTime);
break;
default:
throw new ArgumentOutOfRangeException();
}
}

Expand All @@ -76,17 +135,28 @@ public void Draw(SpriteBatch spriteBatch)
button.Draw(spriteBatch);
}

// Draw selector triangle
spriteBatch.Draw(mSelectorTriangle,
position: mSelectorPosition,
sourceRectangle: null,
color: Color.White * mMenuOpacity,
rotation: 0f,
origin: new Vector2(0, 11),
scale: 1f,
effects: SpriteEffects.None,
layerDepth: 0f);

// Draw menu window
spriteBatch.StrokedRectangle(mMenuBoxPosition,
new Vector2(300, 350),
new Vector2(408, 420),
Color.White,
Color.White,
.5f,
.20f);
spriteBatch.DrawString(mLibSans36,
mWindowTitleString,
new Vector2(mMenuBoxPosition.X + 30, mMenuBoxPosition.Y + 10), Color.White);
new Vector2(mMenuBoxPosition.X + 20, mMenuBoxPosition.Y + 10),
new Color(new Vector3(.9137f, .9058f, .8314f)) * mMenuOpacity);

spriteBatch.End();
}
Expand All @@ -99,17 +169,25 @@ public void LoadContent(ContentManager content)
{
mLibSans36 = content.Load<SpriteFont>("LibSans36");
mLibSans20 = content.Load<SpriteFont>("LibSans20");
mStoryButton = new Button(mStoryString, mLibSans20, new Vector2(mMenuBoxPosition.X + 30, mMenuBoxPosition.Y + 90));
mFreePlayButton = new Button(mFreePlayString, mLibSans20, new Vector2(mMenuBoxPosition.X + 30, mMenuBoxPosition.Y + 140));
mBackButton = new Button(mBackString, mLibSans20, new Vector2(mMenuBoxPosition.X + 30, mMenuBoxPosition.Y + 190));
mButtonVerticalCenter = mLibSans20.MeasureString("Gg").Y / 2;

mSelectorPosition = new Vector2(mMenuBoxPosition.X + 22, mButtonTopPadding + mButtonVerticalCenter);
mSelectorTriangle = content.Load<Texture2D>("SelectorTriangle");

mStoryButton = new Button(mStoryString, mLibSans20, new Vector2(mButtonLeftPadding, mButtonTopPadding), new Color(new Vector3(.9137f, .9058f, .8314f)));
mFreePlayButton = new Button(mFreePlayString, mLibSans20, new Vector2(mButtonLeftPadding, mButtonTopPadding + 50), new Color(new Vector3(.9137f, .9058f, .8314f)));
mBackButton = new Button(mBackString, mLibSans20, new Vector2(mButtonLeftPadding, mButtonTopPadding + 100), new Color(new Vector3(.9137f, .9058f, .8314f)));
mButtonList.Add(mStoryButton);
mButtonList.Add(mFreePlayButton);
mButtonList.Add(mBackButton);


mStoryButton.ButtonReleased += MainMenuManagerScreen.OnStoryButtonReleased;
mFreePlayButton.ButtonReleased += MainMenuManagerScreen.OnFreePlayButtonReleased;
mBackButton.ButtonReleased += MainMenuManagerScreen.OnBackButtonReleased;

mStoryButton.ButtonHovering += OnStoryHover;
mFreePlayButton.ButtonHovering += OnFreePlayHover;
mBackButton.ButtonHovering += OnBackHover;
}

/// <summary>
Expand All @@ -129,5 +207,31 @@ public bool DrawLower()
{
return true;
}

public void TransitionTo(EScreen originScreen, EScreen targetScreen, GameTime gameTime)
{
mTargetScreen = targetScreen;
mTransitionDuration = 350;
mTransitionStartTime = gameTime.TotalGameTime.TotalMilliseconds;
TransitionRunning = true;
}

#region Button Hover Handlers

private void OnStoryHover(Object sender, EventArgs eventArgs)
{
mSelectorPosition = new Vector2(mMenuBoxPosition.X + 22, mButtonTopPadding + mButtonVerticalCenter);
}

private void OnFreePlayHover(Object sender, EventArgs eventArgs)
{
mSelectorPosition = new Vector2(mMenuBoxPosition.X + 22, mButtonTopPadding + mButtonVerticalCenter + 50);
}

private void OnBackHover(Object sender, EventArgs eventArgs)
{
mSelectorPosition = new Vector2(mMenuBoxPosition.X + 22, mButtonTopPadding + mButtonVerticalCenter + 100);
}
#endregion
}
}
Loading

0 comments on commit e55703c

Please sign in to comment.