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.

197 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. static int NKEYS = 30;
  25. static char KEYMAP[30]={'z','s','x','d','c','v','g','b','h','n','j','m','q',
  26. '2','w','3','e','r','5','t','6','y','7','u','i','9',
  27. 'o','0','p','['};
  28. ////////////////////////////////////////////////////////////////////////
  29. KeyboardPluginGUI::KeyboardPluginGUI(int w, int h,KeyboardPlugin *o,ChannelHandler *ch,const HostInfo *Info) :
  30. SpiralPluginGUI(w,h,o,ch),
  31. m_Last(-1),
  32. m_Oct(4)
  33. {
  34. Fl_Scroll *Scroll = new Fl_Scroll(2,20,w-4,h-20);
  35. Fl_Group *Group = new Fl_Group(0,20,500,h-40);
  36. Group->box(FL_FLAT_BOX);
  37. Group->user_data(this);
  38. Scroll->add(Group);
  39. int KeyWidth=10,Note,Pos=0,Count=0;
  40. for (int n=0; n<NUM_KEYS; n++)
  41. {
  42. m_Num[n]=n;
  43. Note = n%12;
  44. if (Note!=1 && Note!=3 && Note!=6 && Note!=8 && Note!=10)
  45. {
  46. Pos=Count*KeyWidth;
  47. Count++;
  48. m_Key[n] = new Fl_Button(Pos,20,KeyWidth,50,"");
  49. m_Key[n]->box(FL_THIN_UP_BOX);
  50. m_Key[n]->labelsize(10);
  51. m_Key[n]->when(FL_WHEN_CHANGED);
  52. if (Note==0)
  53. {
  54. int Num=n/12;
  55. sprintf(m_Label[n],"%d",Num);
  56. m_Key[n]->label(m_Label[n]);
  57. m_Key[n]->align(FL_ALIGN_BOTTOM|FL_ALIGN_INSIDE);
  58. }
  59. m_Key[n]->color(FL_WHITE);
  60. m_Key[n]->selection_color(FL_WHITE);
  61. m_Key[n]->callback((Fl_Callback*)cb_Key, &m_Num[n]);
  62. Group->add(m_Key[n]);
  63. }
  64. }
  65. Count=0;
  66. for (int n=0; n<NUM_KEYS; n++)
  67. {
  68. Note = n%12;
  69. if (Note==1 || Note==3 || Note==6 || Note==8 || Note==10)
  70. {
  71. m_Key[n] = new Fl_Button(Pos+5,20,KeyWidth,30,"");
  72. m_Key[n]->box(FL_THIN_UP_BOX);
  73. m_Key[n]->labelsize(10);
  74. m_Key[n]->when(FL_WHEN_CHANGED);
  75. m_Key[n]->color(FL_BLACK);
  76. m_Key[n]->selection_color(FL_BLACK);
  77. m_Key[n]->callback((Fl_Callback*)cb_Key, &m_Num[n]);
  78. Group->add(m_Key[n]);
  79. }
  80. else
  81. {
  82. Pos=Count*KeyWidth;
  83. Count++;
  84. }
  85. }
  86. Group->position(-100,20);
  87. Group->end();
  88. Scroll->end();
  89. }
  90. void KeyboardPluginGUI::Update()
  91. {
  92. int Volume=0,Note=0,EventDevice=0;
  93. if (Fl::event_key(FL_F+1)) m_Oct=0;
  94. if (Fl::event_key(FL_F+2)) m_Oct=1;
  95. if (Fl::event_key(FL_F+3)) m_Oct=2;
  96. if (Fl::event_key(FL_F+4)) m_Oct=3;
  97. if (Fl::event_key(FL_F+5)) m_Oct=4;
  98. if (Fl::event_key(FL_F+6)) m_Oct=5;
  99. if (Fl::event_key(FL_F+7)) m_Oct=6;
  100. if (Fl::event_key(FL_F+8)) m_Oct=7;
  101. if (Fl::event_key(FL_F+9)) m_Oct=8;
  102. if (Fl::event_key(FL_F+10)) m_Oct=9;
  103. if (Fl::event_key(FL_F+11)) m_Oct=10;
  104. int note=0;
  105. char KeyChar=0;
  106. bool KeyPressed=false;
  107. for (int key=0; key<NKEYS; key++)
  108. {
  109. KeyChar=KEYMAP[key];
  110. // check if a key's been pressed
  111. if (Fl::event_key(KeyChar))
  112. {
  113. KeyPressed=true;
  114. Volume = 127;
  115. Note=(m_Oct*12)+note;
  116. if (m_Last!=Note)
  117. {
  118. if (m_Last!=-1)
  119. {
  120. // turn off the old one
  121. m_Key[m_Last]->value(0);
  122. m_Key[m_Last]->parent()->redraw();
  123. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  124. m_GUICH->Wait();
  125. }
  126. m_Last = Note;
  127. m_GUICH->Set("Note",Note);
  128. m_GUICH->SetCommand(KeyboardPlugin::NOTE_ON);
  129. m_Key[Note]->value(1);
  130. m_Key[m_Last]->parent()->redraw();
  131. }
  132. }
  133. else // it's not pressed down
  134. {
  135. //see if the note was pressed down last time
  136. Note=(m_Oct*12)+note;
  137. if (m_Last==Note)
  138. {
  139. m_Key[m_Last]->value(0);
  140. m_Key[m_Last]->parent()->redraw();
  141. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  142. m_Last=-1;
  143. }
  144. }
  145. note++;
  146. }
  147. }
  148. void KeyboardPluginGUI::UpdateValues(SpiralPlugin *o)
  149. {
  150. KeyboardPlugin *Plugin = (KeyboardPlugin*)o;
  151. }
  152. //// Callbacks ////
  153. inline void KeyboardPluginGUI::cb_Key_i(Fl_Button* o, void* v)
  154. {
  155. int k=*(int*)(v);
  156. if (o->value())
  157. {
  158. m_GUICH->Set("Note",k);
  159. m_GUICH->SetCommand(KeyboardPlugin::NOTE_ON);
  160. }
  161. else
  162. {
  163. m_GUICH->SetCommand(KeyboardPlugin::NOTE_OFF);
  164. }
  165. parent()->redraw();
  166. }
  167. void KeyboardPluginGUI::cb_Key(Fl_Button* o, void* v)
  168. { ((KeyboardPluginGUI*)(o->parent()->user_data()))->cb_Key_i(o,v);}
  169. const string KeyboardPluginGUI::GetHelpText(const string &loc){
  170. return string("")
  171. + "This plugin provides you with a soft-keyboard if you don't have midi.\n"
  172. + "You can either use the mouse to play the keyboard GUI, or it will also\n"
  173. + "pick up PC keyboard presses in the ssm window.\n\n"
  174. + "The keyboard map follows the SoundTracker standard - i.e:\n"
  175. + "z=C4 s=C#4 x=D4 ... q=C5 2=C#5 w=D5...\n"
  176. + "The function keys change the octave.";
  177. }