Skip to content

Commit 36fe848

Browse files
MikhailGorobetsTheMostDiligent
authored andcommitted
[Emscripten, ImGui]: Fixed resizing of canvas
1 parent a1f55fc commit 36fe848

File tree

5 files changed

+92
-39
lines changed

5 files changed

+92
-39
lines changed

Imgui/src/ImGuiImplEmscripten.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ void ImGuiImplEmscripten::Render(IDeviceContext* pCtx)
9090

9191
bool ImGuiImplEmscripten::OnMouseEvent(int32_t EventType, const EmscriptenMouseEvent* Event)
9292
{
93+
auto DevicePixelRatio = emscripten_get_device_pixel_ratio();
94+
9395
auto& io = ImGui::GetIO();
94-
io.MousePos = ImVec2(Event->targetX, Event->targetY);
96+
io.MousePos = ImVec2(Event->targetX * DevicePixelRatio, Event->targetY * DevicePixelRatio);
9597
io.MouseDown[0] = Event->buttons & 1;
9698
io.MouseDown[1] = Event->buttons & 2;
9799
io.MouseDown[2] = Event->buttons & 4;

NativeApp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ elseif(PLATFORM_TVOS)
391391

392392
elseif(PLATFORM_EMSCRIPTEN)
393393
set(SOURCE
394+
src/Emscripten/EmscriptenAppBase.cpp
394395
src/Emscripten/EmscriptenMain.cpp
395396
)
396397
set(INCLUDE

NativeApp/include/Emscripten/EmscriptenAppBase.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@ class EmscriptenAppBase : public AppBase
4040
public:
4141
using AppBase::Update;
4242

43+
void Update();
44+
4345
virtual void OnMouseEvent(int32_t EventType, const EmscriptenMouseEvent* Event) = 0;
4446

4547
virtual void OnWheelEvent(int32_t EventType, const EmscriptenWheelEvent* Event) = 0;
@@ -49,6 +51,10 @@ class EmscriptenAppBase : public AppBase
4951
virtual void OnWindowCreated(const char* pCanvasID,
5052
int32_t WindowWidth,
5153
int32_t WindowHeight) = 0;
54+
55+
protected:
56+
Timer m_Timer;
57+
double m_PrevTime = 0.0;
5258
};
5359

5460
} // namespace Diligent
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
13+
*
14+
* In no event and under no legal theory, whether in tort (including negligence),
15+
* contract, or otherwise, unless required by applicable law (such as deliberate
16+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
17+
* liable for any damages, including any direct, indirect, special, incidental,
18+
* or consequential damages of any character arising as a result of this License or
19+
* out of the use or inability to use the software (including but not limited to damages
20+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
21+
* all other commercial damages or losses), even if such Contributor has been advised
22+
* of the possibility of such damages.
23+
*/
24+
25+
#include "EmscriptenAppBase.hpp"
26+
27+
void Diligent::EmscriptenAppBase::Update()
28+
{
29+
auto CurrTime = m_Timer.GetElapsedTime();
30+
auto ElapsedTime = CurrTime - m_PrevTime;
31+
m_PrevTime = CurrTime;
32+
33+
if (IsReady())
34+
{
35+
Update(CurrTime, ElapsedTime);
36+
Render();
37+
}
38+
}
Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,69 +31,75 @@
3131
#include "NativeAppBase.hpp"
3232
#include "Timer.hpp"
3333

34-
std::unique_ptr<Diligent::NativeAppBase> g_pTheApp = nullptr;
35-
Diligent::Timer g_Timer = {};
36-
double g_PrevTime = 0.0;
3734

38-
void EventLoopCallback()
35+
struct NativeAppCallbackData
3936
{
40-
auto CurrTime = g_Timer.GetElapsedTime();
41-
auto ElapsedTime = CurrTime - g_PrevTime;
42-
g_PrevTime = CurrTime;
37+
Diligent::NativeAppBase* pApplication = nullptr;
38+
const char* CanvasID = nullptr;
39+
};
4340

44-
if (g_pTheApp->IsReady())
41+
void EventLoopCallback(void* pUserData)
42+
{
43+
auto pAppUserData = static_cast<NativeAppCallbackData*>(pUserData);
44+
45+
if (pAppUserData->pApplication->IsReady())
4546
{
46-
g_pTheApp->Update(CurrTime, ElapsedTime);
47-
g_pTheApp->Render();
47+
pAppUserData->pApplication->Update();
48+
pAppUserData->pApplication->Render();
4849
}
4950
}
5051

5152
EM_BOOL EventResizeCallback(int32_t EventType, const EmscriptenUiEvent* Event, void* pUserData)
5253
{
53-
if (g_pTheApp->IsReady())
54-
g_pTheApp->WindowResize(Event->documentBodyClientWidth, Event->documentBodyClientHeight);
54+
auto pAppUserData = static_cast<NativeAppCallbackData*>(pUserData);
55+
56+
int32_t CanvasWidth = 0;
57+
int32_t CanvasHeight = 0;
58+
emscripten_get_canvas_element_size(pAppUserData->CanvasID, &CanvasWidth, &CanvasHeight);
59+
if (pAppUserData->pApplication->IsReady())
60+
pAppUserData->pApplication->WindowResize(CanvasWidth, CanvasHeight);
5561
return true;
5662
}
5763

5864
EM_BOOL EventMouseCallback(int32_t EventType, const EmscriptenMouseEvent* Event, void* pUserData)
5965
{
60-
g_pTheApp->OnMouseEvent(EventType, Event);
66+
auto pAppUserData = static_cast<NativeAppCallbackData*>(pUserData);
67+
pAppUserData->pApplication->OnMouseEvent(EventType, Event);
6168
return true;
6269
}
6370

6471
EM_BOOL EventWheelCallback(int32_t EventType, const EmscriptenWheelEvent* Event, void* pUserData)
6572
{
66-
g_pTheApp->OnWheelEvent(EventType, Event);
73+
auto pAppUserData = static_cast<NativeAppCallbackData*>(pUserData);
74+
pAppUserData->pApplication->OnWheelEvent(EventType, Event);
6775
return true;
6876
}
6977

7078
EM_BOOL EventKeyCallback(int32_t EventType, const EmscriptenKeyboardEvent* Event, void* pUserData)
7179
{
72-
g_pTheApp->OnKeyEvent(EventType, Event);
80+
auto pAppUserData = static_cast<NativeAppCallbackData*>(pUserData);
81+
pAppUserData->pApplication->OnKeyEvent(EventType, Event);
7382
return true;
7483
}
7584

76-
7785
int main(int argc, char* argv[])
7886
{
79-
g_pTheApp.reset(Diligent::CreateApplication());
80-
81-
int32_t CanvasWidth = 0;
82-
int32_t CanvasHeight = 0;
83-
const char* CanvasID = "#canvas";
84-
85-
emscripten_get_canvas_element_size(CanvasID, &CanvasWidth, &CanvasHeight);
86-
emscripten_set_mousedown_callback(CanvasID, nullptr, true, EventMouseCallback);
87-
emscripten_set_mouseup_callback(CanvasID, nullptr, true, EventMouseCallback);
88-
emscripten_set_mousemove_callback(CanvasID, nullptr, true, EventMouseCallback);
89-
emscripten_set_wheel_callback(CanvasID, nullptr, true, EventWheelCallback);
90-
emscripten_set_keydown_callback(CanvasID, nullptr, true, EventKeyCallback);
91-
emscripten_set_keyup_callback(CanvasID, nullptr, true, EventKeyCallback);
92-
emscripten_set_keypress_callback(CanvasID, nullptr, true, EventKeyCallback);
93-
emscripten_set_resize_callback(CanvasID, nullptr, true, EventResizeCallback);
94-
95-
g_pTheApp->OnWindowCreated(CanvasID, CanvasWidth, CanvasHeight);
96-
emscripten_set_main_loop(EventLoopCallback, 0, true);
97-
98-
g_pTheApp.reset();
87+
std::unique_ptr<Diligent::NativeAppBase> pApplication{Diligent::CreateApplication()};
88+
89+
NativeAppCallbackData AppUserData{pApplication.get(), "#canvas"};
90+
91+
int32_t CanvasWidth = 0;
92+
int32_t CanvasHeight = 0;
93+
emscripten_get_canvas_element_size(AppUserData.CanvasID, &CanvasWidth, &CanvasHeight);
94+
emscripten_set_mousedown_callback(AppUserData.CanvasID, &AppUserData, true, EventMouseCallback);
95+
emscripten_set_mouseup_callback(AppUserData.CanvasID, &AppUserData, true, EventMouseCallback);
96+
emscripten_set_mousemove_callback(AppUserData.CanvasID, &AppUserData, true, EventMouseCallback);
97+
emscripten_set_wheel_callback(AppUserData.CanvasID, &AppUserData, true, EventWheelCallback);
98+
emscripten_set_keydown_callback(AppUserData.CanvasID, &AppUserData, true, EventKeyCallback);
99+
emscripten_set_keyup_callback(AppUserData.CanvasID, &AppUserData, true, EventKeyCallback);
100+
emscripten_set_keypress_callback(AppUserData.CanvasID, &AppUserData, true, EventKeyCallback);
101+
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, &AppUserData, true, EventResizeCallback);
102+
pApplication->OnWindowCreated(AppUserData.CanvasID, CanvasWidth, CanvasHeight);
103+
104+
emscripten_set_main_loop_arg(EventLoopCallback, &AppUserData, 0, true);
99105
}

0 commit comments

Comments
 (0)