blob: 6a6161f64f2b55947ded068c6425b59c289b54c5 [file] [log] [blame]
edisonn@google.comcf2cfa12013-08-21 16:31:37 +00001/*
2 * Copyright 2013 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#ifndef SkTracker_DEFINED
9#define SkTracker_DEFINED
edisonn@google.comac03d912013-07-22 15:36:39 +000010
11#include "SkBitmap.h"
12#include "SkPoint.h"
13
14#define MAX_TRACKING_POINTS 100
15
16class SkTracker {
17public:
18 SkTracker() : fEnabled(false)
19 , fBreakOnAny(false)
20 , fCntExpectedTouched(0)
21 , fCntExpectedUntouched(0)
22 , fHits(0) {}
23
24 virtual ~SkTracker() {}
25
26 void clearPoints() {
27 fCntExpectedTouched = 0;
28 fCntExpectedUntouched = 0;
29 }
30
31 void enableTracking(bool b) {
32 fEnabled = b;
33 }
34
35 bool trackingEnabled() {
36 return fEnabled;
37 }
38
39 void any() {
40 fBreakOnAny = true;
41 }
42
43 void all() {
44 fBreakOnAny = false;
45 }
46
47 bool requireAllExpectedTouched() {
48 return !fBreakOnAny;
49 }
50
51 int cntExpectedTouched() {
52 return fCntExpectedTouched;
53 }
54
55 const SkIPoint* expectedTouched() {
56 return fExpectedTouched;
57 }
58
59 int cntExpectedUntouched() {
60 return fCntExpectedUntouched;
61 }
62
63 const SkIPoint* expectedUntouched() {
64 return fExpectedUntouched;
65 }
66
67 bool addExpectTouch(int x, int y) {
68 if (fCntExpectedTouched >= MAX_TRACKING_POINTS) {
69 return false;
70 }
71 if (found(x, y)) {
72 return false;
73 }
74 fExpectedTouched[fCntExpectedTouched] = SkIPoint::Make(x, y);
75 fCntExpectedTouched++;
76 return true;
77 }
78
79 bool addExpectUntouch(int x, int y) {
80 if (fCntExpectedUntouched >= MAX_TRACKING_POINTS) {
81 return false;
82 }
83 if (found(x, y)) {
84 return false;
85 }
86 fExpectedUntouched[fCntExpectedUntouched] = SkIPoint::Make(x, y);
87 fCntExpectedUntouched++;
88 return true;
89 }
90
91 void newFrame() {
92 fHits = 0;
93 }
94
95 int hits() {
96 return fHits;
97 }
98
99 void before(const SkBitmap& bitmap) {
100 if (fCntExpectedTouched == 0) {
101 return;
102 }
103
104 for (int i = 0 ; i < fCntExpectedTouched; i++) {
105 fBeforeTouched[i] = pickColor(bitmap, fExpectedTouched[i].x(), fExpectedTouched[i].y());
106 }
107 for (int i = 0 ; i < fCntExpectedUntouched; i++) {
108 fBeforeUntouched[i] = pickColor(bitmap, fExpectedUntouched[i].x(), fExpectedUntouched[i].y());
109 }
110 }
111
112 // any/all of the expected touched has to be changed, and all expected untouched must be intact
113 void after(const SkBitmap& bitmap) {
114 if (fCntExpectedTouched == 0) {
115 return;
116 }
117
118 bool doBreak;
119 if (fBreakOnAny) {
120 doBreak = false;
121 for (int i = 0 ; i < fCntExpectedTouched; i++) {
122 doBreak = doBreak || fBeforeTouched[i] != pickColor(bitmap, fExpectedTouched[i].x(), fExpectedTouched[i].y());
123 }
124 } else {
125 doBreak = true;
126 for (int i = 0 ; i < fCntExpectedTouched; i++) {
127 doBreak = doBreak && fBeforeTouched[i] != pickColor(bitmap, fExpectedTouched[i].x(), fExpectedTouched[i].y());
128 }
129 }
130
131 for (int i = 0 ; i < fCntExpectedUntouched; i++) {
132 doBreak = doBreak && fBeforeUntouched[i] == pickColor(bitmap, fExpectedUntouched[i].x(), fExpectedUntouched[i].y());
133 }
134
135 if (doBreak) {
136 fHits++;
137 if (fEnabled) {
138 breakExecution();
139 }
140 }
141 }
142
143private:
144 inline SkColor pickColor(const SkBitmap& bitmap, int x, int y) {
145 return bitmap.getColor(x, y);
146 }
147
148 void breakExecution() {
149 printf("break;\n");
150 }
151
152 inline bool found(int x, int y) {
153 for (int i = 0 ; i < fCntExpectedTouched; i++) {
154 if (x == fExpectedTouched[i].x() && y == fExpectedTouched[i].y()) {
155 return true;
156 }
157 }
158 for (int i = 0 ; i < fCntExpectedUntouched; i++) {
159 if (x == fExpectedUntouched[i].x() && y == fExpectedUntouched[i].y()) {
160 return true;
161 }
162 }
163 return false;
164 }
165
166
167 bool fEnabled;
168 // break on any change on expected touched or all.
169 bool fBreakOnAny;
170 SkIPoint fExpectedTouched[MAX_TRACKING_POINTS];
171 SkColor fBeforeTouched[MAX_TRACKING_POINTS];
172 int fCntExpectedTouched;
173
174 SkIPoint fExpectedUntouched[MAX_TRACKING_POINTS];
175 SkColor fBeforeUntouched[MAX_TRACKING_POINTS];
176 int fCntExpectedUntouched;
177
178 int fHits;
179};
180
edisonn@google.comcf2cfa12013-08-21 16:31:37 +0000181#endif // SkTracker_DEFINED