UIBuildSettingsAndroid.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <TurboBadger/tb_layout.h>
  6. #include <TurboBadger/tb_editfield.h>
  7. #include <TurboBadger/tb_select.h>
  8. #include <Atomic/Core/Context.h>
  9. #include <Atomic/IO/Log.h>
  10. #include <Atomic/IO/FileSystem.h>
  11. #include <Atomic/IO/MemoryBuffer.h>
  12. #include <Atomic/UI/UI.h>
  13. #include "AEEditor.h"
  14. #include "AEEvents.h"
  15. #include "AEPreferences.h"
  16. #include "Project/ProjectUtils.h"
  17. #include "Subprocess/AESubprocessSystem.h"
  18. #include "Build/BuildSystem.h"
  19. #include "UIBuildSettingsAndroid.h"
  20. namespace AtomicEditor
  21. {
  22. UIBuildSettingsAndroid::UIBuildSettingsAndroid(Context* context) :
  23. AEWidget(context)
  24. {
  25. UI* tbui = GetSubsystem<UI>();
  26. tbui->LoadResourceFile(delegate_, "AtomicEditor/editor/ui/buildsettings_android.tb.txt");
  27. appNameEdit_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("app_name"));
  28. assert(appNameEdit_);
  29. appPackageEdit_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("app_package"));
  30. assert(appPackageEdit_);
  31. productNameEdit_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("product_name"));
  32. assert(productNameEdit_);
  33. companyNameEdit_ = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("company_name"));
  34. assert(companyNameEdit_);
  35. sdkTargetSelect_ = delegate_->GetWidgetByIDAndType<TBSelectDropdown>(TBIDC("sdk_target_select"));
  36. assert(sdkTargetSelect_);
  37. TBTextField* jdk_root_text = delegate_->GetWidgetByIDAndType<TBTextField>(TBIDC("jdk_root_text"));
  38. assert(jdk_root_text);
  39. TBTextField* ant_path_text = delegate_->GetWidgetByIDAndType<TBTextField>(TBIDC("ant_path_text"));
  40. assert(ant_path_text);
  41. #ifdef ATOMIC_PLATFORM_WINDOWS
  42. jdk_root_text->SetText("JDK Root: (Ex. C:\\Program Files\\Java\\jdk1.8.0_31)");
  43. ant_path_text->SetText("Ant Path: (The folder that contains ant.bat)");
  44. #else
  45. TBButton* choose_jdk_root = delegate_->GetWidgetByIDAndType<TBButton>(TBIDC("choose_jdk_root"));
  46. assert(choose_jdk_root);
  47. TBButton* choose_ant_path = delegate_->GetWidgetByIDAndType<TBButton>(TBIDC("choose_ant_path"));
  48. assert(choose_ant_path);
  49. choose_jdk_root->SetVisibilility(WIDGET_VISIBILITY_GONE);
  50. choose_ant_path->SetVisibilility(WIDGET_VISIBILITY_GONE);
  51. TBEditField* ant_path = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("ant_path"));
  52. assert(ant_path);
  53. ant_path->SetVisibilility(WIDGET_VISIBILITY_GONE);
  54. ant_path_text->SetVisibilility(WIDGET_VISIBILITY_GONE);
  55. TBEditField* jdk_root = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("jdk_root"));
  56. assert(jdk_root);
  57. jdk_root->SetVisibilility(WIDGET_VISIBILITY_GONE);
  58. jdk_root_text->SetVisibilility(WIDGET_VISIBILITY_GONE);
  59. #endif
  60. }
  61. UIBuildSettingsAndroid::~UIBuildSettingsAndroid()
  62. {
  63. }
  64. void UIBuildSettingsAndroid::HandleEvent(StringHash eventType, VariantMap& eventData)
  65. {
  66. if (eventType == E_SUBPROCESSOUTPUT)
  67. {
  68. targetOutput_ += eventData[SubprocessOutput::P_TEXT].GetString();
  69. }
  70. else if (eventType == E_SUBPROCESSCOMPLETE)
  71. {
  72. TBGenericStringItemSource* source = sdkTargetSelect_->GetDefaultSource();
  73. MemoryBuffer reader(targetOutput_.CString(), targetOutput_.Length() + 1);
  74. String id;
  75. bool parsed = false;
  76. while (!reader.IsEof())
  77. {
  78. String line = reader.ReadLine();
  79. if (line.StartsWith("id:"))
  80. {
  81. //id: 33 or "Google Inc.:Google APIs (x86 System Image):19"
  82. parsed = false;
  83. Vector<String> elements = line.Split('\"');
  84. if (elements.Size() == 2)
  85. {
  86. String api = elements[1];
  87. TBGenericStringItem* item = new TBGenericStringItem(api.CString());
  88. item->tag = TBValue(id.CString());
  89. source->AddItem(item);
  90. }
  91. }
  92. }
  93. // force update
  94. sdkTargetSelect_->SetValue(-1);
  95. sdkTargetSelect_->SetValue(0);
  96. //LOGINFOF("%s", targetOutput_.CString());
  97. }
  98. }
  99. void UIBuildSettingsAndroid::RefreshAndroidTargets()
  100. {
  101. Editor* editor = context_->GetSubsystem<Editor>();
  102. SubprocessSystem* subs = context_->GetSubsystem<SubprocessSystem>();
  103. TBGenericStringItemSource* source = sdkTargetSelect_->GetDefaultSource();
  104. source->DeleteAllItems();
  105. String androidCommand = GetNativePath(editor->GetPreferences()->GetAndroidSDKPath());
  106. if (!androidCommand.Length())
  107. return;
  108. #ifdef ATOMIC_PLATFORM_OSX
  109. Vector<String> args = String("list targets").Split(' ');
  110. androidCommand += "tools/android";
  111. #else
  112. Vector<String> args;
  113. // android is a batch file on windows, so have to run with cmd /c
  114. args.Push("/c");
  115. args.Push("\"" + androidCommand + "\\tools\\android.bat\"");
  116. args.Push("list");
  117. args.Push("targets");
  118. androidCommand = "cmd";
  119. #endif
  120. targetOutput_.Clear();
  121. targetLookup_.Clear();
  122. Subprocess* subprocess = subs->Launch(androidCommand, args);
  123. if (!subprocess)
  124. {
  125. //ERROR
  126. assert(0);
  127. }
  128. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(UIBuildSettingsAndroid, HandleEvent));
  129. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(UIBuildSettingsAndroid, HandleEvent));
  130. }
  131. void UIBuildSettingsAndroid::StoreSettings()
  132. {
  133. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  134. AndroidBuildSettings settings;
  135. TBStr text;
  136. appNameEdit_->GetText(text);
  137. settings.appName = text.CStr();
  138. text.Clear();
  139. appPackageEdit_->GetText(text);
  140. settings.package = text.CStr();
  141. text.Clear();
  142. productNameEdit_->GetText(text);
  143. settings.productName = text.CStr();
  144. text.Clear();
  145. companyNameEdit_->GetText(text);
  146. settings.companyName = text.CStr();
  147. text.Clear();
  148. sdkTargetSelect_->GetText(text);
  149. settings.targetSDKVersion = text.CStr();
  150. text.Clear();
  151. buildSystem->GetBuildSettings()->SetAndroidSettings(settings);
  152. }
  153. void UIBuildSettingsAndroid::Refresh()
  154. {
  155. Editor* editor = context_->GetSubsystem<Editor>();
  156. AEPreferences* prefs = editor->GetPreferences();
  157. TBEditField* sdk_path = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("sdk_path"));
  158. sdk_path->SetText(prefs->GetAndroidSDKPath().CString());
  159. TBEditField* ant_path = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("ant_path"));
  160. ant_path->SetText(prefs->GetAntPath().CString());
  161. TBEditField* jdk_root = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("jdk_root"));
  162. jdk_root->SetText(prefs->GetJDKRootPath().CString());
  163. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  164. const AndroidBuildSettings& settings = buildSystem->GetBuildSettings()->GetAndroidSettings();
  165. appNameEdit_->SetText(settings.appName.CString());
  166. appPackageEdit_->SetText(settings.package.CString());
  167. productNameEdit_->SetText(settings.productName.CString());
  168. companyNameEdit_->SetText(settings.companyName.CString());
  169. sdkTargetSelect_->SetText(settings.targetSDKVersion.CString());
  170. }
  171. bool UIBuildSettingsAndroid::OnEvent(const TBWidgetEvent &ev)
  172. {
  173. Editor* editor = context_->GetSubsystem<Editor>();
  174. ProjectUtils* utils = context_->GetSubsystem<ProjectUtils>();
  175. if (ev.type == EVENT_TYPE_CLICK)
  176. {
  177. if (ev.target->GetID() == TBIDC("choose_sdk_path"))
  178. {
  179. String path = utils->GetAndroidSDKPath("");
  180. if (path.Length())
  181. {
  182. editor->GetPreferences()->SetAndroidSDKPath(path);
  183. Refresh();
  184. }
  185. return true;
  186. }
  187. else if (ev.target->GetID() == TBIDC("choose_ant_path"))
  188. {
  189. String path = utils->GetAntPath("");
  190. if (path.Length())
  191. {
  192. editor->GetPreferences()->SetAntPath(path);
  193. Refresh();
  194. }
  195. return true;
  196. }
  197. else if (ev.target->GetID() == TBIDC("choose_jdk_root"))
  198. {
  199. String path = utils->GetJDKRootPath("");
  200. if (path.Length())
  201. {
  202. editor->GetPreferences()->SetJDKRootPath(path);
  203. Refresh();
  204. }
  205. return true;
  206. }
  207. else if (ev.target->GetID() == TBIDC("refresh_sdk_targets"))
  208. {
  209. RefreshAndroidTargets();
  210. }
  211. }
  212. return false;
  213. }
  214. }