Finished localization
This commit is contained in:
@ -62,7 +62,6 @@ namespace Dawn {
|
||||
* @return 0 if successful, otherwise unsuccessful.
|
||||
*/
|
||||
int32_t rewind();
|
||||
|
||||
|
||||
/**
|
||||
* Retreive the current byte position within the asset that the head is
|
||||
|
@ -19,10 +19,18 @@ void AssetManager::queueLoad(std::vector<Asset*> assets) {
|
||||
vectorAppend(&this->assetsToLoad, &assets);
|
||||
}
|
||||
|
||||
void AssetManager::queueLoad(Asset *asset) {
|
||||
this->assetsToLoad.push_back(asset);
|
||||
}
|
||||
|
||||
void AssetManager::queueUnload(std::vector<Asset*> assets) {
|
||||
vectorAppend(&this->assetsToUnload, &assets);
|
||||
}
|
||||
|
||||
void AssetManager::queueUnload(Asset *asset) {
|
||||
this->assetsToUnload.push_back(asset);
|
||||
}
|
||||
|
||||
void AssetManager::queueSwap(
|
||||
std::vector<Asset*> newAssets,
|
||||
std::vector<Asset*> oldAssets
|
||||
@ -57,6 +65,7 @@ void AssetManager::syncTick() {
|
||||
auto asset = *it;
|
||||
if(asset->loaded) {
|
||||
it = this->assetsToLoad.erase(it);
|
||||
asset->eventLoaded.invoke();
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -65,6 +74,7 @@ void AssetManager::syncTick() {
|
||||
|
||||
if(asset->loaded) {
|
||||
it = this->assetsToLoad.erase(it);
|
||||
asset->eventLoaded.invoke();
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,7 @@ namespace Dawn {
|
||||
* @param assets Assets to load.
|
||||
*/
|
||||
void queueLoad(std::vector<Asset*> assets);
|
||||
void queueLoad(Asset *assets);
|
||||
|
||||
/**
|
||||
* Takes a list of lists to queue to unload. Does not immediately unload.
|
||||
@ -39,6 +40,7 @@ namespace Dawn {
|
||||
* @param assets Assets to unload.
|
||||
*/
|
||||
void queueUnload(std::vector<Asset*> assets);
|
||||
void queueUnload(Asset* assets);
|
||||
|
||||
/**
|
||||
* Queues load and unload based on the difference between two sets of
|
||||
@ -82,10 +84,26 @@ namespace Dawn {
|
||||
return asset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Both gets an asset, and puts it into the load queue.
|
||||
*
|
||||
* @param name Name of the asset to load.
|
||||
* @return The asset element to be loaded.
|
||||
*/
|
||||
template<class T>
|
||||
T * getAndLoad(std::string name) {
|
||||
auto asset = this->get<T>(name);
|
||||
this->queueLoad(asset);
|
||||
return asset;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void unload(T *asset) {
|
||||
assertUnreachable();
|
||||
}
|
||||
|
||||
void unload(std::string name) {
|
||||
assertUnreachable();
|
||||
//should delete the asset for you
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,6 @@ void TextureAsset::updateSync() {
|
||||
this->texture.buffer(this->colors);
|
||||
this->state = 0x05;
|
||||
this->loaded = true;
|
||||
this->eventLoaded.invoke();
|
||||
}
|
||||
|
||||
void TextureAsset::updateAsync() {
|
||||
|
@ -95,7 +95,6 @@ void TilesetAsset::updateAsync() {
|
||||
|
||||
this->state = 0x07;
|
||||
this->loaded = true;
|
||||
this->eventLoaded.invoke();
|
||||
|
||||
memoryFree(buffer);
|
||||
}
|
@ -29,7 +29,6 @@ void TrueTypeAsset::updateSync() {
|
||||
|
||||
this->state = 0x05;
|
||||
this->loaded = true;
|
||||
this->eventLoaded.invoke();
|
||||
}
|
||||
|
||||
void TrueTypeAsset::updateAsync() {
|
||||
|
@ -11,7 +11,7 @@
|
||||
#include "input/InputManager.hpp"
|
||||
#include "time/TimeManager.hpp"
|
||||
#include "input/InputBinds.hpp"
|
||||
#include "locale/LanguageManager.hpp"
|
||||
#include "locale/LocaleManager.hpp"
|
||||
|
||||
#define DAWN_GAME_INIT_RESULT_SUCCESS 0
|
||||
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0
|
||||
|
@ -7,5 +7,5 @@
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
LanguageManager.cpp
|
||||
LocaleManager.cpp
|
||||
)
|
@ -1,19 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "LanguageManager.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
LanguageManager::LanguageManager(DawnGame *game) {
|
||||
this->game = game;
|
||||
|
||||
auto lang = this->game->assetManager.get<LanguageAsset>("language_en");
|
||||
while(!lang->loaded) {
|
||||
lang->updateAsync();
|
||||
}
|
||||
std::cout << lang->getValue("test2");
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "event/Event.hpp"
|
||||
#include "asset/AssetManager.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
|
||||
struct LocalizedString {
|
||||
std::string data;
|
||||
};
|
||||
|
||||
class LanguageManager {
|
||||
private:
|
||||
DawnGame *game;
|
||||
LanguageAsset *asset;
|
||||
|
||||
public:
|
||||
Event<> eventLanguageChanged;
|
||||
|
||||
LanguageManager(DawnGame *game);
|
||||
|
||||
struct LocalizedString getString(std::string key);
|
||||
};
|
||||
}
|
70
src/dawn/locale/LocaleManager.cpp
Normal file
70
src/dawn/locale/LocaleManager.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
// 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);
|
||||
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
|
||||
}
|
||||
}
|
||||
|
||||
void LocaleManager::setLocale(struct Locale locale) {
|
||||
auto oldLocale = this->getLocale();
|
||||
|
||||
this->locale = locale;
|
||||
|
||||
// Did the language change?
|
||||
if(this->locale.language != oldLocale.language) {
|
||||
// Yes, switch languages.
|
||||
|
||||
// Are we already loading something? Stop it.
|
||||
if(this->loadingAsset != nullptr) {
|
||||
this->loadingAsset->eventLoaded.removeListener(this, &LocaleManager::onLanguageLoaded);
|
||||
this->game->assetManager.queueUnload(this->loadingAsset);
|
||||
this->loadingAsset = nullptr;
|
||||
}
|
||||
|
||||
this->loadingAsset = this->game->assetManager.getAndLoad<LanguageAsset>("language_" + this->locale.language);
|
||||
this->loadingAsset->eventLoaded.addListener(this, &LocaleManager::onLanguageLoaded);
|
||||
}
|
||||
|
||||
this->eventLocaleChanged.invoke();
|
||||
}
|
||||
|
||||
struct Locale LocaleManager::getLocale() {
|
||||
return this->locale;
|
||||
}
|
||||
|
||||
std::string LocaleManager::getString(std::string key) {
|
||||
assertNotNull(this->currentlyLoadedAsset);
|
||||
return this->currentlyLoadedAsset->getValue(key);
|
||||
}
|
||||
|
||||
|
||||
void LocaleManager::onLanguageLoaded() {
|
||||
// Unload the previously loaded language
|
||||
if(this->currentlyLoadedAsset != nullptr) {
|
||||
this->game->assetManager.queueUnload(this->currentlyLoadedAsset);
|
||||
}
|
||||
|
||||
// Swap loaded languages
|
||||
this->currentlyLoadedAsset = this->loadingAsset;
|
||||
this->loadingAsset = nullptr;
|
||||
|
||||
// Notify.
|
||||
this->eventLanguageUpdated.invoke();
|
||||
}
|
68
src/dawn/locale/LocaleManager.hpp
Normal file
68
src/dawn/locale/LocaleManager.hpp
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "event/Event.hpp"
|
||||
#include "asset/AssetManager.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
|
||||
struct Locale {
|
||||
std::string language;
|
||||
};
|
||||
|
||||
class LocaleManager {
|
||||
private:
|
||||
DawnGame *game;
|
||||
LanguageAsset *asset;
|
||||
struct Locale locale;
|
||||
LanguageAsset *currentlyLoadedAsset = nullptr;
|
||||
LanguageAsset *loadingAsset = nullptr;
|
||||
|
||||
/** Listens for when the pending language loads. */
|
||||
void onLanguageLoaded();
|
||||
|
||||
public:
|
||||
Event<> eventLocaleChanged;
|
||||
Event<> 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);
|
||||
};
|
||||
}
|
@ -23,7 +23,7 @@ add_subdirectory(visualnovel)
|
||||
add_subdirectory(scenes)
|
||||
|
||||
# Assets
|
||||
tool_language(language_en en.csv)
|
||||
tool_language(language_en locale/en.csv)
|
||||
tool_texture(texture_test texture_test.png)
|
||||
tool_texture(texture_city_day borrowed/city_day.png)
|
||||
tool_texture(texture_city_night borrowed/city_night.png)
|
||||
|
@ -15,12 +15,13 @@ DawnGame::DawnGame(DawnHost *host) :
|
||||
host(host),
|
||||
renderManager(this),
|
||||
inputManager(this),
|
||||
languageManager(this)
|
||||
localeManager(this)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t DawnGame::init() {
|
||||
this->assetManager.init();
|
||||
this->localeManager.init();
|
||||
this->renderManager.init();
|
||||
|
||||
this->scene = new TestScene(this);
|
||||
|
@ -15,7 +15,7 @@ namespace Dawn {
|
||||
AssetManager assetManager;
|
||||
InputManager inputManager;
|
||||
TimeManager timeManager;
|
||||
LanguageManager languageManager;
|
||||
LocaleManager localeManager;
|
||||
|
||||
DawnGame(DawnHost *host);
|
||||
int32_t init() override;
|
||||
|
Reference in New Issue
Block a user