SkProgressBarView.cpp revision 80bacfeb4bda06541e8695bd502229727bccfea
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc. 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkProgressBarView.h" 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkAnimator.h" 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkWidgetViews.h" 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTime.h" 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkSystemEventTypes.h" 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkProgressBarView::SkProgressBarView() 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru init_skin_anim(kProgress_SkinEnum, &fAnim); 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.setHostEventSink(this); 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fProgress = 0; 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fMax = 100; 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkProgressBarView::changeProgress(int diff) 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int newProg = fProgress + diff; 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (newProg > 0 && newProg < fMax) 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->setProgress(newProg); 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru //otherwise i'll just leave it as it is 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru //this implies that if a new max and progress are set, max must be set first 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*virtual*/ void SkProgressBarView::onDraw(SkCanvas* canvas) 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPaint paint; 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkAnimator::DifferenceType diff = fAnim.draw(canvas, &paint, SkTime::GetMSecs()); 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (diff == SkAnimator::kDifferent) 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->inval(NULL); 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru else if (diff == SkAnimator::kPartiallyDifferent) 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru { 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkRect bounds; 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.getInvalBounds(&bounds); 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->inval(&bounds); 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*virtual*/ bool SkProgressBarView::onEvent(const SkEvent& evt) 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (evt.isType(SK_EventType_Inval)) 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru { 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->inval(NULL); 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return true; 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (evt.isType("recommendDim")) 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru { 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkScalar height; 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (evt.findScalar("y", &height)) 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->setHeight(height); 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return true; 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return this->INHERITED::onEvent(evt); 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*virtual*/ void SkProgressBarView::onInflate(const SkDOM& dom, const SkDOM::Node* node) 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->INHERITED::onInflate(dom, node); 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int32_t temp; 6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (dom.findS32(node, "max", &temp)) 7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->setMax(temp); 7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (dom.findS32(node, "progress", &temp)) 7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->setProgress(temp); 7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*virtual*/ void SkProgressBarView::onSizeChange() 7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru this->INHERITED::onSizeChange(); 7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent evt("user"); 7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru evt.setString("id", "setDim"); 8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru evt.setScalar("dimX", this->width()); 8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru evt.setScalar("dimY", this->height()); 8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.doUserEvent(evt); 8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkProgressBarView::reset() 8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fProgress = 0; 8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent e("user"); 8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru e.setString("id", "reset"); 9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.doUserEvent(e); 9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkProgressBarView::setMax(int max) 9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fMax = max; 9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent e("user"); 9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru e.setString("id", "setMax"); 9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru e.setS32("newMax", max); 9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.doUserEvent(e); 10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkProgressBarView::setProgress(int progress) 10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{ 10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fProgress = progress; 10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkEvent e("user"); 10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru e.setString("id", "setProgress"); 10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru e.setS32("newProgress", progress); 10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fAnim.doUserEvent(e); 10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 110