Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vehicle Spawner + Fix 3 Bugs that Caused the Menu to be Unusable #78

Merged
merged 10 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions src/core/frontend/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#include "core/logger/LogHelper.hpp"
#include "game/backend/FiberPool.hpp" // TODO: game import in core

#include <mutex>

namespace YimMenu
{

Notification Notifications::ShowImpl(std::string title, std::string message, NotificationType type, int duration, std::function<void()> context_function, std::string context_function_name)
{
if (title.empty() || message.empty())
Expand All @@ -30,13 +33,15 @@ namespace YimMenu
notification.m_context_function_name = context_function_name.empty() ? "Context Function" : context_function_name;
}

std::lock_guard<std::mutex> lock(m_mutex);
auto result = m_Notifications.insert(std::make_pair(title + message, notification));

return notification;
}

bool Notifications::EraseImpl(Notification notification)
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto& [id, n] : m_Notifications)
{
if (id == notification.GetIdentifier())
Expand Down Expand Up @@ -112,36 +117,45 @@ namespace YimMenu

void Notifications::DrawImpl()
{
int position = 0;

for (auto& [id, notification] : m_Notifications)
std::vector<std::string> keys_to_erase;
{
DrawNotification(notification, position);

if (!notification.erasing)
{
if (notification.m_AnimationOffset < 0)
notification.m_AnimationOffset += m_CardAnimationSpeed;
std::lock_guard<std::mutex> lock(m_mutex);
int position = 0;

//Need this to account for changes in card size (x dimension), custom increments might result in odd numbers
if (notification.m_AnimationOffset > 0)
notification.m_AnimationOffset = 0.f;
}
else
for (auto& [id, notification] : m_Notifications)
{
notification.m_AnimationOffset -= m_CardAnimationSpeed;
if (notification.m_AnimationOffset <= -m_CardSizeX)
m_Notifications.erase(id);
DrawNotification(notification, position);

if (!notification.erasing)
{
if (notification.m_AnimationOffset < 0)
notification.m_AnimationOffset += m_CardAnimationSpeed;

//Need this to account for changes in card size (x dimension), custom increments might result in odd numbers
if (notification.m_AnimationOffset > 0)
notification.m_AnimationOffset = 0.f;
}
else
{
notification.m_AnimationOffset -= m_CardAnimationSpeed;
if (notification.m_AnimationOffset <= -m_CardSizeX)
keys_to_erase.push_back(id);
}


if ((float)std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - notification.m_created_on)
.count()
>= notification.m_Duration)
keys_to_erase.push_back(id);

position++;
}


if ((float)std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - notification.m_created_on)
.count()
>= notification.m_Duration)
Erase(notification);

position++;
}
std::lock_guard<std::mutex> lock(m_mutex);
for (const auto& key : keys_to_erase)
{
m_Notifications.erase(key);
}
}
}
18 changes: 9 additions & 9 deletions src/core/frontend/Notifications.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace YimMenu
{
static inline float m_CardSizeX = 350.f;
static inline float m_CardSizeY = 100.f;
static inline float m_CardSizeX = 350.f;
static inline float m_CardSizeY = 100.f;
static inline float m_CardAnimationSpeed = 50.f;

enum class NotificationType
Expand All @@ -19,23 +19,24 @@ namespace YimMenu
NotificationType m_Type;
std::string m_Title;
std::string m_Message;
std::chrono::time_point<std::chrono::system_clock> m_created_on;
std::chrono::time_point<std::chrono::system_clock> m_created_on;
int m_Duration;
std::function<void()> m_context_function;
std::string m_context_function_name;
float m_AnimationOffset = -m_CardSizeX;
bool erasing = false;
bool erasing = false;

std::string GetIdentifier()
{
return std::string(m_Title).append(m_Message);
}
};

class Notifications
{
class Notifications
{
private:
std::unordered_map<std::string, Notification> m_Notifications = {};
std::mutex m_mutex;

// duration is in milliseconds
Notification ShowImpl(std::string title, std::string message, NotificationType type, int duration, std::function<void()> context_function, std::string context_function_name);
Expand All @@ -49,7 +50,7 @@ namespace YimMenu
}

public:
static Notification Show(std::string title, std::string message, NotificationType type = NotificationType::Info, int duration = 5000 ,std::function<void()> context_function = nullptr, std::string context_function_name = "")
static Notification Show(std::string title, std::string message, NotificationType type = NotificationType::Info, int duration = 5000, std::function<void()> context_function = nullptr, std::string context_function_name = "")
{
return GetInstance().ShowImpl(title, message, type, duration, context_function, context_function_name);
}
Expand All @@ -63,7 +64,6 @@ namespace YimMenu
{
return GetInstance().EraseImpl(notification);
}

};
};

}
6 changes: 6 additions & 0 deletions src/core/settings/Settings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Settings.hpp"

