aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIString.h
blob: 29e5a0684d8e7f90d166034f70baf9c27f61c57d (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
#pragma once

#include <set>
#include <functional>


#ifndef __PS3__

typedef function<wstring(void)> StringBuilder;

#else

class StringBuilderCore
{
public:
	virtual wstring getString() = 0;
};

struct StringBuilder
{
	shared_ptr<StringBuilderCore> m_coreBuilder;
	virtual wstring operator()() { return m_coreBuilder->getString(); }
	StringBuilder() {}
	StringBuilder(StringBuilderCore *core) { m_coreBuilder = shared_ptr<StringBuilderCore>(core); }
};

class IdsStringBuilder : public StringBuilderCore
{
	const int m_ids;
public:
	IdsStringBuilder(int ids) : m_ids(ids) {}
	virtual wstring getString(void) { return app.GetString(m_ids); }
};
#endif

using namespace std;

class UIString
{
protected:
	static int s_currentLanguage;
	static int s_currentLocale;

public:
	static bool setCurrentLanguage();
	static int getCurrentLanguage();

protected:
	class UIStringCore : public enable_shared_from_this<UIStringCore>
	{
	private:
		int				m_lastSetLanguage;
		int				m_lastSetLocale;

		int				m_lastUpdatedLanguage;
		int				m_lastUpdatedLocale;

		wstring			m_wstrCache;

		bool			m_bIsConstant;

		StringBuilder	m_fStringBuilder;
		
	public:
		UIStringCore(StringBuilder wstrBuilder);
		UIStringCore(const wstring &str);

		wstring &getString();

		bool hasNewString();
		bool update(bool force);

		bool needsUpdating();
		void setUpdated();
	};	

	shared_ptr<UIStringCore> m_core;

public:
	UIString();
	
	UIString(int ids); // Create a dynamic UI string from a string id value.

	UIString(StringBuilder wstrBuilder); // Create a dynamic UI string with a custom update function.
	
	// Create a UIString with a constant value.
	UIString(const  string &constant);
	UIString(const wstring &constant); 
	UIString(const wchar_t *constant);

	~UIString();

	bool	empty();
	bool	compare(const UIString &uiString);

	bool	needsUpdating();	// Language has been change since the last time setUpdated was called.
	void	setUpdated();		// The new text has been used.

	wstring	&getString();

	const wchar_t *c_str();
	unsigned int length();
};