OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "OverView.h" | 8 #include "OverView.h" |
9 | |
10 #include "SampleCode.h" | 9 #include "SampleCode.h" |
11 | |
12 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
13 #include "SkView.h" | 11 #include "SkView.h" |
14 | 12 |
15 static const int N = 8; | |
16 static const SkScalar kWidth = SkIntToScalar(640); | |
17 static const SkScalar kHeight = SkIntToScalar(480); | |
18 static const char gIsOverview[] = "is-overview"; | 13 static const char gIsOverview[] = "is-overview"; |
19 | 14 |
| 15 static int to_lower(int c) { |
| 16 if ('A' <= c && c <= 'Z') { |
| 17 c = c - 'A' + 'a'; |
| 18 } |
| 19 return c; |
| 20 } |
| 21 |
| 22 static void make_lc(SkString* str) { |
| 23 char* ptr = str->writable_str(); |
| 24 while (*ptr) { |
| 25 *ptr = to_lower(*ptr); |
| 26 ptr += 1; |
| 27 } |
| 28 } |
| 29 |
| 30 static bool case_insensitive_find(const SkString& base, const SkString& sub) { |
| 31 SkString lcBase(base); |
| 32 SkString lcSub(sub); |
| 33 make_lc(&lcBase); |
| 34 make_lc(&lcSub); |
| 35 return lcBase.find(lcSub.c_str()) >= 0; |
| 36 } |
| 37 |
| 38 static bool draw_this_name(const SkString& name, const SkString& filter) { |
| 39 if (filter.isEmpty()) { |
| 40 return true; |
| 41 } |
| 42 return case_insensitive_find(name, filter); |
| 43 } |
| 44 |
20 class OverView : public SkView { | 45 class OverView : public SkView { |
21 public: | 46 public: |
22 OverView(int count, const SkViewFactory* factories[]); | 47 OverView(int count, const SkViewFactory* factories[]); |
23 virtual ~OverView(); | 48 virtual ~OverView(); |
24 | 49 |
25 protected: | 50 protected: |
26 // Overridden from SkEventSink: | |
27 bool onEvent(const SkEvent&) SK_OVERRIDE; | 51 bool onEvent(const SkEvent&) SK_OVERRIDE; |
28 bool onQuery(SkEvent* evt) SK_OVERRIDE { | 52 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
29 if (SampleCode::TitleQ(*evt)) { | 53 if (SampleCode::TitleQ(*evt)) { |
30 SampleCode::TitleR(evt, "Overview"); | 54 SampleCode::TitleR(evt, "Overview"); |
31 return true; | 55 return true; |
32 } | 56 } |
33 if (evt->isType(gIsOverview)) { | 57 if (evt->isType(gIsOverview)) { |
34 return true; | 58 return true; |
35 } | 59 } |
| 60 SkUnichar uni; |
| 61 if (SampleCode::CharQ(*evt, &uni)) { |
| 62 fMatchStr.appendUnichar(uni); |
| 63 this->inval(NULL); |
| 64 return true; |
| 65 } |
36 return this->INHERITED::onQuery(evt); | 66 return this->INHERITED::onQuery(evt); |
37 } | 67 } |
38 | 68 |
39 | 69 void onDraw(SkCanvas* canvas) SK_OVERRIDE; |
40 // Overridden from SkView: | |
41 void onSizeChange() SK_OVERRIDE; | |
42 void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
43 canvas->drawColor(SK_ColorLTGRAY); | |
44 } | |
45 | |
46 SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE; | |
47 | 70 |
48 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRID
E { | 71 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRID
E { |
49 return false; | 72 return false; |
50 } | 73 } |
51 | 74 |
52 Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE
{ | 75 Click* onFindClickHandler(SkScalar cx, SkScalar cy, unsigned modi) SK_OVERRI
DE { |
53 int ix = (int)(SkScalarDiv(x * N, kWidth)); | 76 const SkRect crect = SkRect::MakeXYWH(cx - 0.5f, cy - 0.5f, 1, 1); |
54 int iy = (int)(SkScalarDiv(y * N, kHeight)); | 77 SkPoint loc = this->start(); |
55 if (ix >= 0 && iy >= 0) { | 78 for (int i = 0; i < fCount; ++i) { |
56 SkEvent evt("set-curr-index"); | 79 if (draw_this_name(fNames[i], fMatchStr)) { |
57 evt.setFast32(iy * N + ix); | 80 if (this->bounds(loc).intersects(crect)) { |
58 this->sendEventToParents(evt); | 81 SkEvent evt("set-curr-index"); |
| 82 evt.setFast32(i); |
| 83 this->sendEventToParents(evt); |
| 84 break; |
| 85 } |
| 86 this->next(&loc); |
| 87 } |
59 } | 88 } |
60 return NULL; | 89 return NULL; |
61 } | 90 } |
62 | 91 |
63 private: | 92 private: |
64 int fCount; | 93 int fCount; |
65 const SkViewFactory** fFactories; | 94 const SkViewFactory** fFactories; |
| 95 SkString* fNames; |
| 96 SkString fMatchStr; |
| 97 SkPaint fNamePaint; |
| 98 SkPaint::FontMetrics fNameMetrics; |
| 99 SkScalar fNameW; |
| 100 SkScalar fNameH; |
| 101 |
| 102 SkRect bounds(const SkPoint& loc) const { |
| 103 return SkRect::MakeXYWH(loc.x(), loc.y() + fNameMetrics.fAscent, fNameW,
fNameH); |
| 104 } |
| 105 |
| 106 SkPoint start() const { |
| 107 return SkPoint::Make(10, -fNameMetrics.fTop); |
| 108 } |
| 109 |
| 110 void next(SkPoint* loc) const { |
| 111 loc->fY += fNameH; |
| 112 if (loc->fY > this->height() - fNameMetrics.fBottom) { |
| 113 loc->fY = -fNameMetrics.fTop; |
| 114 loc->fX += fNameW; |
| 115 } |
| 116 } |
66 | 117 |
67 typedef SkView INHERITED; | 118 typedef SkView INHERITED; |
68 }; | 119 }; |
69 | 120 |
70 SkView* create_overview(int count, const SkViewFactory* factories[]) { | 121 SkView* create_overview(int count, const SkViewFactory* factories[]) { |
71 return SkNEW_ARGS(OverView, (count, factories)); | 122 return SkNEW_ARGS(OverView, (count, factories)); |
72 } | 123 } |
73 | 124 |
74 bool is_overview(SkView* view) { | 125 bool is_overview(SkView* view) { |
75 SkEvent isOverview(gIsOverview); | 126 SkEvent isOverview(gIsOverview); |
76 return view->doQuery(&isOverview); | 127 return view->doQuery(&isOverview); |
77 } | 128 } |
78 | 129 |
79 OverView::OverView(int count, const SkViewFactory* factories[]) { | 130 OverView::OverView(int count, const SkViewFactory* factories[]) { |
80 fCount = count; | 131 fCount = count; |
81 fFactories = factories; | 132 fFactories = factories; |
| 133 |
| 134 fNames = new SkString[count]; |
| 135 for (int i = 0; i < count; ++i) { |
| 136 SkView* view = (*fFactories[i])(); |
| 137 if (view) { |
| 138 (void)SampleCode::RequestTitle(view, &fNames[i]); |
| 139 if (0 == fNames[i].find("GM:")) { |
| 140 fNames[i].remove(0, 3); |
| 141 } |
| 142 } |
| 143 } |
| 144 |
| 145 fNamePaint.setAntiAlias(true); |
| 146 fNamePaint.setTextSize(12); |
| 147 fNameW = 160; |
| 148 fNameH = fNamePaint.getFontMetrics(&fNameMetrics); |
82 } | 149 } |
83 | 150 |
84 OverView::~OverView() { | 151 OverView::~OverView() { |
| 152 delete[] fNames; |
85 } | 153 } |
86 | 154 |
87 bool OverView::onEvent(const SkEvent& evt) { | 155 bool OverView::onEvent(const SkEvent& evt) { |
88 return this->INHERITED::onEvent(evt); | 156 return this->INHERITED::onEvent(evt); |
89 } | 157 } |
90 | 158 |
91 void OverView::onSizeChange() { | 159 void OverView::onDraw(SkCanvas* canvas) { |
92 this->detachAllChildren(); | 160 SkPaint paint; |
| 161 paint.setColor(0xFFF8F8F8); |
| 162 canvas->drawPaint(paint); |
93 | 163 |
94 SkScalar locX = 0; | 164 SkPoint loc = this->start(); |
95 SkScalar locY = 0; | 165 for (int i = 0; i < fCount; ++i) { |
96 for (int i = 0; i < fCount; i++) { | 166 if (draw_this_name(fNames[i], fMatchStr)) { |
97 SkView* view = (*fFactories[i])(); | 167 canvas->drawRect(this->bounds(loc), paint); |
98 view->setVisibleP(true); | 168 canvas->drawText(fNames[i].c_str(), fNames[i].size(), loc.x(), loc.y
(), fNamePaint); |
99 this->attachChildToBack(view)->unref(); | 169 this->next(&loc); |
100 view->setLoc(locX, locY); | |
101 view->setSize(kWidth, kHeight); | |
102 locX += kWidth; | |
103 if ((i % N) == N - 1) { | |
104 locY += kHeight; | |
105 locX = 0; | |
106 } | 170 } |
107 } | 171 } |
108 } | 172 } |
109 | 173 |
110 SkCanvas* OverView::beforeChildren(SkCanvas* canvas) { | |
111 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N); | |
112 return canvas; | |
113 } | |
OLD | NEW |