add Window::SetMinimumSize()

This commit is contained in:
Nomango 2020-05-20 11:55:53 +08:00
parent 01ce68accb
commit 1de59e3a15
3 changed files with 110 additions and 18 deletions

View File

@ -29,6 +29,8 @@ Window::Window()
, is_fullscreen_(false)
, width_(0)
, height_(0)
, min_width_(100)
, min_height_(50)
{
}

View File

@ -128,6 +128,14 @@ public:
*/
virtual void Resize(uint32_t width, uint32_t height) = 0;
/**
* \~chinese
* @brief
* @param width
* @param height
*/
virtual void SetMinimumSize(uint32_t width, uint32_t height) = 0;
/**
* \~chinese
* @brief
@ -183,6 +191,8 @@ protected:
bool is_fullscreen_;
uint32_t width_;
uint32_t height_;
uint32_t min_width_;
uint32_t min_height_;
WindowHandle handle_;
String title_;
std::queue<EventPtr> event_queue_;

View File

@ -56,6 +56,8 @@ public:
void Resize(uint32_t width, uint32_t height) override;
void SetMinimumSize(uint32_t width, uint32_t height) override;
void SetCursor(CursorType cursor) override;
void PumpEvents() override;
@ -70,6 +72,8 @@ public:
private:
bool resizable_;
bool is_resizing_;
bool is_minimized_;
CursorType mouse_cursor_;
String device_name_;
@ -133,6 +137,8 @@ void AdjustWindow(uint32_t width, uint32_t height, DWORD style, uint32_t* win_wi
WindowWin32Impl::WindowWin32Impl()
: resizable_(false)
, is_resizing_(false)
, is_minimized_(false)
, mouse_cursor_(CursorType::Arrow)
, key_map_{}
{
@ -321,6 +327,12 @@ void WindowWin32Impl::Resize(uint32_t width, uint32_t height)
::SetWindowPos(handle_, 0, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
}
void WindowWin32Impl::SetMinimumSize(uint32_t width, uint32_t height)
{
min_width_ = width;
min_height_ = height;
}
void WindowWin32Impl::SetCursor(CursorType cursor)
{
mouse_cursor_ = cursor;
@ -475,6 +487,44 @@ LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA
if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam)
{
KGE_SYS_LOG("Window minimized");
is_minimized_ = true;
// Pause game when window is minimized
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
}
}
else if (SIZE_MAXIMIZED == wparam)
{
if (is_minimized_)
{
is_minimized_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
}
}
}
else if (wparam == SIZE_RESTORED)
{
if (is_minimized_)
{
KGE_SYS_LOG("Window restored");
// the window was restored and was previously minimized
is_minimized_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
}
}
else if (is_resizing_)
{
// DO NOTHING until the dragging / resizing has stopped.
}
else
{
@ -489,8 +539,41 @@ LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA
this->PushEvent(evt);
}
}
}
break;
case WM_ENTERSIZEMOVE:
{
is_resizing_ = true;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Pause();
}
return 0;
}
break;
case WM_EXITSIZEMOVE:
{
is_resizing_ = false;
if (Application::GetInstance().IsRunning())
{
TimerPtr timer = Application::GetInstance().GetTimer();
timer->Resume();
}
return 0;
}
break;
case WM_GETMINMAXINFO:
{
// prevent the window from becoming too small
((MINMAXINFO*)lparam)->ptMinTrackSize.x = LONG(min_width_);
((MINMAXINFO*)lparam)->ptMinTrackSize.y = LONG(min_height_);
}
return 0;
case WM_MOVE:
{
WindowMovedEventPtr evt = new WindowMovedEvent;
@ -500,6 +583,13 @@ LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA
}
break;
case WM_MENUCHAR:
{
// Disables the crazy beeping sound when pressing a mnemonic key.
// Simply tell Windows that we want the menu closed.
return MAKELRESULT(0, MNC_CLOSE);
}
case WM_ACTIVATE:
{
bool active = (LOWORD(wparam) != WA_INACTIVE);
@ -507,16 +597,6 @@ LRESULT WindowWin32Impl::MessageProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA
WindowFocusChangedEventPtr evt = new WindowFocusChangedEvent;
evt->focus = active;
this->PushEvent(evt);
// Pause game when window is inactive
TimerPtr timer = Application::GetInstance().GetTimer();
if (timer)
{
if (active)
timer->Resume();
else
timer->Pause();
}
}
break;