Base refactor

This commit is contained in:
2023-11-14 09:16:48 -06:00
parent 214082d00f
commit 1817dcaf3a
410 changed files with 749 additions and 20823 deletions

View File

@ -1,11 +0,0 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
LocaleManager.cpp
)

View File

@ -1,82 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "LocaleManager.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
LocaleManager::LocaleManager(DawnGame *game) {
this->game = game;
this->locale.language = "en";
}
void LocaleManager::init() {
// Begin loading
if(this->currentlyLoadedAsset == nullptr && this->loadingAsset == nullptr) {
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
if(this->loadingAsset->loaded) {
this->onLanguageLoaded();
} else {
eventTeardownLanguageLoaded = useEvent([&]() {
this->onLanguageLoaded();
}, this->loadingAsset->eventLoaded);
}
}
}
void LocaleManager::setLocale(struct Locale locale) {
auto oldLocale = this->getLocale();
this->locale = locale;
this->eventLocaleChanged.invoke();
// Did the language change?
if(this->locale.language != oldLocale.language) {
// Yes, switch languages.
// Are we already loading something? Stop it.
if(this->loadingAsset != nullptr) {
eventTeardownLanguageLoaded();
this->game->assetManager.queueUnload(this->loadingAsset);
this->loadingAsset = nullptr;
}
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
if(this->loadingAsset->loaded) {
this->onLanguageLoaded();
} else {
eventTeardownLanguageLoaded = useEvent([&]() {
this->onLanguageLoaded();
}, this->loadingAsset->eventLoaded);
}
}
}
struct Locale LocaleManager::getLocale() {
return this->locale;
}
std::string LocaleManager::getString(std::string key) {
assertNotNull(this->currentlyLoadedAsset, "LocaleManager::getString: Currently loaded asset cannot be null");
return this->currentlyLoadedAsset->getValue(key);
}
void LocaleManager::onLanguageLoaded() {
// Unload the previously loaded language
if(this->currentlyLoadedAsset != nullptr) {
eventTeardownLanguageLoaded();
this->game->assetManager.queueUnload(this->currentlyLoadedAsset);
}
// Swap loaded languages
this->currentlyLoadedAsset = this->loadingAsset;
this->loadingAsset = nullptr;
// Notify.
this->eventLanguageUpdated.invoke();
}

View File

@ -1,70 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "state/StateEvent.hpp"
#include "asset/assets/LanguageAsset.hpp"
#include "state/StateOwner.hpp"
namespace Dawn {
class DawnGame;
struct Locale {
std::string language;
};
class LocaleManager : public StateOwner {
private:
DawnGame *game;
LanguageAsset *asset;
struct Locale locale;
std::shared_ptr<LanguageAsset> currentlyLoadedAsset;
std::shared_ptr<LanguageAsset> loadingAsset;
std::function<void()> eventTeardownLanguageLoaded;
/** Listens for when the pending language loads. */
void onLanguageLoaded();
public:
StateEvent<> eventLocaleChanged;
StateEvent<> eventLanguageUpdated;
/**
* Initializes the Locale Manager Instance. Locale Manager is responsible
* for handling anything that will change depending on which region the
* player is in.
*
* @param game Game instance this manager belongs to.
*/
LocaleManager(DawnGame *game);
/**
* Initializes the LocaleManager and loads the default language.
*/
void init();
/**
* Change the locale and begin loading the new language.
*
* @param locale Locale to switch to.
*/
void setLocale(struct Locale locale);
/**
* Gets the current locale.
*
* @return Current locale.
*/
struct Locale getLocale();
/**
* Returns a language string from the language CSV file.
*
* @param key Key of the string to get.
* @return The translated string.
*/
std::string getString(std::string key);
};
}