L10n.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Graphics/Material.h>
  5. #include <Urho3D/Graphics/Model.h>
  6. #include <Urho3D/Graphics/Octree.h>
  7. #include <Urho3D/Graphics/StaticModel.h>
  8. #include <Urho3D/Graphics/Zone.h>
  9. #include <Urho3D/Resource/Localization.h>
  10. #include <Urho3D/Resource/ResourceEvents.h>
  11. #include <Urho3D/UI/Button.h>
  12. #include <Urho3D/UI/Font.h>
  13. #include <Urho3D/UI/Text.h>
  14. #include <Urho3D/UI/Text3D.h>
  15. #include <Urho3D/UI/UIEvents.h>
  16. #include <Urho3D/UI/Window.h>
  17. #include "L10n.h"
  18. #include <Urho3D/DebugNew.h>
  19. URHO3D_DEFINE_APPLICATION_MAIN(L10n)
  20. L10n::L10n(Context* context) :
  21. Sample(context)
  22. {
  23. }
  24. void L10n::Start()
  25. {
  26. // Execute base class startup
  27. Sample::Start();
  28. // Enable and center OS cursor
  29. auto* input = GetSubsystem<Input>();
  30. input->SetMouseVisible(true);
  31. input->CenterMousePosition();
  32. // Load strings from JSON files and subscribe to the change language event
  33. InitLocalizationSystem();
  34. // Init the 3D space
  35. CreateScene();
  36. // Init the user interface
  37. CreateGUI();
  38. // Set the mouse mode to use in the sample
  39. Sample::InitMouseMode(MM_FREE);
  40. }
  41. void L10n::InitLocalizationSystem()
  42. {
  43. auto* l10n = GetSubsystem<Localization>();
  44. // JSON files must be in UTF8 encoding without BOM
  45. // The first found language will be set as current
  46. l10n->LoadJSONFile("StringsEnRu.json");
  47. // You can load multiple files
  48. l10n->LoadJSONFile("StringsDe.json");
  49. l10n->LoadJSONFile("StringsLv.json", "lv");
  50. // Hook up to the change language
  51. SubscribeToEvent(E_CHANGELANGUAGE, URHO3D_HANDLER(L10n, HandleChangeLanguage));
  52. }
  53. void L10n::CreateGUI()
  54. {
  55. // Get localization subsystem
  56. auto* l10n = GetSubsystem<Localization>();
  57. auto* cache = GetSubsystem<ResourceCache>();
  58. UIElement* root = GetSubsystem<UI>()->GetRoot();
  59. root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  60. auto* window = new Window(context_);
  61. root->AddChild(window);
  62. window->SetMinSize(384, 192);
  63. window->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  64. window->SetAlignment(HA_CENTER, VA_CENTER);
  65. window->SetStyleAuto();
  66. auto* windowTitle = new Text(context_);
  67. windowTitle->SetName("WindowTitle");
  68. windowTitle->SetStyleAuto();
  69. window->AddChild(windowTitle);
  70. // In this place the current language is "en" because it was found first when loading the JSON files
  71. String langName = l10n->GetLanguage();
  72. // Languages are numbered in the loading order
  73. int langIndex = l10n->GetLanguageIndex(); // == 0 at the beginning
  74. // Get string with identifier "title" in the current language
  75. String localizedString = l10n->Get("title");
  76. // Localization::Get returns String::EMPTY if the id is empty.
  77. // Localization::Get returns the id if translation is not found and will be added a warning into the log.
  78. windowTitle->SetText(localizedString + " (" + String(langIndex) + " " + langName + ")");
  79. auto* b = new Button(context_);
  80. window->AddChild(b);
  81. b->SetStyle("Button");
  82. b->SetMinHeight(24);
  83. auto* t = b->CreateChild<Text>("ButtonTextChangeLang");
  84. // The showing text value will automatically change when language is changed
  85. t->SetAutoLocalizable(true);
  86. // The text value used as a string identifier in this mode.
  87. // Remember that a letter case of the id and of the lang name is important.
  88. t->SetText("Press this button");
  89. t->SetAlignment(HA_CENTER, VA_CENTER);
  90. t->SetStyle("Text");
  91. SubscribeToEvent(b, E_RELEASED, URHO3D_HANDLER(L10n, HandleChangeLangButtonPressed));
  92. b = new Button(context_);
  93. window->AddChild(b);
  94. b->SetStyle("Button");
  95. b->SetMinHeight(24);
  96. t = b->CreateChild<Text>("ButtonTextQuit");
  97. t->SetAlignment(HA_CENTER, VA_CENTER);
  98. t->SetStyle("Text");
  99. // Manually set text in the current language
  100. t->SetText(l10n->Get("quit"));
  101. SubscribeToEvent(b, E_RELEASED, URHO3D_HANDLER(L10n, HandleQuitButtonPressed));
  102. }
  103. void L10n::CreateScene()
  104. {
  105. // Get localization subsystem
  106. auto* l10n = GetSubsystem<Localization>();
  107. auto* cache = GetSubsystem<ResourceCache>();
  108. scene_ = new Scene(context_);
  109. scene_->CreateComponent<Octree>();
  110. auto* zone = scene_->CreateComponent<Zone>();
  111. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  112. zone->SetAmbientColor(Color(0.5f, 0.5f, 0.5f));
  113. zone->SetFogColor(Color(0.4f, 0.5f, 0.8f));
  114. zone->SetFogStart(1.0f);
  115. zone->SetFogEnd(100.0f);
  116. Node* planeNode = scene_->CreateChild("Plane");
  117. planeNode->SetScale(Vector3(300.0f, 1.0f, 300.0f));
  118. auto* planeObject = planeNode->CreateComponent<StaticModel>();
  119. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  120. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  121. Node* lightNode = scene_->CreateChild("DirectionalLight");
  122. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
  123. auto* light = lightNode->CreateComponent<Light>();
  124. light->SetLightType(LIGHT_DIRECTIONAL);
  125. light->SetColor(Color(0.8f, 0.8f, 0.8f));
  126. cameraNode_ = scene_->CreateChild("Camera");
  127. cameraNode_->CreateComponent<Camera>();
  128. cameraNode_->SetPosition(Vector3(0.0f, 10.0f, -30.0f));
  129. Node* text3DNode = scene_->CreateChild("Text3D");
  130. text3DNode->SetPosition(Vector3(0.0f, 0.1f, 30.0f));
  131. auto* text3D = text3DNode->CreateComponent<Text3D>();
  132. // Manually set text in the current language.
  133. text3D->SetText(l10n->Get("lang"));
  134. text3D->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  135. text3D->SetColor(Color::BLACK);
  136. text3D->SetAlignment(HA_CENTER, VA_BOTTOM);
  137. text3DNode->SetScale(15);
  138. auto* renderer = GetSubsystem<Renderer>();
  139. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  140. renderer->SetViewport(0, viewport);
  141. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(L10n, HandleUpdate));
  142. }
  143. void L10n::HandleUpdate(StringHash eventType, VariantMap& eventData)
  144. {
  145. using namespace Update;
  146. float timeStep = eventData[P_TIMESTEP].GetFloat();
  147. auto* input = GetSubsystem<Input>();
  148. const float MOUSE_SENSITIVITY = 0.1f;
  149. IntVector2 mouseMove = input->GetMouseMove();
  150. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  151. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  152. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  153. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  154. }
  155. void L10n::HandleChangeLangButtonPressed(StringHash eventType, VariantMap& eventData)
  156. {
  157. auto* l10n = GetSubsystem<Localization>();
  158. // Languages are numbered in the loading order
  159. int lang = l10n->GetLanguageIndex();
  160. lang++;
  161. if (lang >= l10n->GetNumLanguages())
  162. lang = 0;
  163. l10n->SetLanguage(lang);
  164. }
  165. void L10n::HandleQuitButtonPressed(StringHash eventType, VariantMap& eventData)
  166. {
  167. if (GetPlatform() != "Web")
  168. engine_->Exit();
  169. }
  170. // You can manually change texts, sprites and other aspects of the game when language is changed
  171. void L10n::HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
  172. {
  173. auto* l10n = GetSubsystem<Localization>();
  174. UIElement* uiRoot = GetSubsystem<UI>()->GetRoot();
  175. auto* windowTitle = uiRoot->GetChildStaticCast<Text>("WindowTitle", true);
  176. windowTitle->SetText(l10n->Get("title") + " (" + String(l10n->GetLanguageIndex()) + " " + l10n->GetLanguage() + ")");
  177. auto* buttonText = uiRoot->GetChildStaticCast<Text>("ButtonTextQuit", true);
  178. buttonText->SetText(l10n->Get("quit"));
  179. auto* text3D = scene_->GetChild("Text3D")->GetComponent<Text3D>();
  180. text3D->SetText(l10n->Get("lang"));
  181. // A text on the button "Press this button" changes automatically
  182. }