Skip to content

Commit

Permalink
Merge pull request #9 from Quest84/master
Browse files Browse the repository at this point in the history
Simple touch controls
  • Loading branch information
SeanOMik authored Jul 30, 2021
2 parents a2152f2 + 810dced commit d6ef58d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ INCLUDES := include include/menus/book include/menus/book-chooser include/he
ROMFS := romfs

VERSION_MAJOR := 0
VERSION_MINOR := 3
VERSION_MINOR := 4
VERSION_MICRO := 0

APP_TITLE := eBookReader
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Dark and light mode
* Landscape reading view
* Portrait reading view
* Touch screen controls
* Touch the botton/top of the screen to zoom in/out and left and right to change the page.

### TODO:
* Do some extra testing on file compatibility.
* 2 pages side by side in landscape.
* Touch screen for going to next page.
* Hardware lock to prevent accidental touches (maybe Vol- ?) (?).
* Save orientation, and dark mode settings.

Expand Down Expand Up @@ -45,7 +46,7 @@ Light Mode Landscape Reading:
* NX-Shell Team - A good amount of the code is from an old version of their application.

### Building
* Release built with [libnx release v.4.1.3](https://github.com/switchbrew/libnx).
* Release built with [libnx release v4.1.3](https://github.com/switchbrew/libnx).
* Uses `freetype` and other libs which comes with `switch-portlibs` via `devkitPro pacman`:
```
pacman -S libnx switch-portlibs
Expand All @@ -57,7 +58,7 @@ make
```
to build.

If you don't have twili debugger installed then delete the -ltwili flag on the Makefile to compile:
If you don't have twili debugger installed, delete the `-ltwili` flag on the Makefile to compile:
```
LIBS: -ltwili
```
26 changes: 12 additions & 14 deletions source/menus/book-chooser/MenuChooser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void Menu_StartChoosing() {
SDL_GetWindowSize(WINDOW, &windowX, &windowY);
int warningWidth = 700;
int warningHeight = 300;

padConfigureInput(1, HidNpadStyleSet_NpadStandard);

PadState pad;
padInitializeDefault(&pad);

while(appletMainLoop()) {
if (readingBook) {
break;
Expand All @@ -59,23 +65,15 @@ void Menu_StartChoosing() {
SDL_ClearScreen(RENDERER, backColor);
SDL_RenderClear(RENDERER);

//hidScanInput();
padConfigureInput(1, HidNpadStyleSet_NpadStandard);

PadState pad;
padInitializeDefault(&pad);

padUpdate(&pad);

//u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
//u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);

u64 kDown = padGetButtonsDown(&pad);
u64 kHeld = padGetButtons(&pad);
u64 kUp = padGetButtonsUp(&pad);

/*if (!isWarningOnScreen && kDown & KEY_PLUS) {
if (!isWarningOnScreen && kDown & HidNpadButton_Plus) {
break;
}*/
}

if (kDown & HidNpadButton_B) {
if (!isWarningOnScreen) {
Expand Down Expand Up @@ -118,23 +116,23 @@ void Menu_StartChoosing() {
}
}

if (kDown & HidNpadButton_Up) {
if (kDown & HidNpadButton_Up || kDown & HidNpadButton_StickRUp) {
if (choosenIndex != 0 && !isWarningOnScreen) {
choosenIndex--;
} else if (choosenIndex == 0) {
choosenIndex = amountOfFiles-1;
}
}

if (kDown & HidNpadButton_Down) {
if (kDown & HidNpadButton_Down || kDown & HidNpadButton_StickRDown) {
if (choosenIndex == amountOfFiles-1) {
choosenIndex = 0;
} else if (choosenIndex < amountOfFiles-1 && !isWarningOnScreen) {
choosenIndex++;
}
}

if (kDown & HidNpadButton_Minus) {
if (kUp & HidNpadButton_Minus) {
configDarkMode = !configDarkMode;
}

Expand Down
2 changes: 1 addition & 1 deletion source/menus/book/BookReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <mupdf/pdf.h>
#include <string>
#include "PageLayout.hpp"

#include <switch.h>
struct SDL_Texture;

typedef enum {
Expand Down
59 changes: 47 additions & 12 deletions source/menus/book/menu_book_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ void Menu_OpenBook(char *path) {

/*TouchInfo touchInfo;
Touch_Init(&touchInfo);*/

hidInitializeTouchScreen();

s32 prev_touchcount=0;
bool helpMenu = false;

// Configure our supported input layout: a single player with standard controller syles
Expand All @@ -34,17 +36,46 @@ void Menu_OpenBook(char *path) {
while(result >= 0 && appletMainLoop()) {
reader->draw(helpMenu);

//hidScanInput();

//u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
//u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);

padUpdate(&pad);

u64 kDown = padGetButtonsDown(&pad);
u64 kHeld = padGetButtons(&pad);
u64 kUp = padGetButtonsUp(&pad);

HidTouchScreenState state={0};

if(hidGetTouchScreenStates(&state, 1)) {
if(state.count != prev_touchcount) {
prev_touchcount = state.count;
}
}

for(s32 i=0; i<state.count; i++) {
if (state.touches[i].x > 1000 && (state.touches[i].y > 200 && state.touches[i].y < 500))
if (reader->currentPageLayout() == BookPageLayoutPortrait)
reader->next_page(1);
else if (reader->currentPageLayout() == BookPageLayoutLandscape)
reader->zoom_in();

if (state.touches[i].x < 280 && (state.touches[i].y > 200 && state.touches[i].y < 500))
if (reader->currentPageLayout() == BookPageLayoutPortrait)
reader->previous_page(1);
else if (reader->currentPageLayout() == BookPageLayoutLandscape)
reader->zoom_out();

if (state.touches[i].y < 200)
if (reader->currentPageLayout() == BookPageLayoutPortrait)
reader->zoom_in();
else if (reader->currentPageLayout() == BookPageLayoutLandscape)
reader->previous_page(1);

if (state.touches[i].y > 500)
if (reader->currentPageLayout() == BookPageLayoutPortrait)
reader->zoom_out();
else if (reader->currentPageLayout() == BookPageLayoutLandscape)
reader->next_page(1);
}

if (!helpMenu && kDown & HidNpadButton_Left) {
if (reader->currentPageLayout() == BookPageLayoutPortrait ) {
reader->previous_page(1);
Expand Down Expand Up @@ -83,27 +114,30 @@ void Menu_OpenBook(char *path) {
if (reader->currentPageLayout() == BookPageLayoutPortrait ) {
reader->move_page_up();
} else if ((reader->currentPageLayout() == BookPageLayoutLandscape) ) {
reader->move_page_left();
reader->move_page_right();
}
} else if (!helpMenu && kHeld & HidNpadButton_StickLDown) {
if (reader->currentPageLayout() == BookPageLayoutPortrait ) {
reader->move_page_down();
} else if ((reader->currentPageLayout() == BookPageLayoutLandscape) ) {
reader->move_page_right();
reader->move_page_left();
}
} else if (!helpMenu && kHeld & HidNpadButton_StickLRight) {
if ((reader->currentPageLayout() == BookPageLayoutLandscape) ) {
//reader->move_page_up();
reader->move_page_down();
}
} else if (!helpMenu && kHeld & HidNpadButton_StickLLeft) {
if ((reader->currentPageLayout() == BookPageLayoutLandscape) ) {
//reader->move_page_down();
reader->move_page_up();
}
}

if (kDown & HidNpadButton_B) {
if (!helpMenu && kDown & HidNpadButton_LeftSR)
reader->next_page(10);
else if (!helpMenu && kDown & HidNpadButton_LeftSL)
reader->previous_page(10);

if (kUp & HidNpadButton_B) {
if (helpMenu) {
helpMenu = !helpMenu;
} else {
Expand All @@ -115,7 +149,7 @@ void Menu_OpenBook(char *path) {
reader->permStatusBar = !reader->permStatusBar;
}

if (!helpMenu && kDown & HidNpadButton_StickL || kDown & HidNpadButton_StickR) {
if ((!helpMenu && kDown & HidNpadButton_StickL) || kDown & HidNpadButton_StickR) {
reader->reset_page();
}

Expand Down Expand Up @@ -157,4 +191,5 @@ void Menu_OpenBook(char *path) {
std::cout << "Opening chooser" << std::endl;
Menu_StartChoosing();
delete reader;
// consoleExit(NULL);
}
12 changes: 4 additions & 8 deletions source/status_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,12 @@ static void StatusBar_GetBatteryStatus(int x, int y) {

snprintf(buf, 5, "%d%%", percent);
TTF_SizeText(ROBOTO_20, buf, &width, NULL);
SDL_DrawHorizonalAlignedImageText(RENDERER, batteryImage, ROBOTO_20, WHITE, buf, (x + width + 5), y, 34, 34, -2, -7);
SDL_DrawHorizonalAlignedImageText(RENDERER, batteryImage, ROBOTO_20, WHITE, buf, (x + width - 30), y, 34, 34, -2, 0);
//SDL_DrawText(RENDERER, ROBOTO_20, (x + width + 5), y, WHITE, buf);
} else {
snprintf(buf, 5, "%d%%", percent);
TTF_SizeText(ROBOTO_20, buf, &width, NULL);
SDL_DrawHorizonalAlignedImageText(RENDERER, battery_unknown, ROBOTO_20, WHITE, buf, x, y, 34, 34, -2, -7);

SDL_DrawHorizonalAlignedImageText(RENDERER, battery_unknown, ROBOTO_20, WHITE, buf, x, y, 34, 34, -2, 0);
/*SDL_DrawText(RENDERER, ROBOTO_20, (x + width + 5), y, WHITE, buf);
SDL_DrawImage(RENDERER, battery_unknown, x, 1);*/
}
Expand All @@ -126,19 +125,16 @@ void StatusBar_DisplayTime(bool portriat) {

if (portriat) {
int timeX = (1280 - timeWidth) + timeHeight;
int timeY = (720 - timeWidth) + 15;
SDL_DrawRotatedText(RENDERER, ROBOTO_25, (double) 90, timeX, timeY, WHITE, Clock_GetCurrentTime());
int timeY = (720 - timeWidth) + 15;
SDL_DrawRotatedText(RENDERER, ROBOTO_25, (double) 90, timeX, timeY, WHITE, Clock_GetCurrentTime());

int helpX = (1280 - helpWidth) + 21;
int helpY = (720 - helpHeight) - (720 - timeY) - 75;
SDL_DrawRotatedText(RENDERER, ROBOTO_20, (double) 90, helpX, helpY, WHITE, "\"+\" - Help");

//SDL_DrawRotatedText(RENDERER, ROBOTO_25, (double) 90, 1270 - width, (720 - height), WHITE, Clock_GetCurrentTime());
} else {
SDL_DrawText(RENDERER, ROBOTO_25, 1260 - timeWidth, (40 - timeHeight) / 2, WHITE, Clock_GetCurrentTime());

SDL_DrawText(RENDERER, ROBOTO_20, 1260 - helpWidth - timeWidth - 25, (40 - helpHeight) / 2, WHITE, "\"+\" - Help");

StatusBar_GetBatteryStatus(1260 - (timeWidth + helpWidth) - 110, (40 - timeHeight) / 2 + 34); // 34 is height of battery img
}
}

0 comments on commit d6ef58d

Please sign in to comment.