From e56f5b02c4a115b1f0aad97dd7d82f6a105222cf Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Fri, 13 Aug 2021 23:45:56 +0600 Subject: [PATCH] Cleanup --- .vscode/c_cpp_properties.json | 10 +-- src/Paint.h | 2 +- src/Ped.cpp | 6 +- src/Ped.h | 2 +- src/Player.cpp | 6 +- src/Player.h | 2 +- src/ResourceStore.cpp | 15 ++++- src/ResourceStore.h | 4 +- src/Ui.cpp | 115 ++++++++++++++++++---------------- src/Ui.h | 4 +- src/Vehicle.cpp | 9 +-- src/Vehicle.h | 4 +- src/Weapon.cpp | 6 +- src/Weapon.h | 4 ++ 14 files changed, 102 insertions(+), 87 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 9bf22bb..aa011ad 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,10 +6,10 @@ "${workspaceFolder}/**", "${PLUGIN_SDK_DIR}/*", "${DIRECTX9_SDK_DIR}/Include/*", - // "${PLUGIN_SDK_DIR}/plugin_sa/*", - // "${PLUGIN_SDK_DIR}/plugin_sa/game_sa/*", - "${PLUGIN_SDK_DIR}/plugin_vc/*", - "${PLUGIN_SDK_DIR}/plugin_vc/game_vc/*", + "${PLUGIN_SDK_DIR}/plugin_sa/*", + "${PLUGIN_SDK_DIR}/plugin_sa/game_sa/*", + // "${PLUGIN_SDK_DIR}/plugin_vc/*", + // "${PLUGIN_SDK_DIR}/plugin_vc/game_vc/*", "${PLUGIN_SDK_DIR}/shared/*", "${PLUGIN_SDK_DIR}/shared/game/*", "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.19041.0\\ucrt\\*", @@ -20,7 +20,7 @@ "IS_PLATFORM_WIN", "_CRT_SECURE_NO_WARNINGS", "_CRT_NON_CONFORMING_SWPRINTFS", - "GTAVC", + "GTASA", "_DX9_SDK_INSTALLED", "PLUGIN_SGV_10US" ], diff --git a/src/Paint.h b/src/Paint.h index afa497d..4d4fee8 100644 --- a/src/Paint.h +++ b/src/Paint.h @@ -73,7 +73,7 @@ private: inline static VehicleExtendedData m_VehData; protected: - inline static ResourceStore m_TextureData { "textures" }; + inline static ResourceStore m_TextureData { "textures", eResourceType::TYPE_IMAGE, ImVec2(100, 80) }; struct veh_nodes { diff --git a/src/Ped.cpp b/src/Ped.cpp index 2d3ce83..b7dd366 100644 --- a/src/Ped.cpp +++ b/src/Ped.cpp @@ -260,8 +260,7 @@ void Ped::Draw() { ImGui::Spacing(); #ifdef GTASA - Ui::DrawImages(m_PedData.m_ImagesList, ImVec2(65, 110), m_PedData.m_Categories, m_PedData.m_Selected, - m_PedData.m_Filter, SpawnPed, nullptr, + Ui::DrawImages(m_PedData, SpawnPed, nullptr, [](std::string str) { return m_PedData.m_pJson->m_Data[str].get(); }); #elif GTAVC Ui::DrawJSON(m_PedData, SpawnPed, nullptr); @@ -295,8 +294,7 @@ void Ped::Draw() Weapon::m_WeaponData.m_pJson->m_Data[std::to_string(m_SpawnPed::m_nWeaponId)].get().c_str()); ImGui::Spacing(); - Ui::DrawImages(Weapon::m_WeaponData.m_ImagesList, ImVec2(65, 65), Weapon::m_WeaponData.m_Categories, - Weapon::m_WeaponData.m_Selected, Weapon::m_WeaponData.m_Filter, + Ui::DrawImages(Weapon::m_WeaponData, [](std::string str) { m_SpawnPed::m_nWeaponId = std::stoi(str); }, nullptr, [](std::string str) diff --git a/src/Ped.h b/src/Ped.h index 9697413..6ac16b0 100644 --- a/src/Ped.h +++ b/src/Ped.h @@ -11,7 +11,7 @@ private: inline static CJson m_SpecialPedJson = CJson("ped special"); #endif - inline static ResourceStore m_PedData{"ped", eResourceType::TYPE_TEXT}; + inline static ResourceStore m_PedData{"ped", eResourceType::TYPE_TEXT, ImVec2(65, 110)}; inline static bool m_bImagesLoaded; inline static bool m_bExGangWarsInstalled; inline static int m_nPedRemoveRadius = 5; diff --git a/src/Player.cpp b/src/Player.cpp index fdb3743..6e4177b 100644 --- a/src/Player.cpp +++ b/src/Player.cpp @@ -534,8 +534,7 @@ void Player::Draw() { if (bClothOption == 0) { - Ui::DrawImages(m_ClothData.m_ImagesList, ImVec2(70, 100), m_ClothData.m_Categories, m_ClothData.m_Selected, - m_ClothData.m_Filter, ChangePlayerCloth, nullptr, + Ui::DrawImages(m_ClothData, ChangePlayerCloth, nullptr, [](std::string str) { std::stringstream ss(str); @@ -596,8 +595,7 @@ void Player::Draw() } if (ImGui::BeginTabItem("Ped skins")) { - Ui::DrawImages(Ped::m_PedData.m_ImagesList, ImVec2(65, 110), Ped::m_PedData.m_Categories, - Ped::m_PedData.m_Selected, Ped::m_PedData.m_Filter, ChangePlayerModel, nullptr, + Ui::DrawImages(Ped::m_PedData, ChangePlayerModel, nullptr, [](std::string str) { return Ped::m_PedData.m_pJson->m_Data[str].get(); }); ImGui::EndTabItem(); } diff --git a/src/Player.h b/src/Player.h index 422d8b4..f154f5a 100644 --- a/src/Player.h +++ b/src/Player.h @@ -15,7 +15,7 @@ private: #ifdef GTASA inline static bool m_bAimSkinChanger; inline static int m_nUiBodyState; - inline static ResourceStore m_ClothData { "clothes" }; + inline static ResourceStore m_ClothData { "clothes" , eResourceType::TYPE_IMAGE, ImVec2(70, 100)}; struct m_CustomSkins { inline static std::string m_Path = paths::GetGameDirPathA() + std::string("\\modloader\\Custom Skins\\");; diff --git a/src/ResourceStore.cpp b/src/ResourceStore.cpp index a8aef6b..23d808f 100644 --- a/src/ResourceStore.cpp +++ b/src/ResourceStore.cpp @@ -2,10 +2,15 @@ #include "CFileLoader.h" #include "extensions/Paths.h" -ResourceStore::ResourceStore(const char* text, eResourceType type) +ResourceStore::ResourceStore(const char* text, eResourceType type, ImVec2 imageSize) +: m_ImageSize(imageSize) { - if (type == eResourceType::TYPE_IMAGE) + if (type != eResourceType::TYPE_TEXT) { + /* + Textures need to be loaded from main thread + Loading it directly here doesn't work + */ Events::processScriptsEvent += [text, this]() { if (!m_bTexturesLoaded) @@ -14,9 +19,15 @@ ResourceStore::ResourceStore(const char* text, eResourceType type) m_bTexturesLoaded = true; } }; + + if (type == eResourceType::TYPE_BOTH) + { + goto loadJson; + } } else { + loadJson: m_pJson = std::make_unique(text); // Generate categories diff --git a/src/ResourceStore.h b/src/ResourceStore.h index a3623b2..847f256 100644 --- a/src/ResourceStore.h +++ b/src/ResourceStore.h @@ -40,6 +40,7 @@ enum eResourceType { TYPE_IMAGE, TYPE_TEXT, + TYPE_BOTH, }; using TextureResourceList = std::vector>; @@ -54,7 +55,8 @@ public: std::string m_Selected = "All"; std::unique_ptr m_pJson; TextureResourceList m_ImagesList; + ImVec2 m_ImageSize; bool m_bTexturesLoaded = false; - ResourceStore(const char* text, eResourceType type = TYPE_IMAGE); + ResourceStore(const char* text, eResourceType type = TYPE_IMAGE, ImVec2 imageSize = ImVec2(64, 64)); }; \ No newline at end of file diff --git a/src/Ui.cpp b/src/Ui.cpp index ed904c5..ad1576c 100644 --- a/src/Ui.cpp +++ b/src/Ui.cpp @@ -42,10 +42,10 @@ bool Ui::ListBoxStr(const char* label, std::vector& all_items, std: return rtn; } -bool Ui::ListBoxCustomNames(const char* label, std::vector& all_items, std::string& selected, const char* custom_names[], size_t length) +bool Ui::ListBoxCustomNames(const char* label, std::vector& all_items, std::string& selected, const char* customNames[], size_t length) { bool rtn = false; - std::string display_selected = (selected == "All") ? selected : custom_names[std::stoi(selected)]; + std::string display_selected = (selected == "All") ? selected : customNames[std::stoi(selected)]; if (ImGui::BeginCombo(label, display_selected.c_str())) { @@ -57,7 +57,7 @@ bool Ui::ListBoxCustomNames(const char* label, std::vector& all_ite for (size_t i = 0; i < length; ++i) { - if (ImGui::MenuItem(custom_names[i])) + if (ImGui::MenuItem(customNames[i])) { selected = std::to_string(i); rtn = true; @@ -183,7 +183,7 @@ bool Ui::CheckboxWithHint(const char* label, bool* v, const char* hint, bool is_ // set things up bool pressed = false; const ImGuiStyle& style = ImGui::GetStyle(); - const ImVec2 text_size = ImGui::CalcTextSize(label, nullptr, true); + const ImVec2 textSize = ImGui::CalcTextSize(label, nullptr, true); float square_sz = ImGui::GetFrameHeight(); ImDrawList* drawlist = ImGui::GetWindowDrawList(); ImU32 color = ImGui::GetColorU32(ImGuiCol_FrameBg); @@ -482,74 +482,79 @@ void Ui::FilterWithHint(const char* label, ImGuiTextFilter& filter, const char* } } -// clean up the code someday -void Ui::DrawImages(std::vector>& img_vec, ImVec2 image_size, - std::vector& category_vec, std::string& selected_item, ImGuiTextFilter& filter, - std::function on_left_click, std::function on_right_click, - std::function get_name_func, - std::function verify_func, const char** custom_names, size_t length) +void Ui::DrawImages(ResourceStore &store, std::function onLeftClick, std::function onRightClick, + std::function getName, std::function verifyFunc, + const char** customNames, size_t length) { - // scale image size - image_size.x *= screen::GetScreenWidth() / 1366.0f; - image_size.y *= screen::GetScreenHeight() / 768.0f; + /* + Trying to scale images based on resolutions + Native 1366x768 + */ + store.m_ImageSize.x *= screen::GetScreenWidth() / 1366.0f; + store.m_ImageSize.y *= screen::GetScreenHeight() / 768.0f; - int images_in_row = static_cast(ImGui::GetWindowContentRegionWidth() / image_size.x); - image_size.x = ImGui::GetWindowContentRegionWidth() / images_in_row - static_cast(ImGuiStyleVar_ItemSpacing) * - 0.65f; - - int images_count = 1; + int imageCount = 1; + int imagesInRow = static_cast(ImGui::GetWindowContentRegionWidth() / store.m_ImageSize.x); + store.m_ImageSize.x = ImGui::GetWindowContentRegionWidth() / imagesInRow - static_cast(ImGuiStyleVar_ItemSpacing) * 0.65f; ImGui::Spacing(); + // Hide the popup if right clicked again if (ImGui::IsMouseClicked(1)) + { imgPopup.function = nullptr; + } ImGui::PushItemWidth(ImGui::GetContentRegionAvailWidth() / 2 - 5); - if (custom_names) + if (customNames) { - ListBoxCustomNames("##Categories", category_vec, selected_item, custom_names, length); + ListBoxCustomNames("##Categories", store.m_Categories, store.m_Selected, customNames, length); } else { - ListBoxStr("##Categories", category_vec, selected_item); + ListBoxStr("##Categories", store.m_Categories, store.m_Selected); } ImGui::SameLine(); - FilterWithHint("##Filter", filter, "Search"); + FilterWithHint("##Filter", store.m_Filter, "Search"); ImGui::Spacing(); ImGui::BeginChild("DrawImages"); - for (uint i = 0; i < img_vec.size(); i++) + for (uint i = 0; i < store.m_ImagesList.size(); ++i) { - std::string text = img_vec[i]->m_FileName; - std::string model_name = get_name_func(text); + std::string text = store.m_ImagesList[i]->m_FileName; + std::string modelName = getName(text); - if (filter.PassFilter(model_name.c_str()) - && (img_vec[i]->m_CategoryName == selected_item || selected_item == "All") - && (verify_func == nullptr || verify_func(text)) + if (store.m_Filter.PassFilter(modelName.c_str()) + && (store.m_ImagesList[i]->m_CategoryName == store.m_Selected || store.m_Selected == "All") + && (verifyFunc == nullptr || verifyFunc(text)) ) { + /* + Couldn't figure out how to laod images for Dx11 + Using texts for now + */ if (Globals::renderer == Render_DirectX11) { - if (ImGui::MenuItem(model_name.c_str())) + if (ImGui::MenuItem(modelName.c_str())) { - on_left_click(text); + onLeftClick(text); } } else { - if (ImGui::ImageButton(img_vec[i]->m_pTexture, image_size, ImVec2(0, 0), ImVec2(1, 1), 1, ImVec4(1, 1, 1, 1), ImVec4(1, 1, 1, 1))) + if (ImGui::ImageButton(store.m_ImagesList[i]->m_pTexture, store.m_ImageSize, ImVec2(0, 0), ImVec2(1, 1), 1, ImVec4(1, 1, 1, 1), ImVec4(1, 1, 1, 1))) { - on_left_click(text); + onLeftClick(text); } } - - if (ImGui::IsItemClicked(1) && on_right_click != nullptr) + // Right click popup + if (ImGui::IsItemClicked(1) && onRightClick != nullptr) { - imgPopup.function = on_right_click; - imgPopup.value = model_name; + imgPopup.function = onRightClick; + imgPopup.value = modelName; } if (Globals::renderer != Render_DirectX11) @@ -558,45 +563,46 @@ void Ui::DrawImages(std::vector>& img_vec, ImV { ImDrawList* drawlist = ImGui::GetWindowDrawList(); - ImVec2 btn_min = ImGui::GetItemRectMin(); - ImVec2 btn_max = ImGui::GetItemRectMax(); + // Drawing selected overlay + ImVec2 btnMin = ImGui::GetItemRectMin(); + ImVec2 btnMax = ImGui::GetItemRectMax(); + drawlist->AddRectFilled(btnMin, btnMax, ImGui::GetColorU32(ImGuiCol_ModalWindowDimBg)); - drawlist->AddRectFilled(btn_min, btn_max, ImGui::GetColorU32(ImGuiCol_ModalWindowDimBg)); - - ImVec2 text_size = ImGui::CalcTextSize(model_name.c_str()); - if (text_size.x < image_size.x) + // Calculating and drawing text over the image + ImVec2 textSize = ImGui::CalcTextSize(modelName.c_str()); + if (textSize.x < store.m_ImageSize.x) { - float offsetX = (ImGui::GetItemRectSize().x - text_size.x) / 2; - drawlist->AddText(ImVec2(btn_min.x + offsetX, btn_min.y + 10), ImGui::GetColorU32(ImGuiCol_Text), - model_name.c_str()); + float offsetX = (ImGui::GetItemRectSize().x - textSize.x) / 2; + drawlist->AddText(ImVec2(btnMin.x + offsetX, btnMin.y + 10), ImGui::GetColorU32(ImGuiCol_Text), + modelName.c_str()); } else { std::string buff = ""; - - std::stringstream ss(model_name); + std::stringstream ss(modelName); short count = 1; while (ss >> buff) { - text_size = ImGui::CalcTextSize(buff.c_str()); - float offsetX = (ImGui::GetItemRectSize().x - text_size.x) / 2; - drawlist->AddText(ImVec2(btn_min.x + offsetX, btn_min.y + 10 * count), + textSize = ImGui::CalcTextSize(buff.c_str()); + float offsetX = (ImGui::GetItemRectSize().x - textSize.x) / 2; + drawlist->AddText(ImVec2(btnMin.x + offsetX, btnMin.y + 10 * count), ImGui::GetColorU32(ImGuiCol_Text), buff.c_str()); ++count; } } } - if (images_count % images_in_row != 0) + if (imageCount % imagesInRow != 0) { ImGui::SameLine(0.0, ImGui::GetStyle().ItemInnerSpacing.x); } } - images_count++; + imageCount++; } } + // Draw popup code if (imgPopup.function != nullptr) { if (ImGui::BeginPopupContextWindow()) @@ -604,11 +610,14 @@ void Ui::DrawImages(std::vector>& img_vec, ImV ImGui::Text(imgPopup.value.c_str()); ImGui::Separator(); if (ImGui::MenuItem("Remove")) + { imgPopup.function(imgPopup.value); - + } if (ImGui::MenuItem("Close")) + { imgPopup.function = nullptr; + } ImGui::EndPopup(); } diff --git a/src/Ui.h b/src/Ui.h index 68c2367..c1a094e 100644 --- a/src/Ui.h +++ b/src/Ui.h @@ -51,9 +51,7 @@ public: static void DrawJSON(ResourceStore& data, std::function func_left_click, std::function func_right_click); - static void DrawImages(std::vector>& img_vec, ImVec2 image_size, - std::vector& category_vec, std::string& selected_item, ImGuiTextFilter& filter, - std::function on_left_click, + static void DrawImages(ResourceStore &store, std::function on_left_click, std::function on_right_click, std::function get_name_func, std::function verify_func = nullptr, diff --git a/src/Vehicle.cpp b/src/Vehicle.cpp index 1a6c3be..68640ad 100644 --- a/src/Vehicle.cpp +++ b/src/Vehicle.cpp @@ -1106,8 +1106,7 @@ void Vehicle::Draw() ImGui::SetNextItemWidth(ImGui::GetWindowContentRegionWidth() - 2.5); ImGui::InputTextWithHint("##LicenseText", "License plate text", m_Spawner::m_nLicenseText, 9); - Ui::DrawImages(m_Spawner::m_VehData.m_ImagesList, ImVec2(100, 75), m_Spawner::m_VehData.m_Categories, - m_Spawner::m_VehData.m_Selected, m_Spawner::m_VehData.m_Filter, SpawnVehicle, nullptr, + Ui::DrawImages(m_Spawner::m_VehData, SpawnVehicle, nullptr, [](std::string str) { return GetNameFromModel(std::stoi(str)); @@ -1320,8 +1319,7 @@ void Vehicle::Draw() ImGui::SameLine(); ImGui::Checkbox("Material filter", &m_Color::m_bMatFilter); ImGui::Spacing(); - Ui::DrawImages(m_TextureData.m_ImagesList, ImVec2(100, 80), m_TextureData.m_Categories, m_TextureData.m_Selected, - m_TextureData.m_Filter, + Ui::DrawImages(m_TextureData, [](std::string& str) { Paint::SetNodeTexture(FindPlayerPed()->m_pVehicle, Paint::veh_nodes::selected, str, @@ -1339,8 +1337,7 @@ void Vehicle::Draw() if (ImGui::BeginTabItem("Tune")) { ImGui::Spacing(); - Ui::DrawImages(m_TuneData.m_ImagesList, ImVec2(100, 80), m_TuneData.m_Categories, m_TuneData.m_Selected, - m_TuneData.m_Filter, + Ui::DrawImages(m_TuneData, [](std::string& str) { AddComponent(str); }, [](std::string& str) { RemoveComponent(str); }, [](std::string& str) { return str; }, diff --git a/src/Vehicle.h b/src/Vehicle.h index 5dc3873..3facb4f 100644 --- a/src/Vehicle.h +++ b/src/Vehicle.h @@ -41,13 +41,13 @@ private: inline static bool m_bApplyOnTraffic; inline static uint m_bTrafficTimer; }; - inline static ResourceStore m_TuneData { "components" }; + inline static ResourceStore m_TuneData { "components", eResourceType::TYPE_IMAGE, ImVec2(100, 80) }; #endif struct m_Spawner { #ifdef GTASA - inline static ResourceStore m_VehData { "vehicles" }; + inline static ResourceStore m_VehData { "vehicles", eResourceType::TYPE_IMAGE, ImVec2(100, 75)}; #elif GTAVC inline static ResourceStore m_VehData{"vehicle", eResourceType::TYPE_TEXT}; #endif diff --git a/src/Weapon.cpp b/src/Weapon.cpp index eea9202..999ccdc 100644 --- a/src/Weapon.cpp +++ b/src/Weapon.cpp @@ -230,8 +230,7 @@ void Weapon::Draw() m_WeaponData.m_pJson->m_Data[std::to_string(m_nGangWeaponList[m_nSelectedGang][m_nSelectedWeapon])].get< std::string>().c_str()); ImGui::Spacing(); - Ui::DrawImages(m_WeaponData.m_ImagesList, ImVec2(65, 65), m_WeaponData.m_Categories, m_WeaponData.m_Selected, - m_WeaponData.m_Filter, SetGangWeapon, nullptr, + Ui::DrawImages(m_WeaponData, SetGangWeapon, nullptr, [](std::string str) { return m_WeaponData.m_pJson->m_Data[str].get(); }, [](std::string str) { return str != "-1"; /*Jetpack*/ } ); @@ -245,8 +244,7 @@ void Weapon::Draw() m_nAmmoCount = (m_nAmmoCount < 0) ? 0 : m_nAmmoCount; m_nAmmoCount = (m_nAmmoCount > 99999) ? 99999 : m_nAmmoCount; } - Ui::DrawImages(m_WeaponData.m_ImagesList, ImVec2(65, 65), m_WeaponData.m_Categories, m_WeaponData.m_Selected, - m_WeaponData.m_Filter, GiveWeaponToPlayer, nullptr, + Ui::DrawImages(m_WeaponData, GiveWeaponToPlayer, nullptr, [](std::string str) { return m_WeaponData.m_pJson->m_Data[str].get(); }, [](std::string str) { return str != "0"; /*Unarmed*/ } ); diff --git a/src/Weapon.h b/src/Weapon.h index 05a605c..285e114 100644 --- a/src/Weapon.h +++ b/src/Weapon.h @@ -3,7 +3,11 @@ class Weapon { public: +#ifdef GTASA + inline static ResourceStore m_WeaponData{ "weapon", eResourceType::TYPE_BOTH, ImVec2(65, 65) }; +#elif GTAVC inline static ResourceStore m_WeaponData{ "weapon", eResourceType::TYPE_TEXT }; +#endif inline static bool m_bAutoAim; inline static bool m_bFastReload; inline static bool m_bHugeDamage;