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"
|
|
|
|
#include "tools/Registry.h"
|
2019-07-15 18:01:37 +00:00
|
|
|
#include "tools/SkMetaData.h"
|
2019-08-29 14:39:22 +00:00
|
|
|
#include "tools/skui/InputState.h"
|
|
|
|
#include "tools/skui/ModifierKey.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
|
2020-04-22 15:09:30 +00:00
|
|
|
#include <functional>
|
|
|
|
|
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:
|
2020-04-22 15:09:30 +00:00
|
|
|
Click() {}
|
|
|
|
Click(std::function<bool(Click*)> f) : fFunc(f), fHasFunc(true) {}
|
2019-07-12 15:32:43 +00:00
|
|
|
virtual ~Click() = default;
|
2020-04-22 15:09:30 +00:00
|
|
|
|
2019-07-12 15:32:43 +00:00
|
|
|
SkPoint fOrig = {0, 0};
|
|
|
|
SkPoint fPrev = {0, 0};
|
|
|
|
SkPoint fCurr = {0, 0};
|
2019-08-29 14:39:22 +00:00
|
|
|
skui::InputState fState = skui::InputState::kDown;
|
|
|
|
skui::ModifierKey fModifierKeys = skui::ModifierKey::kNone;
|
2018-08-08 15:36:17 +00:00
|
|
|
SkMetaData fMeta;
|
2020-04-22 15:09:30 +00:00
|
|
|
|
|
|
|
std::function<bool(Click*)> fFunc;
|
|
|
|
bool fHasFunc = false;
|
2018-08-08 15:36:17 +00:00
|
|
|
};
|
2019-08-29 14:39:22 +00:00
|
|
|
bool mouse(SkPoint point, skui::InputState clickState, skui::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-08-29 14:39:22 +00:00
|
|
|
virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::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
|