From f760aecb97556844bb10829d2a8ca123e6298dc3 Mon Sep 17 00:00:00 2001 From: Dmitry Grigoryev Date: Wed, 23 Dec 2020 15:25:33 +0100 Subject: [PATCH] Fixed invisible mouse cursor Added Enter/Escape keys emulation Cleaned up touchscreen/joystick handling code --- README.md | 6 +- src/engine/localevent.cpp | 265 ++++++++------------------------- src/engine/localevent.h | 4 +- src/engine/screen.cpp | 4 +- src/fheroes2/game/fheroes2.cpp | 2 +- src/fheroes2/gui/cursor.cpp | 2 +- 6 files changed, 73 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 67f4cbebe12..2c29ca13c40 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ Working controls controls are: - L-stick - move mouse cursor - A - left mouse click - B - Right mouse click +- X - Escape +- Y - Enter [4] TODO @@ -59,7 +61,7 @@ Working controls controls are: Here's a list of known bugs / missing features, approximately in order of priority. Don't bother reporting those as issues. -- cursor is invisible +- videos have wrong aspect ratio - locales are not supported @@ -67,7 +69,9 @@ The game icon was downloaded from [this page](https://iconarchive.com/show/mega- with the following copyright notice: >Artist: Exhumed + >License: CC Attribution-Noncommercial-No Derivate 4.0 + >Commercial usage: Not allowed diff --git a/src/engine/localevent.cpp b/src/engine/localevent.cpp index ae2d5e740b1..0b189999b2a 100644 --- a/src/engine/localevent.cpp +++ b/src/engine/localevent.cpp @@ -30,14 +30,12 @@ #define TAP_DELAY_EMULATE 1050 #ifdef __SWITCH__ -#define JOY_DEADZONE 1000 -#define JOY_ANALOG +#define JOY_DEADZONE 100 #define JOY_XAXIS 0 #define JOY_YAXIS 1 #define JOY_XAXISR 2 #define JOY_YAXISR 3 - enum { BTN_LEFT = 12, BTN_DOWN = 15, @@ -586,7 +584,6 @@ bool LocalEvent::HandleEvents( bool delay, bool allowExit ) HandleJoyButtonEvent(event.jbutton); break; case SDL_FINGERDOWN: - case SDL_FINGERUP: case SDL_FINGERMOTION: HandleTouchEvent(event.tfinger); @@ -658,71 +655,16 @@ bool LocalEvent::HandleEvents( bool delay, bool allowExit ) return true; } -#if defined(VITA) || defined(__SWITCH__) +#if defined(__SWITCH__) Sint16 xaxis_value = 0; Sint16 yaxis_value = 0; -float xaxis_float = 0; -float yaxis_float = 0; -float xaxis_starting = 0; -float yaxis_starting = 0; -float xcursor_starting = 0; -float ycursor_starting = 0; -bool secondTouchDown = false; void LocalEvent::HandleTouchEvent(const SDL_TouchFingerEvent & event) { - if(event.touchId != 0 || vita_touchcontrol_type == 0) - return; - - //doesn't really work at this point.. - if(event.fingerId == 1) - { - if (event.type == SDL_FINGERDOWN) - secondTouchDown = true; - else if (event.type == SDL_FINGERUP) - secondTouchDown = false; - return; - } - - if (event.type == SDL_FINGERDOWN) - { - xaxis_starting = event.x * (float)960; - yaxis_starting = event.y * (float)544; - xcursor_starting = xaxis_float; - ycursor_starting = yaxis_float; - } - SetModes(MOUSE_MOTION); - if (vita_touchcontrol_type == 1) - { - int w = 960; - int h = 544; - - //fullscreen touch location fix - { - w = fheroes2::Display::instance().width(); - h = fheroes2::Display::instance().height(); - } - - xaxis_float = event.x * (float)w; - yaxis_float = event.y * (float)h; - } - else if (vita_touchcontrol_type == 2) - { - float deltaX = ((event.x * (float)960) - xaxis_starting) * (vita_touchcontrol_speed / 10.0f); - float deltaY = ((event.y * (float)544) - yaxis_starting) * (vita_touchcontrol_speed / 10.0f); - xaxis_float = xcursor_starting + deltaX; - yaxis_float = ycursor_starting + deltaY; - } - - if(xaxis_float < 0) xaxis_float = 0; - if(yaxis_float < 0) yaxis_float = 0; - if(xaxis_float > fheroes2::Display::instance().width()) xaxis_float = fheroes2::Display::instance().width(); - if(yaxis_float > fheroes2::Display::instance().height()) yaxis_float = fheroes2::Display::instance().height(); - - mouse_cu.x = static_cast(xaxis_float); - mouse_cu.y = static_cast(yaxis_float); + mouse_cu.x = static_cast(event.x * fheroes2::Display::instance().width()); + mouse_cu.y = static_cast(event.y * fheroes2::Display::instance().height()); if((modes & MOUSE_MOTION) && redraw_cursor_func) { @@ -732,35 +674,22 @@ void LocalEvent::HandleTouchEvent(const SDL_TouchFingerEvent & event) (*(redraw_cursor_func))(mouse_cu.x, mouse_cu.y); } - if(!secondTouchDown) + //if(!secondTouchDown) { - if(event.type == SDL_FINGERDOWN) - { - mouse_pl = mouse_cu; - SetModes(MOUSE_PRESSED); - SetModes(CLICK_LEFT); + if(event.type == SDL_FINGERDOWN) + { + mouse_pl = mouse_cu; + SetModes(MOUSE_PRESSED); + SetModes(CLICK_LEFT); + } + else if(event.type == SDL_FINGERUP) + { + mouse_rl = mouse_cu; + ResetModes(MOUSE_PRESSED); + } + mouse_button = SDL_BUTTON_LEFT; } - else if(event.type == SDL_FINGERUP) - { - mouse_rl = mouse_cu; - ResetModes(MOUSE_PRESSED); - } - mouse_button = SDL_BUTTON_LEFT; - } - else - { - if(event.type == SDL_FINGERDOWN) - { - mouse_pr = mouse_cu; - SetModes(MOUSE_PRESSED); - } - else if(event.type == SDL_FINGERUP) - { - mouse_rr = mouse_cu; - ResetModes(MOUSE_PRESSED); - } - mouse_button = SDL_BUTTON_RIGHT; - } + } void LocalEvent::HandleJoyAxisEvent(const SDL_JoyAxisEvent & motion) @@ -780,36 +709,28 @@ void LocalEvent::HandleJoyAxisEvent(const SDL_JoyAxisEvent & motion) yaxis_value = 0; } } -#endif -#if defined(VITA) void LocalEvent::HandleJoyButtonEvent(const SDL_JoyButtonEvent & button) { - if (button.state == SDL_PRESSED) - SetModes(KEY_PRESSED); - else if (button.state == SDL_RELEASED) - ResetModes(KEY_PRESSED); - - if(button.button == BTN_CROSS) + if(button.button == BTN_A) { - if(modes & KEY_PRESSED) - { - mouse_pl = mouse_cu; - SetModes(MOUSE_PRESSED); - SetModes(CLICK_LEFT); + if(button.state == SDL_PRESSED) + { + mouse_pl = mouse_cu; + SetModes(MOUSE_PRESSED); + SetModes(CLICK_LEFT); + } + else + { + mouse_rl = mouse_cu; + ResetModes(MOUSE_PRESSED); + } + mouse_button = SDL_BUTTON_LEFT; + ResetModes(KEY_PRESSED); } - else - { - mouse_rl = mouse_cu; - ResetModes(MOUSE_PRESSED); - } - mouse_button = SDL_BUTTON_LEFT; - - ResetModes(KEY_PRESSED); - } - else if(button.button == BTN_CIRCLE) + else if(button.button == BTN_B) { - if(modes & KEY_PRESSED) + if(button.state == SDL_PRESSED) { mouse_pr = mouse_cu; SetModes(MOUSE_PRESSED); @@ -823,117 +744,56 @@ void LocalEvent::HandleJoyButtonEvent(const SDL_JoyButtonEvent & button) ResetModes(KEY_PRESSED); } - else if(modes & KEY_PRESSED) - { - if(button.button == BTN_LEFT) - { - key_value = KEY_KP4; - } - else if(button.button == BTN_RIGHT) - { - key_value = KEY_KP6; - } - else if(button.button == BTN_UP) - { - key_value = KEY_KP8; - } - else if(button.button == BTN_DOWN) - { - key_value = KEY_KP2; - } - } -} -#elif defined(__SWITCH__) -void LocalEvent::HandleJoyButtonEvent(const SDL_JoyButtonEvent & button) -{ - if(button.button == BTN_A) - { - if(button.state == SDL_PRESSED) - { - mouse_pl = mouse_cu; - SetModes(MOUSE_PRESSED); - SetModes(CLICK_LEFT); - } - else - { - mouse_rl = mouse_cu; - ResetModes(MOUSE_PRESSED); - } - mouse_button = SDL_BUTTON_LEFT; - - ResetModes(KEY_PRESSED); - } - else if(button.button == BTN_B) + else if(button.button == BTN_X) { if(button.state == SDL_PRESSED) { - mouse_pr = mouse_cu; - SetModes(MOUSE_PRESSED); + key_value = KEY_ESCAPE; + SetModes( KEY_PRESSED ); + SetModes( KEY_HOLD ); } else { - mouse_rr = mouse_cu; - ResetModes(MOUSE_PRESSED); + key_value = KEY_NONE; + ResetModes( KEY_PRESSED ); + ResetModes( KEY_HOLD ); } - mouse_button = SDL_BUTTON_RIGHT; - - ResetModes(KEY_PRESSED); } - else if(button.state == SDL_PRESSED) + else if(button.button == BTN_Y) { - if(button.button == BTN_LEFT) - { - key_value = KEY_LEFT; - } - else if(button.button == BTN_RIGHT) - { - key_value = KEY_RIGHT; - } - else if(button.button == BTN_UP) - { - key_value = KEY_UP; - } - else if(button.button == BTN_DOWN) - { - key_value = KEY_DOWN; - } - else if(button.button == BTN_X) + if(button.state == SDL_PRESSED) { - key_value = KEY_ESCAPE; + key_value = KEY_RETURN; + SetModes( KEY_PRESSED ); + SetModes( KEY_HOLD ); } - else if(button.button == BTN_Y) + else { - key_value = KEY_RETURN; + key_value = KEY_NONE; + ResetModes( KEY_PRESSED ); + ResetModes( KEY_HOLD ); } } } -#endif -#if defined(VITA) || defined(__SWITCH__) void LocalEvent::ProcessAxisMotion() { - float movementSpeed = 8192 * 20; - float settingsSpeedMod = static_cast(vita_pointer_speed) / 10.0f; - - Uint32 currentTime = SDL_GetTicks(); - float deltaTime = currentTime - lastTime; - lastTime = currentTime; - - if (xaxis_value == 0 && yaxis_value == 0) - return; + float settingsSpeedMod = pointer_speed/20000.0f; SetModes(MOUSE_MOTION); - xaxis_float += ((pow(abs(xaxis_value), 1.03f) * (xaxis_value / abs(xaxis_value)) * deltaTime) / movementSpeed) * settingsSpeedMod; - yaxis_float += ((pow(abs(yaxis_value), 1.03f) * (yaxis_value / abs(yaxis_value)) * deltaTime) / movementSpeed) * settingsSpeedMod; - - if(xaxis_float < 0) xaxis_float = 0; - if(yaxis_float < 0) yaxis_float = 0; - if(xaxis_float > fheroes2::Display::instance().width()) xaxis_float = fheroes2::Display::instance().width(); - if(yaxis_float > fheroes2::Display::instance().height()) yaxis_float = fheroes2::Display::instance().height(); + //xaxis_float += ((pow(abs(xaxis_value), 1.03f) * (xaxis_value / abs(xaxis_value)) * deltaTime)) * settingsSpeedMod; + //yaxis_float += ((pow(abs(yaxis_value), 1.03f) * (yaxis_value / abs(yaxis_value)) * deltaTime)) * settingsSpeedMod; + + float accel_factor = std::min(std::abs(xaxis_value)+std::abs(yaxis_value)+10000.0f / 10000.0f, 3.0f); + + mouse_cu.x += static_cast(xaxis_value * accel_factor * settingsSpeedMod); + mouse_cu.y += static_cast(yaxis_value * accel_factor * settingsSpeedMod); - mouse_cu.x = static_cast(xaxis_float); - mouse_cu.y = static_cast(yaxis_float); + if(mouse_cu.x < 0) mouse_cu.x = 0; + if(mouse_cu.y < 0) mouse_cu.y = 0; + if(mouse_cu.x >= fheroes2::Display::instance().width()) mouse_cu.x = fheroes2::Display::instance().width()-1; + if(mouse_cu.y >= fheroes2::Display::instance().height()) mouse_cu.y = fheroes2::Display::instance().height()-1; if((modes & MOUSE_MOTION) && redraw_cursor_func) { @@ -944,6 +804,7 @@ void LocalEvent::ProcessAxisMotion() } } #endif + bool LocalEvent::MouseMotion( void ) const { return ( modes & MOUSE_MOTION ) == MOUSE_MOTION; diff --git a/src/engine/localevent.h b/src/engine/localevent.h index d39e8845f76..832c02e7a15 100644 --- a/src/engine/localevent.h +++ b/src/engine/localevent.h @@ -348,9 +348,7 @@ class LocalEvent KeySym emulate_press_right; #endif #if defined(__SWITCH__) - int vita_pointer_speed = 10; - int vita_touchcontrol_type = 1; - int vita_touchcontrol_speed = 15; + float pointer_speed = 2.0f; #endif }; diff --git a/src/engine/screen.cpp b/src/engine/screen.cpp index 0e22e4aec96..a58a894efcb 100644 --- a/src/engine/screen.cpp +++ b/src/engine/screen.cpp @@ -114,7 +114,7 @@ namespace namespace { -#if SDL_VERSION_ATLEAST( 2, 0, 0 ) +#if SDL_VERSION_ATLEAST( 2, 0, 0 ) && !defined(WITHOUT_MOUSE) class RenderCursor : public fheroes2::Cursor { public: @@ -910,7 +910,7 @@ namespace fheroes2 void Display::render() { - if ( _cursor->isVisible() && !_cursor->_image.empty() ) { + if (/* _cursor->isVisible() && !_cursor->_image.empty()*/1 ) { const Sprite & cursorImage = _cursor->_image; const Sprite backup = Crop( *this, cursorImage.x(), cursorImage.y(), cursorImage.width(), cursorImage.height() ); Blit( cursorImage, *this, cursorImage.x(), cursorImage.y() ); diff --git a/src/fheroes2/game/fheroes2.cpp b/src/fheroes2/game/fheroes2.cpp index 8a40befaaf1..84ad0d0fa1a 100644 --- a/src/fheroes2/game/fheroes2.cpp +++ b/src/fheroes2/game/fheroes2.cpp @@ -146,7 +146,7 @@ int main( int argc, char ** argv ) display.resize( conf.VideoMode().w, conf.VideoMode().h ); fheroes2::engine().setTitle( GetCaption() ); - //!SDL_ShowCursor( SDL_DISABLE ); // hide system cursor + SDL_ShowCursor( SDL_DISABLE ); // hide system cursor // Ensure the mouse position is updated to prevent bad initial values. LocalEvent::Get().RegisterCycling(); diff --git a/src/fheroes2/gui/cursor.cpp b/src/fheroes2/gui/cursor.cpp index 1e45eb7466a..be265e9d1d1 100644 --- a/src/fheroes2/gui/cursor.cpp +++ b/src/fheroes2/gui/cursor.cpp @@ -26,7 +26,7 @@ // This is new Graphics engine. To change the code slowly we have to do some hacks here for now #include "screen.h" -#if SDL_VERSION_ATLEAST( 2, 0, 0 ) +#if SDL_VERSION_ATLEAST( 2, 0, 0 ) && !defined(WITHOUT_MOUSE) #define USE_SDL_CURSOR #endif