Advertisement
Guest User

NameCompletion

a guest
Aug 19th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // console.cpp: the console buffer, its display, and command line control
  2.  
  3. int completionChosen = 1, lastpoint = -1;  // added: for the name completion.
  4. string StringToFind, leftText, rightText;  // variables to save the text on the chat console  
  5. const char *competionString = NULL; // pointer to the player name.
  6.  
  7. void resetnameCompletion() { if(!completionChosen) completionChosen = 1; competionString = NULL; }
  8.  
  9. /*
  10.  *  some functions before our code. ^.^
  11.  *
  12.  */
  13.  
  14. void consolekey(int code, bool isdown, int cooked)
  15. {
  16.     #ifdef __APPLE__
  17.         #define MOD_KEYS (KMOD_LMETA|KMOD_RMETA)
  18.     #else
  19.         #define MOD_KEYS (KMOD_LCTRL|KMOD_RCTRL)
  20.     #endif
  21.  
  22.     if(isdown)
  23.     {
  24.         switch(code)
  25.         {
  26.             case SDLK_RETURN:
  27.             case SDLK_KP_ENTER:
  28.                 break;
  29.  
  30.             case SDLK_HOME:
  31.                 //if(strlen(commandbuf)) commandpos = 0;
  32.                 if(strlen(commandbuf)) { commandpos = 0; resetnameCompletion();} // added: to reset the name completion.
  33.                 break;
  34.  
  35.             case SDLK_END:
  36.                 commandpos = -1;
  37.                 if(strlen(commandbuf)) resetnameCompletion();  // added: to reset the name completion.
  38.                 break;
  39.  
  40.             case SDLK_DELETE:
  41.             {
  42.                 int len = (int)strlen(commandbuf);
  43.                 if(commandpos<0) break;
  44.                 memmove(&commandbuf[commandpos], &commandbuf[commandpos+1], len - commandpos);
  45.                 resetcomplete();
  46.                 resetnameCompletion();  // added: to reset the name completion.
  47.                 if(commandpos>=len-1) commandpos = -1;
  48.                 break;
  49.             }
  50.  
  51.             case SDLK_BACKSPACE:
  52.             {
  53.                 int len = (int)strlen(commandbuf), i = commandpos>=0 ? commandpos : len;
  54.                 if(i<1) break;
  55.                 memmove(&commandbuf[i-1], &commandbuf[i], len - i + 1);
  56.                 resetcomplete();
  57.                 resetnameCompletion(); // added: to reset the name completion.
  58.                 if(commandpos>0) commandpos--;
  59.                 else if(!commandpos && len<=1) commandpos = -1;
  60.                 break;
  61.             }
  62.  
  63.             case SDLK_LEFT:
  64.                 if(commandpos>0) commandpos--;
  65.                 else if(commandpos<0) commandpos = (int)strlen(commandbuf)-1;
  66.                 resetnameCompletion();   // added: to reset the name completion.
  67.                 break;
  68.  
  69.             case SDLK_RIGHT:
  70.                 if(commandpos>=0 && ++commandpos>=(int)strlen(commandbuf)) commandpos = -1;
  71.                 resetnameCompletion();
  72.                 break;
  73.  
  74.             case SDLK_UP:
  75.                 if(histpos > history.length()) histpos = history.length();
  76.                 if(histpos > 0) history[--histpos]->restore();
  77.                 resetnameCompletion();  // added: to reset the name completion.
  78.                 break;
  79.  
  80.             case SDLK_DOWN:
  81.                 if(histpos + 1 < history.length()) history[++histpos]->restore();
  82.                 resetnameCompletion();  // added: to reset the name completion.
  83.                 break;
  84.  
  85.             case SDLK_TAB:
  86.                 if(SDL_GetModState()&KMOD_SHIFT) {  // changed: now the command complete works with SHIFT + TAB
  87.                     if(commandflags&CF_COMPLETE)
  88.                     {
  89.                         complete(commandbuf, commandflags&CF_EXECUTE ? "/" : NULL);
  90.                         if(commandpos>=0 && commandpos>=(int)strlen(commandbuf)) commandpos = -1;
  91.                     }
  92.                 }
  93.                 /*
  94.                  * added, for the name completion.
  95.                  */
  96.                 else if(!mainmenu)  // first, check if we are connected. this is not a good way to know that but
  97.                 {                   // i cant use game:connect >.<' (well, i can but i dont wanted). ^.^
  98.                    
  99.                     if(completionChosen)
  100.                     {
  101.                         int len = (int)strlen(commandbuf);  // size of the text in the chat (commandbuf): len.
  102.                         const char *pTexto = "";
  103.                        
  104.                         StringToFind[0] = leftText[0] = rightText[0] = '\0';
  105.                        
  106.                         if(len) // There si text (string) to find.
  107.                         {
  108.                             int leftTextsize = 0, StringSize = 0, leftOffset = 0;
  109.                             //  cursor position on chat/command/team console (a copy of commandpos).
  110.                             int posCursor = commandpos<0 ? len : commandpos;
  111.  
  112.                             // pointer to move through the text. This would be unnecessary.
  113.                             pTexto = &commandbuf[posCursor];
  114.  
  115.                             // find the begining of the word to find = lengh of the text at left of our string to find (if exist)
  116.                             while((leftOffset<posCursor) && !isspace(*(pTexto-1))) {
  117.                                 ++leftOffset; --pTexto;
  118.                             }
  119.                             // size (end position) of the text at the left of the string to find.
  120.                             leftTextsize = posCursor - leftOffset;
  121.                            
  122.                             // save the text at left to the string to find (if exist)
  123.                             copystring(leftText, commandbuf, leftTextsize + 1);
  124.                            
  125.                             // put the cursor in the start of the string to find.
  126.                             posCursor = leftTextsize;
  127.                            
  128.                             // get the size of the string to find: Stringsize
  129.                             while((posCursor<len) && !isspace(*pTexto)) {
  130.                                 ++StringSize; ++posCursor; ++pTexto;
  131.                             }
  132.                             // save the string selected on StringToFind.
  133.                             copystring(StringToFind, &commandbuf[leftTextsize], StringSize +1);
  134.                            
  135.                             // save the text at the right to the string to find (if exist).
  136.                             copystring(rightText, &commandbuf[leftTextsize+StringSize], (len-(leftTextsize+StringSize))+1);
  137.                         }
  138.                         completionChosen = 0;   // reset completion var
  139.                         //thereismatch = 0;
  140.                        
  141.                         // start from the begining of the layer list.
  142.                         if(lastpoint>=0) lastpoint = -1;
  143.                     }
  144.  
  145.                     // get the name (if exist) who match with our string to fine.
  146.                     // or if there is no string, get the names one by one.
  147.                     competionString = game::findPlayer(lastpoint, StringToFind);
  148.  
  149.                     if(competionString)
  150.                     {
  151.                         // update the position of the cursor.
  152.                         int newposCursor = (int)strlen(leftText) + (int)strlen(competionString);
  153.                         char separator[3] = "";
  154.                         string auxbuffer;
  155.                        
  156.                         // add (in auxbuffer) the text at the left, if exist.
  157.                         copystring(auxbuffer, leftText);
  158.                        
  159.                         // add the name.
  160.                         concatstring(auxbuffer, competionString);
  161.  
  162.                         // set the separator, if is necesary.
  163.                         if(!leftText[0])
  164.                             copystring(separator, !rightText[0] ? ": " : ":", !rightText[0] ? 3 : 2);
  165.  
  166.                         // add separator.
  167.                         concatstring(auxbuffer, separator);
  168.                        
  169.                         // update the position of the cursor (when separator is added).
  170.                         newposCursor += (int)strlen(separator);
  171.                        
  172.                         // add the text at the right.
  173.                         concatstring(auxbuffer,rightText);
  174.                        
  175.                         // update the text in the chat: commandbuf.
  176.                         copystring(commandbuf, auxbuffer);
  177.                        
  178.                         // and... update the posición of the cursor.
  179.                         commandpos = newposCursor;
  180.                     }
  181.                 }
  182.                 break;
  183.  
  184.             case SDLK_v:
  185.                 if(SDL_GetModState()&MOD_KEYS) { pasteconsole(); return; }
  186.                 // fall through
  187.  
  188.             default:
  189.                 resetcomplete();
  190.                 resetnameCompletion();  // added: to reset the name completion.
  191.                 if(cooked)
  192.                 {
  193.                     size_t len = (int)strlen(commandbuf);
  194.                     if(len+1<sizeof(commandbuf))
  195.                     {
  196.                         if(commandpos<0) commandbuf[len] = cooked;
  197.                         else
  198.                         {
  199.                             memmove(&commandbuf[commandpos+1], &commandbuf[commandpos], len - commandpos);
  200.                             commandbuf[commandpos++] = cooked;
  201.                         }
  202.                         commandbuf[len+1] = '\0';
  203.                     }
  204.                 }
  205.                 break;
  206.         }
  207.     }
  208.     else
  209.     {
  210.         if(code==SDLK_RETURN || code==SDLK_KP_ENTER)
  211.         {
  212.             hline *h = NULL;
  213.             if(commandbuf[0])
  214.             {
  215.                 if(history.empty() || history.last()->shouldsave())
  216.                 {
  217.                     if(maxhistory && history.length() >= maxhistory)
  218.                     {
  219.                         loopi(history.length()-maxhistory+1) delete history[i];
  220.                         history.remove(0, history.length()-maxhistory+1);
  221.                     }
  222.                     history.add(h = new hline)->save();
  223.                 }
  224.                 else h = history.last();
  225.             }
  226.             histpos = history.length();
  227.             inputcommand(NULL);
  228.             resetnameCompletion();  // added: to reset the name completion.
  229.             if(h) h->run();
  230.         }
  231.         else if(code==SDLK_ESCAPE)
  232.         {
  233.             histpos = history.length();
  234.             inputcommand(NULL);
  235.             resetnameCompletion(); // // added: to reset the name completion.
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement