Pārlūkot izejas kodu

input: minor cleanup.

legacy
Sam Hocevar pirms 5 gadiem
vecāks
revīzija
a713297cfb
7 mainītis faili ar 25 papildinājumiem un 35 dzēšanām
  1. +2
    -2
      doc/tutorial/07_input.cpp
  2. +1
    -1
      doc/tutorial/11_fractal.cpp
  3. +1
    -1
      src/application/android-app.cpp
  4. +1
    -1
      src/ui/gui.cpp
  5. +1
    -1
      src/ui/input.cpp
  6. +18
    -28
      src/ui/input.h
  7. +1
    -1
      src/ui/sdl-input.cpp

+ 2
- 2
doc/tutorial/07_input.cpp Parādīt failu

@@ -135,8 +135,8 @@ public:
auto mouse = input::get()->mouse();
m_text->SetText(lol::format(
"cursor: (%0.3f, %0.3f) - pixel (%d, %d)",
mouse->GetCursor(0).x, mouse->GetCursor(0).y,
mouse->GetCursorPixel(0).x, mouse->GetCursorPixel(0).y));
mouse->get_cursor_uv(0).x, mouse->get_cursor_uv(0).y,
mouse->get_cursor_pixel(0).x, mouse->get_cursor_pixel(0).y));
}
else
{


+ 1
- 1
doc/tutorial/11_fractal.cpp Parādīt failu

@@ -157,7 +157,7 @@ public:
{
WorldEntity::tick_game(seconds);

ivec2 mousepos = input::get()->mouse()->GetCursorPixel(0);
ivec2 mousepos = input::get()->mouse()->get_cursor_pixel(0);

int prev_frame = (m_frame + 4) % 4;
m_frame = (m_frame + 1) % 4;


+ 1
- 1
src/application/android-app.cpp Parādīt failu

@@ -206,7 +206,7 @@ int32_t lol::AndroidAppData::HandleInput(AInputEvent* event)
AMotionEvent_getY(event, 0));
pos *= m_wanted_resolution / Video::GetSize();
pos.y = m_wanted_resolution.y - 1 - pos.y;
mouse->SetCursor(0, vec2(pos) / vec2(m_wanted_resolution), pos);
mouse->internal_set_cursor(0, vec2(pos) / vec2(m_wanted_resolution), pos);
// Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick
mouse->internal_set_axis(0, (pos.x - m_prev_pos.x) / max_screen_size * 100.f);
// Unlike SDL, no need to negate Y axis


+ 1
- 1
src/ui/gui.cpp Parādīt failu

@@ -252,7 +252,7 @@ void gui::tick_game(float seconds)
keyboard->capture_text(io.WantTextInput);

// Update mouse
vec2 cursor = mouse->GetCursor(0);
vec2 cursor = mouse->get_cursor_uv(0);
cursor.y = 1.f - cursor.y;

io.MousePos = cursor * video_size;


+ 1
- 1
src/ui/input.cpp Parādīt failu

@@ -154,7 +154,7 @@ void InputDevice::AddCursor(int index, const char* name)
while (index >= (int)m_cursor_names.size())
{
m_cursor_names.push_back("");
m_cursors.push(vec2::zero, ivec2::zero);
m_cursors.push_back(cursor_state());
}

m_cursor_names[index] = name;


+ 18
- 28
src/ui/input.h Parādīt failu

@@ -65,23 +65,17 @@ public:
}

/** Gets the index of the corresponding key, needed to call GetKey */
ptrdiff_t GetKeyIndex(std::string const &name) const
int GetKeyIndex(std::string const &name) const
{
return GetItemIndex(name, m_key_names);
}

/** Gets the index of the corresponding axis, needed to call GetAxis */
ptrdiff_t GetAxisIndex(std::string const &name) const
int GetAxisIndex(std::string const &name) const
{
return GetItemIndex(name, m_axis_names);
}

