Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // console.cpp: the console buffer, its display, and command line control
- int completionChosen = 1, lastpoint = -1; // added: for the name completion.
- string StringToFind, leftText, rightText; // variables to save the text on the chat console
- const char *competionString = NULL; // pointer to the player name.
- void resetnameCompletion() { if(!completionChosen) completionChosen = 1; competionString = NULL; }
- /*
- * some functions before our code. ^.^
- *
- */
- void consolekey(int code, bool isdown, int cooked)
- {
- #ifdef __APPLE__
- #define MOD_KEYS (KMOD_LMETA|KMOD_RMETA)
- #else
- #define MOD_KEYS (KMOD_LCTRL|KMOD_RCTRL)
- #endif
- if(isdown)
- {
- switch(code)
- {
- case SDLK_RETURN:
- case SDLK_KP_ENTER:
- break;
- case SDLK_HOME:
- //if(strlen(commandbuf)) commandpos = 0;
- if(strlen(commandbuf)) { commandpos = 0; resetnameCompletion();} // added: to reset the name completion.
- break;
- case SDLK_END:
- commandpos = -1;
- if(strlen(commandbuf)) resetnameCompletion(); // added: to reset the name completion.
- break;
- case SDLK_DELETE:
- {
- int len = (int)strlen(commandbuf);
- if(commandpos<0) break;
- memmove(&commandbuf[commandpos], &commandbuf[commandpos+1], len - commandpos);
- resetcomplete();
- resetnameCompletion(); // added: to reset the name completion.
- if(commandpos>=len-1) commandpos = -1;
- break;
- }
- case SDLK_BACKSPACE:
- {
- int len = (int)strlen(commandbuf), i = commandpos>=0 ? commandpos : len;
- if(i<1) break;
- memmove(&commandbuf[i-1], &commandbuf[i], len - i + 1);
- resetcomplete();
- resetnameCompletion(); // added: to reset the name completion.
- if(commandpos>0) commandpos--;
- else if(!commandpos && len<=1) commandpos = -1;
- break;
- }
- case SDLK_LEFT:
- if(commandpos>0) commandpos--;
- else if(commandpos<0) commandpos = (int)strlen(commandbuf)-1;
- resetnameCompletion(); // added: to reset the name completion.
- break;
- case SDLK_RIGHT:
- if(commandpos>=0 && ++commandpos>=(int)strlen(commandbuf)) commandpos = -1;
- resetnameCompletion();
- break;
- case SDLK_UP:
- if(histpos > history.length()) histpos = history.length();
- if(histpos > 0) history[--histpos]->restore();
- resetnameCompletion(); // added: to reset the name completion.
- break;
- case SDLK_DOWN:
- if(histpos + 1 < history.length()) history[++histpos]->restore();
- resetnameCompletion(); // added: to reset the name completion.
- break;
- case SDLK_TAB:
- if(SDL_GetModState()&KMOD_SHIFT) { // changed: now the command complete works with SHIFT + TAB
- if(commandflags&CF_COMPLETE)
- {
- complete(commandbuf, commandflags&CF_EXECUTE ? "/" : NULL);
- if(commandpos>=0 && commandpos>=(int)strlen(commandbuf)) commandpos = -1;
- }
- }
- /*
- * added, for the name completion.
- */
- else if(!mainmenu) // first, check if we are connected. this is not a good way to know that but
- { // i cant use game:connect >.<' (well, i can but i dont wanted). ^.^
- if(completionChosen)
- {
- int len = (int)strlen(commandbuf); // size of the text in the chat (commandbuf): len.
- const char *pTexto = "";
- StringToFind[0] = leftText[0] = rightText[0] = '\0';
- if(len) // There si text (string) to find.
- {
- int leftTextsize = 0, StringSize = 0, leftOffset = 0;
- // cursor position on chat/command/team console (a copy of commandpos).
- int posCursor = commandpos<0 ? len : commandpos;
- // pointer to move through the text. This would be unnecessary.
- pTexto = &commandbuf[posCursor];
- // find the begining of the word to find = lengh of the text at left of our string to find (if exist)
- while((leftOffset<posCursor) && !isspace(*(pTexto-1))) {
- ++leftOffset; --pTexto;
- }
- // size (end position) of the text at the left of the string to find.
- leftTextsize = posCursor - leftOffset;
- // save the text at left to the string to find (if exist)
- copystring(leftText, commandbuf, leftTextsize + 1);
- // put the cursor in the start of the string to find.
- posCursor = leftTextsize;
- // get the size of the string to find: Stringsize
- while((posCursor<len) && !isspace(*pTexto)) {
- ++StringSize; ++posCursor; ++pTexto;
- }
- // save the string selected on StringToFind.
- copystring(StringToFind, &commandbuf[leftTextsize], StringSize +1);
- // save the text at the right to the string to find (if exist).
- copystring(rightText, &commandbuf[leftTextsize+StringSize], (len-(leftTextsize+StringSize))+1);
- }
- completionChosen = 0; // reset completion var
- //thereismatch = 0;
- // start from the begining of the layer list.
- if(lastpoint>=0) lastpoint = -1;
- }
- // get the name (if exist) who match with our string to fine.
- // or if there is no string, get the names one by one.
- competionString = game::findPlayer(lastpoint, StringToFind);
- if(competionString)
- {
- // update the position of the cursor.
- int newposCursor = (int)strlen(leftText) + (int)strlen(competionString);
- char separator[3] = "";
- string auxbuffer;
- // add (in auxbuffer) the text at the left, if exist.
- copystring(auxbuffer, leftText);
- // add the name.
- concatstring(auxbuffer, competionString);
- // set the separator, if is necesary.
- if(!leftText[0])
- copystring(separator, !rightText[0] ? ": " : ":", !rightText[0] ? 3 : 2);
- // add separator.
- concatstring(auxbuffer, separator);
- // update the position of the cursor (when separator is added).
- newposCursor += (int)strlen(separator);
- // add the text at the right.
- concatstring(auxbuffer,rightText);
- // update the text in the chat: commandbuf.
- copystring(commandbuf, auxbuffer);
- // and... update the posición of the cursor.
- commandpos = newposCursor;
- }
- }
- break;
- case SDLK_v:
- if(SDL_GetModState()&MOD_KEYS) { pasteconsole(); return; }
- // fall through
- default:
- resetcomplete();
- resetnameCompletion(); // added: to reset the name completion.
- if(cooked)
- {
- size_t len = (int)strlen(commandbuf);
- if(len+1<sizeof(commandbuf))
- {
- if(commandpos<0) commandbuf[len] = cooked;
- else
- {
- memmove(&commandbuf[commandpos+1], &commandbuf[commandpos], len - commandpos);
- commandbuf[commandpos++] = cooked;
- }
- commandbuf[len+1] = '\0';
- }
- }
- break;
- }
- }
- else
- {
- if(code==SDLK_RETURN || code==SDLK_KP_ENTER)
- {
- hline *h = NULL;
- if(commandbuf[0])
- {
- if(history.empty() || history.last()->shouldsave())
- {
- if(maxhistory && history.length() >= maxhistory)
- {
- loopi(history.length()-maxhistory+1) delete history[i];
- history.remove(0, history.length()-maxhistory+1);
- }
- history.add(h = new hline)->save();
- }
- else h = history.last();
- }
- histpos = history.length();
- inputcommand(NULL);
- resetnameCompletion(); // added: to reset the name completion.
- if(h) h->run();
- }
- else if(code==SDLK_ESCAPE)
- {
- histpos = history.length();
- inputcommand(NULL);
- resetnameCompletion(); // // added: to reset the name completion.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement