home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.70.zip / src / gorder.c < prev    next >
C/C++ Source or Header  |  2008-04-01  |  14KB  |  622 lines

  1. //
  2. // GOATTRACKER v2 orderlist & songname editor
  3. //
  4.  
  5. #define GORDER_C
  6.  
  7. #include "goattrk2.h"
  8.  
  9. unsigned char trackcopybuffer[MAX_SONGLEN+2];
  10. int trackcopyrows = 0;
  11. int trackcopywhole;
  12. int trackcopyrpos;
  13.  
  14. int espos[MAX_CHN];
  15. int esend[MAX_CHN];
  16. int eseditpos;
  17. int esview;
  18. int escolumn;
  19. int eschn;
  20. int esnum;
  21. int esmarkchn = -1;
  22. int esmarkstart;
  23. int esmarkend;
  24. int enpos;
  25.  
  26. void orderlistcommands(void);
  27. void namecommands(void);
  28.  
  29. void orderlistcommands(void)
  30. {
  31.   int c, scrrep;
  32.  
  33.   if (hexnybble >= 0)
  34.   {
  35.     if (eseditpos != songlen[esnum][eschn])
  36.     {
  37.       switch(escolumn)
  38.       {
  39.         case 0:
  40.         songorder[esnum][eschn][eseditpos] &= 0x0f;
  41.         songorder[esnum][eschn][eseditpos] |= hexnybble << 4;
  42.         if (eseditpos < songlen[esnum][eschn])
  43.         {
  44.           if (songorder[esnum][eschn][eseditpos] >= MAX_PATT)
  45.             songorder[esnum][eschn][eseditpos] = MAX_PATT - 1;
  46.         }
  47.         else
  48.         {
  49.           if (songorder[esnum][eschn][eseditpos] >= MAX_SONGLEN)
  50.             songorder[esnum][eschn][eseditpos] = MAX_SONGLEN - 1;
  51.         }
  52.         break;
  53.  
  54.         case 1:
  55.         songorder[esnum][eschn][eseditpos] &= 0xf0;
  56.         if ((songorder[esnum][eschn][eseditpos] & 0xf0) == 0xd0)
  57.         {
  58.           hexnybble--;
  59.           if (hexnybble < 0) hexnybble = 0xf;
  60.         }
  61.         if ((songorder[esnum][eschn][eseditpos] & 0xf0) == 0xe0)
  62.         {
  63.           hexnybble = 16 - hexnybble;
  64.           hexnybble &= 0xf;
  65.         }
  66.         songorder[esnum][eschn][eseditpos] |= hexnybble;
  67.  
  68.         if (eseditpos < songlen[esnum][eschn])
  69.         {
  70.           if (songorder[esnum][eschn][eseditpos] == LOOPSONG)
  71.             songorder[esnum][eschn][eseditpos] = LOOPSONG-1;
  72.           if (songorder[esnum][eschn][eseditpos] == TRANSDOWN)
  73.             songorder[esnum][eschn][eseditpos] = TRANSDOWN+0x0f;
  74.         }
  75.         else
  76.         {
  77.           if (songorder[esnum][eschn][eseditpos] >= MAX_SONGLEN)
  78.             songorder[esnum][eschn][eseditpos] = MAX_SONGLEN - 1;
  79.         }
  80.         break;
  81.       }
  82.       escolumn++;
  83.       if (escolumn > 1)
  84.       {
  85.         escolumn = 0;
  86.         if (eseditpos < (songlen[esnum][eschn]+1))
  87.         {
  88.           eseditpos++;
  89.           if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  90.         }
  91.       }
  92.     }
  93.   }
  94.  
  95.   switch(key)
  96.   {
  97.     case 'R':
  98.     if (eseditpos < songlen[esnum][eschn])
  99.     {
  100.       songorder[esnum][eschn][eseditpos] = REPEAT + 0x01;
  101.       escolumn = 1;
  102.     }
  103.     break;
  104.  
  105.     case '+':
  106.     if (eseditpos < songlen[esnum][eschn])
  107.     {
  108.       songorder[esnum][eschn][eseditpos] = TRANSUP;
  109.       escolumn = 1;
  110.     }
  111.     break;
  112.  
  113.     case '-':
  114.     if (eseditpos < songlen[esnum][eschn])
  115.     {
  116.       songorder[esnum][eschn][eseditpos] = TRANSDOWN + 0x0F;
  117.       escolumn = 1;
  118.     }
  119.     break;
  120.  
  121.     case '>':
  122.     case ')':
  123.     case ']':
  124.     nextsong();
  125.     break;
  126.  
  127.     case '<':
  128.     case '(':
  129.     case '[':
  130.     prevsong();
  131.     break;
  132.   }
  133.   switch(rawkey)
  134.   {
  135.     case KEY_1:
  136.     case KEY_2:
  137.     case KEY_3:
  138.     if (shiftpressed)
  139.     {
  140.       int schn = eschn;
  141.       int tchn = 0;
  142.  
  143.       esmarkchn = -1;
  144.       if (rawkey == KEY_1) tchn = 0;
  145.       if (rawkey == KEY_2) tchn = 1;
  146.       if (rawkey == KEY_3) tchn = 2;
  147.       if (schn != tchn)
  148.       {
  149.         for (c = 0; c < MAX_SONGLEN+2; c++)
  150.         {
  151.           unsigned char temp = songorder[esnum][schn][c];
  152.           songorder[esnum][schn][c] = songorder[esnum][tchn][c];
  153.           songorder[esnum][tchn][c] = temp;
  154.         }
  155.       }
  156.     }
  157.     break;
  158.  
  159.     case KEY_X:
  160.     if (shiftpressed)
  161.     {
  162.       if (esmarkchn != -1)
  163.       {
  164.         int d = 0;
  165.  
  166.         eschn = esmarkchn;
  167.         if (esmarkstart <= esmarkend)
  168.         {
  169.           eseditpos = esmarkstart;
  170.           for (c = esmarkstart; c <= esmarkend; c++)
  171.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  172.           trackcopyrows = d;
  173.         }
  174.         else
  175.         {
  176.           eseditpos = esmarkend;
  177.           for (c = esmarkend; c <= esmarkstart; c++)
  178.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  179.           trackcopyrows = d;
  180.         }
  181.         if (trackcopyrows == songlen[esnum][eschn])
  182.         {
  183.           trackcopywhole = 1;
  184.           trackcopyrpos = songorder[esnum][eschn][songlen[esnum][eschn]+1];
  185.         }
  186.         else trackcopywhole = 0;
  187.         for (c = 0; c < trackcopyrows; c++) deleteorder();
  188.         esmarkchn = -1;
  189.       }
  190.     }
  191.     break;
  192.  
  193.     case KEY_C:
  194.     if (shiftpressed)
  195.     {
  196.       if (esmarkchn != -1)
  197.       {
  198.         int d = 0;
  199.         if (esmarkstart <= esmarkend)
  200.         {
  201.           for (c = esmarkstart; c <= esmarkend; c++)
  202.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  203.           trackcopyrows = d;
  204.         }
  205.         else
  206.         {
  207.           for (c = esmarkend; c <= esmarkstart; c++)
  208.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  209.           trackcopyrows = d;
  210.         }
  211.         if (trackcopyrows == songlen[esnum][eschn])
  212.         {
  213.           trackcopywhole = 1;
  214.           trackcopyrpos = songorder[esnum][eschn][songlen[esnum][eschn]+1];
  215.         }
  216.         else trackcopywhole = 0;
  217.         esmarkchn = -1;
  218.       }
  219.     }
  220.     break;
  221.  
  222.     case KEY_V:
  223.     if (shiftpressed)
  224.     {
  225.       int oldlen = songlen[esnum][eschn];
  226.       
  227.       if (eseditpos < songlen[esnum][eschn])
  228.       {
  229.         for (c = trackcopyrows-1; c >= 0; c--)
  230.           insertorder(trackcopybuffer[c]);
  231.       }
  232.       else
  233.       {
  234.         for (c = 0; c < trackcopyrows; c++)
  235.           insertorder(trackcopybuffer[c]);
  236.       }
  237.       if ((trackcopywhole) && (!oldlen))
  238.         songorder[esnum][eschn][songlen[esnum][eschn]+1] = trackcopyrpos;
  239.     }
  240.     break;
  241.  
  242.     case KEY_L:
  243.     if (shiftpressed)
  244.     {
  245.       if (esmarkchn == -1)
  246.       {
  247.         esmarkchn = eschn;
  248.         esmarkstart = 0;
  249.         esmarkend = songlen[esnum][eschn]-1;
  250.       }
  251.       else esmarkchn = -1;
  252.     }
  253.     break;
  254.  
  255.  
  256.     case KEY_SPACE:
  257.     if (!shiftpressed)
  258.     {
  259.       if (eseditpos < songlen[esnum][eschn]) espos[eschn] = eseditpos;
  260.       if (esend[eschn] < espos[eschn]) esend[eschn] = 0;
  261.     }
  262.     else
  263.     {
  264.         for (c = 0; c < MAX_CHN; c++)
  265.         {
  266.         if (eseditpos < songlen[esnum][c]) espos[c] = eseditpos;
  267.         if (esend[c] < espos[c]) esend[c] = 0;
  268.       }
  269.     }
  270.     break;
  271.  
  272.     case KEY_BACKSPACE:
  273.     if (!shiftpressed)
  274.     {
  275.         if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn]))
  276.         {
  277.         if (eseditpos < songlen[esnum][eschn]) esend[eschn] = eseditpos;
  278.       }
  279.       else esend[eschn] = 0;
  280.     }
  281.     else
  282.     {
  283.         if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn]))
  284.         {
  285.           for (c = 0; c < MAX_CHN; c++)
  286.           {
  287.           if (eseditpos < songlen[esnum][c]) esend[c] = eseditpos;
  288.         }
  289.       }
  290.       else
  291.       {
  292.           for (c = 0; c < MAX_CHN; c++) esend[c] = 0;
  293.         }
  294.     }
  295.     break;
  296.  
  297.     case KEY_ENTER:
  298.     if (eseditpos < songlen[esnum][eschn])
  299.     {
  300.       if (!shiftpressed)
  301.       {
  302.         if (songorder[esnum][eschn][eseditpos] < MAX_PATT)
  303.           epnum[eschn] = songorder[esnum][eschn][eseditpos];
  304.       }
  305.       else
  306.       {
  307.         int c, d;
  308.  
  309.         for (c = 0; c < MAX_CHN; c++)
  310.         {
  311.           int start;
  312.  
  313.           if (eseditpos != espos[eschn]) start = eseditpos;
  314.           else start = espos[c];
  315.  
  316.           for (d = start; d < songlen[esnum][c]; d++)
  317.           {
  318.             if (songorder[esnum][c][d] < MAX_PATT)
  319.             {
  320.                 epnum[c] = songorder[esnum][c][d];
  321.                 break;
  322.             }
  323.           }
  324.         }
  325.       }
  326.       epmarkchn = -1;
  327.     }
  328.     epchn = eschn;
  329.     epcolumn = 0;
  330.     eppos = 0;
  331.     epview = - VISIBLEPATTROWS/2;
  332.     editmode = EDIT_PATTERN;
  333.     if (epchn == epmarkchn) epmarkchn = -1;
  334.     break;
  335.  
  336.     case KEY_DEL:
  337.     esmarkchn = -1;
  338.     deleteorder();
  339.     break;
  340.  
  341.     case KEY_INS:
  342.     esmarkchn = -1;
  343.     insertorder(0);
  344.     break;
  345.  
  346.     case KEY_HOME:
  347.     if (songlen[esnum][eschn])
  348.     {
  349.       while ((eseditpos != 0) || (escolumn != 0)) orderleft();
  350.     }
  351.     break;
  352.  
  353.     case KEY_END:
  354.     while (eseditpos != songlen[esnum][eschn]+1) orderright();
  355.     break;
  356.  
  357.     case KEY_PGUP:
  358.     for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--)
  359.       orderleft();
  360.     break;
  361.  
  362.     case KEY_PGDN:
  363.     for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--)
  364.       orderright();
  365.     break;
  366.  
  367.     case KEY_LEFT:
  368.     orderleft();
  369.     break;
  370.  
  371.     case KEY_RIGHT:
  372.     orderright();
  373.     break;
  374.  
  375.     case KEY_UP:
  376.     eschn--;
  377.     if (eschn < 0) eschn = MAX_CHN - 1;
  378.     if ((eseditpos == songlen[esnum][eschn]) || (eseditpos > songlen[esnum][eschn]+1))
  379.     {
  380.       eseditpos = songlen[esnum][eschn]+1;
  381.       escolumn = 0;
  382.     }
  383.     if (shiftpressed) esmarkchn = -1;
  384.     break;
  385.  
  386.     case KEY_DOWN:
  387.     eschn++;
  388.     if (eschn >= MAX_CHN) eschn = 0;
  389.     if ((eseditpos == songlen[esnum][eschn]) || (eseditpos > songlen[esnum][eschn]+1))
  390.     {
  391.       eseditpos = songlen[esnum][eschn]+1;
  392.       escolumn = 0;
  393.     }
  394.     if (shiftpressed) esmarkchn = -1;
  395.     break;
  396.   }
  397.   if (eseditpos - esview < 0)
  398.   {
  399.     esview = eseditpos;
  400.   }
  401.   if (eseditpos - esview >= VISIBLEORDERLIST)
  402.   {
  403.     esview = eseditpos - VISIBLEORDERLIST + 1;
  404.   }
  405. }
  406.  
  407. void namecommands(void)
  408. {
  409.   switch(rawkey)
  410.   {
  411.     case KEY_DOWN:
  412.     case KEY_ENTER:
  413.     enpos++;
  414.     if (enpos > 2) enpos = 0;
  415.     break;
  416.  
  417.     case KEY_UP:
  418.     enpos--;
  419.     if (enpos < 0) enpos = 2;
  420.     break;
  421.   }
  422.   switch(enpos)
  423.   {
  424.     case 0:
  425.     editstring(songname, MAX_STR);
  426.     break;
  427.  
  428.     case 1:
  429.     editstring(authorname, MAX_STR);
  430.     break;
  431.  
  432.     case 2:
  433.     editstring(copyrightname, MAX_STR);
  434.     break;
  435.   }
  436. }
  437.  
  438. void insertorder(unsigned char byte)
  439. {
  440.   if ((songlen[esnum][eschn] - eseditpos)-1 >= 0)
  441.   {
  442.     int len;
  443.     if (songlen[esnum][eschn] < MAX_SONGLEN)
  444.     {
  445.       len = songlen[esnum][eschn]+1;
  446.       songorder[esnum][eschn][len+1] =
  447.         songorder[esnum][eschn][len];
  448.       songorder[esnum][eschn][len] = LOOPSONG;
  449.       if (len) songorder[esnum][eschn][len-1] = byte;
  450.       countthispattern();
  451.     }
  452.     memmove(&songorder[esnum][eschn][eseditpos+1],
  453.       &songorder[esnum][eschn][eseditpos],
  454.       (songlen[esnum][eschn] - eseditpos)-1);
  455.     songorder[esnum][eschn][eseditpos] = byte;
  456.     len = songlen[esnum][eschn]+1;
  457.     if ((songorder[esnum][eschn][len] > eseditpos) &&
  458.        (songorder[esnum][eschn][len] < (len-2)))
  459.        songorder[esnum][eschn][len]++;
  460.   }
  461.   else
  462.   {
  463.     if (eseditpos > songlen[esnum][eschn])
  464.     {
  465.       if (songlen[esnum][eschn] < MAX_SONGLEN)
  466.       {
  467.         songorder[esnum][eschn][eseditpos+1] =
  468.           songorder[esnum][eschn][eseditpos];
  469.         songorder[esnum][eschn][eseditpos] = LOOPSONG;
  470.         if (eseditpos) songorder[esnum][eschn][eseditpos-1] = byte;
  471.         countthispattern();
  472.         eseditpos = songlen[esnum][eschn]+1;
  473.       }
  474.     }
  475.   }
  476. }
  477.  
  478. void deleteorder(void)
  479. {
  480.   if ((songlen[esnum][eschn] - eseditpos)-1 >= 0)
  481.   {
  482.     int len;
  483.     memmove(&songorder[esnum][eschn][eseditpos],
  484.       &songorder[esnum][eschn][eseditpos+1],
  485.       (songlen[esnum][eschn] - eseditpos)-1);
  486.     songorder[esnum][eschn][songlen[esnum][eschn]-1] = 0x00;
  487.     if (songlen[esnum][eschn] > 0)
  488.     {
  489.       songorder[esnum][eschn][songlen[esnum][eschn]-1] =
  490.         songorder[esnum][eschn][songlen[esnum][eschn]];
  491.       songorder[esnum][eschn][songlen[esnum][eschn]] =
  492.         songorder[esnum][eschn][songlen[esnum][eschn]+1];
  493.       countthispattern();
  494.     }
  495.     if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  496.     len = songlen[esnum][eschn]+1;
  497.     if ((songorder[esnum][eschn][len] > eseditpos) &&
  498.        (songorder[esnum][eschn][len] > 0))
  499.        songorder[esnum][eschn][len]--;
  500.   }
  501.   else
  502.   {
  503.     if (eseditpos > songlen[esnum][eschn])
  504.     {
  505.       if (songlen[esnum][eschn] > 0)
  506.       {
  507.         songorder[esnum][eschn][songlen[esnum][eschn]-1] =
  508.           songorder[esnum][eschn][songlen[esnum][eschn]];
  509.         songorder[esnum][eschn][songlen[esnum][eschn]] =
  510.           songorder[esnum][eschn][songlen[esnum][eschn]+1];
  511.         countthispattern();
  512.         eseditpos = songlen[esnum][eschn]+1;
  513.       }
  514.     }
  515.   }
  516. }
  517.  
  518. void orderleft(void)
  519. {
  520.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn]))
  521.   {
  522.     if ((esmarkchn != eschn) || (eseditpos != esmarkend))
  523.     {
  524.       esmarkchn = eschn;
  525.       esmarkstart = esmarkend = eseditpos;
  526.     }
  527.   }
  528.   escolumn--;
  529.   if (escolumn < 0)
  530.   {
  531.     if (eseditpos > 0)
  532.     {
  533.       eseditpos--;
  534.       if (eseditpos == songlen[esnum][eschn]) eseditpos--;
  535.       escolumn = 1;
  536.       if (eseditpos < 0)
  537.       {
  538.         eseditpos = 1;
  539.         escolumn = 0;
  540.       }
  541.     }
  542.     else escolumn = 0;
  543.   }
  544.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos;
  545. }
  546.  
  547. void orderright(void)
  548. {
  549.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn]))
  550.   {
  551.     if ((esmarkchn != eschn) || (eseditpos != esmarkend))
  552.     {
  553.       esmarkchn = eschn;
  554.       esmarkstart = esmarkend = eseditpos;
  555.     }
  556.   }
  557.   escolumn++;
  558.   if (escolumn > 1)
  559.   {
  560.     escolumn = 0;
  561.     if (eseditpos < (songlen[esnum][eschn]+1))
  562.     {
  563.       eseditpos++;
  564.       if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  565.     }
  566.     else escolumn = 1;
  567.   }
  568.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos;
  569. }
  570.  
  571. void nextsong(void)
  572. {
  573.   esnum++;
  574.   if (esnum >= MAX_SONGS) esnum = MAX_SONGS - 1;
  575.   songchange();
  576. }
  577.  
  578. void prevsong(void)
  579. {
  580.   esnum--;
  581.   if (esnum < 0) esnum = 0;
  582.   songchange();
  583. }
  584.  
  585. void songchange(void)
  586. {
  587.   int c;
  588.   
  589.   for (c = 0; c < MAX_CHN; c++)
  590.   {
  591.     espos[c] = 0;
  592.     esend[c] = 0;
  593.     epnum[c] = c;
  594.   }
  595.   updateviewtopos();
  596.  
  597.   eppos = 0;
  598.   epview = - VISIBLEPATTROWS/2;
  599.   eseditpos = 0;
  600.   if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  601.   esview = 0;
  602.   epmarkchn = -1;
  603.   esmarkchn = -1;
  604.   stopsong();
  605. }
  606.  
  607. void updateviewtopos(void)
  608. {
  609.   int c, d;
  610.   for (c = 0; c < MAX_CHN; c++)
  611.   {
  612.     for (d = espos[c]; d < songlen[esnum][c]; d++)
  613.     {
  614.       if (songorder[esnum][c][d] < MAX_PATT)
  615.       {
  616.           epnum[c] = songorder[esnum][c][d];
  617.           break;
  618.       }
  619.     }
  620.   }
  621. }
  622.