Remove deprecated interfaces

Note that these are not fully replaced by modules yet.
This commit is contained in:
Drew DeVault 2015-09-24 22:38:32 -04:00
parent 3722468d01
commit 49fb3435da
6 changed files with 1 additions and 426 deletions

View File

@ -1,221 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using TrueCraft.Client.Rendering;
using TrueCraft.Client.Input;
namespace TrueCraft.Client.Interface
{
public class ChatInterface : Control
{
private static bool TryParseKey(Keys key, bool shift, out char value)
{
// Credit to Roy Triesscheijn for thinking of this.
// His implementation of this solution can be found at:
// http://roy-t.nl/index.php/2010/02/11/code-snippet-converting-keyboard-input-to-text-in-xna/
switch (key)
{
case Keys.NumPad1: value = '1'; break;
case Keys.NumPad2: value = '2'; break;
case Keys.NumPad3: value = '3'; break;
case Keys.NumPad4: value = '4'; break;
case Keys.NumPad5: value = '5'; break;
case Keys.NumPad6: value = '6'; break;
case Keys.NumPad7: value = '7'; break;
case Keys.NumPad8: value = '8'; break;
case Keys.NumPad9: value = '9'; break;
case Keys.NumPad0: value = '0'; break;
case Keys.D1: value = (shift) ? '!' : '1'; break;
case Keys.D2: value = (shift) ? '@' : '2'; break;
case Keys.D3: value = (shift) ? '#' : '3'; break;
case Keys.D4: value = (shift) ? '$' : '4'; break;
case Keys.D5: value = (shift) ? '%' : '5'; break;
case Keys.D6: value = (shift) ? '^' : '6'; break;
case Keys.D7: value = (shift) ? '&' : '7'; break;
case Keys.D8: value = (shift) ? '*' : '8'; break;
case Keys.D9: value = (shift) ? '(' : '9'; break;
case Keys.D0: value = (shift) ? ')' : '0'; break;
case Keys.A: value = (shift) ? 'A' : 'a'; break;
case Keys.B: value = (shift) ? 'B' : 'b'; break;
case Keys.C: value = (shift) ? 'C' : 'c'; break;
case Keys.D: value = (shift) ? 'D' : 'd'; break;
case Keys.E: value = (shift) ? 'E' : 'e'; break;
case Keys.F: value = (shift) ? 'F' : 'f'; break;
case Keys.G: value = (shift) ? 'G' : 'g'; break;
case Keys.H: value = (shift) ? 'H' : 'h'; break;
case Keys.I: value = (shift) ? 'I' : 'i'; break;
case Keys.J: value = (shift) ? 'J' : 'j'; break;
case Keys.K: value = (shift) ? 'K' : 'k'; break;
case Keys.L: value = (shift) ? 'L' : 'l'; break;
case Keys.M: value = (shift) ? 'M' : 'm'; break;
case Keys.N: value = (shift) ? 'N' : 'n'; break;
case Keys.O: value = (shift) ? 'O' : 'o'; break;
case Keys.P: value = (shift) ? 'P' : 'p'; break;
case Keys.Q: value = (shift) ? 'Q' : 'q'; break;
case Keys.R: value = (shift) ? 'R' : 'r'; break;
case Keys.S: value = (shift) ? 'S' : 's'; break;
case Keys.T: value = (shift) ? 'T' : 't'; break;
case Keys.U: value = (shift) ? 'U' : 'u'; break;
case Keys.V: value = (shift) ? 'V' : 'v'; break;
case Keys.W: value = (shift) ? 'W' : 'w'; break;
case Keys.X: value = (shift) ? 'X' : 'x'; break;
case Keys.Y: value = (shift) ? 'Y' : 'y'; break;
case Keys.Z: value = (shift) ? 'Z' : 'z'; break;
case Keys.OemTilde: value = (shift) ? '~' : '`'; break;
case Keys.OemSemicolon: value = (shift) ? ':' : ';'; break;
case Keys.OemQuotes: value = (shift) ? '"' : '\''; break;
case Keys.OemQuestion: value = (shift) ? '?' : '/'; break;
case Keys.OemPlus: value = (shift) ? '+' : '='; break;
case Keys.OemPipe: value = (shift) ? '|' : '\\'; break;
case Keys.OemPeriod: value = (shift) ? '>' : '.'; break;
case Keys.OemOpenBrackets: value = (shift) ? '{' : '['; break;
case Keys.OemCloseBrackets: value = (shift) ? '}' : ']'; break;
case Keys.OemMinus: value = (shift) ? '_' : '-'; break;
case Keys.OemComma: value = (shift) ? '<' : ','; break;
case Keys.Space: value = ' '; break;
default: value = default(char); return false;
}
return true;
}
private class ChatMessage
{
public string Message { get; set; }
public DateTime Time { get; set; }
public ChatMessage(string message)
{
Message = message;
Time = DateTime.UtcNow;
}
}
public bool HasFocus { get; set; }
public MultiplayerClient Client { get; set; }
public KeyboardHandler Keyboard { get; set; }
public FontRenderer Font { get; set; }
private readonly object Lock = new object();
private string Input { get; set; }
private List<ChatMessage> Messages { get; set; }
private Texture2D DummyTexture { get; set; }
public ChatInterface(MultiplayerClient client, KeyboardHandler keyboard, FontRenderer font)
{
Client = client;
Keyboard = keyboard;
Font = font;
Input = string.Empty;
Messages = new List<ChatMessage>();
DummyTexture = new Texture2D(keyboard.Game.GraphicsDevice, 1, 1);
DummyTexture.SetData(new[] { Color.White });
Client.ChatMessage += OnChatMessage;
Keyboard.KeyDown += OnKeyDown;
}
protected override void OnShow() { }
protected override void OnUpdate(GameTime gameTime)
{
lock (Lock)
{
for (int i = 0; i < Messages.Count; i++)
{
var message = Messages[i];
if ((DateTime.UtcNow - message.Time).TotalSeconds > 10)
{
Messages.RemoveAt(i);
i--;
}
}
}
}
protected override void OnDrawSprites(GameTime gameTime, SpriteBatch spriteBatch)
{
// UI scaling
var scale = GetScaleFactor();
var xOrigin = (int)(10 * scale);
var yOffset = (int)(25 * scale);
var yOrigin = (int)(5 * scale) + (spriteBatch.GraphicsDevice.Viewport.Height - (yOffset * 7));
var color = Color.Lerp(Color.Transparent, Color.Black, 0.5f);
lock (Lock)
{
if ((Messages.Count == 0) && !HasFocus) return;
spriteBatch.Draw(DummyTexture, new Rectangle(xOrigin - 2, yOrigin - 2, (int)(600 * scale) + 4, (yOffset * 5) + 4), color);
var total = 5;
for (int i = (Messages.Count - 1); i >= 0; i--)
{
var message = Messages[i];
total--;
Font.DrawText(spriteBatch, xOrigin , yOrigin + (yOffset * total), message.Message, scale);
if (total == 0) break;
}
if (HasFocus)
{
spriteBatch.Draw(DummyTexture, new Rectangle(xOrigin - 2, yOrigin + (yOffset * 5) + xOrigin - 2, (int)(600 * scale) + 4, yOffset + 4), color);
Font.DrawText(spriteBatch, xOrigin, yOrigin + (yOffset * 5) + xOrigin, "> " + Input, scale);
}
}
}
private void OnChatMessage(object sender, Events.ChatMessageEventArgs e)
{
AddMessage(e.Message);
}
private void OnKeyDown(object sender, KeyboardKeyEventArgs e)
{
if (HasFocus)
{
if (e.Key == Keys.Back)
Input = Input.Length > 0 ? Input.Substring(0, Input.Length - 1) : Input;
else
{
var shift = (Keyboard.State.IsKeyDown(Keys.LeftShift) || Keyboard.State.IsKeyDown(Keys.RightShift));
var value = default(char);
if (TryParseKey(e.Key, shift, out value))
Input += new string(new char[] { value });
}
}
else
{
if (Input != string.Empty)
{
Client.SendMessage(Input);
Input = string.Empty;
}
}
}
protected override void OnHide() { }
public void AddMessage(string message)
{
lock (Lock)
{
Messages.Add(new ChatMessage(message));
Console.WriteLine(message);
}
}
}
}

