Touchscreen improvements

This commit is contained in:
elasota
2020-10-14 18:12:02 -04:00
parent 62e234c777
commit e4cddda183
10 changed files with 249 additions and 45 deletions

View File

@@ -366,25 +366,59 @@ namespace TouchScreenCtrlIDs
{
enum TouchScreenCtrlID
{
None,
MoveLeft,
MoveRight,
Flip,
Bands,
BatteryHelium,
Movement,
Count,
Invalid,
};
};
typedef TouchScreenCtrlIDs::TouchScreenCtrlID TouchScreenCtrlID_t;
struct touchScreenFingerID
{
int64_t m_deviceID;
int64_t m_fingerID;
touchScreenFingerID();
touchScreenFingerID(int64_t deviceID, int64_t fingerID);
bool operator ==(const touchScreenFingerID &other) const;
bool operator !=(const touchScreenFingerID &other) const;
};
inline touchScreenFingerID::touchScreenFingerID()
: m_fingerID(0)
, m_deviceID(0)
{
}
inline touchScreenFingerID::touchScreenFingerID(int64_t deviceID, int64_t fingerID)
: m_fingerID(fingerID)
, m_deviceID(deviceID)
{
}
inline bool touchScreenFingerID::operator==(const touchScreenFingerID &other) const
{
return this->m_fingerID == other.m_fingerID && this->m_deviceID == other.m_deviceID;
}
inline bool touchScreenFingerID::operator!=(const touchScreenFingerID &other) const
{
return !((*this) == other);
}
typedef struct
{
int fingerID;
touchScreenFingerID tfingerID;
Point point;
TouchScreenCtrlID_t capturingControl;
bool active;
} touchScreenFingerState;
typedef struct

View File

