Author Topic: Complete Input Popup Menu  (Read 3727 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Complete Input Popup Menu
« on: November 26, 2020, 08:17:39 am »
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2.  
  3. FOR i = 1 TO 100
  4.     'LINE (RND * 640, RND * 640)-(RND * 640, RND * 640), &HFF000000 + RND * &HFFFFFF, BF
  5.  
  6.  
  7.  
  8. choice = PopUpMenu("New|Start|Run|Quit", 100, 100, 16, 2, _RGB32(255, 255, 0), _RGB32(255, 128, 255))
  9. CLS , SkyBlue
  10. PRINT "You chose choice #"; choice
  11.  
  12. f = _LOADFONT("courbd.ttf", 72, "monospace")
  13.  
  14. choice = PopUpMenu("Cheese|Beef|Soda|Frog", 150, 150, f, 5, Silver, Purple)
  15.  
  16. PRINT choice
  17. PRINT "And this time you chose choice #"; choice
  18.  
  19.  
  20. FUNCTION PopUpMenu (choice$, left, top, font, framesize, framecolor, fontcolor)
  21.     D = _DEST
  22.     fh = _FONTHEIGHT(font)
  23.     tempimage = _NEWIMAGE(1000, 1000, 32)
  24.     _DEST tempimage
  25.     _FONT font
  26.  
  27.     DO
  28.         count = count + 1
  29.         REDIM _PRESERVE temp(count) AS STRING
  30.         l = INSTR(choice$, "|")
  31.  
  32.         IF l THEN
  33.             temp(count) = LEFT$(choice$, l - 1)
  34.             choice$ = MID$(choice$, l + 1)
  35.         ELSE
  36.             temp(count) = choice$
  37.         END IF
  38.         IF _PRINTWIDTH(temp(count)) > pw THEN pw = _PRINTWIDTH(temp(count))
  39.  
  40.         PRINT count, temp(count)
  41.     LOOP UNTIL l = 0
  42.     _FREEIMAGE tempimage
  43.  
  44.     iw = pw + framesize * 2
  45.     ih = fh * count + (framesize) * (count + 2) - 1
  46.  
  47.     tempimage = _NEWIMAGE(iw, ih, 32)
  48.     _DEST tempimage: _FONT font
  49.     FOR i = 0 TO framesize - 1: LINE (i, i)-(iw - i, ih - i), framecolor, B: NEXT
  50.  
  51.     COLOR fontcolor, 0
  52.     FOR i = 1 TO UBOUND(temp)
  53.         Center fh * (i - 1) + framesize * (i + 1), temp(i), tempimage
  54.     NEXT
  55.     Highlight = BoxImage(iw - framesize + 2, fh + framesize * 2, framesize, 0, fontcolor)
  56.     _DEST Highlight: CLS , 0
  57.     FOR i = 0 TO framesize - 1: LINE (i, i)-(iw - i - framesize * 2, fh + framesize - i), fontcolor, B: NEXT
  58.     _DEST D
  59.  
  60.     BG = _NEWIMAGE(_WIDTH(D), _HEIGHT(D), 32)
  61.     _PUTIMAGE , D, BG
  62.     PopUpMenu = 1
  63.     B = _BLEND(D)
  64.  
  65.     oldmouse = -1
  66.     DO
  67.         _DONTBLEND D
  68.         _PUTIMAGE , BG, D 'Restore our original background
  69.         _BLEND D
  70.         _PUTIMAGE (left, top), tempimage, D 'Put our options on the screen
  71.         _PUTIMAGE (left + framesize, top + (fh + framesize) * (PopUpMenu - 1) + framesize), Highlight, D 'put our highlight onto the screen
  72.         k = _KEYHIT
  73.         SELECT CASE k
  74.             CASE 20480: PopUpMenu = (PopUpMenu + 1) 'down
  75.             CASE 18432: PopUpMenu = (PopUpMenu - 1) 'up
  76.             CASE 13, 32: EXIT DO 'space or enter to select
  77.         END SELECT
  78.  
  79.         scroll = 0
  80.         WHILE _MOUSEINPUT
  81.             scroll = scroll + _MOUSEWHEEL
  82.         WEND
  83.         PopUpMenu = (PopUpMenu + SGN(scroll)) 'up/down from scrolling the mousewheel
  84.         IF _MOUSEX >= left AND _MOUSEX <= left + iw THEN 'we're in the right spot of the screen for mouse input
  85.             IF _MOUSEY > top + framesize AND _MOUSEY < top + ih THEN
  86.                 y = (_MOUSEY - top - framesize) \ (fh + framesize * 2)
  87.                 PopUpMenu = y + 1
  88.                 IF _MOUSEBUTTON(1) AND NOT oldmouse THEN EXIT DO
  89.             END IF
  90.         END IF
  91.         IF _MOUSEBUTTON(1) AND NOT oldmouse THEN EXIT DO
  92.         'Note:  If you only want the mouse to work inside the box, and not be counted as a viable click, then
  93.         'remark out the command above
  94.         oldmouse = _MOUSEBUTTON(1): scroll = 0
  95.  
  96.  
  97.         FOR i = 1 TO 3 'We have up/down movement on the joystick
  98.             IF ABS(STICK(1, i) - 127) > 10 THEN PopUpMenu = PopUpMenu + SGN(STICK(1, i) - 127): _DELAY .25
  99.         NEXT
  100.         IF STRIG(1) AND STRIG(0) THEN EXIT DO 'Joystick button 1 was pressed.
  101.         IF PopUpMenu > count THEN PopUpMenu = 1
  102.         IF PopUpMenu < 1 THEN PopUpMenu = count
  103.  
  104.         _LIMIT 10
  105.         _DISPLAY
  106.     LOOP
  107.  
  108.     _FREEIMAGE tempimage 'free the menu text
  109.     _FREEIMAGE Highlight 'free the highlight box
  110.     _DONTBLEND D
  111.     _PUTIMAGE , BG, D 'Restore our original background
  112.     _FREEIMAGE BG 'free the background copy
  113.     _DEST D
  114.     IF B = 0 THEN _DONTBLEND D
  115.     _DISPLAY
  116.  
  117.  
  118.  
  119. SUB Center (FromTop, what$, where)
  120.     D = _DEST: pw = _PRINTWIDTH(what$): sw = _WIDTH(where) 'print width and screen width
  121.     IF pw > sw THEN Explode "Center, with pw > sw"
  122.     _DEST where: _PRINTSTRING ((sw - pw) \ 2, FromTop), what$: _DEST D
  123.  
  124. SUB CenterImage (FromTop, what, where)
  125.     pw = _WIDTH(what): sw = _WIDTH(where) 'print width and screen width
  126.     IF pw > sw THEN Explode "CenterImage, with pw > sw"
  127.     _PUTIMAGE ((sw - pw) \ 2, FromTop), what, where
  128.  
  129. SUB Explode (where$)
  130.     PRINT "YOU F'ED UP!!  THERE'S A GLITCH IN THE CODE SOMEWHERE!"
  131.     FOR i = 1 TO 10: PRINT "FIX IT! ": BEEP: _DELAY .5: NEXT
  132.     PRINT: PRINT: PRINT "THE GLITCH IS IN: "; where$
  133.     END
  134.  
  135. FUNCTION BoxImage (wide, tall, thick, BGcolor, FrameColor)
  136.     D = _DEST
  137.     BoxImage = _NEWIMAGE(wide, tall, 32)
  138.     _DEST BoxImage
  139.     CLS , BGcolor
  140.     FOR i = 0 TO thick - 1
  141.         LINE (i, i)-STEP(wide - i * 2, tall - i * 2), FrameColor, B
  142.     NEXT
  143.     _DEST D

The demo above should showcase what this little snippet is all about -- it pops up a menu on the screen and asks the user to respond to it.  Keyboard, mouse, and joystick all should be valid methods to interact with the menu and have it do what you expect it to.   Works with all fonts -- monospaced and not -- and restores your background when it closes itself.
« Last Edit: February 08, 2021, 04:11:53 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Complete Input Popup Menu
« Reply #1 on: November 26, 2020, 11:58:57 am »
I am trying to decide if I like this better than Right Click pop-ups, they seem more refined.

What would you (or anyone) say advantage for this menu system is?

@SMcNeill I am watching the Keyboard Library thread for Samples Gallery, Informatics probably, do you plan to add this to that library? Not completely unrelated for input tools that go nice with Extended Input.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Complete Input Popup Menu
« Reply #2 on: November 26, 2020, 01:05:05 pm »
Advantage for me, is just ease of menu integration.  If you see the splash screen demo for the game I’ve been working on you’ll see this for the title screen options.  It’s all self contained, builds itself, updates itself, and works with mouse, scroll wheel, joystick, and keyboard interactions.  It maintains and restores background, so it can be used as a pop-up selection tool, with no worries of screen corruption.  Just toss this into a mouse routine to check for Right Clicks, and this can pop-up just like those can — only offering more options with interactions, fonts, and such.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Complete Input Popup Menu
« Reply #3 on: November 26, 2020, 01:08:26 pm »
Ah good! Thanks. Do you plan to include it with Keyboard Library with Extended Input?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Complete Input Popup Menu
« Reply #4 on: November 26, 2020, 01:13:47 pm »
I can, if you want.  There’s also a pop-up text box, message box, and input box which I’ll be sharing soon, for those who might be interested.  In the end, it’ll probably be nice to wrap them all up in a library together, but for discussion/examples, I find it easier to keep them independent to avoid any confusion anywhere.  :)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Complete Input Popup Menu
« Reply #5 on: November 26, 2020, 01:45:14 pm »
I can, if you want.  There’s also a pop-up text box, message box, and input box which I’ll be sharing soon, for those who might be interested.  In the end, it’ll probably be nice to wrap them all up in a library together, but for discussion/examples, I find it easier to keep them independent to avoid any confusion anywhere.  :)

Well you should do it how you want, sorry to interrupt, I was just thinking if all the parts were integrated and work well with each other ie your use of keyhit numbers working with the other parts or if you had a mouse or device routine working with all the other parts. I've discovered the need for one mouse/device handler (probably including keyboard) to cover all the box controls in a screen up at the same time.