Initial touch input support

This commit adds virtual buttons on-screen to let you navigate through
menus, and play the game.
This commit is contained in:
AllyTally 2024-01-28 21:29:22 -04:00
parent a3571cec2d
commit 424939678d
28 changed files with 515 additions and 43 deletions

View File

@ -104,6 +104,7 @@ set(VVV_SRC
src/TerminalScripts.cpp
src/Textbox.cpp
src/Tower.cpp
src/Touch.cpp
src/UtilityClass.cpp
src/WarpClass.cpp
src/XMLUtils.cpp

View File

@ -88,6 +88,9 @@ def zipRepoAssetsTask = tasks.register("zipRepoAssets", Zip) {
from('../../lang') { spec ->
spec.into('lang')
}
from('../../touch') { spec ->
spec.into('graphics')
}
archiveFileName.set('repo.zip')
destinationDirectory.value(layout.buildDirectory.dir("generated/main/assets"))
}

View File

@ -314,6 +314,8 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath, char* langD
doesFontsDirExist = mount_pre_datazip(NULL, "fonts", "graphics/", fontsDir);
mount_pre_datazip(NULL, "touch", "graphics/", NULL);
/* Mount the stock content last */
if (assetsPath)
{

View File

@ -19,6 +19,7 @@
#include "RoomnameTranslator.h"
#include "Screen.h"
#include "Script.h"
#include "Touch.h"
#include "UtilityClass.h"
#include "VFormat.h"
#include "Vlogging.h"
@ -1174,6 +1175,31 @@ void Graphics::draw_texture(SDL_Texture* image, const int x, const int y)
copy_texture(image, NULL, &dstrect);
}
void Graphics::draw_texture(SDL_Texture* image, const int x, const int y, const int scalex, const int scaley)
{
int w, h;
if (query_texture(image, NULL, NULL, &w, &h) != 0)
{
return;
}
int flip = SDL_FLIP_NONE;
if (scalex < 0)
{
flip |= SDL_FLIP_HORIZONTAL;
}
if (scaley < 0)
{
flip |= SDL_FLIP_VERTICAL;
}
const SDL_Rect dstrect = { x, y, w * SDL_abs(scalex), h * SDL_abs(scaley) };
copy_texture(image, NULL, &dstrect, 0, NULL, (SDL_RendererFlip)flip);
}
void Graphics::draw_texture_part(SDL_Texture* image, const int x, const int y, const int x2, const int y2, const int w, const int h, const int scalex, const int scaley)
{
const SDL_Rect srcrect = {x2, y2, w, h};
@ -3379,6 +3405,8 @@ void Graphics::screenshake(void)
get_stretch_info(&rect);
copy_texture(tempShakeTexture, NULL, &rect, 0, NULL, flipmode ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE);
touch::render();
}
void Graphics::updatescreenshake(void)
@ -3453,6 +3481,8 @@ void Graphics::render(void)
get_stretch_info(&rect);
copy_texture(gameTexture, NULL, &rect, 0, NULL, flipmode ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE);
touch::render();
}
void Graphics::renderwithscreeneffects(void)

View File

@ -150,6 +150,8 @@ public:
void draw_texture(SDL_Texture* image, int x, int y);
void draw_texture(SDL_Texture* image, int x, int y, int scalex, int scaley);
void draw_texture_part(SDL_Texture* image, int x, int y, int x2, int y2, int w, int h, int scalex, int scaley);
void draw_grid_tile(SDL_Texture* texture, int t, int x, int y, int width, int height, int scalex, int scaley);
@ -248,6 +250,7 @@ public:
int screenshake_y;
void draw_window_background(void);
void draw_touch(void);
void get_stretch_info(SDL_Rect* rect);

View File

@ -429,6 +429,10 @@ void GraphicsResources::init(void)
im_image10 = LoadImage("graphics/ending.png");
im_image11 = LoadImage("graphics/site4.png", TEX_WHITE);
im_button_left = LoadImage("graphics/buttons/button_left.png");
im_button_right = LoadImage("graphics/buttons/button_right.png");
im_button_map = LoadImage("graphics/buttons/button_map.png");
im_sprites_translated = NULL;
im_flipsprites_translated = NULL;
@ -476,6 +480,10 @@ void GraphicsResources::destroy(void)
CLEAR(im_sprites_translated);
CLEAR(im_flipsprites_translated);
CLEAR(im_button_left);
CLEAR(im_button_right);
CLEAR(im_button_map);
#undef CLEAR
VVV_freefunc(SDL_FreeSurface, im_sprites_surf);

View File

@ -48,6 +48,11 @@ public:
SDL_Texture* im_sprites_translated;
SDL_Texture* im_flipsprites_translated;
/* Touch */
SDL_Texture* im_button_left;
SDL_Texture* im_button_right;
SDL_Texture* im_button_map;
};
SDL_Surface* LoadImageSurface(const char* filename);

View File

@ -23,6 +23,7 @@
#include "RoomnameTranslator.h"
#include "Screen.h"
#include "Script.h"
#include "Touch.h"
#include "UtilityClass.h"
#include "Vlogging.h"
@ -2320,26 +2321,37 @@ void titleinput(void)
controller_down |= key.controllerWantsLeft(false);
}
if (key.isDown(left) || key.isDown(KEYBOARD_UP) || key.isDown(a) || key.isDown(KEYBOARD_w) || controller_up)
if (key.isDown(left) || key.isDown(KEYBOARD_UP) || key.isDown(a) || key.isDown(KEYBOARD_w) || controller_up || touch::button_tapped(TOUCH_BUTTON_LEFT))
{
game.press_left = true;
}
if (key.isDown(right) || key.isDown(KEYBOARD_DOWN) || key.isDown(d) || key.isDown(KEYBOARD_s) || controller_down)
if (key.isDown(right) || key.isDown(KEYBOARD_DOWN) || key.isDown(d) || key.isDown(KEYBOARD_s) || controller_down || touch::button_tapped(TOUCH_BUTTON_RIGHT))
{
game.press_right = true;
}
}
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip)) game.press_action = true;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip)
|| (!game.menustart ? touch::screen_tapped() : touch::button_tapped(TOUCH_BUTTON_CONFIRM)))
{
game.press_action = true;
}
//|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)) game.press_action = true; //on menus, up and down don't work as action
if (key.isDown(KEYBOARD_ENTER)) game.press_map = true;
//In the menu system, all keypresses are single taps rather than holds. Therefore this test has to be done for all presses
if (!game.press_action && !game.press_left && !game.press_right && !key.isDown(27) && !key.isDown(game.controllerButton_esc)) game.jumpheld = false;
if (!game.press_action && !game.press_left && !game.press_right && !key.isDown(27) && !key.isDown(game.controllerButton_esc)
&& !touch::button_tapped(TOUCH_BUTTON_CANCEL))
{
game.jumpheld = false;
}
if (!game.press_map) game.mapheld = false;
if (!game.jumpheld && graphics.fademode == FADE_NONE)
{
if (game.press_action || game.press_left || game.press_right || game.press_map || key.isDown(27) || key.isDown(game.controllerButton_esc))
if (game.press_action || game.press_left || game.press_right || game.press_map || key.isDown(27) || key.isDown(game.controllerButton_esc)
|| touch::button_tapped(TOUCH_BUTTON_CANCEL))
{
game.jumpheld = true;
}
@ -2358,7 +2370,7 @@ void titleinput(void)
if (game.menustart
&& game.menucountdown <= 0
&& (key.isDown(27) || key.isDown(game.controllerButton_esc)))
&& (key.isDown(27) || key.isDown(game.controllerButton_esc) || touch::button_tapped(TOUCH_BUTTON_CANCEL)))
{
if (game.currentmenuname == Menu::language && loc::pre_title_lang_menu)
{
@ -2535,16 +2547,17 @@ void gameinput(void)
game.press_action = false;
game.press_interact = false;
if (key.isDown(KEYBOARD_LEFT) || key.isDown(KEYBOARD_a) || key.controllerWantsLeft(false))
if (key.isDown(KEYBOARD_LEFT) || key.isDown(KEYBOARD_a) || key.controllerWantsLeft(false) || touch::buttons[TOUCH_BUTTON_LEFT].down)
{
game.press_left = true;
}
if (key.isDown(KEYBOARD_RIGHT) || key.isDown(KEYBOARD_d) || key.controllerWantsRight(false))
if (key.isDown(KEYBOARD_RIGHT) || key.isDown(KEYBOARD_d) || key.controllerWantsRight(false) || touch::buttons[TOUCH_BUTTON_RIGHT].down)
{
game.press_right = true;
}
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || key.isDown(KEYBOARD_w) || key.isDown(KEYBOARD_s)|| key.isDown(game.controllerButton_flip))
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || key.isDown(KEYBOARD_w)
|| key.isDown(KEYBOARD_s) || key.isDown(game.controllerButton_flip) || touch::touching_right())
{
game.press_action = true;
}
@ -2556,7 +2569,7 @@ void gameinput(void)
}
game.press_map = false;
if (key.isDown(KEYBOARD_ENTER) || key.isDown(SDLK_KP_ENTER) || key.isDown(game.controllerButton_map) )
if (key.isDown(KEYBOARD_ENTER) || key.isDown(SDLK_KP_ENTER) || key.isDown(game.controllerButton_map) || touch::button_tapped(TOUCH_BUTTON_MAP))
{
game.press_map = true;
}
@ -2573,7 +2586,12 @@ void gameinput(void)
{
game.press_action = false;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || key.isDown(KEYBOARD_w) || key.isDown(KEYBOARD_s) || key.isDown(game.controllerButton_flip)) game.press_action = true;
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || key.isDown(KEYBOARD_w)
|| key.isDown(KEYBOARD_s) || key.isDown(game.controllerButton_flip) || touch::screen_tapped()
)
{
game.press_action = true;
}
}
if (game.press_action && !game.jumpheld)
@ -2605,7 +2623,8 @@ void gameinput(void)
//immediately open again
//We really need a better input system soon...
&& !key.isDown(27)
&& !key.isDown(game.controllerButton_esc))
&& !key.isDown(game.controllerButton_esc)
&& !touch::button_tapped(TOUCH_BUTTON_CANCEL))
{
game.mapheld = false;
}
@ -2950,7 +2969,7 @@ void gameinput(void)
}
if (!game.mapheld
&& (key.isDown(27) || key.isDown(game.controllerButton_esc))
&& (key.isDown(27) || key.isDown(game.controllerButton_esc) || touch::button_tapped(TOUCH_BUTTON_CANCEL))
&& (!map.custommode || map.custommodeforreal))
{
game.mapheld = true;
@ -3072,15 +3091,15 @@ void mapinput(void)
controller_down |= key.controllerWantsLeft(false);
}
if (key.isDown(left) || key.isDown(KEYBOARD_UP) || key.isDown(a) || key.isDown(KEYBOARD_w)|| controller_up)
if (key.isDown(left) || key.isDown(KEYBOARD_UP) || key.isDown(a) || key.isDown(KEYBOARD_w)|| controller_up || touch::button_tapped(TOUCH_BUTTON_LEFT))
{
game.press_left = true;
}
if (key.isDown(right) || key.isDown(KEYBOARD_DOWN) || key.isDown(d) || key.isDown(KEYBOARD_s)|| controller_down)
if (key.isDown(right) || key.isDown(KEYBOARD_DOWN) || key.isDown(d) || key.isDown(KEYBOARD_s)|| controller_down || touch::button_tapped(TOUCH_BUTTON_RIGHT))
{
game.press_right = true;
}
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip))
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip) || touch::button_tapped(TOUCH_BUTTON_CONFIRM))
{
game.press_action = true;
}
@ -3088,8 +3107,8 @@ void mapinput(void)
|| (game.menupage >= 20 && game.menupage <= 21)
|| (game.menupage >= 30 && game.menupage <= 32))
{
if (key.isDown(KEYBOARD_ENTER) || key.isDown(game.controllerButton_map) ) game.press_map = true;
if (key.isDown(27) && !game.mapheld)
if (key.isDown(KEYBOARD_ENTER) || key.isDown(game.controllerButton_map) || touch::button_tapped(TOUCH_BUTTON_CONFIRM)) game.press_map = true;
if ((key.isDown(27) || touch::button_tapped(TOUCH_BUTTON_CANCEL)) && !game.mapheld)
{
game.mapheld = true;
if (game.menupage < 9
@ -3110,7 +3129,11 @@ void mapinput(void)
}
else
{
if (key.isDown(KEYBOARD_ENTER) || key.isDown(27)|| key.isDown(game.controllerButton_map) ) game.press_map = true;
if (key.isDown(KEYBOARD_ENTER) || key.isDown(27) || key.isDown(game.controllerButton_map)
|| touch::button_tapped(TOUCH_BUTTON_CANCEL) || touch::button_tapped(TOUCH_BUTTON_CONFIRM))
{
game.press_map = true;
}
}
//In the menu system, all keypresses are single taps rather than holds. Therefore this test has to be done for all presses
@ -3311,11 +3334,16 @@ void teleporterinput(void)
if(graphics.menuoffset==0)
{
if (key.isDown(KEYBOARD_LEFT)|| key.isDown(KEYBOARD_a) || key.controllerWantsLeft(false) ) game.press_left = true;
if (key.isDown(KEYBOARD_RIGHT) || key.isDown(KEYBOARD_d)|| key.controllerWantsRight(false) ) game.press_right = true;
if (key.isDown(KEYBOARD_LEFT)|| key.isDown(KEYBOARD_a) || key.controllerWantsLeft(false) || touch::button_tapped(TOUCH_BUTTON_LEFT)) game.press_left = true;
if (key.isDown(KEYBOARD_RIGHT) || key.isDown(KEYBOARD_d)|| key.controllerWantsRight(false) || touch::button_tapped(TOUCH_BUTTON_RIGHT)) game.press_right = true;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)|| key.isDown(KEYBOARD_w)|| key.isDown(KEYBOARD_s) || key.isDown(game.controllerButton_flip)) game.press_action = true;
if (!game.separate_interact && (key.isDown(KEYBOARD_ENTER) || key.isDown(game.controllerButton_map)))
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || key.isDown(KEYBOARD_w)
|| key.isDown(KEYBOARD_s) || key.isDown(game.controllerButton_flip)
|| touch::button_tapped(TOUCH_BUTTON_CONFIRM))
{
game.press_action = true;
}
if (!game.separate_interact && (key.isDown(KEYBOARD_ENTER) || key.isDown(game.controllerButton_map) || touch::button_tapped(TOUCH_BUTTON_CONFIRM)))
{
game.press_map = true;
}
@ -3328,7 +3356,7 @@ void teleporterinput(void)
if (!game.press_action && !game.press_left && !game.press_right && !game.press_interact) game.jumpheld = false;
if (!game.press_map) game.mapheld = false;
if (key.isDown(27))
if (key.isDown(27) || touch::button_tapped(TOUCH_BUTTON_CANCEL))
{
if (!map.custommode || map.custommodeforreal)
{
@ -3455,7 +3483,7 @@ void gamecompleteinput(void)
graphics.titlebg.bypos += graphics.titlebg.bscroll;
game.oldcreditposition = game.creditposition;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip))
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip) || touch::screen_tapped())
{
game.creditposition -= 6;
if (game.creditposition <= -Credits::creditmaxposition)
@ -3503,7 +3531,7 @@ void gamecompleteinput2(void)
//Do this here because input comes first
game.oldcreditposx = game.creditposx;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip))
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip) || touch::screen_tapped())
{
game.creditposx++;
game.oldcreditposx++;

View File

@ -15,6 +15,7 @@
#include "LocalizationStorage.h"
#include "Music.h"
#include "Screen.h"
#include "Touch.h"
#include "UTF8.h"
#include "Vlogging.h"
@ -57,6 +58,8 @@ KeyPoll::KeyPoll(void)
linealreadyemptykludge = false;
isActive = true;
using_touch = false;
}
void KeyPoll::enabletextentry(void)
@ -149,6 +152,12 @@ void KeyPoll::Poll(void)
bool fullscreenkeybind = false;
SDL_GameController *controller = NULL;
SDL_Event evt;
int screen_width;
int screen_height;
gameScreen.GetScreenSize(&screen_width, &screen_height);
touch::reset();
while (SDL_PollEvent(&evt))
{
switch (evt.type)
@ -356,6 +365,70 @@ void KeyPoll::Poll(void)
break;
}
/* Touch Events */
case SDL_FINGERDOWN:
{
using_touch = true;
VVV_Finger finger;
finger.pressed = true;
finger.x = evt.tfinger.x * screen_width;
finger.y = evt.tfinger.y * screen_height;
finger.id = evt.tfinger.fingerId;
touch::fingers.push_back(finger);
raw_mousex = evt.tfinger.x * screen_width;
raw_mousey = evt.tfinger.y * screen_height;
leftbutton = 1;
break;
}
case SDL_FINGERMOTION:
{
using_touch = true;
for (int i = 0; i < (int) touch::fingers.size(); i++)
{
if (touch::fingers[i].id == evt.tfinger.fingerId)
{
touch::fingers[i].x = evt.tfinger.x * screen_width;
touch::fingers[i].y = evt.tfinger.y * screen_height;
break;
}
}
raw_mousex = evt.tfinger.x * screen_width;
raw_mousey = evt.tfinger.y * screen_height;
break;
}
case SDL_FINGERUP:
{
using_touch = true;
for (int i = (int)touch::fingers.size() - 1; i >= 0; i--)
{
if (touch::fingers[i].id == evt.tfinger.fingerId)
{
// Unpress any buttons that this finger may belong to
for (int j = 0; j < NUM_TOUCH_BUTTONS; j++)
{
if (touch::buttons[j].finger == &touch::fingers[i])
{
touch::buttons[j].down = false;
touch::buttons[j].finger = NULL;
}
}
touch::fingers.erase(touch::fingers.begin() + i);
break;
}
}
raw_mousex = evt.tfinger.x * screen_width;
raw_mousey = evt.tfinger.y * screen_height;
leftbutton = 0;
break;
}
/* Window Events */
case SDL_WINDOWEVENT:
switch (evt.window.event)
@ -470,6 +543,8 @@ void KeyPoll::Poll(void)
mousex = (raw_mousex - rect.x) * SCREEN_WIDTH_PIXELS / rect.w;
mousey = (raw_mousey - rect.y) * SCREEN_HEIGHT_PIXELS / rect.h;
touch::update_buttons();
}
bool KeyPoll::isDown(SDL_Keycode key)

