Utils.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "Utils.h"
  9. #include "LastPathBus.h"
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/std/algorithm.h>
  12. #include <AzCore/Utils/Utils.h>
  13. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  14. #include <QFileDialog>
  15. namespace
  16. {
  17. template<typename StringType>
  18. void ToUnixPath(StringType& path)
  19. {
  20. AZStd::replace(path.begin(), path.end(), '\\', '/');
  21. }
  22. template<typename StringType>
  23. StringType GetAbsoluteEngineRoot()
  24. {
  25. AZ::IO::FixedMaxPath engineRoot = AZ::Utils::GetEnginePath();
  26. if (engineRoot.empty())
  27. {
  28. return "";
  29. }
  30. StringType engineRootString(engineRoot.c_str());
  31. ToUnixPath(engineRootString);
  32. return engineRootString;
  33. }
  34. template<typename StringType>
  35. StringType GetAbsoluteProjectRoot()
  36. {
  37. AZ::IO::FixedMaxPath projectRoot = AZ::Utils::GetProjectPath();
  38. if (projectRoot.empty())
  39. {
  40. return "";
  41. }
  42. StringType projectRootString(projectRoot.c_str());
  43. ToUnixPath(projectRootString);
  44. return projectRootString;
  45. }
  46. template<typename StringType>
  47. StringType GetProjectName();
  48. template<>
  49. AZStd::string GetProjectName()
  50. {
  51. auto projectName = AZ::Utils::GetProjectName();
  52. return AZStd::string{projectName.c_str()};
  53. }
  54. template<>
  55. QString GetProjectName()
  56. {
  57. auto projectName = AZ::Utils::GetProjectName();
  58. return QString::fromUtf8(projectName.c_str(), aznumeric_cast<int>(projectName.size()));
  59. }
  60. }
  61. namespace ProjectSettingsTool
  62. {
  63. void* ConvertFunctorToVoid(AZStd::pair<QValidator::State, const QString> (*func)(const QString&))
  64. {
  65. return reinterpret_cast<void*>(func);
  66. }
  67. AZStd::string GetEngineRoot()
  68. {
  69. return GetAbsoluteEngineRoot<AZStd::string>();
  70. }
  71. AZStd::string GetProjectRoot()
  72. {
  73. return GetAbsoluteProjectRoot<AZStd::string>();
  74. }
  75. AZStd::string GetProjectName()
  76. {
  77. return ::GetProjectName<AZStd::string>();
  78. }
  79. QString SelectXmlFromFileDialog(const QString& currentFile)
  80. {
  81. // The selected file must be relative to this path
  82. QString defaultPath = GetAbsoluteEngineRoot<QString>();
  83. QString startPath;
  84. // Choose the starting path for file dialog
  85. if (currentFile != "")
  86. {
  87. if (currentFile.contains(defaultPath))
  88. {
  89. startPath = currentFile;
  90. }
  91. else
  92. {
  93. startPath = defaultPath + currentFile;
  94. }
  95. }
  96. else
  97. {
  98. startPath = defaultPath;
  99. }
  100. QString pickedPath = QFileDialog::getOpenFileName(nullptr, QObject::tr("Select Override"),
  101. startPath, QObject::tr("Extensible Markup Language file (*.xml)"));
  102. ToUnixPath(pickedPath);
  103. // Remove the default relative path
  104. if (pickedPath.contains(defaultPath))
  105. {
  106. pickedPath = pickedPath.mid(defaultPath.length());
  107. }
  108. return pickedPath;
  109. }
  110. QString SelectImageFromFileDialog(const QString& currentFile)
  111. {
  112. QString defaultPath = QStringLiteral("%1Code%2/Resources/").arg(GetAbsoluteEngineRoot<QString>(), ::GetProjectName<QString>());
  113. QString startPath;
  114. // Choose the starting path for file dialog
  115. if (currentFile != "")
  116. {
  117. if (QDir::isAbsolutePath(currentFile))
  118. {
  119. startPath = currentFile;
  120. }
  121. else
  122. {
  123. startPath = defaultPath + currentFile;
  124. }
  125. }
  126. else
  127. {
  128. LastPathBus::BroadcastResult(
  129. startPath,
  130. &LastPathBus::Handler::GetLastImagePath);
  131. }
  132. QString pickedPath = QFileDialog::getOpenFileName(nullptr, QObject::tr("Select Image"),
  133. startPath, QObject::tr("Image file (*.png)"));
  134. ToUnixPath(pickedPath);
  135. if (!pickedPath.isEmpty())
  136. {
  137. LastPathBus::Broadcast(
  138. &LastPathBus::Handler::SetLastImagePath,
  139. pickedPath.left(pickedPath.lastIndexOf('/')));
  140. }
  141. // Remove the default relative path if it is used
  142. if (pickedPath.contains(defaultPath))
  143. {
  144. pickedPath = pickedPath.mid(defaultPath.length());
  145. }
  146. return pickedPath;
  147. }
  148. AZStd::string GenDefaultImagePath(ImageGroup group, AZStd::string size)
  149. {
  150. AZStd::string root;
  151. // Android
  152. if (group <= ImageGroup::AndroidPortrait)
  153. {
  154. root = GetEngineRoot() + "/Code/Tools/Android/ProjectBuilder/app_";
  155. }
  156. //Ios
  157. else
  158. {
  159. using AZ::IO::SystemFile;
  160. root = GetProjectRoot() + "/Gem/Resources/Platform/iOS/Images.xcassets/";
  161. if (!SystemFile::Exists(root.c_str()))
  162. {
  163. root = GetProjectRoot() + "/Gem/Resources/IOSLauncher/Images.xcassets/";
  164. }
  165. }
  166. AZStd::string groupStr;
  167. switch (group)
  168. {
  169. case ImageGroup::AndroidIcons:
  170. groupStr = "icon-";
  171. break;
  172. case ImageGroup::AndroidLandscape:
  173. groupStr = "splash-land-";
  174. break;
  175. case ImageGroup::AndroidPortrait:
  176. groupStr = "splash-port-";
  177. break;
  178. case ImageGroup::IosIcons:
  179. groupStr = "AppIcon.appiconset/";
  180. break;
  181. case ImageGroup::IosLaunchScreens:
  182. groupStr = "LaunchImage.launchimage/";
  183. break;
  184. default:
  185. AZ_Assert(false, "Unknown ImageGroup.");
  186. break;
  187. }
  188. return root + groupStr + size + ".png";
  189. }
  190. } // namespace ProjectSettingsTool