Advertisement
sseebbyy

CMSS v2 (13.03.2015)

Mar 13th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.70 KB | None | 0 0
  1. /*
  2.  
  3. Custom Menu System by Seby v2.0 for Vice City Multiplayer.
  4. VC:MP Website:  http://www.vc-mp.org
  5. VC:MP Forum:    http://forum.vc-mp.org
  6. ===================================================================================================================================================================
  7.  
  8. CREDITS:
  9.  
  10. Scripter: Seby (aka sseebbyy)
  11. Special credits goes to shadow.heekz, Knucis and Gudio because I learnt new squirrel stuff from them.
  12.  
  13. DO NOT REMOVE THEM, OR WE WILL NEVER OFFER YOU HELP OR SCRIPTS AGAIN !
  14.  
  15. ===================================================================================================================================================================
  16.  
  17. Description:
  18.     Custom Menu System by Seby ( aka CMSS ) is a system that gives scripters the ability to create their own INTERACTIVE MENU,
  19. up to 10 options, but every option can also have a submenu (with another 10 options) !!
  20.  
  21.     Version 2.0 works a lot different than v1.0:
  22.         -Now there are 9 sprites, created when the script is loaded. We only show them when needed.
  23.         -The textdraws are all created right in the moment of accessing the menu, but don't worry, it will not reach the
  24. VC:MP limit (256) that fast, because it works in this way: when a player X wants to see a menu that is also shown for
  25. player Y, the textdraws will be just shown to X, not re-created ! So the textdraws will be removed only when there will be
  26. no viewer.
  27.         -No more double textdraws. ( 2 textdraws were used for one option, in v1.0, one grey, one white )
  28.         -To avoid the bugs, a player that is watching a menu and leave the server or just get killed without closing it,
  29. everything related to menus will be instantly canceled for him. ( menuRageQuit( player ) )
  30.         -You are now able to set the title of a submenu. (not like before, where the title were auto-set)
  31.         -No need to manually hide the main menu, when you want to show the submenu ! Just use "menuInstance.ShowSubmenu( player, 5 )"
  32. and it will do everything for you. Same goes for HideSubmenu function.
  33.  
  34.     And maybe more, but I don't remember right now... but take a look at the script, and you will understand. :)
  35.     I made it as understable as I could.
  36.    
  37.     The single thing that stays the same as in v1.0, is the way we save menus' instance: " menu[ ID ] "
  38.     So, when you want to add a submenu for that menu, you will do like this: " menu[3].AddSubmenu( 3, "/Option 3 - blabla", "option 1", "option 2", ... ) "
  39.    
  40.     Keys used to interact with the menu, used in my example, are: UP_ARROW, DOWN_ARROW, SPACEBAR (select), BACKSPACE (back)
  41.    
  42.     I hope I didn't miss anything important.
  43.    
  44. ===================================================================================================================================================================
  45.     Here is the list with the new functions'n'events:  
  46.  
  47.     Menu functions: CreateMenu( title, option1, option2, ... ),
  48.                     menuRageQuit( player ),
  49.                     menuRageQuitAll( ),
  50.                    
  51.                     menuInstance.ShowMenu( player ),
  52.                     menuInstance.HideMenu( player ),
  53.                    
  54.                     menuInstance.AddSubmenu( optionID of mainmenu, title, option1, option2, ... ),
  55.                    
  56.                     menuInstance.ShowSubmenu( player, option ),
  57.                     menuInstance.HideSubmenu( player, option );
  58.  
  59.     Menu events:    onPlayerEnterMenu( player, menuID ),
  60.                     onPlayerExitMenu( player, menuID ),
  61.                     onPlayerSwitchOptionsInMenu( player, menuID, oldOption, newOption )
  62.                     onPlayerSelectInMenu( player, menuID, option );
  63.                    
  64.     Submenu events: onPlayerEnterSubmenu( player, menuID, submenuID ),
  65.                     onPlayerExitSubmenu( player, menuID, submenuID ),
  66.                     onPlayerSwitchOptionsInSubmenu( player, menuID, submenuID, oldOption, newOption )
  67.                     onPlayerSelectInSubmenu( player, menuID, submenuID, option );
  68.                    
  69. ===================================================================================================================================================================
  70.  
  71. */
  72.  
  73. print( "Custom Menu System by Seby (v2.0) was successfully loaded !" );
  74.  
  75. // Bind Keys ==========================================================
  76. KEY_MENU_UP <- BindKey(true, 0x26, 0, 0);       // UP_ARROW
  77. KEY_MENU_DOWN <- BindKey(true, 0x28, 0, 0);     // DOWN_ARROW
  78. KEY_MENU_SELECT <- BindKey(true, 0x20, 0, 0);   // SPACEBAR
  79. KEY_MENU_BACK <- BindKey(true, 0x08, 0, 0);     // BACKSPAGE
  80. // ====================================================================
  81.  
  82. // Changeable settings
  83. menuX <- 80;                                    // menu pos x
  84. menuY <- 310;                                   // menu pos y
  85. menuSpritePosX <- menuX - 10;                   // sprite pos x
  86. menuSpritePosY <- menuY - 10;                   // sprite pos y
  87. menuSpriteAlpha <- 200;                         // sprites' alpha
  88. menuOptionPrefix <- "::";                       // options' prefix
  89. menuOptionPosX <- menuX + 50;                   // options' pos x
  90. menuTitleColor <- 0xFF00FFFF;                   // menus' title color
  91. menuOptionColorSelected <- 0xFFFFFFFF;          // selected options' color
  92. menuOptionColorUnselected <- 0xFFAAAAAA;        // unselected options' color
  93.  
  94.  
  95. // Don't touch this !
  96. menu <- array( 100, null );
  97. menuCount <- 0;
  98. menuSprite <- array( 9, null );
  99.  
  100. menuOptionPosY <- array( 11, null );
  101. menuOptionPosY[0] = menuY;
  102.  
  103. menuOnScreen <- array( GetMaxPlayers(), null );
  104. menuOptionChoosen <- array( GetMaxPlayers(), null );
  105.  
  106. submenuOnScreen <- array( GetMaxPlayers(), null );
  107. submenuOptionChoosen <- array( GetMaxPlayers(), null );
  108.  
  109. for(local i = 0; i < menuSprite.len(); i++)
  110.     menuSprite[i] = CreateSprite( "menuBackType" + i + ".png", menuSpritePosX, menuSpritePosY, 0, 0, 0,  menuSpriteAlpha);
  111.  
  112. class menuTextdrawClass
  113. {
  114.     instance = null;
  115.     text = null;
  116.     number = null;
  117.  
  118.    
  119.         constructor( paramText, paramNumber )
  120.         {
  121.             text = paramText;
  122.             number = paramNumber;
  123.         }
  124.        
  125.         function Show( player, way )
  126.         {
  127.             switch( way )
  128.             {
  129.                 case 0:
  130.                     if( number == 0 )
  131.                         // number 0 is the title
  132.                         instance = ::CreateTextdraw( text, menuX, menuY, menuTitleColor );
  133.                     else
  134.                     {
  135.                         menuOptionPosY[number] = menuOptionPosY[number-1] + 15;
  136.                         instance = ::CreateTextdraw( menuOptionPrefix + " " + text, menuOptionPosX, menuOptionPosY[number], menuOptionColorUnselected );
  137.                     }
  138.                     break;
  139.                 default: break;
  140.             }
  141.                
  142.             instance.ShowForPlayer( player );
  143.         }
  144.        
  145.         function Hide( player, way )
  146.         {
  147.             instance.HideFromPlayer( player );
  148.            
  149.             switch( way )
  150.             {
  151.                 case -1:
  152.                 case 0:
  153.                     instance.Delete( );
  154.                     break;
  155.                 default: break;
  156.             }
  157.         }
  158.        
  159.         function BeSelected( player )
  160.         {
  161.             instance.SetColourForPlayer( player, menuOptionColorSelected );
  162.         }
  163.        
  164.         function BeUnselected( player )
  165.         {
  166.             instance.SetColourForPlayer( player, menuOptionColorUnselected );
  167.         }
  168. }
  169.  
  170. class ExCreateMenu
  171. {
  172.     ID = null;
  173.     title = null;
  174.     menuOption = null;
  175.     params = null;
  176.    
  177.     subtitle = null;
  178.     subparams = null;
  179.    
  180.     viewers = null;
  181.     submenuViewers = null;
  182.        
  183.         constructor( menuID, ... )
  184.         {  
  185.             if ( vargv.len() < 3 || vargv.len() > 11 ) print("[CMSS Error] CreateMenu failed - the number of options written is under 2 or bigger than 10 !");
  186.             else
  187.             {
  188.                 title = vargv[0].tostring();
  189.                
  190.                 viewers = 0;
  191.                 submenuViewers = array( 11, null );
  192.                
  193.                 subparams = array( 11, null );
  194.                 menuOption = array( 11, null );
  195.                 for( local i = 0; i < menuOption.len(); i++)
  196.                 {
  197.                     menuOption[i] = array( 11, null );
  198.                 }
  199.                    
  200.                 params = vargv.len();
  201.                        
  202.                     for(local i = 0; i < vargv.len(); i++)
  203.                         menuOption[0][i] = menuTextdrawClass( vargv[i], i );
  204.                        
  205.                 ID = menuID;
  206.             }
  207.         }
  208.        
  209.         function ShowMenu( player )
  210.         {
  211.             if( !player ) return;
  212.             else
  213.             {
  214.                 menuSprite[ params - 3 ].ShowForPlayer( player );
  215.            
  216.                 for(local i = 0; i < params; i++)
  217.                     if( menuOption[0][i] ) menuOption[0][i].Show( player, viewers );
  218.                
  219.                 menuOption[0][1].BeSelected( player );
  220.                 menuOptionChoosen[ player.ID ] = 1;
  221.                
  222.                 if( menuOnScreen[ player.ID ] == -1 )
  223.                 {  
  224.                     menuOnScreen[ player.ID ] = ID;
  225.                     player.IsFrozen = true;
  226.                        
  227.                     viewers++;
  228.                    
  229.                     onPlayerEnterMenu( player, ID );
  230.                     onPlayerSwitchOptionsInMenu( player, ID, 0, 1 );
  231.                 }
  232.             }
  233.         }
  234.        
  235.         function HideMenu( player )
  236.         {
  237.             if( !player ) return;
  238.             else
  239.             {
  240.                 menuSprite[ params - 3 ].HideFromPlayer( player );
  241.  
  242.                 for(local i = 0; i < params; i++)
  243.                     if( menuOption[0][i] )
  244.                         if( submenuOnScreen[ player.ID ] == 1 ) menuOption[0][i].Hide( player, viewers );
  245.                         else menuOption[0][i].Hide( player, viewers - 1 );
  246.                
  247.                 if( menuOnScreen[ player.ID ] != -1 && submenuOnScreen[ player.ID ] == 0 )
  248.                 {
  249.                     menuOnScreen[ player.ID ] = -1;
  250.                     menuOptionChoosen[ player.ID ] = 0;
  251.                     player.IsFrozen = false;
  252.                        
  253.                     viewers--;
  254.                    
  255.                     onPlayerExitMenu( player, ID );
  256.                 }
  257.             }
  258.         }
  259.        
  260.         function AddSubmenu( option, ... )
  261.         {
  262.             if( option < 1 || option > 10 ) print( "[CMSS Error] AddSubmenu failed - the specified option doesn't exist. It needs to be from 1 to 10." );
  263.             else if ( vargv.len() < 3 || vargv.len() > 11 ) print( "[CMSS Error] AddSubmenu failed - the number of suboptions written is under 2 or bigger than 10 !" );
  264.             else
  265.             {
  266.                 subtitle = vargv[0].tostring();
  267.                 subparams[ option ] = vargv.len();
  268.                        
  269.                     for(local i = 0; i < vargv.len(); i++)
  270.                         menuOption[ option ][ i ] = menuTextdrawClass( vargv[i], i );
  271.                        
  272.                 submenuViewers[ option ] = 0;
  273.             }
  274.         }
  275.        
  276.         function ShowSubmenu( player, option )
  277.         {
  278.             if( !player ) return;
  279.             else
  280.             {
  281.                 for( local i = 0; i < subparams[ option ]; i++ )
  282.                     if( menuOption[ option ][ i ] ) menuOption[ option ][ i ].Show( player, submenuViewers[ option ] );
  283.                
  284.                 menuOption[ option ][ 1 ].BeSelected( player );
  285.                 submenuOnScreen[ player.ID ] = 1;
  286.                 submenuOptionChoosen[ player.ID ] = 1;
  287.                
  288.                 submenuViewers[ option ]++;
  289.                
  290.                 menu[ ID ].HideMenu( player );
  291.                
  292.                 menuSprite[ subparams[ option ] - 3 ].ShowForPlayer( player );
  293.                
  294.                 onPlayerEnterSubmenu( player, ID, option );
  295.                 onPlayerSwitchOptionsInSubmenu( player, ID, option, 0, 1 );
  296.             }
  297.         }
  298.        
  299.         function HideSubmenu( player, option )
  300.         {
  301.             if( !player ) return;
  302.             else
  303.             {
  304.                 menuSprite[ subparams[ option ] - 3 ].HideFromPlayer( player );
  305.            
  306.                 menu[ ID ].ShowMenu( player );
  307.                
  308.                 for(local i = 0; i < subparams[ option ]; i++ )
  309.                     if( menuOption[ option ][ i ] ) menuOption[ option ][ i ].Hide( player, submenuViewers[ option ] - 1 );
  310.                    
  311.                 submenuOnScreen[ player.ID ] = 0;
  312.                 submenuOptionChoosen[ player.ID ] = 0;
  313.                
  314.                 submenuViewers[ option ]--;
  315.                
  316.                 onPlayerExitSubmenu( player, ID, option );
  317.             }
  318.         }
  319. }
  320.  
  321. function CreateMenu( ... )
  322. {
  323.     if ( vargv.len() < 3 || vargv.len() > 11 ) print("[CMSS Error] CreateMenu failed - the number of options written is under 2 or bigger than 10 !");
  324.     else
  325.     {
  326.         switch( vargv.len() )
  327.         {
  328.             case 3:
  329.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2] );
  330.                 menuCount++;
  331.                 return menu[menuCount];
  332.             case 4:
  333.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3] );
  334.                 menuCount++;
  335.                 return menu[menuCount];
  336.             case 5:
  337.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4] );
  338.                 menuCount++;
  339.                 return menu[menuCount];
  340.             case 6:
  341.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5] );
  342.                 menuCount++;
  343.                 return menu[menuCount];
  344.             case 7:
  345.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5], vargv[6] );
  346.                 menuCount++;
  347.                 return menu[menuCount];
  348.             case 8:
  349.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5], vargv[6], vargv[7] );
  350.                 menuCount++;
  351.                 return menu[menuCount];
  352.             case 9:
  353.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5], vargv[6], vargv[7], vargv[8] );
  354.                 menuCount++;
  355.                 return menu[menuCount];
  356.             case 10:
  357.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5], vargv[6], vargv[7], vargv[8], vargv[9] );
  358.                 menuCount++;
  359.                 return menu[menuCount];
  360.             case 11:
  361.                 menu[menuCount] = ExCreateMenu( menuCount, vargv[0], vargv[1], vargv[2], vargv[3], vargv[4], vargv[5], vargv[6], vargv[7], vargv[8], vargv[9], vargv[10] );
  362.                 menuCount++;
  363.                 return menu[menuCount];
  364.         }
  365.     }
  366. }
  367.  
  368. function menuJoin( player )
  369. {
  370.     menuOnScreen[ player.ID ] = -1;
  371.     menuOptionChoosen[ player.ID ] = 0;
  372.    
  373.     submenuOnScreen[ player.ID ] = 0;
  374.     submenuOptionChoosen[ player.ID ] = 0;
  375. }
  376.  
  377. // ====================================== C U S T O M   M E N U   E V E N T S =========================================
  378. /*
  379. function onPlayerEnterMenu( player, menuID )
  380. {
  381.     print( player.Name + " entered in menu " + menuID );
  382. }
  383.  
  384. function onPlayerExitMenu( player, menuID )
  385. {
  386.     print( player.Name + " left the menu " + menuID );
  387. }
  388.  
  389. function onPlayerEnterSubmenu( player, menuID, submenuID )
  390. {
  391.     print( player.Name + " entered in submenu " + submenuID + " of menu " + menuID );
  392. }
  393.  
  394. function onPlayerExitSubmenu( player, menuID, submenuID )
  395. {
  396.     print( player.Name + " left the submenu " + submenuID + " of menu " + menuID );
  397. }
  398.  
  399. function onPlayerSwitchOptionsInMenu( player, menuID, oldOption, newOption )
  400. {
  401.     print( player.Name + " switched options in menu ID " + menuID + ". (old: " + oldOption + ") (new: " + newOption + ")");
  402. }
  403.  
  404. function onPlayerSwitchOptionsInSubmenu( player, menuID, submenuID, oldOption, newOption )
  405. {
  406.     print( player.Name + " switched options in submenu ID " + submenuID + " of menu ID " + menuID + ". (old: " + oldOption + ") (new: " + newOption + ")");
  407. }
  408.  
  409. function onPlayerSelectInMenu( player, menuID, option )
  410. {
  411.     print( player.Name + " selected option " + option + " from menu " + menuID);
  412. }
  413.  
  414. function onPlayerSelectInSubmenu( player, menuID, submenuID, option )
  415. {
  416.     print( player.Name + " selected option " + option + " from submenu " + submenuID + " of menu " + menuID );
  417. }
  418. */
  419. // ================================== E N D   OF   O F F I C I A L   E V E N T S ======================================
  420.  
  421. function menuRageQuit( player )
  422. {
  423.     if( player )
  424.     {
  425.         if( menuOnScreen[ player.ID ] != -1 )
  426.         {
  427.             local   menuID = menuOnScreen[ player.ID ],
  428.                     menuInstance = menu[ menuID ],
  429.                     option = menuOptionChoosen[ player.ID ];
  430.            
  431.             if( submenuOnScreen[ player.ID ] != 0 )
  432.             {
  433.                 menuInstance.HideSubmenu( player, option );
  434.             }
  435.            
  436.             menuInstance.HideMenu( player );
  437.         }
  438.     }
  439. }
  440.  
  441. function menuRageQuitAll( )
  442. {
  443.     for( local plr, i = 0; i <= GetPlayers(); i++ )
  444.     {
  445.         plr = FindPlayer(i);
  446.         if( plr ) menuRageQuit( plr );
  447.     }
  448. }
  449.  
  450. function menuKeyDown( player, key )
  451. {
  452.     if( menuOnScreen[ player.ID ] > -1 )
  453.     {
  454.         local   menuID = menuOnScreen[ player.ID ],
  455.                 menuInstance = menu[ menuID ],
  456.                 option = menuOptionChoosen[ player.ID ],
  457.                 upperOption = option - 1,
  458.                 downerOption = option + 1,
  459.                 paramsNumber = menuInstance.params - 1,
  460.  
  461.                 suboption = submenuOptionChoosen[ player.ID ],
  462.                 upperSubOption = suboption - 1,
  463.                 downerSubOption = suboption + 1,
  464.                 subparamsNumber = menuInstance.subparams[ menuOptionChoosen[ player.ID ] ];
  465.            
  466.         switch( key )
  467.         {
  468.             case KEY_MENU_UP:
  469.                 if( submenuOnScreen[ player.ID ] != 1 )
  470.                 {
  471.                     menuInstance.menuOption[ 0 ][ option ].BeUnselected( player );
  472.                    
  473.                     if( option != 1 )
  474.                     {
  475.                         menuInstance.menuOption[ 0 ][ upperOption ].BeSelected( player );
  476.                         menuOptionChoosen[ player.ID ]--;
  477.                     }
  478.                     else
  479.                     {
  480.                         menuInstance.menuOption[ 0 ][ paramsNumber ].BeSelected( player );
  481.                         menuOptionChoosen[ player.ID ] = paramsNumber;
  482.                     }
  483.                    
  484.                     onPlayerSwitchOptionsInMenu( player, menuID, option, menuOptionChoosen[ player.ID ] );
  485.                 }
  486.                 else
  487.                 {
  488.                     menuInstance.menuOption[ option ][ suboption ].BeUnselected( player );
  489.                    
  490.                     if( suboption != 1 )
  491.                     {
  492.                         menuInstance.menuOption[ option ][ upperSubOption ].BeSelected( player );
  493.                         submenuOptionChoosen[ player.ID ]--;
  494.                     }
  495.                     else
  496.                     {
  497.                         menuInstance.menuOption[ option ][ subparamsNumber - 1 ].BeSelected( player );
  498.                         submenuOptionChoosen[ player.ID ] = subparamsNumber - 1;
  499.                     }
  500.                    
  501.                     onPlayerSwitchOptionsInSubmenu( player, menuID, option, suboption, submenuOptionChoosen[ player.ID ] );
  502.                 }
  503.                 break;
  504.             case KEY_MENU_DOWN:
  505.                 if( submenuOnScreen[ player.ID ] != 1 )
  506.                 {
  507.                     menuInstance.menuOption[0][option].BeUnselected( player );
  508.                    
  509.                     if( option != paramsNumber )
  510.                     {
  511.                         menuInstance.menuOption[0][downerOption].BeSelected( player );
  512.                         menuOptionChoosen[ player.ID ]++;
  513.                     }
  514.                     else
  515.                     {
  516.                         menuInstance.menuOption[0][1].BeSelected( player );
  517.                         menuOptionChoosen[ player.ID ] = 1;
  518.                     }
  519.                    
  520.                     onPlayerSwitchOptionsInMenu( player, menuID, option, menuOptionChoosen[ player.ID ] );
  521.                 }
  522.                 else
  523.                 {
  524.                     menuInstance.menuOption[ option ][ suboption ].BeUnselected( player );
  525.                    
  526.                     if( suboption != subparamsNumber - 1 )
  527.                     {
  528.                         menuInstance.menuOption[ option ][ downerSubOption ].BeSelected( player );
  529.                         submenuOptionChoosen[ player.ID ]++;
  530.                     }
  531.                     else
  532.                     {
  533.                         menuInstance.menuOption[ option ][ 1 ].BeSelected( player );
  534.                         submenuOptionChoosen[ player.ID ] = 1;
  535.                     }
  536.                    
  537.                     onPlayerSwitchOptionsInSubmenu( player, menuID, option, suboption, submenuOptionChoosen[ player.ID ] );
  538.                 }
  539.                 break;
  540.             case KEY_MENU_SELECT:
  541.                 if( subparamsNumber > 0 )
  542.                 {
  543.                     if( submenuOnScreen[ player.ID ] == 0 ) menuInstance.ShowSubmenu( player, option );
  544.                     else onPlayerSelectInSubmenu( player, menuID, option, suboption );
  545.                 }
  546.                 else onPlayerSelectInMenu( player, menuID, option );
  547.                 break;
  548.             case KEY_MENU_BACK:
  549.                 if( submenuOnScreen[ player.ID ] == 1 ) menuInstance.HideSubmenu( player, option );
  550.                 else menuInstance.HideMenu( player );
  551.                 break;
  552.             default: break;
  553.         }
  554.     }
  555. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement