Sample.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2011 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. #ifndef SampleCode_DEFINED
  8. #define SampleCode_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPoint.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/core/SkString.h"
  13. #include "include/private/SkMacros.h"
  14. #include "tools/InputState.h"
  15. #include "tools/ModifierKey.h"
  16. #include "tools/Registry.h"
  17. #include "tools/SkMetaData.h"
  18. class SkCanvas;
  19. class Sample;
  20. using SampleFactory = Sample* (*)();
  21. using SampleRegistry = sk_tools::Registry<SampleFactory>;
  22. #define DEF_SAMPLE(code) \
  23. static Sample* SK_MACRO_APPEND_LINE(F_)() { code } \
  24. static SampleRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
  25. ///////////////////////////////////////////////////////////////////////////////
  26. class Sample {
  27. public:
  28. Sample()
  29. : fBGColor(SK_ColorWHITE)
  30. , fWidth(0), fHeight(0)
  31. , fHaveCalledOnceBeforeDraw(false)
  32. {}
  33. virtual ~Sample() = default;
  34. SkScalar width() const { return fWidth; }
  35. SkScalar height() const { return fHeight; }
  36. void setSize(SkScalar width, SkScalar height);
  37. void setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); }
  38. void setWidth(SkScalar width) { this->setSize(width, fHeight); }
  39. void setHeight(SkScalar height) { this->setSize(fWidth, height); }
  40. /** Call this to have the view draw into the specified canvas. */
  41. virtual void draw(SkCanvas* canvas);
  42. virtual bool onChar(SkUnichar) { return false; }
  43. // Click handling
  44. class Click {
  45. public:
  46. virtual ~Click() = default;
  47. SkPoint fOrig = {0, 0};
  48. SkPoint fPrev = {0, 0};
  49. SkPoint fCurr = {0, 0};
  50. InputState fState = InputState::kDown;
  51. ModifierKey fModifierKeys = ModifierKey::kNone;
  52. SkMetaData fMeta;
  53. };
  54. bool mouse(SkPoint point, InputState clickState, ModifierKey modifierKeys);
  55. void setBGColor(SkColor color) { fBGColor = color; }
  56. bool animate(double nanos) { return this->onAnimate(nanos); }
  57. virtual SkString name() = 0;
  58. protected:
  59. /** Override to be notified of size changes. Overriders must call the super class. */
  60. virtual void onSizeChange();
  61. /** Override this if you might handle the click */
  62. virtual Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi);
  63. /** Override to track clicks. Return true as long as you want to track the pen/mouse. */
  64. virtual bool onClick(Click*);
  65. virtual void onDrawBackground(SkCanvas*);
  66. virtual void onDrawContent(SkCanvas*) = 0;
  67. virtual bool onAnimate(double /*nanos*/) { return false; }
  68. virtual void onOnceBeforeDraw() {}
  69. private:
  70. std::unique_ptr<Click> fClick;
  71. SkColor fBGColor;
  72. SkScalar fWidth, fHeight;
  73. bool fHaveCalledOnceBeforeDraw;
  74. Sample(const Sample&) = delete;
  75. Sample& operator=(const Sample&) = delete;
  76. };
  77. #endif