Advertisement
ZoriaRPG

TypeAString.zh (v1.2, Venrob)

Mar 1st, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.99 KB | None | 0 0
  1. /*
  2.     Script Header: TypeAString.zh
  3.     Author: Venrob
  4.     Version: 1.2
  5. */
  6.  
  7. typedef const int DEFINE;
  8.  
  9. DEFINE MAX_LENGTH = 8192;
  10. DEFINE MAX_CHARS_PER_FRAME = 2;
  11.  
  12. int keyboardInput[MAX_LENGTH];
  13. int typing_vars[9];
  14.  
  15. DEFINE KEY_FINAL = KEY_SEMICOLON; //Last input key to check
  16.  
  17. DEFINE TVAR_TYPING = 0;
  18. DEFINE TVAR_MAXCHARS = 1;
  19. DEFINE TVAR_CAPSLOCK = 2;
  20. DEFINE TVAR_INDEX = 3;
  21. DEFINE TVAR_ALLOWSYMBOLS = 4;
  22. DEFINE TVAR_ENTERENDS = 5;
  23. DEFINE TVAR_TYPING_ENDED = 6;
  24. DEFINE TVAR_BACKSPACEANDDELETE = 7;
  25. DEFINE TVAR_WRAP = 8;
  26. //DEFINE TVAR_LEFTRIGHT = 9; /Planned feature: Left/Right arrows will move the index, and you will be able to type in-between already typed letters.
  27. //DEFINE TVAR_
  28.  
  29. int typing_allowed_symbols[] = {'"','!','@','#','$','%','^','&','*','(',')','-','_','=','+','[',']','{','}',';',':',',','.','<','>','/','?','|','`','~',CHAR_QUOTE,CHAR_BSLASH};
  30.  
  31. //Begin taking keyboard input, up to maxChars characters. Allow symbols from the allowed symbols table if "allowSymbols" is true.
  32. void startTypingMode(int maxChars, bool allowSymbols)
  33. {
  34.     clearTypedInput();
  35.     __setTvar(TVAR_TYPING,1);
  36.     Game->TypingMode = true;
  37.     __setTvar(TVAR_MAXCHARS,Clamp(maxChars,0,MAX_LENGTH));
  38.     if(allowSymbols)__setTvar(TVAR_ALLOWSYMBOLS,1);
  39.     else __setTvar(TVAR_ALLOWSYMBOLS,0);
  40. }
  41.  
  42. //Begin taking keyboard input, up to maxChars characters. Only alphanumeric.
  43. void startTypingMode(int maxChars)
  44. {
  45.     startTypingMode(maxChars,false);
  46. }
  47.  
  48. //Begin taking keyboard input, up to 128 chars. Only alphanumeric.
  49. void startTypingMode()
  50. {
  51.     startTypingMode(128,false);
  52. }
  53.  
  54. //End typing mode
  55. void endTypingMode()
  56. {
  57.     __setTvar(TVAR_TYPING,0);
  58.     Game->TypingMode = false;
  59.     __setTvar(TVAR_TYPING_ENDED,1);
  60. }
  61.  
  62. void handleTyping()
  63. {
  64.     if(__getTvar(TVAR_TYPING_ENDED))__setTvar(TVAR_TYPING_ENDED,0);
  65.     if(!__getTvar(TVAR_TYPING))return;
  66.     if(__getTvar(TVAR_BACKSPACEANDDELETE))
  67.     {
  68.         if(ReadKey(KEY_BACKSPACE))
  69.         {
  70.             __incTvar(TVAR_INDEX,-1);
  71.             __setTvar(TVAR_INDEX,Clamp(__getTvar(TVAR_INDEX),0,MAX_INT));
  72.             keyboardInput[__getTvar(TVAR_INDEX)] = 0;
  73.             __reorder();
  74.         }
  75.         if(ReadKey(KEY_DEL) || ReadKey(KEY_DEL_PAD))
  76.         {
  77.             keyboardInput[__getTvar(TVAR_INDEX)] = 0;
  78.             __reorder();
  79.         }
  80.     }
  81.     if(__getTvar(TVAR_ENTERENDS) && (KeyPress(KEY_ENTER) || KeyPress(KEY_ENTER_PAD)))
  82.     {
  83.         __setTvar(TVAR_TYPING_ENDED,1);
  84.         __setTvar(TVAR_TYPING,0);
  85.         Game->TypingMode = false;
  86.         return;
  87.     }
  88.     if(__getTvar(TVAR_INDEX)>=__getTvar(TVAR_MAXCHARS))
  89.     {
  90.         if(__getTvar(TVAR_WRAP))__setTvar(TVAR_INDEX,0);//If max characters specified is reached, wrap around
  91.         else return;
  92.     }
  93.     if(CapsLock())__setTvar(TVAR_CAPSLOCK,(__getTvar(TVAR_CAPSLOCK) + 1) % 2);
  94.     bool shift = PressShift();
  95.     int localBuf[MAX_CHARS_PER_FRAME + 1]; //+1 to ensure a NULL terminator is present
  96.     int localIndex = 0;
  97.     for(int i = KEY_A; i <= KEY_FINAL && localIndex < MAX_CHARS_PER_FRAME; i++)
  98.     {
  99.         if(!ReadKey(i))continue;
  100.         int char = KeyToChar(i,shift);
  101.         if(__getTvar(TVAR_CAPSLOCK))char = LowerToUpper(char);
  102.         bool valid = isAlphaNumeric(char);
  103.         if(!valid)
  104.         {
  105.             for(int q = SizeOfArray(typing_allowed_symbols)-1; q>=0; q--)
  106.             {
  107.                 if(char == typing_allowed_symbols[q])
  108.                 {
  109.                     valid = true;
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.         if(valid)
  115.         {
  116.             localBuf[localIndex] = char;
  117.             localIndex++;
  118.         }
  119.     }
  120.     int startindex = __getTvar(TVAR_INDEX)
  121.     for(int index = startindex; index < __getTvar(TVAR_MAXCHARS); index++)
  122.     {
  123.         if(localBuf[index-startindex]!=0)
  124.         {
  125.             keyboardInput[index] = localBuf[index-startindex];
  126.             __incTvar(TVAR_INDEX,1);
  127.         }
  128.     }
  129.     //strcpy(keyboardInput,__getTvar(TVAR_INDEX),localBuf,__getTvar(TVAR_MAXCHARS)-__getTvar(TVAR_INDEX));
  130.     //__incTvar(TVAR_INDEX,localIndex);
  131. }
  132.  
  133. //Example of how it works. Load this into any .QST as Slot 2, then type stuff. Pressing ENTER will write what you typed to the console. Feel free to play around with the settings.
  134. //By default, BACKSPACE and DELETE will work as well. The character max is 8 (in "startTypingMode(8)"). "int buf[9]" should be one larger than this.
  135. global script typingExample
  136. {
  137.     void run()
  138.     {
  139.         TraceNL();TraceNL();
  140.         //Settings:
  141.         setEnterEndsTyping(true);
  142.         setAllowBackspaceDelete(true);
  143.         setOverflowWraps(false);
  144.         //
  145.         startTypingMode(8);
  146.         while(true)
  147.         {
  148.             handleTyping();
  149.             if(typingJustEnded())
  150.             {
  151.                 int buf[9];
  152.                 getType(buf);
  153.                 TraceS(buf);
  154.                 startTypingMode(8);
  155.             }
  156.             Waitdraw();
  157.             Waitframe();
  158.         }
  159.     }
  160. }
  161.  
  162. global script typingBasic
  163. {
  164.     void run()
  165.     {
  166.         //You should set all three of these depending on what settings you want.
  167.         setEnterEndsTyping(true);
  168.         setAllowBackspaceDelete(true);
  169.         setOverflowWraps(false);
  170.         //
  171.         while(true)
  172.         {
  173.             //handleTyping() should be the FIRST thing in your while(true), so that it can cancel Link's input if typing is active.
  174.             handleTyping();
  175.             Waitdraw();
  176.             Waitframe();
  177.         }
  178.     }
  179. }
  180.  
  181. //Pressing ENTER will exit typing mode
  182. void setEnterEndsTyping(bool allow)
  183. {
  184.     __setTvar(TVAR_ENTERENDS,Cond(allow,1,0));
  185. }
  186.  
  187. //Backspace and Delete will function, removing characters.
  188. void setAllowBackspaceDelete(bool allow)
  189. {
  190.     __setTvar(TVAR_BACKSPACEANDDELETE,Cond(allow,1,0));
  191. }
  192.  
  193. //When you type the final character which fits in the alloted space, it will set your cursor back to the beginning.
  194. void setOverflowWraps(bool allow)
  195. {
  196.     __setTvar(TVAR_WRAP,Cond(allow,1,0));
  197. }
  198.  
  199. //Like Waitframe(), but instead of waiting for the next frame, it waits for the player to finish typing.
  200. //Do NOT call this from a global script, or you WILL crash your global script.
  201. void Waittype()
  202. {
  203.     while(__getTvar(TVAR_TYPING))Waitframe();
  204. }
  205.  
  206. //Like the above, but SPECIFICALLY for global scripts.
  207. //Calling this from any other script will have undefined effects, and is not recommended.
  208. //This will stop any other global scripts you have running aside from this one. Keep this in mind.
  209. void WaittypeGlobal()
  210. {
  211.     while(__getTvar(TVAR_TYPING))
  212.     {
  213.         Waitframe();
  214.         handleTyping();
  215.     }
  216. }
  217.  
  218. //Returns true if typing ended within the last frame.
  219. bool typingJustEnded()
  220. {
  221.     return __getTvar(TVAR_TYPING_ENDED)>0;
  222. }
  223.  
  224. //Get a section of text into a buffer, with various overloads.
  225. //Gets ALL text input
  226. void getType(int buf)
  227. {
  228.     getType(buf, 0, __getTvar(TVAR_MAXCHARS));
  229. }
  230.  
  231. //Gets all text starting at index "start", inclusive.
  232. void getType(int buf, int start)
  233. {
  234.     getType(buf, start, __getTvar(TVAR_MAXCHARS));
  235. }
  236.  
  237. //Gets all input between index "start" and "end", both inclusive.
  238. void getType(int buf, int start, int end)
  239. {
  240.     int bufsize = SizeOfArray(buf);
  241.     for(int i = 0; i<=end-start && keyboardInput[i+start] != 0 && i < bufsize; i++)
  242.     {
  243.         buf[i] = keyboardInput[i+start];
  244.     }
  245. }
  246.  
  247. //Gets the required length of the buffer for the given text, for the given start/end values to the above functions.
  248. int getTypeLength(int start, int end)
  249. {
  250.     int i = 0;
  251.     while(i<=end-start && keyboardInput[i+start] != 0)i++;
  252.     return i+1; //+1 for NULL terminator character
  253. }
  254.  
  255. int getTypeLength(int start)
  256. {
  257.     getTypeLength(start, __getTvar(TVAR_MAXCHARS));
  258. }
  259.  
  260. int getTypeLength()
  261. {
  262.     getTypeLength(0,__getTvar(TVAR_MAXCHARS));
  263. }
  264.  
  265. //Clear the input buffer, clearing all stored text.
  266. void clearTypedInput()
  267. {
  268.     for(int i = 0; keyboardInput[i]!=0; i++)
  269.     {
  270.         keyboardInput[i]=0;
  271.     }
  272.     __setTvar(TVAR_INDEX,0);
  273. }
  274.  
  275. //Clear ALL the input in the buffer, regardless of NULL characters.
  276. //If something is getting stuck, use this; it is slower than clearTypedInput, but more thourough.
  277. //If you need this, there is a good chance you are doing something wrong.
  278. void __forceClearInput()
  279. {
  280.     for(int i = 0; i<MAX_LENGTH; i++)
  281.     {
  282.         keyboardInput[i]=0;
  283.     }
  284.     __setTvar(TVAR_INDEX,0);
  285. }
  286.  
  287. void __setTvar(int index, int value)
  288. {
  289.     typing_vars[index] = value;
  290. }
  291.  
  292. int __getTvar(int index)
  293. {
  294.     return typing_vars[index];
  295. }
  296.  
  297. void __incTvar(int index, int incBy){
  298.     typing_vars[index] += incBy;
  299. }
  300.  
  301. void __reorder()
  302. {
  303.     bool foundNULL = false;
  304.     for(int i = 0; i < MAX_LENGTH-1; i++)
  305.     {
  306.         if(keyboardInput[i]==0)foundNULL=true;
  307.         if(foundNULL)
  308.         {
  309.             keyboardInput[i] = keyboardInput[i+1];
  310.             if(keyboardInput[i] == 0)return;
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement