main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 : ListBox
  33. Name = "projectList"
  34. Color = 190,255,190
  35. Left = 90%-100
  36. Right = 100%-5
  37. Top = 5
  38. Bottom = 100%-50
  39. End
  40. Begin : Button
  41. Name = "buildButton"
  42. Text = "Build and run"
  43. Color = 190,255,190
  44. Left = 90%-100
  45. Right = 100%-5
  46. Top = 100%-45
  47. Bottom = 100%-5
  48. End
  49. Begin : Picture
  50. Name = "previewPicture"
  51. Interpolation = 1
  52. Left = 5
  53. Top = 5
  54. Right = 90%-105
  55. Bottom = 70%-5
  56. End
  57. Begin : Label
  58. Name = "descriptionLabel"
  59. Color = 190,255,190
  60. Left = 5
  61. Right = 90%-105
  62. Top = 70%
  63. Bottom = 100%-5
  64. End
  65. End
  66. End
  67. )QUOTE";
  68. // Visual components
  69. Component projectList;
  70. Component buildButton;
  71. Component descriptionLabel;
  72. Component previewPicture;
  73. // Media
  74. int boomSound;
  75. struct Project {
  76. String buildScript; // To execute
  77. String title; // To display
  78. String description; // To show when selected
  79. OrderedImageRgbaU8 preview;
  80. Project(const ReadableString &buildScript);
  81. };
  82. List<Project> projects;
  83. Project::Project(const ReadableString &buildScript)
  84. : buildScript(buildScript) {
  85. String projectFolderPath = file_getRelativeParentFolder(buildScript);
  86. this->title = file_getPathlessName(projectFolderPath);
  87. String descriptionPath = file_combinePaths(projectFolderPath, U"Description.txt");
  88. if (file_getEntryType(descriptionPath) == EntryType::File) {
  89. this->description = string_load(descriptionPath);
  90. } else {
  91. this->description = string_combine(U"Project at ", projectFolderPath, U" did not have any Description.txt to display!");
  92. }
  93. String previewPath = file_combinePaths(projectFolderPath, U"Preview.jpg");
  94. if (file_getEntryType(previewPath) == EntryType::File) {
  95. this->preview = image_load_RgbaU8(previewPath);
  96. } else {
  97. previewPath = file_combinePaths(projectFolderPath, U"Preview.gif");
  98. if (file_getEntryType(previewPath) == EntryType::File) {
  99. this->preview = image_load_RgbaU8(previewPath);
  100. } else {
  101. this->preview = OrderedImageRgbaU8();
  102. }
  103. }
  104. }
  105. static ReadableString findParent(const ReadableString& startPath, const ReadableString& parentName) {
  106. int64_t pathEndIndex = -1; // Last character of path leading to Source.
  107. file_getPathEntries(startPath, [&pathEndIndex, &parentName](ReadableString entry, int64_t firstIndex, int64_t lastIndex) {
  108. if (string_match(entry, parentName)) {
  109. pathEndIndex = lastIndex;
  110. }
  111. });
  112. if (pathEndIndex == -1) {
  113. throwError(U"Could not find the Source folder with SDK examples.");
  114. return startPath;
  115. } else {
  116. return string_until(startPath, pathEndIndex);
  117. }
  118. }
  119. static void findProjects(const ReadableString& folderPath) {
  120. file_getFolderContent(folderPath, [](const ReadableString& entryPath, const ReadableString& entryName, EntryType entryType) {
  121. if (entryType == EntryType::Folder) {
  122. findProjects(entryPath);
  123. } else if (entryType == EntryType::File) {
  124. ReadableString extension = string_upperCase(file_getExtension(entryName));
  125. Project newProject = Project(entryPath);
  126. // If we find a project within folderPath...
  127. if (string_match(extension, U"DSRPROJ")) {
  128. // ...and the folder is not namned wizard...
  129. if (!string_match(newProject.title, U"wizard")) {
  130. // ...then add it to the list of projects.
  131. projects.push(newProject);
  132. }
  133. }
  134. }
  135. });
  136. }
  137. static void selectProject(int64_t index) {
  138. int oldIndex = component_getProperty_integer(projectList, U"SelectedIndex", true);
  139. printText(oldIndex, U" -> ", index, U"\n");
  140. // Don't trigger new events if the selected index is already updated manually.
  141. if (index != oldIndex) {
  142. printText(U"Assigned ", index, U"\n");
  143. component_setProperty_integer(projectList, U"SelectedIndex", index, false);
  144. }
  145. printText(U"Assigning description\n");
  146. component_setProperty_string(descriptionLabel, U"Text", projects[index].description);
  147. component_setProperty_image(previewPicture, U"Image", projects[index].preview, false);
  148. }
  149. static void populateInterface(const ReadableString& folderPath) {
  150. findProjects(folderPath);
  151. for (int p = 0; p < projects.length(); p++) {
  152. component_call(projectList, U"PushElement", projects[p].title);
  153. }
  154. selectProject(0);
  155. }
  156. DSR_MAIN_CALLER(dsrMain)
  157. void dsrMain(List<String> args) {
  158. // Start sound
  159. sound_initialize();
  160. boomSound = loadSoundFromFile(file_combinePaths(file_getApplicationFolder(), U"Boom.wav"));
  161. // Create a window
  162. window = window_create(U"DFPSR wizard application", 800, 600);
  163. window_loadInterfaceFromString(window, interfaceContent);
  164. // Find components
  165. projectList = window_findComponentByName(window, U"projectList");
  166. buildButton = window_findComponentByName(window, U"buildButton");
  167. descriptionLabel = window_findComponentByName(window, U"descriptionLabel");
  168. previewPicture = window_findComponentByName(window, U"previewPicture");
  169. // Find projects to showcase
  170. // On systems that don't allow getting the application's folder, the program must be started somewhere within the Source folder.
  171. String applicationFolder = file_getApplicationFolder();
  172. String sourceFolder = findParent(applicationFolder, U"Source");
  173. populateInterface(sourceFolder);
  174. // Bind methods to events
  175. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  176. DsrKey key = event.dsrKey;
  177. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  178. if (key == DsrKey_Escape) {
  179. running = false;
  180. }
  181. }
  182. });
  183. component_setPressedEvent(buildButton, []() {
  184. // TODO: Implement building and running of the selected project.
  185. playSound(boomSound, false, 1.0, 1.0, 0.7);
  186. component_setProperty_string(descriptionLabel, U"Text", U"Compiling and running projects from the wizard application is not yet implemented.");
  187. });
  188. component_setSelectEvent(projectList, [](int64_t index) {
  189. printText(U"Selecting ", index, U"\n");
  190. playSound(boomSound, false, 0.5, 0.5, 0.5);
  191. selectProject(index);
  192. });
  193. window_setCloseEvent(window, []() {
  194. running = false;
  195. });
  196. // Execute
  197. playSound(boomSound, false, 1.0, 1.0, 0.25);
  198. while(running) {
  199. // Wait for actions so that we don't render until an action has been recieved
  200. // This will save battery on laptops for applications that don't require animation
  201. while (!window_executeEvents(window)) {
  202. time_sleepSeconds(0.01);
  203. }
  204. // Fill the background
  205. AlignedImageRgbaU8 canvas = window_getCanvas(window);
  206. image_fill(canvas, ColorRgbaI32(64, 64, 64, 255));
  207. // Draw interface
  208. window_drawComponents(window);
  209. // Show the final image
  210. window_showCanvas(window);
  211. }
  212. // Close sound
  213. sound_terminate();
  214. }