1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "emoticon.h"
4
5#include "chat.h"
6
7#include <base/dbg.h>
8
9#include <engine/graphics.h>
10#include <engine/shared/config.h>
11
12#include <generated/protocol.h>
13
14#include <game/client/animstate.h>
15#include <game/client/gameclient.h>
16#include <game/client/ui.h>
17
18CEmoticon::CEmoticon()
19{
20 m_SelectorMouse = vec2(0.0f, 0.0f);
21 OnReset();
22}
23
24void CEmoticon::ConKeyEmoticon(IConsole::IResult *pResult, void *pUserData)
25{
26 CEmoticon *pSelf = (CEmoticon *)pUserData;
27
28 if(pSelf->GameClient()->m_Scoreboard.IsActive())
29 return;
30
31 if(!pSelf->GameClient()->m_Snap.m_SpecInfo.m_Active && pSelf->Client()->State() != IClient::STATE_DEMOPLAYBACK)
32 pSelf->m_Active = pResult->GetInteger(Index: 0) != 0;
33}
34
35void CEmoticon::ConEmote(IConsole::IResult *pResult, void *pUserData)
36{
37 ((CEmoticon *)pUserData)->Emote(Emoticon: pResult->GetInteger(Index: 0));
38}
39
40void CEmoticon::OnConsoleInit()
41{
42 Console()->Register(pName: "+emote", pParams: "", Flags: CFGFLAG_CLIENT, pfnFunc: ConKeyEmoticon, pUser: this, pHelp: "Open emote selector");
43 Console()->Register(pName: "emote", pParams: "i[emote-id]", Flags: CFGFLAG_CLIENT, pfnFunc: ConEmote, pUser: this, pHelp: "Use emote");
44}
45
46void CEmoticon::OnReset()
47{
48 m_WasActive = false;
49 m_Active = false;
50 m_SelectedEmote = -1;
51 m_SelectedEyeEmote = -1;
52 m_TouchState = CUi::CTouchState();
53 m_TouchPressedOutside = false;
54}
55
56void CEmoticon::OnRelease()
57{
58 OnReset();
59}
60
61bool CEmoticon::OnCursorMove(float x, float y, IInput::ECursorType CursorType)
62{
63 if(!m_Active)
64 return false;
65
66 Ui()->ConvertMouseMove(pX: &x, pY: &y, CursorType);
67 m_SelectorMouse += vec2(x, y);
68 return true;
69}
70
71bool CEmoticon::OnInput(const IInput::CEvent &Event)
72{
73 if(IsActive() && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_ESCAPE)
74 {
75 OnRelease();
76 return true;
77 }
78 return false;
79}
80
81void CEmoticon::OnRender()
82{
83 if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
84 return;
85
86 if(!m_Active)
87 {
88 if(m_TouchPressedOutside)
89 {
90 m_SelectedEmote = -1;
91 m_SelectedEyeEmote = -1;
92 m_TouchPressedOutside = false;
93 }
94
95 if(m_WasActive && m_SelectedEmote != -1)
96 Emote(Emoticon: m_SelectedEmote);
97 if(m_WasActive && m_SelectedEyeEmote != -1)
98 EyeEmote(EyeEmote: m_SelectedEyeEmote);
99 m_WasActive = false;
100 return;
101 }
102
103 if(GameClient()->m_Snap.m_SpecInfo.m_Active ||
104 !GameClient()->m_Snap.m_pLocalCharacter ||
105 GameClient()->m_Scoreboard.IsActive())
106 {
107 OnRelease();
108 return;
109 }
110
111 m_WasActive = true;
112
113 const CUIRect Screen = *Ui()->Screen();
114
115 const bool WasTouchPressed = m_TouchState.m_AnyPressed;
116 Ui()->UpdateTouchState(State&: m_TouchState);
117 if(m_TouchState.m_AnyPressed)
118 {
119 const vec2 TouchPos = (m_TouchState.m_PrimaryPosition - vec2(0.5f, 0.5f)) * Screen.Size();
120 const float TouchCenterDistance = length(a: TouchPos);
121 if(TouchCenterDistance <= 170.0f)
122 {
123 m_SelectorMouse = TouchPos;
124 }
125 else if(TouchCenterDistance > 190.0f)
126 {
127 m_TouchPressedOutside = true;
128 }
129 }
130 else if(WasTouchPressed)
131 {
132 m_Active = false;
133 return;
134 }
135
136 if(length(a: m_SelectorMouse) > 170.0f)
137 m_SelectorMouse = normalize(v: m_SelectorMouse) * 170.0f;
138
139 m_SelectedEmote = -1;
140 m_SelectedEyeEmote = -1;
141 if(length(a: m_SelectorMouse) > 110.0f)
142 m_SelectedEmote = (int)(std::fmod(x: angle(a: m_SelectorMouse) + 2 * pi + pi / (int)NUM_EMOTICONS, y: 2 * pi) / (2 * pi) * (float)NUM_EMOTICONS);
143 else if(length(a: m_SelectorMouse) > 40.0f)
144 m_SelectedEyeEmote = (int)(std::fmod(x: angle(a: m_SelectorMouse) + 2 * pi + pi / (int)NUM_EMOTES, y: 2 * pi) / (2 * pi) * (float)NUM_EMOTES);
145
146 const vec2 ScreenCenter = Screen.Center();
147
148 Ui()->MapScreen();
149
150 Graphics()->TextureClear();
151 Graphics()->QuadsBegin();
152 Graphics()->SetColor(r: 0, g: 0, b: 0, a: 0.3f);
153 Graphics()->DrawCircle(CenterX: ScreenCenter.x, CenterY: ScreenCenter.y, Radius: 190.0f, Segments: 64);
154 Graphics()->QuadsEnd();
155
156 Graphics()->WrapClamp();
157 for(int Emote = 0; Emote < NUM_EMOTICONS; Emote++)
158 {
159 float Angle = 2 * pi * Emote / (float)NUM_EMOTICONS;
160 if(Angle > pi)
161 Angle -= 2 * pi;
162
163 Graphics()->TextureSet(Texture: GameClient()->m_EmoticonsSkin.m_aSpriteEmoticons[Emote]);
164 Graphics()->QuadsSetSubset(TopLeftU: 0, TopLeftV: 0, BottomRightU: 1, BottomRightV: 1);
165 Graphics()->QuadsBegin();
166 const vec2 Nudge = direction(angle: Angle) * 150.0f;
167 const float Size = m_SelectedEmote == Emote ? 80.0f : 50.0f;
168 IGraphics::CQuadItem QuadItem(ScreenCenter.x + Nudge.x, ScreenCenter.y + Nudge.y, Size, Size);
169 Graphics()->QuadsDraw(pArray: &QuadItem, Num: 1);
170 Graphics()->QuadsEnd();
171 }
172 Graphics()->WrapNormal();
173
174 if(GameClient()->m_GameInfo.m_AllowEyeWheel && g_Config.m_ClEyeWheel && GameClient()->m_aLocalIds[g_Config.m_ClDummy] >= 0)
175 {
176 Graphics()->TextureClear();
177 Graphics()->QuadsBegin();
178 Graphics()->SetColor(r: 1.0, g: 1.0, b: 1.0, a: 0.3f);
179 Graphics()->DrawCircle(CenterX: ScreenCenter.x, CenterY: ScreenCenter.y, Radius: 100.0f, Segments: 64);
180 Graphics()->QuadsEnd();
181
182 CTeeRenderInfo TeeInfo = GameClient()->m_aClients[GameClient()->m_aLocalIds[g_Config.m_ClDummy]].m_RenderInfo;
183
184 for(int Emote = 0; Emote < NUM_EMOTES; Emote++)
185 {
186 float Angle = 2 * pi * Emote / (float)NUM_EMOTES;
187 if(Angle > pi)
188 Angle -= 2 * pi;
189
190 const vec2 Nudge = direction(angle: Angle) * 70.0f;
191 TeeInfo.m_Size = m_SelectedEyeEmote == Emote ? 64.0f : 48.0f;
192 RenderTools()->RenderTee(pAnim: CAnimState::GetIdle(), pInfo: &TeeInfo, Emote, Dir: vec2(-1, 0), Pos: ScreenCenter + Nudge);
193 }
194
195 Graphics()->TextureClear();
196 Graphics()->QuadsBegin();
197 Graphics()->SetColor(r: 0, g: 0, b: 0, a: 0.3f);
198 Graphics()->DrawCircle(CenterX: ScreenCenter.x, CenterY: ScreenCenter.y, Radius: 30.0f, Segments: 64);
199 Graphics()->QuadsEnd();
200 }
201 else
202 m_SelectedEyeEmote = -1;
203
204 RenderTools()->RenderCursor(Center: ScreenCenter + m_SelectorMouse, Size: 24.0f);
205}
206
207void CEmoticon::Emote(int Emoticon)
208{
209 CNetMsg_Cl_Emoticon Msg;
210 Msg.m_Emoticon = Emoticon;
211 Client()->SendPackMsgActive(pMsg: &Msg, Flags: MSGFLAG_VITAL);
212
213 if(g_Config.m_ClDummyCopyMoves)
214 {
215 CMsgPacker MsgDummy(NETMSGTYPE_CL_EMOTICON, false);
216 MsgDummy.AddInt(i: Emoticon);
217 Client()->SendMsg(Conn: !g_Config.m_ClDummy, pMsg: &MsgDummy, Flags: MSGFLAG_VITAL);
218 }
219}
220
221void CEmoticon::EyeEmote(int Emote)
222{
223 char aBuf[32];
224 switch(Emote)
225 {
226 case EMOTE_NORMAL:
227 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote normal %d", g_Config.m_ClEyeDuration);
228 break;
229 case EMOTE_PAIN:
230 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote pain %d", g_Config.m_ClEyeDuration);
231 break;
232 case EMOTE_HAPPY:
233 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote happy %d", g_Config.m_ClEyeDuration);
234 break;
235 case EMOTE_SURPRISE:
236 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote surprise %d", g_Config.m_ClEyeDuration);
237 break;
238 case EMOTE_ANGRY:
239 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote angry %d", g_Config.m_ClEyeDuration);
240 break;
241 case EMOTE_BLINK:
242 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "/emote blink %d", g_Config.m_ClEyeDuration);
243 break;
244 default:
245 dbg_assert_failed("Invalid Emote: %d", Emote);
246 }
247 GameClient()->m_Chat.SendChat(Team: 0, pLine: aBuf);
248}
249