Pārlūkot izejas kodu

Input : Small refactor, for clarity, easiness and beautification.

Constants : Added minus one.
undefined
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> pirms 11 gadiem
vecāks
revīzija
747da72c44
10 mainītis faili ar 129 papildinājumiem un 55 dzēšanām
  1. +31
    -31
      src/input/controller.cpp
  2. +20
    -8
      src/input/controller.h
  3. +4
    -2
      src/input/input.cpp
  4. +3
    -0
      src/input/input.h
  5. +1
    -0
      src/input/input_internal.h
  6. +3
    -0
      src/lol/math/vector.h
  7. +25
    -1
      src/lolcore.vcxproj
  8. +28
    -0
      src/lolcore.vcxproj.filters
  9. +3
    -0
      src/math/constants.cpp
  10. +11
    -13
      src/platform/sdl/sdlinput.cpp

+ 31
- 31
src/input/controller.cpp Parādīt failu

@@ -20,33 +20,33 @@ namespace lol
///////////////////////////////////////////////////////////////////////////////
// KeyBinding

void KeyBinding::Bind(const char* device_name, const char* key_name)
void KeyBinding::Bind(const String& device_name, const String& key_name)
{
const InputDevice* device = InputDevice::Get(device_name);
const InputDevice* device = InputDevice::Get(device_name.C());

if (!device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C());
return;
}

int keyindex = device->GetKeyIndex(key_name);
int keyindex = device->GetKeyIndex(key_name.C());
if (keyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name.C(), key_name.C());
return;
}

m_keybindings.Push(device, keyindex);
}

bool KeyBinding::Unbind(const char* device_name, const char* key_name)
bool KeyBinding::Unbind(const String& device_name, const String& key_name)
{
for (int i = 0; i < m_keybindings.Count(); ++i)
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name))
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(key_name.C()))
{
m_keybindings.Remove(i);
return true;
@@ -64,77 +64,77 @@ void KeyBinding::ClearBindings()
///////////////////////////////////////////////////////////////////////////////
// AxisBinding

void AxisBinding::Bind(const char* device_name, const char* axis_name)
void AxisBinding::Bind(const String& device_name, const String& axis_name)
{
const InputDevice* device = InputDevice::Get(device_name);
const InputDevice* device = InputDevice::Get(device_name.C());
if (!device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C());
return;
}

int axisindex = device->GetAxisIndex(axis_name);
int axisindex = device->GetAxisIndex(axis_name.C());
if (axisindex < 0)
{
Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name);
Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name.C(), axis_name.C());
return;
}

m_axisbindings.Push(device, axisindex);
}

void AxisBinding::BindKey(const char* device_name, const char* key_name)
void AxisBinding::BindKey(const String& device_name, const String& key_name)
{
const InputDevice* device = InputDevice::Get(device_name);
const InputDevice* device = InputDevice::Get(device_name.C());
if (!device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C());
return;
}

int keyindex = device->GetKeyIndex(key_name);
int keyindex = device->GetKeyIndex(key_name.C());
if (keyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name.C(), key_name.C());
return;
}

m_keybindings.Push(device, -1, keyindex);
}

void AxisBinding::BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name)
void AxisBinding::BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name)
{
const InputDevice* device = InputDevice::Get(device_name);
const InputDevice* device = InputDevice::Get(device_name.C());
if (!device)
{
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name.C());
return;
}

int minkeyindex = device->GetKeyIndex(min_key_name);
int minkeyindex = device->GetKeyIndex(min_key_name.C());
if (minkeyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name);
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name.C());
return;
}

int maxkeyindex = device->GetKeyIndex(max_key_name);
int maxkeyindex = device->GetKeyIndex(max_key_name.C());
if (maxkeyindex < 0)
{
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name);
Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name.C());
return;
}

m_keybindings.Push(device, minkeyindex, maxkeyindex);
}

