fov scrolling
This commit is contained in:
parent
3bd06263f1
commit
f7505def53
|
@ -2,6 +2,11 @@
|
|||
|
||||
#include <Orange/Core/Bitset.h>
|
||||
|
||||
namespace orange
|
||||
{
|
||||
class Window;
|
||||
}
|
||||
|
||||
namespace orange::Input
|
||||
{
|
||||
namespace ButtonCodes
|
||||
|
@ -123,17 +128,22 @@ namespace orange::Input
|
|||
Mouse_3, // Middle Click
|
||||
Mouse_4,
|
||||
Mouse_5,
|
||||
Mouse_WheelUp,
|
||||
Mouse_WheelDown,
|
||||
|
||||
Count,
|
||||
};
|
||||
}
|
||||
using ButtonCode = ButtonCodes::ButtonCode;
|
||||
|
||||
struct MouseDelta
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
};
|
||||
|
||||
class InputHandler
|
||||
{
|
||||
public:
|
||||
|
||||
bool get(ButtonCode code)
|
||||
{
|
||||
return m_pressed.get(code);
|
||||
|
@ -147,6 +157,23 @@ namespace orange::Input
|
|||
return m_firstPressed.get(code);
|
||||
}
|
||||
|
||||
MouseDelta getMouseMotionDelta()
|
||||
{
|
||||
MouseDelta delta = m_motionDelta;
|
||||
m_motionDelta = MouseDelta{};
|
||||
return delta;
|
||||
}
|
||||
|
||||
MouseDelta getMouseScrollDelta()
|
||||
{
|
||||
MouseDelta delta = m_scrollDelta;
|
||||
m_scrollDelta = MouseDelta{};
|
||||
return delta;
|
||||
}
|
||||
|
||||
protected:
|
||||
friend Window;
|
||||
|
||||
void set(ButtonCode code, bool pressed)
|
||||
{
|
||||
m_firstPressed.set(code, pressed);
|
||||
|
@ -157,10 +184,27 @@ namespace orange::Input
|
|||
{
|
||||
m_firstPressed.clearAll();
|
||||
}
|
||||
|
||||
void addMouseMotionDelta(MouseDelta delta)
|
||||
{
|
||||
m_motionDelta.x += delta.x;
|
||||
m_motionDelta.y += delta.y;
|
||||
}
|
||||
|
||||
void addMouseScrollDelta(MouseDelta delta)
|
||||
{
|
||||
m_scrollDelta.x += delta.x;
|
||||
m_scrollDelta.y += delta.y;
|
||||
}
|
||||
|
||||
private:
|
||||
Bitset<ButtonCodes::Count> m_pressed;
|
||||
Bitset<ButtonCodes::Count> m_firstPressed;
|
||||
|
||||
MouseDelta m_motionDelta = {};
|
||||
MouseDelta m_scrollDelta = {};
|
||||
};
|
||||
|
||||
ButtonCode SDLScancodeToButton(int scancode);
|
||||
ButtonCode SDLMouseButtonToButtonCode(int button);
|
||||
}
|
||||
|
|
|
@ -9,12 +9,6 @@ struct SDL_Window;
|
|||
|
||||
namespace orange
|
||||
{
|
||||
struct MouseDelta
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
};
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
|
@ -31,8 +25,6 @@ namespace orange
|
|||
bool Update(Input::InputHandler& handler);
|
||||
|
||||
void EnableRelativeMouse(bool relative);
|
||||
|
||||
MouseDelta GetMouseDelta();
|
||||
protected:
|
||||
friend Result<Window>;
|
||||
Window(SDL_Window* window);
|
||||
|
@ -40,7 +32,5 @@ namespace orange
|
|||
SDL_Window* m_window = nullptr;
|
||||
bool m_relativeMouse = false;
|
||||
bool m_hasFocus = false;
|
||||
|
||||
MouseDelta m_mouseDelta = {};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -618,7 +618,8 @@ int main(int argc, char** argv)
|
|||
t1 = t2;
|
||||
|
||||
{
|
||||
auto mouseDelta = r_window->GetMouseDelta();
|
||||
auto mouseDelta = handler.getMouseMotionDelta();
|
||||
auto scrollDelta = handler.getMouseScrollDelta();
|
||||
//if (mouseDelta.x || mouseDelta.y)
|
||||
//{
|
||||
// log::info("mouse: %d %d", mouseDelta.x, mouseDelta.y);
|
||||
|
@ -638,6 +639,12 @@ int main(int argc, char** argv)
|
|||
|
||||
const float movementSpeed = handler.get(Input::ButtonCodes::Key_LShift) ? 64.0f : 16.0f;
|
||||
|
||||
if (scrollDelta.y)
|
||||
{
|
||||
camera.fieldOfView += Radian(5_deg) * -scrollDelta.y;
|
||||
camera.fieldOfView = Clamp(camera.fieldOfView, Radian(5_deg), Radian(180_deg));
|
||||
}
|
||||
|
||||
if (handler.get(Input::ButtonCodes::Key_W))
|
||||
camera.transform.position += camera.forward() * movementSpeed * dt;
|
||||
else if (handler.get(Input::ButtonCodes::Key_S))
|
||||
|
|
|
@ -69,4 +69,19 @@ namespace orange::Input
|
|||
{
|
||||
return s_scancodeToKey[scancode];
|
||||
}
|
||||
|
||||
ButtonCode SDLMouseButtonToButtonCode(int button)
|
||||
{
|
||||
switch (button)
|
||||
{
|
||||
case SDL_BUTTON_LEFT: return ButtonCodes::Mouse_1;
|
||||
case SDL_BUTTON_RIGHT: return ButtonCodes::Mouse_2;
|
||||
case SDL_BUTTON_MIDDLE: return ButtonCodes::Mouse_3;
|
||||
case SDL_BUTTON_X1: return ButtonCodes::Mouse_4;
|
||||
case SDL_BUTTON_X2: return ButtonCodes::Mouse_5;
|
||||
default:
|
||||
//Assert(false);
|
||||
return ButtonCodes::Mouse_1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,14 +71,18 @@ namespace orange
|
|||
{
|
||||
case SDL_MOUSEMOTION:
|
||||
if (m_hasFocus)
|
||||
{
|
||||
m_mouseDelta.x += e.motion.xrel;
|
||||
m_mouseDelta.y += e.motion.yrel;
|
||||
}
|
||||
handler.addMouseMotionDelta(Input::MouseDelta{ e.motion.xrel, e.motion.yrel });
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
handler.set(Input::SDLScancodeToButton(e.key.keysym.scancode), e.type == SDL_KEYDOWN);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
handler.set(Input::SDLMouseButtonToButtonCode(e.button.button), e.type == SDL_MOUSEBUTTONDOWN);
|
||||
break;
|
||||
case SDL_MOUSEWHEEL:
|
||||
handler.addMouseScrollDelta(Input::MouseDelta{ e.wheel.x, e.wheel.y});
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (e.window.event)
|
||||
|
@ -113,11 +117,4 @@ namespace orange
|
|||
SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE);
|
||||
SDL_ShowCursor(m_hasFocus && relative ? SDL_FALSE : SDL_TRUE);
|
||||
}
|
||||
|
||||
MouseDelta Window::GetMouseDelta()
|
||||
{
|
||||
MouseDelta delta = m_mouseDelta;
|
||||
m_mouseDelta = MouseDelta{};
|
||||
return delta;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue