2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SampleCode_DEFINED
9 #define SampleCode_DEFINED
20 #define DEF_SAMPLE(code) \
21 static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \
22 static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
27 static bool KeyQ(const SkEvent&, SkKey* outKey);
28 static bool CharQ(const SkEvent&, SkUnichar* outUni);
30 static bool TitleQ(const SkEvent&);
31 static void TitleR(SkEvent*, const char title[]);
32 static bool RequestTitle(SkView* view, SkString* title);
34 static bool PrefSizeQ(const SkEvent&);
35 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
37 static bool FastTextQ(const SkEvent&);
39 friend class SampleWindow;
42 //////////////////////////////////////////////////////////////////////////////
44 // interface that constructs SkViews
45 class SkViewFactory : public SkRefCnt {
47 virtual SkView* operator() () const = 0;
50 typedef SkView* (*SkViewCreateFunc)();
52 // wraps SkViewCreateFunc in SkViewFactory interface
53 class SkFuncViewFactory : public SkViewFactory {
55 SkFuncViewFactory(SkViewCreateFunc func);
56 SkView* operator() () const SK_OVERRIDE;
59 SkViewCreateFunc fCreateFunc;
66 // factory function that creates a skiagm::GM
67 typedef skiagm::GM* (*GMFactoryFunc)(void*);
69 // Takes a GM factory function and implements the SkViewFactory interface
70 // by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
71 // the SampleView interface to skiagm::GM.
72 class SkGMSampleViewFactory : public SkViewFactory {
74 SkGMSampleViewFactory(GMFactoryFunc func);
75 SkView* operator() () const SK_OVERRIDE;
80 class SkViewRegister : public SkRefCnt {
82 explicit SkViewRegister(SkViewFactory*);
83 explicit SkViewRegister(SkViewCreateFunc);
84 explicit SkViewRegister(GMFactoryFunc);
90 static const SkViewRegister* Head() { return gHead; }
92 SkViewRegister* next() const { return fChain; }
93 const SkViewFactory* factory() const { return fFact; }
97 SkViewRegister* fChain;
99 static SkViewRegister* gHead;
102 ///////////////////////////////////////////////////////////////////////////////
104 class SampleView : public SkView {
107 : fPipeState(SkOSMenu::kOffState)
108 , fBGColor(SK_ColorWHITE)
110 , fHaveCalledOnceBeforeDraw(false)
113 void setBGColor(SkColor color) { fBGColor = color; }
114 bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
116 static bool IsSampleView(SkView*);
117 static bool SetRepeatDraw(SkView*, int count);
118 static bool SetUsePipe(SkView*, SkOSMenu::TriState);
121 * Call this to request menu items from a SampleView.
122 * Subclassing notes: A subclass of SampleView can overwrite this method
123 * to add new items of various types to the menu and change its title.
124 * The events attached to any new menu items must be handled in its onEvent
125 * method. See SkOSMenu.h for helper functions.
127 virtual void requestMenu(SkOSMenu* menu) {}
129 virtual void onTileSizeChanged(const SkSize& tileSize) {}
132 virtual void onDrawBackground(SkCanvas*);
133 virtual void onDrawContent(SkCanvas*) = 0;
134 virtual bool onAnimate(const SkAnimTimer&) { return false; }
135 virtual void onOnceBeforeDraw() {}
138 virtual bool onEvent(const SkEvent& evt);
139 virtual bool onQuery(SkEvent* evt);
140 virtual void draw(SkCanvas*);
141 virtual void onDraw(SkCanvas*);
143 SkOSMenu::TriState fPipeState;
148 bool fHaveCalledOnceBeforeDraw;
149 typedef SkView INHERITED;