amarullz

AROMA Bootloader - Material #updated

Dec 30th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.21 KB | None | 0 0
  1. /********************************************************************[libaroma]*
  2.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *______________________________________________________________________________
  16.  *
  17.  * Filename    : libaroma_test_graphonly.c
  18.  * Description : libaroma test file
  19.  *
  20.  * + This is part of libaroma, an embedded ui toolkit.
  21.  * + 04/02/15 - Author(s): Ahmad Amarullah
  22.  *
  23.  */
  24. #ifndef __libaroma_test_graphonly_c__
  25. #define __libaroma_test_graphonly_c__
  26.  
  27. /* libaroma header */
  28. #include <aroma.h>
  29.  
  30. /*
  31.  DUMMY PNG
  32. */
  33. #include <png.h>
  34. #include <pngstruct.h>
  35. void PNGAPI png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr){}
  36. void png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp){}
  37.  
  38.  
  39. static LIBAROMA_CANVASP dc; /* display canvas */
  40.  
  41. /* init */
  42. byte init_aroma(){
  43.   /* init graph and font */
  44.   if (!libaroma_fb_init()) {
  45.     printf("cannot init framebuffer\n");
  46.     return 0;
  47.   }
  48.   if (!libaroma_font_init()) {
  49.     printf("Cannot init font\n");
  50.     libaroma_fb_release();
  51.     return 0;
  52.   }
  53.   /* load font */
  54.   libaroma_font(0,
  55.     libaroma_stream(
  56.       "file:///sdcard/Roboto-Regular.ttf"
  57.     )
  58.   );
  59.  
  60.   dc=libaroma_fb()->canvas;
  61.  
  62.   /* clean display */
  63.   libaroma_canvas_blank(dc);
  64.   return 1;
  65. }
  66. /* cleanup */
  67. void release_aroma(){
  68.   libaroma_font_release();
  69.   libaroma_fb_release();
  70. }
  71.  
  72. /**********************************************************************************
  73.  **
  74.  ** MINIMAL LIST
  75.  **
  76.  **/
  77. #define SCROLL_INDICATOR_WIDTH libaroma_dp(5)
  78. typedef struct{
  79.   LIBAROMA_CANVASP cv;
  80.   LIBAROMA_CANVASP cva;
  81.   int n;  /* item count */
  82.   int w;  /* list width */
  83.   int ih; /* item height */
  84.   word bgcolor;
  85.   word selcolor;
  86.   word textcolor;
  87.   word textselcolor;
  88. } MINLIST;
  89. MINLIST * list_create(int width, int itemheight, word bg, word sl, word tbg, word tsl){
  90.   MINLIST * list = (MINLIST *) calloc(sizeof(MINLIST),1);
  91.   list->w = width;
  92.   list->ih = itemheight;
  93.   list->n = 0;
  94.   list->bgcolor=bg;
  95.   list->selcolor=sl;
  96.   list->textcolor=tbg;
  97.   list->textselcolor=tsl;
  98.   return list;
  99. }
  100. void list_free(MINLIST * list){
  101.   if (list->n>0){
  102.     libaroma_canvas_free(list->cv);
  103.     libaroma_canvas_free(list->cva);
  104.   }
  105.   free(list);
  106. }
  107.  
  108. #define LIST_ADD_MASK_ICON_COLOR        0x1 /* mask icon with text color */
  109. #define LIST_ADD_WITH_SEPARATOR         0x2 /* add separator below item */
  110. #define LIST_ADD_SEPARATOR_ALIGN_TEXT   0x4 /* align the separator line with text position */
  111. void list_add(MINLIST * list,char * icon, char * title, char * subtitle, byte flags){
  112.   int new_n  = list->n + 1;
  113.   int item_y = list->n * list->ih;
  114.   LIBAROMA_CANVASP cv = libaroma_canvas(list->w, list->ih*new_n);
  115.   LIBAROMA_CANVASP cva= libaroma_canvas(list->w, list->ih*new_n);
  116.  
  117.   /* draw previous */
  118.   if (list->n>0){
  119.     libaroma_draw(cv,list->cv,0,0,0);
  120.     libaroma_draw(cva,list->cva,0,0,0);
  121.     libaroma_canvas_free(list->cv);
  122.     libaroma_canvas_free(list->cva);
  123.   }
  124.  
  125.   /* draw bg */
  126.   libaroma_draw_rect(cv, 0, item_y, list->w, list->ih, list->bgcolor, 0xff);
  127.  
  128.   /* selected bg */
  129.   libaroma_draw_rect(cva, 0, item_y, list->w, list->ih, list->bgcolor, 0xff);
  130.   libaroma_draw_rect(cva, 0, item_y+libaroma_dp(1), list->w, list->ih-libaroma_dp(1), list->selcolor, 0xff);
  131.  
  132.   char text[256];
  133.   if (subtitle!=NULL){
  134.     word scolor = libaroma_alpha(list->bgcolor,list->textcolor,0x66);
  135.     snprintf(text,256,"<b>%s</b>\n<#%02X%02X%02X><size=3>%s</size></#>",title?title:"",
  136.       libaroma_color_r(scolor),
  137.       libaroma_color_g(scolor),
  138.       libaroma_color_b(scolor),
  139.       subtitle);
  140.   }
  141.   else{
  142.     snprintf(text,256,"<b>%s</b>",title?title:"");
  143.   }
  144.  
  145.   /* draw text */
  146.   int left_pad=libaroma_dp(72);
  147.   int right_pad=libaroma_dp(16);
  148.   LIBAROMA_TEXT txt = libaroma_text(
  149.     text,
  150.     list->textcolor, list->w-(left_pad+right_pad),
  151.     LIBAROMA_FONT(0,4),
  152.     100
  153.   );
  154.   if (txt){
  155.     int txty=item_y + ((list->ih>>1)-((libaroma_text_height(txt)>>1))-libaroma_dp(2));
  156.     libaroma_text_draw(
  157.       cv, txt, left_pad, txty
  158.     );
  159.     libaroma_text_draw_color(
  160.       cva, txt, left_pad, txty, list->textselcolor
  161.     );
  162.     libaroma_text_free(txt);
  163.   }
  164.  
  165.   if (icon!=NULL){
  166.     LIBAROMA_CANVASP ico =libaroma_image_uri(icon);
  167.     if (ico){
  168.       int dpsz=libaroma_dp(40);
  169.       int icoy=item_y + ((list->ih>>1) - (dpsz>>1));
  170.       int icox=libaroma_dp(16);
  171.       byte ismask=(LIST_ADD_MASK_ICON_COLOR&flags)?1:0;
  172.        
  173.       if (ismask){
  174.         libaroma_canvas_fillcolor(ico,libaroma_alpha(list->bgcolor,list->textcolor,0xcc));
  175.       }
  176.       libaroma_draw_scale_smooth(
  177.         cv, ico,
  178.         icox,icoy,
  179.         dpsz, dpsz,
  180.         0, 0, ico->w, ico->h
  181.       );
  182.       if (ismask){
  183.         libaroma_canvas_fillcolor(ico,libaroma_alpha(list->selcolor,list->textselcolor,0xcc));
  184.       }
  185.       libaroma_draw_scale_smooth(
  186.         cva, ico,
  187.         icox,icoy,
  188.         dpsz, dpsz,
  189.         0, 0, ico->w, ico->h
  190.       );
  191.       libaroma_canvas_free(ico);
  192.     }
  193.   }
  194.  
  195.   if (LIST_ADD_WITH_SEPARATOR&flags){
  196.     byte is_dark = libaroma_color_isdark(list->bgcolor);
  197.     int sepxp=0;
  198.     if (flags&LIST_ADD_SEPARATOR_ALIGN_TEXT){
  199.       sepxp=libaroma_dp(72);
  200.     }
  201.     libaroma_draw_rect(
  202.       cv,
  203.       sepxp,
  204.       item_y-libaroma_dp(1),
  205.       cv->w-sepxp,
  206.       libaroma_dp(1),
  207.       is_dark?RGB(555555):RGB(dddddd),
  208.       0xff
  209.     );
  210.   }
  211.  
  212.   list->cv  = cv;
  213.   list->cva = cva;
  214.   list->n   = new_n;
  215. }
  216. void list_show(MINLIST * list, int selectedid, int x, int y, int h){
  217.   /* cleanup */
  218.   libaroma_draw_rect(dc, x, y, list->w, h, list->bgcolor, 0xff);
  219.   if (list->n<1){
  220.     /* forget it */
  221.     goto syncit;
  222.   }
  223.   if ((selectedid<0)||(selectedid>=list->n)){
  224.     /* just blit non active canvas */
  225.     libaroma_draw_ex(
  226.       dc, list->cv, x, y, 0, 0,list->w, h, 0, 0xff
  227.     );
  228.     goto syncit;
  229.   }
  230.  
  231.   int sel_y  = selectedid * list->ih;
  232.   if (list->cv->h<h){
  233.     /* dont need scroll */
  234.     libaroma_draw_ex(
  235.       dc, list->cv, x, y, 0, 0,list->w, h, 0, 0xff
  236.     );
  237.     libaroma_draw_ex(
  238.       dc, list->cva, x, y+sel_y,0,sel_y,list->w, list->ih, 0, 0xff
  239.     );
  240.     goto syncit;
  241.   }
  242.  
  243.   int sel_cy = sel_y + (list->ih>>1);
  244.   int draw_y = (h>>1) - sel_cy;
  245.   draw_y = (draw_y<0)?(0-draw_y):0;
  246.   if (list->cv->h-draw_y<h){
  247.     draw_y = list->cv->h-h;
  248.   }
  249.   libaroma_draw_ex(
  250.     dc, list->cv, x, y, 0, draw_y,list->w, h, 0, 0xff
  251.   );
  252.   libaroma_draw_ex(
  253.     dc, list->cva, x, y+(sel_y-draw_y),0,sel_y,list->w,list->ih, 0, 0xff
  254.   );
  255.   /* draw scroll indicator */
  256.   int si_h = (h * h) / list->cv->h;
  257.   int si_y = draw_y * h;
  258.   if (si_y>0){
  259.     si_y /= list->cv->h;
  260.   }
  261.   int si_w = SCROLL_INDICATOR_WIDTH;
  262.   int pad  = libaroma_dp(1);
  263.   byte is_dark = libaroma_color_isdark(list->bgcolor);
  264.   word indicator_color = is_dark?RGB(cccccc):RGB(666666);
  265.   /* draw indicator */
  266.   libaroma_draw_rect(dc, x+list->w-si_w,  y+si_y, si_w-libaroma_dp(2),
  267.     si_h, indicator_color, 120);
  268.  
  269. syncit:
  270.   /* draw shadow ;) */
  271.   libaroma_gradient_ex1(dc, x, y, list->w,libaroma_dp(5),0,0,0,0,80,0,2);
  272.   libaroma_sync();
  273. }
  274. /**
  275.  **
  276.  ** END OF MINIMAL LIST
  277.  **
  278.  **********************************************************************************/
  279. #define APPBAR_FLAG_ICON_BACK   1 /* back arrow */
  280. #define APPBAR_FLAG_ICON_DRAWER 2 /* drawer */
  281. #define APPBAR_FLAG_SELECTED    4 /* selected */
  282. #define APPBAR_FLAG_WIDEGAP     8 /* align text with text in listbox */
  283. void appbar_draw(
  284.   char * text,
  285.   word bgcolor,
  286.   word textcolor,
  287.   int y,
  288.   int h,
  289.   byte flags
  290. ){
  291.   /* draw appbar */
  292.   libaroma_draw_rect(
  293.     dc, 0, y, dc->w, h, bgcolor, 0xff
  294.   );
  295.   int txt_x = libaroma_dp(16);
  296.  
  297.   if ((flags&APPBAR_FLAG_ICON_BACK)||(flags&APPBAR_FLAG_ICON_DRAWER)){
  298.     if (flags&APPBAR_FLAG_ICON_BACK){
  299.       libaroma_art_arrowdrawer(
  300.         dc,1,0,
  301.         txt_x,
  302.         y+libaroma_dp(16),
  303.         libaroma_dp(24),
  304.         textcolor,
  305.         0xff, 0, 0.5
  306.       );
  307.     }
  308.     else{
  309.       libaroma_art_arrowdrawer(
  310.         dc,1,1,
  311.         txt_x,
  312.         y+libaroma_dp(16),
  313.         libaroma_dp(24),
  314.         textcolor,
  315.         0xff, 0, 0.5
  316.       );
  317.     }
  318.     txt_x = libaroma_dp((flags&APPBAR_FLAG_WIDEGAP)?72:60);
  319.      
  320.     if (flags&APPBAR_FLAG_SELECTED){
  321.       int sel_w=txt_x+libaroma_dp(16);
  322.       LIBAROMA_CANVASP carea=libaroma_canvas_area(dc,0,y,sel_w,h);
  323.       if (carea){
  324.         int center_xy=(h>>1);
  325.         libaroma_draw_circle(
  326.           carea, textcolor, center_xy-libaroma_dp(16), center_xy, sel_w+libaroma_dp(20), 0x40
  327.         );
  328.         libaroma_canvas_free(carea);
  329.       }
  330.     }
  331.   }
  332.  
  333.   LIBAROMA_TEXT txt = libaroma_text(
  334.     text,
  335.     textcolor,
  336.     dc->w-txt_x,
  337.     LIBAROMA_FONT(0,6)|
  338.     LIBAROMA_TEXT_SINGLELINE|
  339.     LIBAROMA_TEXT_LEFT|
  340.     LIBAROMA_TEXT_BOLD|
  341.     LIBAROMA_TEXT_FIXED_INDENT|
  342.     LIBAROMA_TEXT_FIXED_COLOR|
  343.     LIBAROMA_TEXT_NOHR,
  344.     100
  345.   );
  346.   if (txt){
  347.     int txty=y + ((h>>1)-((libaroma_text_height(txt)>>1))-libaroma_dp(2));
  348.     libaroma_text_draw(
  349.       dc, txt, txt_x, txty
  350.     );
  351.     libaroma_text_free(txt);
  352.   }
  353.   libaroma_sync();
  354. }
  355.  
  356. int main(int argc, char **argv){
  357.   if (init_aroma()){
  358.     int i;
  359.     int statusbar_height = libaroma_dp(24);
  360.     int appbar_height    = libaroma_dp(56);
  361.     int list_y           = statusbar_height + appbar_height;
  362.     int list_height      = dc->h-list_y;
  363.    
  364.     /*
  365.      * DRAW STATUSBAR
  366.      */
  367.     libaroma_draw_rect(
  368.       dc, 0, 0, dc->w, statusbar_height, RGB(335577), 0xff
  369.     );
  370.     libaroma_draw_text(
  371.         dc,
  372.         "AROMA Bootloader",
  373.         0, libaroma_dp(2) ,RGB(ffffff), dc->w,
  374.         LIBAROMA_FONT(0,3)|LIBAROMA_TEXT_CENTER,
  375.         100
  376.     );
  377.    
  378.     /*
  379.      * Create List
  380.      */
  381.     MINLIST * list = list_create(
  382.       dc->w,
  383.       libaroma_dp(72),
  384.       RGB(ffffff),
  385.       RGB(cccccc),
  386.       RGB(000000),
  387.       RGB(000000)
  388.     );
  389.    
  390.     /*
  391.      * Add List Items
  392.      */
  393.     for (i=0;i<30;i++){
  394.       char title[64];
  395.       char subtitle[64];
  396.       snprintf(title,64,"Item Number %i", i+1);
  397.       snprintf(subtitle,64,"This is subtitle text number %i", i+1);
  398.       list_add(
  399.         list,
  400.         "file:///sdcard/gesture.png",
  401.         title,
  402.         subtitle,
  403.         LIST_ADD_MASK_ICON_COLOR|LIST_ADD_WITH_SEPARATOR
  404.         /*|LIST_ADD_SEPARATOR_ALIGN_TEXT*/
  405.       );
  406.     }
  407.    
  408.     /* set appbar */
  409.     appbar_draw(
  410.       "Please Select OS",
  411.       RGB(446688),
  412.       RGB(ffffff),
  413.       statusbar_height,
  414.       appbar_height,
  415.       APPBAR_FLAG_ICON_DRAWER|APPBAR_FLAG_SELECTED
  416.     );
  417.    
  418.     /* show & change list selection */
  419.     for (i=-1;i<30;i++){
  420.       list_show(list, i, 0, list_y, list_height);
  421.       usleep(200000);
  422.     }
  423.    
  424.     /* change appbar text */
  425.     for (i=0;i<20;i++){
  426.       char txapp[32];
  427.       snprintf(txapp,32,"App Title #%i",i+1);
  428.      
  429.       byte sflags=0;
  430.       if (i<=10){
  431.         if ((i%2)==0){
  432.           sflags|=APPBAR_FLAG_ICON_BACK;
  433.         }
  434.         else{
  435.           sflags|=APPBAR_FLAG_ICON_DRAWER;
  436.         }
  437.         if ((i%3)==0){
  438.           sflags|=APPBAR_FLAG_SELECTED;
  439.         }
  440.       }
  441.      
  442.       appbar_draw(
  443.         txapp,
  444.         RGB(446688),
  445.         RGB(ffffff),
  446.         statusbar_height,
  447.         appbar_height,
  448.         sflags
  449.       );
  450.       /*
  451.         * app bar selected every i%3 = 0
  452.         * app bar icon switched every i%2 = 0
  453.         * app bar only show icon if i <= 10
  454.       */
  455.       usleep(300000);
  456.     }
  457.    
  458.     /* down to up */
  459.     /*
  460.     for (i=29;i>=-1;i--){
  461.       list_show(list, i, 0, list_y, list_height);
  462.       usleep(300000);
  463.     }
  464.     */
  465.    
  466.     list_free(list);
  467.     release_aroma();
  468.   }
  469.  
  470.   return 0;
  471. } /* End of main */
  472.  
  473. #endif /* __libaroma_test_graphonly_c__ */
Advertisement
Add Comment
Please, Sign In to add comment