L10n.cpp 8.6 KB

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