L10n.cpp 8.7 KB

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