SampleHT.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkDrawable.h"
  9. #include "include/core/SkPictureRecorder.h"
  10. #include "include/utils/SkInterpolator.h"
  11. #include "include/utils/SkRandom.h"
  12. #include "samplecode/Sample.h"
  13. #include "src/core/SkPointPriv.h"
  14. #include "tools/timer/TimeUtils.h"
  15. const SkRect gUnitSquare = { -1, -1, 1, 1 };
  16. static void color_to_floats(SkColor c, SkScalar f[4]) {
  17. f[0] = SkIntToScalar(SkColorGetA(c));
  18. f[1] = SkIntToScalar(SkColorGetR(c));
  19. f[2] = SkIntToScalar(SkColorGetG(c));
  20. f[3] = SkIntToScalar(SkColorGetB(c));
  21. }
  22. static SkColor floats_to_color(const SkScalar f[4]) {
  23. return SkColorSetARGB(SkScalarRoundToInt(f[0]),
  24. SkScalarRoundToInt(f[1]),
  25. SkScalarRoundToInt(f[2]),
  26. SkScalarRoundToInt(f[3]));
  27. }
  28. static bool oval_contains(const SkRect& r, SkScalar x, SkScalar y) {
  29. SkMatrix m;
  30. m.setRectToRect(r, gUnitSquare, SkMatrix::kFill_ScaleToFit);
  31. SkPoint pt;
  32. m.mapXY(x, y, &pt);
  33. return SkPointPriv::LengthSqd(pt) <= 1;
  34. }
  35. static SkColor rand_opaque_color(uint32_t seed) {
  36. SkRandom rand(seed);
  37. return rand.nextU() | (0xFF << 24);
  38. }
  39. class HTDrawable : public SkDrawable {
  40. SkRect fR;
  41. SkColor fColor;
  42. SkInterpolator* fInterp;
  43. SkMSec fTime;
  44. public:
  45. HTDrawable(SkRandom& rand) {
  46. fR = SkRect::MakeXYWH(rand.nextRangeF(0, 640), rand.nextRangeF(0, 480),
  47. rand.nextRangeF(20, 200), rand.nextRangeF(20, 200));
  48. fColor = rand_opaque_color(rand.nextU());
  49. fInterp = nullptr;
  50. fTime = 0;
  51. }
  52. void spawnAnimation(SkMSec now) {
  53. this->setTime(now);
  54. delete fInterp;
  55. fInterp = new SkInterpolator(5, 3);
  56. SkScalar values[5];
  57. color_to_floats(fColor, values); values[4] = 0;
  58. fInterp->setKeyFrame(0, now, values);
  59. values[0] = 0; values[4] = 180;
  60. fInterp->setKeyFrame(1, now + 1000, values);
  61. color_to_floats(rand_opaque_color(fColor), values); values[4] = 360;
  62. fInterp->setKeyFrame(2, now + 2000, values);
  63. fInterp->setMirror(true);
  64. fInterp->setRepeatCount(3);
  65. this->notifyDrawingChanged();
  66. }
  67. bool hitTest(SkScalar x, SkScalar y) {
  68. return oval_contains(fR, x, y);
  69. }
  70. void setTime(SkMSec time) { fTime = time; }
  71. void onDraw(SkCanvas* canvas) override {
  72. SkAutoCanvasRestore acr(canvas, false);
  73. SkPaint paint;
  74. paint.setAntiAlias(true);
  75. if (fInterp) {
  76. SkScalar values[5];
  77. SkInterpolator::Result res = fInterp->timeToValues(fTime, values);
  78. fColor = floats_to_color(values);
  79. canvas->save();
  80. canvas->rotate(values[4], fR.centerX(), fR.centerY());
  81. switch (res) {
  82. case SkInterpolator::kFreezeEnd_Result:
  83. delete fInterp;
  84. fInterp = nullptr;
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. paint.setColor(fColor);
  91. canvas->drawRect(fR, paint);
  92. }
  93. SkRect onGetBounds() override { return fR; }
  94. };
  95. class HTView : public Sample {
  96. public:
  97. enum {
  98. N = 50,
  99. W = 640,
  100. H = 480,
  101. };
  102. struct Rec {
  103. HTDrawable* fDrawable;
  104. };
  105. Rec fArray[N];
  106. sk_sp<SkDrawable> fRoot;
  107. SkMSec fTime;
  108. HTView() {
  109. SkRandom rand;
  110. SkPictureRecorder recorder;
  111. SkCanvas* canvas = recorder.beginRecording(SkRect::MakeWH(W, H));
  112. for (int i = 0; i < N; ++i) {
  113. fArray[i].fDrawable = new HTDrawable(rand);
  114. canvas->drawDrawable(fArray[i].fDrawable);
  115. fArray[i].fDrawable->unref();
  116. }
  117. fRoot = recorder.finishRecordingAsDrawable();
  118. }
  119. protected:
  120. SkString name() override { return SkString("HT"); }
  121. void onDrawContent(SkCanvas* canvas) override {
  122. canvas->drawDrawable(fRoot.get());
  123. }
  124. bool onAnimate(double nanos) override {
  125. fTime = TimeUtils::NanosToMSec(nanos);
  126. for (int i = 0; i < N; ++i) {
  127. fArray[i].fDrawable->setTime(fTime);
  128. }
  129. return true;
  130. }
  131. Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
  132. // search backwards to find the top-most
  133. for (int i = N - 1; i >= 0; --i) {
  134. if (fArray[i].fDrawable->hitTest(x, y)) {
  135. fArray[i].fDrawable->spawnAnimation(fTime);
  136. break;
  137. }
  138. }
  139. return nullptr;
  140. }
  141. private:
  142. typedef Sample INHERITED;
  143. };
  144. //////////////////////////////////////////////////////////////////////////////
  145. DEF_SAMPLE( return new HTView(); )