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.
|
|
|
|
*/
|
2011-10-31 14:18:20 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SampleCode_DEFINED
|
|
|
|
#define SampleCode_DEFINED
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
#include "SkColor.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkEvent.h"
|
2011-04-22 01:59:09 +00:00
|
|
|
#include "SkView.h"
|
2015-02-02 20:55:02 +00:00
|
|
|
|
|
|
|
class SkAnimTimer;
|
2011-07-06 13:59:47 +00:00
|
|
|
|
2014-12-16 16:07:43 +00:00
|
|
|
#define DEF_SAMPLE(code) \
|
|
|
|
static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
|
|
|
|
static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
|
|
|
|
|
2016-06-16 16:52:35 +00:00
|
|
|
static const char gCharEvtName[] = "SampleCode_Char_Event";
|
|
|
|
static const char gTitleEvtName[] = "SampleCode_Title_Event";
|
2014-12-16 16:07:43 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class SampleCode {
|
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
static bool CharQ(const SkEvent&, SkUnichar* outUni);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static bool TitleQ(const SkEvent&);
|
|
|
|
static void TitleR(SkEvent*, const char title[]);
|
2011-08-08 15:37:23 +00:00
|
|
|
static bool RequestTitle(SkView* view, SkString* title);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-02-02 03:01:04 +00:00
|
|
|
friend class SampleWindow;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
// interface that constructs SkViews
|
|
|
|
class SkViewFactory : public SkRefCnt {
|
2012-08-23 18:19:56 +00:00
|
|
|
public:
|
2011-10-31 14:18:20 +00:00
|
|
|
virtual SkView* operator() () const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SkView* (*SkViewCreateFunc)();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
// wraps SkViewCreateFunc in SkViewFactory interface
|
|
|
|
class SkFuncViewFactory : public SkViewFactory {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2011-10-31 14:18:20 +00:00
|
|
|
SkFuncViewFactory(SkViewCreateFunc func);
|
2015-03-26 01:17:31 +00:00
|
|
|
SkView* operator() () const override;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
private:
|
|
|
|
SkViewCreateFunc fCreateFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkViewRegister : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
explicit SkViewRegister(SkViewFactory*);
|
|
|
|
explicit SkViewRegister(SkViewCreateFunc);
|
|
|
|
|
|
|
|
~SkViewRegister() {
|
|
|
|
fFact->unref();
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static const SkViewRegister* Head() { return gHead; }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkViewRegister* next() const { return fChain; }
|
2011-10-31 14:18:20 +00:00
|
|
|
const SkViewFactory* factory() const { return fFact; }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-10-31 14:18:20 +00:00
|
|
|
SkViewFactory* fFact;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkViewRegister* fChain;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static SkViewRegister* gHead;
|
|
|
|
};
|
|
|
|
|
2011-04-22 01:59:09 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class SampleView : public SkView {
|
|
|
|
public:
|
2013-01-08 16:17:50 +00:00
|
|
|
SampleView()
|
2017-11-22 18:23:35 +00:00
|
|
|
: fBGColor(SK_ColorWHITE)
|
2015-02-25 17:04:04 +00:00
|
|
|
, fHaveCalledOnceBeforeDraw(false)
|
2014-06-03 17:53:59 +00:00
|
|
|
{}
|
2011-04-22 01:59:09 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
void setBGColor(SkColor color) { fBGColor = color; }
|
2015-02-02 20:55:02 +00:00
|
|
|
bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
|
2011-04-22 14:10:48 +00:00
|
|
|
|
2011-05-12 22:08:24 +00:00
|
|
|
static bool IsSampleView(SkView*);
|
2013-12-10 21:51:06 +00:00
|
|
|
|
2011-04-22 01:59:09 +00:00
|
|
|
protected:
|
|
|
|
virtual void onDrawBackground(SkCanvas*);
|
|
|
|
virtual void onDrawContent(SkCanvas*) = 0;
|
2015-02-02 20:55:02 +00:00
|
|
|
virtual bool onAnimate(const SkAnimTimer&) { return false; }
|
2015-02-25 17:04:04 +00:00
|
|
|
virtual void onOnceBeforeDraw() {}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-22 01:59:09 +00:00
|
|
|
// overrides
|
|
|
|
virtual bool onQuery(SkEvent* evt);
|
|
|
|
virtual void onDraw(SkCanvas*);
|
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
SkColor fBGColor;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-04-22 01:59:09 +00:00
|
|
|
private:
|
2015-02-25 17:04:04 +00:00
|
|
|
bool fHaveCalledOnceBeforeDraw;
|
2011-04-22 01:59:09 +00:00
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|