View File

@ -72,6 +72,8 @@ public:
bool linealreadyemptykludge;
bool using_touch;
private:
std::map<SDL_JoystickID, SDL_GameController*> controllers;
std::map<SDL_GameControllerButton, bool> buttonmap;

View File

@ -2411,12 +2411,19 @@ void gamerender(void)
if (game.advancetext)
{
char buffer_adv[SCREEN_WIDTH_CHARS + 1];
vformat_buf(
buffer_adv, sizeof(buffer_adv),
loc::gettext("- Press {button} to advance text -"),
"button:but",
vformat_button(ActionSet_InGame, Action_InGame_ACTION)
);
if (key.using_touch)
{
SDL_strlcpy(buffer_adv, loc::gettext("- Tap screen to advance text -"), sizeof(buffer_adv));
}
else
{
vformat_buf(
buffer_adv, sizeof(buffer_adv),
loc::gettext("- Press {button} to advance text -"),
"button:but",
vformat_button(ActionSet_InGame, Action_InGame_ACTION)
);
}
font::print(PR_CEN | PR_BOR, -1, graphics.flipmode ? 228 : 5, buffer_adv, 220 - (help.glow), 220 - (help.glow), 255 - (help.glow / 2));
}
@ -3486,12 +3493,19 @@ void teleporterrender(void)
if (game.advancetext)
{
char buffer_adv[SCREEN_WIDTH_CHARS + 1];
vformat_buf(
buffer_adv, sizeof(buffer_adv),
loc::gettext("- Press {button} to advance text -"),
"button:but",
vformat_button(ActionSet_InGame, Action_InGame_ACTION)
);
if (key.using_touch)
{
SDL_strlcpy(buffer_adv, loc::gettext("- Tap screen to advance text -"), sizeof(buffer_adv));
}
else
{
vformat_buf(
buffer_adv, sizeof(buffer_adv),
loc::gettext("- Press {button} to advance text -"),
"button:but",
vformat_button(ActionSet_InGame, Action_InGame_ACTION)
);
}
font::print(PR_CEN | PR_BOR, -1, graphics.flipmode ? 228 : 5, buffer_adv, 220 - (help.glow), 220 - (help.glow), 255 - (help.glow / 2));
}

