ddnet/src/game/client/components/emoticon.cpp

169 lines
3.8 KiB
C++
Raw Normal View History

2010-05-29 07:25:38 +00:00
#include <engine/graphics.h>
#include <engine/shared/config.h>
#include <game/generated/protocol.h>
#include <game/generated/client_data.h>
#include <game/gamecore.h> // get_angle
#include <game/client/gameclient.h>
#include <game/client/ui.h>
#include <game/client/render.h>
#include "emoticon.h"
CEmoticon::CEmoticon()
{
2010-05-29 07:25:38 +00:00
OnReset();
}
2010-05-29 07:25:38 +00:00
void CEmoticon::ConKeyEmoticon(IConsole::IResult *pResult, void *pUserData)
2008-08-30 08:20:06 +00:00
{
CEmoticon *pSelf = (CEmoticon *)pUserData;
if(!pSelf->m_pClient->m_Snap.m_Spectate && pSelf->Client()->State() != IClient::STATE_DEMOPLAYBACK)
((CEmoticon *)pUserData)->m_Active = pResult->GetInteger(0) != 0;
2008-08-30 08:20:06 +00:00
}
2010-05-29 07:25:38 +00:00
void CEmoticon::ConEmote(IConsole::IResult *pResult, void *pUserData)
2008-08-31 13:14:28 +00:00
{
2010-05-29 07:25:38 +00:00
((CEmoticon *)pUserData)->Emote(pResult->GetInteger(0));
2008-08-31 13:14:28 +00:00
}
2010-05-29 07:25:38 +00:00
void CEmoticon::OnConsoleInit()
2008-08-30 08:20:06 +00:00
{
2010-05-29 07:25:38 +00:00
Console()->Register("+emote", "", CFGFLAG_CLIENT, ConKeyEmoticon, this, "Open emote selector");
Console()->Register("emote", "i", CFGFLAG_CLIENT, ConEmote, this, "Use emote");
2008-08-30 08:20:06 +00:00
}
2010-05-29 07:25:38 +00:00
void CEmoticon::OnReset()
{
2010-05-29 07:25:38 +00:00
m_WasActive = false;
m_Active = false;
m_SelectedEmote = -1;
}
void CEmoticon::OnRelease()
{
m_Active = false;
}
2010-05-29 07:25:38 +00:00
void CEmoticon::OnMessage(int MsgType, void *pRawMsg)
{
}
2010-05-29 07:25:38 +00:00
bool CEmoticon::OnMouseMove(float x, float y)
{
2010-05-29 07:25:38 +00:00
if(!m_Active)
2008-08-30 08:20:06 +00:00
return false;
2010-05-29 07:25:38 +00:00
m_SelectorMouse += vec2(x,y);
2008-08-30 08:20:06 +00:00
return true;
}
2010-05-29 07:25:38 +00:00
void CEmoticon::DrawCircle(float x, float y, float r, int Segments)
{
2010-05-29 07:25:38 +00:00
IGraphics::CFreeformItem Array[32];
int NumItems = 0;
float FSegments = (float)Segments;
for(int i = 0; i < Segments; i+=2)
{
2010-05-29 07:25:38 +00:00
float a1 = i/FSegments * 2*pi;
float a2 = (i+1)/FSegments * 2*pi;
float a3 = (i+2)/FSegments * 2*pi;
float Ca1 = cosf(a1);
float Ca2 = cosf(a2);
float Ca3 = cosf(a3);
float Sa1 = sinf(a1);
float Sa2 = sinf(a2);
float Sa3 = sinf(a3);
Array[NumItems++] = IGraphics::CFreeformItem(
x, y,
2010-05-29 07:25:38 +00:00
x+Ca1*r, y+Sa1*r,
x+Ca3*r, y+Sa3*r,
x+Ca2*r, y+Sa2*r);
if(NumItems == 32)
{
m_pClient->Graphics()->QuadsDrawFreeform(Array, 32);
NumItems = 0;
}
}
2010-05-29 07:25:38 +00:00
if(NumItems)
m_pClient->Graphics()->QuadsDrawFreeform(Array, NumItems);
}
2010-05-29 07:25:38 +00:00
void CEmoticon::OnRender()
{
2010-05-29 07:25:38 +00:00
if(!m_Active)
2008-08-30 08:20:06 +00:00
{
2010-05-29 07:25:38 +00:00
if(m_WasActive && m_SelectedEmote != -1)
Emote(m_SelectedEmote);
m_WasActive = false;
2008-08-30 08:20:06 +00:00
return;
}
2010-05-29 07:25:38 +00:00
m_WasActive = true;
2008-08-30 08:20:06 +00:00
float x, y;
2010-05-29 07:25:38 +00:00
Input()->MouseRelative(&x, &y);
2010-05-29 07:25:38 +00:00
m_SelectorMouse.x += x;
m_SelectorMouse.y += y;
2010-05-29 07:25:38 +00:00
if (length(m_SelectorMouse) > 140)
m_SelectorMouse = normalize(m_SelectorMouse) * 140;
2010-05-29 07:25:38 +00:00
float SelectedAngle = GetAngle(m_SelectorMouse) + 2*pi/24;
if (SelectedAngle < 0)
SelectedAngle += 2*pi;
2010-05-29 07:25:38 +00:00
if (length(m_SelectorMouse) > 100)
m_SelectedEmote = (int)(SelectedAngle / (2*pi) * 12.0f);
2010-05-29 07:25:38 +00:00
CUIRect Screen = *UI()->Screen();
2010-05-29 07:25:38 +00:00
Graphics()->MapScreen(Screen.x, Screen.y, Screen.w, Screen.h);
2009-10-27 14:38:53 +00:00
Graphics()->BlendNormal();
2009-10-27 14:38:53 +00:00
Graphics()->TextureSet(-1);
Graphics()->QuadsBegin();
Graphics()->SetColor(0,0,0,0.3f);
2010-05-29 07:25:38 +00:00
DrawCircle(Screen.w/2, Screen.h/2, 160, 64);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
2010-05-29 07:25:38 +00:00
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_EMOTICONS].m_Id);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsBegin();
for (int i = 0; i < 12; i++)
{
2010-05-29 07:25:38 +00:00
float Angle = 2*pi*i/12.0;
if (Angle > pi)
Angle -= 2*pi;
2010-05-29 07:25:38 +00:00
bool Selected = m_SelectedEmote == i;
2010-05-29 07:25:38 +00:00
float Size = Selected ? 96 : 64;
2010-05-29 07:25:38 +00:00
float NudgeX = 120 * cosf(Angle);
float NudgeY = 120 * sinf(Angle);
RenderTools()->SelectSprite(SPRITE_OOP + i);
IGraphics::CQuadItem QuadItem(Screen.w/2 + NudgeX, Screen.h/2 + NudgeY, Size, Size);
Graphics()->QuadsDraw(&QuadItem, 1);
}
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
2010-05-29 07:25:38 +00:00
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_CURSOR].m_Id);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsBegin();
Graphics()->SetColor(1,1,1,1);
2010-05-29 07:25:38 +00:00
IGraphics::CQuadItem QuadItem(m_SelectorMouse.x+Screen.w/2,m_SelectorMouse.y+Screen.h/2,24,24);
Graphics()->QuadsDrawTL(&QuadItem, 1);
2009-10-27 14:38:53 +00:00
Graphics()->QuadsEnd();
}
2010-05-29 07:25:38 +00:00
void CEmoticon::Emote(int Emoticon)
{
2010-05-29 07:25:38 +00:00
CNetMsg_Cl_Emoticon Msg;
Msg.m_Emoticon = Emoticon;
Client()->SendPackMsg(&Msg, MSGFLAG_VITAL);
}