Skip to content

Commit

Permalink
New shortcuts, settings and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
karwler committed Aug 22, 2018
1 parent 36b2394 commit 5e762a9
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 188 deletions.
2 changes: 2 additions & 0 deletions rsc/data/languages/Deutsch.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ok=ok
set=einstellen
up=hoch
direction=Richtung
zoom=Zoom
spacing=Abstand
fullscreen=Vollbild
size=Größe
portrait=Porträt
Expand Down
2 changes: 2 additions & 0 deletions rsc/data/languages/Polski.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ok=ok
set=ustaw
up=do góry
direction=kierunek
zoom=powiększenie
spacing=odstęp
fullscreen=pełny ekran
size=rozmiar
portrait=portret
Expand Down
2 changes: 2 additions & 0 deletions rsc/data/languages/Русский.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ok=хорошо
set=устанавливай
up=вверх
direction=направление
zoom=зум
spacing=пространство
fullscreen=полноэкранный
size=размер
portrait=портрет
Expand Down
7 changes: 6 additions & 1 deletion src/engine/drawSys.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "world.h"
#include <SDL2/SDL_image.h>
#include <archive.h>
#include <archive_entry.h>
#include <algorithm>
#include <iostream>

// FONT SET

Expand Down Expand Up @@ -55,7 +60,7 @@ DrawSys::DrawSys(SDL_Window* window, int driverIndex) {
// create and set up renderer
renderer = SDL_CreateRenderer(window, driverIndex, Default::rendererFlags);
if (!renderer)
throw "Couldn't create renderer:\n" + string(SDL_GetError());
throw std::exception(string("Couldn't create renderer:\n" + string(SDL_GetError())).c_str());
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

// load default textures with colors and initialize fonts and translations
Expand Down
1 change: 1 addition & 0 deletions src/engine/drawSys.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "utils/layouts.h"
#include <SDL2/SDL_ttf.h>

// loads different font sizes from one file
class FontSet {
Expand Down
44 changes: 31 additions & 13 deletions src/engine/filer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "world.h"
#include <SDL2/SDL_image.h>
#include <archive.h>
#include <archive_entry.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -110,8 +115,8 @@ const string Filer::dirExec = appendDsep(Filer::getExecDir());
const vector<string> Filer::dirFonts = {Filer::dirExec, wgetenv("SystemDrive") + "\\Windows\\Fonts\\"};
const string Filer::dirSets = wgetenv("AppData") + "\\" + Default::titleDefault + "\\";
#else
const vector<string> Filer::dirFonts = {Filer::dirExec, "/usr/share/fonts/", string(getenv("HOME")) + "/.fonts/"};
const string Filer::dirSets = string(getenv("HOME")) + "/." + Default::titleExtra + "/";
const vector<string> Filer::dirFonts = {Filer::dirExec, "/usr/share/fonts/", string(std::getenv("HOME")) + "/.fonts/"};
const string Filer::dirSets = string(std::getenv("HOME")) + "/." + Default::titleExtra + "/";
#endif
const string Filer::dirLangs = Filer::dirExec + Default::dirLanguages + dsep;
const string Filer::dirTexs = Filer::dirExec + Default::dirTextures + dsep;
Expand Down Expand Up @@ -237,7 +242,11 @@ Settings Filer::getSettings() {
else if (il.getArg() == Default::iniKeywordResolution)
sets.setResolution(il.getVal());
else if (il.getArg() == Default::iniKeywordDirection)
sets.direction = strToEnum<Direction::Dir>(Default::directionNames, il.getVal());
sets.direction.set(il.getVal());
else if (il.getArg() == Default::iniKeywordZoom)
sets.zoom = stof(il.getVal());
else if (il.getArg() == Default::iniKeywordSpacing)
sets.spacing = stoi(il.getVal());
else if (il.getArg() == Default::iniKeywordFont)
sets.setFont(il.getVal());
else if (il.getArg() == Default::iniKeywordLanguage)
Expand All @@ -261,7 +270,9 @@ void Filer::saveSettings(const Settings& sets) {
IniLine::line(Default::iniKeywordMaximized, btos(sets.maximized)),
IniLine::line(Default::iniKeywordFullscreen, btos(sets.fullscreen)),
IniLine::line(Default::iniKeywordResolution, sets.getResolutionString()),
IniLine::line(Default::iniKeywordDirection, enumToStr(Default::directionNames, sets.direction)),
IniLine::line(Default::iniKeywordDirection, sets.direction.toString()),
IniLine::line(Default::iniKeywordZoom, ntos(sets.zoom)),
IniLine::line(Default::iniKeywordSpacing, ntos(sets.spacing)),
IniLine::line(Default::iniKeywordFont, sets.getFont()),
IniLine::line(Default::iniKeywordLanguage, sets.getLang()),
IniLine::line(Default::iniKeywordTheme, sets.getTheme()),
Expand All @@ -284,10 +295,13 @@ vector<Binding> Filer::getBindings() {

for (string& line : lines) {
IniLine il(line);
if (il.getType() != IniLine::Type::argKeyVal || il.getVal().size() < 3)
if (il.getType() != IniLine::Type::argVal || il.getVal().length() < 3)
continue;

sizt bid = strToEnum<sizt>(Default::bindingNames, il.getArg());
if (bid >= bindings.size())
continue;

switch (toupper(il.getVal()[0])) {
case 'K': // keyboard key
bindings[bid].setKey(SDL_GetScancodeFromName(il.getVal().substr(2).c_str()));
Expand All @@ -296,7 +310,7 @@ vector<Binding> Filer::getBindings() {
bindings[bid].setJbutton(stoi(il.getVal().substr(2)));
break;
case 'H': // joystick hat
for (sizt i = 2; i < il.getVal().size(); i++)
for (sizt i = 2; i < il.getVal().length(); i++)
if (il.getVal()[i] < '0' || il.getVal()[i] > '9') {
bindings[bid].setJhat(stoi(il.getVal().substr(2, i-2)), jtStrToHat(il.getVal().substr(i+1)));
break;
Expand All @@ -305,12 +319,16 @@ vector<Binding> Filer::getBindings() {
case 'A': // joystick axis
bindings[bid].setJaxis(stoi(il.getVal().substr(3)), (il.getVal()[2] != '-'));
break;
case 'G': // gamepad button
bindings[bid].setGbutton(strToEnum<SDL_GameControllerButton>(Default::gbuttonNames, il.getVal().substr(2)));
break;
case 'X': // gamepad axis
bindings[bid].setGaxis(strToEnum<SDL_GameControllerAxis>(Default::gaxisNames, il.getVal().substr(3)), (il.getVal()[2] != '-'));
}
case 'G': { // gamepad button
SDL_GameControllerButton cid = strToEnum<SDL_GameControllerButton>(Default::gbuttonNames, il.getVal().substr(2));
if (cid < SDL_CONTROLLER_BUTTON_MAX)
bindings[bid].setGbutton(cid);
break; }
case 'X': { // gamepad axis
SDL_GameControllerAxis cid = strToEnum<SDL_GameControllerAxis>(Default::gaxisNames, il.getVal().substr(3));
if (cid < SDL_CONTROLLER_AXIS_MAX)
bindings[bid].setGaxis(cid, (il.getVal()[2] != '-'));
} }
}
return bindings;
}
Expand Down Expand Up @@ -607,7 +625,7 @@ string Filer::findFont(const string& font) {

for (const string& drc : dirFonts) // check font directories
for (string& it : listDirRecursively(drc))
if (!strcicmp(hasExt(it) ? delExt(filename(it)).c_str() : filename(it).c_str(), font.c_str()) && isFont(it))
if (!strcicmp(hasExt(it) ? delExt(filename(it)) : filename(it), font) && isFont(it))
return it;
return ""; // nothing found
}
Expand Down
5 changes: 2 additions & 3 deletions src/engine/inputSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ void InputSys::checkBindingsX(SDL_GameControllerAxis gaxis, bool positive) {
}

bool InputSys::isPressed(Binding::Type type, float& amt) const {
sizt i = static_cast<sizt>(type);
return bindings[i].isAxis() ? isPressed(bindings[i], amt) : false;
return bindings[static_cast<sizt>(type)].isAxis() ? isPressed(bindings[static_cast<sizt>(type)], amt) : false;
}

bool InputSys::isPressed(const Binding& abind, float& amt) const {
Expand Down Expand Up @@ -260,7 +259,7 @@ void InputSys::removeController(int id) {
}

int InputSys::checkAxisValue(int value) const {
return abs(value) > World::winSys()->sets.getDeadzone() ? value : 0;
return std::abs(value) > World::winSys()->sets.getDeadzone() ? value : 0;
}

float InputSys::axisToFloat(int axisValue) {
Expand Down
14 changes: 8 additions & 6 deletions src/engine/windowSys.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "world.h"
#include <SDL2/SDL_image.h>
#include <iostream>

WindowSys::WindowSys() :
run(true),
Expand All @@ -10,8 +12,8 @@ int WindowSys::start() {
try {
init();
exec();
} catch (const string& str) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", str.c_str(), window);
} catch (const std::exception& e) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", e.what(), window);
} catch (...) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Unknown error.", window);
}
Expand All @@ -21,9 +23,9 @@ int WindowSys::start() {

void WindowSys::init() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER))
throw "Couldn't initialize SDL:\n" + string(SDL_GetError());
throw std::exception(string("Couldn't initialize SDL:\n" + string(SDL_GetError())).c_str());
if (TTF_Init())
throw "Couldn't initialize fonts:\n" + string(SDL_GetError());
throw std::exception(string("Couldn't initialize fonts:\n" + string(SDL_GetError())).c_str());

int flags = IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP);
if (!(flags & IMG_INIT_JPG))
Expand Down Expand Up @@ -53,7 +55,7 @@ void WindowSys::init() {
inputSys.reset(new InputSys);
scene.reset(new Scene);
program.reset(new Program);
program->init();
program->start();
}

void WindowSys::exec() {
Expand Down Expand Up @@ -104,7 +106,7 @@ void WindowSys::createWindow() {

window = SDL_CreateWindow(Default::titleDefault, Default::windowPos.x, Default::windowPos.y, sets.resolution.x, sets.resolution.y, flags);
if (!window)
throw "Couldn't create window:\n" + string(SDL_GetError());
throw std::exception(string("Couldn't create window:\n" + string(SDL_GetError())).c_str());

// minor stuff
SDL_Surface* icon = IMG_Load(string(Filer::dirExec + Default::fileIcon).c_str());
Expand Down
48 changes: 28 additions & 20 deletions src/prog/defaults.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
#pragma once

// stuff that's used pretty much everywhere
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <archive.h>
#include <archive_entry.h>
#include "utils/vec2.h"

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <SDL2/SDL.h>
#include <string>
#include <sstream>
#include <vector>
Expand Down Expand Up @@ -73,6 +65,7 @@ class Layout;
class Program;
class ProgState;

// events
using PCall = void (Program::*)(Button*);
using SBCall = void (ProgState::*)();
using SACall = void (ProgState::*)(float);
Expand All @@ -92,6 +85,8 @@ namespace Default {
const char language[] = "English";
const bool maximized = false;
const bool fullscreen = false;
const float zoom = 1.f;
const int spacing = 10;
const vec2i resolution(800, 600);
const char font[] = "Arial";
const vec2f scrollSpeed(800.f, 1000.f);
Expand Down Expand Up @@ -125,14 +120,17 @@ const SDL_Scancode keyRight = SDL_SCANCODE_RIGHT;
const SDL_Scancode keyCenterView = SDL_SCANCODE_C;
const SDL_Scancode keyScrollFast = SDL_SCANCODE_X;
const SDL_Scancode keyScrollSlow = SDL_SCANCODE_Z;
const SDL_Scancode keyPageUp = SDL_SCANCODE_W;
const SDL_Scancode keyPageDown = SDL_SCANCODE_S;
const SDL_Scancode keyZoomIn = SDL_SCANCODE_D;
const SDL_Scancode keyZoomOut = SDL_SCANCODE_A;
const SDL_Scancode keyNextPage = SDL_SCANCODE_PAGEDOWN;
const SDL_Scancode keyPrevPage = SDL_SCANCODE_PAGEUP;
const SDL_Scancode keyZoomIn = SDL_SCANCODE_W;
const SDL_Scancode keyZoomOut = SDL_SCANCODE_S;
const SDL_Scancode keyZoomReset = SDL_SCANCODE_R;
const SDL_Scancode keyNextDir = SDL_SCANCODE_E;
const SDL_Scancode keyPrevDir = SDL_SCANCODE_Q;
const SDL_Scancode keyFullscreen = SDL_SCANCODE_F;
const SDL_Scancode keyToStart = SDL_SCANCODE_HOME;
const SDL_Scancode keyToEnd = SDL_SCANCODE_END;
const SDL_Scancode keyNextDir = SDL_SCANCODE_N;
const SDL_Scancode keyPrevDir = SDL_SCANCODE_B;
const SDL_Scancode keyFullscreen = SDL_SCANCODE_F11;
const SDL_Scancode keyRefresh = SDL_SCANCODE_F5;

// joystick bindings
const uint8 jbuttonEnter = 2;
Expand Down Expand Up @@ -184,7 +182,7 @@ const bool axisDirRight = true;
const bool axisDirLeft = false;

// widgets' properties
const int spacing = 5;
const int itemSpacing = 5;
const int itemHeight = 30;
const int sbarSize = 10;
const int checkboxSpacing = 5;
Expand Down Expand Up @@ -212,6 +210,8 @@ const char iniKeywordMaximized[] = "maximized";
const char iniKeywordFullscreen[] = "fullscreen";
const char iniKeywordResolution[] = "resolution";
const char iniKeywordDirection[] = "direction";
const char iniKeywordZoom[] = "zoom";
const char iniKeywordSpacing[] = "spacing";
const char iniKeywordFont[] = "font";
const char iniKeywordLanguage[] = "language";
const char iniKeywordTheme[] = "theme";
Expand Down Expand Up @@ -239,15 +239,19 @@ const vector<string> bindingNames = {
"center view",
"scroll fast",
"scroll slow",
"page up",
"page down",
"next page",
"prev page",
"zoom in",
"zoom out",
"zoom reset",
"to start",
"to end",
"next directory",
"prev directory",
"fullscreen"
"fullscreen",
"refresh"
};

const map<uint8, string> hatNames = {
pair<uint8, string>(SDL_HAT_CENTERED, "Center"),
pair<uint8, string>(SDL_HAT_UP, "Up"),
Expand All @@ -259,6 +263,7 @@ const map<uint8, string> hatNames = {
pair<uint8, string>(SDL_HAT_LEFTDOWN, "Left-Down"),
pair<uint8, string>(SDL_HAT_LEFTUP, "Left-Up")
};

const vector<string> gbuttonNames = {
"A",
"B",
Expand All @@ -276,6 +281,7 @@ const vector<string> gbuttonNames = {
"Left",
"Right"
};

const vector<string> gaxisNames = {
"LX",
"LY",
Expand All @@ -284,6 +290,7 @@ const vector<string> gaxisNames = {
"LT",
"RT"
};

const vector<string> colorNames = {
"background",
"normal",
Expand All @@ -293,6 +300,7 @@ const vector<string> colorNames = {
"text",
"texture"
};

const vector<string> directionNames = {
"Up",
"Down",
Expand Down
16 changes: 12 additions & 4 deletions src/prog/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Program::Program() :
state(new ProgState) // necessary as a placeholder to prevent nullptr exceptions
{}

void Program::init() {
void Program::start() {
if (!(World::getArgs().size() && openFile(World::getArg(0))))
eventOpenBookList();
}
Expand Down Expand Up @@ -38,7 +38,7 @@ void Program::eventOpenLastPage(Button* but) {

bool Program::openFile(string file) {
file = absolutePath(file);
if (Filer::isPicture(file)) {
if (Filer::isPicture(file) || Filer::isArchive(file)) {
browser.reset(new Browser(dseps, parentPath(file), &Program::eventOpenBookList));
if (startReader(filename(file)))
return true;
Expand Down Expand Up @@ -120,7 +120,15 @@ void Program::eventOpenSettings(Button* but) {
}

void Program::eventSwitchDirection(Button* but) {
World::winSys()->sets.direction = strToEnum<Direction::Dir>(Default::directionNames, static_cast<SwitchBox*>(but)->getText());
World::winSys()->sets.direction.set(static_cast<SwitchBox*>(but)->getText());
}

void Program::eventSetZoom(Button * but) {
World::winSys()->sets.zoom = stof(static_cast<LineEdit*>(but)->getText());
}

void Program::eventSetSpacing(Button* but) {
World::winSys()->sets.spacing = stoi(static_cast<LineEdit*>(but)->getText());
}

void Program::eventSwitchLanguage(Button* but) {
Expand Down Expand Up @@ -149,7 +157,7 @@ void Program::eventOpenLibDirBrowser(Button* but) {
#ifdef _WIN32
browser.reset(new Browser("\\", Filer::wgetenv("UserProfile"), &Program::eventOpenSettings));
#else
browser.reset(new Browser("/", getenv("HOME"), &Program::eventOpenSettings));
browser.reset(new Browser("/", std::getenv("HOME"), &Program::eventOpenSettings));
#endif
setState(new ProgSearchDir);
}
Expand Down
Loading

0 comments on commit 5e762a9

Please sign in to comment.