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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
//=========== Copyright Valve Corporation, All rights reserved. ===============//
//
// Purpose:
//=============================================================================//
#ifndef PANORAMA_ILOCALIZE_H
#define PANORAMA_ILOCALIZE_H
#include "language.h"
#include "tier0/platform.h"
#include "tier1/interface.h"
#include "tier1/utlflags.h"
#include "tier1/utlsymbol.h"
#include "tier1/utlstring.h"
#include "tier1/utlmap.h"
#include "tier1/fileio.h"
#include "tier1/utlpriorityqueue.h"
#include "steam/steamtypes.h"
#if defined( SOURCE2_PANORAMA )
#include "currencyamount.h"
#else
#include "stime.h"
#include "constants.h"
#include "globals.h"
#include "amount.h"
#include "rtime.h"
#endif
#include "reliabletimer.h"
#include "language.h"
namespace panorama
{
//-----------------------------------------------------------------------------
// Purpose: type of variable data we are supporting
//-----------------------------------------------------------------------------
enum EPanelKeyType
{
k_ePanelVartype_None,
k_ePanelVartype_String,
k_ePanelVartype_Time,
k_ePanelVartype_Money,
k_ePanelVartype_Number,
k_ePanelVartype_Generic
};
// help function syntax for the generic key handler
typedef const char *( *PFNLocalizeDialogVariableHandler )( const CUtlString &sStringValue, int nIntValue, const IUIPanel *pPanel, const char *pszKey, void *pUserData );
enum EPanelKeyTimeModifiers
{
k_ePanelKeyTimeModifiers_ShortDate = 1 << 0,
k_ePanelKeyTimeModifiers_LongDate = 1 << 1,
k_ePanelKeyTimeModifiers_ShortTime = 1 << 2,
k_ePanelKeyTimeModifiers_LongTime = 1 << 3,
k_ePanelKeyTimeModifiers_DateTime = 1 << 4,
k_ePanelKeyTimeModifiers_Relative = 1 << 5,
k_ePanelKeyTimeModifiers_Duration = 1 << 6,
};
enum EStringTruncationStyle
{
k_eStringTruncationStyle_None,
k_eStringTruncationStyle_Rear, // prevent any chars being added above max length
k_eStringTruncationStyle_Front, // remove
};
enum EStringTransformStyle
{
k_eStringTransformStyle_None,
k_eStringTransformStyle_Uppercase,
k_eStringTransformStyle_Lowercase,
};
//-----------------------------------------------------------------------------
// Purpose: callback interface to help measuring strings
//-----------------------------------------------------------------------------
class ILocalizationStringSizeResolver
{
public:
virtual ~ILocalizationStringSizeResolver() {}
virtual int ResolveStringLengthInPixels( const char *pchString ) = 0;
};
const uint32 k_nLocalizeMaxChars = (uint32)~0;
class CPanelKeyValue;
class CLocalization;
class CPanel2D;
//-----------------------------------------------------------------------------
// Purpose: interface to final string data to display to users
//-----------------------------------------------------------------------------
class ILocalizationString
{
public:
virtual ~ILocalizationString() {}
// Get the length of the string in characters
virtual int Length() const = 0;
virtual bool IsEmpty() const = 0;
virtual const char *String() const = 0;
virtual const char *StringNoTransform() const = 0;
virtual operator const char *() const = 0;
// add this string on the end
virtual bool AppendText( const char *pchText ) = 0;
// done with this string, delete it
virtual void Release() const = 0;
virtual EStringTransformStyle GetTransformStyle() const = 0;
virtual const IUIPanel *GetOwningPanel() const = 0;
virtual uint32 GetMaxChars() const = 0;
virtual EStringTruncationStyle GetTruncationStyle() const = 0;
#ifdef DBGFLAG_VALIDATE
virtual void Validate( CValidator &validator, const tchar *pchName ) = 0;
#endif
protected:
// internal loc engine helpers, you won't call these
friend class CLocalization;
virtual void Recalculate( const CUtlString *pString ) = 0;
virtual bool BContainsDialogVariable( const CPanelKeyValue &key ) = 0;
};
class CLocStringSafePointer
{
public:
CLocStringSafePointer()
{
m_pString = NULL;
}
CLocStringSafePointer( const ILocalizationString *pLocString )
{
m_pString = pLocString;
}
CLocStringSafePointer &operator =( const ILocalizationString *pString )
{
if ( m_pString == pString )
return *this;
Clear();
m_pString = const_cast<ILocalizationString *>( pString );
return *this;
}
const ILocalizationString *operator ->( ) const
{
return Get();
}
const ILocalizationString *operator *( ) const
{
return Get();
}
operator const ILocalizationString*( ) const
{
return Get();
}
~CLocStringSafePointer()
{
Clear();
}
void Clear()
{
if ( m_pString )
{
m_pString->Release();
m_pString = NULL;
}
}
bool IsValid() { return m_pString != nullptr; }
const ILocalizationString *Get() const
{
return m_pString;
}
private:
const ILocalizationString *m_pString;
};
//-----------------------------------------------------------------------------
// Purpose: interface to localize strings
//-----------------------------------------------------------------------------
class IUILocalization
{
public:
// change the language used by the loc system
virtual bool SetLanguage( const char *pchLanguage ) = 0;
#if defined( SOURCE2_PANORAMA )
// add a loc file to the system, in the form of <prefix>_<language>.txt , i.e dota_french.txt, the files will be loaded from the panorama/localization folder of your mod
virtual bool BLoadLocalizationFile( const char *pchFilePrefix ) = 0;
#else
virtual ELanguage CurrentLanguage() = 0;
#endif
virtual void InstallCustomDialogVariableHandler( const char *pchCustomHandlerName, PFNLocalizeDialogVariableHandler pfnLocalizeFunc, void *pUserData = NULL ) = 0;
virtual void RemoveCustomDialogVariableHandler( const char *pchCustomHandlerName ) = 0;
// find the string corresponding to this localization token, or if we don't find it then just return back the string wrapped in a loc object
virtual const ILocalizationString *PchFindToken( const IUIPanel *pPanel, const char *pchToken, const uint32 ccMax , EStringTruncationStyle eTrunkStyle, EStringTransformStyle eTransformStyle, bool bAllowDialogVariable = false ) = 0;
// give me a localize string wrapper around this string, don't try and apply token localizing on it though, but do optionally allow it to have dialog variables that we parse in it
// be careful allowing dialog variable parsing, you want to sanitize any user input before allowing it
virtual const ILocalizationString *PchSetString( const IUIPanel *pPanel, const char *pchText, const uint32 ccMax, EStringTruncationStyle eTrunkStyle, EStringTransformStyle eTransformStyle, bool bAllowDialogVariable, bool bStringAlreadyFullyParsed ) = 0;
virtual const ILocalizationString *ChangeTransformStyleAndRelease( const ILocalizationString *pLocalizationString, EStringTransformStyle eTranformStyle ) = 0;
// copy an existing loc string without altering the ref count on the current
virtual ILocalizationString *CloneString( const IUIPanel *pPanel, const ILocalizationString *pLocToken ) = 0;
// return the raw, un-parsed, value for this loc token, returns NULL if we didn't have this token in a loc file from disk
virtual const char *PchFindRawString( const char *pchToken ) = 0;
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, const char *pchValue ) = 0;
#if defined( SOURCE2_PANORAMA )
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, time_t timeVal ) = 0;
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CCurrencyAmount amount ) = 0;
#else
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CRTime timeVal ) = 0;
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, CAmount amount ) = 0;
#endif
virtual bool SetDialogVariable( const IUIPanel *pPanel, const char *pchKey, int nVal ) = 0;
// copy all the dialog vars to a new panel
virtual void CloneDialogVariables( const IUIPanel *pPanelFrom, IUIPanel *pPanelTo ) = 0;
// force a re-evaluation of a specific dialog variable
virtual void DirtyDialogVariable( const IUIPanel *pPanel, const char *pchKey ) = 0;
// given this loc string find the longest string in any language that we could display here and update to use it
virtual void SetLongestStringForToken( const ILocalizationString *pLocalizationString, ILocalizationStringSizeResolver *pResolver ) = 0;
};
//-----------------------------------------------------------------------------
// Purpose: Wrapper around ILocalizationString that will take care of making
// a copy if it ends up needing to be mutable.
//-----------------------------------------------------------------------------
class CMutableLocalizationString
{
public:
CMutableLocalizationString()
: m_bMutable( false )
, m_pString( NULL )
{
}
CMutableLocalizationString( ILocalizationString *pString )
: m_bMutable( true )
, m_pString( pString )
{
}
CMutableLocalizationString( const ILocalizationString *pString )
: m_bMutable( false )
, m_pString( const_cast< ILocalizationString * >( pString ) )
{
}
CMutableLocalizationString( const CMutableLocalizationString &other )
{
m_bMutable = true;
if ( other.m_pString )
{
m_pString = UILocalize()->CloneString( other.m_pString->GetOwningPanel(), other.m_pString );
}
else
{
m_pString = NULL;
}
}
~CMutableLocalizationString()
{
Clear();
}
const ILocalizationString *Get() const
{
return m_pString;
}
ILocalizationString *GetMutable()
{
if ( m_bMutable || !m_pString )
return m_pString;
ILocalizationString *pOldString = m_pString;
m_pString = UILocalize()->CloneString( pOldString->GetOwningPanel(), pOldString );
m_bMutable = true;
pOldString->Release();
return m_pString;
}
void Clear()
{
if ( m_pString )
{
m_pString->Release();
m_pString = NULL;
}
}
const ILocalizationString *Extract()
{
const ILocalizationString *pString = m_pString;
m_pString = NULL;
return pString;
}
ILocalizationString *ExtractMutable()
{
ILocalizationString *pString = GetMutable();
m_pString = NULL;
return pString;
}
// The -> operator is only overloaded to return a const string. If you need a mutable version, you must call GetMutable directly.
const ILocalizationString *operator ->() const
{
return Get();
}
explicit operator bool() const
{
return Get() != NULL;
}
bool operator ==( const CMutableLocalizationString &other ) const
{
return m_pString == other.m_pString;
}
bool operator !=( const CMutableLocalizationString &other ) const
{
return !( *this == other );
}
bool operator ==( const ILocalizationString *pString ) const
{
return m_pString == pString;
}
bool operator !=( const ILocalizationString *pString ) const
{
return !( *this == pString );
}
CMutableLocalizationString &operator =( const CMutableLocalizationString &other )
{
if ( other.m_pString == m_pString )
return *this;
Clear();
m_bMutable = true;
if ( other.m_pString )
{
m_pString = UILocalize()->CloneString( other.m_pString->GetOwningPanel(), other.m_pString );
}
else
{
m_pString = NULL;
}
return *this;
}
CMutableLocalizationString &operator =( const ILocalizationString *pString )
{
if ( m_pString == pString )
return *this;
Clear();
m_bMutable = false;
m_pString = const_cast< ILocalizationString * >( pString );
return *this;
}
private:
bool m_bMutable;
ILocalizationString *m_pString;;
};
} // namespace panorama
#endif // PANORAMA_ILOCALIZE_H
|