Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Portions of this file are copyright Rebirth contributors and licensed as
  3.  * described in COPYING.txt.
  4.  * Portions of this file are copyright Parallax Software and licensed
  5.  * according to the Parallax license below.
  6.  * See COPYING.txt for license details.
  7.  
  8. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  9. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  10. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  11. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  12. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  13. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  14. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  15. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  16. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  17. COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  18. */
  19.  
  20.  
  21. #include <stdlib.h>
  22.  
  23. #include "u_mem.h"
  24. #include "maths.h"
  25. #include "pstypes.h"
  26. #include "gr.h"
  27. #include "event.h"
  28. #include "mouse.h"
  29. #include "ui.h"
  30.  
  31. #include "dxxsconf.h"
  32. #include "dsx-ns.h"
  33. #include <memory>
  34.  
  35. namespace dcx {
  36.  
  37. #define MENU_BORDER 2
  38. #define MENU_VERT_SPACING 2
  39.  
  40. namespace {
  41.  
  42. struct menu
  43. {
  44.         std::unique_ptr<std::unique_ptr<UI_GADGET_BUTTON>[]> button_g;
  45.         std::unique_ptr<const char *[]> button;
  46.         int *choice;
  47.         int num_buttons;
  48. };
  49.  
  50. }
  51.  
  52. static window_event_result menu_handler(UI_DIALOG *,const d_event &event, menu *m)
  53. {
  54.         for (int i=0; i<m->num_buttons; i++ )
  55.         {
  56.                 if (GADGET_PRESSED(m->button_g[i].get()))
  57.                 {
  58.                         *(m->choice) = i+1;
  59.                         return window_event_result::handled;
  60.                 }
  61.         }
  62.        
  63.         if ( (*(m->choice)==0) && B1_JUST_RELEASED )
  64.         {
  65.                 *(m->choice) = -1;
  66.                 return window_event_result::handled;
  67.         }
  68.        
  69.         return window_event_result::ignored;
  70. }
  71.  
  72. int MenuX( int x, int y, int NumButtons, const char *const text[] )
  73. {
  74.         UI_DIALOG * dlg;
  75.         int button_width, button_height;
  76.         int w, h;
  77.         int choice;
  78.  
  79.         auto m = std::make_unique<menu>();
  80.         m->num_buttons = NumButtons;
  81.         m->button_g = std::make_unique<std::unique_ptr<UI_GADGET_BUTTON>[]>(NumButtons);
  82.         m->button = std::make_unique<const char *[]>(NumButtons);
  83.         m->choice = &choice;
  84.  
  85.         button_width = button_height = 0;
  86.  
  87.         for (int i=0; i<NumButtons; i++ )
  88.         {
  89.                 m->button[i] = text[i];
  90.  
  91.                 int width, height;
  92.                 ui_get_button_size(*grd_curcanv->cv_font, m->button[i], width, height);
  93.  
  94.                 if ( width > button_width ) button_width = width;
  95.                 if ( height > button_height ) button_height = height;
  96.         }
  97.  
  98.         unsigned width, height;
  99.         width = button_width + 2*(MENU_BORDER+3);
  100.  
  101.         height = (button_height*NumButtons) + (MENU_VERT_SPACING*(NumButtons-1)) ;
  102.         height += (MENU_BORDER+3) * 2;
  103.  
  104.         w = grd_curscreen->get_screen_width();
  105.         h = grd_curscreen->get_screen_height();
  106.  
  107.         {
  108.                 int mx, my, mz;
  109.                
  110.                 mouse_get_pos(&mx, &my, &mz);
  111.                 if ( x == -1 ) x = mx - width/2;
  112.                 if ( y == -1 ) y = my;
  113.         }
  114.  
  115.         if (x < 0 ) {
  116.                 x = 0;
  117.         }
  118.  
  119.         if ( (x+width-1) >= w ) {
  120.                 x = w - width;
  121.         }
  122.  
  123.         if (y < 0 ) {
  124.                 y = 0;
  125.         }
  126.  
  127.         if ( (y+height-1) >= h ) {
  128.                 y = h - height;
  129.         }
  130.  
  131.         dlg = ui_create_dialog(x, y, width, height, static_cast<dialog_flags>(DF_FILLED | DF_SAVE_BG | DF_MODAL), menu_handler, m.get());
  132.  
  133.         x = MENU_BORDER+3;
  134.         y = MENU_BORDER+3;
  135.  
  136.         for (int i=0; i<NumButtons; i++ )
  137.         {
  138.                 m->button_g[i] = ui_add_gadget_button( dlg, x, y, button_width, button_height, m->button[i], NULL );
  139.                 y += button_height+MENU_VERT_SPACING;
  140.         }
  141.  
  142.         choice = 0;
  143.  
  144.         while(choice==0)
  145.                 event_process();
  146.  
  147.         ui_close_dialog(dlg);
  148.         return choice;
  149. }
  150.  
  151. }
  152.