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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkWidget.h" |
| 9 | #include "SkCanvas.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 10 | #include "SkMath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkShader.h" |
| 12 | #include "SkInterpolator.h" |
| 13 | #include "SkTime.h" |
| 14 | |
| 15 | SkProgressView::SkProgressView(uint32_t flags) : SkView(flags), fOnShader(NULL), fOffShader(NULL) |
| 16 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | fValue = 0; |
| 18 | fMax = 0; |
| 19 | fInterp = NULL; |
| 20 | fDoInterp = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | SkProgressView::~SkProgressView() |
| 24 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 25 | delete fInterp; |
| 26 | SkSafeUnref(fOnShader); |
| 27 | SkSafeUnref(fOffShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void SkProgressView::setMax(U16CPU max) |
| 31 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | if (fMax != max) |
| 33 | { |
| 34 | fMax = SkToU16(max); |
| 35 | if (fValue > 0) |
| 36 | this->inval(NULL); |
| 37 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SkProgressView::setValue(U16CPU value) |
| 41 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 42 | if (fValue != value) |
| 43 | { |
| 44 | if (fDoInterp) |
| 45 | { |
| 46 | if (fInterp) |
| 47 | delete fInterp; |
| 48 | fInterp = new SkInterpolator(1, 2); |
| 49 | SkScalar x = (SkScalar)(fValue << 8); |
| 50 | fInterp->setKeyFrame(0, SkTime::GetMSecs(), &x, 0); |
| 51 | x = (SkScalar)(value << 8); |
| 52 | fInterp->setKeyFrame(1, SkTime::GetMSecs() + 333, &x); |
| 53 | } |
| 54 | fValue = SkToU16(value); |
| 55 | this->inval(NULL); |
| 56 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void SkProgressView::onDraw(SkCanvas* canvas) |
| 60 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | if (fMax == 0) |
| 62 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 64 | SkFixed percent; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | if (fInterp) |
| 67 | { |
| 68 | SkScalar x; |
| 69 | if (fInterp->timeToValues(SkTime::GetMSecs(), &x) == SkInterpolator::kFreezeEnd_Result) |
| 70 | { |
| 71 | delete fInterp; |
| 72 | fInterp = NULL; |
| 73 | } |
| 74 | percent = (SkFixed)x; // now its 16.8 |
| 75 | percent = SkMax32(0, SkMin32(percent, fMax << 8)); // now its pinned |
| 76 | percent = SkFixedDiv(percent, fMax << 8); // now its 0.16 |
| 77 | this->inval(NULL); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | U16CPU value = SkMax32(0, SkMin32(fValue, fMax)); |
| 82 | percent = SkFixedDiv(value, fMax); |
| 83 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | |
| 85 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 86 | SkRect r; |
| 87 | SkPaint p; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | r.set(0, 0, this->width(), this->height()); |
| 90 | p.setAntiAlias(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 92 | r.fRight = r.fLeft + SkScalarMul(r.width(), SkFixedToScalar(percent)); |
| 93 | p.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | p.setColor(SK_ColorDKGRAY); |
| 96 | p.setShader(fOnShader); |
| 97 | canvas->drawRect(r, p); |
| 98 | |
| 99 | p.setColor(SK_ColorWHITE); |
| 100 | p.setShader(fOffShader); |
| 101 | r.fLeft = r.fRight; |
| 102 | r.fRight = this->width() - SK_Scalar1; |
| 103 | if (r.width() > 0) |
| 104 | canvas->drawRect(r, p); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | #include "SkImageDecoder.h" |
| 108 | |
| 109 | static SkShader* inflate_shader(const char file[]) |
| 110 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | SkBitmap bm; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | return SkImageDecoder::DecodeFile(file, &bm) ? |
| 114 | SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode) : |
| 115 | NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void SkProgressView::onInflate(const SkDOM& dom, const SkDOM::Node* node) |
| 119 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | this->INHERITED::onInflate(dom, node); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | const char* s; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | SkASSERT(fOnShader == NULL); |
| 125 | SkASSERT(fOffShader == NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | if ((s = dom.findAttr(node, "src-on")) != NULL) |
| 128 | fOnShader = inflate_shader(s); |
| 129 | if ((s = dom.findAttr(node, "src-off")) != NULL) |
| 130 | fOffShader = inflate_shader(s); |
| 131 | (void)dom.findBool(node, "do-interp", &fDoInterp); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |