Skip to content

Commit

Permalink
Merge pull request pjasicek#148 from RicoRodriges/touch-events
Browse files Browse the repository at this point in the history
Touch event system
  • Loading branch information
pjasicek authored Jan 15, 2020
2 parents 2358a99 + c964735 commit adfeeae
Show file tree
Hide file tree
Showing 39 changed files with 1,605 additions and 119 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 33 additions & 4 deletions Build_Release/ASSETS/INGAME_MENU.XML
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,49 @@
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
<MenuItem>
<Name>TOUCHSCREEN_BUTTON</Name>
<Type>Button</Type>
<State>Active</State>
<InactiveImage>/CONTROLS/touchscreen_title.png</InactiveImage>
<ActiveImage>/CONTROLS/touchscreen_title.png</ActiveImage>
<Position x="325" y="452" />
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Help_TouchScreen</PageName>
</GeneratedEvent>
</MenuItem>
</MenuPage>

<!-- HELP TOUCHSCREEN PAGE -->
<MenuPage>
<PageName>MenuPage_Help_TouchScreen</PageName>
<KeyboardEvent>
<KeyType>Space</KeyType>
<KeyType>Escape</KeyType>
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
<KeyboardEvent>
<KeyType>Enter</KeyType>
<MenuItem>
<Name>CONTROL_IMAGE</Name>
<Type>Text</Type>
<State>Disabled</State>
<DisabledImage>/CONTROLS/touchscreen_img.png</DisabledImage>
<Position x="10" y="135" />
</MenuItem>
<MenuItem>
<Name>BACK_BUTTON</Name>
<Type>Button</Type>
<State>Active</State>
<InactiveImage>/STATES/MENU/IMAGES/MAIN/SINGLEPLAYER/SAVE/001_PREVIOUSMENU.PCX</InactiveImage>
<ActiveImage>/STATES/MENU/IMAGES/MAIN/SINGLEPLAYER/SAVE/002_PREVIOUSMENU.PCX</ActiveImage>
<Position x="266" y="420" />
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
</MenuItem>
</MenuPage>

<!-- OPTIONS PAGE -->
Expand Down
37 changes: 33 additions & 4 deletions Build_Release/ASSETS/MENU.xml
Original file line number Diff line number Diff line change
Expand Up @@ -553,20 +553,49 @@
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
<MenuItem>
<Name>TOUCHSCREEN_BUTTON</Name>
<Type>Button</Type>
<State>Active</State>
<InactiveImage>/CONTROLS/touchscreen_title.png</InactiveImage>
<ActiveImage>/CONTROLS/touchscreen_title.png</ActiveImage>
<Position x="325" y="452" />
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Help_TouchScreen</PageName>
</GeneratedEvent>
</MenuItem>
</MenuPage>

<!-- HELP TOUCHSCREEN PAGE -->
<MenuPage>
<PageName>MenuPage_Help_TouchScreen</PageName>
<KeyboardEvent>
<KeyType>Space</KeyType>
<KeyType>Escape</KeyType>
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
<KeyboardEvent>
<KeyType>Enter</KeyType>
<MenuItem>
<Name>CONTROL_IMAGE</Name>
<Type>Text</Type>
<State>Disabled</State>
<DisabledImage>/CONTROLS/touchscreen_img.png</DisabledImage>
<Position x="10" y="135" />
</MenuItem>
<MenuItem>
<Name>BACK_BUTTON</Name>
<Type>Button</Type>
<State>Active</State>
<InactiveImage>/STATES/MENU/IMAGES/MAIN/SINGLEPLAYER/SAVE/001_PREVIOUSMENU.PCX</InactiveImage>
<ActiveImage>/STATES/MENU/IMAGES/MAIN/SINGLEPLAYER/SAVE/002_PREVIOUSMENU.PCX</ActiveImage>
<Position x="266" y="420" />
<GeneratedEvent>
<Type>SwitchPage</Type>
<PageName>MenuPage_Main</PageName>
</GeneratedEvent>
</KeyboardEvent>
</MenuItem>
</MenuPage>

