-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpeakerList.cpp
246 lines (195 loc) · 8.3 KB
/
SpeakerList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//
// by Weikton 05.09.23
//
#include "main.h"
#include "CLoader.h"
#include "game/game.h"
#include "game/playerped.h"
#include "net/netgame.h"
#include "SpeakerList.h"
#include "PluginConfig.h"
extern CGUI *pGUI;
extern CNetGame *pNetGame;
extern CLoader *pLoader;
bool SpeakerList::Init() noexcept
{
if(SpeakerList::initStatus)
return false;
try
{
SpeakerList::tSpeakerIcon = (RwTexture*)LoadTextureFromDB("samp", "speaker");
}
catch (const std::exception& exception)
{
LogVoice("[sv:err:speakerlist:init] : failed to create speaker icon");
SpeakerList::tSpeakerIcon = nullptr;
return false;
}
if(!PluginConfig::IsSpeakerLoaded())
{
PluginConfig::SetSpeakerLoaded(true);
SpeakerList::ResetConfigs();
}
SpeakerList::initStatus = true;
SpeakerList::SyncConfigs();
return true;
}
void SpeakerList::Free() noexcept
{
if(!SpeakerList::initStatus)
return;
SpeakerList::tSpeakerIcon = nullptr;
SpeakerList::initStatus = false;
}
void SpeakerList::Show() noexcept
{
SpeakerList::showStatus = true;
}
bool SpeakerList::IsShowed() noexcept
{
return SpeakerList::showStatus;
}
void SpeakerList::Hide() noexcept
{
SpeakerList::showStatus = false;
}
void SpeakerList::Render()
{
if(!SpeakerList::initStatus || !SpeakerList::IsShowed())
return;
if(!pNetGame) return;
CPlayerPool *pPlayerPool = pNetGame->GetPlayerPool();
if(!pPlayerPool) return;
int curTextLine;
char szText[256], szText2[256];
ImVec2 textPos = ImVec2(pGUI->ScaleX(24), pGUI->ScaleY(480));
for(PLAYERID playerId { 0 }; playerId < MAX_PLAYERS; ++playerId)
{
CRemotePlayer* pPlayer = pPlayerPool->GetAt(playerId);
if(pPlayer && pPlayer->IsActive())
{
CPlayerPed* pPlayerPed = pPlayer->GetPlayerPed();
if(pPlayerPed)
{
if(const auto playerName = pPlayerPool->GetPlayerName(playerId); playerName)
{
if(!SpeakerList::playerStreams[playerId].empty())
{
if(!pLoader->Get().bVoiceChatEnable) continue;
for(const auto& playerStream : SpeakerList::playerStreams[playerId])
{
if(!pLoader->Get().bVoiceChatEnable) continue;
if(playerStream.second.GetType() != StreamType::GlobalStream)
{
if(pPlayerPed->GetDistanceFromLocalPlayerPed() > playerStream.second.GetDistance())
SpeakerList::OnSpeakerStop(*SpeakerList::playerStream[playerId], playerId);
}
if(playerStream.second.GetType() == StreamType::LocalStreamAtPlayer)
{
VECTOR VecPos;
if(!pPlayerPed->IsAdded()) continue;
VecPos.X = 0.0f;
VecPos.Y = 0.0f;
VecPos.Z = 0.0f;
pPlayerPed->GetBonePosition(8, &VecPos);
CAMERA_AIM *pCam = GameGetInternalAim();
int dwHitEntity = 0;
dwHitEntity = ScriptCommand(&get_line_of_sight, VecPos.X, VecPos.Y, VecPos.Z,
pCam->pos1x, pCam->pos1y, pCam->pos1z, 1, 0, 0, 1, 0);
if(dwHitEntity && pPlayerPed->GetDistanceFromLocalPlayerPed() <= 40.0f)
SpeakerList::Draw(&VecPos, pPlayerPed->GetDistanceFromCamera());
}
}
if(curTextLine < 8)
{
ImVec2 a = ImVec2(textPos.x, textPos.y);
ImVec2 b = ImVec2(textPos.x + pGUI->GetFontSize(), textPos.y + pGUI->GetFontSize());
ImGui::GetBackgroundDrawList()->AddImage((ImTextureID)SpeakerList::tSpeakerIcon->raster, a, b);
float fSpaceFromIconToText = pGUI->GetFontSize() + ImGui::GetStyle().ItemSpacing.x;
unsigned char RGBcolors[3];
RGBcolors[0] = (pPlayerPool->GetAt(playerId)->GetPlayerColorAsARGB() - 0xFF000000) >> 16;
RGBcolors[1] = ((pPlayerPool->GetAt(playerId)->GetPlayerColorAsARGB() - 0xFF000000) & 0x00ff00) >> 8;
RGBcolors[2] = ((pPlayerPool->GetAt(playerId)->GetPlayerColorAsARGB() - 0xFF000000) & 0x0000ff);
textPos.x += fSpaceFromIconToText;
sprintf(szText, "%s (%d) ", playerName, playerId);
pGUI->RenderText(textPos, ImColor(RGBcolors[0], RGBcolors[1], RGBcolors[2]), false, szText);
for(const auto& streamInfo : SpeakerList::playerStreams[playerId])
{
if(!pLoader->Get().bVoiceChatEnable) continue;
if(streamInfo.second.GetColor() == NULL)
continue;
textPos.x += ImGui::CalcTextSize(szText).x;
sprintf(szText2, "[%s]", streamInfo.second.GetName().c_str());
pGUI->RenderText(textPos, streamInfo.second.GetColor(), false, szText2);
}
textPos.x -= fSpaceFromIconToText;
textPos.x -= ImGui::CalcTextSize(szText).x;
textPos.y += pGUI->GetFontSize();
curTextLine++;
}
}
}
}
}
}
}
void SpeakerList::Draw(VECTOR* vec, float fDist)
{
VECTOR TagPos;
TagPos.X = vec->X;
TagPos.Y = vec->Y;
TagPos.Z = vec->Z;
TagPos.Z += 0.25f + (fDist * 0.0475f);
VECTOR Out;
// CSprite::CalcScreenCoors
(( void (*)(VECTOR*, VECTOR*, float*, float*, bool, bool))(g_libGTASA+0x54EEC0+1))(&TagPos, &Out, 0, 0, 0, 0);
if(Out.Z < 1.0f)
return;
ImVec2 pos = ImVec2(Out.X, Out.Y);
pos.x -= SpeakerList::GetSpeakerIconScale() / 2;
pos.y -= pGUI->GetFontSize() * 1.5;
ImVec2 a = ImVec2(pos.x, pos.y);
ImVec2 b = ImVec2(pos.x + SpeakerList::GetSpeakerIconScale(), pos.y + SpeakerList::GetSpeakerIconScale());
ImGui::GetBackgroundDrawList()->AddImage((ImTextureID)SpeakerList::tSpeakerIcon->raster, a, b);
}
void SpeakerList::OnSpeakerPlay(const Stream& stream, const uint16_t speaker) noexcept
{
uint16_t wSpeaker = speaker;
if(speaker < 0) wSpeaker = 0;
else if(speaker > MAX_PLAYERS - 1) wSpeaker = MAX_PLAYERS - 1;
if(speaker != wSpeaker) return;
SpeakerList::playerStream[speaker] = &stream;
SpeakerList::playerStreams[speaker][(Stream*)(&stream)] = stream.GetInfo();
}
void SpeakerList::OnSpeakerStop(const Stream& stream, const uint16_t speaker) noexcept
{
uint16_t wSpeaker = speaker;
if(speaker < 0) wSpeaker = 0;
else if(speaker > MAX_PLAYERS - 1) wSpeaker = MAX_PLAYERS - 1;
if(speaker != wSpeaker) return;
SpeakerList::playerStreams[speaker].erase((Stream*)(&stream));
}
float SpeakerList::GetSpeakerIconScale() noexcept
{
return PluginConfig::GetSpeakerIconScale();
}
void SpeakerList::SetSpeakerIconScale(const float speakerIconScale) noexcept
{
float iSpeakerIconScale = speakerIconScale;
if(speakerIconScale < 20.f) iSpeakerIconScale = 20.f;
else if(speakerIconScale > 60.f) iSpeakerIconScale = 60.f;
PluginConfig::SetSpeakerIconScale(iSpeakerIconScale);
}
void SpeakerList::SyncConfigs() noexcept
{
SpeakerList::SetSpeakerIconScale(PluginConfig::GetSpeakerIconScale());
}
void SpeakerList::ResetConfigs() noexcept
{
PluginConfig::SetSpeakerIconScale(PluginConfig::kSpeakerIconSize);
}
std::array<std::unordered_map<Stream*, StreamInfo>, MAX_PLAYERS> SpeakerList::playerStreams;
std::array<const Stream*, MAX_PLAYERS> SpeakerList::playerStream;
bool SpeakerList::initStatus { false };
bool SpeakerList::showStatus { false };
RwTexture* SpeakerList::tSpeakerIcon { nullptr };