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.

203 lines
4.0KB

  1. /* TriggerWidget
  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 "Fl_Trigger.h"
  19. #include <iostream>
  20. #include <FL/fl_draw.H>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include "Fl_Loop.h"
  24. /////////////////////////////////////////////////////////////////////////////
  25. static const float RADCONV = 0.017453292;
  26. Fl_Trigger::Fl_Trigger(int x, int y, int w, int h, const char* label) :
  27. Fl_Widget(x,y,w,h,label),
  28. m_ID(0),
  29. m_CentreX(0),
  30. m_CentreY(0),
  31. m_CentreRadius(0),
  32. m_Dragging(false),
  33. m_Loop(0),
  34. m_Passes(0),
  35. m_Count(0),
  36. m_Angle(0),
  37. m_MaxLoops(10),
  38. m_MaxPasses(10),
  39. m_Snap(false),
  40. m_SnapDegrees(45)
  41. {
  42. }
  43. void Fl_Trigger::draw()
  44. {
  45. x((int)((sin(m_Angle*RADCONV)*m_CentreRadius)+m_CentreX-w()/2));
  46. y((int)((cos(m_Angle*RADCONV)*m_CentreRadius)+m_CentreY-h()/2));
  47. fl_font(fl_font(),8);
  48. fl_color(255,255,255);
  49. fl_arc(x(), y(), w(), h(), 0, 360);
  50. int cx=x()+w()/2;
  51. int cy=y()+h()/2;
  52. char text[32];
  53. //sprintf(text,"%d",m_Loop);
  54. //fl_draw(text, cx-2, cy-h()+5);
  55. //sprintf(text,"%d",m_Passes);
  56. //fl_draw(text, cx+w()-5, cy+3);
  57. sprintf(text,"%d",m_ID);
  58. fl_draw(text, cx, cy);
  59. //char t[32];
  60. //sprintf(t,"%d",m_Count);
  61. //fl_draw(t, cx-2, cy-h()+5);
  62. }
  63. int Fl_Trigger::handle(int event)
  64. {
  65. static int LastButtonPushed=0;
  66. // call base
  67. if (!Fl_Widget::handle(event))
  68. {
  69. int ww,hh;
  70. ww = w();
  71. hh = h();
  72. int mx = Fl::event_x();
  73. int my = Fl::event_y();
  74. switch (event)
  75. {
  76. case FL_PUSH:
  77. LastButtonPushed=Fl::event_button();
  78. if (LastButtonPushed==1)
  79. {
  80. if (sqrt((float)(mx-x()*my-y()))<w())
  81. {
  82. m_Dragging=true;
  83. }
  84. }
  85. if (LastButtonPushed==2)
  86. {
  87. m_ID++;
  88. if (m_ID>7)
  89. {
  90. m_ID=0;
  91. }
  92. redraw();
  93. Fl_Loop *p=(Fl_Loop*)parent();
  94. p->DrawEveryThing();
  95. p->redraw();
  96. }
  97. if (LastButtonPushed==3)
  98. {
  99. /*m_Passes++;
  100. if (m_Passes>m_MaxPasses)
  101. {
  102. m_Passes=0;
  103. }
  104. redraw();
  105. Fl_Loop *p=(Fl_Loop*)parent();
  106. p->DrawEveryThing();
  107. p->redraw();*/
  108. }
  109. // fall through
  110. case FL_DRAG:
  111. {
  112. if (LastButtonPushed==2)
  113. {
  114. }
  115. else if (LastButtonPushed==1)
  116. {
  117. if (m_Dragging)
  118. {
  119. int px = mx-m_CentreX;
  120. int py = my-m_CentreY;
  121. double angle = 90+atan2((float)-py, (float)px)*180/M_PI;
  122. while (angle < m_Angle-180) angle += 360;
  123. while (angle > m_Angle+180) angle -= 360;
  124. while (angle < 0) angle += 360;
  125. while (angle > 360) angle -= 360;
  126. m_Angle=angle;
  127. // snap
  128. if (m_Snap)
  129. {
  130. m_Angle-=(int)m_Angle%m_SnapDegrees;
  131. }
  132. redraw();
  133. }
  134. }
  135. else if (LastButtonPushed==3)
  136. {
  137. }
  138. }
  139. break;
  140. case FL_RELEASE:
  141. {
  142. m_Dragging=false;
  143. Fl_Loop *p=(Fl_Loop*)parent();
  144. p->DrawEveryThing();
  145. p->redraw();
  146. }
  147. break;
  148. default:
  149. return 0;
  150. }
  151. }
  152. return 1;
  153. }
  154. istream &operator>>(istream &s, Fl_Trigger &o)
  155. {
  156. s>>o.m_CentreX>>o.m_CentreY>>o.m_CentreRadius>>o.m_Dragging>>o.m_ID>>o.m_Passes>>
  157. o.m_Count>>o.m_Angle>>o.m_MaxLoops>>o.m_MaxPasses;
  158. return s;
  159. }
  160. ostream &operator<<(ostream &s, Fl_Trigger &o)
  161. {
  162. s<<o.m_CentreX<<" "<<o.m_CentreY<<" "<<o.m_CentreRadius<<" "<<o.m_Dragging<<" "<<
  163. o.m_ID<<" "<<o.m_Passes<<" "<<o.m_Count<<" "<<o.m_Angle<<" "<<
  164. o.m_MaxLoops<<" "<<o.m_MaxPasses<<" ";
  165. return s;
  166. }