home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.71_stereo.zip / src / gorder.c < prev    next >
C/C++ Source or Header  |  2010-10-21  |  15KB  |  628 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.     case KEY_4:
  139.     case KEY_5:
  140.     case KEY_6:
  141.     if (shiftpressed)
  142.     {
  143.       int schn = eschn;
  144.       int tchn = 0;
  145.  
  146.       esmarkchn = -1;
  147.       if (rawkey == KEY_1) tchn = 0;
  148.       if (rawkey == KEY_2) tchn = 1;
  149.       if (rawkey == KEY_3) tchn = 2;
  150.       if (rawkey == KEY_4) tchn = 3;
  151.       if (rawkey == KEY_5) tchn = 4;
  152.       if (rawkey == KEY_6) tchn = 5;
  153.       if (schn != tchn)
  154.       {
  155.         for (c = 0; c < MAX_SONGLEN+2; c++)
  156.         {
  157.           unsigned char temp = songorder[esnum][schn][c];
  158.           songorder[esnum][schn][c] = songorder[esnum][tchn][c];
  159.           songorder[esnum][tchn][c] = temp;
  160.         }
  161.       }
  162.     }
  163.     break;
  164.  
  165.     case KEY_X:
  166.     if (shiftpressed)
  167.     {
  168.       if (esmarkchn != -1)
  169.       {
  170.         int d = 0;
  171.  
  172.         eschn = esmarkchn;
  173.         if (esmarkstart <= esmarkend)
  174.         {
  175.           eseditpos = esmarkstart;
  176.           for (c = esmarkstart; c <= esmarkend; c++)
  177.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  178.           trackcopyrows = d;
  179.         }
  180.         else
  181.         {
  182.           eseditpos = esmarkend;
  183.           for (c = esmarkend; c <= esmarkstart; c++)
  184.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  185.           trackcopyrows = d;
  186.         }
  187.         if (trackcopyrows == songlen[esnum][eschn])
  188.         {
  189.           trackcopywhole = 1;
  190.           trackcopyrpos = songorder[esnum][eschn][songlen[esnum][eschn]+1];
  191.         }
  192.         else trackcopywhole = 0;
  193.         for (c = 0; c < trackcopyrows; c++) deleteorder();
  194.         esmarkchn = -1;
  195.       }
  196.     }
  197.     break;
  198.  
  199.     case KEY_C:
  200.     if (shiftpressed)
  201.     {
  202.       if (esmarkchn != -1)
  203.       {
  204.         int d = 0;
  205.         if (esmarkstart <= esmarkend)
  206.         {
  207.           for (c = esmarkstart; c <= esmarkend; c++)
  208.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  209.           trackcopyrows = d;
  210.         }
  211.         else
  212.         {
  213.           for (c = esmarkend; c <= esmarkstart; c++)
  214.             trackcopybuffer[d++] = songorder[esnum][eschn][c];
  215.           trackcopyrows = d;
  216.         }
  217.         if (trackcopyrows == songlen[esnum][eschn])
  218.         {
  219.           trackcopywhole = 1;
  220.           trackcopyrpos = songorder[esnum][eschn][songlen[esnum][eschn]+1];
  221.         }
  222.         else trackcopywhole = 0;
  223.         esmarkchn = -1;
  224.       }
  225.     }
  226.     break;
  227.  
  228.     case KEY_V:
  229.     if (shiftpressed)
  230.     {
  231.       int oldlen = songlen[esnum][eschn];
  232.       
  233.       if (eseditpos < songlen[esnum][eschn])
  234.       {
  235.         for (c = trackcopyrows-1; c >= 0; c--)
  236.           insertorder(trackcopybuffer[c]);
  237.       }
  238.       else
  239.       {
  240.         for (c = 0; c < trackcopyrows; c++)
  241.           insertorder(trackcopybuffer[c]);
  242.       }
  243.       if ((trackcopywhole) && (!oldlen))
  244.         songorder[esnum][eschn][songlen[esnum][eschn]+1] = trackcopyrpos;
  245.     }
  246.     break;
  247.  
  248.     case KEY_L:
  249.     if (shiftpressed)
  250.     {
  251.       if (esmarkchn == -1)
  252.       {
  253.         esmarkchn = eschn;
  254.         esmarkstart = 0;
  255.         esmarkend = songlen[esnum][eschn]-1;
  256.       }
  257.       else esmarkchn = -1;
  258.     }
  259.     break;
  260.  
  261.  
  262.     case KEY_SPACE:
  263.     if (!shiftpressed)
  264.     {
  265.       if (eseditpos < songlen[esnum][eschn]) espos[eschn] = eseditpos;
  266.       if (esend[eschn] < espos[eschn]) esend[eschn] = 0;
  267.     }
  268.     else
  269.     {
  270.       for (c = 0; c < MAX_CHN; c++)
  271.       {
  272.         if (eseditpos < songlen[esnum][c]) espos[c] = eseditpos;
  273.         if (esend[c] < espos[c]) esend[c] = 0;
  274.       }
  275.     }
  276.     break;
  277.  
  278.     case KEY_BACKSPACE:
  279.     if (!shiftpressed)
  280.     {
  281.       if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn]))
  282.       {
  283.         if (eseditpos < songlen[esnum][eschn]) esend[eschn] = eseditpos;
  284.       }
  285.       else esend[eschn] = 0;
  286.     }
  287.     else
  288.     {
  289.       if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn]))
  290.       {
  291.         for (c = 0; c < MAX_CHN; c++)
  292.         {
  293.           if (eseditpos < songlen[esnum][c]) esend[c] = eseditpos;
  294.         }
  295.       }
  296.       else
  297.       {
  298.         for (c = 0; c < MAX_CHN; c++) esend[c] = 0;
  299.       }
  300.     }
  301.     break;
  302.  
  303.     case KEY_ENTER:
  304.     if (eseditpos < songlen[esnum][eschn])
  305.     {
  306.       if (!shiftpressed)
  307.       {
  308.         if (songorder[esnum][eschn][eseditpos] < MAX_PATT)
  309.           epnum[eschn] = songorder[esnum][eschn][eseditpos];
  310.       }
  311.       else
  312.       {
  313.         int c, d;
  314.  
  315.         for (c = 0; c < MAX_CHN; c++)
  316.         {
  317.           int start;
  318.  
  319.           if (eseditpos != espos[eschn]) start = eseditpos;
  320.           else start = espos[c];
  321.  
  322.           for (d = start; d < songlen[esnum][c]; d++)
  323.           {
  324.             if (songorder[esnum][c][d] < MAX_PATT)
  325.             {
  326.               epnum[c] = songorder[esnum][c][d];
  327.               break;
  328.             }
  329.           }
  330.         }
  331.       }
  332.       epmarkchn = -1;
  333.     }
  334.     epchn = eschn;
  335.     epcolumn = 0;
  336.     eppos = 0;
  337.     epview = - VISIBLEPATTROWS/2;
  338.     editmode = EDIT_PATTERN;
  339.     if (epchn == epmarkchn) epmarkchn = -1;
  340.     break;
  341.  
  342.     case KEY_DEL:
  343.     esmarkchn = -1;
  344.     deleteorder();
  345.     break;
  346.  
  347.     case KEY_INS:
  348.     esmarkchn = -1;
  349.     insertorder(0);
  350.     break;
  351.  
  352.     case KEY_HOME:
  353.     if (songlen[esnum][eschn])
  354.     {
  355.       while ((eseditpos != 0) || (escolumn != 0)) orderleft();
  356.     }
  357.     break;
  358.  
  359.     case KEY_END:
  360.     while (eseditpos != songlen[esnum][eschn]+1) orderright();
  361.     break;
  362.  
  363.     case KEY_PGUP:
  364.     for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--)
  365.       orderleft();
  366.     break;
  367.  
  368.     case KEY_PGDN:
  369.     for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--)
  370.       orderright();
  371.     break;
  372.  
  373.     case KEY_LEFT:
  374.     orderleft();
  375.     break;
  376.  
  377.     case KEY_RIGHT:
  378.     orderright();
  379.     break;
  380.  
  381.     case KEY_UP:
  382.     eschn--;
  383.     if (eschn < 0) eschn = MAX_CHN - 1;
  384.     if ((eseditpos == songlen[esnum][eschn]) || (eseditpos > songlen[esnum][eschn]+1))
  385.     {
  386.       eseditpos = songlen[esnum][eschn]+1;
  387.       escolumn = 0;
  388.     }
  389.     if (shiftpressed) esmarkchn = -1;
  390.     break;
  391.  
  392.     case KEY_DOWN:
  393.     eschn++;
  394.     if (eschn >= MAX_CHN) eschn = 0;
  395.     if ((eseditpos == songlen[esnum][eschn]) || (eseditpos > songlen[esnum][eschn]+1))
  396.     {
  397.       eseditpos = songlen[esnum][eschn]+1;
  398.       escolumn = 0;
  399.     }
  400.     if (shiftpressed) esmarkchn = -1;
  401.     break;
  402.   }
  403.   if (eseditpos - esview < 0)
  404.   {
  405.     esview = eseditpos;
  406.   }
  407.   if (eseditpos - esview >= VISIBLEORDERLIST)
  408.   {
  409.     esview = eseditpos - VISIBLEORDERLIST + 1;
  410.   }
  411. }
  412.  
  413. void namecommands(void)
  414. {
  415.   switch(rawkey)
  416.   {
  417.     case KEY_DOWN:
  418.     case KEY_ENTER:
  419.     enpos++;
  420.     if (enpos > 2) enpos = 0;
  421.     break;
  422.  
  423.     case KEY_UP:
  424.     enpos--;
  425.     if (enpos < 0) enpos = 2;
  426.     break;
  427.   }
  428.   switch(enpos)
  429.   {
  430.     case 0:
  431.     editstring(songname, MAX_STR);
  432.     break;
  433.  
  434.     case 1:
  435.     editstring(authorname, MAX_STR);
  436.     break;
  437.  
  438.     case 2:
  439.     editstring(copyrightname, MAX_STR);
  440.     break;
  441.   }
  442. }
  443.  
  444. void insertorder(unsigned char byte)
  445. {
  446.   if ((songlen[esnum][eschn] - eseditpos)-1 >= 0)
  447.   {
  448.     int len;
  449.     if (songlen[esnum][eschn] < MAX_SONGLEN)
  450.     {
  451.       len = songlen[esnum][eschn]+1;
  452.       songorder[esnum][eschn][len+1] =
  453.         songorder[esnum][eschn][len];
  454.       songorder[esnum][eschn][len] = LOOPSONG;
  455.       if (len) songorder[esnum][eschn][len-1] = byte;
  456.       countthispattern();
  457.     }
  458.     memmove(&songorder[esnum][eschn][eseditpos+1],
  459.       &songorder[esnum][eschn][eseditpos],
  460.       (songlen[esnum][eschn] - eseditpos)-1);
  461.     songorder[esnum][eschn][eseditpos] = byte;
  462.     len = songlen[esnum][eschn]+1;
  463.     if ((songorder[esnum][eschn][len] > eseditpos) &&
  464.        (songorder[esnum][eschn][len] < (len-2)))
  465.        songorder[esnum][eschn][len]++;
  466.   }
  467.   else
  468.   {
  469.     if (eseditpos > songlen[esnum][eschn])
  470.     {
  471.       if (songlen[esnum][eschn] < MAX_SONGLEN)
  472.       {
  473.         songorder[esnum][eschn][eseditpos+1] =
  474.           songorder[esnum][eschn][eseditpos];
  475.         songorder[esnum][eschn][eseditpos] = LOOPSONG;
  476.         if (eseditpos) songorder[esnum][eschn][eseditpos-1] = byte;
  477.         countthispattern();
  478.         eseditpos = songlen[esnum][eschn]+1;
  479.       }
  480.     }
  481.   }
  482. }
  483.  
  484. void deleteorder(void)
  485. {
  486.   if ((songlen[esnum][eschn] - eseditpos)-1 >= 0)
  487.   {
  488.     int len;
  489.     memmove(&songorder[esnum][eschn][eseditpos],
  490.       &songorder[esnum][eschn][eseditpos+1],
  491.       (songlen[esnum][eschn] - eseditpos)-1);
  492.     songorder[esnum][eschn][songlen[esnum][eschn]-1] = 0x00;
  493.     if (songlen[esnum][eschn] > 0)
  494.     {
  495.       songorder[esnum][eschn][songlen[esnum][eschn]-1] =
  496.         songorder[esnum][eschn][songlen[esnum][eschn]];
  497.       songorder[esnum][eschn][songlen[esnum][eschn]] =
  498.         songorder[esnum][eschn][songlen[esnum][eschn]+1];
  499.       countthispattern();
  500.     }
  501.     if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  502.     len = songlen[esnum][eschn]+1;
  503.     if ((songorder[esnum][eschn][len] > eseditpos) &&
  504.        (songorder[esnum][eschn][len] > 0))
  505.        songorder[esnum][eschn][len]--;
  506.   }
  507.   else
  508.   {
  509.     if (eseditpos > songlen[esnum][eschn])
  510.     {
  511.       if (songlen[esnum][eschn] > 0)
  512.       {
  513.         songorder[esnum][eschn][songlen[esnum][eschn]-1] =
  514.           songorder[esnum][eschn][songlen[esnum][eschn]];
  515.         songorder[esnum][eschn][songlen[esnum][eschn]] =
  516.           songorder[esnum][eschn][songlen[esnum][eschn]+1];
  517.         countthispattern();
  518.         eseditpos = songlen[esnum][eschn]+1;
  519.       }
  520.     }
  521.   }
  522. }
  523.  
  524. void orderleft(void)
  525. {
  526.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn]))
  527.   {
  528.     if ((esmarkchn != eschn) || (eseditpos != esmarkend))
  529.     {
  530.       esmarkchn = eschn;
  531.       esmarkstart = esmarkend = eseditpos;
  532.     }
  533.   }
  534.   escolumn--;
  535.   if (escolumn < 0)
  536.   {
  537.     if (eseditpos > 0)
  538.     {
  539.       eseditpos--;
  540.       if (eseditpos == songlen[esnum][eschn]) eseditpos--;
  541.       escolumn = 1;
  542.       if (eseditpos < 0)
  543.       {
  544.         eseditpos = 1;
  545.         escolumn = 0;
  546.       }
  547.     }
  548.     else escolumn = 0;
  549.   }
  550.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos;
  551. }
  552.  
  553. void orderright(void)
  554. {
  555.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn]))
  556.   {
  557.     if ((esmarkchn != eschn) || (eseditpos != esmarkend))
  558.     {
  559.       esmarkchn = eschn;
  560.       esmarkstart = esmarkend = eseditpos;
  561.     }
  562.   }
  563.   escolumn++;
  564.   if (escolumn > 1)
  565.   {
  566.     escolumn = 0;
  567.     if (eseditpos < (songlen[esnum][eschn]+1))
  568.     {
  569.       eseditpos++;
  570.       if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  571.     }
  572.     else escolumn = 1;
  573.   }
  574.   if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos;
  575. }
  576.  
  577. void nextsong(void)
  578. {
  579.   esnum++;
  580.   if (esnum >= MAX_SONGS) esnum = MAX_SONGS - 1;
  581.   songchange();
  582. }
  583.  
  584. void prevsong(void)
  585. {
  586.   esnum--;
  587.   if (esnum < 0) esnum = 0;
  588.   songchange();
  589. }
  590.  
  591. void songchange(void)
  592. {
  593.   int c;
  594.   
  595.   for (c = 0; c < MAX_CHN; c++)
  596.   {
  597.     espos[c] = 0;
  598.     esend[c] = 0;
  599.     epnum[c] = c;
  600.   }
  601.   updateviewtopos();
  602.  
  603.   eppos = 0;
  604.   epview = - VISIBLEPATTROWS/2;
  605.   eseditpos = 0;
  606.   if (eseditpos == songlen[esnum][eschn]) eseditpos++;
  607.   esview = 0;
  608.   epmarkchn = -1;
  609.   esmarkchn = -1;
  610.   stopsong();
  611. }
  612.  
  613. void updateviewtopos(void)
  614. {
  615.   int c, d;
  616.   for (c = 0; c < MAX_CHN; c++)
  617.   {
  618.     for (d = espos[c]; d < songlen[esnum][c]; d++)
  619.     {
  620.       if (songorder[esnum][c][d] < MAX_PATT)
  621.       {
  622.         epnum[c] = songorder[esnum][c][d];
  623.         break;
  624.       }
  625.     }
  626.   }
  627. }
  628.