diff --git a/doc/tutorial/07_input.cpp b/doc/tutorial/07_input.cpp index 2bfb2fc0..12fa16a1 100644 --- a/doc/tutorial/07_input.cpp +++ b/doc/tutorial/07_input.cpp @@ -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 { diff --git a/doc/tutorial/11_fractal.cpp b/doc/tutorial/11_fractal.cpp index abfe6c5a..c1c30e13 100644 --- a/doc/tutorial/11_fractal.cpp +++ b/doc/tutorial/11_fractal.cpp @@ -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; diff --git a/src/application/android-app.cpp b/src/application/android-app.cpp index 354cfd17..9ce3d29b 100644 --- a/src/application/android-app.cpp +++ b/src/application/android-app.cpp @@ -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 diff --git a/src/ui/gui.cpp b/src/ui/gui.cpp index 69f15777..436c5b6d 100644 --- a/src/ui/gui.cpp +++ b/src/ui/gui.cpp @@ -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; diff --git a/src/ui/input.cpp b/src/ui/input.cpp index 712da7d0..977671b5 100644 --- a/src/ui/input.cpp +++ b/src/ui/input.cpp @@ -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; diff --git a/src/ui/input.h b/src/ui/input.h index d4330415..6d7662a3 100644 --- a/src/ui/input.h +++ b/src/ui/input.h @@ -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 m_axis; /** Cursor position */ - array m_cursors; + struct cursor_state { vec2 uv; ivec2 pixel; }; + std::vector m_cursors; private: static array devices; template - size_t GetItemIndex(std::string const &name, std::vector const& a) const + int GetItemIndex(std::string const &name, std::vector const& a) const { for (size_t i = 0; i < a.size(); ++i) if (a[i] == name) - return i; + return (int)i; return -1; } diff --git a/src/ui/sdl-input.cpp b/src/ui/sdl-input.cpp index 579062aa..1bbe27d9 100644 --- a/src/ui/sdl-input.cpp +++ b/src/ui/sdl-input.cpp @@ -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)