Advertisement
ZoriaRPG

Keyboard_Parsing_Test.zh for 2.54 (Alpha 21+), Version 8

Dec 25th, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.98 KB | None | 0 0
  1. import "std.zh"
  2. //rev 8
  3. //the cursor adjustment is not fast-enough to keep up with rapidly typed characters, or the timing is incorrect.
  4. //move the cursor djustments into the functions that insert char into the buffer! -Z (25th Dec, 2017 @ 15:30GMT)
  5.  
  6. const int SFX_KEYPRESS = 58;           
  7.  
  8. const int LINKTILEOFFSET = -261; const int KEY_DELAY = 6; //Trying this as 6.5 and timer changes as 1.0, using 65 and 10
  9. const int LINE_LENGTH = 54; const int BUFFER_LENGTH = 55;
  10. //int b1[55]; int b2[55]; int b3[55]; int b4[55]; int b5[55];
  11. //int b6[55]; int b7[55]; int b8[55]; int b9[55]; int b10[55];
  12. //int bufptr;
  13.  
  14. //Very fast typing can lock the timer in such a way that CursorAdvance() stops working, and the last typed char
  15. //overwrites the previous typed char. Why? I have no clue. -Z (25th Dec, 2017)
  16. //Specifically, ram[ram_q] stops advancing.
  17. //This seems to have been an issue with function calls not setting ram[ram_q]++ fast enough???
  18.  
  19. const int CHAR_WIDTH = 4;
  20. const int CURSOR_WIDTH = 4;
  21. const int CURSOR_POINT_CHAR = 62;
  22. const int CURSOR_Y_OFFSET = 4;
  23. const int CURSOR_X_OFFSET = 4;
  24. const int BUFFER_OUTPUT_X = 0;
  25. const int BUFFER_OUTPUT_Y = 0;
  26. const int CURSOR_BLINK_DUR = 70;
  27. const int CURSOR_Y_OFS = 1;
  28. const int TAB_WIDTH = 4; //number of spaces in a tab keypress.
  29.         //! The actual 'CHAR_TAB' shows as '^' in some fonts, hence doing it this way.
  30.  
  31. int ram[64]; const int RAM_q = 0; const int RAM_lastchar = 1; const int RAM_cursorspacing = 2; const int RAM_cursorposition = 3;
  32.  
  33.  
  34. int buffer[214747]; //={CURSOR_POINT_CHAR, CHAR_SPACE};
  35. int bufferoverlay[214747]; //handles the cursor.
  36. bool ShiftKey;//deprecated
  37. //int curbuffer = 0;
  38. int q; int timer = KEY_DELAY; bool cantype = true;
  39.  
  40. int temp_q; //the current index of the buffer string that we are modifying.
  41. int cursorblink = 0;
  42.  
  43. int command_buffer[256];
  44.  
  45.  
  46. //CursorAdv(RAM_q);
  47. void CursorAdv(int n){
  48.     TraceS("CursorAdvance: ram[RAM_cursorposition] is IMMEDIATE: "); Trace(ram[RAM_cursorposition]);
  49.     if ( ram[RAM_cursorposition] == SizeOfArray(ram) ) return;
  50.     bufferoverlay[ram[n]] = CHAR_SPACE;
  51.     ram[n]++; //the current index of the cursor.
  52.     //bufferoverlay[ram[n]] = '_'; //Cursor.
  53.     ram[RAM_cursorposition] += ram[RAM_cursorspacing];
  54.     TraceS("CursorAdvance: ram[RAM_cursorposition] is AFTER: "); Trace(ram[RAM_cursorposition]);
  55.    
  56. }
  57.  
  58. //CursorRev(RAM_q);
  59. void CursorRev(int n){
  60.     TraceS("CursorReverse: ram[RAM_cursorposition] is IMMEDIATE: "); Trace(ram[RAM_cursorposition]);
  61.     //if ( ram[RAM_cursorposition] == 0 ) return;
  62.     bufferoverlay[ram[n]] = 0;
  63.     ram[n]--;
  64.     if ( ram[RAM_cursorposition] >= ram[RAM_cursorspacing] ) ram[RAM_cursorposition] -= ram[RAM_cursorspacing];
  65.     if ( ram[RAM_cursorposition] < 0 ) ram[RAM_cursorposition] = 0;
  66.     TraceS("CursorReverse: ram[RAM_cursorposition] is AFTER: "); Trace(ram[RAM_cursorposition]);
  67.     //bufferoverlay[ram[n]] = '_'; //Cursor.
  68. }
  69.  
  70. void ResetCursor(){
  71.     //bufferoverlay[ram[]] = 0;
  72.     ram[RAM_q] = 0;
  73.     ram[RAM_cursorposition] = 0;
  74.     ram[RAM_cursorspacing] = 0;
  75. }
  76.  
  77. //Put char into buffer for normal keypresses:
  78. void NormalKeyPresses()
  79. {
  80.     int k;
  81.     int LegalKeys[]= {
  82.     KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O,
  83.     KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z, KEY_0, KEY_1, KEY_2, KEY_3,
  84.     KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0_PAD, KEY_1_PAD, KEY_2_PAD, KEY_3_PAD, KEY_4_PAD, KEY_5_PAD,
  85.     KEY_6_PAD, KEY_7_PAD, KEY_8_PAD, KEY_9_PAD, KEY_TILDE, KEY_MINUS, KEY_EQUALS, KEY_OPENBRACE, KEY_CLOSEBRACE,
  86.     KEY_COLON, KEY_QUOTE, KEY_BACKSLASH, KEY_BACKSLASH2, KEY_COMMA, KEY_SEMICOLON, KEY_SLASH, KEY_SPACE, KEY_SLASH_PAD,
  87.     KEY_ASTERISK, KEY_MINUS_PAD, KEY_PLUS_PAD, KEY_CIRCUMFLEX, KEY_COLON2, KEY_EQUALS_PAD, KEY_STOP };
  88.  
  89.     int sz = SizeOfArray(LegalKeys);
  90.    
  91.     for ( int kk = 0; kk < sz; kk++ )
  92.     {
  93.         k = LegalKeys[kk];
  94.         if ( Input->Key[k] )
  95.         {
  96.             if ( ram[RAM_q] < 214746 )
  97.             {
  98.                 TraceS("temp_q is: "); Trace(temp_q);
  99.                 TraceS("ram[RAM_q] is: "); Trace(ram[RAM_q]);
  100.                 buffer[ram[RAM_q]] = KeyToChar(k,( Input->Key[KEY_LSHIFT] || Input->Key[KEY_RSHIFT] || Input->Key[KEY_CAPSLOCK] ),true);
  101.                 TraceS("character is: "); Trace( buffer[ram[RAM_q]] );
  102.                 TraceS("key press registered: "); Trace(k);
  103.                 //CursorAdv(RAM_q);
  104.                 temp_q++;
  105.                 ram[RAM_q]++; //the current index of the cursor.
  106.                 TraceS("CursorAdvance: ram[RAM_cursorposition] is IMMEDIATE: "); Trace(ram[RAM_cursorposition]);
  107.    
  108.                 bufferoverlay[ram[RAM_q]] = CHAR_SPACE;
  109.    
  110.                 ram[RAM_cursorposition] += ram[RAM_cursorspacing];
  111.                 TraceS("CursorAdvance: ram[RAM_cursorposition] is AFTER: "); Trace(ram[RAM_cursorposition]);
  112.                 Game->PlaySound(SFX_KEYPRESS);
  113.                 timer = KEY_DELAY;
  114.             }
  115.         }
  116.        
  117.     }
  118. }
  119.  
  120.  
  121. global script a{
  122.     void run(){
  123.        
  124.        
  125.  
  126. //int buffers[]={b1, b2, b3, b4, b5, b6, b7, b8, b9, b10};
  127.     //bufptr = buffers;
  128.     //int curbuffer = bufptr[ram[RAM_q]];
  129.         Link->Y = 90;
  130.         //Game->Cheat = 4;
  131.         while(true){
  132.             if ( PressControl() && Input->Key[KEY_I] ) Game->TypingMode = true;
  133.            
  134.             if ( timer > 0 ) cantype = false;
  135.             if ( timer <= 0 ) cantype = true;
  136.             if ( timer > 0 ) timer --;
  137.             if ( timer <= 0 ) timer = 0;
  138.             if ( cursorblink > 0 ) cursorblink--;
  139.             if ( cursorblink <= 0 ) cursorblink = 100;
  140.             if ( cantype && Game->TypingMode ) {
  141.                 if ( !PressControl() )
  142.                 {
  143.                    
  144.                    
  145.                     NormalKeyPresses();
  146.                    
  147.                     if ( Input->Key[KEY_BACKSPACE] ){ //backspace key
  148.                     //Do not permit backspace is we reach the first character of the buffer!
  149.                         if ( ram[RAM_q] < 214746 && ram[RAM_q] > 0 ) {
  150.                             TraceS("Prior to reversing cursor");
  151.                             TraceS("temp_q is: "); Trace(temp_q);
  152.                             TraceS("ram[RAM_q] is: "); Trace(ram[RAM_q]);
  153.                             CursorRev(RAM_q); temp_q--; buffer[ram[RAM_q]] = 0; //clear the last character typed.
  154.                             TraceS("Reversed cursor");
  155.                             TraceS("temp_q is: "); Trace(temp_q);
  156.                             TraceS("ram[RAM_q] is: "); Trace(ram[RAM_q]);
  157.                             Game->PlaySound(SFX_KEYPRESS);
  158.                             timer = KEY_DELAY;
  159.                         }
  160.                     }  
  161.                    
  162.                     if ( ram[RAM_q] < 214746 )
  163.                     {
  164.                     //tilde -- this might have issues unless we can set uncap false.
  165.                    
  166.                         //Special actions for non-character keys.
  167.                            
  168.  
  169.                         if ( Input->Key[KEY_TAB] ){ //tab
  170.                             for ( int w = 0; w < TAB_WIDTH; w++ )
  171.                             {
  172.                                 buffer[ram[RAM_q]] = CHAR_SPACE;
  173.                                 CursorAdv(RAM_q); temp_q++;
  174.                             }
  175.                             Game->PlaySound(SFX_KEYPRESS);
  176.                             timer = KEY_DELAY;
  177.                         }
  178.                        
  179.                         //Enter key goes here.
  180.                         if ( Input->Key[KEY_ENTER] || Input->Key[KEY_ENTER_PAD] ){ //enqueue
  181.                             int ps;
  182.                             while ( ps < 256 )
  183.                             {
  184.                                 if ( buffer[ps] == 0 ) break;
  185.                                 command_buffer[ps] = buffer[ps];
  186.                                 ++ps;
  187.                             }
  188.                            
  189.                             Game->PlaySound(SFX_KEYPRESS);
  190.                             timer = KEY_DELAY;
  191.                             TraceS(command_buffer); //after this is in the command buffer,
  192.                                         //we will copy the to the STACK
  193.                                         //and clear this buffer.
  194.                                         //Sending text to this buffer should
  195.                                         //temporarily block further KB input
  196.                                         //setting (cantype) false.
  197.                                         // ( 25th Dec, 2017 " 15:45GMT )
  198.                         }
  199.                    
  200.                        
  201.                    
  202.                    
  203.                         if ( Input->Key[KEY_DEL] ){ //delete
  204.                             for ( int w = ram[RAM_q]; w >= 0; w-- ) buffer[w] = 0;
  205.                             Game->PlaySound(SFX_KEYPRESS);
  206.                             ResetCursor(); temp_q = 0;
  207.                             timer = KEY_DELAY;
  208.                         }
  209.                    
  210.                         if ( Input->Key[KEY_LEFT] ){ //leftarrow
  211.                             //ram[RAM_q]--;
  212.                            
  213.                             TraceS("Prior to reversing cursor");
  214.                             TraceS("temp_q is: "); Trace(temp_q);
  215.                             TraceS("ram[RAM_q] is: "); Trace(ram[RAM_q]);
  216.                            
  217.                             CursorRev(RAM_q);
  218.                            
  219.                             TraceS("Reversed cursor");
  220.                             TraceS("temp_q is: "); Trace(temp_q);
  221.                             TraceS("ram[RAM_q] is: "); Trace(ram[RAM_q]);
  222.                             Game->PlaySound(SFX_KEYPRESS);//temp_q--;
  223.                             timer = KEY_DELAY;
  224.                         }
  225.                         if ( Input->Key[KEY_RIGHT] ){ //leftarrow
  226.                             CursorAdv(RAM_q);
  227.                             //ram[RAM_q]++;
  228.                             Game->PlaySound(SFX_KEYPRESS);//temp_q--; //needs to operate on temp_q so that the highest position is recorded.
  229.                             timer = KEY_DELAY;
  230.                         }
  231.                    
  232.                         if ( Input->Key[KEY_UP] ){ //up
  233.                             //find the previous command in the list
  234.                             /* if ( ram[RAM_q] < 214746 ) {
  235.                                 if ( PressShift() ){
  236.                                 {   buffer[ram[RAM_q]] = '|'; q++; temp_q++; if ( SFX_KEYPRESS > 0 ) Game->PlaySound(SFX_KEYPRESS);}
  237.                                 else { buffer[ram[RAM_q]] = CHAR_BSLASH; q++; temp_q++; if ( SFX_KEYPRESS > 0 ) Game->PlaySound(SFX_KEYPRESS);}
  238.                                 timer = KEY_DELAY;
  239.                             }
  240.                             */
  241.                         }
  242.                         if ( Input->Key[KEY_DOWN] ){ //up
  243.                             //find the next command in the list, clear if none
  244.                             /* if ( ram[RAM_q] < 214746 ) {
  245.                                 if ( PressShift() ){
  246.                                 {   buffer[ram[RAM_q]] = '|'; q++; temp_q++; if ( SFX_KEYPRESS > 0 ) Game->PlaySound(SFX_KEYPRESS);}
  247.                                 else { buffer[ram[RAM_q]] = CHAR_BSLASH; q++; temp_q++; if ( SFX_KEYPRESS > 0 ) Game->PlaySound(SFX_KEYPRESS);}
  248.                                 timer = KEY_DELAY;
  249.                             }
  250.                             */
  251.                         }
  252.                    
  253.                         if ( Input->Key[KEY_ENTER_PAD] ){
  254.                             for ( int w = q; w >= 0; w-- ) buffer[w] = 0;
  255.                             Game->PlaySound(SFX_KEYPRESS);
  256.                             q = 0; temp_q = 0;
  257.                             timer = KEY_DELAY;
  258.                         }
  259.                    
  260.                    
  261.                         //for ( int qqq = 0; qqram[RAM_q] < SizeOfArray(bufferoverlay); qqq++ ) bufferoverlay[ram[RAM_q]] = CHAR_SPACE;
  262.                         //bufferoverlay[ram[RAM_q]] = '_'; //Cursor.
  263.                     }
  264.                 }
  265.             }
  266.            
  267.             //Adjust the cursor position based on the size of the last typed character.
  268.             if ( ram[RAM_q] > 0 ) {
  269.                 ram[RAM_lastchar] = buffer[ram[RAM_q]-1];
  270.                 ram[RAM_cursorspacing] = GetCursorSpacing(ram[RAM_lastchar]);
  271.             }
  272.             //If we backspace all the way, rfeser the cursor position to 0 in all respects.
  273.             if ( ram[RAM_q] == 0 ) {
  274.                 ram[RAM_lastchar] = 0;
  275.                 ram[RAM_cursorspacing] = 0;
  276.                
  277.             }
  278.            
  279.             /*
  280.             if ( timer == KEY_DELAY ) {
  281.                 Trace(ram[RAM_lastchar]);
  282.                 Trace(GetCursorSpacing(ram[RAM_lastchar]));
  283.                 Trace(ram[RAM_cursorspacing]);
  284.                 Trace(ram[RAM_cursorposition]);
  285.             }
  286.             */
  287.                
  288.             if ( Game->TypingMode )
  289.             {
  290.                 Screen->DrawString(6,0,0,2, 0x01, -1, 0, GetBuffer(),128);
  291.    
  292.                 if ( cursorblink > CURSOR_BLINK_DUR ) {
  293.                     int cursor[] = "_";
  294.                     Screen->DrawString(6,ram[RAM_cursorposition]+ram[RAM_cursorspacing],0+CURSOR_Y_OFS,2, 0x01, -1, 0, cursor,128); //cursor
  295.                     //Screen->DrawString(6,0,0+CURSOR_Y_OFS,2, 0x01, -1, 0, GetCursorOverlay(),128); //cursor
  296.                     /*
  297.                     Screen->Line(6,
  298.                         0+0+(q*CHAR_WIDTH),
  299.                         //BUFFER_OUTPUT_X+CURSOR_X_OFFSET+(q*CHAR_WIDTH),
  300.                         //BUFFER_OUTPUT_Y+CURSOR_Y_OFFSET,
  301.                         6,
  302.                         0+0+(q*CHAR_WIDTH)+4,
  303.                         //BUFFER_OUTPUT_X+CURSOR_X_OFFSET+(q*CHAR_WIDTH) + CURSOR_WIDTH,
  304.                         6,
  305.                         //BUFFER_OUTPUT_Y+CURSOR_Y_OFFSET,
  306.                         0x01, 1, 0, 0, 0, 128);
  307.                     */
  308.                 }  
  309.                 if ( PressControl() && Input->Key[KEY_C] ) { Game->TypingMode = false; }
  310.                 //if ( Input->Key[KEY_F6] )
  311.                 //{
  312.                 //  Input->Key[KEY_F6] = false; //works.
  313.                 //  TraceS("Pressed F6");
  314.                 //}
  315.             }
  316.             else
  317.             {
  318.                 Screen->DrawString(6,0,0,2, 0x01, -1, 0, "Press Ctl+I to insert text, Ctl+C to break",128);
  319.             }
  320.             if ( Input->Key[KEY_F6] ) TraceS("Pressed F6");
  321.             Waitdraw(); Waitframe();
  322.         }
  323.     }
  324. }  
  325.  
  326. int GetCursorSpacing(int chr, int font){
  327.     if ( font == 0 ) {
  328.         int spacing[]={
  329.             0, 0, 0, 0, 0, 0, 0, 0, 0, 16, //tab
  330.             0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  331.             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //(29)
  332.             0, 0, // (31)
  333.             4, 2, 4, 6, 6, 4, 5, 2, 3, 3,
  334.             4, 4, 3, 4, 2, 0, 4, 3, 4, 4, //literal 3
  335.             4, 4, 4, 4, 4, 4, 2, 2, 4, 4, //equals
  336.             4, 4, 5, //@ sign
  337.             //A
  338.             5, 5, 4, 5, 4, 4, 5, 4, 2, 4,
  339.             //K
  340.             5, 4, 6, 6, 5, 5, 5, 5, 5, 4,
  341.             //U
  342.             5, 6, 6, 6, 4, 4, //Z
  343.             //lbrace
  344.             4, 5, 4, 4, 4, 4,
  345.             //a
  346.             5, 5, 4, 5, 4, 4, 5, 4, 2, 4,
  347.             //k
  348.             5, 4, 6, 6, 5, 5, 5, 5, 5, 4,
  349.             //u
  350.             5, 6, 6, 4, 4, 4, //z
  351.             //{
  352.             4, 2, 4, 5,
  353.             //127
  354.             0,
  355.             //Forward Del
  356.             0
  357.         };
  358.         return spacing[chr];
  359.     }
  360. }
  361.  
  362. int GetCursorSpacing(int chr){
  363.     //Custom kerning... Organised by English-language, and C-Programming linguistic frequency for short-circuiting,
  364.      
  365.     if ( chr == ' ' ) return 4;
  366.    
  367.     if ( chr == 'E' ) return 4; if ( chr == 'T' ) return 4; if ( chr == 'A' ) return 5; if ( chr == 'O' ) return 5; if ( chr == 'I' ) return 2;
  368.     if ( chr == 'N' ) return 6; if ( chr == 'S' ) return 5; if ( chr == 'H' ) return 4; if ( chr == 'R' ) return 5; if ( chr == 'D' ) return 5;
  369.     if ( chr == 'L' ) return 4; if ( chr == 'C' ) return 4; if ( chr == 'U' ) return 5; if ( chr == 'M' ) return 6; if ( chr == 'W' ) return 6;
  370.     if ( chr == 'F' ) return 4; if ( chr == 'G' ) return 5; if ( chr == 'Y' ) return 4; if ( chr == 'P' ) return 5; if ( chr == 'B' ) return 5;  
  371.     if ( chr == 'V' ) return 6; if ( chr == 'K' ) return 5; if ( chr == 'J' ) return 4; if ( chr == 'X' ) return 6; if ( chr == 'Q' ) return 5;
  372.     if ( chr == 'Z' ) return 4;
  373.    
  374.     if ( chr == 'e' ) return 4; if ( chr == 't' ) return 4; if ( chr == 'a' ) return 5; if ( chr == 'o' ) return 5; if ( chr == 'i' ) return 2;
  375.     if ( chr == 'n' ) return 6; if ( chr == 's' ) return 5; if ( chr == 'h' ) return 4; if ( chr == 'r' ) return 5; if ( chr == 'd' ) return 5;
  376.     if ( chr == 'l' ) return 4; if ( chr == 'c' ) return 4; if ( chr == 'u' ) return 5; if ( chr == 'm' ) return 6; if ( chr == 'w' ) return 6;
  377.     if ( chr == 'f' ) return 4; if ( chr == 'g' ) return 5; if ( chr == 'y' ) return 4; if ( chr == 'p' ) return 5; if ( chr == 'b' ) return 5;
  378.     if ( chr == 'v' ) return 6; if ( chr == 'k' ) return 5; if ( chr == 'j' ) return 4; if ( chr == 'x' ) return 4; if ( chr == 'q' ) return 5;
  379.     if ( chr == 'z' ) return 4;
  380.    
  381.     if ( chr == '.' ) return 2; if ( chr == '!' ) return 2; if ( chr == ',' ) return 3; if ( chr == '?' ) return 4; if ( chr == ':' ) return 2;
  382.    
  383.     if ( chr == '1' ) return 3; if ( chr == '2' ) return 4; if ( chr == '3' ) return 4; if ( chr == '4' ) return 4; if ( chr == '5' ) return 4;
  384.     if ( chr == '6' ) return 4; if ( chr == '7' ) return 4; if ( chr == '8' ) return 4; if ( chr == '9' ) return 4; if ( chr == '0' ) return 4;
  385.    
  386.     if ( chr == ';' ) return 2; if ( chr == '(' ) return 3; if ( chr == ')' ) return 3; if ( chr == '-' ) return 4;
  387.    
  388.     if ( chr == CHAR_QUOTE ) return 2; //Zeelda to be a parser bug. Comparing the buffer character ' ' ' to 'A' returned 5.
  389.     if ( chr == CHAR_DQUOTE ) return 4;
  390.    
  391.     if ( chr == '{' ) return 4; if ( chr == '}' ) return 4; if ( chr == '&' ) return 5; if ( chr == '|' ) return 2; if ( chr == '_' ) return 4;
  392.     if ( chr == '=' ) return 4; if ( chr == '*' ) return 4; if ( chr == '+' ) return 4; if ( chr == '/' ) return 4; if ( chr == '%' ) return 4;
  393.     if ( chr == '<' ) return 4; if ( chr == '>' ) return 4; if ( chr == '[' ) return 4; if ( chr == ']' ) return 4; if ( chr == '~' ) return 5;
  394.      
  395.    
  396.     if ( chr == '@' ) return 5; if ( chr == '#' ) return 6; if ( chr == '$' ) return 6; if ( chr == CHAR_POUNDS ) return 5;  if ( chr == '^' ) return 4;
  397.      
  398.     if ( chr == CHAR_BSLASH ) return 5; if ( chr == '`' ) return 4;
  399.    
  400. }
  401.    
  402. int GetBuffer(){
  403.     //int ptr = bufptr;
  404.     //return bufptr[buffer];
  405.     return buffer;
  406.    
  407. }  
  408.  
  409. int GetCursorOverlay(){
  410.     return bufferoverlay;
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement