private void sendMessage_Click(object sender, RoutedEventArgs e) { MessageObject tmpMsg = new MessageObject() { Who = userNameHidden, Type = "text", Data = chattTextBox.Text }; CurrentChatMessages.Add(tmpMsg); db.addToDB(tmpMsg); chattBox.SelectedIndex = CurrentChatMessages.Count - 1; chattBox.ScrollIntoView(chattBox.SelectedItem); try { try { CurrentClient.send_message("{'type': 'message', 'message': '" + chattTextBox.Text + "'}"); } catch (NullReferenceException n) { Console.WriteLine("NullReferenceException: {0}", e); throw new ConnectionException(); } } catch (ConnectionException ce) { Console.WriteLine("connectionException: {0}", ce); System.Windows.MessageBox.Show("Can't send message if not connected", "Connection Error"); } chattTextBox.Text = ""; chattTextBox.Focus(); }
private void sendImage_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|GIF Files (*.gif)|*.gif"; dlg.DefaultExt = ".png"; Nullable <bool> result = dlg.ShowDialog(); if (result.HasValue && result.Value) { string filename = dlg.SafeFileName; byte[] imageArray = System.IO.File.ReadAllBytes(dlg.FileName); System.IO.File.Copy(dlg.FileName, System.AppDomain.CurrentDomain.BaseDirectory + "/assets/img/" + dlg.SafeFileName, true); MessageObject tmpMsg = new MessageObject() { Who = userNameHidden, Type = "image", Data = dlg.SafeFileName }; CurrentChatMessages.Add(tmpMsg); db.addToDB(tmpMsg); CurrentClient.send_image(imageArray, filename); } chattBox.SelectedIndex = CurrentChatMessages.Count - 1; chattBox.ScrollIntoView(chattBox.SelectedItem); }
protected override void Update(GameTime gameTime) { KeyboardState state = Keyboard.GetState(); if (World.GameState == GameState.Uninitialized) { return; } else if (World.GameState == GameState.PregameMenu && !Components.Contains(preGameMenuPanel)) { Components.Clear(); Components.Add(preGameMenuPanel); KeyboardDispatcher.Subscriber = null; usernameTextbox.OnEnterPressed -= loginTBEvent; passwordTextbox.OnEnterPressed -= loginTBEvent; confirmPasswordTextbox.OnEnterPressed -= registerTBEvent; if (!FXCollection.Songs[Songs.PreGame].Playing) { FXCollection.Songs[Songs.PreGame].Play(); } if (FXCollection.Songs[Songs.InGame].Playing) { FXCollection.Songs[Songs.InGame].Stop(); } if (!backgroundPanel.Components.Contains(background)) { backgroundPanel.Components.Add(background); } if (!backgroundPanel.Components.Contains(PreGameMenu)) { backgroundPanel.Components.Add(PreGameMenu); } } else if (World.GameState == GameState.Login && !Components.Contains(loginPanel)) { loginPanel.ClearTextBoxes(); //if going back to the login screen clear the previously typed data out of the text boxes Components.Clear(); Components.Add(loginPanel); KeyboardDispatcher.Subscriber = usernameTextbox; cancelHyperlink.DrawLocation = new Vector2(cancelHyperlink.DrawLocation.X, GraphicsDevice.Viewport.Height / 2 + 30); usernameTextbox.OnEnterPressed -= registerTBEvent; passwordTextbox.OnEnterPressed -= registerTBEvent; passwordTextbox.OnTabPressed -= subscribeToConfirmBox; confirmPasswordTextbox.OnEnterPressed -= registerTBEvent; usernameTextbox.OnEnterPressed += loginTBEvent; passwordTextbox.OnEnterPressed += loginTBEvent; passwordTextbox.OnTabPressed += subscribeToUsernameBox; if (!FXCollection.Songs[Songs.PreGame].Playing) { FXCollection.Songs[Songs.PreGame].Play(); } if (FXCollection.Songs[Songs.InGame].Playing) { FXCollection.Songs[Songs.InGame].Stop(); } if (backgroundPanel.Components.Contains(PreGameMenu)) { backgroundPanel.Components.Remove(PreGameMenu); } } else if (World.GameState == GameState.InGame && !Components.Contains(inGamePanel)) { InputHandler.PreviousState = state; Components.Clear(); Components.Add(inGamePanel); //the text boxes are ALWAYS listening (because the subscriber listening for an enter keypress works outside of the game component model) usernameTextbox.OnEnterPressed -= loginTBEvent; passwordTextbox.OnEnterPressed -= loginTBEvent; confirmPasswordTextbox.OnEnterPressed -= registerTBEvent; if (FXCollection.Songs[Songs.PreGame].Playing) { FXCollection.Songs[Songs.PreGame].Stop(); } if (!FXCollection.Songs[Songs.InGame].Playing) { FXCollection.Songs[Songs.InGame].Play(); } } else if (World.GameState == GameState.Register && !Components.Contains(registerPanel)) { Components.Clear(); Components.Add(registerPanel); KeyboardDispatcher.Subscriber = usernameTextbox; cancelHyperlink.DrawLocation = new Vector2(cancelHyperlink.DrawLocation.X, GraphicsDevice.Viewport.Height / 2 + 80); usernameTextbox.OnEnterPressed -= loginTBEvent; passwordTextbox.OnEnterPressed -= loginTBEvent; passwordTextbox.OnTabPressed -= subscribeToUsernameBox; confirmPasswordTextbox.OnEnterPressed += registerTBEvent; usernameTextbox.OnEnterPressed += registerTBEvent; passwordTextbox.OnEnterPressed += registerTBEvent; passwordTextbox.OnTabPressed += subscribeToConfirmBox; if (!FXCollection.Songs[Songs.PreGame].Playing) { FXCollection.Songs[Songs.PreGame].Play(); } if (FXCollection.Songs[Songs.InGame].Playing) { FXCollection.Songs[Songs.InGame].Stop(); } if (backgroundPanel.Components.Contains(PreGameMenu)) { backgroundPanel.Components.Remove(PreGameMenu); } } else if (World.GameState == GameState.HowToPlay && !Components.Contains(howToPlayPanel)) { Components.Clear(); Components.Add(howToPlayPanel); if (backgroundPanel.Components.Contains(PreGameMenu)) { backgroundPanel.Components.Remove(PreGameMenu); } } if (state.IsKeyDown(Keys.Escape) && !previousState.IsKeyDown(Keys.Escape)) { if (World.GameState == GameState.Login || World.GameState == GameState.Register || World.GameState == GameState.HowToPlay) { FXCollection.SoundEffects[SoundEffects.Select].Play(); usernameTextbox.Text = ""; passwordTextbox.Text = ""; confirmPasswordTextbox.Text = ""; World.GameState = GameState.PregameMenu; } } // Render new text messages if (World.GameState == GameState.InGame) { if (untexturedChatMessages.Count > 0) { foreach (var pair in untexturedChatMessages) { CurrentChatMessages.Add(pair.Key, chatRenderer.GenerateTexture(pair.Value)); } untexturedChatMessages.Clear(); } } //We have an in-game dialog (for teh winz) change the mouse visibility. If a dialog is open, DON'T CHANGE IT BACK! if (XNADialog.ModalDialogs != null && XNADialog.ModalDialogs.Count == 0) { IsMouseVisible = World.GameState == GameState.Login || World.GameState == GameState.Register || World.GameState == GameState.LoggingIn || World.GameState == GameState.Registering || World.GameState == GameState.PregameMenu || World.GameState == GameState.HowToPlay; } previousState = state; foreach (LoopedSoundPlayer player in FXCollection.Songs) { player.Update(); } base.Update(gameTime); }
protected override void Initialize() { (System.Windows.Forms.Form.FromHandle(this.Window.Handle)).Visible = false; try { World.MainPlayer = new ConnectedPlayer("127.0.0.1", 8085 /*, true*/); //uncomment to try out dual-mode IPv6 sockets! World.MainPlayer.AddChatMessage = (message) => { untexturedChatMessages.Add(DateTime.Now, message); if (untexturedChatMessages.Count + CurrentChatMessages.Count > maxChatRecords) { CurrentChatMessages.RemoveAt(CurrentChatMessages.Count - 1); } }; World.MainPlayer.OnSocketError = (o, e) => { World.GameState = GameState.PregameMenu; XNADialog dlg = new XNADialog(this, "You were unexpectedly disconnected from the game server! Restart the game.", "Server connection error!"); dlg.CloseAction = (s) => { //exit the game once the dialog is closed (custom close action) this.Dispose(); this.Exit(); }; }; (System.Windows.Forms.Form.FromHandle(this.Window.Handle)).Visible = true; } catch { // Let the client know that we couldn't connect to the server System.Windows.Forms.MessageBox.Show("The client was unable to contact the server. Make sure the server is started.", "Error connecting!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); Environment.Exit(0); Dispose(); return; } ShowChatMessages = false; ShowChatPrompt = false; GamePaused = false; CurrentChatMessages = new SortedList <DateTime, Texture2D>(new DescendedDateComparer()); untexturedChatMessages = new SortedList <DateTime, ChatMessage>(); // Load map try { World.CurrentMap = new Map(typeof(Map).Assembly.GetManifestResourceStream("GameClassLibrary.game.bmap")); // Make sure all items are initialized for (int i = 0; i < World.CurrentMap.Width; i++) { for (int j = 0; j < World.CurrentMap.Height; j++) { World.CurrentMap.AddTile(new Vector2(i, j), LAYERS.Item, new ItemTile(ItemTileSpec.NONE, i, j)); } } } catch { throw new Exception("There was a problem loading the map!"); } base.Initialize(); }