<!-- LOAD GAME PAGE -->
Expand Down
12 changes: 11 additions & 1 deletion Build_Release/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@
<ClawRunningSpeed>4.5</ClawRunningSpeed>
<SpringBoardSpringHeight>UNUSED</SpringBoardSpringHeight>
<SpringBoardSpringSpeed>11</SpringBoardSpringSpeed>
<UseAlternateControls>false</UseAlternateControls>
<ClawMinFallHeight>500</ClawMinFallHeight>
<LoadAllLevelSaves>true</LoadAllLevelSaves>
<ShowFps>true</ShowFps>
<ShowPosition>true</ShowPosition>
</GlobalOptions>
<ControlOptions>
<UseAlternateControls>false</UseAlternateControls>
<TouchScreen>
<Enable>true</Enable>
<!-- Distance to make a decision (separate tap/press and move events) -->
<!-- Range value 0..1 where 1 is screen width/height -->
<DistanceThreshold>0.05</DistanceThreshold>
<!-- How long make a decision in ms (separate short tap and long press) -->
<TimeThreshold>100</TimeThreshold>
</TouchScreen>
</ControlOptions>
<DebugOptions>
<SkipMenu>false</SkipMenu>
<SkipMenuToLevel>12</SkipMenuToLevel>
Expand Down
4 changes: 3 additions & 1 deletion Build_Release/config_linux_release.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
<ClawRunningSpeed>4.5</ClawRunningSpeed>
<SpringBoardSpringHeight>UNUSED</SpringBoardSpringHeight>
<SpringBoardSpringSpeed>11</SpringBoardSpringSpeed>
<UseAlternateControls>false</UseAlternateControls>
<ClawMinFallHeight>500</ClawMinFallHeight>
<LoadAllLevelSaves>false</LoadAllLevelSaves>
<ShowFps>false</ShowFps>
<ShowPosition>false</ShowPosition>
</GlobalOptions>
<Control>
<UseAlternateControls>false</UseAlternateControls>
</Control>
<DebugOptions>
<SkipMenu>false</SkipMenu>
<SkipMenuToLevel>10</SkipMenuToLevel>
Expand Down
180 changes: 175 additions & 5 deletions OpenClaw/ActorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
#include "Engine/Events/Events.h"
#include "ClawEvents.h"
#include "Engine/GameApp/BaseGameApp.h"
#include "Engine/UserInterface/Touch/TouchRecognizers/TapRecognizer.h"
#include "Engine/UserInterface/Touch/TouchRecognizers/JoystickRecognizer.h"
#include "Engine/UserInterface/Touch/TouchRecognizers/SwipeRecognizer.h"
#include "Engine/UserInterface/Touch/TouchRecognizers/PressRecognizer.h"

#define PAUSE_TAP_RECOGNIZER 1
#define JUMP_PRESS_RECOGNIZER 2
#define ATTACK_TAP_RECOGNIZER 3
#define WEAPON_TAP_RECOGNIZER 4
#define MOVEMENT_JOYSTICK_RECOGNIZER 5
#define PROJECTILE_SWIPE_RECOGNIZER 6

