add window close callback

This commit is contained in:
Sri Sham Haran 2022-10-02 01:00:07 +08:00
parent 64d1c2ab2e
commit 9e8b4414d1
2 changed files with 27 additions and 0 deletions

View File

@ -272,6 +272,17 @@ namespace SHADE
windowResizeCallbacks.erase(callbackid); windowResizeCallbacks.erase(callbackid);
} }
SHWindow::CALLBACKID SHWindow::RegisterWindowCloseCallback(WindowResizeCallbackFn windowCloseCallback)
{
windowCloseCallbacks.try_emplace(windowResizeCallbackCount, windowCloseCallback);
return windowCloseCallbackCount++;
}
void SHWindow::UnregisterWindowCloseCallback(CALLBACKID const& callbackid)
{
windowCloseCallbacks.erase(callbackid);
}
LRESULT SHWindow::WndProcStatic(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) LRESULT SHWindow::WndProcStatic(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{ {
auto window = windowMap.GetWindow(hwnd); auto window = windowMap.GetWindow(hwnd);
@ -307,6 +318,8 @@ namespace SHADE
case WM_CREATE: case WM_CREATE:
OnCreate(hwnd, reinterpret_cast<LPCREATESTRUCT>(wparam)); OnCreate(hwnd, reinterpret_cast<LPCREATESTRUCT>(wparam));
break; break;
case WM_CLOSE:
case WM_DESTROY: case WM_DESTROY:
OnDestroy(); OnDestroy();
return 0; return 0;
@ -365,6 +378,14 @@ namespace SHADE
} }
void SHADE::SHWindow::OnClose()
{
for (const auto& callbackFn : windowCloseCallbacks | std::views::values)
{
callbackFn();
}
}
void SHWindow::OnDestroy() void SHWindow::OnDestroy()
{ {
this->Destroy(); this->Destroy();

View File

@ -75,6 +75,7 @@ namespace SHADE
public: public:
using SHVec2 = std::pair<uint32_t, uint32_t>; using SHVec2 = std::pair<uint32_t, uint32_t>;
typedef std::function<void(uint32_t width, uint32_t height)> WindowResizeCallbackFn; typedef std::function<void(uint32_t width, uint32_t height)> WindowResizeCallbackFn;
typedef std::function<void(void)> WindowCloseCallbackFn;
typedef uint16_t CALLBACKID; typedef uint16_t CALLBACKID;
SHWindow(); SHWindow();
@ -125,6 +126,8 @@ namespace SHADE
CALLBACKID RegisterWindowSizeCallback(WindowResizeCallbackFn); CALLBACKID RegisterWindowSizeCallback(WindowResizeCallbackFn);
void UnregisterWindowSizeCallback(CALLBACKID const& callbackid); void UnregisterWindowSizeCallback(CALLBACKID const& callbackid);
CALLBACKID RegisterWindowCloseCallback(WindowResizeCallbackFn);
void UnregisterWindowCloseCallback(CALLBACKID const& callbackid);
bool IsMinimized() const { return wndData.isMinimised; } bool IsMinimized() const { return wndData.isMinimised; }
protected: protected:
@ -157,10 +160,13 @@ namespace SHADE
HFONT font; HFONT font;
std::unordered_map<CALLBACKID, WindowResizeCallbackFn> windowResizeCallbacks; std::unordered_map<CALLBACKID, WindowResizeCallbackFn> windowResizeCallbacks;
std::unordered_map<CALLBACKID, WindowCloseCallbackFn> windowCloseCallbacks;
CALLBACKID windowResizeCallbackCount{}; CALLBACKID windowResizeCallbackCount{};
CALLBACKID windowCloseCallbackCount{};
//TODO: Shift to events abstraction //TODO: Shift to events abstraction
void OnCreate(HWND hwnd, LPCREATESTRUCT create_struct); void OnCreate(HWND hwnd, LPCREATESTRUCT create_struct);
void OnClose();
void OnDestroy(); void OnDestroy();
//void OnFileDrop(HDROP drop); //void OnFileDrop(HDROP drop);
void OnSize(UINT msg, UINT type, SIZE size); void OnSize(UINT msg, UINT type, SIZE size);