1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SampleCode_DEFINED
9#define SampleCode_DEFINED
10
11#include "SkColor.h"
12#include "SkEvent.h"
13#include "SkKey.h"
14#include "SkView.h"
15#include "SkOSMenu.h"
16
17class GrContext;
18class SkAnimTimer;
19
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_));
23
24
25class SampleCode {
26public:
27    static bool KeyQ(const SkEvent&, SkKey* outKey);
28    static bool CharQ(const SkEvent&, SkUnichar* outUni);
29
30    static bool TitleQ(const SkEvent&);
31    static void TitleR(SkEvent*, const char title[]);
32    static bool RequestTitle(SkView* view, SkString* title);
33
34    static bool PrefSizeQ(const SkEvent&);
35    static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
36
37    static bool FastTextQ(const SkEvent&);
38
39    friend class SampleWindow;
40};
41
42//////////////////////////////////////////////////////////////////////////////
43
44// interface that constructs SkViews
45class SkViewFactory : public SkRefCnt {
46public:
47    virtual SkView* operator() () const = 0;
48};
49
50typedef SkView* (*SkViewCreateFunc)();
51
52// wraps SkViewCreateFunc in SkViewFactory interface
53class SkFuncViewFactory : public SkViewFactory {
54public:
55    SkFuncViewFactory(SkViewCreateFunc func);
56    SkView* operator() () const override;
57
58private:
59    SkViewCreateFunc fCreateFunc;
60};
61
62namespace skiagm {
63class GM;
64}
65
66// factory function that creates a skiagm::GM
67typedef skiagm::GM* (*GMFactoryFunc)(void*);
68
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.
72class SkGMSampleViewFactory : public SkViewFactory {
73public:
74    SkGMSampleViewFactory(GMFactoryFunc func);
75    SkView* operator() () const override;
76private:
77    GMFactoryFunc fFunc;
78};
79
80class SkViewRegister : public SkRefCnt {
81public:
82    explicit SkViewRegister(SkViewFactory*);
83    explicit SkViewRegister(SkViewCreateFunc);
84    explicit SkViewRegister(GMFactoryFunc);
85
86    ~SkViewRegister() {
87        fFact->unref();
88    }
89
90    static const SkViewRegister* Head() { return gHead; }
91
92    SkViewRegister* next() const { return fChain; }
93    const SkViewFactory*   factory() const { return fFact; }
94
95private:
96    SkViewFactory*  fFact;
97    SkViewRegister* fChain;
98
99    static SkViewRegister* gHead;
100};
101
102///////////////////////////////////////////////////////////////////////////////
103
104class SampleView : public SkView {
105public:
106    SampleView()
107        : fPipeState(SkOSMenu::kOffState)
108        , fBGColor(SK_ColorWHITE)
109        , fRepeatCount(1)
110        , fHaveCalledOnceBeforeDraw(false)
111    {}
112
113    void setBGColor(SkColor color) { fBGColor = color; }
114    bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
115
116    static bool IsSampleView(SkView*);
117    static bool SetRepeatDraw(SkView*, int count);
118    static bool SetUsePipe(SkView*, SkOSMenu::TriState);
119
120    /**
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.
126     */
127    virtual void requestMenu(SkOSMenu* menu) {}
128
129    virtual void onTileSizeChanged(const SkSize& tileSize) {}
130
131protected:
132    virtual void onDrawBackground(SkCanvas*);
133    virtual void onDrawContent(SkCanvas*) = 0;
134    virtual bool onAnimate(const SkAnimTimer&) { return false; }
135    virtual void onOnceBeforeDraw() {}
136
137    // overrides
138    virtual bool onEvent(const SkEvent& evt);
139    virtual bool onQuery(SkEvent* evt);
140    virtual void onDraw(SkCanvas*);
141
142    SkOSMenu::TriState fPipeState;
143    SkColor fBGColor;
144
145private:
146    int fRepeatCount;
147    bool fHaveCalledOnceBeforeDraw;
148    typedef SkView INHERITED;
149};
150
151#endif
152