main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. 
  2. // TODO:
  3. // * A catalogue of SDK examples with images and descriptions loaded automatically from their folder.
  4. // Offer one-click build and execution of SDK examples on multiple platforms, while explaining how the building works.
  5. // How can the file library execute other applications and scripts in a portable way when scripts need to select a terminal application to execute them?
  6. // Maybe call the builder as a static library and have it call the compiler directly in a simulated terminal window embedded into the wizard, instead of using unreliable scripts?
  7. // * Let the user browse a file system and select a location for a new or existing project.
  8. // Should a multi-frame tab container be created to allow having multiple frames in the same container?
  9. // Can let frames have a caption for when used within a container.
  10. #include "../../DFPSR/includeFramework.h"
  11. #include "sound.h"
  12. using namespace dsr;
  13. // Global
  14. bool running = true;
  15. Window window;
  16. String interfaceContent =
  17. UR"QUOTE(
  18. Begin : Panel
  19. Name = "mainPanel"
  20. Solid = 0
  21. Begin : Panel
  22. Name = "upperPanel"
  23. Bottom = 50
  24. Solid = 1
  25. Color = 190,255,190
  26. End
  27. Begin : Panel
  28. Name = "lowerPanel"
  29. Solid = 1
  30. Top = 50
  31. Color = 0,0,0
  32. Begin : Picture
  33. Name = "previewPicture"
  34. Interpolation = 1
  35. Left = 5
  36. Top = 5
  37. Right = 90%-105
  38. Bottom = 70%-5
  39. End
  40. Begin : Label
  41. Name = "descriptionLabel"
  42. Color = 190,255,190
  43. Left = 5
  44. Right = 90%-105
  45. Top = 70%
  46. Bottom = 100%-5
  47. End
  48. Begin : ListBox
  49. Name = "projectList"
  50. Color = 190,255,190
  51. Left = 90%-100
  52. Right = 100%-5
  53. Top = 5
  54. Bottom = 100%-50
  55. End
  56. Begin : Button
  57. Name = "launchButton"
  58. Text = "Launch"
  59. Color = 190,255,190
  60. Left = 90%-100
  61. Right = 100%-5
  62. Top = 100%-45
  63. Bottom = 100%-5
  64. End
  65. End
  66. End
  67. )QUOTE";
  68. // Visual components
  69. Component projectList;
  70. Component launchButton;
  71. Component descriptionLabel;
  72. Component previewPicture;
  73. // Media
  74. int boomSound;
  75. struct Project {
  76. String projectFilePath;
  77. String executableFilePath;
  78. String title; // To display
  79. String description; // To show when selected
  80. DsrProcess programHandle;
  81. DsrProcessStatus lastStatus = DsrProcessStatus::NotStarted;
  82. OrderedImageRgbaU8 preview;
  83. Project(const ReadableString &projectFilePath);
  84. };
  85. List<Project> projects;
  86. Project::Project(const ReadableString &projectFilePath)
  87. : projectFilePath(projectFilePath) {
  88. String projectFolderPath = file_getRelativeParentFolder(projectFilePath);
  89. String extensionlessProjectPath = file_getExtensionless(projectFilePath);
  90. this->title = file_getPathlessName(extensionlessProjectPath);
  91. // TODO: Get the native extension for each type of file? .exe, .dll, .so...
  92. #ifdef USE_MICROSOFT_WINDOWS
  93. this->executableFilePath = string_combine(extensionlessProjectPath, U".exe");
  94. #else
  95. this->executableFilePath = extensionlessProjectPath;
  96. #endif
  97. if (file_getEntryType(this->executableFilePath) != EntryType::File) {
  98. this->executableFilePath = U"";
  99. }
  100. String descriptionPath = file_combinePaths(projectFolderPath, U"Description.txt");
  101. if (file_getEntryType(descriptionPath) == EntryType::File) {
  102. this->description = string_load(descriptionPath);
  103. } else {
  104. this->description = string_combine(U"Project at ", projectFolderPath, U" did not have any Description.txt to display!");
  105. }
  106. String previewPath = file_combinePaths(projectFolderPath, U"Preview.jpg");
  107. if (file_getEntryType(previewPath) == EntryType::File) {
  108. this->preview = image_load_RgbaU8(previewPath);
  109. } else {
  110. previewPath = file_combinePaths(projectFolderPath, U"Preview.gif");
  111. if (file_getEntryType(previewPath) == EntryType::File) {
  112. this->preview = image_load_RgbaU8(previewPath);
  113. } else {
  114. this->preview = OrderedImageRgbaU8();
  115. }
  116. }
  117. }
  118. static ReadableString findParent(const ReadableString& startPath, const ReadableString& parentName) {
  119. int64_t pathEndIndex = -1; // Last character of path leading to Source.
  120. file_getPathEntries(startPath, [&pathEndIndex, &parentName](ReadableString entry, int64_t firstIndex, int64_t lastIndex) {
  121. if (string_match(entry, parentName)) {
  122. pathEndIndex = lastIndex;
  123. }
  124. });
  125. if (pathEndIndex == -1) {
  126. throwError(U"Could not find the Source folder with SDK examples.");
  127. return startPath;
  128. } else {
  129. return string_until(startPath, pathEndIndex);
  130. }
  131. }
  132. static void findProjects(const ReadableString& folderPath) {
  133. file_getFolderContent(folderPath, [](const ReadableString& entryPath, const ReadableString& entryName, EntryType entryType) {
  134. if (entryType == EntryType::Folder) {
  135. findProjects(entryPath);
  136. } else if (entryType == EntryType::File) {
  137. ReadableString extension = string_upperCase(file_getExtension(entryName));
  138. Project newProject = Project(entryPath);
  139. // If we find a project within folderPath...
  140. if (string_match(extension, U"DSRPROJ")) {
  141. // ...and the folder is not namned wizard...
  142. if (!string_match(newProject.title, U"Wizard")) {
  143. // ...then add it to the list of projects.
  144. projects.push(newProject);
  145. }
  146. }
  147. }
  148. });
  149. }
  150. // Returns true iff the interface needs to be redrawn.
  151. static bool updateInterface(bool forceUpdate) {
  152. bool needToDraw = false;
  153. int projectIndex = component_getProperty_integer(projectList, U"SelectedIndex", true);
  154. //Application name from project name?
  155. if (projectIndex >= 0 && projectIndex < projects.length()) {
  156. DsrProcessStatus newStatus = process_getStatus(projects[projectIndex].programHandle);
  157. DsrProcessStatus lastStatus = projects[projectIndex].lastStatus;
  158. if (newStatus != lastStatus || forceUpdate) {
  159. if (newStatus == DsrProcessStatus::Running) {
  160. component_setProperty_string(descriptionLabel, U"Text", string_combine(projects[projectIndex].title, U" is running."));
  161. } else if (newStatus == DsrProcessStatus::Crashed) {
  162. component_setProperty_string(descriptionLabel, U"Text", string_combine(projects[projectIndex].title, U" crashed."));
  163. } else if (newStatus == DsrProcessStatus::Completed) {
  164. component_setProperty_string(descriptionLabel, U"Text", string_combine(projects[projectIndex].title, U" terminated safely."));
  165. } else if (newStatus == DsrProcessStatus::NotStarted) {
  166. component_setProperty_string(descriptionLabel, U"Text", projects[projectIndex].description);
  167. }
  168. needToDraw = true;
  169. projects[projectIndex].lastStatus = newStatus;
  170. }
  171. component_setProperty_image(previewPicture, U"Image", projects[projectIndex].preview, false);
  172. bool foundExecutable = string_length(projects[projectIndex].executableFilePath) > 0;
  173. component_setProperty_integer(launchButton, U"Visible", foundExecutable);
  174. }
  175. return needToDraw;
  176. }
  177. static void selectProject(int64_t projectIndex) {
  178. // Don't trigger new events if the selected index is already updated manually.
  179. if (projectIndex != component_getProperty_integer(projectList, U"SelectedIndex", true)) {
  180. component_setProperty_integer(projectList, U"SelectedIndex", projectIndex, false);
  181. }
  182. updateInterface(true);
  183. }
  184. static void populateInterface(const ReadableString& folderPath) {
  185. findProjects(folderPath);
  186. for (int p = 0; p < projects.length(); p++) {
  187. component_call(projectList, U"PushElement", projects[p].title);
  188. }
  189. selectProject(0);
  190. }
  191. DSR_MAIN_CALLER(dsrMain)
  192. void dsrMain(List<String> args) {
  193. // Start sound
  194. sound_initialize();
  195. boomSound = loadSoundFromFile(file_combinePaths(file_getApplicationFolder(), U"Boom.wav"));
  196. // Create a window
  197. window = window_create(U"DFPSR wizard application", 800, 600);
  198. window_loadInterfaceFromString(window, interfaceContent);
  199. // Find components
  200. projectList = window_findComponentByName(window, U"projectList");
  201. launchButton = window_findComponentByName(window, U"launchButton");
  202. descriptionLabel = window_findComponentByName(window, U"descriptionLabel");
  203. previewPicture = window_findComponentByName(window, U"previewPicture");
  204. // Find projects to showcase
  205. // On systems that don't allow getting the application's folder, the program must be started somewhere within the Source folder.
  206. String applicationFolder = file_getApplicationFolder();
  207. String sourceFolder = findParent(applicationFolder, U"Source");
  208. populateInterface(sourceFolder);
  209. // Bind methods to events
  210. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  211. DsrKey key = event.dsrKey;
  212. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  213. if (key == DsrKey_Escape) {
  214. running = false;
  215. }
  216. }
  217. });
  218. component_setPressedEvent(launchButton, []() {
  219. // TODO: Implement building and running of the selected project.
  220. playSound(boomSound, false, 1.0, 1.0, 0.7);
  221. int projectIndex = component_getProperty_integer(projectList, U"SelectedIndex", true);
  222. //Application name from project name?
  223. if (projectIndex >= 0 && projectIndex < projects.length()) {
  224. if (file_getEntryType(projects[projectIndex].executableFilePath) != EntryType::File) {
  225. // Could not find the application.
  226. component_setProperty_string(descriptionLabel, U"Text", string_combine(U"Could not find the executable at ", projects[projectIndex].executableFilePath, U"!\n"), true);
  227. } else if (process_getStatus(projects[projectIndex].programHandle) != DsrProcessStatus::Running) {
  228. // Select input arguments.
  229. List<String> arguments;
  230. if (string_match(projects[projectIndex].title, U"BasicCLI")) {
  231. // Give some random arguments to the CLI template, so that it will do something more than just printing "Hello World".
  232. arguments.push(U"1");
  233. arguments.push(U"TWO");
  234. arguments.push(U"three");
  235. arguments.push(U"Four");
  236. }
  237. // Launch the application.
  238. projects[projectIndex].programHandle = process_execute(projects[projectIndex].executableFilePath, arguments);
  239. updateInterface(true);
  240. }
  241. }
  242. });
  243. component_setSelectEvent(projectList, [](int64_t index) {
  244. playSound(boomSound, false, 0.5, 0.5, 0.5);
  245. selectProject(index);
  246. });
  247. window_setCloseEvent(window, []() {
  248. running = false;
  249. });
  250. // Execute
  251. playSound(boomSound, false, 1.0, 1.0, 0.25);
  252. while(running) {
  253. // Wait for actions so that we don't render until an action has been recieved
  254. // This will save battery on laptops for applications that don't require animation
  255. while (!(window_executeEvents(window) || updateInterface(false))) {
  256. time_sleepSeconds(0.01);
  257. }
  258. // Fill the background
  259. AlignedImageRgbaU8 canvas = window_getCanvas(window);
  260. image_fill(canvas, ColorRgbaI32(64, 64, 64, 255));
  261. // Draw interface
  262. window_drawComponents(window);
  263. // Show the final image
  264. window_showCanvas(window);
  265. }
  266. // Close sound
  267. sound_terminate();
  268. }