You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.1KB

  1. /* SpiralPlugin
  2. * Copyleft (C) 2000 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "KeyboardPluginGUI.h"
  19. #include <Fl/fl_draw.H>
  20. #include <FL/fl_file_chooser.H>
  21. #include <FL/Fl_Hold_Browser.H>
  22. #include <FL/Fl_Double_Window.H>
  23. #include <FL/Fl_Scroll.H>
  24. using namespace std;
  25. static int NKEYS = 30;
  26. static char KEYMAP[30]={'z','s','x','d','c','v','g','b','h','n','j','m','q',
  27. '2','w','3','e','r','5','t','6','y','7','u','i','9',
  28. 'o','0','p','['};
  29. ////////////////////////////////////////////////////////////////////////
  30. KeyboardPluginGUI::KeyboardPluginGUI(int w, int h,KeyboardPlugin *o,ChannelHandler *ch,const HostInfo *Info) :
  31. SpiralPluginGUI(w,h,o,ch),
  32. m_Last(-1),
  33. m_Oct(4)
  34. {
  35. Fl_Scroll *Scroll = new Fl_Scroll(2,20,w-4,h-20);
  36. Fl_Group *Group = new Fl_Group(0,20,500,h-40);
  37. Group->box(FL_FLAT_BOX);
  38. Group->user_data(this);
  39. Scroll->add(Group);
  40. int KeyWidth=10,Note,Pos=0,Count=0;
  41. for (int n=0; n<NUM_KEYS; n++)
  42. {
  43. m_Num[n]=n;
  44. Note = n%12;
  45. if (Note!=1 && Note!=3 && Note!=6 && Note!=8 && Note!=10)
  46. {
  47. Pos=Count*KeyWidth;
  48. Count++;
  49. m_Key[n] = new Fl_Button(Pos,20,KeyWidth,50,"");
  50. m_Key[n]->box(FL_THIN_UP_BOX);
  51. m_Key[n]->labelsize(10);
  52. m_Key[n]->when(FL_WHEN_CHANGED);
  53. if (Note==0)
  54. {
  55. int Num=n/12;
  56. sprintf(m_Label[n],"%d",Num);
  57. m_Key[n]->label(m_Label[n]);
  58. m_Key[n]->align(FL_ALIGN_BOTTOM|FL_ALIGN_INSIDE);
  59. }
  60. m_Key[n]->color(FL_WHITE);
  61. m_Key[n]->selection_color(FL_WHITE);
  62. m_Key[n]->callback((Fl_Callback*)cb_Key, &m_Num[n]);
  63. Group->add(m_Key[n]);
  64. }
  65. }
  66. Count=0;
  67. for (int n=0; n<NUM_KEYS; n++)
  68. {
  69. Note = n%12;
  70. if (Note==1 || Note==3 || Note==6 || Note==8 || Note==10)
  71. {
  72. m_Key[n] = new Fl_Button(Pos+5,20,KeyWidth,30,"");
  73. m_Key[n]->box(FL_THIN_UP_BOX);
  74. m_Key[n]->labelsize(10);
  75. m_Key[n]->when(FL_WHEN_CHANGED);
  76. m_Key[n]->color(FL_BLACK);
  77. m_Key[n]->selection_color(FL_BLACK);
  78. m_Key[n]->callback((Fl_Callback*)cb_Key, &m_Num[n]);
  79. Group->add(m_Key[n]);
  80. }
  81. else
  82. {
  83. Pos=Count*KeyWidth;
  84. Count++;
  85. }
  86. }
  87. Group->position(-100,20);
  88. Group->end();
  89. Scroll->end();
  90. }
  91. void KeyboardPluginGUI::Update()
  92. {
  93. int Volume=0,Note=0,EventDevice=0;
  94. if (Fl::event_key(FL_F+1)) m_Oct=0;
  95. if (Fl::event_key(FL_F+2)) m_Oct=1;
  96. if (Fl::event_key(FL_F+3)) m_Oct=2;
  97. if (Fl::event_key(FL_F+4)) m_Oct=3;
  98. if (Fl::event_key(FL_F+5)) m_Oct=4;
  99. if (Fl::event_key(FL_F+6)) m_Oct=5;
  100. if (Fl::event_key(FL_F+7)) m_Oct=6;
  101. if (Fl::event_key(FL_F+8)) m_Oct=7;
  102. if (Fl::event_key(FL_F+9)) m_Oct=8;
  103. if (Fl::event_key(FL_F+10)) m_Oct=9;
  104. if (Fl::event_key(FL_F+11)) m_Oct=10;
  105. int note=0;
  106. char KeyChar=0;
  107. bool KeyPressed=false;
  108. for (int key=0; key<NKEYS; key++)
  109. {
  110. KeyChar=KEYMAP[key];
  111. // check if a key's been pressed
  112. if (Fl::event_key(KeyChar))
  113. {
  114. KeyPressed=true;
  115. Volume = 127;
  116. Note=(m_Oct*12)+note;
  117. if (m_Last!=Note)
  118. {
  119. if (m_Last!=-1)
  120. {
  121. // turn off the old one
  122. m_Key[m_Last]->value(0);
  123. m_Key[m_Last]->parent()->redraw();
  124. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  125. m_GUICH->Wait();
  126. }
  127. m_Last = Note;
  128. m_GUICH->Set("Note",Note);
  129. m_GUICH->SetCommand(KeyboardPlugin::NOTE_ON);
  130. m_Key[Note]->value(1);
  131. m_Key[m_Last]->parent()->redraw();
  132. }
  133. }
  134. else // it's not pressed down
  135. {
  136. //see if the note was pressed down last time
  137. Note=(m_Oct*12)+note;
  138. if (m_Last==Note)
  139. {
  140. m_Key[m_Last]->value(0);
  141. m_Key[m_Last]->parent()->redraw();
  142. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  143. m_Last=-1;
  144. }
  145. }
  146. note++;
  147. }
  148. }
  149. void KeyboardPluginGUI::UpdateValues(SpiralPlugin *o)
  150. {
  151. KeyboardPlugin *Plugin = (KeyboardPlugin*)o;
  152. }
  153. //// Callbacks ////
  154. inline void KeyboardPluginGUI::cb_Key_i(Fl_Button* o, void* v)
  155. {
  156. int k=*(int*)(v);
  157. if (o->value())
  158. {
  159. m_GUICH->Set("Note",k);
  160. m_GUICH->SetCommand(KeyboardPlugin::NOTE_ON);
  161. }
  162. else
  163. {
  164. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  165. }
  166. parent()->redraw();
  167. }
  168. void KeyboardPluginGUI::cb_Key(Fl_Button* o, void* v)
  169. { ((KeyboardPluginGUI*)(o->parent()->user_data()))->cb_Key_i(o,v);}
  170. const string KeyboardPluginGUI::GetHelpText(const string &loc){
  171. return string("")
  172. + "This plugin provides you with a soft-keyboard if you don't have midi.\n"
  173. + "You can either use the mouse to play the keyboard GUI, or it will also\n"
  174. + "pick up PC keyboard presses in the ssm window.\n\n"
  175. + "The keyboard map follows the SoundTracker standard - i.e:\n"
  176. + "z=C4 s=C#4 x=D4 ... q=C5 2=C#5 w=D5...\n"
  177. + "The function keys change the octave.";
  178. }