*/}}

fruits.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. extern LA MAIN;
  20. STRUCTURE(Bowl){
  21. laListItem Item;
  22. laSafeString* Name;
  23. laListHandle Fruits;
  24. };
  25. STRUCTURE(Basket){
  26. int pad;
  27. int example;
  28. laSafeString* name;
  29. laListHandle Stuff;
  30. laListHandle Bowls;
  31. };
  32. #define FRUIT_TYPE_APPLE 1
  33. #define FRUIT_TYPE_PEAR 2
  34. STRUCTURE(Fruit){
  35. laListItem Item;
  36. int Type;
  37. Fruit* WhyNot;
  38. laListHandle* Parent;
  39. Bowl* Container;
  40. laListHandle Bundled;
  41. };
  42. STRUCTURE(Apple){
  43. Fruit Base;
  44. real HowSweet;
  45. };
  46. STRUCTURE(Pear){
  47. Fruit Base;
  48. int Really;
  49. };
  50. Basket B={0};
  51. laPropContainer* pcApple,*pcPear;
  52. laProp* pBasket, *pFruit, *pBowl;
  53. int INV_AddBowl(laOperator* a, laEvent* e){
  54. Bowl* b=memAcquireHyper(sizeof(Bowl));
  55. lstAppendItem(&B.Bowls,b);
  56. laRecordAndPushProp(0, "basket");laNotifyUsers("basket");
  57. laPrintDBInstInfo();
  58. return LA_FINISHED;
  59. }
  60. int INV_RemoveBowl(laOperator* a, laEvent* e){
  61. Bowl* b=a->This?a->This->EndInstance:B.Bowls.pLast; if(!b) return LA_FINISHED;
  62. lstRemoveItem(&B.Bowls, b);
  63. memLeave(b);
  64. laRecordAndPushProp(0, "basket");laNotifyUsers("basket");
  65. laPrintDBInstInfo();
  66. return LA_FINISHED;
  67. }
  68. int INV_AddFruit(laOperator* a, laEvent* e){
  69. laListHandle* into=(a->This&&a->This->LastPs->p==pBowl)?&((Bowl*)a->This->EndInstance)->Fruits:0;
  70. if(!into) into=a->This?&((Fruit*)a->This->EndInstance)->Bundled:0;
  71. char* stype; int size=sizeof(Apple); int type=FRUIT_TYPE_APPLE;
  72. if(stype=strGetArgumentString(a->ExtraInstructionsP,"type")){
  73. if(!strcmp(stype,"pear")) {size=sizeof(Pear);type=FRUIT_TYPE_PEAR;}
  74. }
  75. Fruit* f=memAcquire(size); f->Type=type;
  76. if(into){ lstAppendItem(into, f); f->Parent=into; laNotifyUsers("basket"); /*laNotifyUsersPPPath(a->This,"base.bundled");*/ }
  77. else{ lstAppendItem(&B.Stuff, f); laNotifyUsers("basket");}
  78. laRecordAndPushProp(0, "basket");
  79. laPrintDBInstInfo();
  80. return LA_FINISHED;
  81. }
  82. void DestroyFruit(Fruit* f){
  83. Fruit* NextF;
  84. for(Fruit*fi=f->Bundled.pFirst;fi;fi=NextF){
  85. NextF=fi->Item.pNext;
  86. DestroyFruit(fi);
  87. }
  88. memLeave(f);
  89. }
  90. int INV_RemoveFruit(laOperator* a, laEvent* e){
  91. Fruit* f=a->This?a->This->EndInstance:0; laListHandle* l;
  92. if(f->Parent){ l=f->Parent;lstRemoveItem(l,f); laNotifyUsers("basket"); }
  93. else{ l=&B.Stuff; lstRemoveItem(l,f);laNotifyUsers("basket"); }
  94. DestroyFruit(f);
  95. laRecordAndPushProp(0, "basket");
  96. return LA_FINISHED;
  97. }
  98. int INV_MoveFruit(laOperator* a, laEvent* e){
  99. Fruit* f=a->This?a->This->EndInstance:0; laListHandle* l; int direction=0; char* dir;
  100. if(dir=strGetArgumentString(a->ExtraInstructionsP,"direction")){
  101. if(!strcmp(dir,"up")) {direction=1;}
  102. }
  103. if(f->Parent){ l=f->Parent; laNotifyUsers("basket"); /*laNotifyUsersPPPath(a->This,"base.parent.bundled");*/ }
  104. else{ l=&B.Stuff; laNotifyUsers("basket"); }
  105. if(direction) lstMoveUp(l,f);
  106. else lstMoveDown(l,f);
  107. laRecordAndPushProp(0, "basket");
  108. return LA_FINISHED;
  109. }
  110. int INV_PushBasketState(laOperator* a, laEvent* e){
  111. laRecordAndPushProp(0, "basket");
  112. logPrint("Pushed state: \"basket\"\n");
  113. return LA_FINISHED;
  114. }
  115. laPropContainer* FruitGetType(Fruit* f){
  116. if(f->Type==FRUIT_TYPE_APPLE) return pcApple;
  117. if(f->Type==FRUIT_TYPE_PEAR) return pcPear;
  118. return pcApple;
  119. }
  120. void* FruitGetBasket(void* none){
  121. return &B;
  122. }
  123. void* BowlGetFirst(void* none, void *UNUSED){
  124. return B.Bowls.pFirst;
  125. }
  126. void* FruitGetFirst(void* none, void *UNUSED){
  127. return B.Stuff.pFirst;
  128. }
  129. void FruitsPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  130. laColumn* c=laFirstColumn(uil);
  131. laColumn* cl;
  132. laSplitColumn(uil,c,0.5);
  133. cl=laLeftColumn(c,0);
  134. laShowItem(uil,c,0,"basket");
  135. laUiItem* r=laBeginRow(uil,c,0,0);
  136. strSafeSet(&laShowItem(uil,c,0,"FRUIT_add")->ExtraInstructions,"type=apple;icon=🍏;text=Add Apple;");
  137. strSafeSet(&laShowItem(uil,c,0,"FRUIT_add")->ExtraInstructions,"type=pear;icon=🍐;text=Add Pear;");
  138. laShowItem(uil,c,0,"BOWL_add");
  139. laEndRow(uil,r);
  140. laShowSeparator(uil,c);
  141. r=laBeginRow(uil,c,0,0);
  142. laShowItem(uil,c,0,"LA_undo");
  143. laShowItem(uil,c,0,"LA_redo");
  144. laShowItem(uil,c,0,"STATE_push");
  145. laEndRow(uil,r);
  146. }
  147. void UIBowl(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  148. laColumn* c=laFirstColumn(uil);
  149. laColumn* cl,*cr;
  150. laSplitColumn(uil,c,0.4);
  151. cl=laLeftColumn(c, 1);
  152. cr=laRightColumn(c,0);
  153. laUiItem*u;
  154. laUiItem* r=laBeginRow(uil,cr,1,0);
  155. laShowItem(uil,cr,This,"name")->Expand=1;
  156. laShowItem(uil,cr,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  157. laEndRow(uil,r);
  158. r=laOnConditionToggle(uil,cl,0,0,0,0,0);{
  159. laShowItem(uil,cr,This,"fruits");
  160. laUiItem* r1=laBeginRow(uil,cr,0,0);
  161. strSafeSet(&laShowItem(uil,cr,This,"add_fruit")->ExtraInstructions,"type=apple;icon=🍏;text=Add Apple;");
  162. strSafeSet(&laShowItem(uil,cr,This,"add_fruit")->ExtraInstructions,"type=pear;icon=🍐;text=Add Pear;");
  163. laEndRow(uil,r1);
  164. }laEndCondition(uil,r);
  165. }
  166. void UIApple(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  167. laColumn* c=laFirstColumn(uil);
  168. laColumn* cl,*cr;
  169. laSplitColumn(uil,c,0.4);
  170. cl=laLeftColumn(c, 1);
  171. cr=laRightColumn(c,0);
  172. laUiItem*u;
  173. laUiItem* r=laBeginRow(uil,cr,1,0);
  174. laShowLabel(uil,cr,"Apple",0,0)->Expand=1;
  175. laShowItem(uil,cr,This,"how_sweet")->Expand=1;
  176. laShowItem(uil,cr,This,"base.why_not")->Expand=1;
  177. laShowItem(uil,cr,This,"base.container")->Expand=1;
  178. laShowItem(uil,cr,This,"base.remove")->Flags|=LA_UI_FLAGS_ICON;
  179. u=laShowItem(uil,cr,This,"base.move"); strSafeSet(&u->ExtraInstructions,"direction=up;icon=🡹;");u->Flags|=LA_UI_FLAGS_ICON;
  180. u=laShowItem(uil,cr,This,"base.move"); strSafeSet(&u->ExtraInstructions,"direction=down;icon=🡻;");u->Flags|=LA_UI_FLAGS_ICON;
  181. laEndRow(uil,r);
  182. r=laOnConditionToggle(uil,cl,0,0,0,0,0);{
  183. laShowItem(uil,cr,This,"base.bundled");
  184. laUiItem* r1=laBeginRow(uil,cr,0,0);
  185. strSafeSet(&laShowItem(uil,cr,This,"base.add")->ExtraInstructions,"type=apple;icon=🍏;text=Add Apple;");
  186. strSafeSet(&laShowItem(uil,cr,This,"base.add")->ExtraInstructions,"type=pear;icon=🍐;text=Add Pear;");
  187. laEndRow(uil,r1);
  188. }laEndCondition(uil,r);
  189. }
  190. void UIPear(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  191. laColumn* c=laFirstColumn(uil);
  192. laColumn* cl,*cr;
  193. laSplitColumn(uil,c,0.4);
  194. cl=laLeftColumn(c, 1);
  195. cr=laRightColumn(c,0);
  196. laUiItem*u;
  197. laUiItem* r=laBeginRow(uil,cr,1,0);
  198. laShowLabel(uil,cr,"Pear",0,0)->Expand=1;
  199. laShowItem(uil,cr,This,"really")->Expand=1;
  200. laShowItem(uil,cr,This,"base.why_not")->Expand=1;
  201. laShowItem(uil,cr,This,"base.container")->Expand=1;
  202. laShowItem(uil,cr,This,"base.remove")->Flags|=LA_UI_FLAGS_ICON;
  203. u=laShowItem(uil,cr,This,"base.move"); strSafeSet(&u->ExtraInstructions,"direction=up;icon=🡹;");u->Flags|=LA_UI_FLAGS_ICON;
  204. u=laShowItem(uil,cr,This,"base.move"); strSafeSet(&u->ExtraInstructions,"direction=down;icon=🡻;");u->Flags|=LA_UI_FLAGS_ICON;
  205. laEndRow(uil,r);
  206. r=laOnConditionToggle(uil,cl,0,0,0,0,0);{
  207. laShowItem(uil,cr,This,"base.bundled");
  208. laUiItem* r1=laBeginRow(uil,cr,0,0);
  209. strSafeSet(&laShowItem(uil,cr,This,"base.add")->ExtraInstructions,"type=apple;icon=🍏;text=Add Apple;");
  210. strSafeSet(&laShowItem(uil,cr,This,"base.add")->ExtraInstructions,"type=pear;icon=🍐;text=Add Pear;");
  211. laEndRow(uil,r1);
  212. }laEndCondition(uil,r);
  213. }
  214. void UIBowlSimple(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  215. laColumn* c=laFirstColumn(uil);
  216. laShowItemFull(uil,c,This,"name",LA_WIDGET_STRING_PLAIN, 0 ,0,0);
  217. }
  218. void UIFruitSimple(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  219. laColumn* c=laFirstColumn(uil);
  220. laShowItem(uil,c,This,"type");
  221. }
  222. void ScenePanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  223. laColumn* c=laFirstColumn(uil);
  224. laUiItem* ui=laShowCanvas(uil,c,0,"tns.world",0,-1);
  225. laDefault3DViewOverlay(ui);
  226. }
  227. int RegisterEverything(){
  228. laRegisterUiTemplate("panel_fruit", "Fruits", FruitsPanel, 0, 0,0, 0,0,0);
  229. laCreateOperatorType("BOWL_add", "Add Bowl", "Add a bowl", 0,0,0,INV_AddBowl,0,'+',0);
  230. laCreateOperatorType("BOWL_remove", "Remove Bowl", "Remove a bowl", 0,0,0,INV_RemoveBowl,0,'-',0);
  231. laCreateOperatorType("FRUIT_add", "Add Fruit", "Add a fruit into the basket or bundle", 0,0,0,INV_AddFruit,0,'+',0);
  232. laCreateOperatorType("FRUIT_remove", "Remove Fruit", "Remove a fruit", 0,0,0,INV_RemoveFruit,0,'-',0);
  233. laCreateOperatorType("FRUIT_move", "Move Fruit", "Move a fruit", 0,0,0,INV_MoveFruit,0,'~',0);
  234. laCreateOperatorType("STATE_push", "Push State", "Push basket state", 0,0,0,INV_PushBasketState,0,0,0);
  235. laPropContainer* pc=laDefineRoot();
  236. laAddSubGroup(pc, "basket", "Basket", "The basket", "basket",0,0,0,-1,FruitGetBasket,0,0,0,0,0,0,LA_UDF_SINGLE|LA_UDF_LOCAL);
  237. laProp*p;
  238. pc=laAddPropertyContainer("bowl", "Bowl", "Some sort of a bowl", 0, UIBowl, sizeof(Bowl), 0, 0, 2);
  239. laAddStringProperty(pc,"name","Name","Name of the bowl",0,0,0,0,1,offsetof(Bowl,Name),0,0,0,0,LA_AS_IDENTIFIER);
  240. laAddSubGroup(pc, "fruits", "Fruits","Fruits","fruit",FruitGetType,0,0,-1,0,0,0,0,0,0,offsetof(Bowl,Fruits),0);
  241. laAddOperatorProperty(pc,"remove","Remove","Remove a bowl", "BOWL_remove", '-', 0);
  242. laAddOperatorProperty(pc,"add_fruit","Add","Add a fruit into the bowl", "FRUIT_add", '+', 0);
  243. pc=laAddPropertyContainer("basket", "Basket", "A basket of fruits", 0, 0, sizeof(Basket), 0, 0, 1|LA_UDF_LOCAL|LA_PROP_OTHER_ALLOC|LA_UDF_SINGLE);
  244. laAddIntProperty(pc,"example","Example","Example int",0,0,0,0,0,1,0,0,offsetof(Basket,example),0,0,0,0,0,0,0,0,0,0,0);
  245. laAddStringProperty(pc,"name","Name","Name of the basket",0,0,0,0,1,offsetof(Basket,name),0,0,0,0,LA_UDF_REFER);
  246. laAddSubGroup(pc, "stuff", "Stuff","Stuffs","fruit",FruitGetType,0,0,-1,0,0,0,0,0,0,offsetof(Basket,Stuff),0);
  247. pBowl = laAddSubGroup(pc, "bowls", "Bowls","Bowls","bowl",0,0,0,-1,0,0,0,0,0,0,offsetof(Basket,Bowls),0);
  248. pc=laAddPropertyContainer("fruit", "Fruit", "A fruit", 0, 0, sizeof(Fruit), 0, 0, 1);
  249. p=laAddEnumProperty(pc,"type","Type","Type",0,0,0,0,0,offsetof(Fruit,Type),0,0,0,0,0,0,0,0,0,LA_AS_IDENTIFIER|LA_READ_ONLY);
  250. laAddEnumItemAs(p,"APPLE","Apple","Apple", FRUIT_TYPE_APPLE, L'🍏');
  251. laAddEnumItemAs(p,"PEAR","Pear","Pear", FRUIT_TYPE_PEAR, L'🍐');
  252. pFruit = laAddSubGroup(pc, "bundled", "Bundled","Bundled","fruit",FruitGetType,0,0,-1,0,0,0,0,0,0,offsetof(Fruit,Bundled),0);
  253. laAddSubGroup(pc, "why_not", "Why Not","Why not try","fruit",0,LA_WIDGET_COLLECTION_SELECTOR,UIFruitSimple,offsetof(Fruit,WhyNot),FruitGetFirst,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  254. laAddSubGroup(pc, "parent", "Parent","Parent","any_pointer",0,0,0,offsetof(Fruit,Parent),0,0,0,0,0,0,0,LA_UDF_REFER);
  255. laAddSubGroup(pc, "container", "Container","Container of this fruit","bowl",0,LA_WIDGET_COLLECTION_SELECTOR,UIBowlSimple,offsetof(Fruit, Container),BowlGetFirst,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  256. laAddOperatorProperty(pc,"add","Add","Add a fruit into bundled", "FRUIT_add", '+', 0);
  257. laAddOperatorProperty(pc,"remove","Remove","Remove a fruit", "FRUIT_remove", '-', 0);
  258. laAddOperatorProperty(pc,"move","Move","Move a fruit", "FRUIT_move", '~', 0);
  259. pcApple=pc=laAddPropertyContainer("apple", "Apple", "An apple", 0, UIApple, sizeof(Apple), 0, 0, 1);
  260. laAddSubGroup(pc,"base", "Base","Base fruit","fruit",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_SINGLE|LA_UDF_LOCAL);
  261. laAddFloatProperty(pc,"how_sweet","How Sweet","How sweet",0,0,0,100,0,0.1,0,0,offsetof(Apple,HowSweet),0,0,0,0,0,0,0,0,0,0,LA_AS_IDENTIFIER);
  262. pcPear=pc=laAddPropertyContainer("pear", "Pear", "A pear", 0, UIPear, sizeof(Pear), 0, 0, 1);
  263. laAddSubGroup(pc, "base","Base","Base fruit","fruit",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_SINGLE|LA_UDF_LOCAL);
  264. p=laAddEnumProperty(pc,"really","Really","Really a pear?",LA_WIDGET_ENUM_CYCLE,0,0,0,0,offsetof(Pear,Really),0,0,0,0,0,0,0,0,0,LA_AS_IDENTIFIER);
  265. laAddEnumItemAs(p,"NAH","Nah","Nah I don't think so", 0, L'🤔');
  266. laAddEnumItemAs(p,"YEAH","Yeah","It's really a pear", 1, L'😄');
  267. laSaveProp("basket");
  268. tnsObject* s=tnsCreateRootObject("My Root");
  269. tnsObject* o=tnsCreateLight(s,"Sun",100,100,100,1,1);
  270. tnsVector3d target={0,0,0}, up={0,0,1};
  271. tnsLookAt(o,target,up);
  272. }
  273. int main(int argc, char *argv[]){
  274. laGetReady();
  275. RegisterEverything();
  276. laRefreshUDFRegistries();
  277. // Use this to save and load preference when exit and during start up
  278. // laEnsureUserPreferences();
  279. laAddRootDBInst("basket");
  280. laWindow* w = laDesignWindow(-1,-1,600,600);
  281. laLayout* l = laDesignLayout(w, "Fruit Layout");
  282. laBlock* b = l->FirstBlock;
  283. laPanel* p=laCreatePanel(b, "panel_fruit");
  284. laStartWindow(w);
  285. laMainLoop();
  286. }