Guest User

Untitled

a guest
Jul 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. using SRClient.Objects;
  10. using SRClient.GUI;
  11. using SRClient.Mathematics;
  12. using SRClient.Managers.Networking;
  13. using FarseerPhysics.Collision;
  14. using FarseerPhysics.Controllers;
  15. using FarseerPhysics.ConvertUnits;
  16. using FarseerPhysics.Dynamics;
  17. using FarseerPhysics.Factories;
  18. using FarseerPhysics.Common;
  19.  
  20. namespace SRClient.Managers
  21. {
  22.     class ChatManager
  23.     {
  24.  
  25.         public struct Chatline
  26.         {
  27.             public string text;
  28.             public int row;
  29.             public string prefix;
  30.             public Color prefixColor;
  31.  
  32.         }
  33.  
  34.  
  35.         private Vector2 pos_Console;
  36.         private Texture2D tex_DialogueBox;
  37.         private Texture2D tex_Console;
  38.         private Texture2D tex_OverExitDialogueBox;
  39.         private Texture2D tex_StringBox;
  40.         private Rectangle rect_DialogueBox;
  41.  
  42.         private String currentString = "";
  43.         private String oldString = "";
  44.         private String newString = "";
  45.         private String loopUnderScore = "";
  46.         //amount of rows
  47.         private int increment = 0;
  48.         private int Loopincrement = 60;
  49.         private SpriteFont textFont;
  50.         private bool hasText = false;
  51.         List<Chatline> chatList = new List<Chatline>();
  52.         private SpriteBatch spriteBatch1;
  53.         private RenderTarget2D radarTarget;
  54.         private int lastKeyPressed = 0;
  55.         private bool renderChat = true;
  56.  
  57.         //Viewing the chatlist
  58.         private Camera2D chatCamera;
  59.  
  60.         public ChatManager(ContentManager Content, SpriteBatch spriteBatch, GraphicsDevice graphics)
  61.         {
  62.             tex_DialogueBox = Content.Load<Texture2D>(@"GUI/DialogueBox");
  63.             tex_Console = Content.Load<Texture2D>(@"GUI/chatbox2");
  64.             tex_OverExitDialogueBox = Content.Load<Texture2D>(@"GUI/OverExitDialogueBox");
  65.             textFont = Content.Load<SpriteFont>(@"GUI/drawFont");
  66.             rect_DialogueBox = new Rectangle(0, 0, tex_DialogueBox.Width, tex_DialogueBox.Width);
  67.             radarTarget = new RenderTarget2D(spriteBatch.GraphicsDevice, tex_Console.Width, tex_Console.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);
  68.             chatCamera = new Camera2D();
  69.             chatCamera.Pos = new Vector2(tex_Console.Width / 2, tex_Console.Height / 2);
  70.             spriteBatch1 = new SpriteBatch(graphics);
  71.         }
  72.  
  73.         public virtual void Update(GameTime gameTime)
  74.         {
  75.             #region Key Input
  76.             //lastKeyPressed++; // Will be used to implement hold-down key repeat
  77.             if (KeyboardManager.IsTyping)
  78.             {
  79.                 string oldcmd = currentString;
  80.                 bool shift = (IsPressed(Keys.LeftShift) || IsPressed(Keys.RightShift));
  81.                 bool ctrl = (IsPressed(Keys.LeftControl) || IsPressed(Keys.RightControl));
  82.                 bool alt = (IsPressed(Keys.LeftAlt) || IsPressed(Keys.RightAlt));
  83.                 //Alphabet, Numbers, Space
  84.                 for (int i = 0; i < 26; i++)
  85.                 {
  86.                     if (IsTapped(Keys.A + i))
  87.                         if (shift)
  88.                         {
  89.                             currentString += (char)('A' + i);
  90.                             hasText = true;
  91.                         }
  92.                         else
  93.                         {
  94.                             currentString += (char)('a' + i);
  95.                             hasText = true;
  96.                         }
  97.  
  98.                     if (i <= 9 && (IsTapped(Keys.D0 + i) || IsTapped(Keys.NumPad0 + i)))
  99.                     {
  100.                         if (!shift)
  101.                         {
  102.                             currentString += (char)('0' + i);
  103.                             hasText = true;
  104.                         }
  105.                         else if (i == 1)
  106.                         {
  107.                             currentString += '!';
  108.                             hasText = true;
  109.                         }
  110.                         else if (i == 2)
  111.                         {
  112.                             currentString += '@';
  113.                             hasText = true;
  114.                         }
  115.                         else if (i == 3)
  116.                         {
  117.                             currentString += '#';
  118.                             hasText = true;
  119.                         }
  120.                         else if (i == 4)
  121.                         {
  122.                             currentString += '$';
  123.                             hasText = true;
  124.                         }
  125.                         else if (i == 5)
  126.                         {
  127.                             currentString += '%';
  128.                             hasText = true;
  129.                         }
  130.                         else if (i == 6)
  131.                         {
  132.                             currentString += '^';
  133.                             hasText = true;
  134.                         }
  135.                         else if (i == 7)
  136.                         {
  137.                             currentString += '&';
  138.                             hasText = true;
  139.                         }
  140.                         else if (i == 8)
  141.                         {
  142.                             currentString += '*';
  143.                             hasText = true;
  144.                         }
  145.                         else if (i == 9)
  146.                         {
  147.                             currentString += '(';
  148.                             hasText = true;
  149.                         }
  150.                         else if (i == 0)
  151.                         {
  152.                             currentString += ')';
  153.                             hasText = true;
  154.                         }
  155.                     }
  156.                 }
  157.                 if (IsTapped(Keys.OemBackslash))
  158.                 {
  159.                     currentString += '\\';
  160.                     hasText = true;
  161.                 }
  162.                 if (IsTapped(Keys.Subtract) || IsTapped(Keys.OemMinus))
  163.                 {
  164.                     currentString += shift ? '_' : '-';
  165.                     hasText = true;
  166.                 }
  167.                 if (IsTapped(Keys.OemQuotes))
  168.                 {
  169.                     currentString += shift ? '"' : '\'';
  170.                     hasText = true;
  171.                 }
  172.                 if (IsTapped(Keys.OemTilde))
  173.                 {
  174.                     currentString += shift ? '~' : '`';
  175.                     hasText = true;
  176.                 }
  177.                 if (IsTapped(Keys.OemSemicolon))
  178.                 {
  179.                     currentString += shift ? ':' : ';';
  180.                     hasText = true;
  181.                 }
  182.                 if (IsTapped(Keys.OemOpenBrackets))
  183.                 {
  184.                     currentString += shift ? '{' : '[';
  185.                     hasText = true;
  186.                 }
  187.                 if (IsTapped(Keys.OemCloseBrackets))
  188.                 {
  189.                     currentString += shift ? '}' : ']';
  190.                     hasText = true;
  191.                 }
  192.                 if (IsTapped(Keys.OemQuestion))
  193.                 {
  194.                     currentString += shift ? '?' : '/';
  195.                     hasText = true;
  196.                 }
  197.                 if (IsTapped(Keys.OemPipe))
  198.                 {
  199.                     currentString += shift ? '|' : '\\';
  200.                     hasText = true;
  201.                 }
  202.                 if (IsTapped(Keys.OemPlus))
  203.                 {
  204.                     currentString += shift ? '+' : '=';
  205.                     hasText = true;
  206.                 }
  207.                 if (IsTapped(Keys.Divide))
  208.                 {
  209.                     currentString += '/';
  210.                     hasText = true;
  211.                 }
  212.                 if (IsTapped(Keys.Space))
  213.                 {
  214.                     currentString += ' ';
  215.                     hasText = true;
  216.                 }
  217.                 if (IsTapped(Keys.OemPeriod))
  218.                 {
  219.                     if (shift) currentString += '>';
  220.                     else currentString += '.';
  221.                     hasText = true;
  222.                 }
  223.                 if (IsTapped(Keys.OemComma))
  224.                 {
  225.                     if (shift) currentString += '<';
  226.                     else currentString += ',';
  227.                     hasText = true;
  228.                 }
  229.  
  230.                 //Control Keys
  231.                 if (IsPressed(Keys.Back))
  232.                 {
  233.                     if(IsTapped(Keys.Back))
  234.                         currentString = (currentString.Length != 0
  235.                            ? currentString.Substring(0, currentString.Length - 1)
  236.                            : "");
  237.                 }
  238.             }
  239. #endregion
  240.  
  241.             #region ResetFocus / Send Input
  242.             if (KeyboardManager.currentState.IsKeyDown(Keys.Enter) && KeyboardManager.oldState.IsKeyUp(Keys.Enter))
  243.             {
  244.                 if (KeyboardManager.IsTyping)
  245.                 {
  246.                     if (!(string.IsNullOrWhiteSpace(currentString))) // Checks so that people can't spam space.
  247.                     {
  248.                         oldString = currentString;
  249.                         currentString = "";
  250.                         if (hasText)
  251.                         {
  252.                             Chatline c = new Chatline();
  253.                             c.row = increment;
  254.                             chatCamera.Pos = new Vector2(tex_Console.Width / 2, increment * textFont.LineSpacing - 50);
  255.                             c.text = oldString;
  256.                             c = CheckCommands(c);
  257.                             newString = WrapText(textFont, c.text, (tex_Console.Width - 40));
  258.                             c.text = newString;
  259.                             if (c.prefix == null)
  260.                             {
  261.                                 c.prefix = SRClient.Managers.Networking.ClientManager.username + " radios: ";
  262.                                 c.prefixColor = Color.MediumPurple;
  263.                             }
  264.                             chatList.Add(c);
  265.                             increment++;
  266.                             renderChat = true;
  267.                             hasText = false;
  268.                         }
  269.                     }
  270.                     KeyboardManager.IsTyping = false;
  271.                 }
  272.                 else
  273.                     KeyboardManager.IsTyping = true;
  274.             }
  275.             #endregion
  276.  
  277.             #region LoopingUnderScore
  278.             //constantly lower the increment every iteration
  279.             Loopincrement--;
  280.  
  281.             //once it gets low
  282.             if (Loopincrement < 0)
  283.             {
  284.                 loopUnderScore = LoopUnderScore(loopUnderScore);   //change it from nothing to an underscore
  285.                 Loopincrement = 40;                               //and reset the increment to 100 so we can do it again
  286.             }
  287.  
  288.  
  289.             #endregion
  290.         }
  291.  
  292.  
  293.         public Chatline CheckCommands(Chatline c)
  294.         {
  295.             string text1 = c.text;
  296.             if (c.text.StartsWith(@"/"))
  297.             {
  298.                 c.text = c.text.Remove(0, 1);
  299.                 if (c.text.ToString().StartsWith("s ") || c.text.ToString().StartsWith("shout "))
  300.                 {
  301.                     c.text = c.text.Remove(0, 2);
  302.                     //SRClient.Managers.Networking.MessageManager.sendClientTextInput(c.text, SRClient.Managers.Networking.ClientManager.client, 0);
  303.                     c.prefix = SRClient.Managers.Networking.ClientManager.username + " shouts: ";
  304.                     c.prefixColor = Color.Green;
  305.                 }
  306.                 else if (c.text.ToString().StartsWith("t ") || c.text.ToString().StartsWith("tell "))
  307.                 {
  308.                     c.text.Remove(0, 2);
  309.                     //SRClient.Managers.Networking.MessageManager.sendClientTextInput(c.text, SRClient.Managers.Networking.ClientManager.client, 1);
  310.                     //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:
  311.                     //if(text == names in nameList)
  312.                     //then send to server so it can be sent to the player. If the player is offline the server will return a message to
  313.                     //ChatManager that will say "Player Offline."
  314.                 }
  315.                 else if (c.text == ("h") || c.text == "help")
  316.                 {
  317.                     c.prefixColor = Color.Yellow;
  318.                     c.prefix = "HELP: ";
  319.                     c.text = "Help Initiated.";
  320.                     DisplayToChatbox(c);
  321.                 }
  322.                 else if (c.text == ("login"))
  323.                 {
  324.                     var credentials = c.text.Split(' ');
  325.  
  326.                     List<string> loginInfo = new List<string>();
  327.  
  328.                     bool loggedIn = false, waitLoop = true;
  329.                     float areaID = 0;
  330.  
  331.                     Console.WriteLine("Username: " + credentials[0] + " Password: " + credentials[1]);
  332.  
  333.                     MessageManager.sendLogin((byte)ClientManager.MessageTypes.ClientLoginRequest, credentials[0], credentials[1], ClientManager.client); //Sends login data to server
  334.                 }
  335.             }
  336.             return c;
  337.         }
  338.  
  339.  
  340.         public void DisplayToChatbox(Chatline c)
  341.         {
  342.             c.row = increment;
  343.             c.text = WrapText(textFont, c.text, (tex_Console.Width - 40));
  344.             chatList.Add(c);
  345.             renderChat = true;
  346.         }
  347.  
  348.  
  349.         /// <summary>
  350.         /// Draws Radar and Chat GUI elements to RenderTarget2D
  351.         /// </summary>
  352.         /// <param name="spriteBatch"></param>
  353.         /// <param name="planetList"></param>
  354.         /// <param name="shipPos"></param>
  355.         public virtual void DrawChatbox(SpriteBatch spriteBatch, GraphicsDevice graphics)
  356.         {
  357.             pos_Console.Y = spriteBatch1.GraphicsDevice.Viewport.Height - tex_Console.Height;
  358.  
  359.             // Only draws chat & strings when there has been a change.
  360.             if (renderChat)
  361.             {
  362.                 graphics.SetRenderTarget(radarTarget);
  363.                 graphics.Clear(ClearOptions.Target, new Color(0, 0, 0, 0), 0, 0);
  364.  
  365.                 ////////////////////////////////////
  366.                 //DRAW CHAT
  367.                 spriteBatch1.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend,
  368.                     null,
  369.                     null,
  370.                     null,
  371.                     null,
  372.                     chatCamera.get_transformation(spriteBatch1.GraphicsDevice));
  373.  
  374.                 for (int i = 0; i < chatList.Count(); i++)
  375.                 {
  376.                     Chatline c = chatList[i];
  377.                     spriteBatch1.DrawString(textFont, c.prefix, new Vector2(10, (textFont.LineSpacing * c.row)),
  378.                         c.prefixColor, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
  379.                     spriteBatch1.DrawString(textFont, c.text, new Vector2((10 + textFont.MeasureString(c.prefix).X), (textFont.LineSpacing * c.row)),
  380.                         Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
  381.                 }
  382.  
  383.                 spriteBatch1.End();
  384.                 tex_StringBox = radarTarget;
  385.                 graphics.SetRenderTarget(null);
  386.                 renderChat = false;
  387.             }
  388.         }
  389.  
  390.         public void DrawChatboxToScreen(SpriteBatch spriteBatch)
  391.         {
  392.             // DRAW STATIC STUFF
  393.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
  394.             spriteBatch.Draw(tex_Console, new Vector2(0, spriteBatch.GraphicsDevice.Viewport.Height - tex_Console.Height), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1f);
  395.  
  396.             if (KeyboardManager.IsTyping)
  397.                 spriteBatch.DrawString(textFont, currentString + loopUnderScore, new Vector2(10, spriteBatch.GraphicsDevice.Viewport.Height - textFont.LineSpacing - 5),
  398.                     Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
  399.             else
  400.                 spriteBatch.DrawString(textFont, currentString, new Vector2(10, spriteBatch.GraphicsDevice.Viewport.Height - textFont.LineSpacing - 5),
  401.                     Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, .9f);
  402.             spriteBatch.End();
  403.  
  404.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
  405.             // Draw Text & RenderTarget
  406.             spriteBatch.Draw(tex_StringBox, new Vector2(0, spriteBatch.GraphicsDevice.Viewport.Height - radarTarget.Height), Color.White);
  407.             spriteBatch.End();
  408.         }
  409.  
  410.  
  411.         public string LoopUnderScore(String s)
  412.         {
  413.             if (s == "")
  414.                 s = "_";
  415.             else
  416.                 s = "";
  417.             return s;
  418.         }
  419.         /// <summary>
  420.         ///  This function splits up the String and adds lines if the String is longer (in pixels) than the maxLineWidth.
  421.         /// </summary>
  422.         /// <param name="spriteFont">the font, so we can get the font.measurment of each char</param>
  423.         /// <param name="text">string you send in</param>
  424.         /// <param name="maxLineWidth">max line width</param>
  425.         /// <returns></returns>
  426.         public string WrapText(SpriteFont spriteFont, string text, float maxLineWidth)
  427.         {
  428.             string[] words = text.Split(' ');
  429.  
  430.             StringBuilder sb = new StringBuilder();
  431.  
  432.             float lineWidth = 0f;
  433.  
  434.             float spaceWidth = spriteFont.MeasureString(" ").X;
  435.  
  436.             foreach (string word in words)
  437.             {
  438.                 Vector2 size = spriteFont.MeasureString(word);
  439.  
  440.                 if (lineWidth + size.X < maxLineWidth)
  441.                 {
  442.                     sb.Append(word + " ");
  443.                     lineWidth += size.X + spaceWidth;
  444.                 }
  445.                 else
  446.                 {
  447.                     increment++;
  448.                     sb.Append("\n" + word + " ");
  449.                     lineWidth = size.X + spaceWidth;
  450.                 }
  451.             }
  452.  
  453.             return sb.ToString();
  454.         }
  455.  
  456.         private bool IsPressed(Keys key)
  457.         {
  458.             return KeyboardManager.currentState.IsKeyDown(key);
  459.         }
  460.         private bool IsTapped(Keys key)
  461.         {
  462.             return KeyboardManager.currentState.IsKeyDown(key) && KeyboardManager.oldState.IsKeyUp(key);
  463.         }
  464.     }
  465. }
Add Comment
Please, Sign In to add comment