*/}}

fruits.c 13 KB

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