Skip to content

Commit

Permalink
Add Overlay Binding Notification on Game Start
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonzkiller committed Dec 14, 2024
1 parent c8d9774 commit 1d02ce7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/overlay/Overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void Overlay::PostInitialize()
}

m_initialized = true;

m_startTime = std::chrono::steady_clock::now();
}
}

Expand Down Expand Up @@ -195,12 +197,40 @@ void Overlay::Update()
}
}

// Display binding in bottom right corner for 5 seconds
if (!Bindings::IsFirstTimeSetup() && !m_bindHintShown)
{
const auto currentTime = std::chrono::steady_clock::now();
if ((currentTime - m_startTime) < std::chrono::seconds(5))
{
if (m_notificationString.empty())
{
auto overlayBindCode = CET::Get().GetBindings().GetBindCodeForModBind(Bindings::GetOverlayToggleModBind());

m_notificationString = "CET Overlay Bind: ";
m_notificationString += VKBindings::GetBindString(overlayBindCode);
}

m_drawNotification = true;
}
else
{
m_bindHintShown = true;
m_drawNotification = false;
m_notificationString.clear();
}
}

if (m_drawNotification)
DrawNotification();

if (!m_enabled)
return;

const auto [width, height] = CET::Get().GetD3D12().GetResolution();
const auto heightLimit = 2 * ImGui::GetFrameHeight() + 2 * ImGui::GetStyle().WindowPadding.y;
ImGui::SetNextWindowPos({width * 0.25f, height * 0.05f}, ImGuiCond_FirstUseEver);
ImGui::SetNextWindowBgAlpha(1.0);
ImGui::SetNextWindowPos({width * 0.25f, height * 0.05f});
ImGui::SetNextWindowSizeConstraints({width * 0.5f, heightLimit}, {FLT_MAX, heightLimit});
if (ImGui::Begin("Cyber Engine Tweaks"))
DrawToolbar();
Expand Down Expand Up @@ -346,3 +376,34 @@ void Overlay::DrawToolbar()
if (ImGui::Button("Reload all mods", ImVec2(itemWidth, 0)))
m_vm.ReloadAllMods();
}

// TODO: Multiple notifications?
void Overlay::DrawNotification()
{
// ID must be unique for different messages, so why not use the FNV1a hash?
const auto imGuiID = RED4ext::FNV1a32(m_notificationString.c_str());
const auto [width, height] = CET::Get().GetD3D12().GetResolution();

const auto notificationWidth = ImGui::CalcTextSize(m_notificationString.c_str()).x + 2 * ImGui::GetStyle().WindowPadding.x;
const auto notificationHeight = ImGui::CalcTextSize(m_notificationString.c_str()).y + 2 * ImGui::GetStyle().WindowPadding.y;

ImGui::SetNextWindowBgAlpha(0.5);
ImGui::SetNextWindowPos({ImGui::GetStyle().WindowPadding.x, height - (notificationHeight + ImGui::GetStyle().WindowPadding.y)});
ImGui::SetNextWindowSize({notificationWidth, notificationHeight});
if (ImGui::Begin(std::format("##{}", imGuiID).c_str(), nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoSavedSettings))
{
ImGui::TextUnformatted(m_notificationString.c_str());
}
ImGui::End();
}

void Overlay::ShowNotification(const std::string& notification)
{
m_notificationString = notification;
m_drawNotification = true;
}

void Overlay::HideNotification()
{
m_drawNotification = false;
}
9 changes: 9 additions & 0 deletions src/overlay/Overlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ struct Overlay

void Update();

void ShowNotification(const std::string& notification);
void HideNotification();

protected:
void Hook();

static BOOL ClipToCenter(RED4ext::CGameEngine::UnkD0* apThis);

private:
void DrawToolbar();
void DrawNotification();

Console m_console;
Bindings m_bindings;
Expand All @@ -47,8 +51,13 @@ struct Overlay
std::atomic_bool m_enabled{false};
std::atomic_bool m_toggled{false};
bool m_initialized{false};
std::atomic_bool m_drawNotification{false};
std::chrono::time_point<std::chrono::steady_clock> m_startTime;
bool m_bindHintShown{false};

Options& m_options;
PersistentState& m_persistentState;
LuaVM& m_vm;

std::string m_notificationString;
};

0 comments on commit 1d02ce7

Please sign in to comment.