1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SampleCode_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SampleCode_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com#include "SkColor.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkEvent.h"
15f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com#include "SkKey.h"
162eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org#include "SkView.h"
17b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com#include "SkOSMenu.h"
183cec4d71e6f53fda99705427f9820d025cfbf64breed@google.comclass GrContext;
193cec4d71e6f53fda99705427f9820d025cfbf64breed@google.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SampleCode {
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
22f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static bool KeyQ(const SkEvent&, SkKey* outKey);
23f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static bool CharQ(const SkEvent&, SkUnichar* outUni);
24f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool TitleQ(const SkEvent&);
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void TitleR(SkEvent*, const char title[]);
27db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    static bool RequestTitle(SkView* view, SkString* title);
28ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool PrefSizeQ(const SkEvent&);
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
31f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
32f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static bool FastTextQ(const SkEvent&);
33f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
344417740a480786ca65379ba5b398c500ba34ee18reed@android.com    static SkMSec GetAnimTime();
35f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static SkMSec GetAnimTimeDelta();
36f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    static SkScalar GetAnimSecondsDelta();
374417740a480786ca65379ba5b398c500ba34ee18reed@android.com    static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
388500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    // gives a sinusoidal value between 0 and amplitude
398500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com    static SkScalar GetAnimSinScalar(SkScalar amplitude,
408500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com                                     SkScalar periodInSec,
418500322537c5158a4632be6805b0c4210a20558bbsalomon@google.com                                     SkScalar phaseInSec = 0);
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// interface that constructs SkViews
4748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewFactory : public SkRefCnt {
48ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.compublic:
4948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    virtual SkView* operator() () const = 0;
5048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com};
5148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
5248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef SkView* (*SkViewCreateFunc)();
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// wraps SkViewCreateFunc in SkViewFactory interface
5548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkFuncViewFactory : public SkViewFactory {
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
5748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkFuncViewFactory(SkViewCreateFunc func);
5848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    virtual SkView* operator() () const SK_OVERRIDE;
59ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
6048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate:
6148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkViewCreateFunc fCreateFunc;
6248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com};
6348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
6448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comnamespace skiagm {
6548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass GM;
6648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}
6748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
6848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// factory function that creates a skiagm::GM
6948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef skiagm::GM* (*GMFactoryFunc)(void*);
7048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
71ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com// Takes a GM factory function and implements the SkViewFactory interface
7248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
7348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// the SampleView interface to skiagm::GM.
7448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkGMSampleViewFactory : public SkViewFactory {
7548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic:
7648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkGMSampleViewFactory(GMFactoryFunc func);
7748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    virtual SkView* operator() () const SK_OVERRIDE;
7848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate:
7948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    GMFactoryFunc fFunc;
8048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com};
8148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
8248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewRegister : public SkRefCnt {
8348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic:
8448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    explicit SkViewRegister(SkViewFactory*);
8548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    explicit SkViewRegister(SkViewCreateFunc);
8648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    explicit SkViewRegister(GMFactoryFunc);
8748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com
8848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    ~SkViewRegister() {
8948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com        fFact->unref();
9048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    }
91ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const SkViewRegister* Head() { return gHead; }
93ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkViewRegister* next() const { return fChain; }
9548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    const SkViewFactory*   factory() const { return fFact; }
96ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
9848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com    SkViewFactory*  fFact;
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkViewRegister* fChain;
100ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkViewRegister* gHead;
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1042eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org///////////////////////////////////////////////////////////////////////////////
1052eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
1062eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgclass SampleView : public SkView {
1072eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgpublic:
1084d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com    SampleView()
1094d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        : fPipeState(SkOSMenu::kOffState)
1104d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        , fBGColor(SK_ColorWHITE)
1114d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com        , fRepeatCount(1)
112868074b50b0fc3e460d2aa97c1096827fe0a1935reed    {}
1132eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
114f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com    void setBGColor(SkColor color) { fBGColor = color; }
115f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com
116a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com    static bool IsSampleView(SkView*);
117f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com    static bool SetRepeatDraw(SkView*, int count);
118b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    static bool SetUsePipe(SkView*, SkOSMenu::TriState);
119ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
120db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    /**
121db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com     *  Call this to request menu items from a SampleView.
122ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com     *  Subclassing notes: A subclass of SampleView can overwrite this method
123db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com     *  to add new items of various types to the menu and change its title.
124db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com     *  The events attached to any new menu items must be handled in its onEvent
125ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com     *  method. See SkOSMenu.h for helper functions.
126db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com     */
127db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    virtual void requestMenu(SkOSMenu* menu) {}
1282eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
129bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org    virtual void onTileSizeChanged(const SkSize& tileSize) {}
130bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org
1312eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprotected:
1322eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    virtual void onDrawBackground(SkCanvas*);
1332eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    virtual void onDrawContent(SkCanvas*) = 0;
134ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1352eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    // overrides
1362eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    virtual bool onEvent(const SkEvent& evt);
1372eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    virtual bool onQuery(SkEvent* evt);
138db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    virtual void draw(SkCanvas*);
1392eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    virtual void onDraw(SkCanvas*);
1402eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
141b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com    SkOSMenu::TriState fPipeState;
142db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com    SkColor fBGColor;
143ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1442eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprivate:
1452eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    int fRepeatCount;
1460faac1e8579088a39f38d02ff675f14d7deb608dreed@google.com
1472eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org    typedef SkView INHERITED;
1482eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org};
1492eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
151