amarullz

Minimal List - graph only

Dec 29th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.15 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. static LIBAROMA_CANVASP dc; /* display canvas */
  31.  
  32. /* init */
  33. byte init_aroma(){
  34.   /* init graph and font */
  35.   if (!libaroma_fb_init()) {
  36.     printf("cannot init framebuffer\n");
  37.     return 0;
  38.   }
  39.   if (!libaroma_font_init()) {
  40.     printf("Cannot init font\n");
  41.     libaroma_fb_release();
  42.     return 0;
  43.   }
  44.   /* load font */
  45.   libaroma_font(0,
  46.     libaroma_stream(
  47.       "file:///sdcard/Roboto-Regular.ttf"
  48.     )
  49.   );
  50.  
  51.   dc=libaroma_fb()->canvas;
  52.  
  53.   /* clean display */
  54.   libaroma_canvas_blank(dc);
  55.   return 1;
  56. }
  57. /* cleanup */
  58. void release_aroma(){
  59.   libaroma_font_release();
  60.   libaroma_fb_release();
  61. }
  62.  
  63. /* MINIMAL LIST */
  64. #define SCROLL_INDICATOR_WIDTH libaroma_dp(5)
  65.  
  66. typedef struct{
  67.   LIBAROMA_CANVASP cv;
  68.   LIBAROMA_CANVASP cva;
  69.   int n;  /* item count */
  70.   int w;  /* list width */
  71.   int ih; /* item height */
  72.   word bgcolor;
  73.   word selcolor;
  74.   word textcolor;
  75.   word textselcolor;
  76. } MINLIST;
  77.  
  78. MINLIST * list_create(int width, int itemheight, word bg, word sl, word tbg, word tsl){
  79.   MINLIST * list = (MINLIST *) calloc(sizeof(MINLIST),1);
  80.   list->w = width;
  81.   list->ih = itemheight;
  82.   list->n = 0;
  83.   list->bgcolor=bg;
  84.   list->selcolor=sl;
  85.   list->textcolor=tbg;
  86.   list->textselcolor=tsl;
  87.   return list;
  88. }
  89.  
  90. void list_free(MINLIST * list){
  91.   if (list->n>0){
  92.     libaroma_canvas_free(list->cv);
  93.     libaroma_canvas_free(list->cva);
  94.   }
  95.   free(list);
  96. }
  97.  
  98. void list_add(MINLIST * list,char * text){
  99.   int new_n  = list->n + 1;
  100.   int item_y = list->n * list->ih;
  101.   LIBAROMA_CANVASP cv = libaroma_canvas(list->w, list->ih*new_n);
  102.   LIBAROMA_CANVASP cva= libaroma_canvas(list->w, list->ih*new_n);
  103.  
  104.   /* draw previous */
  105.   if (list->n>0){
  106.     libaroma_draw(cv,list->cv,0,0,0);
  107.     libaroma_draw(cva,list->cva,0,0,0);
  108.     libaroma_canvas_free(list->cv);
  109.     libaroma_canvas_free(list->cva);
  110.   }
  111.  
  112.   /* draw bg */
  113.   libaroma_draw_rect(cv, 0, item_y, list->w, list->ih, list->bgcolor, 0xff);
  114.  
  115.   /* selected bg */
  116.   libaroma_draw_rect(cva, 0, item_y, list->w, list->ih, list->selcolor, 0xff);
  117.  
  118.   /* draw text */
  119.   LIBAROMA_TEXT txt = libaroma_text(
  120.     text,
  121.     list->textcolor, list->w,
  122.     LIBAROMA_FONT(0,4)|LIBAROMA_TEXT_CENTER,
  123.     100
  124.   );
  125.   if (txt){
  126.     int txty=item_y + ((list->ih>>1)-(libaroma_text_height(txt)>>1));
  127.     libaroma_text_draw(
  128.       cv, txt, 0, txty
  129.     );
  130.     libaroma_text_draw_color(
  131.       cva, txt, 0, txty, list->textselcolor
  132.     );
  133.     libaroma_text_free(txt);
  134.   }
  135.  
  136.   list->cv  = cv;
  137.   list->cva = cva;
  138.   list->n   = new_n;
  139. }
  140.  
  141. void list_show(MINLIST * list, int selectedid, int x, int y, int h){
  142.  
  143.   /* cleanup */
  144.   libaroma_draw_rect(dc, x, y, list->w, h, list->bgcolor, 0xff);
  145.  
  146.   if (list->n<1){
  147.     /* forget it */
  148.     goto syncit;
  149.   }
  150.   if ((selectedid<0)||(selectedid>=list->n)){
  151.     /* just blit non active canvas */
  152.     libaroma_draw_ex(
  153.       dc, list->cv, x, y, 0, 0,list->w, h, 0, 0xff
  154.     );
  155.     goto syncit;
  156.   }
  157.  
  158.   int sel_y  = selectedid * list->ih;
  159.  
  160.   if (list->cv->h<h){
  161.     /* dont need scroll */
  162.     libaroma_draw_ex(
  163.       dc, list->cv, x, y, 0, 0,list->w, h, 0, 0xff
  164.     );
  165.     libaroma_draw_ex(
  166.       dc, list->cva, x, y+sel_y,0,sel_y,list->w, list->ih, 0, 0xff
  167.     );
  168.     goto syncit;
  169.   }
  170.  
  171.  
  172.   int sel_cy = sel_y + (list->ih>>1);
  173.   int draw_y = (h>>1) - sel_cy;
  174.   draw_y = (draw_y<0)?(0-draw_y):0;
  175.   if (list->cv->h-draw_y<h){
  176.     draw_y = list->cv->h-h;
  177.   }
  178.   libaroma_draw_ex(
  179.     dc, list->cv, x, y, 0, draw_y,list->w, h, 0, 0xff
  180.   );
  181.   libaroma_draw_ex(
  182.     dc, list->cva, x, y+(sel_y-draw_y),0,sel_y,list->w,list->ih, 0, 0xff
  183.   );
  184.  
  185.   /* draw scroll indicator */
  186.   int si_h = (h * h) / list->cv->h;
  187.   int si_y = (selectedid+1) * (h-si_h);
  188.   if (si_y>0){
  189.     si_y /= list->n;
  190.     int si_w = SCROLL_INDICATOR_WIDTH;
  191.     int pad  = libaroma_dp(1);
  192.    
  193.     /* draw indicator */
  194.     libaroma_draw_rect(dc, x+list->w-si_w,
  195.       y+si_y, si_w, si_h, list->bgcolor, 0xff
  196.     );
  197.    
  198.     libaroma_draw_rect(dc, x+list->w-si_w+pad,
  199.       y+si_y+pad, si_w-pad*2, si_h-pad*2, list->textcolor, 0xff
  200.     );
  201.   }
  202.  
  203. syncit:
  204.   libaroma_sync();
  205. }
  206.  
  207.  
  208. int main(int argc, char **argv){
  209.  
  210.  
  211.   if (init_aroma()){
  212.     MINLIST * list = list_create(
  213.       dc->w-100,
  214.       libaroma_dp(48),
  215.       RGB(000000),
  216.       RGB(446688),
  217.       RGB(ffffff),
  218.       RGB(ffdd88)
  219.     );
  220.    
  221.     int i;
  222.     for (i=0;i<30;i++){
  223.       char txt[64];
  224.       snprintf(txt,64,"Item <b>Num %i</b> <u>%i</u>", i, (i+1) * 10);
  225.       list_add(list,txt);
  226.     }
  227.    
  228.     int list_height = libaroma_dp(48)*8; /* 8 items per page */
  229.    
  230.     /* draw frame */
  231.     libaroma_draw_rect(dc, 40, 290, dc->w-80, list_height+20, RGB(666666), 0xff);
  232.    
  233.     /* up to down */
  234.     for (i=-1;i<30;i++){
  235.       list_show(list, i, 50, 300, list_height);
  236.       usleep(300000);
  237.     }
  238.    
  239.     /* down to up */
  240.     for (i=29;i>=-1;i--){
  241.       list_show(list, i, 50, 300, list_height);
  242.       usleep(300000);
  243.     }
  244.    
  245.    
  246.     list_free(list);
  247.     release_aroma();
  248.   }
  249.  
  250.   return 0;
  251. } /* End of main */
  252.  
  253. #endif /* __libaroma_test_graphonly_c__ */
Advertisement
Add Comment
Please, Sign In to add comment