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.

718 lines
13KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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 "../Geometry.hpp"
  17. START_NAMESPACE_DGL
  18. // -----------------------------------------------------------------------
  19. // Point
  20. template<typename T>
  21. Point<T>::Point() noexcept
  22. : fX(0),
  23. fY(0)
  24. {
  25. }
  26. template<typename T>
  27. Point<T>::Point(const T& x, const T& y) noexcept
  28. : fX(x),
  29. fY(y)
  30. {
  31. }
  32. template<typename T>
  33. Point<T>::Point(const Point<T>& pos) noexcept
  34. : fX(pos.fX),
  35. fY(pos.fY)
  36. {
  37. }
  38. template<typename T>
  39. const T& Point<T>::getX() const noexcept
  40. {
  41. return fX;
  42. }
  43. template<typename T>
  44. const T& Point<T>::getY() const noexcept
  45. {
  46. return fY;
  47. }
  48. template<typename T>
  49. void Point<T>::setX(const T& x) noexcept
  50. {
  51. fX = x;
  52. }
  53. template<typename T>
  54. void Point<T>::setY(const T& y) noexcept
  55. {
  56. fY = y;
  57. }
  58. template<typename T>
  59. void Point<T>::setPos(const T& x, const T& y) noexcept
  60. {
  61. fX = x;
  62. fY = y;
  63. }
  64. template<typename T>
  65. void Point<T>::setPos(const Point<T>& pos) noexcept
  66. {
  67. fX = pos.fX;
  68. fY = pos.fY;
  69. }
  70. template<typename T>
  71. void Point<T>::moveBy(const T& x, const T& y) noexcept
  72. {
  73. fX += x;
  74. fY += y;
  75. }
  76. template<typename T>
  77. void Point<T>::moveBy(const Point<T>& pos) noexcept
  78. {
  79. fX += pos.fX;
  80. fY += pos.fY;
  81. }
  82. template<typename T>
  83. Point<T>& Point<T>::operator=(const Point<T>& pos) noexcept
  84. {
  85. fX = pos.fX;
  86. fY = pos.fY;
  87. return *this;
  88. }
  89. template<typename T>
  90. Point<T>& Point<T>::operator+=(const Point<T>& pos) noexcept
  91. {
  92. fX += pos.fX;
  93. fY += pos.fY;
  94. return *this;
  95. }
  96. template<typename T>
  97. Point<T>& Point<T>::operator-=(const Point<T>& pos) noexcept
  98. {
  99. fX -= pos.fX;
  100. fY -= pos.fY;
  101. return *this;
  102. }
  103. template<typename T>
  104. bool Point<T>::operator==(const Point<T>& pos) const noexcept
  105. {
  106. return (fX == pos.fX && fY== pos.fY);
  107. }
  108. template<typename T>
  109. bool Point<T>::operator!=(const Point<T>& pos) const noexcept
  110. {
  111. return !operator==(pos);
  112. }
  113. // -----------------------------------------------------------------------
  114. // Size
  115. template<typename T>
  116. Size<T>::Size() noexcept
  117. : fWidth(0),
  118. fHeight(0)
  119. {
  120. }
  121. template<typename T>
  122. Size<T>::Size(const T& width, const T& height) noexcept
  123. : fWidth(width),
  124. fHeight(height)
  125. {
  126. }
  127. template<typename T>
  128. Size<T>::Size(const Size<T>& size) noexcept
  129. : fWidth(size.fWidth),
  130. fHeight(size.fHeight)
  131. {
  132. }
  133. template<typename T>
  134. const T& Size<T>::getWidth() const noexcept
  135. {
  136. return fWidth;
  137. }
  138. template<typename T>
  139. const T& Size<T>::getHeight() const noexcept
  140. {
  141. return fHeight;
  142. }
  143. template<typename T>
  144. void Size<T>::setWidth(const T& width) noexcept
  145. {
  146. fWidth = width;
  147. }
  148. template<typename T>
  149. void Size<T>::setHeight(const T& height) noexcept
  150. {
  151. fHeight = height;
  152. }
  153. template<typename T>
  154. void Size<T>::setSize(const T& width, const T& height) noexcept
  155. {
  156. fWidth = width;
  157. fHeight = height;
  158. }
  159. template<typename T>
  160. void Size<T>::setSize(const Size<T>& size) noexcept
  161. {
  162. fWidth = size.fWidth;
  163. fHeight = size.fHeight;
  164. }
  165. template<typename T>
  166. void Size<T>::growBy(const T& multiplier) noexcept
  167. {
  168. fWidth *= multiplier;
  169. fHeight *= multiplier;
  170. }
  171. template<typename T>
  172. void Size<T>::shrinkBy(const T& divider) noexcept
  173. {
  174. fWidth /= divider;
  175. fHeight /= divider;
  176. }
  177. template<typename T>
  178. Size<T>& Size<T>::operator=(const Size<T>& size) noexcept
  179. {
  180. fWidth = size.fWidth;
  181. fHeight = size.fHeight;
  182. return *this;
  183. }
  184. template<typename T>
  185. Size<T>& Size<T>::operator+=(const Size<T>& size) noexcept
  186. {
  187. fWidth += size.fWidth;
  188. fHeight += size.fHeight;
  189. return *this;
  190. }
  191. template<typename T>
  192. Size<T>& Size<T>::operator-=(const Size<T>& size) noexcept
  193. {
  194. fWidth -= size.fWidth;
  195. fHeight -= size.fHeight;
  196. return *this;
  197. }
  198. template<typename T>
  199. Size<T>& Size<T>::operator*=(const T& m) noexcept
  200. {
  201. fWidth *= m;
  202. fHeight *= m;
  203. return *this;
  204. }
  205. template<typename T>
  206. Size<T>& Size<T>::operator/=(const T& d) noexcept
  207. {
  208. fWidth /= d;
  209. fHeight /= d;
  210. return *this;
  211. }
  212. template<typename T>
  213. bool Size<T>::operator==(const Size<T>& size) const noexcept
  214. {
  215. return (fWidth == size.fWidth && fHeight == size.fHeight);
  216. }
  217. template<typename T>
  218. bool Size<T>::operator!=(const Size<T>& size) const noexcept
  219. {
  220. return !operator==(size);
  221. }
  222. // -----------------------------------------------------------------------
  223. // Line
  224. template<typename T>
  225. Line<T>::Line() noexcept
  226. : fPosStart(0, 0),
  227. fPosEnd(0, 0)
  228. {
  229. }
  230. template<typename T>
  231. Line<T>::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept
  232. : fPosStart(startX, startY),
  233. fPosEnd(endX, endY)
  234. {
  235. }
  236. template<typename T>
  237. Line<T>::Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept
  238. : fPosStart(startX, startY),
  239. fPosEnd(endPos)
  240. {
  241. }
  242. template<typename T>
  243. Line<T>::Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept
  244. : fPosStart(startPos),
  245. fPosEnd(endX, endY)
  246. {
  247. }
  248. template<typename T>
  249. Line<T>::Line(const Point<T>& startPos, const Point<T>& endPos) noexcept
  250. : fPosStart(startPos),
  251. fPosEnd(endPos)
  252. {
  253. }
  254. template<typename T>
  255. Line<T>::Line(const Line<T>& line) noexcept
  256. : fPosStart(line.fPosStart),
  257. fPosEnd(line.fPosEnd)
  258. {
  259. }
  260. template<typename T>
  261. const T& Line<T>::getStartX() const noexcept
  262. {
  263. return fPosStart.fX;
  264. }
  265. template<typename T>
  266. const T& Line<T>::getStartY() const noexcept
  267. {
  268. return fPosStart.fY;
  269. }
  270. template<typename T>
  271. const T& Line<T>::getEndX() const noexcept
  272. {
  273. return fPosEnd.fX;
  274. }
  275. template<typename T>
  276. const T& Line<T>::getEndY() const noexcept
  277. {
  278. return fPosEnd.fY;
  279. }
  280. template<typename T>
  281. const Point<T>& Line<T>::getStartPos() const noexcept
  282. {
  283. return fPosStart;
  284. }
  285. template<typename T>
  286. const Point<T>& Line<T>::getEndPos() const noexcept
  287. {
  288. return fPosEnd;
  289. }
  290. template<typename T>
  291. void Line<T>::setStartX(const T& x) noexcept
  292. {
  293. fPosStart.fX = x;
  294. }
  295. template<typename T>
  296. void Line<T>::setStartY(const T& y) noexcept
  297. {
  298. fPosStart.fY = y;
  299. }
  300. template<typename T>
  301. void Line<T>::setStartPos(const T& x, const T& y) noexcept
  302. {
  303. fPosStart = Point<T>(x, y);
  304. }
  305. template<typename T>
  306. void Line<T>::setStartPos(const Point<T>& pos) noexcept
  307. {
  308. fPosStart = pos;
  309. }
  310. template<typename T>
  311. void Line<T>::setEndX(const T& x) noexcept
  312. {
  313. fPosEnd.fX = x;
  314. }
  315. template<typename T>
  316. void Line<T>::setEndY(const T& y) noexcept
  317. {
  318. fPosEnd.fY = y;
  319. }
  320. template<typename T>
  321. void Line<T>::setEndPos(const T& x, const T& y) noexcept
  322. {
  323. fPosEnd = Point<T>(x, y);
  324. }
  325. template<typename T>
  326. void Line<T>::setEndPos(const Point<T>& pos) noexcept
  327. {
  328. fPosEnd = pos;
  329. }
  330. template<typename T>
  331. void Line<T>::moveBy(const T& x, const T& y) noexcept
  332. {
  333. fPosStart.fX += x;
  334. fPosStart.fY += y;
  335. fPosEnd.fX += x;
  336. fPosEnd.fY += y;
  337. }
  338. template<typename T>
  339. void Line<T>::moveBy(const Point<T>& pos) noexcept
  340. {
  341. fPosStart += pos;
  342. fPosEnd += pos;
  343. }
  344. template<typename T>
  345. void Line<T>::draw()
  346. {
  347. glBegin(GL_LINES);
  348. {
  349. glVertex2i(fPosStart.fX, fPosStart.fY);
  350. glVertex2i(fPosEnd.fX, fPosEnd.fY);
  351. }
  352. glEnd();
  353. }
  354. template<typename T>
  355. Line<T>& Line<T>::operator=(const Line<T>& line) noexcept
  356. {
  357. fPosStart = line.fPosStart;
  358. fPosEnd = line.fPosEnd;
  359. return *this;
  360. }
  361. template<typename T>
  362. bool Line<T>::operator==(const Line<T>& line) const noexcept
  363. {
  364. return (fPosStart == line.fPosStart && fPosEnd == line.fPosEnd);
  365. }
  366. template<typename T>
  367. bool Line<T>::operator!=(const Line<T>& line) const noexcept
  368. {
  369. return !operator==(line);
  370. }
  371. // -----------------------------------------------------------------------
  372. // Rectangle
  373. template<typename T>
  374. Rectangle<T>::Rectangle() noexcept
  375. : fPos(0, 0),
  376. fSize(0, 0)
  377. {
  378. }
  379. template<typename T>
  380. Rectangle<T>::Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept
  381. : fPos(x, y),
  382. fSize(width, height)
  383. {
  384. }
  385. template<typename T>
  386. Rectangle<T>::Rectangle(const T& x, const T& y, const Size<T>& size) noexcept
  387. : fPos(x, y),
  388. fSize(size)
  389. {
  390. }
  391. template<typename T>
  392. Rectangle<T>::Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept
  393. : fPos(pos),
  394. fSize(width, height)
  395. {
  396. }
  397. template<typename T>
  398. Rectangle<T>::Rectangle(const Point<T>& pos, const Size<T>& size) noexcept
  399. : fPos(pos),
  400. fSize(size)
  401. {
  402. }
  403. template<typename T>
  404. Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept
  405. : fPos(rect.fPos),
  406. fSize(rect.fSize)
  407. {
  408. }
  409. template<typename T>
  410. const T& Rectangle<T>::getX() const noexcept
  411. {
  412. return fPos.fX;
  413. }
  414. template<typename T>
  415. const T& Rectangle<T>::getY() const noexcept
  416. {
  417. return fPos.fY;
  418. }
  419. template<typename T>
  420. const T& Rectangle<T>::getWidth() const noexcept
  421. {
  422. return fSize.fWidth;
  423. }
  424. template<typename T>
  425. const T& Rectangle<T>::getHeight() const noexcept
  426. {
  427. return fSize.fHeight;
  428. }
  429. template<typename T>
  430. const Point<T>& Rectangle<T>::getPos() const noexcept
  431. {
  432. return fPos;
  433. }
  434. template<typename T>
  435. const Size<T>& Rectangle<T>::getSize() const noexcept
  436. {
  437. return fSize;
  438. }
  439. template<typename T>
  440. void Rectangle<T>::setX(const T& x) noexcept
  441. {
  442. fPos.fX = x;
  443. }
  444. template<typename T>
  445. void Rectangle<T>::setY(const T& y) noexcept
  446. {
  447. fPos.fY = y;
  448. }
  449. template<typename T>
  450. void Rectangle<T>::setPos(const T& x, const T& y) noexcept
  451. {
  452. fPos.fX = x;
  453. fPos.fY = y;
  454. }
  455. template<typename T>
  456. void Rectangle<T>::setPos(const Point<T>& pos) noexcept
  457. {
  458. fPos = pos;
  459. }
  460. template<typename T>
  461. void Rectangle<T>::moveBy(const T& x, const T& y) noexcept
  462. {
  463. fPos.fX += x;
  464. fPos.fY += y;
  465. }
  466. template<typename T>
  467. void Rectangle<T>::moveBy(const Point<T>& pos) noexcept
  468. {
  469. fPos += pos;
  470. }
  471. template<typename T>
  472. void Rectangle<T>::setWidth(const T& width) noexcept
  473. {
  474. fSize.fWidth = width;
  475. }
  476. template<typename T>
  477. void Rectangle<T>::setHeight(const T& height) noexcept
  478. {
  479. fSize.fHeight = height;
  480. }
  481. template<typename T>
  482. void Rectangle<T>::setSize(const T& width, const T& height) noexcept
  483. {
  484. fSize.fWidth = width;
  485. fSize.fHeight = height;
  486. }
  487. template<typename T>
  488. void Rectangle<T>::setSize(const Size<T>& size) noexcept
  489. {
  490. fSize = size;
  491. }
  492. template<typename T>
  493. void Rectangle<T>::growBy(const T& multiplier) noexcept
  494. {
  495. fSize.fWidth *= multiplier;
  496. fSize.fHeight *= multiplier;
  497. }
  498. template<typename T>
  499. void Rectangle<T>::shrinkBy(const T& divider) noexcept
  500. {
  501. fSize.fWidth /= divider;
  502. fSize.fHeight /= divider;
  503. }
  504. template<typename T>
  505. bool Rectangle<T>::contains(const T& x, const T& y) const noexcept
  506. {
  507. return (x >= fPos.fX && y >= fPos.fY && x <= fPos.fX+fSize.fWidth && y <= fPos.fY+fSize.fHeight);
  508. }
  509. template<typename T>
  510. bool Rectangle<T>::contains(const Point<T>& pos) const noexcept
  511. {
  512. return contains(pos.fX, pos.fY);
  513. }
  514. template<typename T>
  515. bool Rectangle<T>::containsX(const T& x) const noexcept
  516. {
  517. return (x >= fPos.fX && x <= fPos.fX + fSize.fWidth);
  518. }
  519. template<typename T>
  520. bool Rectangle<T>::containsY(const T& y) const noexcept
  521. {
  522. return (y >= fPos.fY && y <= fPos.fY + fSize.fHeight);
  523. }
  524. template<typename T>
  525. void Rectangle<T>::draw()
  526. {
  527. _draw(false);
  528. }
  529. template<typename T>
  530. void Rectangle<T>::drawOutline()
  531. {
  532. _draw(true);
  533. }
  534. template<typename T>
  535. Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept
  536. {
  537. fPos = rect.fPos;
  538. fSize = rect.fSize;
  539. return *this;
  540. }
  541. template<typename T>
  542. Rectangle<T>& Rectangle<T>::operator*=(const T& m) noexcept
  543. {
  544. fSize.fWidth *= m;
  545. fSize.fHeight *= m;
  546. return *this;
  547. }
  548. template<typename T>
  549. Rectangle<T>& Rectangle<T>::operator/=(const T& d) noexcept
  550. {
  551. fSize.fWidth /= d;
  552. fSize.fHeight /= d;
  553. return *this;
  554. }
  555. template<typename T>
  556. bool Rectangle<T>::operator==(const Rectangle<T>& rect) const noexcept
  557. {
  558. return (fPos == rect.fPos && fSize == rect.fSize);
  559. }
  560. template<typename T>
  561. bool Rectangle<T>::operator!=(const Rectangle<T>& rect) const noexcept
  562. {
  563. return !operator==(rect);
  564. }
  565. template<typename T>
  566. void Rectangle<T>::_draw(const bool isOutline)
  567. {
  568. glBegin(isOutline ? GL_LINE_LOOP : GL_QUADS);
  569. {
  570. glTexCoord2f(0.0f, 0.0f);
  571. glVertex2i(fPos.fX, fPos.fY);
  572. glTexCoord2f(1.0f, 0.0f);
  573. glVertex2i(fPos.fX+fSize.fWidth, fPos.fY);
  574. glTexCoord2f(1.0f, 1.0f);
  575. glVertex2i(fPos.fX+fSize.fWidth, fPos.fY+fSize.fHeight);
  576. glTexCoord2f(0.0f, 1.0f);
  577. glVertex2i(fPos.fX, fPos.fY+fSize.fHeight);
  578. }
  579. glEnd();
  580. }
  581. // -----------------------------------------------------------------------
  582. // Possible template data types
  583. template class Point<double>;
  584. template class Point<float>;
  585. template class Point<int>;
  586. template class Point<short>;
  587. template class Size<double>;
  588. template class Size<float>;
  589. template class Size<int>;
  590. template class Size<short>;
  591. template class Line<double>;
  592. template class Line<float>;
  593. template class Line<int>;
  594. template class Line<short>;
  595. template class Rectangle<double>;
  596. template class Rectangle<float>;
  597. template class Rectangle<int>;
  598. template class Rectangle<short>;
  599. // -----------------------------------------------------------------------
  600. END_NAMESPACE_DGL