View File

@ -20,6 +20,7 @@
#include "LocalizationStorage.h"
#include "Map.h"
#include "Music.h"
#include "Touch.h"
#include "Unreachable.h"
#include "UtilityClass.h"
#include "VFormat.h"
@ -820,7 +821,7 @@ void scriptclass::run(void)
game.hascontrol = false;
game.pausescript = true;
if (key.isDown(90) || key.isDown(32) || key.isDown(86)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)) game.jumpheld = true;
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || touch::screen_tapped()) game.jumpheld = true;
}
game.backgroundtext = false;
@ -1863,7 +1864,7 @@ void scriptclass::run(void)
game.hascontrol = false;
game.pausescript = true;
if (key.isDown(90) || key.isDown(32) || key.isDown(86)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)) game.jumpheld = true;
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || touch::screen_tapped()) game.jumpheld = true;
}
game.backgroundtext = false;
}
@ -1887,7 +1888,7 @@ void scriptclass::run(void)
game.hascontrol = false;
game.pausescript = true;
if (key.isDown(90) || key.isDown(32) || key.isDown(86)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)) game.jumpheld = true;
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || touch::screen_tapped()) game.jumpheld = true;
}
game.backgroundtext = false;
}
@ -1907,7 +1908,7 @@ void scriptclass::run(void)
game.hascontrol = false;
game.pausescript = true;
if (key.isDown(90) || key.isDown(32) || key.isDown(86)
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN)) game.jumpheld = true;
|| key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_DOWN) || touch::screen_tapped()) game.jumpheld = true;
}
game.backgroundtext = false;
}

