Commit 9f392617 authored by Administrator's avatar Administrator
Browse files

added mouse and right button drag of viewpoint

parent 67bf391c
#include "CoreHandler.h" #include "CoreHandler.h"
#include <sstream>
CoreHandler::CoreHandler(Logger& logger) using namespace std;
CoreHandler::CoreHandler(Window& window, Logger& logger)
{ {
// init local variable // init local variable
seed = 0.0f; seed = 0.0f;
seed = int(seed) % 10000; seed = int(seed) % 10000;
}; };
void CoreHandler::CoreProcess(Window& window, Graphics& gfx, Logger& logger) void CoreHandler::CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger)
{ {
// Process event
while (!Window::mouse.IsQueueEmpty())
{
auto mouseEvent = Window::mouse.ReadEvent();
switch (mouseEvent.GetType())
{
case (Mouse::MouseEvent::Type::Move):
{
std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Moved";
// Check if drag by right button
if (Window::mouse.IsRightClicked())
{
cameraDeltaX = Window::mouse.GetRightDragDeltaX();
cameraDeltaY = Window::mouse.GetRightDragDeltaY();
}
oss << " | ViewAt(" << cameraX << "," << cameraY << ")";
oss << " | Dragging(" << cameraDeltaX << "," << cameraDeltaY << ")";
oss << " | vSinValue(" << sin((cameraY + cameraDeltaY) / 100.0f) << ")";
window.SetTitle(hWnd, oss.str());
break;
}
case (Mouse::MouseEvent::Type::LClick):
{
std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Clicked";
window.SetTitle(hWnd, oss.str());
break;
}
case (Mouse::MouseEvent::Type::LRelease):
{
std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Left Released";
window.SetTitle(hWnd, oss.str());
break;
}
case (Mouse::MouseEvent::Type::RClick):
{
std::ostringstream oss;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Clicked";
window.SetTitle(hWnd, oss.str());
break;
}
case (Mouse::MouseEvent::Type::RRelease):
{
std::ostringstream oss;
cameraX += cameraDeltaX;
cameraY += cameraDeltaY;
cameraDeltaX = 0;
cameraDeltaY = 0;
oss << "(" << mouseEvent.GetX() << "," << mouseEvent.GetY() << "): Mouse Right Released";
window.SetTitle(hWnd, oss.str());
break;
}
}
}
// Display background and set depthstencil // Display background and set depthstencil
window.GetGfx().RendorFrame(); window.GetGfx().RendorFrame();
...@@ -25,7 +90,7 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, Logger& logger) ...@@ -25,7 +90,7 @@ void CoreHandler::CoreProcess(Window& window, Graphics& gfx, Logger& logger)
void CoreHandler::SetCamera(Window& window, Logger& logger) void CoreHandler::SetCamera(Window& window, Logger& logger)
{ {
// Put camera calculation logic here // Put camera calculation logic here
seed++; /*seed++;
auto dt = sin(seed / 1000); auto dt = sin(seed / 1000);
logger.PutLog("seed", std::to_string(seed)); logger.PutLog("seed", std::to_string(seed));
logger.PutLog("dt", std::to_string(dt)); logger.PutLog("dt", std::to_string(dt));
...@@ -36,6 +101,21 @@ void CoreHandler::SetCamera(Window& window, Logger& logger) ...@@ -36,6 +101,21 @@ void CoreHandler::SetCamera(Window& window, Logger& logger)
// Set camera // Set camera
DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(5.0f * sinValue, 3.0f, -5.0f * cosValue, 1.0f); DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(5.0f * sinValue, 3.0f, -5.0f * cosValue, 1.0f);
DirectX::XMVECTOR focusPosition = DirectX::XMVectorZero(); DirectX::XMVECTOR focusPosition = DirectX::XMVectorZero();
DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);*/
float radius = 5.0f;
auto vSinValue = sin((cameraY + cameraDeltaY) / 100.0f);
auto vCosValue = cos((cameraY + cameraDeltaY) / 100.0f);
float eyePosY = radius * vSinValue;
float hRadius = radius * vCosValue; // horizon radius
auto sinValue = sin((cameraX + cameraDeltaX) / 100.0f);
auto cosValue = cos((cameraX + cameraDeltaX) / 100.0f);
DirectX::XMVECTOR eyePosition = DirectX::XMVectorSet(-hRadius * sinValue, eyePosY, hRadius * cosValue, 1.0f);
DirectX::XMVECTOR focusPosition = DirectX::XMVectorZero();
DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f); DirectX::XMVECTOR upDirection = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f);
window.GetGfx().SetCamera(logger, eyePosition, focusPosition, upDirection); window.GetGfx().SetCamera(logger, eyePosition, focusPosition, upDirection);
......
...@@ -2,13 +2,21 @@ ...@@ -2,13 +2,21 @@
#include "Window.h" #include "Window.h"
#include "Graphics.h" #include "Graphics.h"
#include "Logger.h" #include "Logger.h"
#include "Mouse.h"
#include <memory>
class CoreHandler class CoreHandler
{ {
public: public:
CoreHandler(Logger& logger); CoreHandler(Window& window, Logger& logger);
void CoreProcess(Window& window, Graphics& gfx, Logger& logger); void CoreProcess(Window& window, Graphics& gfx, HWND& hWnd, Logger& logger);
void SetCamera(Window& window, Logger& logger); void SetCamera(Window& window, Logger& logger);
private: private:
double seed; double seed;
int cameraX = 80;
int cameraY = 60;
int cameraZ = 0;
int cameraDeltaX = 0;
int cameraDeltaY = 0;
int cameraDeltaZ = 0;
}; };
\ No newline at end of file
#include "Mouse.h"
Mouse::Mouse()
{
}
int Mouse::GetX()
{
return x;
}
int Mouse::GetY()
{
return y;
}
bool Mouse::IsLeftClicked()
{
return isLeftClicked;
}
bool Mouse::IsRightClicked()
{
return isRightClicked;
}
int Mouse::GetLeftClickAtX()
{
return leftClickAtX;
}
int Mouse::GetLeftClickAtY()
{
return leftClickAtY;
}
int Mouse::GetRightClickAtX()
{
return rightClickAtX;
}
int Mouse::GetRightClickAtY()
{
return rightClickAtY;
}
int Mouse::GetRightDragDeltaX()
{
return rightDragDeltaX;
}
int Mouse::GetRightDragDeltaY()
{
return rightDragDeltaY;
}
void Mouse::OnMouseMove(int newX, int newY)
{
x = newX;
y = newY;
if (rightClickAtX != NULL)
{
rightDragDeltaX = newX - rightClickAtX;
rightDragDeltaY = newY - rightClickAtY;
}
mouseEvents.push(Mouse::MouseEvent(Mouse::MouseEvent::Type::Move, *this));
TrimEvents();
}
void Mouse::OnLeftClick(int newX, int newY)
{
isLeftClicked = true;
leftClickAtX = newX;
leftClickAtY = newY;
mouseEvents.push(Mouse::MouseEvent(Mouse::MouseEvent::Type::LClick, *this));
TrimEvents();
}
void Mouse::OnleftRelease(int newX, int newY)
{
isLeftClicked = false;
leftClickAtX = NULL;
leftClickAtY = NULL;
mouseEvents.push(Mouse::MouseEvent(Mouse::MouseEvent::Type::LRelease, *this));
TrimEvents();
}
void Mouse::OnRightClick(int newX, int newY)
{
isRightClicked = true;
rightClickAtX = newX;
rightClickAtY = newY;
mouseEvents.push(Mouse::MouseEvent(Mouse::MouseEvent::Type::RClick, *this));
TrimEvents();
}
void Mouse::OnRightRelease(int newX, int newY)
{
isRightClicked = false;
rightClickAtX = NULL;
rightClickAtY = NULL;
mouseEvents.push(Mouse::MouseEvent(Mouse::MouseEvent::Type::RRelease, *this));
TrimEvents();
}
Mouse::MouseEvent Mouse::ReadEvent()
{
if (mouseEvents.size() > 0)
{
Mouse::MouseEvent mouseEvent = mouseEvents.front();
mouseEvents.pop();
return mouseEvent;
}
else
{
return Mouse::MouseEvent();
}
}
void Mouse::TrimEvents()
{
while (mouseEvents.size() > bufferSize)
{
mouseEvents.pop();
}
}
bool Mouse::IsQueueEmpty()
{
return mouseEvents.empty();
}
\ No newline at end of file
#pragma once
#include <queue>
class Mouse
{
public:
class MouseEvent
{
public:
enum class Type
{
LClick,
LRelease,
RClick,
RRelease,
WheelUp,
WheelDown,
Move,
Enter,
Leave,
Invalid
};
public:
MouseEvent()
:
type(Type::Invalid),
isLeftClicked(false),
isRightClicked(false),
x(0),
y(0)
{}
MouseEvent(Type type, const Mouse& parent)
:
type(type),
isLeftClicked(parent.isLeftClicked),
isRightClicked(parent.isRightClicked),
x(parent.x),
y(parent.y)
{}
bool isValid()
{
return (type != Type::Invalid);
}
Type GetType()
{
return type;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
bool IsLeftClicked()
{
return isLeftClicked;
}
bool IsRightClicked()
{
return isRightClicked;
}
private:
Type type;
int x;
int y;
bool isLeftClicked;
bool isRightClicked;
};
public:
Mouse();
int GetX();
int GetY();
bool IsLeftClicked();
bool IsRightClicked();
int GetLeftClickAtX();
int GetLeftClickAtY();
int GetRightClickAtX();
int GetRightClickAtY();
int GetRightDragDeltaX();
int GetRightDragDeltaY();
void OnMouseMove(int newX, int newY);
void OnLeftClick(int newX, int newY);
void OnleftRelease(int newX, int newY);
void OnRightClick(int newX, int newY);
void OnRightRelease(int newX, int newY);
Mouse::MouseEvent ReadEvent();
void TrimEvents();
bool IsQueueEmpty();
private:
int x;
int y;
bool isLeftClicked;
bool isRightClicked;
int leftClickAtX;
int leftClickAtY;
int rightClickAtX;
int rightClickAtY;
int rightDragDeltaX;
int rightDragDeltaY;
static constexpr unsigned int bufferSize = 16u;
std::queue<MouseEvent> mouseEvents;
};
\ No newline at end of file
...@@ -135,6 +135,7 @@ ...@@ -135,6 +135,7 @@
<ClCompile Include="DrawableBase.cpp" /> <ClCompile Include="DrawableBase.cpp" />
<ClCompile Include="Graphics.cpp" /> <ClCompile Include="Graphics.cpp" />
<ClCompile Include="Ground.cpp" /> <ClCompile Include="Ground.cpp" />
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="Sampler.cpp" /> <ClCompile Include="Sampler.cpp" />
<ClCompile Include="Surface.cpp" /> <ClCompile Include="Surface.cpp" />
<ClCompile Include="IndexBuffer.cpp" /> <ClCompile Include="IndexBuffer.cpp" />
...@@ -190,6 +191,7 @@ ...@@ -190,6 +191,7 @@
<ClInclude Include="DrawableBase.h" /> <ClInclude Include="DrawableBase.h" />
<ClInclude Include="Graphics.h" /> <ClInclude Include="Graphics.h" />
<ClInclude Include="Ground.h" /> <ClInclude Include="Ground.h" />
<ClInclude Include="Mouse.h" />
<ClInclude Include="Sampler.h" /> <ClInclude Include="Sampler.h" />
<ClInclude Include="Surface.h" /> <ClInclude Include="Surface.h" />
<ClInclude Include="IndexBuffer.h" /> <ClInclude Include="IndexBuffer.h" />
......
...@@ -120,6 +120,9 @@ ...@@ -120,6 +120,9 @@
<ClCompile Include="CoreHandler.cpp"> <ClCompile Include="CoreHandler.cpp">
<Filter>Source Files\Common</Filter> <Filter>Source Files\Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Mouse.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="PixelShader.hlsl"> <FxCompile Include="PixelShader.hlsl">
...@@ -205,5 +208,8 @@ ...@@ -205,5 +208,8 @@
<ClInclude Include="CoreHandler.h"> <ClInclude Include="CoreHandler.h">
<Filter>Header Files\Common</Filter> <Filter>Header Files\Common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Mouse.h">
<Filter>Header Files\Common</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -21,7 +21,7 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -21,7 +21,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
Window window(hWnd, hInstance, nCmdShow, 800, 600, logger); Window window(hWnd, hInstance, nCmdShow, 800, 600, logger);
// init core handler for interactive process, i.e. retrieve IO and process graphics // init core handler for interactive process, i.e. retrieve IO and process graphics
CoreHandler coreHandler(logger); CoreHandler coreHandler(window, logger);
// this struct holds Windows event messages // this struct holds Windows event messages
MSG msg; MSG msg;
...@@ -46,7 +46,7 @@ int WINAPI WinMain(HINSTANCE hInstance, ...@@ -46,7 +46,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
else else
{ {
// Retrieve IO and process graphics // Retrieve IO and process graphics
coreHandler.CoreProcess(window, window.GetGfx(), logger); coreHandler.CoreProcess(window, window.GetGfx(), hWnd, logger);
} }
} }
......
#include "Window.h" #include "Window.h"
#include "Ground.h" #include "Ground.h"
Mouse Window::mouse;
Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Logger& logger) Window::Window(HWND& hWnd, HINSTANCE& hInstance, int nCmdShow, int x, int y, Logger& logger)
{ {
// this struct holds information for the window class // this struct holds information for the window class
...@@ -86,6 +88,7 @@ LRESULT CALLBACK Window::HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPAR ...@@ -86,6 +88,7 @@ LRESULT CALLBACK Window::HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPAR
LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{ {
POINTS pt;
switch (msg) switch (msg)
{ {
// we don't want the DefProc to handle this message because // we don't want the DefProc to handle this message because
...@@ -93,6 +96,26 @@ LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noe ...@@ -93,6 +96,26 @@ LRESULT Window::HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noe
case WM_CLOSE: case WM_CLOSE:
PostQuitMessage(0); PostQuitMessage(0);
return 0; return 0;
case WM_MOUSEMOVE:
pt = MAKEPOINTS(lParam);
mouse.OnMouseMove(pt.x, pt.y);
break;
case WM_LBUTTONDOWN:
pt = MAKEPOINTS(lParam);
mouse.OnLeftClick(pt.x, pt.y);
break;
case WM_LBUTTONUP:
pt = MAKEPOINTS(lParam);
mouse.OnleftRelease(pt.x, pt.y);
break;
case WM_RBUTTONDOWN:
pt = MAKEPOINTS(lParam);
mouse.OnRightClick(pt.x, pt.y);
break;
case WM_RBUTTONUP:
pt = MAKEPOINTS(lParam);
mouse.OnRightRelease(pt.x, pt.y);
break;
} }
return DefWindowProc(hWnd, msg, wParam, lParam); return DefWindowProc(hWnd, msg, wParam, lParam);
...@@ -117,4 +140,9 @@ void Window::DrawScenes( ...@@ -117,4 +140,9 @@ void Window::DrawScenes(
{ {
section->DrawShapes(gfx, logger); section->DrawShapes(gfx, logger);
} }
}; };
\ No newline at end of file
void Window::SetTitle(HWND hWnd, const std::string& title)
{
SetWindowText(hWnd, title.c_str());
}
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "Logger.h" #include "Logger.h"
#include <vector> #include <vector>
#include "Section.h" #include "Section.h"
#include "Mouse.h"
using namespace std; using namespace std;
...@@ -26,10 +27,16 @@ public: ...@@ -26,10 +27,16 @@ public:
// Draw all scenes // Draw all scenes
void DrawScenes(Graphics& gfx, Logger& logger) noexcept; void DrawScenes(Graphics& gfx, Logger& logger) noexcept;
// Set title for test
void SetTitle(HWND hWnd, const std::string& title);
private: private:
static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept; static LRESULT CALLBACK HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept; static LRESULT CALLBACK HandleMsgThunk(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
LRESULT HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept; static LRESULT HandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept;
public:
static Mouse mouse;
private: private:
unique_ptr<Graphics> pGfx; unique_ptr<Graphics> pGfx;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment