Skip to content

Commit

Permalink
Implemented alert service
Browse files Browse the repository at this point in the history
  • Loading branch information
joncloud committed Feb 1, 2020
1 parent 98807f3 commit 1f89b5b
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Tetrominoes.OpenGL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Xna.Framework.Input;
using System;
using Tetrominoes.Audio;
using Tetrominoes.Graphics;
using Tetrominoes.Input;
using Tetrominoes.Options;

Expand Down Expand Up @@ -58,6 +59,7 @@ protected override void Initialize()
OptionEditorComponent.AddTo(this);
MenuComponent.AddTo(this);
MatchComponent.AddTo(this);
AlertComponent.AddTo(this);
base.Initialize();
}

Expand Down
138 changes: 138 additions & 0 deletions src/Tetrominoes/Graphics/AlertComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#nullable disable
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;

namespace Tetrominoes.Graphics
{
public class AlertComponent : DrawableGameComponent, IAlertService
{
public AlertComponent(Game game) : base(game)
{
Enabled = Visible = false;
}

public static AlertComponent AddTo(Game game)
{
var component = new AlertComponent(game);
game.Components.Add(component);
game.Services.AddService<IAlertService>(component);
return component;
}

UIFonts _uiFonts;
SpriteBatch _spriteBatch;
protected override void LoadContent()
{
_uiFonts = new UIFonts(FontSize.Small, Game.Content);
_spriteBatch = new SpriteBatch(GraphicsDevice);

base.LoadContent();
}

readonly Queue<Message> _messages = new Queue<Message>();
Message _current;

public override void Update(GameTime gameTime)
{
if (_current == default)
{
if (!_messages.TryDequeue(out _current))
{
Enabled = Visible = false;
}
}
else
{
if (_current.TryUpdate(gameTime))
{
_current = default;
}
}
base.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{
if (_current == default) return;

var pp = GraphicsDevice.PresentationParameters;
var tx = Matrix.CreateTranslation(
pp.BackBufferWidth,
pp.BackBufferHeight,
0
);
_spriteBatch.Begin(transformMatrix: tx, samplerState: SamplerState.PointClamp);
_current.Draw(_spriteBatch);
_spriteBatch.End();

base.Draw(gameTime);
}

public void Display(string text)
{
_messages.Enqueue(
new Message(
text,
_uiFonts.BoldWeight
)
);
Enabled = Visible = true;
}

class Message
{
readonly string _source;
readonly StringBuilder _stringBuilder;
readonly SpriteFont _font;
int _position;
public Message(string text, SpriteFont font)
{
_source = text ?? throw new ArgumentNullException(nameof(text));
_font = font ?? throw new ArgumentOutOfRangeException(nameof(font));
_stringBuilder = new StringBuilder(text.Length);
}

TimeSpan _timer;
int _target = 50;
public bool TryUpdate(GameTime gameTime)
{
_timer += gameTime.ElapsedGameTime;
while (_timer.TotalMilliseconds > _target)
{
_timer -= TimeSpan.FromMilliseconds(_target);

if (_position < _source.Length)
{
_stringBuilder.Append(_source[_position]);
_position++;
}
else if (_position == _source.Length)
{
_target = 2000;
_position++;
}
else if (_position > _source.Length)
{
return true;
}
}

return false;
}

public void Draw(SpriteBatch batch)
{
var measurement = _font.MeasureString(_stringBuilder);
batch.DrawString(
_font,
_stringBuilder,
-measurement,
Color.Black
);
}
}
}
}
7 changes: 7 additions & 0 deletions src/Tetrominoes/Graphics/IAlertService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Tetrominoes.Graphics
{
public interface IAlertService
{
void Display(string text);
}
}
3 changes: 3 additions & 0 deletions src/Tetrominoes/Input/EnabledWrapperInputMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public InputConnection Update(GameTime gameTime) =>
_enabled(_options)
? _inner.Update(gameTime)
: InputConnection.Disconnected;

public override string ToString() =>
_inner.ToString() ?? "";
}
}
2 changes: 1 addition & 1 deletion src/Tetrominoes/Input/GamePadInputMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public InputConnection GetConnectionState()
}

public override string ToString() =>
$"CONTROLLER {GamePadOptions.PlayerIndex.ToString().ToUpper()}";
$"Controller {GamePadOptions.PlayerIndex}";
}
}
9 changes: 5 additions & 4 deletions src/Tetrominoes/Input/InputComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Tetrominoes.Graphics;
using Tetrominoes.Options;

namespace Tetrominoes.Input
Expand All @@ -23,12 +24,12 @@ public static InputComponent AddTo(Game game)
}

IOptionService _options;
//IAlertService _alertService;
IAlertService _alert;
readonly List<IInputMapper> _enabled = new List<IInputMapper>();
readonly List<IInputMapper> _disabled = new List<IInputMapper>();
public override void Initialize()
{
//_alertService = Game.Services.GetService<IAlertService>();
_alert = Game.Services.GetService<IAlertService>();
_options = Game.Services.GetService<IOptionService>();
_enabled.Add(
new KeyboardInputMapper(_options)
Expand Down Expand Up @@ -56,7 +57,7 @@ public override void Update(GameTime gameTime)
_enabled.RemoveAt(index);
_disabled.Add(mapper);

//_alertService.Display($"{mapper} DISCONNECTED", default);
_alert.Display($"{mapper} DISCONNECTED");
}
else
{
Expand All @@ -74,7 +75,7 @@ public override void Update(GameTime gameTime)
_disabled.RemoveAt(index);
_enabled.Add(mapper);

//_alertService.Display($"{mapper} CONNECTED", default);
_alert.Display($"{mapper} CONNECTED");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tetrominoes/Input/KeyboardInputMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public InputConnection GetConnectionState() =>
InputConnection.Connected;

public override string ToString() =>
"KEYBOARD";
"Keyboard";
}
}
3 changes: 3 additions & 0 deletions src/Tetrominoes/Options/OptionEditorComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public void Show()
IMenuService _menu;
IAudioService _audio;
IOptionService _options;
IAlertService _alert;
GraphicsDeviceManager _manager;
public override void Initialize()
{
_input = Game.Services.GetService<IInputService>();
_menu = Game.Services.GetService<IMenuService>();
_audio = Game.Services.GetService<IAudioService>();
_options = Game.Services.GetService<IOptionService>();
_alert = Game.Services.GetService<IAlertService>();
_manager = Game.Services.GetService<GraphicsDeviceManager>();

base.Initialize();
Expand Down Expand Up @@ -576,6 +578,7 @@ void HandleOptionValue(InputState state)

Hide();
_menu.Show();
_alert.Display("Options saved");
}
break;

Expand Down

0 comments on commit 1f89b5b

Please sign in to comment.