@@ -8,6 +8,7 @@
#include "PLDialogs.h"
#include "PLKeyEncoding.h"
#include "DialogManager.h"
#include "Environ.h"
#include "Externs.h"
#include "InputManager.h"
#include "MainWindow.h"
@@ -39,6 +40,8 @@ Boolean isEscPauseKey, paused, batteryWasEngaged;
extern long gameFrame;
extern short otherPlayerEscaped;
extern Boolean quitting, playing, onePlayerLeft, twoPlayerGame, demoGoing;
extern touchScreenControlState touchScreen;
extern macEnviron thisMac;
//============================================================== Functions
@@ -320,6 +323,8 @@ void GetInput (gliderPtr thisGlider)
bool holdFlipState = false;
bool leftState = false;
bool rightState = false;
bool bandsState = false;
bool batteryState = false;
const KeyDownStates *theKeys = PortabilityLayer::InputManager::GetInstance()->GetKeys();
@@ -337,6 +342,56 @@ void GetInput (gliderPtr thisGlider)
else
thisGlider->tipped = false;
if (thisMac.isTouchscreen)
{
for (int fi = 0; fi < touchScreenControlState::kMaxFingers; fi++)
{
const touchScreenFingerState &fstate = touchScreen.fingers[fi];
if (!fstate.active)
continue;
const Point touchScreenPoint = touchScreen.fingers[fi].point;
if (!touchScreen.controls[fstate.capturingControl].touchRect.Contains(touchScreenPoint))
continue;
switch (fstate.capturingControl)
{
case TouchScreenCtrlIDs::Movement:
{
int32_t screenWidth = mainWindowRect.Width();
const bool touchLeftState = (touchScreenPoint.h * 2 <= screenWidth);
const bool touchRightState = (touchScreenPoint.h * 2 - screenWidth >= 0);
if (touchLeftState)
{
if (touchRightState)
continuousFlipState = true;
else
leftState = true;
}
else
{
if (touchRightState)
rightState = true;
}
}
break;
case TouchScreenCtrlIDs::Flip:
holdFlipState = true;
break;
case TouchScreenCtrlIDs::Bands:
bandsState = true;
break;
case TouchScreenCtrlIDs::BatteryHelium:
batteryState = true;
break;
default:
break;
}
}
}
if (theKeys->IsSet(thisGlider->gamepadRightKey))
rightState = true;
@@ -407,7 +462,7 @@ void GetInput (gliderPtr thisGlider)
if (!leftState && !rightState)
thisGlider->tipped = false;
if ((theKeys->IsSet(thisGlider->battKey) || theKeys->IsSet(thisGlider->gamepadBattKey)) && (batteryTotal != 0) &&
if ((theKeys->IsSet(thisGlider->battKey) || theKeys->IsSet(thisGlider->gamepadBattKey) || batteryState) && (batteryTotal != 0) &&
(thisGlider->mode == kGliderNormal))
{
#ifdef CREATEDEMODATA
@@ -421,7 +476,7 @@ void GetInput (gliderPtr thisGlider)
else
batteryWasEngaged = false;
if ((theKeys->IsSet(thisGlider->bandKey) || theKeys->IsSet(thisGlider->gamepadBandKey)) && (bandsTotal > 0) &&
if ((theKeys->IsSet(thisGlider->bandKey) || theKeys->IsSet(thisGlider->gamepadBandKey) || bandsState) && (bandsTotal > 0) &&
(thisGlider->mode == kGliderNormal))
{
#ifdef CREATEDEMODATA

View File

@@ -390,14 +390,16 @@ void HandleGameResolutionChange(void)
//-------------------------------------------------------------- HandleTouchUp
void HandleTouchUp(int fingerID)
void HandleTouchUp(touchScreenFingerID fingerID)
{
for (int i = 0; i < touchScreenControlState::kMaxFingers; i++)
{
if (touchScreen.fingers[i].fingerID == fingerID)
touchScreenFingerState &fstate = touchScreen.fingers[i];
if (fstate.active && fstate.tfingerID == fingerID)
{
touchScreen.fingers[i].fingerID = -1;
touchScreen.fingers[i].capturingControl = TouchScreenCtrlIDs::None;
fstate.active = false;
fstate.tfingerID = touchScreenFingerID();
fstate.capturingControl = TouchScreenCtrlIDs::Invalid;
return;
}
}
@@ -405,13 +407,15 @@ void HandleTouchUp(int fingerID)
//-------------------------------------------------------------- HandleTouchMove
void HandleTouchMove(int fingerID, const Point &pt)
void HandleTouchMove(touchScreenFingerID fingerID, const Point &pt)
{
for (int i = 0; i < touchScreenControlState::kMaxFingers; i++)
{
if (touchScreen.fingers[i].fingerID == fingerID)
touchScreenFingerState &fstate = touchScreen.fingers[i];
if (fstate.active && fstate.tfingerID == fingerID)
{
touchScreen.fingers[i].point = pt;
fstate.point = pt;
return;
}
}
@@ -419,19 +423,21 @@ void HandleTouchMove(int fingerID, const Point &pt)
//-------------------------------------------------------------- HandleTouchDown
void HandleTouchDown(int fingerID, const Point &pt)
void HandleTouchDown(touchScreenFingerID fingerID, const Point &pt)
{
int freeFingerIndex = -1;
for (int i = 0; i < touchScreenControlState::kMaxFingers; i++)
{
if (touchScreen.fingers[i].fingerID == fingerID)
touchScreenFingerState &fstate = touchScreen.fingers[i];
if (fstate.active && fstate.tfingerID == fingerID)
{
// Finger is already considered down, something weird happened
HandleTouchMove(fingerID, pt);
return;
}
else if (touchScreen.fingers[i].fingerID < 0)
else if (!fstate.active)
freeFingerIndex = i;
}
@@ -446,7 +452,8 @@ void HandleTouchDown(int fingerID, const Point &pt)
{
if (touchScreen.controls[j].touchRect.Contains(pt))
{
fingerState.fingerID = fingerID;
fingerState.tfingerID = fingerID;
fingerState.active = true;
fingerState.capturingControl = static_cast<TouchScreenCtrlID_t>(j);
fingerState.point = pt;
return;
@@ -458,14 +465,17 @@ void HandleTouchDown(int fingerID, const Point &pt)
//-------------------------------------------------------------- HandleTouchLeave
void HandleTouchLeave(int fingerID)
void HandleTouchLeave(touchScreenFingerID fingerID)
{
for (int i = 0; i < touchScreenControlState::kMaxFingers; i++)
{
if (touchScreen.fingers[i].fingerID == fingerID)
touchScreenFingerState &fstate = touchScreen.fingers[i];
if (fstate.active && fstate.tfingerID == fingerID)
{
touchScreen.fingers[i].fingerID = -1;
touchScreen.fingers[i].capturingControl = TouchScreenCtrlIDs::None;
fstate.tfingerID = touchScreenFingerID();
fstate.active = false;
fstate.capturingControl = TouchScreenCtrlIDs::Invalid;
return;
}
}
@@ -492,17 +502,43 @@ void HandleInGameEvents(void)
{
case GpMouseEventTypes::kDown:
if (mouseInput.m_button == GpMouseButtons::kLeft)
HandleTouchDown(0, mousePt);
HandleTouchDown(touchScreenFingerID(), mousePt);
break;
case GpMouseEventTypes::kLeave:
HandleTouchLeave(0);
HandleTouchLeave(touchScreenFingerID());
break;
case GpMouseEventTypes::kUp:
HandleTouchMove(0, mousePt);
HandleTouchUp(0);
HandleTouchMove(touchScreenFingerID(), mousePt);
HandleTouchUp(touchScreenFingerID());
break;
case GpMouseEventTypes::kMove:
HandleTouchMove(0, mousePt);
HandleTouchMove(touchScreenFingerID(), mousePt);
break;
default:
break;
};
}
if (!thisMac.isMouseTouchscreen && evt.m_vosEvent.m_eventType == GpVOSEventTypes::kTouchInput)
{
const GpTouchInputEvent &touchInput = evt.m_vosEvent.m_event.m_touchInputEvent;
const Point touchPt = mainWindow->TouchToLocal(touchInput);
switch (touchInput.m_eventType)
{
case GpTouchEventTypes::kDown:
HandleTouchDown(touchScreenFingerID(touchInput.m_deviceID, touchInput.m_fingerID), touchPt);
break;
case GpTouchEventTypes::kLeave:
HandleTouchLeave(touchScreenFingerID(touchInput.m_deviceID, touchInput.m_fingerID));
break;
case GpTouchEventTypes::kUp:
HandleTouchMove(touchScreenFingerID(touchInput.m_deviceID, touchInput.m_fingerID), touchPt);
HandleTouchUp(touchScreenFingerID(touchInput.m_deviceID, touchInput.m_fingerID));
break;
case GpTouchEventTypes::kMove:
HandleTouchMove(touchScreenFingerID(), touchPt);
break;
default:
break;
@@ -530,9 +566,7 @@ void ResetTouchScreenControlBounds (void)
Point points[TouchScreenCtrlIDs::Count];
Point sizes[TouchScreenCtrlIDs::Count];
points[TouchScreenCtrlIDs::MoveLeft] = Point::Create(mainWindowRect.left + touchScreenControlEdgeSpacing, mainWindowRect.bottom - touchScreenControlEdgeSpacing - touchScreenControlSize);
points[TouchScreenCtrlIDs::MoveRight] = points[TouchScreenCtrlIDs::MoveLeft] + Point::Create(touchScreenControlInterSpacing + touchScreenControlSize, 0);
points[TouchScreenCtrlIDs::Movement] = Point::Create(mainWindowRect.left, mainWindowRect.top);
points[TouchScreenCtrlIDs::BatteryHelium] = Point::Create(mainWindowRect.right - touchScreenControlEdgeSpacing - touchScreenControlSize, mainWindowRect.bottom - touchScreenControlEdgeSpacing - touchScreenControlSize);
points[TouchScreenCtrlIDs::Flip] = points[TouchScreenCtrlIDs::BatteryHelium] + Point::Create(0, -touchScreenControlInterSpacing - touchScreenControlSize);
points[TouchScreenCtrlIDs::Bands] = points[TouchScreenCtrlIDs::BatteryHelium] + Point::Create(-touchScreenControlInterSpacing - touchScreenControlSize, 0);
@@ -540,6 +574,8 @@ void ResetTouchScreenControlBounds (void)
for (int i = 0; i < TouchScreenCtrlIDs::Count; i++)
sizes[i] = Point::Create(touchScreenControlSize, touchScreenControlSize);
sizes[TouchScreenCtrlIDs::Movement] = Point::Create(mainWindowRect.Width(), mainWindowRect.Height());
for (int i = 0; i < TouchScreenCtrlIDs::Count; i++)
{
Point lowerRight = points[i] + sizes[i];
@@ -550,8 +586,9 @@ void ResetTouchScreenControlBounds (void)
// Clear all active touches
for (int i = 0; i < touchScreenControlState::kMaxFingers; i++)
{
touchScreen.fingers[i].fingerID = -1;
touchScreen.fingers[i].capturingControl = TouchScreenCtrlIDs::None;
touchScreen.fingers[i].tfingerID = touchScreenFingerID();
touchScreen.fingers[i].active = false;
touchScreen.fingers[i].capturingControl = TouchScreenCtrlIDs::Invalid;
}
}
@@ -581,10 +618,13 @@ void InitTouchScreenControlState(void)
void PlayGame (void)
{
const houseType *debugHouse = nullptr;
if (thisHouse)
debugHouse = *thisHouse;
InitTouchScreenControlState();
touchScreen.controls[TouchScreenCtrlIDs::MoveLeft].isEnabled = true;
touchScreen.controls[TouchScreenCtrlIDs::MoveRight].isEnabled = true;
touchScreen.controls[TouchScreenCtrlIDs::Movement].isEnabled = true;
touchScreen.controls[TouchScreenCtrlIDs::Flip].isEnabled = true;
touchScreen.controls[TouchScreenCtrlIDs::Bands].isEnabled = true;
touchScreen.controls[TouchScreenCtrlIDs::BatteryHelium].isEnabled = true;

View File

@@ -615,8 +615,7 @@ void RenderTouchScreenControls (void)
for (int i = 0; i < TouchScreenCtrlIDs::Count; i++)
ctrlGraphics[i] = nullptr;
ctrlGraphics[TouchScreenCtrlIDs::MoveLeft] = touchScreen.graphics[touchScreenControlGraphics::MoveLeftIdle];
ctrlGraphics[TouchScreenCtrlIDs::MoveRight] = touchScreen.graphics[touchScreenControlGraphics::MoveRightIdle];
ctrlGraphics[TouchScreenCtrlIDs::Movement] = nullptr;
ctrlGraphics[TouchScreenCtrlIDs::Flip] = touchScreen.graphics[touchScreenControlGraphics::FlipIdle];
ctrlGraphics[TouchScreenCtrlIDs::Bands] = touchScreen.graphics[touchScreenControlGraphics::BandsDisabled];
ctrlGraphics[TouchScreenCtrlIDs::BatteryHelium] = touchScreen.graphics[touchScreenControlGraphics::BatteryDisabled];
@@ -636,7 +635,7 @@ void RenderTouchScreenControls (void)
if (batteryTotal < 0)
ctrlGraphics[TouchScreenCtrlIDs::BatteryHelium] = touchScreen.graphics[touchScreenControlGraphics::HeliumActive];
else if (batteryTotal > 0)
ctrlGraphics[TouchScreenCtrlIDs::BatteryHelium] = touchScreen.graphics[touchScreenControlGraphics::BandsActive];
ctrlGraphics[TouchScreenCtrlIDs::BatteryHelium] = touchScreen.graphics[touchScreenControlGraphics::BatteryActive];
}
else if (touchScreen.fingers[i].capturingControl == TouchScreenCtrlIDs::Bands)
{
@@ -645,10 +644,6 @@ void RenderTouchScreenControls (void)
}
else if (touchScreen.fingers[i].capturingControl == TouchScreenCtrlIDs::Flip)
ctrlGraphics[TouchScreenCtrlIDs::Flip] = touchScreen.graphics[touchScreenControlGraphics::FlipActive];
else if (touchScreen.fingers[i].capturingControl == TouchScreenCtrlIDs::MoveLeft)
ctrlGraphics[TouchScreenCtrlIDs::MoveLeft] = touchScreen.graphics[touchScreenControlGraphics::MoveLeftActive];
else if (touchScreen.fingers[i].capturingControl == TouchScreenCtrlIDs::MoveRight)
ctrlGraphics[TouchScreenCtrlIDs::MoveRight] = touchScreen.graphics[touchScreenControlGraphics::MoveRightActive];
}
for (int i = 0; i < TouchScreenCtrlIDs::Count; i++)