-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cpp
267 lines (226 loc) · 7.98 KB
/
Menu.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "Menu.h"
#undef max
#undef min
ImVec4 Menu::ARGBToImVec4(const uint32_t& color)
{
const float
a = (color >> 24 & 0xFF) / 255.0f,
r = (color >> 16 & 0xFF) / 255.0f,
g = (color >> 8 & 0xFF) / 255.0f,
b = (color & 0xFF) / 255.0f;
return ImVec4(r, g, b, a);
}
uint32_t Menu::ImVec4ToARGB(const ImVec4& color)
{
const uint32_t
a = static_cast<uint32_t>(color.w * 255.0f) & 0xFF,
r = static_cast<uint32_t>(color.x * 255.0f) & 0xFF,
g = static_cast<uint32_t>(color.y * 255.0f) & 0xFF,
b = static_cast<uint32_t>(color.z * 255.0f) & 0xFF;
return (a << 24) | (r << 16) | (g << 8) | b;
}
void Menu::Render()
{
const auto& mSelectedLine = Chat::getInstance().mSelectedLine;
const auto& io = ImGui::GetIO();
if (IsEditLineActive()) Chat::setSampCursorMode(2);
const int cursorMode = Chat::getSampCursorMode();
const bool cursorEnabled = cursorMode >= 2 && cursorMode <= 3;
// Stop render
if (Chat::isGTAMenuActive()) return;
if (!cursorEnabled && !IsPopupActive()) return;
if (mSelectedLine > -1)
{
if (ImGui::Begin("##SelectedLine", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground))
{
const auto& position = Chat::getInstance().getChatEntryManager().get(mSelectedLine);
const auto& rect = position.rect;
const auto& charHeight = static_cast<size_t>(Chat::getInstance().pChat->m_nCharHeight);
const auto
xPos = static_cast<float>(rect.x1) - ImGui::CalcTextSize("> ").x,
yPos = static_cast<float>(rect.y1) + std::ceil(static_cast<float>(rect.y2 - rect.y1) / 2.0f) - Chat::getSampFontSize() + (Chat::getSampFontSizeParam() + static_cast<float>(charHeight) / 6);
ImGui::SetWindowPos(ImVec2(xPos, yPos));
ImGui::SetCursorPosX(0.0f);
ImGui::Text(">");
ImGui::End();
}
}
const auto& chat = Chat::getInstance();
if (ImGui::Begin("##PopupWrapper", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoTitleBar))
{
if (this->openPopup && !ImGui::IsPopupOpen("TextContextMenu"))
{
ImGui::OpenPopup("TextContextMenu");
this->openPopup = false;
this->popupActive = true;
} else if (this->popupActive && (!ImGui::IsPopupOpen("TextContextMenu") || mSelectedLine < 0) )
{
ClosePopup();
}
if (ImGui::BeginPopupContextItem("TextContextMenu"))
{
if (chat.mSelectedLine != -1)
{
ImGui::PushItemWidth(350.0f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(12.0f, 12.0f));
ImGui::PushStyleColor(ImGuiCol_PopupBg, ImVec4(0.12f, 0.12f, 0.12f, 0.9f));
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
if (ImGui::MenuItem("Copy"))
{
const bool with_colors = (GetKeyState(VK_SHIFT) & 0x8000) != 0; // Hold shift
std::string text(chat.pChat->m_entry[chat.mSelectedLine].m_szText);
if (!with_colors)
{
for (size_t pos = 0; pos + 7 < text.size();)
{
if (text[pos] == '{' && text[pos + 7] == '}')
{
bool is_hex = true;
for (size_t i = 1; i < 7; ++i)
{
if (!std::isxdigit(static_cast<unsigned char>(text[pos + i])))
{
is_hex = false;
break;
}
}
if (is_hex)
{
text.erase(pos, 8);
continue;
}
}
++pos;
}
}
ImGui::SetClipboardText(Chat::convertToUTF8(text).c_str());
}
if (ImGui::MenuItem(u8"Delete"))
{
Chat::sampDeleteChatLine(chat.mSelectedLine);
}
if (ImGui::MenuItem("Edit"))
{
const auto& pChat = Chat::getInstance().pChat;
editLineBuffer[0] = '\0';
strcat(editLineBuffer, chat.convertToUTF8(pChat->m_entry[chat.mSelectedLine].m_szText).c_str());
editLineColor = ARGBToImVec4(pChat->m_entry[mSelectedLine].m_textColor);
editLineActive = true;
}
if (ImGui::MenuItem("Clear"))
{
Chat::sampDeleteChatLineAll();
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
ImGui::PopStyleColor(2);
}
ImGui::EndPopup();
ImGui::End();
}
}
if (IsEditLineActive() && chat.mSelectedLine > -1)
{
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.12f, 0.12f, 0.12f, 0.9f));
if (ImGui::Begin("##EditLine", &editLineActive, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoBackground))
{
ImGui::SetWindowPos(ImVec2((io.DisplaySize.x - ImGui::GetWindowWidth()) * 0.5f,
(io.DisplaySize.y - ImGui::GetWindowHeight()) * 0.5f));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(12.0f, 12.0f));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(4.0f, 12.0f));
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, ImVec4(0.5f, 0.5f, 0.5f, 1.0f));
ImGui::PushItemWidth(io.DisplaySize.x * (750.0f / 1920));
if (ImGui::ColorButton("ColorButton", editLineColor, ImGuiColorEditFlags_NoTooltip))
{
ImGui::OpenPopup("ColorPickerPopup");
}
if (ImGui::BeginPopup("ColorPickerPopup"))
{
this->colorPopupActive = true;
if (ImGui::ColorPicker3("##picker", (float*)&editLineColor, ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview))
{
auto& pChat = chat.pChat;
pChat->m_entry[mSelectedLine].m_textColor = ImVec4ToARGB(editLineColor);
chat.chatUpdate();
}
ImGui::EndPopup();
} else this->colorPopupActive = false;
ImGui::SameLine(0.0f, 5.0f);
if (ImGui::InputText("##ChatLine", editLineBuffer, IM_ARRAYSIZE(editLineBuffer)))
{
if (chat.mSelectedLine > -1)
{
const auto& pChat = chat.pChat;
pChat->m_entry[chat.mSelectedLine].m_szText[0] = '\0';
const std::string text(Chat::convertFromUTF8(editLineBuffer));
strcat(pChat->m_entry[chat.mSelectedLine].m_szText, text.c_str());
chat.chatUpdate();
}
}
// Input outline
ImVec2 inputPos = ImGui::GetItemRectMin();
ImVec2 inputSize = ImGui::GetItemRectSize();
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddRect(inputPos, ImVec2(inputPos.x + inputSize.x, inputPos.y + inputSize.y), IM_COL32(255, 255, 255, 255));
ImGui::PopItemWidth();
ImGui::PopStyleVar(2);
ImGui::PopStyleColor();
ImGui::End();
}
ImGui::PopStyleColor();
}
}
void Menu::RebuildFonts()
{
std::string fontPath(256, '\0');
if (SHGetSpecialFolderPathA(Chat::getGameHWND(), fontPath.data(), CSIDL_FONTS, false))
{
ImGui_ImplDX9_InvalidateDeviceObjects();
fontPath.resize(fontPath.find('\0'));
fontPath += "\\";
std::string fontFileName{ Chat::getFontRelativePathByName(Chat::getSampFontName()) };
if (fontFileName.empty() || fontFileName.find("arial") != std::string::npos) fontFileName = "ArialBd.ttf";
fontPath += fontFileName;
auto& io = ImGui::GetIO();
ImVector<ImWchar> ranges;
ImFontGlyphRangesBuilder builder;
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
builder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
builder.AddText(u8"‚„…†‡ˆ‰‹‘’“”•–—™›¹");
builder.BuildRanges(&ranges);
ImFontConfig cfg;
cfg.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_LightHinting | ImGuiFreeTypeBuilderFlags_ForceAutoHint;
io.Fonts->Clear();
io.Fonts->AddFontFromFileTTF(fontPath.c_str(), Chat::getSampFontSize() - 2, &cfg, ranges.Data);
io.Fonts->Build();
ImGui::GetStyle().ItemSpacing.y = 2;
ImGui_ImplDX9_CreateDeviceObjects();
}
}
void Menu::ShowPopup()
{
this->openPopup = true;
}
void Menu::ClosePopup()
{
ImGui::CloseCurrentPopup();
this->popupActive = false;
}
bool Menu::IsPopupActive() const
{
return popupActive;
}
bool Menu::IsEditLineActive() const
{
return editLineActive;
}
bool Menu::IsColorPopupActive() const
{
return this->colorPopupActive;
}
void Menu::CloseEditLine()
{
this->editLineActive = false;
Chat::setSampCursorMode(0);
Chat::getInstance().mSelectedLine = -1;
}