update Component interface

This commit is contained in:
Nomango 2019-07-30 13:32:10 +08:00
parent d025adb453
commit df3b5e459e
6 changed files with 127 additions and 44 deletions

View File

@ -28,7 +28,16 @@ namespace kiwano
{ {
public: public:
virtual void SetupComponent(Application*) = 0; virtual void SetupComponent(Application*) = 0;
virtual void DestroyComponent() = 0; virtual void DestroyComponent() = 0;
virtual void BeforeUpdate() {}
virtual void OnUpdate(float dt) {}
virtual void AfterUpdate() {}
virtual void BeforeRender() {}
virtual void OnRender() {}
virtual void AfterRender() {}
virtual void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {}
}; };
} }

View File

@ -20,7 +20,7 @@
#include "input.h" #include "input.h"
#include "logs.h" #include "logs.h"
#include <cstring> #include <windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
namespace kiwano namespace kiwano
{ {
@ -67,6 +67,41 @@ namespace kiwano
mouse_pos_y_ = y; mouse_pos_y_ = y;
} }
void Input::HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
//case WM_LBUTTONDBLCLK:
case WM_MBUTTONUP:
case WM_MBUTTONDOWN:
//case WM_MBUTTONDBLCLK:
case WM_RBUTTONUP:
case WM_RBUTTONDOWN:
//case WM_RBUTTONDBLCLK:
case WM_MOUSEMOVE:
case WM_MOUSEWHEEL:
{
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); }
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); }
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); }
else if (msg == WM_MOUSEMOVE) { UpdateMousePos(static_cast<float>(GET_X_LPARAM(lparam)), static_cast<float>(GET_Y_LPARAM(lparam))); }
break;
}
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
{
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
UpdateKey((int)wparam, down);
}
}
}
bool Input::IsDown(int key_or_btn) bool Input::IsDown(int key_or_btn)
{ {
KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM); KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM);

View File

@ -69,6 +69,8 @@ namespace kiwano
void UpdateMousePos(float, float); void UpdateMousePos(float, float);
void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override;
protected: protected:
Input(); Input();

View File

