PeriodicApp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  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 <Atomic/Engine/Application.h>
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Graphics/Camera.h>
  25. #include <Atomic/Engine/Engine.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Input/Input.h>
  29. #include <Atomic/Input/InputEvents.h>
  30. #include <Atomic/Graphics/Renderer.h>
  31. #include <Atomic/Resource/ResourceCache.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Scene/SceneEvents.h>
  34. #include <Atomic/Graphics/Texture2D.h>
  35. #include <Atomic/Graphics/Zone.h>
  36. #include <Atomic/Graphics/Material.h>
  37. #include <Atomic/Graphics/Model.h>
  38. #include <Atomic/Graphics/Octree.h>
  39. #include <Atomic/Graphics/Renderer.h>
  40. #include <Atomic/Graphics/StaticModel.h>
  41. #include <Atomic/Core/Timer.h>
  42. #include <Atomic/UI/UI.h>
  43. #include <Atomic/UI/UIEvents.h>
  44. #include <Atomic/UI/UIView.h>
  45. #include <Atomic/UI/UILayout.h>
  46. #include <Atomic/UI/UICheckBox.h>
  47. #include <Atomic/UI/UITextField.h>
  48. #include <Atomic/UI/UIButton.h>
  49. #include <Atomic/UI/UIEditField.h>
  50. #include <Atomic/UI/UITabContainer.h>
  51. #include <Atomic/UI/UIWindow.h>
  52. #include <Atomic/UI/UIFinderWindow.h>
  53. #include <Atomic/UI/UIPromptWindow.h>
  54. #include <Atomic/Resource/XMLFile.h>
  55. #include <Atomic/IO/Log.h>
  56. #include "PeriodicApp.h"
  57. #include "Spinner.h"
  58. ATOMIC_DEFINE_APPLICATION_MAIN(PeriodicApp)
  59. PeriodicApp::PeriodicApp(Context* context) :
  60. Application(context)
  61. {
  62. // Register an object factory for the Rotator component
  63. context->RegisterFactory<Rotator>();
  64. }
  65. void PeriodicApp::Setup()
  66. {
  67. engineParameters_["WindowWidth"] = 1280; // Modify engine startup parameters
  68. engineParameters_["WindowHeight"] = 720;
  69. engineParameters_["FullScreen"] = false;
  70. engineParameters_["Headless"] = false;
  71. engineParameters_["Sound"] = false;
  72. engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("atomic", "logs") + GetTypeName() + ".log";
  73. engineParameters_["ResourcePaths"] = "Data;PlayerData;CoreData";
  74. // Construct a search path to find the resource prefix with two entries:
  75. // The first entry is an empty path which will be substituted with program/bin directory -- this entry is for binary when it is still in build tree
  76. // The second and third entries are possible relative paths from the installed program/bin directory to the asset directory -- these entries are for binary when it is in the Atomic SDK installation location
  77. if (!engineParameters_.Contains(EP_RESOURCE_PREFIX_PATHS))
  78. {
  79. // TODO: This is dependent on a source build
  80. String resourcePrefix = ToString("%s/Resources;%s/Submodules/AtomicExamples/FeatureExamples/CPlusPlus", ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR);
  81. engineParameters_[EP_RESOURCE_PREFIX_PATHS] = resourcePrefix;
  82. }
  83. }
  84. void PeriodicApp::Start()
  85. {
  86. UI* ui = GetSubsystem<UI>();
  87. ui->Initialize("DefaultUI/language/lng_en.tb.txt");
  88. ui->LoadDefaultPlayerSkin();
  89. uiview_ = new UIView(context_); // create new UIView
  90. SetWindowTitleAndIcon(); // Set custom window Title & Icon
  91. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(PeriodicApp, HandleKeyDown)); // to handle the keys
  92. DoSomething(); // your game code goes here
  93. }
  94. void PeriodicApp::Stop()
  95. {
  96. // possibly clean up allocated memory
  97. }
  98. void PeriodicApp::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  99. {
  100. using namespace KeyDown;
  101. int key = eventData[P_KEY].GetInt();
  102. if(key == KEY_ESCAPE)
  103. {
  104. engine_->Exit();
  105. }
  106. else if (key == KEY_F1) // Toggle console with F1
  107. {
  108. GetSubsystem<UI>()->ToggleConsole();
  109. }
  110. else if (key == KEY_F2) // Toggle debug HUD with F2
  111. {
  112. GetSubsystem<UI>()->ToggleDebugHud();
  113. }
  114. }
  115. void PeriodicApp::SetWindowTitleAndIcon()
  116. {
  117. ResourceCache* cache = GetSubsystem<ResourceCache>();
  118. Graphics* graphics = GetSubsystem<Graphics>();
  119. graphics->SetWindowTitle("PeriodicApp");
  120. Image* icon = cache->GetResource<Image>("Textures/AtomicLogo32.png");
  121. graphics->SetWindowIcon(icon);
  122. }
  123. //
  124. // This is the entry point for your 3d game
  125. //
  126. void PeriodicApp::DoSomething()
  127. {
  128. ResourceCache* cache = GetSubsystem<ResourceCache>();
  129. scene_ = new Scene(context_);
  130. scene_->CreateComponent<Octree>();
  131. Node* lightNode = scene_->CreateChild("DirectionalLight");
  132. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
  133. Light* light = lightNode->CreateComponent<Light>();
  134. light->SetLightType(LIGHT_DIRECTIONAL);
  135. Node *cameraNode_ = scene_->CreateChild("Camera");
  136. cameraNode_->CreateComponent<Camera>();
  137. cameraNode_->SetPosition(Vector3(-4.7f, 3.6f, -16.2f));
  138. cameraNode_->SetRotation(Quaternion(11.4f, 15.8f, 0.0f));
  139. Renderer* renderer = GetSubsystem<Renderer>();
  140. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  141. renderer->SetViewport(0, viewport);
  142. UI* ui = GetSubsystem<UI>(); // get the ui pointer
  143. ui->AddFont("Textures/BrokenGlass.ttf", "BrokenGlass"); // add a gooder font
  144. // set up some stuff for mobile, so we can use the same app code & layouts, when we can deploy on mobile
  145. ui->LoadSkin("Textures/desktop.tb.txt"); // load in the app skin
  146. UILayout* lo0 = new UILayout(context_); // make the host widget for all visible ui
  147. lo0->SetRect ( uiview_->GetRect ()); //size it to fill the screen area
  148. lo0->SetLayoutConfig( "YAGAC" ); //all-in-one setting
  149. lo0->SetSkinBg ("background_solid"); // make it look gooder
  150. lo0->Load("Scenes/main_layout.ui.txt"); // load the main layout
  151. uiview_->AddChild(lo0); // And make it show up.
  152. UITabContainer *maintb = (UITabContainer *)lo0->GetWidget("maintabs");
  153. UITabContainer *acttb = (UITabContainer *)lo0->GetWidget("primarytabs");
  154. UITabContainer *semitb = (UITabContainer *)lo0->GetWidget("moretabs");
  155. UITabContainer *viewtb = (UITabContainer *)lo0->GetWidget("supporttabs");
  156. UITabContainer *supporttb = (UITabContainer *)lo0->GetWidget("atomictabs");
  157. supporttb->SetCurrentPage(0);
  158. viewtb->SetCurrentPage(0);
  159. semitb->SetCurrentPage(0);
  160. acttb->SetCurrentPage(0);
  161. maintb->SetCurrentPage(0); // do this or else the tab contents look like crap!
  162. uilog_ = (UITextField*)lo0->GetWidget("LogText");
  163. UIView *someview = lo0->GetView();
  164. UIWidget *ea = lo0->GetWidget("exitapp");
  165. SubscribeToEvent(ea, E_WIDGETEVENT, ATOMIC_HANDLER(PeriodicApp, HandleExitEvent));
  166. setup_table( lo0 );
  167. setup_uiwidget( lo0->GetWidget("pageuiwidget") );
  168. setup_uibargraph( lo0->GetWidget("pageuibargraph") );
  169. setup_uibutton( lo0->GetWidget("pageuibutton") );
  170. setup_uicheckbox( lo0->GetWidget("pageuicheckbox"));
  171. setup_uiclicklabel( lo0->GetWidget("pageuiclicklabel") );
  172. setup_uicolorwheel( lo0->GetWidget("pageuicolorwheel") );
  173. setup_uicolorwidget( lo0->GetWidget("pageuicolorwidget") );
  174. setup_uicontainer( lo0->GetWidget("pageuicontainer"));
  175. setup_uieditfield( lo0->GetWidget("pageuieditfield") );
  176. setup_uifinderwindow( lo0->GetWidget("pageuifinderwindow"), someview );
  177. setup_uifontdescription( lo0->GetWidget("pageuifontdescription") );
  178. setup_uiimagewidget( lo0->GetWidget("pageuiimagewidget") );
  179. setup_uiinlineselect( lo0->GetWidget("pageuiinlineselect") );
  180. setup_uilayoutparams( lo0->GetWidget("pageuilayoutparams") );
  181. setup_uilayout( lo0->GetWidget("pageuilayout"), someview );
  182. setup_uimenuitem( lo0->GetWidget("pageuimenuitem") );
  183. setup_uimenuwindow( lo0->GetWidget("pageuimenuwindow") );
  184. setup_uimessagewindow( lo0->GetWidget("pageuimessagewindow") );
  185. setup_uipromptwindow( lo0->GetWidget("pageuipromptwindow"), someview );
  186. setup_uipulldownmenu( lo0->GetWidget("pageuipulldownmenu") );
  187. setup_uiradiobutton( lo0->GetWidget("pageuiradiobutton") );
  188. setup_uisceneview( lo0->GetWidget("pageuisceneview") );
  189. setup_uiscrollbar( lo0->GetWidget("pageuiscrollbar") );
  190. setup_uiscrollcontainer( lo0->GetWidget("pageuiscrollcontainer") );
  191. setup_uisection( lo0->GetWidget("pageuisection") );
  192. setup_uiselectdropdown( lo0->GetWidget("pageuiselectdropdown") );
  193. setup_uiselectitem( lo0->GetWidget("pageuiselectitem") );
  194. setup_uiselectlist( lo0->GetWidget("pageuiselectlist") );
  195. setup_uiseparator( lo0->GetWidget("pageuiseparator") );
  196. setup_uiskinimage( lo0->GetWidget("pageuiskinimage") );
  197. setup_uislider( lo0->GetWidget("pageuislider") );
  198. setup_uitabcontainer( lo0->GetWidget("pageuitabcontainer") );
  199. setup_uitextfield( lo0->GetWidget("pageuitextfield") );
  200. setup_uitexturewidget( lo0->GetWidget("pageuitexturewidget") );
  201. setup_uiwindow( lo0->GetWidget("pageuiwindow"), someview );
  202. AppLog ( "Ready" );
  203. }
  204. void PeriodicApp::AppLog( const String &logtext )
  205. {
  206. if (uilog_ )
  207. uilog_->SetText ( logtext );
  208. }
  209. void PeriodicApp::ViewCode ( String filename, UIWidget *layoutParent )
  210. {
  211. ResourceCache* cache = GetSubsystem<ResourceCache>();
  212. UIView *someview = layoutParent->GetView();
  213. UIWindow *window = new UIWindow(context_);
  214. window->SetSettings ( UI_WINDOW_SETTINGS_DEFAULT );
  215. window->SetText( "Code Viewer");
  216. window->Load("Scenes/view_code.ui.txt");
  217. SharedPtr<File> filex = cache->GetFile(filename);
  218. String textx = filex->ReadText();
  219. filex->Close();
  220. UIWidget *coder = window->GetWidget("viewCodeText");
  221. coder->SetText(textx);
  222. window->ResizeToFitContent();
  223. someview->AddChild(window);
  224. window->Center();
  225. UIWidget *someok = window->GetWidget("viewCodeOK");
  226. SubscribeToEvent(someok, E_WIDGETEVENT, ATOMIC_HANDLER(PeriodicApp, HandleViewCodeEvent ));
  227. }
  228. void PeriodicApp::HandleViewCodeEvent(StringHash eventType, VariantMap& eventData)
  229. {
  230. using namespace WidgetEvent;
  231. if (eventData[P_TYPE] == UI_EVENT_TYPE_CLICK)
  232. {
  233. UIWidget* widget = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  234. if (widget)
  235. {
  236. UIWindow *mywindow = static_cast<UIWindow *>(FindTheWindowParent(widget));
  237. if (mywindow)
  238. mywindow->Close();
  239. }
  240. }
  241. }
  242. void PeriodicApp::HandleExitEvent(StringHash eventType, VariantMap& eventData)
  243. {
  244. using namespace WidgetEvent;
  245. if (eventData[P_TYPE] == UI_EVENT_TYPE_CLICK)
  246. {
  247. UIWidget* widget = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  248. if (widget)
  249. {
  250. engine_->Exit();
  251. }
  252. }
  253. }
  254. String PeriodicApp::EventReport( int eventNumber)
  255. {
  256. switch ( eventNumber ) {
  257. case 0: return "UI_EVENT_TYPE_CLICK";
  258. case 1: return "UI_EVENT_TYPE_LONG_CLICK";
  259. case 2: return "UI_EVENT_TYPE_POINTER_DOWN";
  260. case 3: return "UI_EVENT_TYPE_POINTER_UP";
  261. case 4: return "UI_EVENT_TYPE_POINTER_MOVE";
  262. case 5: return "UI_EVENT_TYPE_RIGHT_POINTER_DOWN";
  263. case 6: return "UI_EVENT_TYPE_RIGHT_POINTER_UP";
  264. case 7: return "UI_EVENT_TYPE_WHEEL";
  265. case 8: return "UI_EVENT_TYPE_CHANGED";
  266. case 9: return "UI_EVENT_TYPE_KEY_DOWN";
  267. case 10: return "UI_EVENT_TYPE_KEY_UP";
  268. case 11: return "UI_EVENT_TYPE_SHORTCUT";
  269. case 12: return "UI_EVENT_TYPE_CONTEXT_MENU";
  270. case 13: return "UI_EVENT_TYPE_FILE_DROP";
  271. case 14: return "UI_EVENT_TYPE_TAB_CHANGED";
  272. case 15: return "UI_EVENT_TYPE_CUSTOM";
  273. }
  274. return "EVENT_UKNOWN";
  275. }
  276. UIWidget *PeriodicApp::FindTheWindowParent( UIWidget* fromThisWidget )
  277. {
  278. tb::TBWidget *widgetx = fromThisWidget->GetInternalWidget();
  279. UI* ui = GetSubsystem<UI>();
  280. if (widgetx == NULL)
  281. return 0;
  282. if ( widgetx->GetClassName() == "TBWindow" || widgetx->GetClassName() == "TBDockWindow" )
  283. return ui->WrapWidget(widgetx);
  284. tb::TBWidget* tbw = widgetx->GetParent();
  285. while(tbw)
  286. {
  287. if ( tbw->GetClassName() == "TBWindow" || tbw->GetClassName() == "TBDockWindow" )
  288. return ui->WrapWidget(tbw);
  289. tbw = tbw->GetParent();
  290. }
  291. return 0;
  292. }