DISTRHO Plugin Framework
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.

341 lines
9.3KB

  1. /*
  2. * DISTRHO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../ImageKnob.hpp"
  17. #include <cassert>
  18. #include <cstdio>
  19. START_NAMESPACE_DGL
  20. // -----------------------------------------------------------------------
  21. ImageKnob::ImageKnob(Window& parent, const Image& image, Orientation orientation)
  22. : Widget(parent),
  23. fImage(image),
  24. fMinimum(0.0f),
  25. fMaximum(1.0f),
  26. fValue(0.5f),
  27. fOrientation(orientation),
  28. fRotationAngle(0),
  29. fDragging(false),
  30. fLastX(0),
  31. fLastY(0),
  32. fCallback(nullptr),
  33. fIsImgVertical(image.getHeight() > image.getWidth()),
  34. fImgLayerSize(fIsImgVertical ? image.getWidth() : image.getHeight()),
  35. fImgLayerCount(fIsImgVertical ? image.getHeight()/fImgLayerSize : image.getWidth()/fImgLayerSize),
  36. fKnobArea(0, 0, fImgLayerSize, fImgLayerSize),
  37. fTextureId(0)
  38. {
  39. setSize(fImgLayerSize, fImgLayerSize);
  40. }
  41. ImageKnob::ImageKnob(Widget* widget, const Image& image, Orientation orientation)
  42. : Widget(widget->getParentWindow()),
  43. fImage(image),
  44. fMinimum(0.0f),
  45. fMaximum(1.0f),
  46. fValue(0.5f),
  47. fOrientation(orientation),
  48. fRotationAngle(0),
  49. fDragging(false),
  50. fLastX(0),
  51. fLastY(0),
  52. fCallback(nullptr),
  53. fIsImgVertical(image.getHeight() > image.getWidth()),
  54. fImgLayerSize(fIsImgVertical ? image.getWidth() : image.getHeight()),
  55. fImgLayerCount(fIsImgVertical ? image.getHeight()/fImgLayerSize : image.getWidth()/fImgLayerSize),
  56. fKnobArea(0, 0, fImgLayerSize, fImgLayerSize),
  57. fTextureId(0)
  58. {
  59. setSize(fImgLayerSize, fImgLayerSize);
  60. }
  61. ImageKnob::ImageKnob(const ImageKnob& imageKnob)
  62. : Widget(imageKnob.getParentWindow()),
  63. fImage(imageKnob.fImage),
  64. fMinimum(imageKnob.fMinimum),
  65. fMaximum(imageKnob.fMaximum),
  66. fValue(imageKnob.fValue),
  67. fOrientation(imageKnob.fOrientation),
  68. fRotationAngle(imageKnob.fRotationAngle),
  69. fDragging(false),
  70. fLastX(0),
  71. fLastY(0),
  72. fCallback(imageKnob.fCallback),
  73. fIsImgVertical(imageKnob.fIsImgVertical),
  74. fImgLayerSize(imageKnob.fImgLayerSize),
  75. fImgLayerCount(imageKnob.fImgLayerCount),
  76. fKnobArea(imageKnob.fKnobArea),
  77. fTextureId(0)
  78. {
  79. setSize(fImgLayerSize, fImgLayerSize);
  80. if (fRotationAngle != 0)
  81. {
  82. // force new texture creation
  83. fRotationAngle = 0;
  84. setRotationAngle(imageKnob.fRotationAngle);
  85. }
  86. }
  87. float ImageKnob::getValue() const
  88. {
  89. return fValue;
  90. }
  91. void ImageKnob::setOrientation(Orientation orientation)
  92. {
  93. if (fOrientation == orientation)
  94. return;
  95. fOrientation = orientation;
  96. }
  97. void ImageKnob::setRange(float min, float max)
  98. {
  99. if (fValue < min)
  100. {
  101. fValue = min;
  102. repaint();
  103. if (fCallback != nullptr)
  104. fCallback->imageKnobValueChanged(this, fValue);
  105. }
  106. else if (fValue > max)
  107. {
  108. fValue = max;
  109. repaint();
  110. if (fCallback != nullptr)
  111. fCallback->imageKnobValueChanged(this, fValue);
  112. }
  113. fMinimum = min;
  114. fMaximum = max;
  115. }
  116. void ImageKnob::setValue(float value, bool sendCallback)
  117. {
  118. if (fValue == value)
  119. return;
  120. fValue = value;
  121. repaint();
  122. if (sendCallback && fCallback != nullptr)
  123. fCallback->imageKnobValueChanged(this, fValue);
  124. }
  125. void ImageKnob::setRotationAngle(int angle)
  126. {
  127. if (fRotationAngle == angle)
  128. return;
  129. if (fRotationAngle != 0)
  130. {
  131. // delete old texture
  132. glDeleteTextures(1, &fTextureId);
  133. fTextureId = 0;
  134. }
  135. fRotationAngle = angle;
  136. if (angle != 0)
  137. {
  138. glEnable(GL_TEXTURE_2D);
  139. glGenTextures(1, &fTextureId);
  140. glBindTexture(GL_TEXTURE_2D, fTextureId);
  141. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  142. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  143. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, fImage.getFormat(), fImage.getType(), fImage.getRawData());
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  148. float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  149. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  150. glBindTexture(GL_TEXTURE_2D, 0);
  151. glDisable(GL_TEXTURE_2D);
  152. }
  153. }
  154. void ImageKnob::setCallback(Callback* callback)
  155. {
  156. fCallback = callback;
  157. }
  158. void ImageKnob::onDisplay()
  159. {
  160. const float normValue = (fValue - fMinimum) / (fMaximum - fMinimum);
  161. if (fRotationAngle != 0)
  162. {
  163. glEnable(GL_TEXTURE_2D);
  164. glBindTexture(GL_TEXTURE_2D, fTextureId);
  165. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  166. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  167. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWidth(), getHeight(), 0, fImage.getFormat(), fImage.getType(), fImage.getRawData());
  168. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  169. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  170. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  171. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  172. float trans[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  173. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, trans);
  174. glPushMatrix();
  175. const GLint w2 = getWidth()/2;
  176. const GLint h2 = getHeight()/2;
  177. glTranslatef(getX()+w2, getY()+h2, 0.0f);
  178. glRotatef(normValue*fRotationAngle, 0.0f, 0.0f, 1.0f);
  179. glBegin(GL_QUADS);
  180. glTexCoord2f(0.0f, 1.0f);
  181. glVertex2i(-w2, -h2);
  182. glTexCoord2f(1.0f, 1.0f);
  183. glVertex2i(getWidth()-w2, -h2);
  184. glTexCoord2f(1.0f, 0.0f);
  185. glVertex2i(getWidth()-w2, getHeight()-h2);
  186. glTexCoord2f(0.0f, 0.0f);
  187. glVertex2i(-w2, getHeight()-h2);
  188. glEnd();
  189. glPopMatrix();
  190. glBindTexture(GL_TEXTURE_2D, 0);
  191. glDisable(GL_TEXTURE_2D);
  192. }
  193. else
  194. {
  195. const int layerDataSize = fImgLayerSize * fImgLayerSize * ((fImage.getFormat() == GL_BGRA || fImage.getFormat() == GL_RGBA) ? 4 : 3);
  196. const int imageDataSize = layerDataSize * fImgLayerCount;
  197. const int imageDataOffset = imageDataSize - layerDataSize - (layerDataSize * int(normValue * float(fImgLayerCount-1)));
  198. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  199. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  200. glRasterPos2i(getX(), getY()+getHeight());
  201. glDrawPixels(fImgLayerSize, fImgLayerSize, fImage.getFormat(), fImage.getType(), fImage.getRawData() + imageDataOffset);
  202. }
  203. }
  204. bool ImageKnob::onMouse(int button, bool press, int x, int y)
  205. {
  206. if (button != 1)
  207. return false;
  208. if (press)
  209. {
  210. if (! getArea().contains(x, y))
  211. return false;
  212. fDragging = true;
  213. fLastX = x;
  214. fLastY = y;
  215. if (fCallback != nullptr)
  216. fCallback->imageKnobDragStarted(this);
  217. return true;
  218. }
  219. else if (fDragging)
  220. {
  221. if (fCallback != nullptr)
  222. fCallback->imageKnobDragFinished(this);
  223. fDragging = false;
  224. return true;
  225. }
  226. return false;
  227. }
  228. bool ImageKnob::onMotion(int x, int y)
  229. {
  230. if (! fDragging)
  231. return false;
  232. if (fOrientation == ImageKnob::Horizontal)
  233. {
  234. int movX = x - fLastX;
  235. if (movX != 0)
  236. {
  237. float d = (getModifiers() & MODIFIER_SHIFT) ? 2000.0f : 200.0f;
  238. float value = fValue + (float(fMaximum - fMinimum) / d * float(movX));
  239. if (value < fMinimum)
  240. value = fMinimum;
  241. else if (value > fMaximum)
  242. value = fMaximum;
  243. setValue(value, true);
  244. }
  245. }
  246. else if (fOrientation == ImageKnob::Vertical)
  247. {
  248. int movY = fLastY - y;
  249. if (movY != 0)
  250. {
  251. float d = (getModifiers() & MODIFIER_SHIFT) ? 2000.0f : 200.0f;
  252. float value = fValue + (float(fMaximum - fMinimum) / d * float(movY));
  253. if (value < fMinimum)
  254. value = fMinimum;
  255. else if (value > fMaximum)
  256. value = fMaximum;
  257. setValue(value, true);
  258. }
  259. }
  260. fLastX = x;
  261. fLastY = y;
  262. return true;
  263. }
  264. void ImageKnob::onReshape(int width, int height)
  265. {
  266. // if (fRotationAngle != 0)
  267. // glEnable(GL_TEXTURE_2D);
  268. Widget::onReshape(width, height);
  269. }
  270. void ImageKnob::onClose()
  271. {
  272. // delete old texture
  273. setRotationAngle(0);
  274. }
  275. // -----------------------------------------------------------------------
  276. END_NAMESPACE_DGL