Advertisement
Redxone

[ZDoom - ACS] MouseMenu system (WIP)

Jul 10th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.12 KB | None | 0 0
  1. #include "ZCommon.acs"
  2.  
  3. // This is an on-screen mouse script written by wtg62.
  4. // I've written it so that it tries to match your operating system mouse sensitivity.
  5. // So mess with your mouse settings as you like!
  6. // No matter what you do, you won't experience awkward cursor movement in game!
  7.  
  8. // Extended Menu functions/Button clicking functionality by Redxone or Lewisk3.
  9.  
  10. // Mouse's coordinates
  11. // Starting values don't matter that much, but I prefer it to start in the center of the screen.
  12. int mx = 0.5;
  13. int my = 0.5;
  14. int mb = 0;
  15.  
  16. // Pixels to move every tic when using keyboard to move cursor.
  17. int kbSpeed = 10.0;
  18. bool inMenu = false;
  19. bool debugMenu;
  20.  
  21.  
  22.  
  23. // Cursor graphic
  24. str cursorGraphic = "cursor";
  25. // Menu graphics
  26. str menuBase = "mBase";
  27. str menuSelect = "mSelect";
  28. int optionSelected = 1;
  29. // ID Lists
  30.     // 10 IDs used by Debug mode.
  31.     // Mouse is ID: 1.
  32.     int baseID = 30;
  33.     int selectOverlayID = 11;
  34.     int imagesID = 12;
  35.     int infoID = 17;
  36.     // Set this to the max amount of images that can be used onscreen.
  37.     int maxImages = 5;
  38.     int imageIndex = 1;
  39. // End ID list.
  40. int hudLayers = 30;
  41. str optionsDesc[5] = {
  42.     "Spawn a deadly archvile to help you rekt all them annoying enemies! \n\n(Press confirm key)",
  43.     "Spawn a friendly shotgunner to help you! \n\n(Press confirm key)",
  44.     "Spawn in a SupplyDrop!",
  45.     "Herooo, tis optian numbra for Kappa",
  46.     "Last option woot!"
  47. };
  48. // Some of these may be not very nessesary, but this is for compatibility in the future.
  49. int optionPosX  [5] = {5525,16985,28977,40875,53059};
  50. int optionPosY  [5] = {7585,7585,7585,7585,7585};
  51. int optionWidth [5] = {6605,6605,6605,6605,6605};
  52. int optionHeight[5] = {20181,20181,20181,20181,20181};
  53. // How many clickable options per screen.
  54. int optionsScreen = 5;
  55.  
  56. // Hud size. The numbers are big because I want a small cursor graphic.
  57. int HUDX = 960;
  58. int HUDY = 600;
  59.  
  60. // Yaw scaling
  61. int yawMul = 5.0;
  62.  
  63. // -- Numerical processing stuff -- \\
  64. // This function will take any value from 0.0 to 1.0.
  65. // It will then convert it to a number between 0 and the "scale" variable.
  66. // It will return a fixed number.
  67. function int fixedToPixel(int x, int scale)
  68. {
  69.     // Here, I multiply 'x' by our scale to get the appropriate number.
  70.     // However, I must round down (basically truncate digits).
  71.     // The best way to do this is to shift our bits to the right, and back to the left.
  72.     // This effectively rounds down for us.
  73.     return (FixedMul(x,scale<<16)>>16)<<16;
  74. }
  75. function int fconvX(int x)
  76. {
  77.     return fixedToPixel(x,HUDX)+0.1;
  78. }
  79. function int fconvY(int x)
  80. {
  81.     return fixedToPixel(x,HUDY)+0.1;
  82. }
  83.  
  84. function int getImageSlot (void)
  85. {
  86.     // Gets next available unused image slot.
  87.     return imagesID+imageIndex-1
  88. }
  89.  
  90. // -- Drawing functions -- \\
  91.  
  92. function void drawDescription(void)
  93. {
  94.     SetFont("BIGFONT");
  95.     HudMessage(s:optionsDesc[optionSelected-1]; HUDMSG_PLAIN, infoID, CR_WHITE , fconvX(6000),fconvY(40000),0);
  96. }
  97.  
  98. function void drawImage(str img, int xpos, int ypos)
  99. {
  100.     SetFont(img);
  101.     HudMessage(s:"a"; HUDMSG_PLAIN, getImageSlot(), CR_UNTRANSLATED, fconvX(xpos), fconvY(ypos),0);
  102.     // Increase image slot index.
  103.     imageIndex++;
  104.     if(imageIndex > maxImages) imageIndex = 1;
  105. }
  106.  
  107. function void clearDraw(int id)
  108. {
  109.     HudMessage(s:"a"; HUDMSG_FADEOUT, id, CR_UNTRANSLATED, 0, 0, 0);
  110. }
  111.  
  112. function void drawSelected (void)
  113. {
  114.     SetFont(menuSelect);
  115.     switch(optionSelected)
  116.     {
  117.         case 1:
  118.             HudMessage(s:"a"; HUDMSG_PLAIN, selectOverlayID, CR_UNTRANSLATED, fconvX(optionPosX[0]), fconvY(optionPosY[0]),0);
  119.         break;
  120.         case 2:
  121.             HudMessage(s:"a"; HUDMSG_PLAIN, selectOverlayID, CR_UNTRANSLATED, fconvX(optionPosX[1]), fconvY(optionPosY[1]),0);
  122.         break;
  123.         case 3:
  124.             HudMessage(s:"a"; HUDMSG_PLAIN, selectOverlayID, CR_UNTRANSLATED, fconvX(optionPosX[2]), fconvY(optionPosY[2]),0);
  125.         break;
  126.         case 4:
  127.             HudMessage(s:"a"; HUDMSG_PLAIN, selectOverlayID, CR_UNTRANSLATED, fconvX(optionPosX[3]), fconvY(optionPosY[3]),0);
  128.         break;
  129.         case 5:
  130.             HudMessage(s:"a"; HUDMSG_PLAIN, selectOverlayID, CR_UNTRANSLATED, fconvX(optionPosX[4]), fconvY(optionPosY[4]),0);
  131.         break;
  132.         default:
  133.         break;
  134.     }
  135.     drawDescription();
  136. }
  137.  
  138. function void drawMenu (void)
  139. {
  140.     SetFont(menuBase);
  141.     HudMessage(s:"a"; HUDMSG_PLAIN, baseID, CR_UNTRANSLATED, fixedToPixel(0,HUDX)+0.1, fixedToPixel(0,HUDY)+0.1, 0);
  142.     drawSelected();
  143.     // Menu item graphics
  144.     drawImage("VILEP1",optionPosX[0]+optionPosX[0]/2, (optionPosY[0]*2) + 7000);
  145.     drawImage("SPOSA1",optionPosX[1]+optionPosX[1]/5, (optionPosY[1]*2) + 7000);
  146.     drawImage("mSupply",optionPosX[2]+optionPosX[2]/8, (optionPosY[2]*2) + 7000);
  147. }
  148.  
  149.  
  150. function void clearMenu (void)
  151. {
  152.   // Fade out menu items.
  153.     HudMessage(s:"a"; HUDMSG_FADEOUT, 1, CR_UNTRANSLATED, fixedToPixel(mx,HUDX)+0.1, fixedToPixel(my,HUDY)+0.1, 0);
  154.     for(int i = 0; i <= hudLayers; i++)
  155.     {
  156.         HudMessage(s:"a"; HUDMSG_FADEOUT, i, CR_UNTRANSLATED, 0, 0, 0);
  157.     }
  158. }
  159.  
  160.  
  161. // -- Menu logic -- \\
  162.  
  163. Script "rv_mouseMenu" (void)
  164. {
  165.    
  166.     if(inMenu == true)
  167.     {
  168.         // Level set up, not an important part of the script at all.
  169.         //SetMusic("");
  170.         SetPlayerProperty(0,1,PROP_TOTALLYFROZEN);
  171.        
  172.         // Set up
  173.         SetHudSize(HUDX,HUDY,0); // I just use these numbers for a small cursor graphic.
  174.         drawMenu();
  175.     }
  176.     while(inMenu)
  177.     {
  178.         // Debug coordinates.
  179.         if(debugMenu)
  180.         {
  181.             SetFont("SMALLFONT");
  182.             HudMessage(s:"X: " ; HUDMSG_PLAIN, 3, CR_UNTRANSLATED, fixedToPixel(mx-7000,HUDX)+0.1, fixedToPixel(my,HUDY)+0.1, 0);
  183.             HudMessage(d:mx ; HUDMSG_PLAIN, 4, CR_UNTRANSLATED, fixedToPixel(mx-3500,HUDX)+0.1, fixedToPixel(my,HUDY)+0.1, 0);
  184.             HudMessage(s:"Y: " ; HUDMSG_PLAIN, 5, CR_UNTRANSLATED, fixedToPixel(mx-7000,HUDX)+0.1, fixedToPixel(my+1000,HUDY)+0.1, 0);
  185.             HudMessage(d:my ; HUDMSG_PLAIN, 6, CR_UNTRANSLATED, fixedToPixel(mx-3500,HUDX)+0.1, fixedToPixel(my+1000,HUDY)+0.1, 0);
  186.             HudMessage(s:"B: " ; HUDMSG_PLAIN, 7, CR_UNTRANSLATED, fixedToPixel(mx-7000,HUDX)+0.1, fixedToPixel(my+2000,HUDY)+0.1, 0);
  187.             HudMessage(d:mb ; HUDMSG_PLAIN, 8, CR_UNTRANSLATED, fixedToPixel(mx-3500,HUDX)+0.1, fixedToPixel(my+2000,HUDY)+0.1, 0);
  188.             HudMessage(s:"S: " ; HUDMSG_PLAIN, 9, CR_UNTRANSLATED, fixedToPixel(mx-7000,HUDX)+0.1, fixedToPixel(my+3000,HUDY)+0.1, 0);
  189.             HudMessage(d:optionSelected ; HUDMSG_PLAIN, 10, CR_UNTRANSLATED, fixedToPixel(mx-3500,HUDX)+0.1, fixedToPixel(my+3000,HUDY)+0.1, 0);
  190.         }
  191.         // We need to support keybinds for keyboard look.
  192.         // So let's check if we're using the keyboard or mouse, and then respond appropriately.
  193.         if(mb == 1 && !(GetPlayerInput(0,INPUT_BUTTONS) & BT_ATTACK))
  194.         {
  195.             mb = 0;
  196.             for(int i = 0; i <= optionsScreen; i++)
  197.             {
  198.                 if(mx >= optionPosX[i] && mx <= optionPosX[i] + optionWidth[i] && my >= optionPosY[i] && my <= optionPosY[i] + optionHeight[i])
  199.                 {
  200.                     optionSelected = i+1;
  201.                 }
  202.             }
  203.             drawMenu();
  204.         }
  205.         if(GetPlayerInput(0,INPUT_BUTTONS) & (BT_LEFT|BT_RIGHT|BT_LOOKUP|BT_LOOKDOWN))
  206.         {
  207.             // Keyboard controlled cursor
  208.             // Should be slow enough for user to be able to precisely hover over stuff.
  209.             // 'kbSpeed' pixels per tic.
  210.             // I use our previously set hud size to make movement speed on both axes look the same.
  211.             if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LEFT)       mx -= (kbSpeed/HUDX);
  212.             if(GetPlayerInput(0,INPUT_BUTTONS) & BT_RIGHT)      mx += (kbSpeed/HUDX);
  213.             if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LOOKUP)     my -= kbSpeed/HUDY;
  214.             if(GetPlayerInput(0,INPUT_BUTTONS) & BT_LOOKDOWN)   my += kbSpeed/HUDY;
  215.         }
  216.         else
  217.         {
  218.             // Mouse click
  219.             if(GetPlayerInput(0,INPUT_BUTTONS) & BT_ATTACK){
  220.                 mb = 1;
  221.             }
  222.            
  223.             // So here's our mouse controlled cursor.
  224.             // Basically, these 2 variables below will take our player's yaw and pitch input and scale it.
  225.             // Input is scaled so that our in-game "cursor" movement accurately matches operating system mouse movement.
  226.             // 'pitchscale' is also basically multiplied by 2.
  227.             // Why? Because pitch input is 2x slower than yaw input.
  228.             // Check for prescaled movement
  229.             if(!GetCVar("m_noprescale")){ yawMul = 2.5; } else { yawMul = 5.0; }
  230.             int yawScale   = FixedDiv(yawMul,FixedMul(GetCVar("m_yaw"),GetCVar("mouse_sensitivity")));
  231.             int pitchScale = FixedDiv(5.0,FixedMul(GetCVar("m_pitch"),GetCVar("mouse_sensitivity")));
  232.            
  233.             // Now we get our delta x and delta y. 'Delta' means 'change in'. So 'change in x', and 'change in y'.
  234.             // We'll use these to offset our current mouse position.
  235.             int dmx = -FixedMul(GetPlayerInput(0,INPUT_YAW),yawScale);
  236.             int dmy = -FixedMul(GetPlayerInput(0,INPUT_PITCH),pitchScale);
  237.            
  238.             // Apply our offsets.
  239.             mx += dmx;
  240.             my += dmy;
  241.         }
  242.         if(inMenu == true)
  243.         {
  244.             // Clamp our mouse position, so our mouse doesn't go offscreen and into the abyss forever.
  245.             if(mx < 0)      mx = 0;
  246.             if(my < 0)      my = 0;
  247.             if(mx > 0.999)  mx = 0.999;
  248.             if(my > 0.999)  my = 0.999;
  249.            
  250.             // Time to display our cursor!
  251.             // I have to remember that we used the SetHudSize function.
  252.             // I can't use a value from 0.0 to 1.0 for our display coordinates.
  253.             // I have to use a value from 0.0 to 960.0 for x, and 0.0 to 600.0 for y.
  254.             // So that's why I have my fixedToPixel function defined above!
  255.             // I also have to add 0.1 to each of our coordinates.
  256.             // This signals to ZDoom I want the upper left corner of my image to be at the integer part of the coordinates.
  257.             SetFont(cursorGraphic);
  258.             HudMessage(s:"a"; HUDMSG_PLAIN, 1, CR_UNTRANSLATED, fixedToPixel(mx,HUDX)+0.1, fixedToPixel(my,HUDY)+0.1, 0);
  259.             Delay(1);
  260.         }
  261.     }
  262. }
  263.  
  264.  
  265. Script "toggleMenu" (void)
  266. {
  267.     debugMenu = getCVar("menu_debug");
  268.     inMenu = !inMenu;
  269.     if(!inMenu)
  270.     {
  271.         // Set level music back, unfreeze player.
  272.         SetMusic("*", 0);
  273.         SetPlayerProperty(0,0,PROP_TOTALLYFROZEN);
  274.    
  275.         clearMenu();
  276.     }
  277.     ACS_NamedExecute("rv_mouseMenu",0);
  278.    
  279. }
  280.  
  281. Script "confirmMenu" (void)
  282. {
  283.     if(inMenu)
  284.     {
  285.         LocalAmbientSound("misc/chat",100.0);
  286.         delay(10);
  287.         //ACS_NamedExecute("toggleMenu", 0);
  288.         //delay(10);
  289.         switch(optionSelected)
  290.         {
  291.             case 1:
  292.                 do {
  293.                     delay(1);
  294.                 } until (Spawn("FriendlyVile", GetActorX(0)-100.0, GetActorY(0), GetActorZ(0), 8, GetActorAngle(0) >> 8));
  295.                  //Thing_Hate(8,!0,5);
  296.             break;
  297.             case 2:
  298.                  do {
  299.                     delay(1);
  300.                 } until (Spawn("FriendlyShotty", GetActorX(0)-100.0, GetActorY(0), GetActorZ(0), 8, GetActorAngle(0) >> 8));
  301.                  //Thing_Hate(8,!0,5);
  302.             break;
  303.             case 3:
  304.                 If(!Spawn("RandomWeapon", GetActorX(0)+40.0, GetActorY(0), GetActorZ(0)+0.25, 12, GetActorAngle(0) >> 8))
  305.                 {
  306.                     ACS_NamedExecute("toggleMenu", 0);
  307.                     delay(10);
  308.                     Print(s:"No room to spawn. ");
  309.                 }
  310.                 else
  311.                 {
  312.                     Spawn("TeleportFog", GetActorX(0)+40.0, GetActorY(0), GetActorZ(0), 12, GetActorAngle(0) >> 8);
  313.                 }
  314.            
  315.             break;
  316.             case 4:
  317.  
  318.             break;
  319.             case 5:
  320.  
  321.             break;
  322.             default:
  323.                 // Just in case something really gets messed up :P
  324.                 SetFont("BIGFONT");
  325.                 HudMessage(s:"Invalid selection."; HUDMSG_PLAIN, infoID, CR_WHITE , fconvX(6000),fconvY(40000),0);
  326.                 delay(35);
  327.                 clearMenu();
  328.             break;
  329.         }
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement