epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
yangsu@google.com | a8a42e2 | 2011-06-16 20:49:55 +0000 | [diff] [blame] | 8 | #include "SkDebuggerViews.h" |
| 9 | |
| 10 | SkCommandListView::SkCommandListView() { |
| 11 | fBGColor = 0xFFBBBBBB; |
| 12 | fTopIndex = 0; |
| 13 | fHighlight = 0; |
| 14 | |
| 15 | SkPaint p; |
| 16 | p.setTextSize(SkIntToScalar(SkDebugger_TextSize)); |
| 17 | fSpacing = p.getFontSpacing(); |
| 18 | fCentered = false; |
| 19 | fRange = (int)(this->height()/fSpacing) - 1; |
| 20 | } |
| 21 | |
| 22 | bool SkCommandListView::onEvent(const SkEvent& evt) { |
| 23 | if (evt.isType(SkDebugger_CommandType)) { |
| 24 | SkString msg(evt.findString(SkDebugger_Atom)); |
| 25 | fList.push_back(msg); |
| 26 | this->inval(NULL); |
| 27 | return true; |
| 28 | } |
| 29 | return this->INHERITED::onEvent(evt); |
| 30 | } |
| 31 | |
| 32 | void SkCommandListView::onSizeChange() { |
| 33 | fRange = (int)(this->height()/fSpacing) - 1; |
| 34 | this->INHERITED::onSizeChange(); |
| 35 | } |
| 36 | |
| 37 | void SkCommandListView::reinit() { |
| 38 | fList.clear(); |
| 39 | fTopIndex = 0; |
| 40 | fHighlight = 0; |
| 41 | } |
| 42 | |
| 43 | void SkCommandListView::alignCenter() { |
| 44 | if (!fCentered || fHighlight < fRange/2 || fHighlight > (fList.size() - fRange/2)) |
| 45 | return; |
| 46 | else { |
| 47 | if (fHighlight > (fTopIndex + fRange/2)) { |
| 48 | fTopIndex += fHighlight - (fTopIndex + fRange/2); |
| 49 | } |
| 50 | if (fHighlight < (fTopIndex + fRange/2)) { |
| 51 | fTopIndex -= (fTopIndex + fRange/2) - fHighlight; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | int SkCommandListView::nextItem() { |
| 57 | if (fHighlight < fList.size() - 1) |
| 58 | ++fHighlight; |
| 59 | if (fHighlight < fTopIndex || fHighlight > (fTopIndex + fRange)) { |
| 60 | fTopIndex = fHighlight; |
| 61 | } |
| 62 | if (fHighlight == (fTopIndex + fRange)) { |
| 63 | ++fTopIndex; |
| 64 | } |
| 65 | this->alignCenter(); |
| 66 | this->inval(NULL); |
| 67 | return fHighlight; |
| 68 | } |
| 69 | |
| 70 | int SkCommandListView::prevItem() { |
| 71 | if (fHighlight > 0) |
| 72 | --fHighlight; |
| 73 | if (fHighlight < fTopIndex || fHighlight > (fTopIndex + fRange)) { |
| 74 | fTopIndex = fHighlight; |
| 75 | } |
| 76 | this->alignCenter(); |
| 77 | this->inval(NULL); |
| 78 | return fHighlight; |
| 79 | } |
| 80 | |
| 81 | int SkCommandListView::scrollUp() { |
| 82 | if (fTopIndex > 0) |
| 83 | --fTopIndex; |
| 84 | this->inval(NULL); |
| 85 | return fHighlight; |
| 86 | } |
| 87 | |
| 88 | int SkCommandListView::scrollDown() { |
| 89 | if (fTopIndex < (fList.size() - 1)) |
| 90 | ++fTopIndex; |
| 91 | this->inval(NULL); |
| 92 | return fHighlight; |
| 93 | } |
| 94 | |
| 95 | void SkCommandListView::highlight(int index) { |
| 96 | SkASSERT(index >= 0 && index < fList.size()); |
| 97 | if (fHighlight != index) { |
| 98 | fHighlight = index; |
| 99 | this->alignCenter(); |
| 100 | this->inval(NULL); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | int SkCommandListView::selectHighlight(int ypos) { |
| 105 | int i = (int)(ypos/fSpacing) + fTopIndex; |
| 106 | if (i >= fList.size()) { |
| 107 | i = fList.size() - 1; |
| 108 | } |
| 109 | if (fHighlight != i) { |
| 110 | fHighlight = i; |
| 111 | this->alignCenter(); |
| 112 | this->inval(NULL); |
| 113 | } |
| 114 | return fHighlight; |
| 115 | } |
| 116 | |
| 117 | void SkCommandListView::toggleCentered() { |
| 118 | fCentered = !fCentered; |
| 119 | this->alignCenter(); |
| 120 | this->inval(NULL); |
| 121 | } |
| 122 | |
| 123 | void SkCommandListView::onDraw(SkCanvas* canvas) { |
| 124 | canvas->drawColor(fBGColor); |
| 125 | |
| 126 | SkPaint p; |
| 127 | p.setTextSize(SkIntToScalar(SkDebugger_TextSize)); |
| 128 | p.setAntiAlias(true); |
| 129 | |
| 130 | //draw highlight |
| 131 | int selected = fHighlight - fTopIndex; |
| 132 | SkRect r = {0, fSpacing * selected, this->width(), fSpacing * (selected+1)}; |
| 133 | p.setColor(0x880033DD); |
| 134 | canvas->drawRect(r, p); |
| 135 | |
| 136 | int endIndex = fTopIndex + fRange; |
| 137 | if (endIndex > fList.size()) |
| 138 | endIndex = fList.size(); |
| 139 | |
| 140 | p.setColor(0xFF000000); |
| 141 | int pos; |
| 142 | for (int i = fTopIndex; i < endIndex; ++i) { |
| 143 | pos = i - fTopIndex; |
| 144 | canvas->drawText(fList[i].c_str(), fList[i].size(), |
| 145 | 0, fSpacing - 2 + fSpacing * pos, p); |
| 146 | } |
| 147 | this->INHERITED::onDraw(canvas); |
| 148 | } |