aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIString.cpp
blob: 04d8b8e37bd7a2b816772a984d8a8f85cd498360 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "stdafx.h"

#include "..\..\..\Minecraft.World\StringHelpers.h"

#include "UIString.h"

bool UIString::setCurrentLanguage()
{
	int nextLanguage, nextLocale;
	nextLanguage = XGetLanguage();
	nextLocale = XGetLocale();

	if ( (nextLanguage != s_currentLanguage) || (nextLocale != s_currentLocale) )
	{
		s_currentLanguage = nextLanguage;
		s_currentLocale = nextLocale;
		return true;
	}

	return false;
}

int UIString::getCurrentLanguage()
{
	return s_currentLanguage;
}

UIString::UIStringCore::UIStringCore(StringBuilder wstrBuilder)
{
	m_bIsConstant = false;

	m_lastSetLanguage = m_lastSetLocale = -1;
	m_lastUpdatedLanguage = m_lastUpdatedLocale = -1;

	m_fStringBuilder = wstrBuilder;

	m_wstrCache = L"";
	update(true);
}

UIString::UIStringCore::UIStringCore(const wstring &str)
{
	m_bIsConstant = true;
	
	m_lastSetLanguage = m_lastSetLocale = -1;
	m_lastUpdatedLanguage = m_lastUpdatedLocale = -1;

	m_wstrCache = str;
}

wstring &UIString::UIStringCore::getString()
{
	if (hasNewString()) update(true);
	return m_wstrCache;
}

bool UIString::UIStringCore::hasNewString()
{
	if (m_bIsConstant) return false;
	return (m_lastSetLanguage != s_currentLanguage) || (m_lastSetLocale != s_currentLocale);
}

bool UIString::UIStringCore::update(bool force)
{
	if ( !m_bIsConstant && (force || hasNewString()) )
	{
		m_wstrCache = m_fStringBuilder();
		m_lastSetLanguage = s_currentLanguage;
		m_lastSetLocale = s_currentLocale;
		return true;
	}
	return false;
}

bool UIString::UIStringCore::needsUpdating()
{
	if (m_bIsConstant) return false;
	return (m_lastSetLanguage != s_currentLanguage) || (m_lastUpdatedLanguage != m_lastSetLanguage)
		|| (m_lastSetLocale != s_currentLocale) || (m_lastUpdatedLocale != m_lastSetLocale);
}

void UIString::UIStringCore::setUpdated()
{
	m_lastUpdatedLanguage = m_lastSetLanguage;
	m_lastUpdatedLocale = m_lastSetLocale;
}

int UIString::s_currentLanguage = -1;
int UIString::s_currentLocale = -1;

UIString::UIString()
{
	m_core = shared_ptr<UIStringCore>();
}

UIString::UIString(int ids)
{
#ifdef __PS3__
	StringBuilder builder = StringBuilder( new IdsStringBuilder(ids) );
#else
	StringBuilder builder = [ids](){ return app.GetString(ids); };
#endif
	UIStringCore *core = new UIStringCore( builder );
	m_core = shared_ptr<UIStringCore>(core);
}

UIString::UIString(StringBuilder wstrBuilder)
{
	UIStringCore *core = new UIStringCore(wstrBuilder);
	m_core = shared_ptr<UIStringCore>(core);
}

UIString::UIString(const string &constant)
{
	wstring wstr = convStringToWstring(constant);
	UIStringCore *core = new UIStringCore( wstr );
	m_core = shared_ptr<UIStringCore>(core);
}

UIString::UIString(const wstring &constant)
{
	UIStringCore *core = new UIStringCore(constant);
	m_core = shared_ptr<UIStringCore>(core);
}

UIString::UIString(const wchar_t *constant)
{
	wstring str = wstring(constant);
	UIStringCore *core = new UIStringCore(str);
	m_core = shared_ptr<UIStringCore>(core);
}

UIString::~UIString()
{
#ifndef __PS3__
	m_core = nullptr;
#endif
}

bool UIString::empty()
{
	return m_core.get() == nullptr;
}

bool UIString::compare(const UIString &uiString)
{
	return m_core.get() != uiString.m_core.get();
}

bool UIString::needsUpdating()
{
	if (m_core != nullptr)	return m_core->needsUpdating();
	else				return false;
}

void UIString::setUpdated()
{
	if (m_core != nullptr)	m_core->setUpdated();
}

wstring &UIString::getString()
{
	static wstring blank(L"");
	if (m_core != nullptr) return m_core->getString();
	else				return blank;
}

const wchar_t *UIString::c_str()
{
	return getString().c_str();
}

unsigned int UIString::length()
{
	return getString().length();
}