1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111

#include "fsCLocaleGui.h"

#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/fsCAppSettings.h>
#include <fsCore/src/fsCVariableTable.h>

#include <fsRenderer/src/components/fsISpriteComponent.h>
#include <fsRenderer/src/components/fsCTextSpriteComponent.h>

#include "elements/fsCButtonComponent.h"
#include "src/fsCProfileManager.h"

const fsCUniqueId fsCLocaleGui::mTypeId("fsCLocaleGui");


void fsCLocaleGui::createLanguageOption(fsStr const& pLanguageCode)
{
	const fsStr localeGuiFolder = {"notpacked/locale/"};
	auto ent = fsIGuiComponent::guiCreateFromFile(fsCResourceName(
											{localeGuiFolder + "localeentity.txt"},
											fsCResourceName::eRES_LOCATION_ASSETS), parentGet(), false);

	ent->nameSet({"locale_" + pLanguageCode});
	auto localeOptionButton = ent->firstChildEntityWithComponentsOfFamilyType<fsCButtonComponent>()->componentGet<fsCButtonComponent>();
	localeOptionButton->brushesReplace(
		fsCResourceName({localeGuiFolder + pLanguageCode + ".png"}, fsCResourceName::eRES_LOCATION_ASSETS),
		fsCResourceName({localeGuiFolder + pLanguageCode + ".png"}, fsCResourceName::eRES_LOCATION_ASSETS),
					fsPoint(328,206));
	mLocaleOptions.push_back(localeOptionButton);
}

void fsCLocaleGui::localeOptionsCreate()
{
	const auto locale = fsCAppCoreMain::instanceGet()->localeGet();

	if (1 == locale->supportedLanguagesGet().size()
		|| "" != fsCProfileManager::instanceGet()->savedDataGet()->currentLocaleGet()
		)
	{
		nextGuiLoad();
		return;
	}

	std::for_each(std::begin(locale->supportedLanguagesGet()), std::end(locale->supportedLanguagesGet()),
		[this](auto lang)
		{
			createLanguageOption(lang.first);
		});

	auto stride = mLocaleOptions.front()->parentGet()->spriteComponentGet()->widthGet() * 1.2f;
	
	fsPoint start = {static_cast<fsS32>(-stride * mLocaleOptions.size() / 2.0f + stride / 2.0), 0};
	std::for_each(std::begin(mLocaleOptions), std::end(mLocaleOptions),
		[&](fsCButtonComponent* but)
		{
			but->parentGet()->posSet(start);
			start.x += stride;
		});
}

void fsCLocaleGui::initialiseFromVariableTableDo(const fsCVariableTable& pVars)
{
	fsIGuiComponent::initialiseFromVariableTableDo(pVars);

	mNextGui = pVars.strGet("nextGui");

	localeOptionsCreate();	
}

void fsCLocaleGui::nextGuiLoad() const
{
	fsCAppCoreMain::instanceGet()->childAddToAppScene(
	guiCreateFromFile(fsCResourceName(mNextGui, fsCResourceName::eRES_LOCATION_ASSETS)));
	parentGet()->destroy();
}

void fsCLocaleGui::localeSet(fsStr const& pLanguageCode)<--- The member function 'fsCLocaleGui::localeSet' can be static.
{
	fsCProfileManager::instanceGet()->savedDataGet()->currentLocaleSet(pLanguageCode);
	fsCAppCoreMain::instanceGet()->localeGet()->localeSet(pLanguageCode);
}

fsBool fsCLocaleGui::onButtonClicked(fsCButtonComponent* pButtonComp)
{
	const fsStr& btnName = pButtonComp->parentGet()->nameGet();
	if (fsStr::npos != btnName.find("locale_"))
	{
		fsStr locale = btnName;
		locale.erase(0, locale.find("_") + 1);
		localeSet(locale);
		nextGuiLoad();
		return true;
	}

	return false;
}

fsIComponent* fsCLocaleGui::create(fsCEntity* pParent)
{
	return new fsCLocaleGui(pParent);
}

fsCLocaleGui::fsCLocaleGui(fsCEntity* const pParent) :
	fsIGuiComponent(pParent)
{
}

fsCLocaleGui::~fsCLocaleGui()
{
}