2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2009-11-03 15:33:14 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkUnitMappers.h"
|
|
|
|
#include "SkCubicInterval.h"
|
|
|
|
|
2010-02-08 21:45:03 +00:00
|
|
|
#include "SkWidgetViews.h"
|
|
|
|
|
|
|
|
static SkStaticTextView* make_textview(SkView* parent,
|
|
|
|
const SkRect& bounds,
|
|
|
|
const SkPaint& paint) {
|
|
|
|
SkStaticTextView* view = new SkStaticTextView;
|
|
|
|
view->setMode(SkStaticTextView::kFixedSize_Mode);
|
|
|
|
view->setPaint(paint);
|
|
|
|
view->setVisibleP(true);
|
|
|
|
view->setSize(bounds.width(), bounds.height());
|
|
|
|
view->setLoc(bounds.fLeft, bounds.fTop);
|
|
|
|
parent->attachChildToFront(view)->unref();
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_scalar(SkStaticTextView* view, SkScalar value) {
|
|
|
|
SkString str;
|
|
|
|
str.appendScalar(value);
|
|
|
|
view->setText(str);
|
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
class UnitMapperView : public SampleView {
|
2009-11-03 15:33:14 +00:00
|
|
|
SkPoint fPts[4];
|
|
|
|
SkMatrix fMatrix;
|
2010-02-08 21:45:03 +00:00
|
|
|
SkStaticTextView* fViews[4];
|
|
|
|
|
|
|
|
void setViews() {
|
|
|
|
set_scalar(fViews[0], fPts[1].fX);
|
|
|
|
set_scalar(fViews[1], fPts[1].fY);
|
|
|
|
set_scalar(fViews[2], fPts[2].fX);
|
|
|
|
set_scalar(fViews[3], fPts[2].fY);
|
|
|
|
}
|
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
public:
|
|
|
|
UnitMapperView() {
|
|
|
|
fPts[0].set(0, 0);
|
|
|
|
fPts[1].set(SK_Scalar1 / 3, SK_Scalar1 / 3);
|
|
|
|
fPts[2].set(SK_Scalar1 * 2 / 3, SK_Scalar1 * 2 / 3);
|
|
|
|
fPts[3].set(SK_Scalar1, SK_Scalar1);
|
|
|
|
|
|
|
|
fMatrix.setScale(SK_Scalar1 * 200, -SK_Scalar1 * 200);
|
|
|
|
fMatrix.postTranslate(SkIntToScalar(100), SkIntToScalar(300));
|
2010-02-08 21:45:03 +00:00
|
|
|
|
|
|
|
SkRect r = {
|
|
|
|
SkIntToScalar(350), SkIntToScalar(100),
|
|
|
|
SkIntToScalar(500), SkIntToScalar(130)
|
|
|
|
};
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTextSize(SkIntToScalar(25));
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
fViews[i] = make_textview(this, r, paint);
|
|
|
|
r.offset(0, r.height());
|
|
|
|
}
|
|
|
|
this->setViews();
|
2009-11-03 15:33:14 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "UnitMapper");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
2009-11-03 15:33:14 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(0xFF8888FF);
|
|
|
|
|
|
|
|
SkRect r = { 0, 0, SK_Scalar1, SK_Scalar1 };
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
canvas->concat(fMatrix);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
paint.setStrokeCap(SkPaint::kRound_Cap);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
SkPath path;
|
|
|
|
path.moveTo(fPts[0]);
|
|
|
|
path.cubicTo(fPts[1], fPts[2], fPts[3]);
|
|
|
|
canvas->drawPath(path, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
paint.setStrokeWidth(0);
|
|
|
|
canvas->drawLine(0, 0, SK_Scalar1, SK_Scalar1, paint);
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorBLUE);
|
|
|
|
paint.setStrokeWidth(SK_Scalar1 / 60);
|
|
|
|
for (int i = 0; i < 50; i++) {
|
|
|
|
SkScalar x = i * SK_Scalar1 / 49;
|
|
|
|
canvas->drawPoint(x, SkEvalCubicInterval(&fPts[1], x), paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
paint.setStrokeWidth(SK_Scalar1 / 20);
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawPoints(SkCanvas::kPoints_PointMode, 2, &fPts[1], paint);
|
|
|
|
}
|
|
|
|
|
2012-06-04 13:22:14 +00:00
|
|
|
bool invertPt(SkScalar x, SkScalar y, SkPoint* result) {
|
|
|
|
if (NULL == result)
|
|
|
|
return true;
|
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
SkMatrix m;
|
2012-06-04 13:22:14 +00:00
|
|
|
if (!fMatrix.invert(&m)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mapXY(x, y, result);
|
|
|
|
return true;
|
2009-11-03 15:33:14 +00:00
|
|
|
}
|
|
|
|
|
2010-02-08 21:45:03 +00:00
|
|
|
int hittest(SkScalar x, SkScalar y) {
|
2009-11-03 15:33:14 +00:00
|
|
|
SkPoint target = { x, y };
|
|
|
|
SkPoint pts[2] = { fPts[1], fPts[2] };
|
|
|
|
fMatrix.mapPoints(pts, 2);
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (SkPoint::Distance(pts[i], target) < SkIntToScalar(4)) {
|
2010-02-08 21:45:03 +00:00
|
|
|
return i + 1;
|
2009-11-03 15:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-08 21:45:03 +00:00
|
|
|
return -1;
|
2009-11-03 15:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
|
2010-02-08 21:45:03 +00:00
|
|
|
fDragIndex = hittest(x, y);
|
|
|
|
return fDragIndex >= 0 ? new Click(this) : NULL;
|
2009-11-03 15:33:14 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
virtual bool onClick(Click* click) {
|
2010-02-08 21:45:03 +00:00
|
|
|
if (fDragIndex >= 0) {
|
2012-08-23 18:19:56 +00:00
|
|
|
if (!invertPt(click->fCurr.fX, click->fCurr.fY,
|
2012-06-04 13:22:14 +00:00
|
|
|
&fPts[fDragIndex])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-08 21:45:03 +00:00
|
|
|
this->setViews();
|
2009-11-03 15:33:14 +00:00
|
|
|
this->inval(NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-11-03 15:33:14 +00:00
|
|
|
private:
|
2010-02-08 21:45:03 +00:00
|
|
|
int fDragIndex;
|
2009-11-03 15:33:14 +00:00
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-11-03 15:33:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new UnitMapperView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|