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