*/}}

example_viewer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Part of LaGUI demonstration programs
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. STRUCTURE(ExampleItem){
  20. laListItem Item;
  21. laSafeString* Name;
  22. laSafeString* Path;
  23. laSafeString* Code;
  24. laUiDefineFunc Define;
  25. };
  26. STRUCTURE(ExampleViewer){
  27. real pad;
  28. laListHandle Examples;
  29. ExampleItem* CurrentExample;
  30. int ShowCode;
  31. };
  32. ExampleViewer* EV;
  33. void ExamplesList(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  34. laColumn* c=laFirstColumn(uil),*cl,*cr; laSplitColumn(uil,c,0.4); cl=laLeftColumn(c,7); cr=laRightColumn(c,0);
  35. laShowItemFull(uil,cl,0,"viewer.examples",0,0,laui_IdentifierOnly,0);
  36. laUiItem* b=laOnConditionThat(uil,cr,laPropExpression(0,"viewer.current"));{
  37. laShowItemFull(uil,cr,0,"viewer.examples",LA_WIDGET_COLLECTION_SINGLE,0,0,0)->Flags|=LA_UI_FLAGS_NO_DECAL;
  38. }laElse(uil,b);{
  39. laShowLabel(uil,cr,"Select an example to begin.",0,0);
  40. }laEndCondition(uil,b);
  41. }
  42. static void AddExample(char* name, char* path, laUiDefineFunc func){
  43. ExampleItem* ei=memAcquire(sizeof(ExampleItem));
  44. strSafeSet(&ei->Name,name); strSafeSet(&ei->Path,path); ei->Define=func; strSafeSet(&ei->Code,"// Unable to locate source code for this example");
  45. lstAppendItem(&EV->Examples,ei);
  46. char filename[512]; sprintf(filename,"example_source_files/%s.c",path);
  47. FILE* f=fopen(filename,"r"); if(!f) return;
  48. int s; fseek(f,0,SEEK_END); s=ftell(f); fseek(f,0,SEEK_SET); if(!s){ fclose(f); return; }
  49. char* buf=calloc(1,s); fread(buf,s,1,f); fclose(f);
  50. strSafeSet(&ei->Code,buf);
  51. free(buf);
  52. }
  53. static void ExamplesCleanUp(){
  54. ExampleItem* ei;
  55. while(ei=lstPopItem(&EV->Examples)){ strSafeDestroy(&ei->Name); strSafeDestroy(&ei->Path); strSafeDestroy(&ei->Code); memFree(ei); }
  56. memFree(EV);
  57. }
  58. #define EXAMPLE_COMMON_BEGIN\
  59. ExampleItem* ei=This->EndInstance;\
  60. laColumn* c=laFirstColumn(uil);\
  61. laUiItem* row=laBeginRow(uil,c,0,0);\
  62. laShowItem(uil,c,This,"name")->Expand=1;\
  63. laUiItem* eui=laShowItem(uil,c,0,"viewer.show_code");eui->Flags|=LA_UI_FLAGS_EXPAND;\
  64. laEndRow(uil,row);\
  65. laUiItem* bu=laOnConditionThat(uil,c,laPropExpression(0,"viewer.show_code"));{\
  66. char instructions[256]; sprintf(instructions,"text=Open \"%s.c\" in external editor;file=%s.c",ei->Path->Ptr,ei->Path->Ptr);\
  67. row=laBeginRow(uil,c,0,0); laShowSeparator(uil,c)->Expand=1; \
  68. laShowItemFull(uil,c,0,"EXAMPLE_open_source_code",0,instructions,0,0);\
  69. laEndRow(uil,row);\
  70. laShowSeparator(uil,c);\
  71. laShowItem(uil,c,This,"base.code")->Extra->HeightCoeff=-2;\
  72. }laElse(uil,bu);{\
  73. row=laBeginRow(uil,c,0,0);\
  74. char instructions[256]; sprintf(instructions,"text=Launch \"%s\";program=%s",ei->Name->Ptr,ei->Path->Ptr);\
  75. laShowItemFull(uil,c,0,"EXAMPLE_launch_program",0,instructions,0,0);\
  76. laEndRow(uil,row);\
  77. laShowSeparator(uil,c);
  78. #define EXAMPLE_COMMON_END\
  79. }laEndCondition(uil,bu);
  80. void ui_Fruits(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  81. EXAMPLE_COMMON_BEGIN
  82. laShowLabel(uil,c,"\
  83. This demo mainly shows three things:\n\
  84. 1) The dynamic-typed list manipulation, operators, and UI adaptation in LaGUI.\n\
  85. 2) The ability to save and load data files based on the data structure description (via PropContainer and Prop).\n\
  86. 3) Automatic undo-redo support based on data structure description.\n\
  87. \n\
  88. Play with this demo:\n\
  89. --------------------\n\
  90. 1) Click 'Stuff' and 'Bowl' can reveal things inside those list handles.\n\
  91. 2) Add or remove fruits or bowls with the UI.\n\
  92. 3) You can select bowl reference in the last select box (COLLECTION_SELECTOR) of each fruit.\n\
  93. 4) Try pressing Ctrl-Z/Ctrl-Shift-Z to undo and redo after you changed anything. In the console you should be able to see undo status.\n\
  94. 5) Try closing the window while you have bowls created, a dialog would show to ask you to save changes.\n\
  95. 6) Assign new files with the dropbox to the right, use file name ending with .udf to save changes.\n\
  96. 7) You can later read the files using File->Read.\
  97. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  98. EXAMPLE_COMMON_END
  99. }
  100. void ui_Simplest(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  101. EXAMPLE_COMMON_BEGIN
  102. laColumn* c=laFirstColumn(uil);
  103. laShowLabel(uil,c,"\
  104. Hello! Thanks for trying LaGUI!\n\
  105. ===============================\n\
  106. The first example is the simplest possible LaGUI application, we only create a panel saying \"Hello world!\".\
  107. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  108. laUiItem* row=laBeginRow(uil,c,0,0);
  109. laShowItemFull(uil,c,0,"EXAMPLE_launch_program",0,"program=simplest;text=Click to launch the program",0,0);
  110. laEndRow(uil,row);
  111. laShowLabel(uil,c,"\n\
  112. How do I begin?\n\
  113. ---------------\n\
  114. Press the 🞆 button on the top left to explore LaGUI's built-in functionalities. Every panel in a LaGUI program should be accessible from the \
  115. 🞆 menu. Since this is the simplest program, you can only find built-in panels in there.\n\
  116. \n\
  117. To manipulate the layout:\n\
  118. -------------------------\n\
  119. 1) You can dock panels by pressing the 🗖 button, then drop the panel to approperiate region.\n\
  120. 2) To tear off a docked the panel, drag the panel title to the center of each region and release.\n\
  121. 3) You could maximize a region by clicking ⯆ on the top left of each region and select approperiate operations.\n\
  122. 4) You can also use 🗗 button on the top to create a new window/layout, then dock some new panels there to customize your workspace.\n\
  123. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  124. EXAMPLE_COMMON_END
  125. }
  126. void ui_Widgets(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  127. EXAMPLE_COMMON_BEGIN
  128. laShowLabel(uil,c,"\
  129. This demo shows commom widgets inside LaGUI.\n\
  130. \n\
  131. What you need to know about widgets in LaGUI:\n\
  132. ---------------------------------------------\n\
  133. 1) Except labels, you need a data source (a defined property in LaGUI) to show any other kind of widget.\n\
  134. 2) To show a button, you need to define an operator, and an operator is basically what you trigger an action with.\n\
  135. 3) A widget can have multiple style/looks controlled by mainly `ui->Flags`. Some ui types accept extra instructions as a string, like you \
  136. could specify \"text=something;\" to a button.\n\
  137. 4) You could arrange widgets in a columns or rows, also with groups and tabs. Currently, group/subprop widgets can not be used inside a row.\n\
  138. 5) Canvas widgets are drawn on their own offscreen buffers. They can also be customized to have your own drawing and event handling callbacks, \
  139. they can be set to display a certain property as well.\n\
  140. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  141. EXAMPLE_COMMON_END
  142. }
  143. void ui_WidgetFlags(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  144. EXAMPLE_COMMON_BEGIN
  145. laShowLabel(uil,c,"\
  146. This demo shows most-used UI flags and their effects on widgets.\n\
  147. \n\
  148. When using widget creator like `laShowItem`, set ui->Flags to the combination of UI flags to adjust the look and interaction of the widget. \
  149. Not all UI flags are supported by every kind of widgets, but the most common ones can always be used, and widgets will just ignore unsupported \
  150. UI flags and won't generate any errors.\n\
  151. \n\
  152. Launch the program to see the effect of each UI flags.\
  153. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  154. EXAMPLE_COMMON_END
  155. }
  156. void ui_Calculator(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  157. EXAMPLE_COMMON_BEGIN
  158. laColumn* c=laFirstColumn(uil);
  159. laShowLabel(uil,c,"\
  160. Calculator demo\n\
  161. ===============\n\
  162. This program mainly demonstrates how to use LaGUI to create a node-based UI. The program creates a simple node-socket graph structure for \
  163. the user to edit the data flow and the logic. The demo didn't implement the actual \"evaluation\" part, it's mainly to provide a reference to \
  164. how should one create such a data structure description in LaGUI's property system, and the special UI flags for enabling the node rack canvas.\n\
  165. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  166. laShowLabel(uil,c,"\
  167. Play with calculator demo:\n\
  168. --------------------------\n\
  169. 1) Click \"New Calculator\" to create a calculator page.\n\
  170. 2) Click the \"+\" buttons to the left/right of the rack name to add a node rack.\n\
  171. 3) Click \"Add Node\" button to insert a node into the rack.\n\
  172. 4) Insert more nodes and connecct nodes with each other, you can create more racks too.\n\
  173. 5) Ctrl-Z and Ctrl-Shift-Z to undo and redo, you should be able to see connections change as how you manipulated them.\n\
  174. 6) Try closing the window while you have an caluclator, a dialog would show to ask you to save changes. You can save them too.\n\
  175. 7) If you try to read calculator files, the calculator page that's referenced by any \"Reference\" node will be automatically loaded too.\n\
  176. 8) The imlementation of a DAG evaluator is left for you as a practice.\n\
  177. 9) There are some node-related configurable options in user preferences, try changing those and see what those options do.\n\
  178. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  179. EXAMPLE_COMMON_END
  180. }
  181. void ui_Modelling(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  182. EXAMPLE_COMMON_BEGIN
  183. laColumn* c=laFirstColumn(uil);
  184. laShowLabel(uil,c,"\
  185. Modelling functionality demo\n\
  186. ============================\n\
  187. LaGUI has a simple geometry layer that allows you to load or model mesh geometries. This example demonstrates the default modeller. \
  188. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  189. EXAMPLE_COMMON_END
  190. }
  191. int inv_LaunchDemo(laOperator* a, laEvent* e){
  192. char* program=strGetArgumentString(a->ExtraInstructionsP,"program"); if(!program) return LA_FINISHED;
  193. char command[512]; sprintf(command,"./%s &",program);
  194. system(command);
  195. return LA_FINISHED;
  196. }
  197. int inv_OpenDemoSource(laOperator* a, laEvent* e){
  198. char* file=strGetArgumentString(a->ExtraInstructionsP,"file"); if(!file) return LA_FINISHED;
  199. char command[512]; sprintf(command,"xdg-open example_source_files/%s &",file);
  200. system(command);
  201. return LA_FINISHED;
  202. }
  203. laPropContainer *pcGeneric,*pcFruits,*pcSimplest,*pcModelling,*pcCalculator,*pcWidgets,*pcWidgetFlags;
  204. void* get_ExampleViewer(void* unused){
  205. return EV;
  206. }
  207. laPropContainer* get_ExamplesGetType(ExampleItem* ei){
  208. if(ei->Define==ui_Fruits) return pcFruits;
  209. if(ei->Define==ui_Simplest) return pcSimplest;
  210. if(ei->Define==ui_Modelling) return pcModelling;
  211. if(ei->Define==ui_Calculator) return pcCalculator;
  212. if(ei->Define==ui_Widgets) return pcWidgets;
  213. if(ei->Define==ui_WidgetFlags) return pcWidgetFlags;
  214. return pcGeneric;
  215. }
  216. void set_CurrentExample(ExampleViewer* v, ExampleItem* ei){
  217. memAssignRef(v,&v->CurrentExample, ei); v->ShowCode=0;
  218. }
  219. #define EXAMPLE_ADD_PC(name,_PC,_UI)\
  220. pc=laAddPropertyContainer("example_" name, name "Example", name "example",0,_UI,sizeof(ExampleItem),0,0,1); _PC=pc;\
  221. laAddStringProperty(pc,"name","Name","Name of the example",LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(ExampleItem,Name),0,0,0,0,LA_READ_ONLY|LA_AS_IDENTIFIER);\
  222. laAddSubGroup(pc,"base","Base","Base example","program_example",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  223. static void InitExamples(){
  224. laCreateOperatorType("EXAMPLE_launch_program","Launch Program", "Launch example program",0,0,0,inv_LaunchDemo,0,L'🏃',0);
  225. laCreateOperatorType("EXAMPLE_open_source_code","Open Source Code", "Open source code of the example in a external program",0,0,0,inv_OpenDemoSource,0,L'🡵',0);
  226. laPropContainer* root=laDefineRoot(),*pc; laProp*p;
  227. laAddSubGroup(root,"viewer","Viewer","Example viewer","example_viewer",0,0,0,-1,0,get_ExampleViewer,0,0,0,0,0,0);
  228. pc=laAddPropertyContainer("example_viewer","Example Viewer","Example viewer root data",0,0,sizeof(ExampleViewer),0,0,1);
  229. laAddSubGroup(pc,"examples","Examples","Example programs","program_example",get_ExamplesGetType,0,0,offsetof(ExampleViewer,CurrentExample),0,0,0,set_CurrentExample,0,0,offsetof(ExampleViewer,Examples),0);
  230. laAddSubGroup(pc,"current","Current Example","Current example","program_example",get_ExamplesGetType,0,0,offsetof(ExampleViewer,CurrentExample),0,0,0,0,0,0,0,LA_UDF_REFER);
  231. p=laAddEnumProperty(pc,"show_code","Show Code","Show code instead of descriptions",0,0,0,0,0,offsetof(ExampleViewer,ShowCode),0,0,0,0,0,0,0,0,0,0);
  232. laAddEnumItemAs(p,"DESC","Description","Show description of the example",0,0);
  233. laAddEnumItemAs(p,"IMPL","Code","Show implementation of the example",1,0);
  234. pc=laAddPropertyContainer("program_example","Program Example","Program example",0,laui_IdentifierOnly,sizeof(ExampleItem),0,0,1); pcGeneric=pc;
  235. laAddStringProperty(pc,"name","Name","Name of the example",LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(ExampleItem,Name),0,0,0,0,LA_READ_ONLY|LA_AS_IDENTIFIER);
  236. laAddStringProperty(pc,"code","Code","Code of the example",LA_WIDGET_STRING_MULTI,0,0,0,1,offsetof(ExampleItem,Code),0,0,0,0,LA_READ_ONLY);
  237. EXAMPLE_ADD_PC("simplest",pcSimplest,ui_Simplest);
  238. EXAMPLE_ADD_PC("fruits",pcFruits,ui_Fruits);
  239. EXAMPLE_ADD_PC("widgets",pcWidgets,ui_Widgets);
  240. EXAMPLE_ADD_PC("widget_flags",pcWidgetFlags,ui_WidgetFlags);
  241. EXAMPLE_ADD_PC("calculator",pcCalculator,ui_Calculator);
  242. EXAMPLE_ADD_PC("modelling_main",pcModelling,ui_Modelling);
  243. EV=memAcquire(sizeof(ExampleViewer));
  244. AddExample("Simplest","simplest",ui_Simplest);
  245. AddExample("Widgets","widgets",ui_Widgets);
  246. AddExample("Widget Flags","widget_flags",ui_WidgetFlags);
  247. AddExample("Fruits","fruits",ui_Fruits);
  248. AddExample("Calculator","calculator",ui_Calculator);
  249. AddExample("Modelling","modelling_main",ui_Modelling);
  250. laSetCleanupCallback(ExamplesCleanUp);
  251. }
  252. int main(int argc, char *argv[]){
  253. laGetReady();
  254. char buf[512];getcwd(buf,512);
  255. printf("%s\n",buf);
  256. InitExamples();
  257. laRegisterUiTemplate("examples_list","Examples List", ExamplesList,0,0,"Demonstration",0,0,0);
  258. laWindow* w = laDesignWindow(-1,-1,800,600);
  259. laLayout* l = laDesignLayout(w,"LaGUI Examples");
  260. laCreatePanel(l->FirstBlock,"examples_list");
  261. laStartWindow(w);
  262. laMainLoop();
  263. }