1/*
2 Copyright (c) 2013 yvt
3
4 This file is part of OpenSpades.
5
6 OpenSpades is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OpenSpades is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OpenSpades.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20#include "ChatSayWindow.as"
21
22namespace spades {
23
24    class ChatLogSayWindow: ClientChatWindow {
25        ChatLogWindow@ owner;
26
27        ChatLogSayWindow(ChatLogWindow@ own, bool isTeamChat) {
28            super(own.ui, isTeamChat);
29            @owner = own;
30        }
31
32        void Close() {
33            owner.SayWindowClosed();
34            @this.Parent = null;
35        }
36    }
37
38    class ChatLogWindow: spades::ui::UIElement {
39
40        float contentsTop, contentsHeight;
41
42        ClientUI@ ui;
43        private ClientUIHelper@ helper;
44
45        private spades::ui::TextViewer@ viewer;
46        ChatLogSayWindow@ sayWindow;
47
48        private spades::ui::UIElement@ sayButton1;
49        private spades::ui::UIElement@ sayButton2;
50
51        ChatLogWindow(ClientUI@ ui) {
52            super(ui.manager);
53            @this.ui = ui;
54            @this.helper = ui.helper;
55
56            @Font = Manager.RootElement.Font;
57            this.Bounds = Manager.RootElement.Bounds;
58
59            float contentsWidth = 700.f;
60            float contentsLeft = (Manager.Renderer.ScreenWidth - contentsWidth) * 0.5f;
61            contentsHeight = Manager.Renderer.ScreenHeight - 200.f;
62            contentsTop = (Manager.Renderer.ScreenHeight - contentsHeight - 106.f) * 0.5f;
63            {
64                spades::ui::Label label(Manager);
65                label.BackgroundColor = Vector4(0, 0, 0, 0.4f);
66                label.Bounds = Bounds;
67                AddChild(label);
68            }
69            {
70                spades::ui::Label label(Manager);
71                label.BackgroundColor = Vector4(0, 0, 0, 0.8f);
72                label.Bounds = AABB2(0.f, contentsTop - 13.f, Size.x, contentsHeight + 27.f);
73                AddChild(label);
74            }
75            {
76                spades::ui::Button button(Manager);
77                button.Caption = _Tr("Client", "Close");
78                button.Bounds = AABB2(
79                    contentsLeft + contentsWidth - 150.f,
80                    contentsTop + contentsHeight - 30.f
81                    , 150.f, 30.f);
82                @button.Activated = spades::ui::EventHandler(this.OnOkPressed);
83                AddChild(button);
84            }
85            {
86                spades::ui::Button button(Manager);
87                button.Caption = _Tr("Client", "Say Global");
88                button.Bounds = AABB2(
89                    contentsLeft,
90                    contentsTop + contentsHeight - 30.f
91                    , 150.f, 30.f);
92                @button.Activated = spades::ui::EventHandler(this.OnGlobalChat);
93                AddChild(button);
94                @this.sayButton1 = button;
95            }
96            {
97                spades::ui::Button button(Manager);
98                button.Caption = _Tr("Client", "Say Team");
99                button.Bounds = AABB2(
100                    contentsLeft + 155.f,
101                    contentsTop + contentsHeight - 30.f
102                    , 150.f, 30.f);
103                @button.Activated = spades::ui::EventHandler(this.OnTeamChat);
104                AddChild(button);
105                @this.sayButton2 = button;
106            }
107            {
108                spades::ui::TextViewer viewer(Manager);
109                AddChild(viewer);
110                viewer.Bounds = AABB2(contentsLeft, contentsTop, contentsWidth, contentsHeight - 40.f);
111                @this.viewer = viewer;
112            }
113        }
114
115        void ScrollToEnd() {
116            viewer.Layout();
117            viewer.ScrollToEnd();
118        }
119
120        void Close() {
121            @ui.ActiveUI = null;
122        }
123
124        void SayWindowClosed() {
125            @sayWindow = null;
126            sayButton1.Enable = true;
127            sayButton2.Enable = true;
128        }
129
130        private void OnOkPressed(spades::ui::UIElement@ sender) {
131            Close();
132        }
133
134        private void OnTeamChat(spades::ui::UIElement@ sender) {
135            if(sayWindow !is null) {
136                sayWindow.IsTeamChat = true;
137                return;
138            }
139            sayButton1.Enable = false;
140            sayButton2.Enable = false;
141            ChatLogSayWindow wnd(this, true);
142            AddChild(wnd);
143            wnd.Bounds = this.Bounds;
144            @this.sayWindow = wnd;
145            @Manager.ActiveElement = wnd.field;
146        }
147
148        private void OnGlobalChat(spades::ui::UIElement@ sender) {
149            if(sayWindow !is null) {
150                sayWindow.IsTeamChat = false;
151                return;
152            }
153            sayButton1.Enable = false;
154            sayButton2.Enable = false;
155            ChatLogSayWindow wnd(this, false);
156            AddChild(wnd);
157            wnd.Bounds = this.Bounds;
158            @this.sayWindow = wnd;
159            @Manager.ActiveElement = wnd.field;
160        }
161
162        void HotKey(string key) {
163            if(sayWindow !is null) {
164                UIElement::HotKey(key);
165                return;
166            }
167            if(IsEnabled and (key == "Escape")) {
168                Close();
169            } else if(IsEnabled and (key == "y")) {
170                OnTeamChat(this);
171            } else if(IsEnabled and (key == "t")) {
172                OnGlobalChat(this);
173            } else {
174                UIElement::HotKey(key);
175            }
176        }
177
178        void Record(string text, Vector4 color) {
179            viewer.AddLine(text, this.IsVisible, color);
180        }
181
182        void Render() {
183            Vector2 pos = ScreenPosition;
184            Vector2 size = Size;
185            Renderer@ r = Manager.Renderer;
186            Image@ img = r.RegisterImage("Gfx/White.tga");
187
188            r.ColorNP = Vector4(1, 1, 1, 0.08f);
189            r.DrawImage(img,
190                AABB2(pos.x, pos.y + contentsTop - 15.f, size.x, 1.f));
191            r.DrawImage(img,
192                AABB2(pos.x, pos.y + contentsTop + contentsHeight + 15.f, size.x, 1.f));
193            r.ColorNP = Vector4(1, 1, 1, 0.2f);
194            r.DrawImage(img,
195                AABB2(pos.x, pos.y + contentsTop - 14.f, size.x, 1.f));
196            r.DrawImage(img,
197                AABB2(pos.x, pos.y + contentsTop + contentsHeight + 14.f, size.x, 1.f));
198
199            UIElement::Render();
200        }
201
202    }
203
204}
205