View File

@ -0,0 +1,233 @@
#include "Touch.h"
#include <SDL.h>
#include <vector>
#include "Constants.h"
#include "Graphics.h"
#include "GraphicsResources.h"
#include "KeyPoll.h"
#include "Screen.h"
#include "Script.h"
#include "UtilityClass.h"
namespace touch
{
std::vector<VVV_Finger> fingers;
TouchButton buttons[NUM_TOUCH_BUTTONS];
int get_rect(TouchButton* button, SDL_Rect* rect)
{
rect->x = button->x;
rect->y = button->y;
rect->w = button->width;
rect->h = button->height;
return 0;
}
int get_scale(void)
{
SDL_Rect rect;
graphics.get_stretch_info(&rect);
int scale_x = rect.w / SCREEN_WIDTH_PIXELS;
int scale_y = rect.h / SCREEN_HEIGHT_PIXELS;
return SDL_min(scale_x, scale_y) * 2;
}
void refresh_buttons(void)
{
int width;
int height;
int scale = get_scale();
gameScreen.GetScreenSize(&width, &height);
buttons[TOUCH_BUTTON_LEFT].x = 0;
buttons[TOUCH_BUTTON_LEFT].y = height - (40 * scale) - 8;
buttons[TOUCH_BUTTON_LEFT].width = 40 * scale;
buttons[TOUCH_BUTTON_LEFT].height = 40 * scale;
buttons[TOUCH_BUTTON_LEFT].image = graphics.grphx.im_button_left;
buttons[TOUCH_BUTTON_RIGHT].x = (40 * scale) + 8;
buttons[TOUCH_BUTTON_RIGHT].y = height - (40 * scale) - 8;
buttons[TOUCH_BUTTON_RIGHT].width = 40 * scale;
buttons[TOUCH_BUTTON_RIGHT].height = 40 * scale;
buttons[TOUCH_BUTTON_RIGHT].image = graphics.grphx.im_button_right;
buttons[TOUCH_BUTTON_MAP].x = width - (35 * scale);
buttons[TOUCH_BUTTON_MAP].y = 0;
buttons[TOUCH_BUTTON_MAP].width = 35 * scale;
buttons[TOUCH_BUTTON_MAP].height = 30 * scale;
buttons[TOUCH_BUTTON_MAP].image = graphics.grphx.im_button_map;
buttons[TOUCH_BUTTON_CANCEL].x = width - (40 * scale);
buttons[TOUCH_BUTTON_CANCEL].y = height - (40 * scale * 2) - 16;
buttons[TOUCH_BUTTON_CANCEL].width = 40 * scale;
buttons[TOUCH_BUTTON_CANCEL].height = 40 * scale;
buttons[TOUCH_BUTTON_CANCEL].image = graphics.grphx.im_button_left;
buttons[TOUCH_BUTTON_CONFIRM].x = width - (40 * scale);
buttons[TOUCH_BUTTON_CONFIRM].y = height - (40 * scale) - 8;
buttons[TOUCH_BUTTON_CONFIRM].width = 40 * scale;
buttons[TOUCH_BUTTON_CONFIRM].height = 40 * scale;
buttons[TOUCH_BUTTON_CONFIRM].image = graphics.grphx.im_button_right;
// First, reset all buttons
for (int i = 0; i < NUM_TOUCH_BUTTONS; i++)
{
buttons[i].active = false;
}
// Now, set the buttons that are active
switch (game.gamestate)
{
case GAMEMODE:
if (!script.running && game.hascontrol)
{
buttons[TOUCH_BUTTON_LEFT].active = true;
buttons[TOUCH_BUTTON_RIGHT].active = true;
buttons[TOUCH_BUTTON_MAP].active = true;
}
break;
case TITLEMODE:
if (game.menustart)
{
buttons[TOUCH_BUTTON_LEFT].active = true;
buttons[TOUCH_BUTTON_RIGHT].active = true;
buttons[TOUCH_BUTTON_CANCEL].active = true;
buttons[TOUCH_BUTTON_CONFIRM].active = true;
}
break;
case TELEPORTERMODE:
if (game.useteleporter)
{
buttons[TOUCH_BUTTON_LEFT].active = true;
buttons[TOUCH_BUTTON_RIGHT].active = true;
buttons[TOUCH_BUTTON_CANCEL].active = true;
buttons[TOUCH_BUTTON_CONFIRM].active = true;
}
break;
case MAPMODE:
buttons[TOUCH_BUTTON_LEFT].active = true;
buttons[TOUCH_BUTTON_RIGHT].active = true;
buttons[TOUCH_BUTTON_CANCEL].active = true;
buttons[TOUCH_BUTTON_CONFIRM].active = true;
break;
case GAMECOMPLETE:
case GAMECOMPLETE2:
case EDITORMODE:
case PRELOADER:
default:
break;
}
}
void render(void)
{
if (!key.using_touch)
{
return;
}
int scale = get_scale();
refresh_buttons();
for (int i = 0; i < NUM_TOUCH_BUTTONS; i++)
{
SDL_Rect rect;
get_rect(&buttons[i], &rect);
if (buttons[i].image != NULL && buttons[i].active)
{
graphics.draw_texture(buttons[i].image, rect.x, rect.y + (buttons[i].down ? 2 * scale : 0), scale, scale);
}
}
}
void reset(void)
{
for (int i = 0; i < NUM_TOUCH_BUTTONS; i++)
{
buttons[i].down = false;
buttons[i].finger = NULL;
}
for (int i = 0; i < fingers.size(); i++)
{
fingers[i].pressed = false;
fingers[i].on_button = false;
}
}
void update_buttons(void)
{
for (int i = 0; i < fingers.size(); i++)
{
fingers[i].on_button = false;
SDL_Point point = { fingers[i].x, fingers[i].y };
SDL_Rect rect;
for (int j = 0; j < NUM_TOUCH_BUTTONS; j++)
{
get_rect(&buttons[j], &rect);
if (SDL_PointInRect(&point, &rect) && buttons[j].active)
{
buttons[j].down = true;
buttons[j].finger = &fingers[i];
fingers[i].on_button = true;
break;
}
}
}
}
bool button_tapped(TouchButtonID button)
{
if (key.using_touch && buttons[button].active && buttons[button].down && buttons[button].finger != NULL)
{
return buttons[button].finger->pressed;
}
return false;
}
bool touching_right(void)
{
int width;
int height;
gameScreen.GetScreenSize(&width, &height);
for (int i = 0; i < fingers.size(); i++)
{
if (fingers[i].on_button)
{
continue;
}
if (fingers[i].x > width / 2)
{
return true;
}
}
return false;
}
bool screen_tapped(void)
{
for (int i = 0; i < fingers.size(); i++)
{
if (fingers[i].on_button)
{
continue;
}
return fingers[i].pressed;
}
return false;
}
}

