Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language selection for Koopair #151

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions koopair/LocalizationManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <fstream>
#include <string>
#include <map>
#include <json/json.h>

class LocalizationManager {
public:
static LocalizationManager& Instance() {
static LocalizationManager instance;
return instance;
}

bool LoadLanguage(const std::string& languageCode) {
std::ifstream file("translations/" + languageCode + ".json");
if (!file.is_open()) {
return false;
}

Json::Value root;
file >> root;
file.close();

mTranslations.clear();
for (const auto& key : root.getMemberNames()) {
mTranslations[key] = root[key].asString();
}
return true;
}

std::string Translate(const std::string& key) const {
auto it = mTranslations.find(key);
if (it != mTranslations.end()) {
return it->second;
}
return key;
}

private:
LocalizationManager() = default;
std::map<std::string, std::string> mTranslations;
};
67 changes: 67 additions & 0 deletions koopair/source/screens/LanguageSelectionScreen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "LanguageSelectionScreen.hpp"
#include "Gfx.hpp"

LanguageSelectionScreen::LanguageSelectionScreen()
: mSelectedLanguage(0) // Select the first language by default
{
mLanguages = {
{ "English", "en" },
{ "Deutsch", "de" },
};
}

LanguageSelectionScreen::~LanguageSelectionScreen()
{
}

void LanguageSelectionScreen::Draw()
{
DrawTopBar("Select Language");

for (size_t i = 0; i < mLanguages.size(); i++) {
int yOff = 75 + i * 150;
Gfx::DrawRectFilled(0, yOff, Gfx::SCREEN_WIDTH, 150, Gfx::COLOR_ALT_BACKGROUND);
Gfx::Print(Gfx::SCREEN_WIDTH / 2, yOff + 75, 60, Gfx::COLOR_TEXT, mLanguages[i].name.c_str(), Gfx::ALIGN_CENTER);

if (i == mSelectedLanguage) {
Gfx::DrawRect(0, yOff, Gfx::SCREEN_WIDTH, 150, 8, Gfx::COLOR_HIGHLIGHTED);
}
}

DrawBottomBar("\ue001 Back", nullptr, "\ue000 Select");
}

bool LanguageSelectionScreen::Update(const CombinedInputController& input)
{
if (input.GetButtonsTriggered() & Controller::BUTTON_B) {
return false;
}

if (input.GetButtonsTriggered() & Controller::BUTTON_DOWN) {
if (mSelectedLanguage < static_cast<int>(mLanguages.size()) - 1) {
mSelectedLanguage++;
}
} else if (input.GetButtonsTriggered() & Controller::BUTTON_UP) {
if (mSelectedLanguage > 0) {
mSelectedLanguage--;
}
}

if (input.GetButtonsTriggered() & Controller::BUTTON_B) {
return false;
}

if (input.GetButtonsTriggered() & Controller::BUTTON_DOWN) {
mSelectedLanguage = (mSelectedLanguage + 1) % mLanguages.size();
} else if (input.GetButtonsTriggered() & Controller::BUTTON_UP) {
mSelectedLanguage = (mSelectedLanguage - 1 + mLanguages.size()) % mLanguages.size();
}

if (input.GetButtonsTriggered() & Controller::BUTTON_A) {
const LanguageOption& selectedLanguage = mLanguages[mSelectedLanguage];
Configuration::SetLanguage(selectedLanguage.code);
return false;
}

return true;
}
32 changes: 32 additions & 0 deletions koopair/source/screens/LanguageSelectionScreen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* LanguageSelectionScreen.hpp
* Defines the page for language selection.
*/

#ifndef LANGUAGESELECTIONSCREEN_HPP
#define LANGUAGESELECTIONSCREEN_HPP

#include "Screen.hpp"
#include <vector>
#include <string>

class LanguageSelectionScreen : public Screen
{
public:
LanguageSelectionScreen();
virtual ~LanguageSelectionScreen();

void Draw() override;
bool Update(const CombinedInputController& input) override;

private:
struct LanguageOption {
std::string name;
std::string code;
};

std::vector<LanguageOption> mLanguages;
int mSelectedLanguage;
};

#endif // LANGUAGESELECTIONSCREEN_HPP
22 changes: 20 additions & 2 deletions koopair/source/screens/SettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
*/
#include "SettingsScreen.hpp"
#include "Gfx.hpp"
#include "LanguageSelectionScreen.hpp"

SettingsScreen::SettingsScreen()
: mSelectedOption(OPTION_LANGUAGE)
{
mEntries = {
{ OPTION_LANGUAGE, "Language Selection" },
// Further entries can be added here
};
}

SettingsScreen::~SettingsScreen()
Expand All @@ -29,9 +35,16 @@ void SettingsScreen::Draw()
{
DrawTopBar("Settings");

Gfx::Print(Gfx::SCREEN_WIDTH / 2, Gfx::SCREEN_HEIGHT / 2, 60, Gfx::COLOR_TEXT, "Nothing here yet :)", Gfx::ALIGN_CENTER);
int yOff = Gfx::SCREEN_HEIGHT / 2 - 75;
Gfx::DrawRectFilled(0, yOff, Gfx::SCREEN_WIDTH, 150, Gfx::COLOR_ALT_BACKGROUND);
Gfx::Print(Gfx::SCREEN_WIDTH / 2, yOff + 75, 60, Gfx::COLOR_TEXT, "Language Selection", Gfx::ALIGN_CENTER);

DrawBottomBar("\ue001 Back", nullptr, nullptr);
// Highlight the button when it is selected
if (mSelectedOption == OPTION_LANGUAGE) {
Gfx::DrawRect(0, yOff, Gfx::SCREEN_WIDTH, 150, 8, Gfx::COLOR_HIGHLIGHTED);
}

DrawBottomBar("\ue001 Back", nullptr, "\ue000 Select");
}

bool SettingsScreen::Update(const CombinedInputController& input)
Expand All @@ -40,5 +53,10 @@ bool SettingsScreen::Update(const CombinedInputController& input)
return false;
}

// If A is pressed and the Language button is selected, switch to the language selection page
if ((input.GetButtonsTriggered() & Controller::BUTTON_A) && mSelectedOption == OPTION_LANGUAGE) {
mSubscreen = std::make_unique<LanguageSelectionScreen>();
}

return true;
}
6 changes: 6 additions & 0 deletions koopair/translations/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"settings_title": "Einstellungen",
"language_selection": "Sprache auswählen",
"back": "Zurück",
"select": "Auswählen"
}
6 changes: 6 additions & 0 deletions koopair/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"settings_title": "Settings",
"language_selection": "Select Language",
"back": "Back",
"select": "Select"
}