ActorController::ActorController(shared_ptr<SceneNode> controlledObject, float speed)
{
Expand Down Expand Up @@ -52,7 +63,7 @@ void ActorController::OnUpdate(uint32 msDiff)
// We need two conditions because I want behaviour such as when both right and left
// buttons are pressed, I dont want actor to move, e.g. to have the move effect nullyfied

if (g_pApp->GetGlobalOptions()->useAlternateControls)
if (g_pApp->GetControlOptions()->useAlternateControls)
{
if (m_InputKeys[SDLK_d])
{
Expand Down Expand Up @@ -121,7 +132,7 @@ void ActorController::OnUpdate(uint32 msDiff)

bool ActorController::VOnKeyDown(SDL_Keycode key)
{
if (g_pApp->GetGlobalOptions()->useAlternateControls)
if (g_pApp->GetControlOptions()->useAlternateControls)
{
if (key == SDLK_e)
{
Expand Down Expand Up @@ -160,7 +171,7 @@ bool ActorController::VOnKeyDown(SDL_Keycode key)

bool ActorController::VOnKeyUp(SDL_Keycode key)
{
if (g_pApp->GetGlobalOptions()->useAlternateControls)
if (g_pApp->GetControlOptions()->useAlternateControls)
{

}
Expand Down Expand Up @@ -190,7 +201,7 @@ bool ActorController::VOnPointerButtonDown(SDL_MouseButtonEvent& mouseEvent)
{
m_MouseLeftButtonDown = true;

if (g_pApp->GetGlobalOptions()->useAlternateControls)
if (g_pApp->GetControlOptions()->useAlternateControls)
{
HandleAction(ActionType_Fire);
}
Expand All @@ -201,7 +212,7 @@ bool ActorController::VOnPointerButtonDown(SDL_MouseButtonEvent& mouseEvent)
{
m_MouseRightButtonDown = true;

if (g_pApp->GetGlobalOptions()->useAlternateControls)
if (g_pApp->GetControlOptions()->useAlternateControls)
{
HandleAction(ActionType_Attack);
}
Expand Down Expand Up @@ -230,3 +241,162 @@ bool ActorController::VOnPointerButtonUp(SDL_MouseButtonEvent& mouseEvent)

return false;
}

std::vector<std::shared_ptr<AbstractRecognizer>> ActorController::VRegisterRecognizers() {
const float thresholdDistance = g_pApp->GetControlOptions()->touchScreen.distanceThreshold;
const unsigned int thresholdMs = g_pApp->GetControlOptions()->touchScreen.timeThreshold;

// TODO: Try to describe it in config?
std::vector<std::shared_ptr<AbstractRecognizer>> recognizers{};
recognizers.reserve(6);

// Pause button. Left up screen corner. Maximum priority
auto pauseRecognizer = std::make_shared<TapRecognizer>(PAUSE_TAP_RECOGNIZER, 100);
pauseRecognizer->SetFrame(Rect{0, 0, 0.1, 0.1});
recognizers.push_back(pauseRecognizer);

// Change weapon button. Right up screen corner. Maximum priority
auto weaponRecognizer = std::make_shared<TapRecognizer>(WEAPON_TAP_RECOGNIZER, 99);
weaponRecognizer->SetFrame(Rect{0.9, 0, 0.1, 0.2});
recognizers.push_back(weaponRecognizer);

// Attack button. All screen area. Minimum priority
auto attackRecognizer = std::make_shared<TapRecognizer>(ATTACK_TAP_RECOGNIZER, 1);
recognizers.push_back(attackRecognizer);

// Movement controller. Left part of screen. Medium priority
auto joystick = std::make_shared<JoystickRecognizer>(MOVEMENT_JOYSTICK_RECOGNIZER, 50, thresholdDistance, thresholdMs + 100);
joystick->SetFrame(Rect{0, 0, 0.5, 1});
recognizers.push_back(joystick);

// Jump button. Right part of screen. Minimum priority but greater that attack button
auto jumpButton = std::make_shared<PressRecognizer>(JUMP_PRESS_RECOGNIZER, 20, thresholdDistance, thresholdMs);
jumpButton->SetFrame(Rect{0.5, 0, 0.5, 1});
recognizers.push_back(jumpButton);

// Swipe to attack. Right part of screen. Medium priority (greater than jump button)
auto swipe = std::make_shared<SwipeRecognizer>(PROJECTILE_SWIPE_RECOGNIZER, 51, thresholdDistance, thresholdMs);
swipe->SetFrame(Rect{0.5, 0, 0.5, 1});
recognizers.push_back(swipe);

return recognizers;
}

bool ActorController::VOnTouch(const Touch_Event &evt) {
auto code = evt.sdlEvent.user.code;
switch (code) {
case TAP:
return OnTap(evt.recognizerId, evt.customData.tap);
case JOYSTICK_MOVE:
case JOYSTICK_RESET:
return OnJoystick(evt.recognizerId, evt.customData.joystick);
case SWIPE_START:
case SWIPE_END:
return OnSwipe(evt.recognizerId, evt.customData.swipe, code == SWIPE_START);
case PRESS_START:
case PRESS_END:
return OnPress(evt.recognizerId, evt.customData.press, code == PRESS_START);
}
return false;
}

bool ActorController::OnTap(int id, const Touch_TapEvent &evt) {
SDL_Keycode key;
switch (id) {
case PAUSE_TAP_RECOGNIZER:
{
// Little hack to open/close ingame menu
key = SDLK_ESCAPE;

SDL_Event event{0};
event.type = SDL_KEYDOWN;
event.key.keysym.sym = key;
event.key.keysym.scancode = SDL_GetScancodeFromKey(key);
g_pApp->OnEvent(event);
event.type = SDL_KEYUP;
g_pApp->OnEvent(event);
return true;
}
case WEAPON_TAP_RECOGNIZER:
key = SDLK_LSHIFT;
break;
case ATTACK_TAP_RECOGNIZER:
key = SDLK_LCTRL;
break;
default:
key = SDLK_UNKNOWN;
break;
}

if (key != SDLK_UNKNOWN) {
VOnKeyDown(key);
return VOnKeyUp(key);
}
return false;
}

bool ActorController::OnJoystick(int id, const Touch_JoystickEvent &evt) {
if (id == MOVEMENT_JOYSTICK_RECOGNIZER) {
const auto dx = evt.dx;
const auto dy = evt.dy;

const float deadzone = 0.05 * 0.05;

SDL_Keycode horizontalKey = SDLK_UNKNOWN;
SDL_Keycode verticalKey = SDLK_UNKNOWN;
if (dx * dx + dy * dy > deadzone) {

if (dx * 2 < dy && dx * 2 > -dy) {
// 247 - 292 deg
verticalKey = SDLK_DOWN;
} else if (dx * 2 < -dy && dx * 2 > dy) {
// 67 deg - 112 deg
verticalKey = SDLK_UP;
}

if (dy < dx * 2 && dy > -dx * 2) {
// 292 - 67 deg
horizontalKey = SDLK_RIGHT;
} else if (dy < -dx * 2 && dy > dx * 2) {
// 112 - 247 deg
horizontalKey = SDLK_LEFT;
}
}

SDL_Keycode keys[] = {SDLK_LEFT, SDLK_UP, SDLK_RIGHT, SDLK_DOWN};
for (SDL_Keycode key : keys) {
bool isPressed = m_InputKeys[key];
bool press = key == horizontalKey || key == verticalKey;
if (!isPressed && press) {
VOnKeyDown(key);
} else if (isPressed && !press) {
VOnKeyUp(key);
}
}

return true;
}
return false;
}

bool ActorController::OnSwipe(int id, const Touch_SwipeEvent &evt, bool start) {
if (id == PROJECTILE_SWIPE_RECOGNIZER) {
if (start) {
return VOnKeyDown(SDLK_LALT);
} else {
return VOnKeyUp(SDLK_LALT);
}
}
return false;
}

bool ActorController::OnPress(int id, const Touch_PressEvent &evt, bool start) {
if (id == JUMP_PRESS_RECOGNIZER) {
if (start) {
return VOnKeyDown(SDLK_SPACE);
} else {
return VOnKeyUp(SDLK_SPACE);
}
}
return false;
}
10 changes: 9 additions & 1 deletion OpenClaw/ActorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "Engine/SharedDefines.h"

class SceneNode;
class ActorController : public IKeyboardHandler, public IPointerHandler
class ActorController : public IKeyboardHandler, public IPointerHandler, public ITouchHandler
{
public:
ActorController(shared_ptr<SceneNode> controlledObject, float speed = 0.36f);
Expand All @@ -22,7 +22,15 @@ class ActorController : public IKeyboardHandler, public IPointerHandler
bool VOnPointerButtonDown(SDL_MouseButtonEvent& mouseEvent);
bool VOnPointerButtonUp(SDL_MouseButtonEvent& mouseEvent);

std::vector<std::shared_ptr<AbstractRecognizer>> VRegisterRecognizers() override;
bool VOnTouch(const Touch_Event &evt) override;

private:
bool OnTap(int id, const Touch_TapEvent &evt);
bool OnJoystick(int id, const Touch_JoystickEvent &evt);
bool OnSwipe(int id, const Touch_SwipeEvent &evt, bool start);
bool OnPress(int id, const Touch_PressEvent &evt, bool start);

shared_ptr<SceneNode> m_pControlledObject;
float m_Speed;

Expand Down
Loading

0 comments on commit adfeeae

Please sign in to comment.