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