Chat.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "Sample.h"
  5. namespace Urho3D
  6. {
  7. class Button;
  8. class LineEdit;
  9. class Text;
  10. class UIElement;
  11. }
  12. /// Chat example
  13. /// This sample demonstrates:
  14. /// - Starting up a network server or connecting to it
  15. /// - Implementing simple chat functionality with network messages
  16. class Chat : public Sample
  17. {
  18. URHO3D_OBJECT(Chat, Sample);
  19. public:
  20. /// Construct.
  21. explicit Chat(Context* context);
  22. /// Setup after engine initialization and before running the main loop.
  23. void Start() override;
  24. protected:
  25. /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
  26. String GetScreenJoystickPatchString() const override { return
  27. "<patch>"
  28. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
  29. " <attribute name=\"Is Visible\" value=\"false\" />"
  30. " </add>"
  31. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
  32. " <attribute name=\"Is Visible\" value=\"false\" />"
  33. " </add>"
  34. "</patch>";
  35. }
  36. private:
  37. /// Create the UI.
  38. void CreateUI();
  39. /// Subscribe to log message, UI and network events.
  40. void SubscribeToEvents();
  41. /// Create a button to the button container.
  42. Button* CreateButton(const String& text, int width);
  43. /// Print chat text.
  44. void ShowChatText(const String& row);
  45. /// Update visibility of buttons according to connection and server status.
  46. void UpdateButtons();
  47. /// Handle log message event; pipe it also to the chat display.
  48. void HandleLogMessage(StringHash eventType, VariantMap& eventData);
  49. /// Handle pressing the send button.
  50. void HandleSend(StringHash eventType, VariantMap& eventData);
  51. /// Handle pressing the connect button.
  52. void HandleConnect(StringHash eventType, VariantMap& eventData);
  53. /// Handle pressing the disconnect button.
  54. void HandleDisconnect(StringHash eventType, VariantMap& eventData);
  55. /// Handle pressing the start server button.
  56. void HandleStartServer(StringHash eventType, VariantMap& eventData);
  57. /// Handle an incoming network message.
  58. void HandleNetworkMessage(StringHash eventType, VariantMap& eventData);
  59. /// Handle connection status change (just update the buttons that should be shown.)
  60. void HandleConnectionStatus(StringHash eventType, VariantMap& eventData);
  61. /// Strings printed so far.
  62. Vector<String> chatHistory_;
  63. /// Chat text element.
  64. SharedPtr<Text> chatHistoryText_;
  65. /// Button container element.
  66. SharedPtr<UIElement> buttonContainer_;
  67. /// Server address / chat message line editor element.
  68. SharedPtr<LineEdit> textEdit_;
  69. /// Send button.
  70. SharedPtr<Button> sendButton_;
  71. /// Connect button.
  72. SharedPtr<Button> connectButton_;
  73. /// Disconnect button.
  74. SharedPtr<Button> disconnectButton_;
  75. /// Start server button.
  76. SharedPtr<Button> startServerButton_;
  77. };