L10n.cpp 8.5 KB

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