Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using SRClient.Objects;
- using SRClient.GUI;
- using SRClient.Mathematics;
- using SRClient.Managers.Networking;
- using FarseerPhysics.Collision;
- using FarseerPhysics.Controllers;
- using FarseerPhysics.ConvertUnits;
- using FarseerPhysics.Dynamics;
- using FarseerPhysics.Factories;
- using FarseerPhysics.Common;
- namespace SRClient.Managers
- {
- class ChatManager
- {
- public struct Chatline
- {
- public string text;
- public int row;
- public string prefix;
- public Color prefixColor;
- }
- private Vector2 pos_Console;
- private Texture2D tex_DialogueBox;
- private Texture2D tex_Console;
- private Texture2D tex_OverExitDialogueBox;
- private Texture2D tex_StringBox;
- private Rectangle rect_DialogueBox;
- private String currentString = "";
- private String oldString = "";
- private String newString = "";
- private String loopUnderScore = "";
- //amount of rows
- private int increment = 0;
- private int Loopincrement = 60;
- private SpriteFont textFont;
- private bool hasText = false;
- List<Chatline> chatList = new List<Chatline>();
- private SpriteBatch spriteBatch1;
- private RenderTarget2D radarTarget;
- private int lastKeyPressed = 0;
- private bool renderChat = true;
- //Viewing the chatlist
- private Camera2D chatCamera;
- public ChatManager(ContentManager Content, SpriteBatch spriteBatch, GraphicsDevice graphics)
- {
- tex_DialogueBox = Content.Load<Texture2D>(@"GUI/DialogueBox");
- tex_Console = Content.Load<Texture2D>(@"GUI/chatbox2");
- tex_OverExitDialogueBox = Content.Load<Texture2D>(@"GUI/OverExitDialogueBox");
- textFont = Content.Load<SpriteFont>(@"GUI/drawFont");
- rect_DialogueBox = new Rectangle(0, 0, tex_DialogueBox.Width, tex_DialogueBox.Width);
- radarTarget = new RenderTarget2D(spriteBatch.GraphicsDevice, tex_Console.Width, tex_Console.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);
- chatCamera = new Camera2D();
- chatCamera.Pos = new Vector2(tex_Console.Width / 2, tex_Console.Height / 2);
- spriteBatch1 = new SpriteBatch(graphics);
- }
- public virtual void Update(GameTime gameTime)
- {
- #region Key Input
- //lastKeyPressed++; // Will be used to implement hold-down key repeat
- if (KeyboardManager.IsTyping)
- {
- string oldcmd = currentString;
- bool shift = (IsPressed(Keys.LeftShift) || IsPressed(Keys.RightShift));
- bool ctrl = (IsPressed(Keys.LeftControl) || IsPressed(Keys.RightControl));
- bool alt = (IsPressed(Keys.LeftAlt) || IsPressed(Keys.RightAlt));
- //Alphabet, Numbers, Space
- for (int i = 0; i < 26; i++)
- {
- if (IsTapped(Keys.A + i))
- if (shift)
- {
- currentString += (char)('A' + i);
- hasText = true;
- }
- else
- {
- currentString += (char)('a' + i);
- hasText = true;
- }
- if (i <= 9 && (IsTapped(Keys.D0 + i) || IsTapped(Keys.NumPad0 + i)))
- {
- if (!shift)
- {
- currentString += (char)('0' + i);
- hasText = true;
- }
- else if (i == 1)
- {
- currentString += '!';
- hasText = true;
- }
- else if (i == 2)
- {
- currentString += '@';
- hasText = true;
- }
- else if (i == 3)
- {
- currentString += '#';
- hasText = true;
- }
- else if (i == 4)
- {
- currentString += '$';
- hasText = true;
- }
- else if (i == 5)
- {
- currentString += '%';
- hasText = true;
- }
- else if (i == 6)
- {
- currentString += '^';
- hasText = true;
- }
- else if (i == 7)
- {
- currentString += '&';
- hasText = true;
- }
- else if (i == 8)
- {
- currentString += '*';
- hasText = true;
- }
- else if (i == 9)
- {
- currentString += '(';
- hasText = true;
- }
- else if (i == 0)
- {
- currentString += ')';
- hasText = true;
- }
- }
- }
- if (IsTapped(Keys.OemBackslash))
- {
- currentString += '\\';
- hasText = true;
- }
- if (IsTapped(Keys.Subtract) || IsTapped(Keys.OemMinus))
- {
- currentString += shift ? '_' : '-';
- hasText = true;
- }
- if (IsTapped(Keys.OemQuotes))
- {
- currentString += shift ? '"' : '\'';
- hasText = true;
- }
- if (IsTapped(Keys.OemTilde))
- {
- currentString += shift ? '~' : '`';
- hasText = true;
- }
- if (IsTapped(Keys.OemSemicolon))
- {
- currentString += shift ? ':' : ';';
- hasText = true;
- }
- if (IsTapped(Keys.OemOpenBrackets))
- {
- currentString += shift ? '{' : '[';
- hasText = true;
- }
- if (IsTapped(Keys.OemCloseBrackets))
- {
- currentString += shift ? '}' : ']';
- hasText = true;
- }
- if (IsTapped(Keys.OemQuestion))
- {
- currentString += shift ? '?' : '/';
- hasText = true;
- }
- if (IsTapped(Keys.OemPipe))
- {
- currentString += shift ? '|' : '\\';
- hasText = true;
- }
- if (IsTapped(Keys.OemPlus))
- {
- currentString += shift ? '+' : '=';
- hasText = true;
- }
- if (IsTapped(Keys.Divide))
- {
- currentString += '/';
- hasText = true;
- }
- if (IsTapped(Keys.Space))
- {
- currentString += ' ';
- hasText = true;
- }
- if (IsTapped(Keys.OemPeriod))
- {
- if (shift) currentString += '>';
- else currentString += '.';
- hasText = true;
- }
- if (IsTapped(Keys.OemComma))
- {
- if (shift) currentString += '<';
- else currentString += ',';
- hasText = true;
- }
- //Control Keys
- if (IsPressed(Keys.Back))
- {
- if(IsTapped(Keys.Back))
- currentString = (currentString.Length != 0
- ? currentString.Substring(0, currentString.Length - 1)
- : "");
- }
- }
- #endregion
- #region ResetFocus / Send Input
- if (KeyboardManager.currentState.IsKeyDown(Keys.Enter) && KeyboardManager.oldState.IsKeyUp(Keys.Enter))
- {
- if (KeyboardManager.IsTyping)
- {
- if (!(string.IsNullOrWhiteSpace(currentString))) // Checks so that people can't spam space.
- {
- oldString = currentString;
- currentString = "";
- if (hasText)
- {
- Chatline c = new Chatline();
- c.row = increment;
- chatCamera.Pos = new Vector2(tex_Console.Width / 2, increment * textFont.LineSpacing - 50);
- c.text = oldString;
- c = CheckCommands(c);
- newString = WrapText(textFont, c.text, (tex_Console.Width - 40));
- c.text = newString;
- if (c.prefix == null)
- {
- c.prefix = SRClient.Managers.Networking.ClientManager.username + " radios: ";
- c.prefixColor = Color.MediumPurple;
- }
- chatList.Add(c);
- increment++;
- renderChat = true;
- hasText = false;
- }
- }
- KeyboardManager.IsTyping = false;
- }
- else
- KeyboardManager.IsTyping = true;
- }
- #endregion
- #region LoopingUnderScore
- //constantly lower the increment every iteration
- Loopincrement--;
- //once it gets low
- if (Loopincrement < 0)
- {
- loopUnderScore = LoopUnderScore(loopUnderScore); //change it from nothing to an underscore
- Loopincrement = 40; //and reset the increment to 100 so we can do it again
- }
- #endregion
- }
- public Chatline CheckCommands(Chatline c)
- {
- string text1 = c.text;
- if (c.text.StartsWith(@"/"))
- {
- c.text = c.text.Remove(0, 1);
- if (c.text.ToString().StartsWith("s ") || c.text.ToString().StartsWith("shout "))
- {
- c.text = c.text.Remove(0, 2);
- //SRClient.Managers.Networking.MessageManager.sendClientTextInput(c.text, SRClient.Managers.Networking.ClientManager.client, 0);
- c.prefix = SRClient.Managers.Networking.ClientManager.username + " shouts: ";
- c.prefixColor = Color.Green;
- }
- else if (c.text.ToString().StartsWith("t ") || c.text.ToString().StartsWith("tell "))
- {
- c.text.Remove(0, 2);
- //SRClient.Managers.Networking.MessageManager.sendClientTextInput(c.text, SRClient.Managers.Networking.ClientManager.client, 1);
- //Go into a file and pull the list of players(inside of the galaxy). Then check the rest of the string with the player names:
- //if(text == names in nameList)
- //then send to server so it can be sent to the player. If the player is offline the server will return a message to
- //ChatManager that will say "Player Offline."
- }
- else if (c.text == ("h") || c.text == "help")
- {
- c.prefixColor = Color.Yellow;
- c.prefix = "HELP: ";
- c.text = "Help Initiated.";
- DisplayToChatbox(c);
- }
- else if (c.text == ("login"))
- {
- var credentials = c.text.Split(' ');
- List<string> loginInfo = new List<string>();
- bool loggedIn = false, waitLoop = true;
- float areaID = 0;
- Console.WriteLine("Username: " + credentials[0] + " Password: " + credentials[1]);
- MessageManager.sendLogin((byte)ClientManager.MessageTypes.ClientLoginRequest, credentials[0], credentials[1], ClientManager.client); //Sends login data to server
- }
- }
- return c;
- }
- public void DisplayToChatbox(Chatline c)
- {
- c.row = increment;
- c.text = WrapText(textFont, c.text, (tex_Console.Width - 40));
- chatList.Add(c);
- renderChat = true;
- }
- /// <summary>
- /// Draws Radar and Chat GUI elements to RenderTarget2D
- /// </summary>
- /// <param name="spriteBatch"></param>
- /// <param name="planetList"></param>
- /// <param name="shipPos"></param>
- public virtual void DrawChatbox(SpriteBatch spriteBatch, GraphicsDevice graphics)
- {
- pos_Console.Y = spriteBatch1.GraphicsDevice.Viewport.Height - tex_Console.Height;
- // Only draws chat & strings when there has been a change.
- if (renderChat)
- {
- graphics.SetRenderTarget(radarTarget);
- graphics.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 0, 0);
- ////////////////////////////////////
- //DRAW CHAT
- spriteBatch1.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend,
- null,
- null,
- null,
- null,
- chatCamera.get_transformation(spriteBatch1.GraphicsDevice));
- for (int i = 0; i < chatList.Count(); i++)
- {
- Chatline c = chatList[i];
- spriteBatch1.DrawString(textFont, c.prefix, new Vector2(10, (textFont.LineSpacing * c.row)),
- c.prefixColor, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
- spriteBatch1.DrawString(textFont, c.text, new Vector2((10 + textFont.MeasureString(c.prefix).X), (textFont.LineSpacing * c.row)),
- Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
- }
- spriteBatch1.End();
- tex_StringBox = radarTarget;
- graphics.SetRenderTarget(null);
- renderChat = false;
- }
- }
- public void DrawChatboxToScreen(SpriteBatch spriteBatch)
- {
- // DRAW STATIC STUFF
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
- spriteBatch.Draw(tex_Console, new Vector2(0, spriteBatch.GraphicsDevice.Viewport.Height - tex_Console.Height), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1f);
- if (KeyboardManager.IsTyping)
- spriteBatch.DrawString(textFont, currentString + loopUnderScore, new Vector2(10, spriteBatch.GraphicsDevice.Viewport.Height - textFont.LineSpacing - 5),
- Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
- else
- spriteBatch.DrawString(textFont, currentString, new Vector2(10, spriteBatch.GraphicsDevice.Viewport.Height - textFont.LineSpacing - 5),
- Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
- spriteBatch.End();
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
- // Draw Text & RenderTarget
- spriteBatch.Draw(tex_StringBox, new Vector2(0, spriteBatch.GraphicsDevice.Viewport.Height - radarTarget.Height), Color.White);
- spriteBatch.End();
- }
- public string LoopUnderScore(String s)
- {
- if (s == "")
- s = "_";
- else
- s = "";
- return s;
- }
- /// <summary>
- /// This function splits up the String and adds lines if the String is longer (in pixels) than the maxLineWidth.
- /// </summary>
- /// <param name="spriteFont">the font, so we can get the font.measurment of each char</param>
- /// <param name="text">string you send in</param>
- /// <param name="maxLineWidth">max line width</param>
- /// <returns></returns>
- public string WrapText(SpriteFont spriteFont, string text, float maxLineWidth)
- {
- string[] words = text.Split(' ');
- StringBuilder sb = new StringBuilder();
- float lineWidth = 0f;
- float spaceWidth = spriteFont.MeasureString(" ").X;
- foreach (string word in words)
- {
- Vector2 size = spriteFont.MeasureString(word);
- if (lineWidth + size.X < maxLineWidth)
- {
- sb.Append(word + " ");
- lineWidth += size.X + spaceWidth;
- }
- else
- {
- increment++;
- sb.Append("\n" + word + " ");
- lineWidth = size.X + spaceWidth;
- }
- }
- return sb.ToString();
- }
- private bool IsPressed(Keys key)
- {
- return KeyboardManager.currentState.IsKeyDown(key);
- }
- private bool IsTapped(Keys key)
- {
- return KeyboardManager.currentState.IsKeyDown(key) && KeyboardManager.oldState.IsKeyUp(key);
- }
- }
- }
Add Comment
Please, Sign In to add comment