cross_animation.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/effects/cross_animation.h"
  8. #include "ui/effects/animation_value.h"
  9. #include "ui/arc_angles.h"
  10. #include "ui/painter.h"
  11. #include <QtCore/QtMath>
  12. #include <QtGui/QPainterPath>
  13. namespace Ui {
  14. namespace {
  15. constexpr auto kPointCount = 12;
  16. constexpr auto kStaticLoadingValue = float64(-666);
  17. //
  18. // 1 3
  19. // X X X X
  20. // X X X X
  21. // 0 X X 4
  22. // X X X X
  23. // X 2 X
  24. // X X
  25. // X X
  26. // 11 5
  27. // X X
  28. // X X
  29. // X 8 X
  30. // X X X X
  31. // 10 X X 6
  32. // X X X X
  33. // X X X X
  34. // 9 7
  35. //
  36. void transformLoadingCross(float64 loading, std::array<QPointF, kPointCount> &points, int &paintPointsCount) {
  37. auto moveTo = [](QPointF &point, QPointF &to, float64 ratio) {
  38. point = point * (1. - ratio) + to * ratio;
  39. };
  40. auto moveFrom = [](QPointF &point, QPointF &from, float64 ratio) {
  41. point = from * (1. - ratio) + point * ratio;
  42. };
  43. auto paintPoints = [&points, &paintPointsCount](std::initializer_list<int> &&indices) {
  44. auto index = 0;
  45. for (auto paintIndex : indices) {
  46. points[index++] = points[paintIndex];
  47. }
  48. paintPointsCount = indices.size();
  49. };
  50. if (loading < 0.125) {
  51. auto ratio = loading / 0.125;
  52. moveTo(points[6], points[5], ratio);
  53. moveTo(points[7], points[8], ratio);
  54. } else if (loading < 0.25) {
  55. auto ratio = (loading - 0.125) / 0.125;
  56. moveTo(points[9], points[8], ratio);
  57. moveTo(points[10], points[11], ratio);
  58. paintPoints({ 0, 1, 2, 3, 4, 9, 10, 11 });
  59. } else if (loading < 0.375) {
  60. auto ratio = (loading - 0.25) / 0.125;
  61. moveTo(points[0], points[11], ratio);
  62. moveTo(points[1], points[2], ratio);
  63. paintPoints({ 0, 1, 2, 3, 4, 8 });
  64. } else if (loading < 0.5) {
  65. auto ratio = (loading - 0.375) / 0.125;
  66. moveTo(points[8], points[4], ratio);
  67. moveTo(points[11], points[3], ratio);
  68. paintPoints({ 3, 4, 8, 11 });
  69. } else if (loading < 0.625) {
  70. auto ratio = (loading - 0.5) / 0.125;
  71. moveFrom(points[8], points[4], ratio);
  72. moveFrom(points[11], points[3], ratio);
  73. paintPoints({ 3, 4, 8, 11 });
  74. } else if (loading < 0.75) {
  75. auto ratio = (loading - 0.625) / 0.125;
  76. moveFrom(points[6], points[5], ratio);
  77. moveFrom(points[7], points[8], ratio);
  78. paintPoints({ 3, 4, 5, 6, 7, 11 });
  79. } else if (loading < 0.875) {
  80. auto ratio = (loading - 0.75) / 0.125;
  81. moveFrom(points[9], points[8], ratio);
  82. moveFrom(points[10], points[11], ratio);
  83. paintPoints({ 3, 4, 5, 6, 7, 8, 9, 10 });
  84. } else {
  85. auto ratio = (loading - 0.875) / 0.125;
  86. moveFrom(points[0], points[11], ratio);
  87. moveFrom(points[1], points[2], ratio);
  88. }
  89. }
  90. } // namespace
  91. void CrossAnimation::paintStaticLoading(
  92. QPainter &p,
  93. const style::CrossAnimation &st,
  94. style::color color,
  95. int x,
  96. int y,
  97. int outerWidth,
  98. float64 shown) {
  99. paint(p, st, color, x, y, outerWidth, shown, kStaticLoadingValue);
  100. }
  101. void CrossAnimation::paint(
  102. QPainter &p,
  103. const style::CrossAnimation &st,
  104. style::color color,
  105. int x,
  106. int y,
  107. int outerWidth,
  108. float64 shown,
  109. float64 loading) {
  110. PainterHighQualityEnabler hq(p);
  111. const auto stroke = style::ConvertScaleExact(st.stroke);
  112. const auto deleteScale = shown + st.minScale * (1. - shown);
  113. const auto deleteSkip = (deleteScale * st.skip)
  114. + (1. - deleteScale) * (st.size / 2);
  115. const auto deleteLeft = 0.
  116. + style::rtlpoint(x + deleteSkip, 0, outerWidth).x();
  117. const auto deleteTop = y + deleteSkip + 0.;
  118. const auto deleteWidth = st.size - 2 * deleteSkip;
  119. const auto deleteHeight = st.size - 2 * deleteSkip;
  120. const auto deleteStroke = stroke / M_SQRT2;
  121. std::array<QPointF, kPointCount> pathDelete = { {
  122. { deleteLeft, deleteTop + deleteStroke },
  123. { deleteLeft + deleteStroke, deleteTop },
  124. { deleteLeft + (deleteWidth / 2.), deleteTop + (deleteHeight / 2.) - deleteStroke },
  125. { deleteLeft + deleteWidth - deleteStroke, deleteTop },
  126. { deleteLeft + deleteWidth, deleteTop + deleteStroke },
  127. { deleteLeft + (deleteWidth / 2.) + deleteStroke, deleteTop + (deleteHeight / 2.) },
  128. { deleteLeft + deleteWidth, deleteTop + deleteHeight - deleteStroke },
  129. { deleteLeft + deleteWidth - deleteStroke, deleteTop + deleteHeight },
  130. { deleteLeft + (deleteWidth / 2.), deleteTop + (deleteHeight / 2.) + deleteStroke },
  131. { deleteLeft + deleteStroke, deleteTop + deleteHeight },
  132. { deleteLeft, deleteTop + deleteHeight - deleteStroke },
  133. { deleteLeft + (deleteWidth / 2.) - deleteStroke, deleteTop + (deleteHeight / 2.) },
  134. } };
  135. auto pathDeleteSize = kPointCount;
  136. const auto staticLoading = (loading == kStaticLoadingValue);
  137. auto loadingArcLength = staticLoading ? arc::kFullLength : 0;
  138. if (loading > 0.) {
  139. transformLoadingCross(loading, pathDelete, pathDeleteSize);
  140. auto loadingArc = (loading >= 0.5) ? (loading - 1.) : loading;
  141. loadingArcLength = qRound(-loadingArc * 2 * arc::kFullLength);
  142. }
  143. if (!staticLoading) {
  144. if (shown < 1.) {
  145. auto alpha = -(shown - 1.) * M_PI_2;
  146. auto cosalpha = cos(alpha);
  147. auto sinalpha = sin(alpha);
  148. auto shiftx = deleteLeft + (deleteWidth / 2.);
  149. auto shifty = deleteTop + (deleteHeight / 2.);
  150. for (auto &point : pathDelete) {
  151. auto x = point.x() - shiftx;
  152. auto y = point.y() - shifty;
  153. point.setX(shiftx + x * cosalpha - y * sinalpha);
  154. point.setY(shifty + y * cosalpha + x * sinalpha);
  155. }
  156. }
  157. QPainterPath path;
  158. path.moveTo(pathDelete[0]);
  159. for (int i = 1; i != pathDeleteSize; ++i) {
  160. path.lineTo(pathDelete[i]);
  161. }
  162. path.lineTo(pathDelete[0]);
  163. p.fillPath(path, color);
  164. }
  165. if (loadingArcLength != 0) {
  166. auto roundSkip = (st.size * (1 - M_SQRT2) + 2 * M_SQRT2 * deleteSkip + stroke) / 2;
  167. auto roundPart = QRectF(x + roundSkip, y + roundSkip, st.size - 2 * roundSkip, st.size - 2 * roundSkip);
  168. if (staticLoading) {
  169. anim::DrawStaticLoading(p, roundPart, stroke, color);
  170. } else {
  171. auto loadingArcStart = arc::kQuarterLength / 2;
  172. if (shown < 1.) {
  173. loadingArcStart -= qRound(-(shown - 1.) * arc::kQuarterLength);
  174. }
  175. if (loadingArcLength < 0) {
  176. loadingArcStart += loadingArcLength;
  177. loadingArcLength = -loadingArcLength;
  178. }
  179. p.setBrush(Qt::NoBrush);
  180. auto pen = color->p;
  181. pen.setWidthF(stroke);
  182. pen.setCapStyle(Qt::RoundCap);
  183. p.setPen(pen);
  184. p.drawArc(roundPart, loadingArcStart, loadingArcLength);
  185. }
  186. }
  187. }
  188. } // namespace Ui