Skip to content
Permalink
Browse files
Engine: Fixed work of touch-screen controller
  • Loading branch information
Wohlstand committed Apr 28, 2019
1 parent 2060a09 commit f4f77dca618d4851f0392abadaad7ebd871bd583
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
@@ -18,12 +18,13 @@
*/

#include "controller_joystick.h"

#include <common_features/logger.h>

JoystickController::JoystickController() :
Controller(type_other),
m_joystickController(nullptr)
{
D_pLogDebugNA("Initialization of joystick controller...");
// qDebug() << "Num of joysticks: " << SDL_NumJoysticks();
// if(SDL_NumJoysticks() > 0){
// //TODO: Select which controller you want to use.
@@ -18,10 +18,12 @@
*/

#include "controller_keyboard.h"
#include <common_features/logger.h>

KeyboardController::KeyboardController() :
Controller(type_keyboard)
{
D_pLogDebugNA("Initialization of keyboard controller...");
kmap.jump.val = SDL_SCANCODE_Z;
kmap.jump_alt.val = SDL_SCANCODE_A;
kmap.run.val = SDL_SCANCODE_X;
@@ -183,8 +183,12 @@ Java_ru_wohlsoft_moondust_moondustActivity_setCanvasSize(JNIEnv *env, jclass typ
TouchScreenController::TouchScreenController() :
Controller(type_touchscreen)
{
D_pLogDebugNA("Initialization of touch-screen controller...");
m_touchDevicesCount = SDL_GetNumTouchDevices();
SDL_GetWindowSize(PGE_Window::window, &m_screenWidth, &m_screenHeight);
D_pLogDebug("Found %d touch devices, screen size: %d x %d",
m_touchDevicesCount,
m_screenWidth, m_screenHeight);
}

static void updateKeyValue(bool &key, bool &key_pressed, bool state)
@@ -242,13 +246,9 @@ static void updateFingerKeyState(TouchScreenController::FingerState &st,
}
}


void TouchScreenController::update()
void TouchScreenController::processTouchDevice(int dev_i)
{
if(m_touchDevicesCount == 0)
return; // Nothing to do

const SDL_TouchID dev = SDL_GetTouchDevice(0);
const SDL_TouchID dev = SDL_GetTouchDevice(dev_i);

int fingers = SDL_GetNumTouchFingers(dev);

@@ -261,7 +261,7 @@ void TouchScreenController::update()
for(int i = 0; i < fingers; i++)
{
SDL_Finger *f = SDL_GetTouchFinger(dev, i);
if(!f || (f->id < 0)) //Skip a wrong finger
if (!f || (f->id < 0)) //Skip a wrong finger
continue;

SDL_FingerID finger_id = f->id;
@@ -298,6 +298,10 @@ void TouchScreenController::update()
{
updateFingerKeyState(found->second, keys, key, true);
st.heldKeyPrev[key] = st.heldKey[key];
// Also: when more than one touch devices found, choose one which is actual
// Otherwise, the spam of on/off events will happen
if(m_actualDevice < 0)
m_actualDevice = dev_i;
}
}
st.alive = (keysCount > 0);
@@ -306,7 +310,7 @@ void TouchScreenController::update()
}

D_pLogDebug("= Finger press: ID=%d, X=%.04f, Y=%.04f, P=%.04f",
static_cast<int>(finger_id), finger_x, finger_y, finger_pressure);
static_cast<int>(finger_id), finger_x, finger_y, finger_pressure);

}

@@ -321,6 +325,26 @@ void TouchScreenController::update()
}
it++;
}
}

void TouchScreenController::update()
{
if(m_touchDevicesCount == 0)
return; // Nothing to do

// If actually used in the game touch was found, use it
if(m_actualDevice >= 0)
{
processTouchDevice(m_actualDevice);
return;
}
// Otherwise, find it
for(int dev_i = 0; dev_i < m_touchDevicesCount; dev_i++)
{
processTouchDevice(dev_i);
if(m_actualDevice >= 0)
break;
}

Controller::update();
}
@@ -35,7 +35,8 @@ class TouchScreenController final : public Controller
int m_screenWidth = 0;
//! Physical screen height
int m_screenHeight = 0;

//! Actual touch device to use
int m_actualDevice = -1;
public:
struct FingerState
{
@@ -67,6 +68,8 @@ class TouchScreenController final : public Controller
* \brief Read current state of keyboard controller
*/
void update() override;

void processTouchDevice(int dev_i);
};


BIN +2.94 KB (100%) _Libs/_sources/SDL-default.tar.gz
Binary file not shown.
BIN +9.1 KB (100%) _Libs/_sources/SDL-default.zip
Binary file not shown.

0 comments on commit f4f77dc

Please sign in to comment.