UIBuildSettingsAndroid.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #endif
  112. #ifdef ATOMIC_PLATFORM_WINDOWS
  113. Vector<String> args;
  114. // android is a batch file on windows, so have to run with cmd /c
  115. args.Push("/c");
  116. args.Push("\"" + androidCommand + "\\tools\\android.bat\"");
  117. args.Push("list");
  118. args.Push("targets");
  119. androidCommand = "cmd";
  120. #endif
  121. #ifdef ATOMIC_PLATFORM_LINUX
  122. Vector<String> args = String("list targets").Split(' ');
  123. androidCommand += "tools/android";
  124. #endif
  125. targetOutput_.Clear();
  126. targetLookup_.Clear();
  127. Subprocess* subprocess = subs->Launch(androidCommand, args);
  128. if (!subprocess)
  129. {
  130. //ERROR
  131. assert(0);
  132. }
  133. SubscribeToEvent(subprocess, E_SUBPROCESSCOMPLETE, HANDLER(UIBuildSettingsAndroid, HandleEvent));
  134. SubscribeToEvent(subprocess, E_SUBPROCESSOUTPUT, HANDLER(UIBuildSettingsAndroid, HandleEvent));
  135. }
  136. void UIBuildSettingsAndroid::StoreSettings()
  137. {
  138. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  139. AndroidBuildSettings settings;
  140. TBStr text;
  141. appNameEdit_->GetText(text);
  142. settings.appName = text.CStr();
  143. text.Clear();
  144. appPackageEdit_->GetText(text);
  145. settings.package = text.CStr();
  146. text.Clear();
  147. productNameEdit_->GetText(text);
  148. settings.productName = text.CStr();
  149. text.Clear();
  150. companyNameEdit_->GetText(text);
  151. settings.companyName = text.CStr();
  152. text.Clear();
  153. sdkTargetSelect_->GetText(text);
  154. settings.targetSDKVersion = text.CStr();
  155. text.Clear();
  156. buildSystem->GetBuildSettings()->SetAndroidSettings(settings);
  157. }
  158. void UIBuildSettingsAndroid::Refresh()
  159. {
  160. Editor* editor = context_->GetSubsystem<Editor>();
  161. AEPreferences* prefs = editor->GetPreferences();
  162. TBEditField* sdk_path = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("sdk_path"));
  163. sdk_path->SetText(prefs->GetAndroidSDKPath().CString());
  164. TBEditField* ant_path = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("ant_path"));
  165. ant_path->SetText(prefs->GetAntPath().CString());
  166. TBEditField* jdk_root = delegate_->GetWidgetByIDAndType<TBEditField>(TBIDC("jdk_root"));
  167. jdk_root->SetText(prefs->GetJDKRootPath().CString());
  168. BuildSystem* buildSystem = GetSubsystem<BuildSystem>();
  169. const AndroidBuildSettings& settings = buildSystem->GetBuildSettings()->GetAndroidSettings();
  170. appNameEdit_->SetText(settings.appName.CString());
  171. appPackageEdit_->SetText(settings.package.CString());
  172. productNameEdit_->SetText(settings.productName.CString());
  173. companyNameEdit_->SetText(settings.companyName.CString());
  174. sdkTargetSelect_->SetText(settings.targetSDKVersion.CString());
  175. }
  176. bool UIBuildSettingsAndroid::OnEvent(const TBWidgetEvent &ev)
  177. {
  178. Editor* editor = context_->GetSubsystem<Editor>();
  179. ProjectUtils* utils = context_->GetSubsystem<ProjectUtils>();
  180. if (ev.type == EVENT_TYPE_CLICK)
  181. {
  182. if (ev.target->GetID() == TBIDC("choose_sdk_path"))
  183. {
  184. String path = utils->GetAndroidSDKPath("");
  185. if (path.Length())
  186. {
  187. editor->GetPreferences()->SetAndroidSDKPath(path);
  188. Refresh();
  189. }
  190. return true;
  191. }
  192. else if (ev.target->GetID() == TBIDC("choose_ant_path"))
  193. {
  194. String path = utils->GetAntPath("");
  195. if (path.Length())
  196. {
  197. editor->GetPreferences()->SetAntPath(path);
  198. Refresh();
  199. }
  200. return true;
  201. }
  202. else if (ev.target->GetID() == TBIDC("choose_jdk_root"))
  203. {
  204. String path = utils->GetJDKRootPath("");
  205. if (path.Length())
  206. {
  207. editor->GetPreferences()->SetJDKRootPath(path);
  208. Refresh();
  209. }
  210. return true;
  211. }
  212. else if (ev.target->GetID() == TBIDC("refresh_sdk_targets"))
  213. {
  214. RefreshAndroidTargets();
  215. }
  216. }
  217. return false;
  218. }
  219. }