View File

@ -1,115 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace TrueCraft.Client.Interface
{
/// <summary>
/// Abstract base class for uniformly-implemented game interfaces.
/// </summary>
public abstract class Control
: IGameInterface
{
private bool _isVisible;
/// <summary>
/// Gets or sets whether the control is visible.
/// </summary>
public bool IsVisible
{
get { return _isVisible; }
set
{
if (_isVisible == value)
return;
_isVisible = value;
if (_isVisible) OnShow();
else OnHide();
}
}
/// <summary>
/// Gets or sets the scale for the control.
/// </summary>
public InterfaceScale Scale { get; set; }
/// <summary>
/// Creates a new control.
/// </summary>
protected Control() { Scale = InterfaceScale.Medium; }
/// <summary>
/// Shows the control.
/// </summary>
public void Show() { IsVisible = true; }
/// <summary>
/// Hides the control.
/// </summary>
public void Hide() { IsVisible = false; }
/// <summary>
/// Called when the control's visibility is set to true.
/// </summary>
protected abstract void OnShow();
/// <summary>
/// Called when the control is updated.
/// </summary>
/// <param name="gameTime"></param>
protected abstract void OnUpdate(GameTime gameTime);
/// <summary>
/// Called when the control is drawn.
/// </summary>
/// <param name="gameTime"></param>
/// <param name="spriteBatch"></param>
protected abstract void OnDrawSprites(GameTime gameTime, SpriteBatch spriteBatch);
/// <summary>
/// Called when the control's visibility is set to false.
/// </summary>
protected abstract void OnHide();
/// <summary>
/// Updates the control.
/// </summary>
/// <param name="gameTime"></param>
public void Update(GameTime gameTime)
{
OnUpdate(gameTime);
}
/// <summary>
/// Draws the control.
/// </summary>
/// <param name="gameTime"></param>
/// <param name="spriteBatch"></param>
public virtual void DrawSprites(GameTime gameTime, SpriteBatch spriteBatch)
{
if (IsVisible)
OnDrawSprites(gameTime, spriteBatch);
}
/// <summary>
/// Returns the preferred scale factor for the control.
/// </summary>
/// <returns></returns>
protected float GetScaleFactor()
{
switch (Scale)
{
case InterfaceScale.Small:
return 0.5f;
default:
case InterfaceScale.Medium:
return 1.0f;
case InterfaceScale.Large:
return 1.5f;
}
}
}
}

