1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SampleCode_DEFINED
11#define SampleCode_DEFINED
12
13#include "SkColor.h"
14#include "SkEvent.h"
15#include "SkKey.h"
16#include "SkView.h"
17#include "SkOSMenu.h"
18class GrContext;
19
20class SampleCode {
21public:
22    static bool KeyQ(const SkEvent&, SkKey* outKey);
23    static bool CharQ(const SkEvent&, SkUnichar* outUni);
24
25    static bool TitleQ(const SkEvent&);
26    static void TitleR(SkEvent*, const char title[]);
27    static bool RequestTitle(SkView* view, SkString* title);
28
29    static bool PrefSizeQ(const SkEvent&);
30    static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
31
32    static bool FastTextQ(const SkEvent&);
33
34    static SkMSec GetAnimTime();
35    static SkMSec GetAnimTimeDelta();
36    static SkScalar GetAnimSecondsDelta();
37    static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
38    // gives a sinusoidal value between 0 and amplitude
39    static SkScalar GetAnimSinScalar(SkScalar amplitude,
40                                     SkScalar periodInSec,
41                                     SkScalar phaseInSec = 0);
42};
43
44//////////////////////////////////////////////////////////////////////////////
45
46// interface that constructs SkViews
47class SkViewFactory : public SkRefCnt {
48public:
49    virtual SkView* operator() () const = 0;
50};
51
52typedef SkView* (*SkViewCreateFunc)();
53
54// wraps SkViewCreateFunc in SkViewFactory interface
55class SkFuncViewFactory : public SkViewFactory {
56public:
57    SkFuncViewFactory(SkViewCreateFunc func);
58    virtual SkView* operator() () const SK_OVERRIDE;
59
60private:
61    SkViewCreateFunc fCreateFunc;
62};
63
64namespace skiagm {
65class GM;
66}
67
68// factory function that creates a skiagm::GM
69typedef skiagm::GM* (*GMFactoryFunc)(void*);
70
71// Takes a GM factory function and implements the SkViewFactory interface
72// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
73// the SampleView interface to skiagm::GM.
74class SkGMSampleViewFactory : public SkViewFactory {
75public:
76    SkGMSampleViewFactory(GMFactoryFunc func);
77    virtual SkView* operator() () const SK_OVERRIDE;
78private:
79    GMFactoryFunc fFunc;
80};
81
82class SkViewRegister : public SkRefCnt {
83public:
84    explicit SkViewRegister(SkViewFactory*);
85    explicit SkViewRegister(SkViewCreateFunc);
86    explicit SkViewRegister(GMFactoryFunc);
87
88    ~SkViewRegister() {
89        fFact->unref();
90    }
91
92    static const SkViewRegister* Head() { return gHead; }
93
94    SkViewRegister* next() const { return fChain; }
95    const SkViewFactory*   factory() const { return fFact; }
96
97private:
98    SkViewFactory*  fFact;
99    SkViewRegister* fChain;
100
101    static SkViewRegister* gHead;
102};
103
104///////////////////////////////////////////////////////////////////////////////
105
106class SampleView : public SkView {
107public:
108    SampleView()
109        : fPipeState(SkOSMenu::kOffState)
110        , fBGColor(SK_ColorWHITE)
111        , fRepeatCount(1)
112    {}
113
114    void setBGColor(SkColor color) { fBGColor = color; }
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
135    // overrides
136    virtual bool onEvent(const SkEvent& evt);
137    virtual bool onQuery(SkEvent* evt);
138    virtual void draw(SkCanvas*);
139    virtual void onDraw(SkCanvas*);
140
141    SkOSMenu::TriState fPipeState;
142    SkColor fBGColor;
143
144private:
145    int fRepeatCount;
146
147    typedef SkView INHERITED;
148};
149
150#endif
151