1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "Sample.h"
26 
27 namespace Urho3D
28 {
29 
30 class Button;
31 class LineEdit;
32 class Text;
33 class UIElement;
34 
35 }
36 
37 /// Chat example
38 /// This sample demonstrates:
39 ///     - Starting up a network server or connecting to it
40 ///     - Implementing simple chat functionality with network messages
41 class Chat : public Sample
42 {
43     URHO3D_OBJECT(Chat, Sample);
44 
45 public:
46     /// Construct.
47     Chat(Context* context);
48 
49     /// Setup after engine initialization and before running the main loop.
50     virtual void Start();
51 
52 protected:
53     /// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
GetScreenJoystickPatchString()54     virtual String GetScreenJoystickPatchString() const { return
55         "<patch>"
56         "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">"
57         "        <attribute name=\"Is Visible\" value=\"false\" />"
58         "    </add>"
59         "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
60         "        <attribute name=\"Is Visible\" value=\"false\" />"
61         "    </add>"
62         "</patch>";
63     }
64 
65 private:
66     /// Create the UI.
67     void CreateUI();
68     /// Subscribe to log message, UI and network events.
69     void SubscribeToEvents();
70     /// Create a button to the button container.
71     Button* CreateButton(const String& text, int width);
72     /// Print chat text.
73     void ShowChatText(const String& row);
74     /// Update visibility of buttons according to connection and server status.
75     void UpdateButtons();
76     /// Handle log message event; pipe it also to the chat display.
77     void HandleLogMessage(StringHash eventType, VariantMap& eventData);
78     /// Handle pressing the send button.
79     void HandleSend(StringHash eventType, VariantMap& eventData);
80     /// Handle pressing the connect button.
81     void HandleConnect(StringHash eventType, VariantMap& eventData);
82     /// Handle pressing the disconnect button.
83     void HandleDisconnect(StringHash eventType, VariantMap& eventData);
84     /// Handle pressing the start server button.
85     void HandleStartServer(StringHash eventType, VariantMap& eventData);
86     /// Handle an incoming network message.
87     void HandleNetworkMessage(StringHash eventType, VariantMap& eventData);
88     /// Handle connection status change (just update the buttons that should be shown.)
89     void HandleConnectionStatus(StringHash eventType, VariantMap& eventData);
90     /// Strings printed so far.
91     Vector<String> chatHistory_;
92     /// Chat text element.
93     SharedPtr<Text> chatHistoryText_;
94     /// Button container element.
95     SharedPtr<UIElement> buttonContainer_;
96     /// Server address / chat message line editor element.
97     SharedPtr<LineEdit> textEdit_;
98     /// Send button.
99     SharedPtr<Button> sendButton_;
100     /// Connect button.
101     SharedPtr<Button> connectButton_;
102     /// Disconnect button.
103     SharedPtr<Button> disconnectButton_;
104     /// Start server button.
105     SharedPtr<Button> startServerButton_;
106 };
107