Просмотр исходного кода

Moved GetKey/Axis status tests to protected, and made them accessible through controller directly to implement layer system in the future.

undefined
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> 9 лет назад
Родитель
Сommit
9477f1f722
7 измененных файлов: 99 добавлений и 42 удалений
  1. +8
    -8
      doc/samples/btphystest.cpp
  2. +7
    -7
      doc/samples/meshviewer.cpp
  3. +8
    -8
      doc/tutorial/07_input.cpp
  4. +6
    -6
      doc/tutorial/12_voronoi.cpp
  5. +6
    -6
      src/imgui/lolimgui.cpp
  6. +37
    -1
      src/input/controller.cpp
  7. +27
    -6
      src/input/controller.h

+ 8
- 8
doc/samples/btphystest.cpp Просмотреть файл

@@ -317,7 +317,7 @@ void BtPhysTest::TickGame(float seconds)
}


if (m_controller->GetKey(KEY_QUIT).JustReleased())
if (m_controller->WasKeyReleased(KEY_QUIT))
Ticker::Shutdown();

m_loop_value += seconds;
@@ -500,15 +500,15 @@ void BtPhysTest::TickGame(float seconds)
mat4 CtlrMx = Character->GetTransform();

vec3 movement(0.f);
movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsPressed() ? 1.f : 0.f)
- (m_controller->GetKey(KEY_MOVE_LEFT).IsPressed() ? 1.f : 0.f);
movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsPressed() ? 1.f : 0.f)
- (m_controller->GetKey(KEY_MOVE_BACK).IsPressed() ? 1.f : 0.f);
movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsPressed() ? 1.f : 0.f)
- (m_controller->GetKey(KEY_MOVE_DOWN).IsPressed() ? 1.f : 0.f);
movement.z = (m_controller->IsKeyPressed(KEY_MOVE_RIGHT) ? 1.f : 0.f)
- (m_controller->IsKeyPressed(KEY_MOVE_LEFT) ? 1.f : 0.f);
movement.x = (m_controller->IsKeyPressed(KEY_MOVE_FORWARD) ? 1.f : 0.f)
- (m_controller->IsKeyPressed(KEY_MOVE_BACK) ? 1.f : 0.f);
movement.y = (m_controller->IsKeyPressed(KEY_MOVE_UP) ? 1.f : 0.f)
- (m_controller->IsKeyPressed(KEY_MOVE_DOWN) ? 1.f : 0.f);
vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);

if (m_controller->GetKey(KEY_MOVE_JUMP).JustReleased())
if (m_controller->WasKeyReleased(KEY_MOVE_JUMP))
Character->Jump();
Character->SetMovementForFrame(CharMove);



+ 7
- 7
doc/samples/meshviewer.cpp Просмотреть файл

@@ -248,13 +248,13 @@ public:
}

#if NO_NACL_EM_INPUT
bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).JustReleased()); }
bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).JustPressed()); }
bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsPressed()); }
bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).JustReleased()); }
bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).JustPressed()); }
bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsPressed()); }
float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE)?(m_controller->GetAxis(index).GetValue()):(0.f); }
bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyReleased(index)); }
bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyPressed(index)); }
bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->IsKeyPressed(index)); }
bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyReleased(index)); }
bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyPressed(index)); }
bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->IsKeyPressed(index)); }
float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE) ? (m_controller->GetAxisValue(index)) : (0.f); }
#endif //NO_NACL_EM_INPUT

void Init()


+ 8
- 8
doc/tutorial/07_input.cpp Просмотреть файл

@@ -107,27 +107,27 @@ public:
/* Handle keyboard */
if (m_keyboard)
{
if (m_controller->GetKey(KEY_MANUAL_ROTATION).JustPressed())
if (m_controller->WasKeyPressed(KEY_MANUAL_ROTATION))
m_autorot = !m_autorot;
}

/* Handle joystick */
if (m_joystick)
{
if (lol::abs(m_controller->GetAxis(AXIS_PITCH).GetValue()) > 0.2f)
m_pitch_angle += m_controller->GetAxis(AXIS_PITCH).GetValue() * seconds * 100;
if (lol::abs(m_controller->GetAxis(AXIS_YAW).GetValue()) > 0.2f)
m_yaw_angle += m_controller->GetAxis(AXIS_YAW).GetValue() * seconds * 100;
if (lol::abs(m_controller->GetAxisValue(AXIS_PITCH)) > 0.2f)
m_pitch_angle += m_controller->GetAxisValue(AXIS_PITCH) * seconds * 100;
if (lol::abs(m_controller->GetAxisValue(AXIS_YAW)) > 0.2f)
m_yaw_angle += m_controller->GetAxisValue(AXIS_YAW) * seconds * 100;
}