bool AxisBinding::Unbind(const char* device_name, const char* axis_name)
bool AxisBinding::Unbind(const String& device_name, const String& axis_name)
{
for (int i = 0; i < m_keybindings.Count(); ++i)
{
if (m_axisbindings[i].m1->GetName() == device_name)
{
if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name))
if (m_axisbindings[i].m2 == m_axisbindings[i].m1->GetAxisIndex(axis_name.C()))
{
m_axisbindings.Remove(i);
return true;
@@ -144,13 +144,13 @@ bool AxisBinding::Unbind(const char* device_name, const char* axis_name)
return false;
}

bool AxisBinding::UnbindKey(const char* device_name, const char* key_name)
bool AxisBinding::UnbindKey(const String& device_name, const String& key_name)
{
for (int i = 0; i < m_keybindings.Count(); ++i)
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == -1 && m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(key_name))
if (m_keybindings[i].m2 == -1 && m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(key_name.C()))
{
m_keybindings.Remove(i);
return true;
@@ -160,14 +160,14 @@ bool AxisBinding::UnbindKey(const char* device_name, const char* key_name)
return false;
}

bool AxisBinding::UnbindKeys(const char* device_name, const char* min_key_name, const char* max_key_name)
bool AxisBinding::UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name)
{
for (int i = 0; i < m_keybindings.Count(); ++i)
{
if (m_keybindings[i].m1->GetName() == device_name)
{
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(min_key_name)
&& m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(max_key_name))
if (m_keybindings[i].m2 == m_keybindings[i].m1->GetKeyIndex(min_key_name.C())
&& m_keybindings[i].m3 == m_keybindings[i].m1->GetKeyIndex(max_key_name.C()))
{
m_keybindings.Remove(i);
return true;


+ 20
- 8
src/input/controller.h Parādīt failu

@@ -34,9 +34,14 @@ public:
bool IsReleased() const { return !m_current && m_previous; }

/** Bind a physical device and key */
void Bind(const char* device_name, const char* key_name);
void Bind(const String& device_name, const String& key_name);
/** Unbind a previously bound physical device and key. Returns true if the binding was existing. */
bool Unbind(const char* device_name, const char* key_name);
bool Unbind(const String& device_name, const String& key_name);
/* Small helpers */
void BindMouse(const String& key_name) { Bind(g_name_mouse, key_name); }
void BindKeyboard(const String& key_name) { Bind(g_name_keyboard, key_name); }
bool UnbindMouse(const String& key_name) { return Unbind(g_name_mouse, key_name); }
bool UnbindKeyboard(const String& key_name) { return Unbind(g_name_mouse, key_name); }
/** Clear current binding */
void ClearBindings();
/** Indicate wheither a physical device and key has been bound. Returns the number of bindings set. */
@@ -78,17 +83,24 @@ public:
float GetDelta() const { return m_current - m_previous; }
/** Bind a physical device and axis */
void Bind(const char* device_name, const char* axis_name);
void Bind(const String& device_name, const String& axis_name);
/** Bind a physical device and key over this axis. The axis value will be 0 if the key is up and 1 if it's down */
void BindKey(const char* device_name, const char* key_name);
void BindKey(const String& device_name, const String& key_name);
/** Bind physical device and keys over this axis. The axis value will be 0 if both the key are up, -1 if minkey is down, and 1 if maxkey is down */
void BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name);
void BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name);
/** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */
bool Unbind(const char* device_name, const char* axis_name);
bool Unbind(const String& device_name, const String& axis_name);
/** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */
bool UnbindKey(const char* device_name, const char* key_name);
bool UnbindKey(const String& device_name, const String& key_name);
/** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */
bool UnbindKeys(const char* device_name, const char* min_key_name, const char* max_key_name);
bool UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name);
/* Small helpers */
void BindMouse(const String& axis_name) { Bind(g_name_mouse, axis_name); }
void BindMouseKey(const String& key_name) { BindKey(g_name_mouse, key_name); }
void BindMouseKeys(const String& min_key_name, const String& max_key_name) { BindKeys(g_name_mouse, min_key_name, max_key_name); }
bool UnbindMouse(const String& axis_name) { return Unbind(g_name_mouse, axis_name); }
bool UnbindMouseKey(const String& key_name) { return UnbindKey(g_name_mouse, key_name); }
bool UnbindMouseKeys(const String& min_key_name, const String& max_key_name){ return UnbindKeys(g_name_mouse, min_key_name, max_key_name); }
/** Clear current binding */
void ClearBindings();
/** Indicate wheither a physical device and axis has been bound. Returns the number of bindings set. */


+ 4
- 2
src/input/input.cpp Parādīt failu

@@ -52,7 +52,7 @@ void InputDeviceInternal::AddCursor(const char* name)

InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard()
{
InputDeviceInternal* keyboard = new InputDeviceInternal("Keyboard");
InputDeviceInternal* keyboard = new InputDeviceInternal(g_name_keyboard.C());
/* "value" is unused, what matters is the index. */
# define KEY_FUNC(key, value) \
keyboard->AddKey(#key);
@@ -63,10 +63,12 @@ InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard()

InputDeviceInternal* InputDeviceInternal::CreateStandardMouse()
{
InputDeviceInternal* mouse = new InputDeviceInternal("Mouse");
InputDeviceInternal* mouse = new InputDeviceInternal(g_name_mouse.C());
mouse->AddKey("Left");
mouse->AddKey("Middle");
mouse->AddKey("Right");
//Added to manage if mouse is in the screen or not.
mouse->AddKey("InScreen");

mouse->AddAxis("X");
mouse->AddAxis("Y");


+ 3
- 0
src/input/input.h Parādīt failu

@@ -16,6 +16,9 @@
namespace lol
{

static const String g_name_mouse("Mouse");
static const String g_name_keyboard("Keyboard");

class InputDevice
{
public:


+ 1
- 0
src/input/input_internal.h Parādīt failu

@@ -29,6 +29,7 @@ public:
void SetKey(int index, bool state) { m_keys[index] = state; }
void SetAxis(int index, float value) { m_axis[index].m1 = value; }
void SetCursor(int index, const vec2& position, const ivec2& pixel) { m_cursors[index].m1 = position; m_cursors[index].m2 = pixel; }
ivec2 GetCursorPixelPos(int index) { return m_cursors[index].m2; }

static bool GetMouseCapture() { return m_capturemouse; }



+ 3
- 0
src/lol/math/vector.h Parādīt failu

@@ -251,6 +251,7 @@ template <typename T> struct Vec2 : BVec2<T>

LOL_MEMBER_OPS(Vec2, x)

static const Vec2<T> mone;
static const Vec2<T> one;
static const Vec2<T> zero;
static const Vec2<T> axis_x;
@@ -525,6 +526,7 @@ template <typename T> struct Vec3 : BVec3<T>

LOL_MEMBER_OPS(Vec3, x)

static const Vec3<T> mone;
static const Vec3<T> one;
static const Vec3<T> zero;
static const Vec3<T> axis_x;
@@ -948,6 +950,7 @@ template <typename T> struct Vec4 : BVec4<T>

LOL_MEMBER_OPS(Vec4, x)

static const Vec4<T> mone;
static const Vec4<T> one;
static const Vec4<T> zero;
static const Vec4<T> axis_x;


+ 25
- 1
src/lolcore.vcxproj Parādīt failu

@@ -159,6 +159,18 @@
<ClCompile Include="messageservice.cpp" />
<ClCompile Include="platform.cpp" />
<ClCompile Include="platform\d3d9\d3d9input.cpp" />
<ClCompile Include="platform\nacl\nacl-app.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="platform\nacl\nacl-instance.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="platform\nacl\nacl-module.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="platform\nacl\opengl_context.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="platform\ps3\ps3app.cpp" />
<ClCompile Include="platform\ps3\ps3input.cpp" />
<ClCompile Include="platform\sdl\sdlapp.cpp" />
@@ -259,6 +271,18 @@
<ClInclude Include="numeric.h" />
<ClInclude Include="platform.h" />
<ClInclude Include="platform\d3d9\d3d9input.h" />
<ClInclude Include="platform\nacl\nacl-app.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="platform\nacl\nacl-instance.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="platform\nacl\opengl_context.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="platform\nacl\opengl_context_ptrs.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="platform\ps3\ps3app.h" />
<ClInclude Include="platform\ps3\ps3input.h" />
<ClInclude Include="platform\ps3\threadbase.h" />
@@ -315,4 +339,4 @@
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)\Lol.Fx.targets" />
</ImportGroup>
</Project>
</Project>

+ 28
- 0
src/lolcore.vcxproj.filters Parādīt failu

@@ -79,6 +79,9 @@
<Filter Include="platform\ps3">
<UniqueIdentifier>{c6c6b597-ed6c-4d82-a166-964beeeeb525}</UniqueIdentifier>
</Filter>
<Filter Include="platform\NaCl">
<UniqueIdentifier>{f6cc3470-c841-4581-969b-e60cea841c27}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="image\image.cpp">
@@ -312,6 +315,19 @@
<ClCompile Include="MessageService.cpp">
<Filter>...</Filter>
</ClCompile>
<ClCompile Include="math\constants.cpp" />
<ClCompile Include="platform\nacl\nacl-app.cpp">
<Filter>platform\NaCl</Filter>
</ClCompile>
<ClCompile Include="platform\nacl\nacl-instance.cpp">
<Filter>platform\NaCl</Filter>
</ClCompile>
<ClCompile Include="platform\nacl\nacl-module.cpp">
<Filter>platform\NaCl</Filter>
</ClCompile>
<ClCompile Include="platform\nacl\opengl_context.cpp">
<Filter>platform\NaCl</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="debug\fps.h">
@@ -617,6 +633,18 @@
<ClInclude Include="messageservice.h">
<Filter>...</Filter>
</ClInclude>
<ClInclude Include="platform\nacl\nacl-app.h">
<Filter>platform\NaCl</Filter>
</ClInclude>
<ClInclude Include="platform\nacl\nacl-instance.h">
<Filter>platform\NaCl</Filter>
</ClInclude>
<ClInclude Include="platform\nacl\opengl_context.h">
<Filter>platform\NaCl</Filter>
</ClInclude>
<ClInclude Include="platform\nacl\opengl_context_ptrs.h">
<Filter>platform\NaCl</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<LolFxCompile Include="gpu\emptymaterial.lolfx">


+ 3
- 0
src/math/constants.cpp Parādīt failu

@@ -30,17 +30,20 @@ namespace lol {

#define LOL_ALL_VECTOR_CONST_INNER(type) \
LOL_VEC_2_CONST(type, one, 1, 1) \
LOL_VEC_2_CONST(type, mone,-1,-1) \
LOL_VEC_2_CONST(type, zero, 0, 0) \
LOL_VEC_2_CONST(type, axis_x, 1, 0) \
LOL_VEC_2_CONST(type, axis_y, 0, 1) \
\
LOL_VEC_3_CONST(type, one, 1, 1, 1) \
LOL_VEC_3_CONST(type, mone,-1,-1,-1) \
LOL_VEC_3_CONST(type, zero, 0, 0, 0) \
LOL_VEC_3_CONST(type, axis_x, 1, 0, 0) \
LOL_VEC_3_CONST(type, axis_y, 0, 1, 0) \
LOL_VEC_3_CONST(type, axis_z, 0, 0, 1) \
\
LOL_VEC_4_CONST(type, one, 1, 1, 1, 1) \
LOL_VEC_4_CONST(type, mone,-1,-1,-1,-1) \
LOL_VEC_4_CONST(type, zero, 0, 0, 0, 0) \
LOL_VEC_4_CONST(type, axis_x, 1, 0, 0, 0) \
LOL_VEC_4_CONST(type, axis_y, 0, 1, 0, 0) \


+ 11
- 13
src/platform/sdl/sdlinput.cpp Parādīt failu

@@ -49,10 +49,8 @@ private:
#if USE_SDL
SdlInputData(int app_w, int app_h, int screen_w, int screen_h) :
m_prevmouse(ivec2::zero),
m_app_w((float)app_w),
m_app_h((float)app_h),
m_screen_w((float)screen_w),
m_screen_h((float)screen_h),
m_app(vec2((float)app_w, (float)app_h)),
m_screen(vec2((float)screen_w, (float)screen_h)),
m_mousecapture(false)
{ }

@@ -61,10 +59,8 @@ private:
InputDeviceInternal* m_keyboard;

ivec2 m_prevmouse;
float m_app_w;
float m_app_h;
float m_screen_w;
float m_screen_h;
vec2 m_app;
vec2 m_screen;
bool m_mousecapture;
#endif // USE_SDL
};
@@ -222,18 +218,20 @@ void SdlInputData::Tick(float seconds)
//SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE);
}

if (mouse.x >= 0 && mouse.x < m_app_w && mouse.y >= 0 && mouse.y < m_app_h)
if (mouse.x >= 0 && mouse.x < m_app.x && mouse.y >= 0 && mouse.y < m_app.y)
{
m_mouse->SetCursor(0, vec2((float)(mouse.x) / m_app_w, (float)(mouse.y) / m_app_h), mouse);
vec2 vmouse = vec2(mouse);
vec2 vprevmouse = vec2(m_prevmouse);
m_mouse->SetCursor(0, vmouse / m_app, mouse);
// Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick
m_mouse->SetAxis(0, (float)(mouse.x - m_prevmouse.x) * 100.0f / m_screen_w);
m_mouse->SetAxis(0, (mouse.x - vprevmouse.x) * 100.0f / m_screen.x);
// Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction)
m_mouse->SetAxis(1, -(float)(mouse.y - m_prevmouse.y) * 100.0f / m_screen_h);
m_mouse->SetAxis(1,-(mouse.y - vprevmouse.y) * 100.0f / m_screen.y);
}

if (m_mousecapture)
{
mouse = ivec2((int)m_app_w / 2, (int)m_app_h / 2);
mouse = ivec2(m_app * .5f);
SdlInputData::SetMousePos(mouse);
}



Notiek ielāde…
Atcelt
Saglabāt