Sample.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2016 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/SkString.h"
  9. #include "samplecode/Sample.h"
  10. #if SK_SUPPORT_GPU
  11. # include "include/gpu/GrContext.h"
  12. #else
  13. class GrContext;
  14. #endif
  15. //////////////////////////////////////////////////////////////////////////////
  16. void Sample::setSize(SkScalar width, SkScalar height) {
  17. width = SkMaxScalar(0, width);
  18. height = SkMaxScalar(0, height);
  19. if (fWidth != width || fHeight != height)
  20. {
  21. fWidth = width;
  22. fHeight = height;
  23. this->onSizeChange();
  24. }
  25. }
  26. void Sample::draw(SkCanvas* canvas) {
  27. if (fWidth && fHeight) {
  28. SkRect r;
  29. r.set(0, 0, fWidth, fHeight);
  30. if (canvas->quickReject(r)) {
  31. return;
  32. }
  33. SkAutoCanvasRestore as(canvas, true);
  34. int sc = canvas->save();
  35. if (!fHaveCalledOnceBeforeDraw) {
  36. fHaveCalledOnceBeforeDraw = true;
  37. this->onOnceBeforeDraw();
  38. }
  39. this->onDrawBackground(canvas);
  40. SkAutoCanvasRestore acr(canvas, true);
  41. this->onDrawContent(canvas);
  42. #if SK_SUPPORT_GPU
  43. // Ensure the GrContext doesn't combine GrDrawOps across draw loops.
  44. if (GrContext* context = canvas->getGrContext()) {
  45. context->flush();
  46. }
  47. #endif
  48. canvas->restoreToCount(sc);
  49. }
  50. }
  51. ////////////////////////////////////////////////////////////////////////////
  52. bool Sample::mouse(SkPoint point, InputState clickState, ModifierKey modifierKeys) {
  53. switch (clickState) {
  54. case InputState::kDown:
  55. fClick = nullptr;
  56. if (point.x() < 0 || point.y() < 0 || point.x() >= fWidth || point.y() >= fHeight) {
  57. return false;
  58. }
  59. fClick.reset(this->onFindClickHandler(point.x(), point.y(), modifierKeys));
  60. if (!fClick) {
  61. return false;
  62. }
  63. fClick->fPrev = fClick->fCurr = fClick->fOrig = point;
  64. fClick->fState = InputState::kDown;
  65. fClick->fModifierKeys = modifierKeys;
  66. this->onClick(fClick.get());
  67. return true;
  68. case InputState::kMove:
  69. if (fClick) {
  70. fClick->fPrev = fClick->fCurr;
  71. fClick->fCurr = point;
  72. fClick->fState = InputState::kMove;
  73. fClick->fModifierKeys = modifierKeys;
  74. return this->onClick(fClick.get());
  75. }
  76. return false;
  77. case InputState::kUp:
  78. if (fClick) {
  79. fClick->fPrev = fClick->fCurr;
  80. fClick->fCurr = point;
  81. fClick->fState = InputState::kUp;
  82. fClick->fModifierKeys = modifierKeys;
  83. bool result = this->onClick(fClick.get());
  84. fClick = nullptr;
  85. return result;
  86. }
  87. return false;
  88. }
  89. SkASSERT(false);
  90. return false;
  91. }
  92. //////////////////////////////////////////////////////////////////////
  93. void Sample::onSizeChange() {}
  94. Sample::Click* Sample::onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) {
  95. return nullptr;
  96. }
  97. bool Sample::onClick(Click*) {
  98. return false;
  99. }
  100. void Sample::onDrawBackground(SkCanvas* canvas) {
  101. canvas->drawColor(fBGColor);
  102. }
  103. // need to explicitly declare this, or we get some weird infinite loop llist
  104. template SampleRegistry* SampleRegistry::gHead;