@ -244,6 +244,13 @@ namespace kiwano
const auto dt = (now - last) * time_scale_; const auto dt = (now - last) * time_scale_;
last = now; last = now;
// Before update
for (Component* c : components_)
{
c->BeforeUpdate();
}
// Updating
if (transition_) if (transition_)
{ {
transition_->Update(dt); transition_->Update(dt);
@ -296,15 +303,22 @@ namespace kiwano
if (debug_node_) if (debug_node_)
debug_node_->Update(dt); debug_node_->Update(dt);
Input::Instance().Update(); // After update
for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
{
(*rit)->AfterUpdate();
}
} }
void Application::Render() void Application::Render()
{ {
ThrowIfFailed( // Before render
Renderer::Instance().BeginDraw() for (Component* c : components_)
); {
c->BeforeRender();
}
// Rendering
if (transition_) if (transition_)
{ {
transition_->Render(); transition_->Render();
@ -319,9 +333,11 @@ namespace kiwano
if (debug_node_) if (debug_node_)
debug_node_->Render(); debug_node_->Render();
ThrowIfFailed( // After render
Renderer::Instance().EndDraw() for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit)
); {
(*rit)->AfterRender();
}
} }
void Application::PreformInMainThread(Closure<void()> function) void Application::PreformInMainThread(Closure<void()> function)
@ -332,12 +348,16 @@ namespace kiwano
LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{ {
Application * app = reinterpret_cast<Application*>( Application * app = reinterpret_cast<Application*>(static_cast<LONG_PTR>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA)));
static_cast<LONG_PTR>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA))
);
if (!app) if (!app)
return ::DefWindowProcW(hwnd, msg, wparam, lparam); return ::DefWindowProcW(hwnd, msg, wparam, lparam);
// Handle Message
for (Component* c : app->components_)
{
c->HandleMessage(hwnd, msg, wparam, lparam);
}
switch (msg) switch (msg)
{ {
@ -356,11 +376,9 @@ namespace kiwano
case WM_KEYUP: case WM_KEYUP:
case WM_SYSKEYUP: case WM_SYSKEYUP:
{ {
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
Input::Instance().UpdateKey((int)wparam, down);
if (!app->transition_ && app->curr_scene_) if (!app->transition_ && app->curr_scene_)
{ {
bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN;
Event evt(down ? Event::KeyDown : Event::KeyUp); Event evt(down ? Event::KeyDown : Event::KeyUp);
evt.key.code = static_cast<int>(wparam); evt.key.code = static_cast<int>(wparam);
evt.key.count = static_cast<int>(lparam & 0xFF); evt.key.count = static_cast<int>(lparam & 0xFF);
@ -395,10 +413,6 @@ namespace kiwano
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
{ {
if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { Input::Instance().UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); }
else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { Input::Instance().UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); }
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { Input::Instance().UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); }
if (!app->transition_ && app->curr_scene_) if (!app->transition_ && app->curr_scene_)
{ {
Event evt; Event evt;
@ -419,24 +433,11 @@ namespace kiwano
app->curr_scene_->Dispatch(evt); app->curr_scene_->Dispatch(evt);
} }
if (msg == WM_MOUSEMOVE)
{
Input::Instance().UpdateMousePos(
static_cast<float>(GET_X_LPARAM(lparam)),
static_cast<float>(GET_Y_LPARAM(lparam))
);
}
} }
break; break;
case WM_SIZE: case WM_SIZE:
{ {
UINT width = LOWORD(lparam);
UINT height = HIWORD(lparam);
Renderer::Instance().Resize(width, height);
if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam) if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam)
{ {
KGE_LOG(L"Window minimized"); KGE_LOG(L"Window minimized");
@ -448,8 +449,8 @@ namespace kiwano
if (app->curr_scene_) if (app->curr_scene_)
{ {
Event evt(Event::WindowResized); Event evt(Event::WindowResized);
evt.win.width = static_cast<int>(width); evt.win.width = LOWORD(lparam);
evt.win.height = static_cast<int>(height); evt.win.height = HIWORD(lparam);
app->curr_scene_->Dispatch(evt); app->curr_scene_->Dispatch(evt);
} }

View File

@ -100,6 +100,35 @@ namespace kiwano
d3d_res_.Reset(); d3d_res_.Reset();
} }
void Renderer::BeforeRender()
{
ThrowIfFailed(
BeginDraw()
);
}
void Renderer::AfterRender()
{
ThrowIfFailed(
EndDraw()
);
}
void Renderer::HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_SIZE:
{
UINT width = LOWORD(lparam);
UINT height = HIWORD(lparam);
Resize(width, height);
break;
}
}
}
HRESULT Renderer::CreateDeviceResources() HRESULT Renderer::CreateDeviceResources()
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;

View File

@ -51,10 +51,6 @@ namespace kiwano
KGE_DECLARE_SINGLETON(Renderer); KGE_DECLARE_SINGLETON(Renderer);
public: public:
HRESULT BeginDraw();
HRESULT EndDraw();
HRESULT CreateLayer( HRESULT CreateLayer(
ComPtr<ID2D1Layer>& layer ComPtr<ID2D1Layer>& layer
); );
@ -142,6 +138,19 @@ namespace kiwano
UINT height UINT height
); );
public:
void SetupComponent(Application*) override;
void DestroyComponent() override;
void BeforeRender() override;
void AfterRender() override;
void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override;
void SetCollectingStatus(bool collecting);
public: public:
struct Status struct Status
{ {
@ -150,12 +159,6 @@ namespace kiwano
int primitives; int primitives;
}; };
void SetupComponent(Application*) override;
void DestroyComponent() override;
void SetCollectingStatus(bool collecting);
inline HWND GetTargetWindow() const { return hwnd_; } inline HWND GetTargetWindow() const { return hwnd_; }
inline Status const& GetStatus() const { return status_; } inline Status const& GetStatus() const { return status_; }
@ -179,6 +182,10 @@ namespace kiwano
HRESULT HandleDeviceLost(); HRESULT HandleDeviceLost();
HRESULT BeginDraw();
HRESULT EndDraw();
private: private:
unsigned long ref_count_; unsigned long ref_count_;