View File

@ -0,0 +1,61 @@
#ifndef TOUCH_H
#define TOUCH_H
#include <SDL.h>
#include <vector>
struct VVV_Finger
{
float x;
float y;
bool pressed;
bool on_button;
SDL_FingerID id;
};
enum TouchButtonID
{
/* General */
TOUCH_BUTTON_LEFT,
TOUCH_BUTTON_RIGHT,
/* Gameplay */
TOUCH_BUTTON_MAP,
/* Menus */
TOUCH_BUTTON_CANCEL,
TOUCH_BUTTON_CONFIRM,
NUM_TOUCH_BUTTONS
};
struct TouchButton
{
int x;
int y;
int width;
int height;
bool down;
bool active;
SDL_Texture* image;
VVV_Finger* finger;
};
namespace touch
{
extern std::vector<VVV_Finger> fingers;
extern TouchButton buttons[NUM_TOUCH_BUTTONS];
void refresh_buttons(void);
void reset(void);
void update_buttons(void);
void render(void);
bool button_tapped(TouchButtonID button);
bool touching_right(void);
bool screen_tapped(void);
}
#endif /* TOUCH_H */

View File

@ -805,6 +805,11 @@ int main(int argc, char *argv[])
key.isActive = true;
if (SDL_GetNumTouchDevices() > 0)
{
key.using_touch = true;
}
gamestate_funcs = get_gamestate_funcs(game.gamestate, &num_gamestate_funcs);
loop_assign_active_funcs();

View File

@ -7,6 +7,7 @@
#include "KeyPoll.h"
#include "Localization.h"
#include "Maths.h"
#include "Touch.h"
#include "UtilityClass.h"
#include "VFormat.h"
@ -23,7 +24,7 @@ void preloaderinput(void)
{
game.press_action = false;
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip)) {
if (key.isDown(KEYBOARD_z) || key.isDown(KEYBOARD_SPACE) || key.isDown(KEYBOARD_v) || key.isDown(game.controllerButton_flip) || touch::screen_tapped()) {
game.press_action = true;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 681 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 720 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 666 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 630 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 655 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 656 B