View File

@ -1,57 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TrueCraft.Client.Rendering;
using TrueCraft.Client.Input;
using TrueCraft.API;
namespace TrueCraft.Client.Interface
{
public class DebugInterface : Control
{
public MultiplayerClient Client { get; set; }
public FontRenderer Font { get; set; }
public int Vertices { private get; set; }
public int Chunks { private get; set; }
public DebugInterface(MultiplayerClient client, FontRenderer font)
{
Client = client;
Font = font;
}
protected override void OnShow() { }
protected override void OnUpdate(GameTime gameTime) { }
protected override void OnDrawSprites(GameTime gameTime, SpriteBatch spriteBatch)
{
// UI scaling
var scale = GetScaleFactor();
var xOrigin = (int)(10 * scale);
var yOrigin = (int)(5 * scale);
var yOffset = (int)(25 * scale);
var fps = (int)(1 / gameTime.ElapsedGameTime.TotalSeconds) + 1;
var position = Client.Position;
Font.DrawText(spriteBatch, xOrigin, yOrigin, string.Format("§lRunning at {0} FPS", GetFPSColor(fps) + fps.ToString()), scale);
Font.DrawText(spriteBatch, xOrigin, yOrigin + (yOffset * 1), string.Format("§o{0} vertices", Vertices), scale);
Font.DrawText(spriteBatch, xOrigin, yOrigin + (yOffset * 2), string.Format("§o{0} chunks", Chunks), scale);
Font.DrawText(spriteBatch, xOrigin, yOrigin + (yOffset * 3), string.Format("§o<{0:N2}, {1:N2}, {2:N2}>", Client.Position.X, Client.Position.Y, Client.Position.Z), scale);
}
protected override void OnHide() { }
private string GetFPSColor(int fps)
{
if (fps <= 16)
return "§c";
else if (fps <= 32)
return "§e";
else
return "§a";
}
}
}

View File

@ -1,14 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace TrueCraft.Client.Interface
{
public interface IGameInterface
{
InterfaceScale Scale { get; set; }
void Update(GameTime gameTime);
void DrawSprites(GameTime gameTime, SpriteBatch spriteBatch);
}
}

View File

@ -1,13 +0,0 @@
using System;
namespace TrueCraft.Client.Interface
{
public enum InterfaceScale
{
Small = 0,
Medium = 1,
Large = 2
}
}

View File

@ -83,9 +83,6 @@
<Compile Include="Input\MouseEventArgs.cs" />
<Compile Include="Input\MouseMoveEventArgs.cs" />
<Compile Include="Input\MouseScrollEventArgs.cs" />
<Compile Include="Interface\DebugInterface.cs" />
<Compile Include="Interface\Control.cs" />
<Compile Include="Interface\InterfaceScale.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\Blocks\CobwebRenderer.cs" />
@ -101,8 +98,6 @@
<Compile Include="MultiplayerClient.cs" />
<Compile Include="Handlers\PacketHandlers.cs" />
<Compile Include="Events\ChatMessageEventArgs.cs" />
<Compile Include="Interface\ChatInterface.cs" />
<Compile Include="Interface\IGameInterface.cs" />
<Compile Include="BMFont.cs" />
<Compile Include="Handlers\ChunkHandler.cs" />
<Compile Include="ReadOnlyWorld.cs" />
@ -235,4 +230,4 @@
<ItemGroup>
<Folder Include="Modules\" />
</ItemGroup>
</Project>
</Project>