| 1 |
{ |
| 2 |
Copyright 2019-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Main user interface class. |
| 17 |
This implements the majority of this application functionality. } |
| 18 |
unit GameViewMain; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses Classes, Generics.Collections, |
| 23 |
CastleUIControls, CastleComponentSerialize, CastleControls, CastleTimeUtils, |
| 24 |
X3DNodes, CastleTextureFontData, CastleScene, CastleWindow, CastleFonts, CastleViewport; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main user interface class. |
| 28 |
This implements the majority of this application functionality. } |
| 29 |
TViewMain = class(TCastleView) |
| 30 |
published |
| 31 |
published |
| 32 |
{ Components designed using CGE editor. |
| 33 |
These fields will be automatically initialized at Start. } |
| 34 |
Viewport: TCastleViewport; |
| 35 |
ButtonSwitchEnglish, ButtonSwitchGerman, ButtonSwitchPolish, |
| 36 |
ButtonSwitchRussian, ButtonSwitchUkrainian: TCastleButton; |
| 37 |
ButtonMessage: TCastleButton; |
| 38 |
Text3d: TCastleText; |
| 39 |
strict private |
| 40 |
Language: string; |
| 41 |
|
| 42 |
{ For animating rotating text. } |
| 43 |
LifeTime: TFloatTime; |
| 44 |
|
| 45 |
MyFont: TCastleFont; |
| 46 |
|
| 47 |
{ Load GetText MO files for new language, |
| 48 |
recreate user interface to show new language (calling InitializeUserInterface). } |
| 49 |
procedure InitializeLanguage(const NewLanguage: string); |
| 50 |
|
| 51 |
procedure ClickButtonMessage(Sender: TObject); |
| 52 |
procedure ClickButtonEnglish(Sender: TObject); |
| 53 |
procedure ClickButtonGerman(Sender: TObject); |
| 54 |
procedure ClickButtonPolish(Sender: TObject); |
| 55 |
procedure ClickButtonRussian(Sender: TObject); |
| 56 |
procedure ClickButtonUkrainian(Sender: TObject); |
| 57 |
public |
| 58 |
constructor Create(AOwner: TComponent); override; |
| 59 |
procedure Start; override; |
| 60 |
{ Animate text rotation. } |
| 61 |
procedure Update(const SecondsPassed: Single; |
| 62 |
var HandleInput: boolean); override; |
| 63 |
end; |
| 64 |
|
| 65 |
var |
| 66 |
ViewMain: TViewMain; |
| 67 |
|
| 68 |
implementation |
| 69 |
|
| 70 |
uses SysUtils, |
| 71 |
CastleLog, CastleUriUtils, |
| 72 |
CastleLocalizationGetText, CastleColors, CastleSystemLanguage, |
| 73 |
CastleVectors, CastleMessages, |
| 74 |
Font_DejaVuSans; |
| 75 |
|
| 76 |
{ Translatable strings in Pascal code. |
| 77 |
Note that we use English as "translation id". |
| 78 |
But this is optional, you could as well use some internal-only ids. } |
| 79 |
resourcestring |
| 80 |
MessageText = 'I am a message!'; |
| 81 |
|
| 82 |
{ TViewMain ----------------------------------------------------------------- } |
| 83 |
|
| 84 |
constructor TViewMain.Create(AOwner: TComponent); |
| 85 |
begin |
| 86 |
inherited; |
| 87 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TViewMain.InitializeLanguage(const NewLanguage: string); |
| 91 |
|
| 92 |
procedure InitializeUserInterface; |
| 93 |
begin |
| 94 |
{ reassign things after DesignUrl changed, changing all component references } |
| 95 |
ButtonSwitchEnglish.OnClick := {$ifdef FPC}@{$endif} ClickButtonEnglish; |
| 96 |
ButtonSwitchGerman.OnClick := {$ifdef FPC}@{$endif} ClickButtonGerman; |
| 97 |
ButtonSwitchPolish.OnClick := {$ifdef FPC}@{$endif} ClickButtonPolish; |
| 98 |
ButtonSwitchRussian.OnClick := {$ifdef FPC}@{$endif} ClickButtonRussian; |
| 99 |
ButtonSwitchUkrainian.OnClick := {$ifdef FPC}@{$endif} ClickButtonUkrainian; |
| 100 |
ButtonMessage.OnClick := {$ifdef FPC}@{$endif} ClickButtonMessage; |
| 101 |
|
| 102 |
Text3d.CustomFont := MyFont; |
| 103 |
end; |
| 104 |
|
| 105 |
begin |
| 106 |
Language := NewLanguage; |
| 107 |
|
| 108 |
// Translate resourcestrings in code |
| 109 |
CastleTranslateResourceStrings('castle-data:/locale/game.' + Language + '.mo'); |
| 110 |
|
| 111 |
// Translate all future loaded user interface |
| 112 |
TranslateAllDesigns('castle-data:/locale/user_interface.' + Language + '.mo'); |
| 113 |
|
| 114 |
{ Reload user interface to show new language, freeing previous UI first. |
| 115 |
This is the simplest way to show translated UI. |
| 116 |
|
| 117 |
More complicated approaches are possible if neded, e.g. you could |
| 118 |
avoid reloading the existing UI, and translate it using "TranslateDesign(UiRoot)". |
| 119 |
TextNode can also be updated without reloading, by "TextNode.SetText([Text3D])". |
| 120 |
You could also preserve the TCastleEdit contents around it. } |
| 121 |
DesignUrl := ''; |
| 122 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 123 |
|
| 124 |
InitializeUserInterface; |
| 125 |
end; |
| 126 |
|
| 127 |
procedure TViewMain.Start; |
| 128 |
begin |
| 129 |
inherited; |
| 130 |
|
| 131 |
{ Load custom font, to make sure font contains all international characters. |
| 132 |
TFontStyleNode.CustomFont is used by TTextNode. |
| 133 |
Everything else uses UIFont (by default). } |
| 134 |
|
| 135 |
MyFont := TCastleFont.Create(FreeAtStop); |
| 136 |
MyFont.Load(TextureFont_DejaVuSans_50, false); |
| 137 |
MyFont.Size := 20; // size for UI |
| 138 |
WritelnLog('Font loaded, size %f, row height %f (should be similar)', [ |
| 139 |
MyFont.Size, |
| 140 |
MyFont.Height |
| 141 |
]); |
| 142 |
|
| 143 |
Text3d.CustomFont := MyFont; |
| 144 |
UIFont := MyFont; |
| 145 |
|
| 146 |
{ initialize language, InitializeLanguage will also intialize UI } |
| 147 |
Language := SystemLanguage; |
| 148 |
{ if we don't have a translation file for this language, fallback to English } |
| 149 |
if not URIFileExists('castle-data:/locale/game.' + Language + '.mo') then |
| 150 |
Language := SystemDefaultLanguage; |
| 151 |
InitializeLanguage(Language); |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TViewMain.Update(const SecondsPassed: Single; |
| 155 |
var HandleInput: boolean); |
| 156 |
begin |
| 157 |
inherited; |
| 158 |
LifeTime += SecondsPassed; |
| 159 |
Text3d.Rotation := Vector4(0, 1, 0, LifeTime); |
| 160 |
end; |
| 161 |
|
| 162 |
procedure TViewMain.ClickButtonMessage(Sender: TObject); |
| 163 |
begin |
| 164 |
MessageOK(Application.MainWindow, MessageText); |
| 165 |
end; |
| 166 |
|
| 167 |
procedure TViewMain.ClickButtonEnglish(Sender: TObject); |
| 168 |
begin |
| 169 |
InitializeLanguage('en'); |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TViewMain.ClickButtonGerman(Sender: TObject); |
| 173 |
begin |
| 174 |
InitializeLanguage('de'); |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TViewMain.ClickButtonPolish(Sender: TObject); |
| 178 |
begin |
| 179 |
InitializeLanguage('pl'); |
| 180 |
end; |
| 181 |
|
| 182 |
procedure TViewMain.ClickButtonRussian(Sender: TObject); |
| 183 |
begin |
| 184 |
InitializeLanguage('ru'); |
| 185 |
end; |
| 186 |
|
| 187 |
procedure TViewMain.ClickButtonUkrainian(Sender: TObject); |
| 188 |
begin |
| 189 |
InitializeLanguage('ua'); |
| 190 |
end; |
| 191 |
|
| 192 |
end. |