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 bool case_insensitive_eq(int charA, int charB) { | |
23 return to_lower(charA) == to_lower(charB); | |
24 } | |
25 | |
26 static bool case_insensitive_find(const char str[], const char sub[]) { | |
27 while (*str) { | |
28 if (case_insensitive_eq(*str, *sub)) { | |
29 for (;;) { | |
30 str++; | |
31 sub++; | |
32 if (0 == *sub) { | |
33 return true; | |
34 } | |
35 if (!case_insensitive_eq(*str, *sub)) { | |
36 return false; | |
37 } | |
38 } | |
39 } | |
40 str++; | |
41 } | |
42 return false; | |
43 } | |
44 | |
45 static bool draw_this_name(const SkString& name, const SkString& filter) { | |
46 if (filter.isEmpty()) { | |
47 return true; | |
48 } | |
49 return case_insensitive_find(name.c_str(), filter.c_str()); | |
djsollen
2015/02/03 18:22:38
why not just use strcasecomp or call tolower on bo
reed1
2015/02/03 19:53:16
slower (due to mallocs) but Done.
| |
50 } | |
51 | |
20 class OverView : public SkView { | 52 class OverView : public SkView { |
21 public: | 53 public: |
22 OverView(int count, const SkViewFactory* factories[]); | 54 OverView(int count, const SkViewFactory* factories[]); |
23 virtual ~OverView(); | 55 virtual ~OverView(); |
24 | 56 |
25 protected: | 57 protected: |
26 // Overridden from SkEventSink: | |
27 bool onEvent(const SkEvent&) SK_OVERRIDE; | 58 bool onEvent(const SkEvent&) SK_OVERRIDE; |
28 bool onQuery(SkEvent* evt) SK_OVERRIDE { | 59 bool onQuery(SkEvent* evt) SK_OVERRIDE { |
29 if (SampleCode::TitleQ(*evt)) { | 60 if (SampleCode::TitleQ(*evt)) { |
30 SampleCode::TitleR(evt, "Overview"); | 61 SampleCode::TitleR(evt, "Overview"); |
31 return true; | 62 return true; |
32 } | 63 } |
33 if (evt->isType(gIsOverview)) { | 64 if (evt->isType(gIsOverview)) { |
34 return true; | 65 return true; |
35 } | 66 } |
67 SkUnichar uni; | |
68 if (SampleCode::CharQ(*evt, &uni)) { | |
69 fMatchStr.appendUnichar(uni); | |
70 this->inval(NULL); | |
71 return true; | |
72 } | |
36 return this->INHERITED::onQuery(evt); | 73 return this->INHERITED::onQuery(evt); |
37 } | 74 } |
38 | 75 |
39 | 76 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 | 77 |
48 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRID E { | 78 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRID E { |
49 return false; | 79 return false; |
50 } | 80 } |
51 | 81 |
52 Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE { | 82 Click* onFindClickHandler(SkScalar cx, SkScalar cy, unsigned modi) SK_OVERRI DE { |
53 int ix = (int)(SkScalarDiv(x * N, kWidth)); | 83 const SkRect crect = SkRect::MakeXYWH(cx - 0.5f, cy - 0.5f, 1, 1); |
54 int iy = (int)(SkScalarDiv(y * N, kHeight)); | 84 SkPoint loc = this->start(); |
55 if (ix >= 0 && iy >= 0) { | 85 for (int i = 0; i < fCount; ++i) { |
56 SkEvent evt("set-curr-index"); | 86 if (draw_this_name(fNames[i], fMatchStr)) { |
57 evt.setFast32(iy * N + ix); | 87 if (this->bounds(loc).intersects(crect)) { |
58 this->sendEventToParents(evt); | 88 SkEvent evt("set-curr-index"); |
89 evt.setFast32(i); | |
90 this->sendEventToParents(evt); | |
91 break; | |
92 } | |
93 this->next(&loc); | |
94 } | |
59 } | 95 } |
60 return NULL; | 96 return NULL; |
61 } | 97 } |
62 | 98 |
63 private: | 99 private: |
64 int fCount; | 100 int fCount; |
65 const SkViewFactory** fFactories; | 101 const SkViewFactory** fFactories; |
102 SkString* fNames; | |
103 SkString fMatchStr; | |
104 SkPaint fNamePaint; | |
105 SkPaint::FontMetrics fNameMetrics; | |
106 SkScalar fNameW; | |
107 SkScalar fNameH; | |
108 | |
109 SkRect bounds(const SkPoint& loc) const { | |
110 return SkRect::MakeXYWH(loc.x(), loc.y() + fNameMetrics.fAscent, fNameW, fNameH); | |
111 } | |
112 | |
113 SkPoint start() const { | |
114 return SkPoint::Make(10, -fNameMetrics.fTop); | |
115 } | |
116 | |
117 void next(SkPoint* loc) const { | |
118 loc->fY += fNameH; | |
119 if (loc->fY > this->height() - fNameMetrics.fBottom) { | |
120 loc->fY = -fNameMetrics.fTop; | |
121 loc->fX += fNameW; | |
122 } | |
123 } | |
66 | 124 |
67 typedef SkView INHERITED; | 125 typedef SkView INHERITED; |
68 }; | 126 }; |
69 | 127 |
70 SkView* create_overview(int count, const SkViewFactory* factories[]) { | 128 SkView* create_overview(int count, const SkViewFactory* factories[]) { |
71 return SkNEW_ARGS(OverView, (count, factories)); | 129 return SkNEW_ARGS(OverView, (count, factories)); |
72 } | 130 } |
73 | 131 |
74 bool is_overview(SkView* view) { | 132 bool is_overview(SkView* view) { |
75 SkEvent isOverview(gIsOverview); | 133 SkEvent isOverview(gIsOverview); |
76 return view->doQuery(&isOverview); | 134 return view->doQuery(&isOverview); |
77 } | 135 } |
78 | 136 |
79 OverView::OverView(int count, const SkViewFactory* factories[]) { | 137 OverView::OverView(int count, const SkViewFactory* factories[]) { |
80 fCount = count; | 138 fCount = count; |
81 fFactories = factories; | 139 fFactories = factories; |
140 | |
141 fNames = new SkString[count]; | |
142 for (int i = 0; i < count; ++i) { | |
143 SkView* view = (*fFactories[i])(); | |
144 if (view) { | |
145 (void)SampleCode::RequestTitle(view, &fNames[i]); | |
146 if (0 == fNames[i].find("GM:")) { | |
147 fNames[i].remove(0, 3); | |
148 } | |
149 } | |
150 } | |
151 | |
152 fNamePaint.setAntiAlias(true); | |
153 fNamePaint.setTextSize(12); | |
154 fNameW = 160; | |
155 fNameH = fNamePaint.getFontMetrics(&fNameMetrics); | |
82 } | 156 } |
83 | 157 |
84 OverView::~OverView() { | 158 OverView::~OverView() { |
159 delete[] fNames; | |
85 } | 160 } |
86 | 161 |
87 bool OverView::onEvent(const SkEvent& evt) { | 162 bool OverView::onEvent(const SkEvent& evt) { |
88 return this->INHERITED::onEvent(evt); | 163 return this->INHERITED::onEvent(evt); |
89 } | 164 } |
90 | 165 |
91 void OverView::onSizeChange() { | 166 void OverView::onDraw(SkCanvas* canvas) { |
92 this->detachAllChildren(); | 167 SkPaint paint; |
168 paint.setColor(0xFFF8F8F8); | |
169 canvas->drawPaint(paint); | |
93 | 170 |
94 SkScalar locX = 0; | 171 SkPoint loc = this->start(); |
95 SkScalar locY = 0; | 172 for (int i = 0; i < fCount; ++i) { |
96 for (int i = 0; i < fCount; i++) { | 173 if (draw_this_name(fNames[i], fMatchStr)) { |
97 SkView* view = (*fFactories[i])(); | 174 canvas->drawRect(this->bounds(loc), paint); |
98 view->setVisibleP(true); | 175 canvas->drawText(fNames[i].c_str(), fNames[i].size(), loc.x(), loc.y (), fNamePaint); |
99 this->attachChildToBack(view)->unref(); | 176 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 } | 177 } |
107 } | 178 } |
108 } | 179 } |
109 | 180 |
110 SkCanvas* OverView::beforeChildren(SkCanvas* canvas) { | |
111 canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N); | |
112 return canvas; | |
113 } | |
OLD | NEW |