Skip to content

Commit

Permalink
Merge pull request #2 from rincew1nd/Development
Browse files Browse the repository at this point in the history
Options and gestures
  • Loading branch information
rincew1nd authored Jul 23, 2018
2 parents e4eff4b + 652c9fd commit f799692
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
APP_TITLE := Minesweeper
APP_AUTHOR := Rincew1nd
APP_VERSION := 1.1.0
APP_VERSION := 2.0.0
ICON := icon.jpg
TARGET := $(notdir $(CURDIR))
BUILD := build
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ Feel free to report bugs and contribute!
* ![Reset field](https://raw.githubusercontent.com/rincew1nd/Minesweeper-Switch/master/romfs/easyButton.bmp) 0.1% of cells contains mine
* ![Reset field](https://raw.githubusercontent.com/rincew1nd/Minesweeper-Switch/master/romfs/mediumButton.bmp) 0.2% of cells contains mine
* ![Reset field](https://raw.githubusercontent.com/rincew1nd/Minesweeper-Switch/master/romfs/hardButton.bmp) 0.3% of cells contains mine
* Pinch to resize board
* Drag to move board

## To-Do:

* Touchscreen gestures for zooming and moving field
* Timer and scoreboard

## Roadmap for next release:

* Timer and scoreboard (000%).
* Custom skins (HYPE!)
6 changes: 3 additions & 3 deletions source/Engine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void GameObject::SetAction(std::function<void()> func)
_onPress = func;
}

bool GameObject::Hovered(touchPosition* pos)
bool GameObject::Hovered(TouchInfo* ti)
{
return pos->px >= _rect->x && pos->px <= _rect->x + _rect->w &&
pos->py >= _rect->y && pos->py <= _rect->y + _rect->h;
return ti->ValueOne >= _rect->x && ti->ValueOne <= _rect->x + _rect->w &&
ti->ValueTwo >= _rect->y && ti->ValueTwo <= _rect->y + _rect->h;
}
3 changes: 2 additions & 1 deletion source/Engine/GameObject.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "Input.hpp"
#include <switch.h>
#include "SDL2/SDL.h"
#include <vector>
Expand All @@ -11,7 +12,7 @@ class GameObject
GameObject(int, int, int, int);
void SetAction(std::function<void()>);
void Press() { if (_onPress != nullptr) _onPress(); };
bool Hovered(touchPosition*);
bool Hovered(TouchInfo*);
void Move(int, int);
virtual ~GameObject() {};

Expand Down
92 changes: 82 additions & 10 deletions source/Engine/Input.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,100 @@
#include "Input.hpp"
#include <switch.h>
#include <cstdio>
#define ABS(x) ((x)<0?-(x):(x))

int Input::Scan()
{
hidScanInput();

_touchCount = hidTouchCount();
_touchType = None;

//Prevent multiple clicks from one touch press
if (_touchCount == 0)
_released = true;
else
if (!_released)
_touchCount = 0;
else
_released = false;
//If touch happens, check touch type
if (_touchCount > 0)
{
//If touch is just pressed, get current app run time
if (_touchPressTime == 0)
{
_touchPressTime = SDL_GetTicks();
_touchPressed = true;
}
//If touch is held more then 500 ms, change event to pinch or drag
if (SDL_GetTicks() - _touchPressTime >= 250)
{
_lastDragX = _touchPoints[0].px;
_lastDragY = _touchPoints[0].py;
_touchType = _touchCount > 1 ? Pinch : Drag;
}
}
//Reset touch info
else if (_touchPressTime > 0)
{
if (SDL_GetTicks() - _touchPressTime < 500)
_touchType = Press;

_touchPressTime = 0;
_touchPressed = false;
_firstPinch = true;
_firstDrag = true;

_lastDragX = 0;
_lastDragY = 0;
_lastPinchDelta = 0;

_lastTouchInfo->ValueOne = 0;
_lastTouchInfo->ValueTwo = 0;
}

//Store touch info
for (int i = 0; i < _touchCount; i++)
hidTouchRead(&_touchPoints[i], i);

//Return is touch pressed
return _touchCount;
}

TouchInfo* Input::GetTouchInfo()
{
_lastTouchInfo->Type = None;
if (_touchType == Press && !_touchPressed)
{
_lastTouchInfo->Type = Press;
_lastTouchInfo->ValueOne = _touchPoints[0].px;
_lastTouchInfo->ValueTwo = _touchPoints[0].py;
} else if (_touchType == Drag)
{
_lastTouchInfo->Type = Drag;
_lastTouchInfo->ValueOne = _touchPoints[0].px - _lastDragX;
_lastTouchInfo->ValueTwo = _touchPoints[0].py - _lastDragY;

if (_firstDrag)
{
_lastTouchInfo->ValueOne = 0;
_lastTouchInfo->ValueTwo = 0;
_firstDrag = false;
_firstPinch = true;
}
} else if (_touchType == Pinch)
{
int x = _touchPoints[0].px - _touchPoints[1].px;
int y = _touchPoints[0].py - _touchPoints[1].py;
_lastTouchInfo->Type = Pinch;
_lastTouchInfo->ValueOne = sqrt(fabs(x) + fabs(y));
_lastTouchInfo->ValueTwo = 0;

int lastPinch = _lastPinchDelta;
_lastPinchDelta = _lastTouchInfo->ValueOne;
_lastTouchInfo->ValueOne -= lastPinch;

if (_firstPinch)
{
_lastTouchInfo->ValueOne = 0;
_firstPinch = false;
_firstDrag = true;
}
}
return _lastTouchInfo;
}

touchPosition* Input::GetPointPosition(int i)
{
if (i < _touchCount)
Expand Down
33 changes: 31 additions & 2 deletions source/Engine/Input.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
#pragma once

#include <switch.h>
#include <SDL2/SDL.h>

enum InputType
{
None,
Press,
Drag,
Pinch
};

typedef struct TouchInfo
{
InputType Type;
int ValueOne;
int ValueTwo;
} TouchInfo;

class Input
{
public:
int Scan();
touchPosition* GetPointPosition(int);
TouchInfo* GetTouchInfo();

private:
touchPosition _touchPoints[10];
int _touchCount;
bool _released;

InputType _touchType = None;
int _lastDragX = 0;
int _lastDragY = 0;
int _lastPinchDelta = 0;

TouchInfo* _lastTouchInfo = new TouchInfo();

int _touchCount = 0;
bool _touchPressed = false;
bool _firstPinch = true;
bool _firstDrag = true;
Uint32 _touchPressTime = 0;
};
4 changes: 2 additions & 2 deletions source/Engine/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void Widget::Draw(SDL_Renderer* renderer)
}
}

void Widget::HandleTouch(touchPosition* pos)
void Widget::HandleTouch(TouchInfo* ti)
{
for(int i = 0; i < _buttons.size(); i++)
if (_buttons[i]->Hovered(pos) && _buttons[i]->IsVisible())
if (_buttons[i]->Hovered(ti) && _buttons[i]->IsVisible())
_buttons[i]->Press();
}
3 changes: 2 additions & 1 deletion source/Engine/Widget.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "Input.hpp"
#include "Button.hpp"
#include "TextObject.hpp"
#include <SDL2/SDL_ttf.h>
Expand All @@ -8,7 +9,7 @@ class Widget
{
public:
Widget(int, int, int, int);
void HandleTouch(touchPosition*);
void HandleTouch(TouchInfo*);
void HandleJoystick() {};

bool IsVisible;
Expand Down
Loading

0 comments on commit f799692

Please sign in to comment.