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"
|
2010-12-20 18:26:13 +00:00
|
|
|
#include "SkKey.h"
|
2011-04-22 01:59:09 +00:00
|
|
|
#include "SkView.h"
|
2012-06-08 15:35:03 +00:00
|
|
|
#include "SkOSMenu.h"
|
2015-02-02 20:55:02 +00:00
|
|
|
|
2011-07-06 13:59:47 +00:00
|
|
|
class GrContext;
|
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_));
|
|
|
|
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class SampleCode {
|
|
|
|
public:
|
2010-12-20 18:26:13 +00:00
|
|
|
static bool KeyQ(const SkEvent&, SkKey* outKey);
|
|
|
|
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
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static bool PrefSizeQ(const SkEvent&);
|
|
|
|
static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
|
2010-12-20 18:26:13 +00:00
|
|
|
|
|
|
|
static bool FastTextQ(const SkEvent&);
|
|
|
|
|
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-01-09 18:06:39 +00:00
|
|
|
SkView* operator() () const SK_OVERRIDE;
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-10-31 14:18:20 +00:00
|
|
|
private:
|
|
|
|
SkViewCreateFunc fCreateFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
class GM;
|
|
|
|
}
|
|
|
|
|
|
|
|
// factory function that creates a skiagm::GM
|
|
|
|
typedef skiagm::GM* (*GMFactoryFunc)(void*);
|
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
// Takes a GM factory function and implements the SkViewFactory interface
|
2011-10-31 14:18:20 +00:00
|
|
|
// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
|
|
|
|
// the SampleView interface to skiagm::GM.
|
|
|
|
class SkGMSampleViewFactory : public SkViewFactory {
|
|
|
|
public:
|
|
|
|
SkGMSampleViewFactory(GMFactoryFunc func);
|
2015-01-09 18:06:39 +00:00
|
|
|
SkView* operator() () const SK_OVERRIDE;
|
2011-10-31 14:18:20 +00:00
|
|
|
private:
|
|
|
|
GMFactoryFunc fFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkViewRegister : public SkRefCnt {
|
|
|
|
public:
|
|
|
|
explicit SkViewRegister(SkViewFactory*);
|
|
|
|
explicit SkViewRegister(SkViewCreateFunc);
|
|
|
|
explicit SkViewRegister(GMFactoryFunc);
|
|
|
|
|
|
|
|
~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()
|
|
|
|
: fPipeState(SkOSMenu::kOffState)
|
|
|
|
, fBGColor(SK_ColorWHITE)
|
|
|
|
, fRepeatCount(1)
|
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*);
|
2011-04-22 14:10:48 +00:00
|
|
|
static bool SetRepeatDraw(SkView*, int count);
|
2012-06-08 15:35:03 +00:00
|
|
|
static bool SetUsePipe(SkView*, SkOSMenu::TriState);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2011-08-08 15:37:23 +00:00
|
|
|
/**
|
|
|
|
* Call this to request menu items from a SampleView.
|
2012-08-23 18:19:56 +00:00
|
|
|
* Subclassing notes: A subclass of SampleView can overwrite this method
|
2011-08-08 15:37:23 +00:00
|
|
|
* to add new items of various types to the menu and change its title.
|
|
|
|
* The events attached to any new menu items must be handled in its onEvent
|
2012-08-23 18:19:56 +00:00
|
|
|
* method. See SkOSMenu.h for helper functions.
|
2011-08-08 15:37:23 +00:00
|
|
|
*/
|
|
|
|
virtual void requestMenu(SkOSMenu* menu) {}
|
2011-04-22 01:59:09 +00:00
|
|
|
|
2013-12-10 21:51:06 +00:00
|
|
|
virtual void onTileSizeChanged(const SkSize& tileSize) {}
|
|
|
|
|
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 onEvent(const SkEvent& evt);
|
|
|
|
virtual bool onQuery(SkEvent* evt);
|
2011-08-08 15:37:23 +00:00
|
|
|
virtual void draw(SkCanvas*);
|
2011-04-22 01:59:09 +00:00
|
|
|
virtual void onDraw(SkCanvas*);
|
|
|
|
|
2012-06-08 15:35:03 +00:00
|
|
|
SkOSMenu::TriState fPipeState;
|
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:
|
|
|
|
int fRepeatCount;
|
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
|