Added the possibility to create new fonts.

Fonts cannot be created after a NewFrame call.
merge-requests/28/head
Nemirtingas 2019-09-05 09:00:02 +02:00
parent e82579b82c
commit 6c09635600
8 changed files with 70 additions and 50 deletions

View File

@ -100,11 +100,11 @@ void OpenGLX_Hook::prepareForOverlay(Display* display, GLXDrawable drawable)
context = glXCreateContext(display, visual_info, nullptr, True); context = glXCreateContext(display, visual_info, nullptr, True);
this->display = display; this->display = display;
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
ImGuiIO& io = ImGui::GetIO();
auto oldContext = glXGetCurrentContext(); auto oldContext = glXGetCurrentContext();
glXMakeCurrent(display, drawable, context); glXMakeCurrent(display, drawable, context);
@ -114,9 +114,7 @@ void OpenGLX_Hook::prepareForOverlay(Display* display, GLXDrawable drawable)
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(io.DisplaySize.x, io.DisplaySize.y); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
ImGui::Render(); ImGui::Render();

View File

@ -86,7 +86,7 @@ void Steam_Overlay::SetupOverlay()
void Steam_Overlay::HookReady() void Steam_Overlay::HookReady()
{ {
if (!is_ready) // If this is the first time we are ready, hook directinput and xinput, so we can intercept em and disable mouse. if (!is_ready)
{ {
// TODO: Uncomment this and draw our own cursor (cosmetics) // TODO: Uncomment this and draw our own cursor (cosmetics)
//ImGuiIO &io = ImGui::GetIO(); //ImGuiIO &io = ImGui::GetIO();
@ -98,10 +98,6 @@ void Steam_Overlay::HookReady()
} }
} }
// https://niemand.com.ar/2019/01/01/how-to-hook-directx-11-imgui/
// https://github.com/spazzarama/Direct3DHook/blob/master/Capture/Hook
// https://github.com/unknownv2/LinuxDetours
void Steam_Overlay::OpenOverlayInvite(CSteamID lobbyId) void Steam_Overlay::OpenOverlayInvite(CSteamID lobbyId)
{ {
ShowOverlay(true); ShowOverlay(true);
@ -172,7 +168,7 @@ void Steam_Overlay::ShowOverlay(bool state)
void Steam_Overlay::NotifyUser(friend_window_state& friend_state, std::string const& message) void Steam_Overlay::NotifyUser(friend_window_state& friend_state, std::string const& message)
{ {
if (!(friend_state.window_state & window_state_show)) if (!(friend_state.window_state & window_state_show) || !show_overlay)
{ {
friend_state.window_state |= window_state_need_attention; friend_state.window_state |= window_state_need_attention;
#ifdef __WINDOWS__ #ifdef __WINDOWS__
@ -385,10 +381,16 @@ void Steam_Overlay::BuildFriendWindow(Friend const& frd, friend_window_state& st
ImGui::End(); ImGui::End();
} }
ImFont *font_default;
ImFont *font_notif;
void Steam_Overlay::BuildNotifications(int width, int height) void Steam_Overlay::BuildNotifications(int width, int height)
{ {
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()); auto now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
int i = 0; int i = 0;
int font_size = ImGui::GetFontSize();
for (auto it = notifications.begin(); it != notifications.end(); ++it, ++i) for (auto it = notifications.begin(); it != notifications.end(); ++it, ++i)
{ {
auto elapsed_notif = now - it->start_time; auto elapsed_notif = now - it->start_time;
@ -414,8 +416,8 @@ void Steam_Overlay::BuildNotifications(int width, int height)
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(255, 255, 255, Notification::max_alpha*2)); ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(255, 255, 255, Notification::max_alpha*2));
} }
ImGui::SetNextWindowPos(ImVec2((float)width - Notification::width, (float)Notification::height * i )); ImGui::SetNextWindowPos(ImVec2((float)width - width * Notification::width, Notification::height * font_size * i ));
ImGui::SetNextWindowSize(ImVec2( Notification::width, Notification::height )); ImGui::SetNextWindowSize(ImVec2( width * Notification::width, Notification::height * font_size ));
ImGui::Begin(std::to_string(10000+i).c_str(), nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGui::Begin(std::to_string(10000+i).c_str(), nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMouseInputs); ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMouseInputs);
@ -430,15 +432,38 @@ void Steam_Overlay::BuildNotifications(int width, int height)
}), notifications.end()); }), notifications.end());
} }
void Steam_Overlay::CreateFonts()
{
ImGuiIO& io = ImGui::GetIO();
ImFontConfig fontcfg;
fontcfg.OversampleH = fontcfg.OversampleV = 1;
fontcfg.PixelSnapH = true;
fontcfg.GlyphRanges = io.Fonts->GetGlyphRangesDefault();
fontcfg.SizePixels = std::round(io.DisplaySize.y / 68);
font_default = io.Fonts->AddFontDefault(&fontcfg);
fontcfg.SizePixels = std::round(io.DisplaySize.y / 60);
font_notif = io.Fonts->AddFontDefault(&fontcfg);
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 0.0; // Disable round window
}
// Try to make this function as short as possible or it might affect game's fps. // Try to make this function as short as possible or it might affect game's fps.
void Steam_Overlay::OverlayProc( int width, int height ) void Steam_Overlay::OverlayProc()
{ {
std::lock_guard<std::recursive_mutex> lock(global_mutex); std::lock_guard<std::recursive_mutex> lock(global_mutex);
if (!Ready()) if (!Ready())
return; return;
BuildNotifications(width, height); ImGuiIO& io = ImGui::GetIO();
ImGui::PushFont(font_notif);
BuildNotifications(io.DisplaySize.x, io.DisplaySize.y);
ImGui::PopFont();
if (show_overlay) if (show_overlay)
{ {
@ -446,12 +471,12 @@ void Steam_Overlay::OverlayProc( int width, int height )
// Set the overlay windows to the size of the game window // Set the overlay windows to the size of the game window
ImGui::SetNextWindowPos({ 0,0 }); ImGui::SetNextWindowPos({ 0,0 });
ImGui::SetNextWindowSize({ static_cast<float>(width), ImGui::SetNextWindowSize({ static_cast<float>(io.DisplaySize.x),
static_cast<float>(height) }); static_cast<float>(io.DisplaySize.y) });
ImGui::SetNextWindowBgAlpha(0.50); ImGui::SetNextWindowBgAlpha(0.50);
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 0.0; // Disable round window ImGui::PushFont(font_default);
if (ImGui::Begin("SteamOverlay", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus)) if (ImGui::Begin("SteamOverlay", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus))
{ {
@ -487,6 +512,8 @@ void Steam_Overlay::OverlayProc( int width, int height )
} }
} }
ImGui::End(); ImGui::End();
ImGui::PopFont();
}// if(show_overlay) }// if(show_overlay)
} }
@ -507,7 +534,7 @@ void Steam_Overlay::Callback(Common_Message *msg)
friend_info->second.window_state |= window_state_need_attention; friend_info->second.window_state |= window_state_need_attention;
} }
AddNotification(friend_info->first.name() + " says: " + steam_message.message()); NotifyUser(friend_info->second, friend_info->first.name() + " says: " + steam_message.message());
} }
} }
} }

