L10n.cpp 8.5 KB

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