November 28, 2020, 02:22:28 AM

Author Topic: [bb] Yes No Cancel Window by semar [ 1+ years ago ]  (Read 555 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Yes No Cancel Window by semar [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Yes No Cancel Window
Author : semar
Posted : 1+ years ago

Description : Some time we need just a simple window to display a message, and wait for the user choice.
This function shows a window with a message, and displays up to three buttons, each one with customizable text.
No gfx and external globals are required; just a bi-dimensional array of 3*3 elements, to be declared in the main program.
The call to the function is very simple:
retcode = win_popup("Sure you want to quit ?","YES","NO","CANCEL")

Works in windowed mode only.


Code :
Code: BlitzBasic
  1. ;windowed mode
  2. Graphics 640,480,0,2
  3.  
  4. ;application title
  5. AppTitle "Simple Window Demo"
  6.  
  7. SetBuffer BackBuffer()
  8.  
  9. ;global array to be declared in the main program
  10. Dim area(3,3)
  11.  
  12. ;just a test screen
  13. ClsColor 0,128,128
  14. Cls
  15.  
  16. ;call of the function
  17. retcode = win_popup("Sure you want to quit ?","YES","NO","CANCEL")
  18.  
  19. ;shows the returned value
  20. Color 255,255,0
  21. Print "Button pressed = " + retcode
  22.  
  23. ;waits for any key pressed
  24. FlushKeys
  25. WaitKey
  26. End
  27.  
  28. ;==================================================================================
  29. Function win_popup(title$,but1_caption$="",but2_caption$="",but3_caption$="")
  30. ;==================================================================================
  31. ;BY SEMAR - semar63@hotmail.com
  32. ;
  33. ;displays a window and waits for a mouse click on a button, or esc with ESC key
  34. ;works only in windowed mode
  35. ;----------------------------------------------------------------------------------
  36. ;parameters:
  37. ;
  38. ;title$ = The title of the window to be displayed in the window bar
  39. ;butX_caption$ = The caption of the X button
  40. ;max three buttons available
  41. ;----------------------------------------------------------------------------------
  42. ;returned value:
  43. ;a number that represents the button pressed (1..3)
  44. ;or 0 if ESC was pressed instead
  45. ;----------------------------------------------------------------------------------
  46.  
  47. Local window_font = LoadFont("arial.ttf",20,True)
  48. SetFont window_font
  49.  
  50. ;show mouse pointer
  51. ShowPointer
  52.  
  53. ;memo the current graphic buffer
  54. Local oldbuffer = GraphicsBuffer()
  55.  
  56. ;dimension of the window based on the lenght of the title
  57. Local window_width = StringWidth(title) + 100
  58.  
  59. ;height of the window (constant)
  60. Local window_height= 100
  61.  
  62. ;center a viewport on the screen
  63. Local window_x = (GraphicsWidth() - window_width)/2
  64. Local window_y = (GraphicsHeight() - window_height)/2
  65.  
  66. ;buttons vars
  67. Local button_width = 80 ;width of each button
  68. Local button_height = 30 ;height of each button
  69. Local x_button ;starting x point for the first button
  70. Local y_button ;y for each button
  71.  
  72. ;make the viewport bigger for the shadow
  73. Local shadow = 10
  74. Viewport (window_x-shadow,window_y-shadow,window_width+2*shadow,window_height+2*shadow)
  75.  
  76. ;draw a grey shadow
  77. Color 72,72,72
  78. Rect window_x+shadow,window_y+shadow,window_width,window_height,1
  79.  
  80. ;draw the window
  81. Color 192,192,192
  82. Rect window_x,window_y,window_width,window_height,1
  83.  
  84. ;draw the window bar
  85. Color 0,0,128
  86. Rect window_x+1,window_y+1,window_width-2,25,1
  87.  
  88. ;center the window title text to the window bar
  89. Color 255,255,255
  90. Text window_x + window_width/2 ,window_y+3, title, True
  91.  
  92. ;draw buttons
  93. x_button = window_x + 10 ;starting x point for the first button
  94. y_button = window_y + 50 ;y for each button
  95.  
  96. ;draw first button
  97. If but1_caption <> "" Then
  98.         Color 0,0,0
  99.         Rect x_button,y_button,button_width,button_height,0
  100.         Text x_button + button_width/2, y_button+5,but1_caption,True
  101.        
  102.         ;make the button 3d
  103.         Color 255,255,255
  104.         Line x_button,y_button,x_button + button_width - 1,y_button
  105.         Line x_button,y_button,x_button,y_button + button_height - 1
  106.        
  107.         ;memo the button coordinates in the array
  108.         area(1,1) = x_button
  109.         area(1,2) = y_button
  110. EndIf
  111.  
  112. ;determine the x position for the middle button
  113. x_button = window_x + window_width/2 - button_width/2
  114. ;draw middle button
  115. If but2_caption <> "" Then
  116.         Color 0,0,0
  117.         Rect x_button,y_button,button_width,button_height,0
  118.         Text x_button + button_width/2, y_button+5,but2_caption,True
  119.  
  120.         ;make the button 3d
  121.         Color 255,255,255
  122.         Line x_button,y_button,x_button + button_width - 1,y_button
  123.         Line x_button,y_button,x_button,y_button + button_height - 1
  124.  
  125.         ;memo the button coordinates in the array
  126.         area(2,1) = x_button
  127.         area(2,2) = y_button
  128. EndIf
  129.  
  130.  
  131. ;determine the x position for the third button
  132. x_button = window_x + window_width - button_width - 10
  133.  
  134. ;draw third button
  135. If but3_caption <> "" Then
  136.         Color 0,0,0
  137.         Rect x_button,y_button,button_width,button_height,0
  138.         Text x_button + button_width/2, y_button+5,but3_caption,True
  139.  
  140.         ;make the button 3d
  141.         Color 255,255,255
  142.         Line x_button,y_button,x_button + button_width - 1,y_button
  143.         Line x_button,y_button,x_button,y_button + button_height - 1
  144.  
  145.         ;memo the button coordinates in the array
  146.         area(3,1) = x_button
  147.         area(3,2) = y_button
  148. EndIf
  149.  
  150.  
  151. ;show the window
  152. Flip
  153.  
  154. ;loop until we click on a button or press the ESC key
  155. click = 0
  156. While (Not KeyDown(1)) And  (Not click)
  157.  
  158.         ;if click with the left button
  159.         If MouseHit(1)
  160.        
  161.                 ;check all the buttons
  162.                 For n = 1 To 3
  163.                
  164.                         ;check if the mouse click was inside a button
  165.                         If (MouseX() > area(n,1)) And (MouseX()< area(n,1) + button_width) Then
  166.                                 If (MouseY() > area(n,2)) And (MouseY()< area(n,2) + button_height) Then
  167.                                
  168.                                         ;acquire the button pressed
  169.                                         click = n
  170.                                        
  171.                                         ;exits from the for..next loop
  172.                                         Exit
  173.                                        
  174.                                 EndIf
  175.                         EndIf
  176.                 Next
  177.         EndIf
  178.        
  179. Wend
  180.  
  181. ;delete the window
  182. Cls
  183.  
  184. ;reset the viewport
  185. Viewport 0,0,GraphicsWidth(),GraphicsHeight()
  186.  
  187. ;reset the buffer to the previous one
  188. SetBuffer oldbuffer
  189.  
  190. ;free the font
  191. FreeFont window_font
  192.  
  193. ;return the button pressed, or 0 if ESC was pressed instead
  194. Return click
  195.  
  196. End Function
  197.  


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal