generated_brush.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "generated_brush.hpp"
  17. #include <ClanLib/Display/sprite_description.h>
  18. class GeneratedBrushImpl : public BrushImpl
  19. {
  20. public:
  21. BrushShape shape;
  22. float radius;
  23. int spikes;
  24. float hardness;
  25. float aspect_ratio;
  26. float angle;
  27. /** When set surface is out of date and needs updating */
  28. bool dirty;
  29. CL_Sprite sprite;
  30. virtual ~GeneratedBrushImpl() {}
  31. CL_Sprite get_sprite();
  32. void update();
  33. BrushImpl* clone() const;
  34. };
  35. GeneratedBrush::GeneratedBrush(const Brush& brush) :
  36. impl(std::dynamic_pointer_cast<GeneratedBrushImpl>(brush.impl))
  37. {
  38. }
  39. GeneratedBrush::GeneratedBrush(BrushShape shape,
  40. float radius,
  41. int spikes, /* 2 - 20 */
  42. float hardness, /* 0.0 - 1.0 */
  43. float aspect_ratio, /* y/x */
  44. float angle) :
  45. impl(new GeneratedBrushImpl())
  46. {
  47. impl->shape = shape;
  48. impl->radius = radius;
  49. impl->spikes = spikes;
  50. impl->hardness = hardness;
  51. impl->aspect_ratio = aspect_ratio;
  52. impl->angle = angle;
  53. impl->dirty = true;
  54. }
  55. void
  56. GeneratedBrushImpl::update()
  57. {
  58. if (dirty)
  59. {
  60. CL_SpriteDescription desc;
  61. desc.add_frame(generate_brushmask(shape,
  62. radius,
  63. spikes,
  64. hardness,
  65. aspect_ratio,
  66. angle));
  67. sprite = CL_Sprite(desc);
  68. sprite.set_alignment(origin_center);
  69. dirty = false;
  70. }
  71. }
  72. void
  73. GeneratedBrush::set_shape(BrushShape shape)
  74. {
  75. impl->shape = shape;
  76. impl->dirty = true;
  77. }
  78. BrushShape
  79. GeneratedBrush::get_shape()
  80. {
  81. return impl->shape;
  82. }
  83. void
  84. GeneratedBrush::set_radius(float radius)
  85. {
  86. impl->radius = radius;
  87. impl->dirty = true;
  88. }
  89. float
  90. GeneratedBrush::get_radius()
  91. {
  92. return impl->radius;
  93. }
  94. void
  95. GeneratedBrush::set_spikes(int spikes)
  96. {
  97. impl->spikes = spikes;
  98. impl->dirty = true;
  99. }
  100. int
  101. GeneratedBrush::get_spikes()
  102. {
  103. return impl->spikes;
  104. }
  105. void
  106. GeneratedBrush::set_hardness(float hardness)
  107. {
  108. impl->hardness = hardness;
  109. impl->dirty = true;
  110. }
  111. float
  112. GeneratedBrush::get_hardness()
  113. {
  114. return impl->hardness;
  115. }
  116. void
  117. GeneratedBrush::set_aspect_ratio(float aspect)
  118. {
  119. impl->aspect_ratio = aspect;
  120. impl->dirty = true;
  121. }
  122. float
  123. GeneratedBrush::get_aspect_ratio()
  124. {
  125. return impl->aspect_ratio;
  126. }
  127. void
  128. GeneratedBrush::set_angle(float angle)
  129. {
  130. impl->angle = angle;
  131. impl->dirty = true;
  132. }
  133. float
  134. GeneratedBrush::get_angle()
  135. {
  136. return impl->angle;
  137. }
  138. CL_Sprite
  139. GeneratedBrushImpl::get_sprite()
  140. {
  141. update();
  142. return sprite;
  143. }
  144. BrushImpl*
  145. GeneratedBrushImpl::clone() const
  146. {
  147. // FIXME: Make this Copy-On-Write cloning, else it might get a
  148. // little bit expensive with all the CL_Sprite's per stroke
  149. GeneratedBrushImpl* c = new GeneratedBrushImpl();
  150. *c = *this;
  151. return c;
  152. }
  153. Brush
  154. GeneratedBrush::to_brush()
  155. {
  156. return Brush(impl);
  157. }
  158. /* EOF */