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"
|
2011-07-06 13:59:47 +00:00
|
|
|
class GrContext;
|
|
|
|
|
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&);
|
|
|
|
|
2009-11-23 21:07:51 +00:00
|
|
|
static SkMSec GetAnimTime();
|
2010-12-20 18:26:13 +00:00
|
|
|
static SkMSec GetAnimTimeDelta();
|
|
|
|
static SkScalar GetAnimSecondsDelta();
|
2009-11-23 21:07:51 +00:00
|
|
|
static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
|
2012-03-28 14:44:37 +00:00
|
|
|
// gives a sinusoidal value between 0 and amplitude
|
|
|
|
static SkScalar GetAnimSinScalar(SkScalar amplitude,
|
|
|
|
SkScalar periodInSec,
|
|
|
|
SkScalar phaseInSec = 0);
|
2011-07-06 13:59:47 +00:00
|
|
|
|
|
|
|
static GrContext* GetGr();
|
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);
|
|
|
|
virtual 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);
|
|
|
|
virtual SkView* operator() () const SK_OVERRIDE;
|
|
|
|
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:
|
2012-08-23 18:19:56 +00:00
|
|
|
SampleView() : fPipeState(SkOSMenu::kOffState),
|
2012-06-08 15:35:03 +00:00
|
|
|
fBGColor(SK_ColorWHITE), fRepeatCount(1) {
|
2011-05-11 05:58:58 +00:00
|
|
|
}
|
2011-04-22 01:59:09 +00:00
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
void setBGColor(SkColor color) { fBGColor = color; }
|
|
|
|
|
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
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void onDrawBackground(SkCanvas*);
|
|
|
|
virtual void onDrawContent(SkCanvas*) = 0;
|
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;
|
2011-05-11 05:58:58 +00:00
|
|
|
|
2011-04-22 01:59:09 +00:00
|
|
|
typedef SkView INHERITED;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
|