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. #include <string.h>
  23.  
  24. #include "u_mem.h"
  25. #include "maths.h"
  26. #include "pstypes.h"
  27. #include "event.h"
  28. #include "gr.h"
  29. #include "ui.h"
  30. #include "mouse.h"
  31. #include "key.h"
  32.  
  33. namespace dcx {
  34.  
  35. #define Middle(x) ((2*(x)+1)/4)
  36.  
  37. static void ui_draw_box_in1(grs_canvas &canvas, const unsigned x1, const unsigned y1, const unsigned x2, const unsigned y2)
  38. {
  39.         const auto color = CWHITE;
  40.         gr_urect(canvas, x1 + 1, y1 + 1, x2 - 1, y2 - 1, color);
  41.         ui_draw_shad(canvas, x1, y1, x2, y2, CGREY, CBRIGHT);
  42. }
  43.  
  44.  
  45. void ui_draw_icon( UI_GADGET_ICON * icon )
  46. {
  47.         int height, width;
  48.         int x, y;
  49.        
  50. #if 0  //ndef OGL
  51.         if ((icon->status==1) || (icon->position != icon->oldposition))
  52. #endif
  53.         {
  54.                 icon->status = 0;
  55.  
  56.                 gr_set_current_canvas( icon->canvas );
  57.                 auto &canvas = *grd_curcanv;
  58.                 gr_get_string_size(*canvas.cv_font, icon->text.get(), &width, &height, nullptr);
  59.        
  60.                 x = ((icon->width-1)/2)-((width-1)/2);
  61.                 y = ((icon->height-1)/2)-((height-1)/2);
  62.  
  63.                 if (icon->position==1 )
  64.                 {
  65.                         // Draw pressed
  66.                         ui_draw_box_in(canvas, 0, 0, icon->width, icon->height);
  67.                         x += 2; y += 2;
  68.                 }
  69.                 else if (icon->flag)
  70.                 {
  71.                         // Draw part out
  72.                         ui_draw_box_in1(canvas, 0, 0, icon->width, icon->height);
  73.                         x += 1; y += 1;
  74.                 }
  75.                 else
  76.                 {
  77.                         // Draw released!
  78.                         ui_draw_box_out(canvas, 0, 0, icon->width, icon->height);
  79.                 }
  80.        
  81.                 gr_set_fontcolor(canvas, CBLACK, -1);
  82.                 gr_ustring(canvas, *canvas.cv_font, x, y, icon->text.get());
  83.         }
  84. }
  85.  
  86.  
  87. std::unique_ptr<UI_GADGET_ICON> ui_add_gadget_icon(UI_DIALOG * dlg, const char * text, short x, short y, short w, short h, int k,int (*f)())
  88. {
  89.         std::unique_ptr<UI_GADGET_ICON> icon{ui_gadget_add<UI_GADGET_ICON>(dlg, x, y, x+w-1, y+h-1)};
  90.  
  91.         icon->width = w;
  92.         icon->height = h;
  93.         auto ltext = strlen(text) + 1;
  94.         MALLOC( icon->text, char[], ltext + 1);//Hack by KRB
  95.         memcpy(icon->text.get(), text, ltext);
  96.         icon->trap_key = k;
  97.         icon->user_function = f;
  98.         icon->oldposition = 0;
  99.         icon->position = 0;
  100.         icon->pressed = 0;
  101.         icon->canvas->cv_font = ui_small_font.get();
  102.         // Call twice to get original;
  103.         if (f)
  104.         {
  105.                 icon->flag = static_cast<int8_t>(f());
  106.                 icon->flag = static_cast<int8_t>(f());
  107.         } else {
  108.                 icon->flag = 0;
  109.         }
  110.         return icon;
  111. }
  112.  
  113. window_event_result ui_icon_do( UI_DIALOG *dlg, UI_GADGET_ICON * icon,const d_event &event )
  114. {
  115.         icon->oldposition = icon->position;
  116.         icon->pressed = 0;
  117.  
  118.         window_event_result rval = window_event_result::ignored;
  119.         if (event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP)
  120.         {
  121.                 int OnMe;
  122.                
  123.                 OnMe = ui_mouse_on_gadget( icon );
  124.  
  125.                 if (B1_JUST_PRESSED && OnMe)
  126.                 {
  127.                         icon->position = 1;
  128.                         rval = window_event_result::handled;
  129.                 }
  130.                 else if (B1_JUST_RELEASED)
  131.                 {
  132.                         if ((icon->position == 1) && OnMe)
  133.                                 icon->pressed = 1;
  134.                                
  135.                         icon->position = 0;
  136.                 }
  137.         }
  138.  
  139.  
  140.         if (event.type == EVENT_KEY_COMMAND)
  141.         {
  142.                 int key;
  143.                
  144.                 key = event_key_get(event);
  145.                
  146.                 if (key == icon->trap_key)
  147.                 {
  148.                         icon->position = 1;
  149.                         rval = window_event_result::handled;
  150.                 }
  151.         }
  152.         else if (event.type == EVENT_KEY_RELEASE)
  153.         {
  154.                 int key;
  155.                
  156.                 key = event_key_get(event);
  157.                
  158.                 icon->position = 0;
  159.                
  160.                 if (key == icon->trap_key)
  161.                         icon->pressed = 1;
  162.         }
  163.                
  164.         if (icon->pressed == 1)
  165.         {
  166.                 icon->status = 1;
  167.                 icon->flag = static_cast<int8_t>(icon->user_function());
  168.                 rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, icon);
  169.                 if (rval == window_event_result::ignored)
  170.                         rval = window_event_result::handled;
  171.         }
  172.  
  173.         if (event.type == EVENT_WINDOW_DRAW)
  174.                 ui_draw_icon( icon );
  175.  
  176.         return rval;
  177. }
  178.  
  179. }
  180.