*/}}

example_viewer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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+1); 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_Operator(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  127. EXAMPLE_COMMON_BEGIN
  128. laColumn* c=laFirstColumn(uil);
  129. laShowLabel(uil,c,"\n\
  130. Operator demonstration program\n\
  131. ------------------------------\n\
  132. Press the \"Something!\" Button to invoke a one-time operator, you can see some strings printed on stdout and LaGUI Terminal.\n\
  133. Use 🞆->Terminal to show LaGUI Terminal.",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  134. EXAMPLE_COMMON_END
  135. }
  136. void ui_SimpleProperties(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  137. EXAMPLE_COMMON_BEGIN
  138. laColumn* c=laFirstColumn(uil);
  139. laShowLabel(uil,c,"\n\
  140. Properties demonstration program\n\
  141. --------------------------------\n\
  142. The simplest program to demonstrate how to register properties for them to show on the interface.\n\n\
  143. Play with this demo:\n\
  144. --------------------\n\
  145. 1) You can manipulate the value widgets.\n\
  146. 2) You can right click them for extra opeartions.\n\
  147. 3) Create a new \"Properties\" panel from the 🞆 button, \
  148. verify that adjustiing values in either panels would cause the other panel to refresh in sync.\n\
  149. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  150. EXAMPLE_COMMON_END
  151. }
  152. void ui_Widgets(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  153. EXAMPLE_COMMON_BEGIN
  154. laShowLabel(uil,c,"\
  155. This demo shows commom widgets inside LaGUI.\n\
  156. \n\
  157. What you need to know about widgets in LaGUI:\n\
  158. ---------------------------------------------\n\
  159. 1) Except labels, you need a data source (a defined property in LaGUI) to show any other kind of widget.\n \
  160. 2) To show a button, you need to define an operator, and an operator is basically what you trigger an action with.\n \
  161. 3) A widget can have multiple style/looks controlled by mainly `ui->Flags`. Some ui types accept extra instructions as a string, like you \
  162. could specify \"text=something;\" to a button.\n\
  163. 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\
  164. 5) Canvas widgets are drawn on their own offscreen buffers. They can also be customized to have your own drawing and event handling callbacks, \
  165. they can be set to display a certain property as well.\n\
  166. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  167. EXAMPLE_COMMON_END
  168. }
  169. void ui_WidgetFlags(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  170. EXAMPLE_COMMON_BEGIN
  171. laShowLabel(uil,c,"\
  172. This demo shows most-used UI flags and their effects on widgets.\n\
  173. \n\
  174. When using widget creator like `laShowItem`, set ui->Flags to the combination of UI flags to adjust the look and interaction of the widget. \
  175. 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 \
  176. UI flags and won't generate any errors.\n\
  177. \n\
  178. Launch the program to see the effect of each UI flags.\
  179. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  180. EXAMPLE_COMMON_END
  181. }
  182. void ui_Calculator(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  183. EXAMPLE_COMMON_BEGIN
  184. laColumn* c=laFirstColumn(uil);
  185. laShowLabel(uil,c,"\
  186. Calculator demo\n\
  187. ===============\n\
  188. This program mainly demonstrates how to use LaGUI to create a node-based UI. The program creates a simple node-socket graph structure for \
  189. 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 \
  190. 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\
  191. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  192. laShowLabel(uil,c,"\
  193. Play with calculator demo:\n\
  194. --------------------------\n\
  195. 1) Click \"New Calculator\" to create a calculator page.\n\
  196. 2) Click the \"+\" buttons to the left/right of the rack name to add a node rack.\n\
  197. 3) Click \"Add Node\" button to insert a node into the rack.\n\
  198. 4) Insert more nodes and connecct nodes with each other, you can create more racks too.\n\
  199. 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\
  200. 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\
  201. 7) If you try to read calculator files, the calculator page that's referenced by any \"Reference\" node will be automatically loaded too.\n\
  202. 8) The imlementation of a DAG evaluator is left for you as a practice.\n\
  203. 9) There are some node-related configurable options in user preferences, try changing those and see what those options do.\n\
  204. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  205. EXAMPLE_COMMON_END
  206. }
  207. void ui_Modelling(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  208. EXAMPLE_COMMON_BEGIN
  209. laColumn* c=laFirstColumn(uil);
  210. laShowLabel(uil,c,"\
  211. Modelling functionality demo\n\
  212. ============================\n\
  213. LaGUI has a simple geometry layer that allows you to load or model mesh geometries. This example demonstrates the default modeller. \
  214. ",0,0)->Flags|=LA_TEXT_LINE_WRAP|LA_TEXT_MONO;
  215. EXAMPLE_COMMON_END
  216. }
  217. int inv_LaunchDemo(laOperator* a, laEvent* e){
  218. char* program=strGetArgumentString(a->ExtraInstructionsP,"program"); if(!program) return LA_FINISHED;
  219. char command[512]; sprintf(command,"./%s &",program);
  220. system(command);
  221. return LA_FINISHED;
  222. }
  223. int inv_OpenDemoSource(laOperator* a, laEvent* e){
  224. char* file=strGetArgumentString(a->ExtraInstructionsP,"file"); if(!file) return LA_FINISHED;
  225. char command[512]; sprintf(command,"xdg-open example_source_files/%s &",file);
  226. system(command);
  227. return LA_FINISHED;
  228. }
  229. laPropContainer *pcGeneric,*pcFruits,*pcSimplest,*pcOperator,*pcSimpleProperties,*pcModelling,*pcCalculator,*pcWidgets,*pcWidgetFlags;
  230. void* get_ExampleViewer(void* unused){
  231. return EV;
  232. }
  233. laPropContainer* get_ExamplesGetType(ExampleItem* ei){
  234. if(ei->Define==ui_Fruits) return pcFruits;
  235. if(ei->Define==ui_Simplest) return pcSimplest;
  236. if(ei->Define==ui_Operator) return pcOperator;
  237. if(ei->Define==ui_SimpleProperties) return pcSimpleProperties;
  238. if(ei->Define==ui_Modelling) return pcModelling;
  239. if(ei->Define==ui_Calculator) return pcCalculator;
  240. if(ei->Define==ui_Widgets) return pcWidgets;
  241. if(ei->Define==ui_WidgetFlags) return pcWidgetFlags;
  242. return pcGeneric;
  243. }
  244. void set_CurrentExample(ExampleViewer* v, ExampleItem* ei){
  245. memAssignRef(v,&v->CurrentExample, ei); v->ShowCode=0;
  246. }
  247. #define EXAMPLE_ADD_PC(name,_PC,_UI) \
  248. pc=laAddPropertyContainer("example_" name, name "Example", name "example",0,_UI,sizeof(ExampleItem),0,0,1); _PC=pc;\
  249. 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);\
  250. laAddSubGroup(pc,"base","Base","Base example","program_example",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  251. static void InitExamples(){
  252. laCreateOperatorType("EXAMPLE_launch_program","Launch Program", "Launch example program",0,0,0,inv_LaunchDemo,0,L'🏃',0);
  253. 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);
  254. laPropContainer* root=laDefineRoot(),*pc; laProp*p;
  255. laAddSubGroup(root,"viewer","Viewer","Example viewer","example_viewer",0,0,0,-1,0,get_ExampleViewer,0,0,0,0,0,0);
  256. pc=laAddPropertyContainer("example_viewer","Example Viewer","Example viewer root data",0,0,sizeof(ExampleViewer),0,0,1);
  257. 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);
  258. 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);
  259. 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);
  260. laAddEnumItemAs(p,"DESC","Description","Show description of the example",0,0);
  261. laAddEnumItemAs(p,"IMPL","Code","Show implementation of the example",1,0);
  262. pc=laAddPropertyContainer("program_example","Program Example","Program example",0,laui_IdentifierOnly,sizeof(ExampleItem),0,0,1); pcGeneric=pc;
  263. 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);
  264. 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);
  265. EXAMPLE_ADD_PC("simplest",pcSimplest,ui_Simplest);
  266. EXAMPLE_ADD_PC("operator",pcOperator,ui_Operator);
  267. EXAMPLE_ADD_PC("simple_properties",pcSimpleProperties,ui_SimpleProperties);
  268. EXAMPLE_ADD_PC("fruits",pcFruits,ui_Fruits);
  269. EXAMPLE_ADD_PC("widgets",pcWidgets,ui_Widgets);
  270. EXAMPLE_ADD_PC("widget_flags",pcWidgetFlags,ui_WidgetFlags);
  271. EXAMPLE_ADD_PC("calculator",pcCalculator,ui_Calculator);
  272. EXAMPLE_ADD_PC("modelling_main",pcModelling,ui_Modelling);
  273. EV=memAcquire(sizeof(ExampleViewer));
  274. AddExample("Simplest","simplest",ui_Simplest);
  275. AddExample("Operator","operator",ui_Operator);
  276. AddExample("Simple Properties","simple_properties",ui_SimpleProperties);
  277. AddExample("Widgets","widgets",ui_Widgets);
  278. AddExample("Widget Flags","widget_flags",ui_WidgetFlags);
  279. AddExample("Fruits","fruits",ui_Fruits);
  280. AddExample("Calculator","calculator",ui_Calculator);
  281. AddExample("Modelling","modelling_main",ui_Modelling);
  282. laSetCleanupCallback(ExamplesCleanUp);
  283. }
  284. int main(int argc, char *argv[]){
  285. laGetReady();
  286. char buf[512];getcwd(buf,512);
  287. printf("%s\n",buf);
  288. InitExamples();
  289. laRegisterUiTemplate("examples_list","Examples List", ExamplesList,0,0,"Demonstration",0,0,0);
  290. laWindow* w = laDesignWindow(-1,-1,800,600);
  291. laLayout* l = laDesignLayout(w,"LaGUI Examples");
  292. laCreatePanel(l->FirstBlock,"examples_list");
  293. laStartWindow(w);
  294. laMainLoop();
  295. }