amarullz

UEFI Dialog with key event

Jan 13th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. /*
  2.  * MENU DIALOG UEFI
  3.  *
  4.  */
  5.  
  6. void button_draw(const char* text, int x, int y, int w, int h) {
  7.   word colorAccent = RGB(ff8800);
  8.   /* draw text */
  9.   LIBAROMA_TEXT textp = libaroma_text(
  10.     text,
  11.     colorAccent,
  12.     w - libaroma_dp(16),
  13.     LIBAROMA_FONT(1,4)|
  14.     LIBAROMA_TEXT_SINGLELINE|
  15.     LIBAROMA_TEXT_CENTER|
  16.     LIBAROMA_TEXT_FIXED_INDENT|
  17.     LIBAROMA_TEXT_FIXED_COLOR|
  18.     LIBAROMA_TEXT_NOHR,
  19.     100
  20.   );
  21.   int ty = y + (h>>1) - ((libaroma_text_height(textp)>>1));
  22.   libaroma_text_draw(dc,textp,x + libaroma_dp(8),ty);
  23.   libaroma_text_free(textp);
  24. }
  25. int button_width(const char* text) {
  26.   /* draw text */
  27.   LIBAROMA_TEXT textp = libaroma_text(
  28.     text,
  29.     0,
  30.     dc->w,
  31.     LIBAROMA_FONT(1,4)|
  32.     LIBAROMA_TEXT_SINGLELINE|
  33.     LIBAROMA_TEXT_CENTER|
  34.     LIBAROMA_TEXT_FIXED_INDENT|
  35.     LIBAROMA_TEXT_FIXED_COLOR|
  36.     LIBAROMA_TEXT_NOHR,
  37.     100
  38.   );
  39.   int w = libaroma_dp(8) + libaroma_text_width(textp) + libaroma_dp(8);
  40.   libaroma_text_free(textp);
  41.   return w;
  42. }
  43.  
  44. int MenuShowDialog(
  45.   const char * Title,
  46.   const char * Message,
  47.   const char * Button1,
  48.   const char * Button2
  49. ){
  50.   int Selection = 0;
  51.   int MaxSelection = (Button1?1:0) + (Button2?1:0);
  52.  
  53.  
  54.   /* COLORS */
  55.   word colorTextSecondary = RGB(cccccc);
  56.   word colorTextPrimary = RGB(ffffff);
  57.   word colorBackground = RGB(444444);
  58.   word colorSelection = RGB(aaaaaa);
  59.  
  60.   /*
  61.     if(Initialized==FALSE)
  62.       return -1;
  63.     UINT32 OldMode = mGop->Mode->Mode;
  64.     mGop->SetMode(mGop, gLKDisplay->GetPortraitMode());
  65.   */
  66.  
  67.   /* Mask Dark */
  68.   libaroma_draw_rect(
  69.     dc, 0, 0, dc->w, dc->h, RGB(000000), 0x7a
  70.   );
  71.  
  72.   /* Init Message & Title Text */
  73.   int dialog_w = dc->w-libaroma_dp(48);
  74.   LIBAROMA_TEXT messagetextp = libaroma_text(
  75.     Message,
  76.     colorTextSecondary,
  77.     dialog_w-libaroma_dp(48),
  78.     LIBAROMA_FONT(0,4)|
  79.     LIBAROMA_TEXT_LEFT|
  80.     LIBAROMA_TEXT_FIXED_INDENT|
  81.     LIBAROMA_TEXT_FIXED_COLOR|
  82.     LIBAROMA_TEXT_NOHR,
  83.     100
  84.   );
  85.   LIBAROMA_TEXT textp = libaroma_text(
  86.     Title,
  87.     colorTextPrimary,
  88.     dialog_w-libaroma_dp(48),
  89.     LIBAROMA_FONT(1,6)|
  90.     LIBAROMA_TEXT_LEFT|
  91.     LIBAROMA_TEXT_FIXED_INDENT|
  92.     LIBAROMA_TEXT_FIXED_COLOR|
  93.     LIBAROMA_TEXT_NOHR,
  94.     100
  95.   );
  96.  
  97.   int dialog_h =
  98.     libaroma_dp(120)+
  99.     libaroma_text_height(textp)+
  100.     libaroma_text_height(messagetextp);
  101.   int dialog_x = libaroma_dp(24);
  102.   int dialog_y = (dc->h>>1)-(dialog_h>>1);
  103.  
  104.   /* draw fake shadow */
  105.   int z;
  106.   int shadow_sz=libaroma_dp(2);
  107.   byte shadow_opa = (byte) (0x60 / shadow_sz);
  108.   for (z=1;z<shadow_sz;z++){
  109.     int wp=z*2;
  110.     libaroma_gradient_ex(dc,
  111.       dialog_x-z, dialog_y+(z>>1),
  112.       dialog_w+wp, dialog_h+wp,
  113.       0,0,
  114.       libaroma_dp(4),
  115.       0x1111,
  116.       shadow_opa, shadow_opa
  117.     );
  118.   }
  119.  
  120.   /* draw dialog */
  121.   libaroma_gradient(dc,
  122.     dialog_x, dialog_y,
  123.     dialog_w, dialog_h,
  124.     colorBackground,colorBackground,
  125.     libaroma_dp(4), /* rounded 4dp */
  126.     0x1111 /* all corners */
  127.   );
  128.  
  129.   /* draw texts */
  130.   libaroma_text_draw(
  131.     dc,
  132.     textp,
  133.     dialog_x+libaroma_dp(24),
  134.     dialog_y+libaroma_dp(24)
  135.   );
  136.   libaroma_text_free(textp);
  137.  
  138.   /* draw text */
  139.   libaroma_text_draw(
  140.     dc,
  141.     messagetextp,
  142.     dialog_x+libaroma_dp(24),
  143.     dialog_y+libaroma_dp(24)+libaroma_text_height(textp) + libaroma_dp(20)
  144.   );
  145.   libaroma_text_free(messagetextp);
  146.  
  147.  
  148.   /* MENU SELECTION */
  149.   UINTN           WaitIndex;
  150.   EFI_INPUT_KEY   Key;
  151.   while(TRUE) {
  152.     int button_y = dialog_y+dialog_h-libaroma_dp(52);
  153.    
  154.     /* clean button area */
  155.     libaroma_draw_rect(dc, dialog_x, button_y, dialog_w, libaroma_dp(36), colorBackground, 0xff);
  156.      
  157.     if(Button1) {
  158.       /* button1 */
  159.       int button_w = button_width(Button1);
  160.       int button_x = dialog_x+dialog_w-button_w-libaroma_dp(16);
  161.      
  162.       if(Selection==0) {
  163.         libaroma_gradient_ex(dc,
  164.           button_x,button_y,
  165.           button_w, libaroma_dp(36),
  166.           colorSelection,colorSelection,
  167.           libaroma_dp(2), /* rounded 2dp */
  168.           0x1111, /* all corners */
  169.           0x40, 0x40 /* start & end alpha */
  170.         );
  171.       }
  172.       button_draw(Button1, button_x, button_y-libaroma_dp(2), button_w, libaroma_dp(36));
  173.  
  174.       /* button2 */
  175.       if(Button2) {
  176.         button_w = button_width(Button2);
  177.         button_x -= (libaroma_dp(8)+button_w);
  178.         if(Selection==1) {
  179.           libaroma_gradient_ex(dc,
  180.             button_x,button_y,
  181.             button_w, libaroma_dp(36),
  182.             colorSelection,colorSelection,
  183.             libaroma_dp(2), /* rounded 2dp */
  184.             0x1111, /* all corners */
  185.             0x40, 0x40 /* start & end alpha */
  186.           );
  187.         }
  188.         button_draw(Button2, button_x, button_y-libaroma_dp(2), button_w, libaroma_dp(36));
  189.       }
  190.     }
  191.     libaroma_sync();
  192.  
  193.     EFI_STATUS Status = gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &WaitIndex);
  194.     ASSERT_EFI_ERROR (Status);
  195.  
  196.     Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
  197.  
  198.     if(Key.ScanCode==SCAN_NULL) {
  199.       switch(Key.UnicodeChar) {
  200.         case CHAR_CARRIAGE_RETURN:
  201.           RenderActiveMenu();
  202.           mGop->SetMode(mGop, OldMode);
  203.           return Selection;
  204.       }
  205.     }
  206.     else {
  207.       switch(Key.ScanCode) {
  208.         case SCAN_UP:
  209.           if(Selection>0)
  210.             Selection--;
  211.           else Selection = MaxSelection-1;
  212.           break;
  213.         case SCAN_DOWN:
  214.           if(Selection+1<MaxSelection)
  215.             Selection++;
  216.           else Selection = 0;
  217.           break;
  218.       }
  219.     }
  220.   }
  221.   return -1;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment