amarullz

Application Programming Idea #1

Jul 23rd, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------- otherlayout.nut --------------------------------
  2. class otherLayout extends Layout {
  3.   button1 = null;
  4.   button2 = null;
  5.   constructor(layout_data){
  6.     base.constructor(layout_data);
  7.     button1 = findControlId("button1");
  8.     button1.onclick = buttonClick;
  9.   }
  10.   function buttonClick(event){
  11.     if (event.id=="button1"){
  12.       if (event.type=="clicked"){
  13.         close(); /* will back to previous layout */
  14.       }
  15.     }
  16.   }
  17. }
  18.  
  19.  
  20.  
  21. ------------------------------- mainlayout.nut ---------------------------------
  22. include("otherlayout.nut");
  23. class mainMenu extends Layout {
  24.   button1 = null;
  25.   button2 = null;
  26.   constructor(layout_data){
  27.     base.constructor(layout_data);
  28.     button1 = findControlId("button1");
  29.     button1.onclick = buttonClick;
  30.     button2 = findControlId("button2");
  31.     button2.onclick = buttonClick;
  32.   }
  33.   function onShow(){ }
  34.   function onClose(){ }
  35.   function buttonClick(event){
  36.     if (event.id=="button1"){
  37.       if (event.type=="clicked"){
  38.         getApplication().setCurrentLayout(otherLayout("@layout1"));
  39.       }
  40.     }
  41.     else if (event.id=="button2"){
  42.       if (event.type=="holded"){
  43.         Application.alert("Button 2 Holded");
  44.         close();
  45.         /* will close layout and exit
  46.           or can call getApplication().exit();
  47.         */
  48.       }
  49.       else if (event.type=="clicked"){
  50.         Application.alert("Button 2 Clicked");
  51.       }
  52.     }
  53.   }
  54. }
  55.  
  56.  
  57.  
  58. ----------------------------------- app.nut ------------------------------------
  59. include("mainlayout.nut");
  60. class myApp extends Application{
  61.   constructor(){
  62.     base.constructor();
  63.     setNavigationItem([
  64.       { id=1,
  65.         type="image",
  66.         image="res:///img/paralax.jpg",
  67.         paralax=true,
  68.         clickable=true
  69.       },
  70.       { id="exit", type="menu", title="Exit", icon="@exit" }
  71.     ]);
  72.    
  73.     local mainMenuLayout = mainMenu("@layout1");
  74.    
  75.     setCurrentLayout(mainMenuLayout);
  76.   }
  77.  
  78.   function navigationItemClick(id,holded){
  79.     if (id=="exit"){
  80.       exit(); /* force exit application */
  81.     }
  82.   }
  83. }
  84.  
  85. local app = myApp();
  86. app.start();
  87.  
  88.  
  89.  
  90. ---------------------------- app.nut (other style) -----------------------------
  91. include("mainlayout.nut");
  92. local app = Application();
  93. app.setNavigationItem([
  94.   { id=1,
  95.     type="image",
  96.     image="res:///img/paralax.jpg",
  97.     paralax=true,
  98.     clickable=true
  99.   },
  100.   { id="exit", type="menu", title="Exit", icon="@exit" }
  101. ]);
  102. app.setCurrentLayout(mainMenu("@layout1"));
  103. app::navigationItemClick=function(id,holded){
  104.   if (id=="exit"){
  105.     exit(); /* force exit application */
  106.   }
  107. };
  108. app.start();
  109.  
  110.  
  111. --------------------------------------------------------------------------------
  112. Layout can also directly pass json/table variable like this:
  113.  
  114.   /* table */
  115.   mainMenu({
  116.     title = "Main Menu",
  117.     primaryColor = "#446688",
  118.     ...
  119.   });
  120.  
  121.  
  122.   /* json */
  123.   mainMenu({
  124.     "title" : "Main Menu",
  125.     "primaryColor" : "#446688",
  126.     ...
  127.   });
  128.  
  129. --------------------------------- layout1.json ---------------------------------
  130. /*
  131.   icon value :
  132.     -> # = system wide icons ( #drawer, #backarrow, #add, #delete, #check )
  133.     -> @ = resource icon ( @XXX = res:///icon/XXdpi/XXX.png ),
  134.     -> res:///
  135.     -> file:///
  136.     -> zip:///
  137.     -> ... any acceptable uri
  138. */
  139. {
  140.   title : "Main Menu",  /* appbar title */
  141.   primaryColor : "#446688",
  142.   primaryTextColor : "#ffffff", /* optional */
  143.   primaryIconColor : "#000000", /* optional */
  144.   icon  : "#drawer",
  145.   tools : [ /* appbar tools */
  146.     {
  147.       icon  : "res:///icon/hdpi/search.png",
  148.       id    : "search",
  149.       title : "Search"
  150.     },
  151.     {
  152.       icon  : "@plus",
  153.       id    : "add",
  154.       title : "Add Menu"
  155.     },
  156.     {
  157.       switch  : true, /* appbar switch tools */
  158.       checked : false,
  159.       id      : "editmode",
  160.       title   : "Edit Mode"
  161.     }
  162.   ],
  163.   menu  : { /* appbar mainmenu */
  164.     /* [ : ]
  165.      *   Help
  166.      *   About
  167.      *   --------------
  168.      *   Exit
  169.      */
  170.     id  : "mainmenu",
  171.     items : [
  172.       "Help",
  173.       "About",
  174.       "-",
  175.       "Exit"
  176.     ]
  177.   },
  178.   control : [
  179.     {
  180.       type : "button",
  181.       id   : "button1",
  182.       text : "Button 1",
  183.       pos  : [ 0, 0, "50%", 24 ] /* x, y, w, h */
  184.     },
  185.     {
  186.       type : "button",
  187.       id   : "button2",
  188.       text : "Button 2",
  189.       pos  : [ "50%", 0, "50%", 24 ] /* x, y, w, h */
  190.     },
  191.     {
  192.       type : "list",
  193.       id   : "list1",
  194.       pos  : [ 0, 24, "100%", "100%" ],
  195.       items : [
  196.         /* Example items:
  197.         {
  198.           id   : 1,
  199.           type : "menu",
  200.           icon : "@menu",
  201.           title: "Test Item",
  202.           secondaryText : "This is secondary text"
  203.         }
  204.         */
  205.       ]
  206.     }
  207.   ]
  208. }
Advertisement
Add Comment
Please, Sign In to add comment