stroke.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  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 3 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, see <http://www.gnu.org/licenses/>.
  16. #include <iostream>
  17. #include "stroke.hpp"
  18. #include "stroke_drawer.hpp"
  19. class StrokeImpl
  20. {
  21. public:
  22. Stroke::Dabs dabs;
  23. StrokeDrawer drawer;
  24. // Additional data which should be moved to the StrokeDrawer, since
  25. // its for caching only and can be generated
  26. //typedef std::vector<CL_Pointf> Normals;
  27. //Normals normals;
  28. mutable bool bounding_rect_needs_recalc;
  29. mutable CL_Rectf bounding_rect;
  30. CL_Rectf calc_bounding_rect() const
  31. {
  32. CL_Rectf rect;
  33. // FIXME: Keep the drawer into account (ie. brushsize)
  34. if (dabs.size() > 0)
  35. {
  36. rect.left = rect.right = dabs.front().pos.x;
  37. rect.top = rect.bottom = dabs.front().pos.y;
  38. for(Stroke::Dabs::const_iterator i = dabs.begin()+1; i != dabs.end(); ++i)
  39. {
  40. rect.left = std::min(i->pos.x, rect.left);
  41. rect.top = std::min(i->pos.y, rect.top);
  42. rect.right = std::max(i->pos.x, rect.right);
  43. rect.bottom = std::max(i->pos.y, rect.bottom);
  44. }
  45. }
  46. return rect;
  47. }
  48. StrokeImpl()
  49. : bounding_rect_needs_recalc(true)
  50. {
  51. }
  52. };
  53. Stroke::Stroke()
  54. : impl(new StrokeImpl())
  55. {
  56. }
  57. void
  58. Stroke::set_drawer(const StrokeDrawer& drawer_)
  59. {
  60. impl->drawer = drawer_;
  61. }
  62. StrokeDrawer
  63. Stroke::get_drawer()
  64. {
  65. return impl->drawer;
  66. }
  67. Stroke::Dabs
  68. Stroke::get_interpolated_dabs(float x_spacing, float y_spacing) const
  69. {
  70. if (impl->dabs.size() > 0)
  71. {
  72. Dabs interpolated_dabs;
  73. interpolated_dabs.push_back(impl->dabs.front());
  74. // The following code basically takes all the event dabs as recieved
  75. // by from the InputDevice and interpolates new dabs inbetween to
  76. // give them an equal spacing (ie. every dab is only 'spacing' away
  77. // from the next)
  78. float overspace = 0.0f;
  79. const Stroke::Dabs& dabs = impl->dabs;
  80. for(unsigned int j = 0; j < dabs.size()-1; ++j)
  81. {
  82. CL_Pointf dist = dabs[j+1].pos - dabs[j].pos;
  83. float length = sqrt(dist.x * dist.x + dist.y * dist.y);
  84. int n = 1;
  85. // Spacing is keep relative to the brush size
  86. // FIXME: This is specific to a Sprite based drawer, might not work for others
  87. // FIXME: y_spacing isn't taken into account either
  88. float local_spacing = x_spacing * dabs[j].pressure;
  89. while (length + overspace > (local_spacing * n))
  90. {
  91. float factor = (local_spacing/length) * n - (overspace/length);
  92. // FIXME: Interpolate tilting, pressure, etc. along the line
  93. interpolated_dabs.push_back(Dab(dabs[j].pos.x + dist.x * factor,
  94. dabs[j].pos.y + dist.y * factor,
  95. dabs[j].pressure));
  96. n += 1;
  97. }
  98. // calculate the space that wasn't used in the last iteration
  99. overspace = (length + overspace) - (local_spacing * (n-1));
  100. }
  101. return interpolated_dabs;
  102. }
  103. else
  104. {
  105. // No dabs available, so nothing to interpolate
  106. return impl->dabs;
  107. }
  108. }
  109. Stroke::Dabs
  110. Stroke::get_dabs() const
  111. {
  112. return impl->dabs;
  113. }
  114. int
  115. Stroke::get_dab_count() const
  116. {
  117. return impl->dabs.size();
  118. }
  119. void
  120. Stroke::draw(CL_GraphicContext* gc) const
  121. {
  122. if (!impl->drawer.is_null())
  123. {
  124. const_cast<StrokeDrawer&>(impl->drawer).draw(*this, gc);
  125. }
  126. else
  127. {
  128. std::cout << "No drawer set!" << std::endl;
  129. }
  130. }
  131. void
  132. Stroke::add_dab(const Dab& dab)
  133. {
  134. impl->dabs.push_back(dab);
  135. }
  136. /* // calc normals
  137. assert(normals.size() == 0);
  138. if (points.size() == 1)
  139. {
  140. normals.push_back(CL_Pointf(1.0f, 1.0f));
  141. }
  142. else if (points.size() == 2)
  143. {
  144. normals.push_back(CL_Pointf(1.0f, 1.0f));
  145. normals.push_back(CL_Pointf(1.0f, 1.0f));
  146. }
  147. else if (points.size() >= 3)
  148. {
  149. for(Points::size_type i = 0; i < int(points.size())-1; ++i)
  150. {
  151. CL_Pointf normal((points[i].y - points[i+1].y),
  152. -(points[i].x - points[i+1].x));
  153. float length = sqrt(normal.x * normal.x + normal.y * normal.y);
  154. normal.x /= length;
  155. normal.y /= length;
  156. normals.push_back(normal);
  157. }
  158. normals.push_back(CL_Pointf(1.0f, 1.0f));
  159. }
  160. //std::cout << normals.size() << " == " << points.size() << std::endl;
  161. assert(normals.size() == points.size());
  162. */
  163. /*
  164. // Calc bounding rect
  165. if (points.size() >= 1)
  166. {
  167. bounding_rect.left = bounding_rect.right = points.front().x;
  168. bounding_rect.top = bounding_rect.bottom = points.front().y;
  169. for(Points::iterator i = points.begin()+1; i != points.end(); ++i)
  170. {
  171. bounding_rect.left = Math::min(bounding_rect.left, i->x);
  172. bounding_rect.right = Math::max(bounding_rect.right, i->x);;
  173. bounding_rect.top = Math::min(bounding_rect.top, i->y);
  174. bounding_rect.bottom = Math::min(bounding_rect.bottom, i->y);
  175. }
  176. // FIXME: Need to take brush size into account
  177. }
  178. */
  179. CL_Rectf
  180. Stroke::get_bounding_rect() const
  181. {
  182. if (impl->bounding_rect_needs_recalc)
  183. {
  184. impl->bounding_rect = impl->calc_bounding_rect();
  185. impl->bounding_rect_needs_recalc = false;
  186. }
  187. return impl->bounding_rect;
  188. }
  189. bool
  190. Stroke::empty() const
  191. {
  192. return (impl->dabs.size() == 0);
  193. }
  194. /* EOF */