#include "IStateSerializer.hpp"
#include "Settings.hpp"


namespace YimMenu
{
Settings::Settings() :
Expand All @@ -16,7 +18,10 @@ namespace YimMenu
m_SettingsFile = settingsFile;

if (!settingsFile.Exists())
{
Reset();
return;
}

std::ifstream file(m_SettingsFile);

Expand All @@ -29,6 +34,7 @@ namespace YimMenu
{
LOG(WARNING) << "Detected corrupt settings, resetting settings...";
Reset();
return;
}

for (auto& serializer : m_StateSerializers)
Expand Down
5 changes: 5 additions & 0 deletions src/core/settings/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace YimMenu
return GetInstance().m_InitialLoadDone;
}

static std::string GetFilePath()
{
return GetInstance().m_SettingsFile.string();
}

private:
static Settings& GetInstance()
{
Expand Down
26 changes: 26 additions & 0 deletions src/game/features/self/SpawnWagon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "core/commands/Command.hpp"
#include "core/frontend/Notifications.hpp"
#include "game/features/Features.hpp"
#include "util/VehicleSpawner.cpp"


namespace YimMenu::Features
{
class SpawnWagon : public Command
{
using Command::Command;

virtual void OnCall() override
{
if (SpawnVehicle("wagonarmoured01x", Self::PlayerPed))
{
Notifications::Show("Vehicle Spawner", "Wagon Spawned!", NotificationType::Success);
return;
}
Notifications::Show("Vehicle Spawner", "Failed to spawn vehicle!", NotificationType::Error);
return;
}
};

static SpawnWagon _SpawnWagon{"spawnwagon", "Spawn Wagon", "Spawns the bounty hunter wagon into the game."};
}
38 changes: 20 additions & 18 deletions src/game/frontend/Menu.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#include "Menu.hpp"
#include "core/renderer/Renderer.hpp"
#include "game/backend/ScriptMgr.hpp"
#include "game/backend/FiberPool.hpp"

#include "core/commands/Commands.hpp"
#include "core/frontend/manager/UIManager.hpp"
#include "core/renderer/Renderer.hpp"
#include "game/backend/FiberPool.hpp"
#include "game/backend/ScriptMgr.hpp"
#include "game/frontend/fonts/Fonts.hpp"
#include "game/pointers/Pointers.hpp"

#include "submenus/Self.hpp"
#include "submenus/Teleport.hpp"
#include "submenus/Debug.hpp"
#include "submenus/Network.hpp"
#include "submenus/Players.hpp"
#include "submenus/Self.hpp"
#include "submenus/Settings.hpp"
#include "submenus/Network.hpp"
#include "submenus/Teleport.hpp"


namespace YimMenu
{
Expand All @@ -24,14 +25,15 @@ namespace YimMenu
UIManager::AddSubmenu(std::make_shared<Submenus::Network>());
UIManager::AddSubmenu(std::make_shared<Submenus::Players>());
UIManager::AddSubmenu(std::make_shared<Submenus::Settings>());
// Wierd glitch causes menu to crash when clicking debug
UIManager::AddSubmenu(std::make_shared<Submenus::Debug>());

Renderer::AddRendererCallBack(
[&] {
if (!GUI::IsOpen())
return;

ImGui::PushFont(Menu::Font::g_DefaultFont);
ImGui::PushFont(Menu::Font::g_DefaultFont);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImU32(ImColor(15, 15, 15)));

// Think this add HTML&PHP with no CSS. Lol just for testing.
Expand All @@ -41,27 +43,27 @@ namespace YimMenu
//ImGui::BeginDisabled(*Pointers.IsSessionStarted);
if (ImGui::Button("Unload", ImVec2(120, 0)))
{
if (ScriptMgr::CanTick())
{
if (ScriptMgr::CanTick())
{
FiberPool::Push([] {
Commands::Shutdown();
g_Running = false;
});
}
else
{
}
else
{
g_Running = false;
}
}
}
//ImGui::EndDisabled();

UIManager::Draw();
UIManager::Draw();

ImGui::End();
}

ImGui::PopStyleColor();
ImGui::PopFont();
ImGui::PopStyleColor();
ImGui::PopFont();
},
-1);
}
Expand Down
Loading
Loading