2018-08-08 15:36:17 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SampleCode_DEFINED
|
|
|
|
#define SampleCode_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/private/SkMacros.h"
|
2019-07-16 13:58:43 +00:00
|
|
|
#include "tools/InputState.h"
|
2019-07-08 20:07:57 +00:00
|
|
|
#include "tools/ModifierKey.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Registry.h"
|
2019-07-15 18:01:37 +00:00
|
|
|
#include "tools/SkMetaData.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
class Sample;
|
|
|
|
|
|
|
|
using SampleFactory = Sample* (*)();
|
|
|
|
using SampleRegistry = sk_tools::Registry<SampleFactory>;
|
|
|
|
|
|
|
|
#define DEF_SAMPLE(code) \
|
|
|
|
static Sample* SK_MACRO_APPEND_LINE(F_)() { code } \
|
|
|
|
static SampleRegistry SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-07-12 15:32:43 +00:00
|
|
|
class Sample {
|
2018-08-08 15:36:17 +00:00
|
|
|
public:
|
|
|
|
Sample()
|
|
|
|
: fBGColor(SK_ColorWHITE)
|
|
|
|
, fWidth(0), fHeight(0)
|
|
|
|
, fHaveCalledOnceBeforeDraw(false)
|
|
|
|
{}
|
|
|
|
|
2019-07-12 15:32:43 +00:00
|
|
|
virtual ~Sample() = default;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
SkScalar width() const { return fWidth; }
|
|
|
|
SkScalar height() const { return fHeight; }
|
|
|
|
void setSize(SkScalar width, SkScalar height);
|
|
|
|
void setSize(const SkPoint& size) { this->setSize(size.fX, size.fY); }
|
|
|
|
void setWidth(SkScalar width) { this->setSize(width, fHeight); }
|
|
|
|
void setHeight(SkScalar height) { this->setSize(fWidth, height); }
|
|
|
|
|
|
|
|
/** Call this to have the view draw into the specified canvas. */
|
|
|
|
virtual void draw(SkCanvas* canvas);
|
|
|
|
|
2019-07-03 19:53:04 +00:00
|
|
|
virtual bool onChar(SkUnichar) { return false; }
|
|
|
|
|
2019-07-12 15:32:43 +00:00
|
|
|
// Click handling
|
2018-08-08 15:36:17 +00:00
|
|
|
class Click {
|
|
|
|
public:
|
2019-07-12 15:32:43 +00:00
|
|
|
virtual ~Click() = default;
|
|
|
|
SkPoint fOrig = {0, 0};
|
|
|
|
SkPoint fPrev = {0, 0};
|
|
|
|
SkPoint fCurr = {0, 0};
|
|
|
|
InputState fState = InputState::kDown;
|
2019-07-08 20:07:57 +00:00
|
|
|
ModifierKey fModifierKeys = ModifierKey::kNone;
|
2018-08-08 15:36:17 +00:00
|
|
|
SkMetaData fMeta;
|
|
|
|
};
|
2019-07-12 15:32:43 +00:00
|
|
|
bool mouse(SkPoint point, InputState clickState, ModifierKey modifierKeys);
|
2018-08-08 15:36:17 +00:00
|
|
|
|
|
|
|
void setBGColor(SkColor color) { fBGColor = color; }
|
2019-07-11 20:32:53 +00:00
|
|
|
bool animate(double nanos) { return this->onAnimate(nanos); }
|
2018-08-08 15:36:17 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
virtual SkString name() = 0;
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
protected:
|
|
|
|
/** Override to be notified of size changes. Overriders must call the super class. */
|
|
|
|
virtual void onSizeChange();
|
|
|
|
|
|
|
|
/** Override this if you might handle the click */
|
2019-07-08 20:07:57 +00:00
|
|
|
virtual Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi);
|
2018-08-08 15:36:17 +00:00
|
|
|
|
|
|
|
/** Override to track clicks. Return true as long as you want to track the pen/mouse. */
|
|
|
|
virtual bool onClick(Click*);
|
|
|
|
|
|
|
|
virtual void onDrawBackground(SkCanvas*);
|
|
|
|
virtual void onDrawContent(SkCanvas*) = 0;
|
2019-07-11 20:32:53 +00:00
|
|
|
virtual bool onAnimate(double /*nanos*/) { return false; }
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual void onOnceBeforeDraw() {}
|
|
|
|
|
|
|
|
private:
|
2019-07-12 15:32:43 +00:00
|
|
|
std::unique_ptr<Click> fClick;
|
2018-08-08 15:36:17 +00:00
|
|
|
SkColor fBGColor;
|
|
|
|
SkScalar fWidth, fHeight;
|
|
|
|
bool fHaveCalledOnceBeforeDraw;
|
2019-07-12 15:32:43 +00:00
|
|
|
|
|
|
|
Sample(const Sample&) = delete;
|
|
|
|
Sample& operator=(const Sample&) = delete;
|
2018-08-08 15:36:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|