View File

@ -41,8 +41,8 @@ struct Friend_Less
struct Notification struct Notification
{ {
static constexpr float width = 200.0; static constexpr float width = 0.25;
static constexpr float height = 60.0; static constexpr float height = 4.0;
static constexpr std::chrono::milliseconds fade_in = std::chrono::milliseconds(2000); static constexpr std::chrono::milliseconds fade_in = std::chrono::milliseconds(2000);
static constexpr std::chrono::milliseconds fade_out = std::chrono::milliseconds(2000); static constexpr std::chrono::milliseconds fade_out = std::chrono::milliseconds(2000);
static constexpr std::chrono::milliseconds show_time = std::chrono::milliseconds(6000) + fade_in + fade_out; static constexpr std::chrono::milliseconds show_time = std::chrono::milliseconds(6000) + fade_in + fade_out;
@ -117,7 +117,8 @@ public:
void HookReady(); void HookReady();
void OverlayProc(int width, int height); void CreateFonts();
void OverlayProc();
void OpenOverlayInvite(CSteamID lobbyId); void OpenOverlayInvite(CSteamID lobbyId);
void OpenOverlay(const char* pchDialog); void OpenOverlay(const char* pchDialog);
@ -139,7 +140,7 @@ public:
class Steam_Overlay class Steam_Overlay
{ {
public: public:
Steam_Overlay(Settings* settings, SteamCallResults* callback_results, SteamCallBacks* callbacks, RunEveryRunCB* run_every_runcb, Networking *network) {} Steam_Overlay(Settings* settings, SteamCallResults* callback_results, SteamCallBacks* callbacks, RunEveryRunCB* run_every_runcb, Networking* network) {}
~Steam_Overlay() {} ~Steam_Overlay() {}
bool Ready() const { return false; } bool Ready() const { return false; }
@ -151,13 +152,15 @@ public:
void SetNotificationInset(int nHorizontalInset, int nVerticalInset) {} void SetNotificationInset(int nHorizontalInset, int nVerticalInset) {}
void SetupOverlay() {} void SetupOverlay() {}
void HookReady(void* hWnd) {} void HookReady() {}
void OverlayProc(int width, int height) {} void CreateFonts() {}
void OverlayProc() {}
void OpenOverlayInvite(CSteamID lobbyId) {} void OpenOverlayInvite(CSteamID lobbyId) {}
void OpenOverlay(const char* pchDialog) {} void OpenOverlay(const char* pchDialog) {}
bool ShowOverlay() const {}
void ShowOverlay(bool state) {} void ShowOverlay(bool state) {}
void SetLobbyInvite(Friend friendId, uint64 lobbyId) {} void SetLobbyInvite(Friend friendId, uint64 lobbyId) {}

View File

@ -75,6 +75,8 @@ void DX10_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pDevice->Release(); pDevice->Release();
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
@ -83,9 +85,7 @@ void DX10_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
ImGui::Render(); ImGui::Render();

View File

@ -87,6 +87,8 @@ void DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pDevice->Release(); pDevice->Release();
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
@ -95,9 +97,7 @@ void DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
ImGui::Render(); ImGui::Render();

View File

@ -87,6 +87,8 @@ void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pDevice->Release(); pDevice->Release();
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
} }
@ -130,9 +132,7 @@ void STDMETHODCALLTYPE DX12_Hook::MyExecuteCommandLists(ID3D12CommandQueue *_thi
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(me->sc_desc.BufferDesc.Width, me->sc_desc.BufferDesc.Height); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
((ID3D12GraphicsCommandList*)ppCommandLists[i])->SetDescriptorHeaps(1, &me->pSrvDescHeap); ((ID3D12GraphicsCommandList*)ppCommandLists[i])->SetDescriptorHeaps(1, &me->pSrvDescHeap);
ImGui::Render(); ImGui::Render();

View File

@ -55,12 +55,6 @@ void DX9_Hook::resetRenderState()
// Try to make this function and overlay's proc as short as possible or it might affect game's fps. // Try to make this function and overlay's proc as short as possible or it might affect game's fps.
void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice) void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
{ {
IDirect3DSwapChain9* pSwapChain;
pDevice->GetSwapChain(0, &pSwapChain);
D3DPRESENT_PARAMETERS PresentParameters;
pSwapChain->GetPresentParameters(&PresentParameters);
pSwapChain->Release();
D3DDEVICE_CREATION_PARAMETERS param; D3DDEVICE_CREATION_PARAMETERS param;
pDevice->GetCreationParameters(&param); pDevice->GetCreationParameters(&param);
@ -75,6 +69,9 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
io.IniFilename = NULL; io.IniFilename = NULL;
ImGui_ImplDX9_Init(pDevice); ImGui_ImplDX9_Init(pDevice);
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
@ -83,9 +80,7 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(PresentParameters.BackBufferWidth, PresentParameters.BackBufferHeight); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
ImGui::Render(); ImGui::Render();

View File

@ -67,9 +67,6 @@ void OpenGL_Hook::resetRenderState()
void OpenGL_Hook::prepareForOverlay(HDC hDC) void OpenGL_Hook::prepareForOverlay(HDC hDC)
{ {
HWND hWnd = WindowFromDC(hDC); HWND hWnd = WindowFromDC(hDC);
RECT rect;
GetClientRect(hWnd, &rect);
if (hWnd != Windows_Hook::Inst()->GetGameHwnd()) if (hWnd != Windows_Hook::Inst()->GetGameHwnd())
resetRenderState(); resetRenderState();
@ -82,6 +79,8 @@ void OpenGL_Hook::prepareForOverlay(HDC hDC)
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
get_steam_client()->steam_overlay->CreateFonts();
initialized = true; initialized = true;
} }
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
@ -89,9 +88,7 @@ void OpenGL_Hook::prepareForOverlay(HDC hDC)
ImGui::NewFrame(); ImGui::NewFrame();
get_steam_client()->steam_overlay->OverlayProc(rect.right, rect.bottom); get_steam_client()->steam_overlay->OverlayProc();
ImGui::EndFrame();
ImGui::Render(); ImGui::Render();