/* Handle mouse */
if (m_mouse)
{
if (m_controller->GetKey(KEY_DRAG_MESH).IsPressed())
if (m_controller->IsKeyPressed(KEY_DRAG_MESH))
{
InputDevice::CaptureMouse(true);
m_pitch_angle -= m_controller->GetAxis(AXIS_DRAG_PITCH).GetValue() * seconds * 100;
m_yaw_angle += m_controller->GetAxis(AXIS_DRAG_YAW).GetValue() * seconds * 100;
m_pitch_angle -= m_controller->GetAxisValue(AXIS_DRAG_PITCH) * seconds * 100;
m_yaw_angle += m_controller->GetAxisValue(AXIS_DRAG_YAW) * seconds * 100;
}
else
{


+ 6
- 6
doc/tutorial/12_voronoi.cpp Просмотреть файл

@@ -78,7 +78,7 @@ public:

{
//Shutdown logic
if (m_controller->GetKey(KEY_ESC).JustReleased())
if (m_controller->WasKeyReleased(KEY_ESC))
Ticker::Shutdown();
}

@@ -169,16 +169,16 @@ public:

{
//Shutdown logic
if (m_controller->GetKey(KEY_POP).JustReleased())
if (m_controller->WasKeyReleased(KEY_POP))
voronoi_points.Pop();
else if (m_controller->GetKey(KEY_PUSH).JustReleased())
else if (m_controller->WasKeyReleased(KEY_PUSH))
voronoi_points.Push(vec3(rand<float>(512.f), rand<float>(512.f), .0f),
vec2(64.f + rand<float>(64.f), 64.f + rand<float>(64.f)));
else if (m_controller->GetKey(KEY_F1).JustReleased())
else if (m_controller->WasKeyReleased(KEY_F1))
m_cur_fbo = SrcVoronoiFbo;
else if (m_controller->GetKey(KEY_F2).JustReleased())
else if (m_controller->WasKeyReleased(KEY_F2))
m_cur_fbo = VoronoiFbo;
else if (m_controller->GetKey(KEY_F3).JustReleased())
else if (m_controller->WasKeyReleased(KEY_F3))
{
voronoi_points.Empty();
if (mode == 0)


+ 6
- 6
src/imgui/lolimgui.cpp Просмотреть файл

@@ -197,15 +197,15 @@ void LolImGui::TickGame(float seconds)
switch (i)
{
default:
io.KeysDown[i] = m_controller->GetKey(i).IsPressed();
io.KeysDown[i] = m_controller->IsKeyPressed(i);
break;
case LolImGuiKey::LShift:
case LolImGuiKey::RShift:
io.KeyShift = (io.KeyShift || m_controller->GetKey(i).IsPressed());
io.KeyShift = (io.KeyShift || m_controller->IsKeyPressed(i));
break;
case LolImGuiKey::LCtrl:
case LolImGuiKey::RCtrl:
io.KeyCtrl = (io.KeyCtrl || m_controller->GetKey(i).IsPressed());
io.KeyCtrl = (io.KeyCtrl || m_controller->IsKeyPressed(i));
break;
}
}
@@ -223,17 +223,17 @@ void LolImGui::TickGame(float seconds)
cursor.y = 1.f - cursor.y;
cursor *= video_size;
io.MousePos = ImVec2(cursor.x, cursor.y);
io.MouseWheel = m_controller->GetAxis(LolImGuiAxis::Scroll).GetValue();
io.MouseWheel = m_controller->GetAxisValue(LolImGuiAxis::Scroll);

for (int i = LolImGuiKey::MOUSE_KEY_START; i < LolImGuiKey::MOUSE_KEY_END; ++i)
{
switch (i)
{
default:
io.MouseDown[i - LolImGuiKey::MOUSE_KEY_START] = m_controller->GetKey(i).IsPressed();
io.MouseDown[i - LolImGuiKey::MOUSE_KEY_START] = m_controller->IsKeyPressed(i);
break;
case LolImGuiKey::Focus:
if (m_controller->GetKey(i).IsPressed())
if (m_controller->IsKeyPressed(i))
io.MousePos = ImVec2(-1.f, -1.f);
break;
}


+ 37
- 1
src/input/controller.cpp Просмотреть файл

@@ -295,13 +295,49 @@ bool Controller::IsLayerActive()
//GetKeys/Axis stuff ----------------------------------------------------------
KeyBinding& Controller::GetKey(int index)
{
//#error do something better IsLayerActive()
return m_keys[index];
}
KeyBinding const& Controller::GetKey(int index) const
{
return m_keys[index];
}
AxisBinding& Controller::GetAxis(int index)
{
return m_axis[index];
}
AxisBinding const& Controller::GetAxis(int index) const
{
return m_axis[index];
}

//Key methods: should not go directly to binding ------------------------------
bool Controller::IsKeyPressed(int index) const
{
//#error do something better IsLayerActive()
return GetKey(index).IsPressed();
}
bool Controller::IsKeyReleased(int index) const
{
return GetKey(index).IsReleased();
}
bool Controller::WasKeyPressed(int index) const
{
return GetKey(index).WasKeyPressed();
}
bool Controller::WasKeyReleased(int index) const
{
return GetKey(index).WasKeyReleased();
}

//Axis methods: should not go directly to binding -----------------------------
float Controller::GetAxisValue(int index) const
{
return GetAxis(index).GetValue();
}
float Controller::GetAxisDelta(int index) const
{
return GetAxis(index).GetDelta();
}

//-----------------------------------------------------------------------------
Controller* Controller::Get(String const &name)


+ 27
- 6
src/input/controller.h Просмотреть файл

@@ -16,22 +16,25 @@ namespace lol
//-----------------------------------------------------------------------------
class KeyBinding
{
friend class Controller;
public:
KeyBinding()
: m_current(false),
m_previous(false)
{}

protected:
//Status methods ----------------------------------------------------------
/** Indicates wheither the key is currently down */
bool IsPressed() const { return m_current; }
/** Indicates wheither the key is currently up */
bool IsReleased() const { return !m_current; }
/** Indicates wheither the key has just been pressed */
bool JustPressed() const { return m_current && !m_previous; }
bool WasKeyPressed() const { return m_current && !m_previous; }
/** Indicates wheither the key has just been released */
bool JustReleased() const { return !m_current && m_previous; }
bool WasKeyReleased() const { return !m_current && m_previous; }

public:
//Binding methods ---------------------------------------------------------
/** Bind a physical device and key */
void Bind(const String& device_name, const String& key_name);
@@ -67,25 +70,26 @@ protected:
bool m_current;
/** Value at the current frame */
bool m_previous;

friend class Controller;
};

//-----------------------------------------------------------------------------
class AxisBinding
{
friend class Controller;
public:
AxisBinding()
: m_current(0.0f),
m_previous(0.0f)
{}

protected:
//Status methods ----------------------------------------------------------
/** Gets the current absolute value of this axis */
float GetValue() const { return m_current; }
/** Gets the current delta value of this axis */
float GetDelta() const { return m_current - m_previous; }

public:
//Binding methods ---------------------------------------------------------
/** Bind a physical device and axis */
void Bind(const String& device_name, const String& axis_name);
@@ -132,8 +136,6 @@ protected:
array<const InputDevice*, int, int> m_keybindings;
float m_current;
float m_previous;

friend class Controller;
};

//-------------------------------------------------------------------------
@@ -348,8 +350,27 @@ public:

/** GetKeys/Axis stuff */
KeyBinding& GetKey(int index);
KeyBinding const& GetKey(int index) const;
AxisBinding& GetAxis(int index);
AxisBinding const& GetAxis(int index) const;

/** Key methods: should not go directly to binding */
/** Indicates wheither the key is currently down */
bool IsKeyPressed(int index) const;
/** Indicates wheither the key is currently up */
bool IsKeyReleased(int index) const;
/** Indicates wheither the key has just been pressed */
bool WasKeyPressed(int index) const;
/** Indicates wheither the key has just been released */
bool WasKeyReleased(int index) const;

/** Axis methods: should not go directly to binding */
/** Gets the current absolute value of this axis */
float GetAxisValue(int index) const;
/** Gets the current delta value of this axis */
float GetAxisDelta(int index) const;

/** Get named controller */
static Controller* Get(String const &name);

protected:


Загрузка…
Отмена
Сохранить