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.

201 lines
5.2KB

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