/** Gets the index of the corresponding cursor, needed to call GetCursor */
ptrdiff_t GetCursorIndex(std::string const &name) const
{
return GetItemIndex(name, m_cursor_names);
}

//
// Keyboard-specific section
//
@@ -94,7 +88,7 @@ public:

/** Get the current state of the given key, true being pressed and
* false being released */
bool key(ptrdiff_t index) const { return m_keys[index]; }
bool key(int index) const { return m_keys[index]; }

/** Gets the latest contents of text input. */
std::string text();
@@ -106,29 +100,29 @@ public:

/** Gets the current value of the given axis. Devices should try to
* clamp this value between -1 and 1, though it is not guaranteed. */
float GetAxis(ptrdiff_t index) const
float GetAxis(int index) const
{
return m_axis[index].m1 * m_axis[index].m2;
}

/** Gets the current value of the given cursor, 0,0 being the bottom-left
* corner and 1,1 being the top-right corner */
vec2 GetCursor(ptrdiff_t index) const
vec2 get_cursor_uv(int index) const
{
return m_cursors[index].m1;
return m_cursors[index].uv;
}

/** Gets the coordinate of the pixel the cursor is currently over,
* 0,0 being the bottom-left corner. */
ivec2 GetCursorPixel(ptrdiff_t index) const
ivec2 get_cursor_pixel(int index) const
{
return m_cursors[index].m2;
return m_cursors[index].pixel;
}

/** Sets a per-device-axis sensitivity factor. The value returned by
* the operating system will be multiplied by this value before being
* returned by GetAxis */
void SetAxisSensitivity(ptrdiff_t index, float sensitivity)
void SetAxisSensitivity(int index, float sensitivity)
{
m_axis[index].m2 = sensitivity;
}
@@ -136,7 +130,7 @@ public:
/** Gets the per-device-axis sensitivity factor. The value returned by
* the operating system will be multiplied by this value before being
* returned by GetAxis */
float GetAxisSensitivity(ptrdiff_t index) const
float GetAxisSensitivity(int index) const
{
return m_axis[index].m2;
}
@@ -189,18 +183,13 @@ public:
AddCursor(-1, name);
}

void SetCursor(int id, vec2 const & position, ivec2 const & pixel)
{
m_cursors[id].m1 = position;
m_cursors[id].m2 = pixel;
}

ivec2 GetCursorPixelPos(int id)
/* Internal functions for the platform-specific drivers. */
void internal_set_cursor(int id, vec2 const & position, ivec2 const & pixel)
{
return m_cursors[id].m2;
m_cursors[id].uv = position;
m_cursors[id].pixel = pixel;
}

/* Internal functions for the platform-specific drivers. */
void internal_set_key(int id, bool state)
{
m_keys[id] = state;
@@ -236,17 +225,18 @@ protected:
array<float, float> m_axis;

/** Cursor position */
array<vec2, ivec2> m_cursors;
struct cursor_state { vec2 uv; ivec2 pixel; };
std::vector<cursor_state> m_cursors;

private:
static array<InputDevice*> devices;

template <typename... T>
size_t GetItemIndex(std::string const &name, std::vector<std::string, T...> const& a) const
int GetItemIndex(std::string const &name, std::vector<std::string, T...> const& a) const
{
for (size_t i = 0; i < a.size(); ++i)
if (a[i] == name)
return i;
return (int)i;
return -1;
}



+ 1
- 1
src/ui/sdl-input.cpp Parādīt failu

@@ -264,7 +264,7 @@ void SdlInput::tick(float seconds)
float max_screen_size = lol::max(m_screen.x, m_screen.y);
vec2 vmouse = vec2(mouse_pos);
vec2 vprevmouse = vec2(m_prev_mouse_pos);
mouse->SetCursor(0, vmouse / m_app, mouse_pos);
mouse->internal_set_cursor(0, vmouse / m_app, mouse_pos);
// Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick
mouse->internal_set_axis(0, (mouse_pos.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size);
// Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction)


Notiek ielāde…
Atcelt
Saglabāt