Home | History | Annotate | Download | only in samplecode
      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 
     17 class GrContext;
     18 class 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 
     25 class SampleCode {
     26 public:
     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
     45 class SkViewFactory : public SkRefCnt {
     46 public:
     47     virtual SkView* operator() () const = 0;
     48 };
     49 
     50 typedef SkView* (*SkViewCreateFunc)();
     51 
     52 // wraps SkViewCreateFunc in SkViewFactory interface
     53 class SkFuncViewFactory : public SkViewFactory {
     54 public:
     55     SkFuncViewFactory(SkViewCreateFunc func);
     56     SkView* operator() () const override;
     57 
     58 private:
     59     SkViewCreateFunc fCreateFunc;
     60 };
     61 
     62 namespace skiagm {
     63 class GM;
     64 }
     65 
     66 // factory function that creates a skiagm::GM
     67 typedef 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.
     72 class SkGMSampleViewFactory : public SkViewFactory {
     73 public:
     74     SkGMSampleViewFactory(GMFactoryFunc func);
     75     SkView* operator() () const override;
     76 private:
     77     GMFactoryFunc fFunc;
     78 };
     79 
     80 class SkViewRegister : public SkRefCnt {
     81 public:
     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 
     95 private:
     96     SkViewFactory*  fFact;
     97     SkViewRegister* fChain;
     98 
     99     static SkViewRegister* gHead;
    100 };
    101 
    102 ///////////////////////////////////////////////////////////////////////////////
    103 
    104 class SampleView : public SkView {
    105 public:
    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 
    131 protected:
    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 draw(SkCanvas*);
    141     virtual void onDraw(SkCanvas*);
    142 
    143     SkOSMenu::TriState fPipeState;
    144     SkColor fBGColor;
    145 
    146 private:
    147     int fRepeatCount;
    148     bool fHaveCalledOnceBeforeDraw;
    149     typedef SkView INHERITED;
    150 };
    151 
    152 #endif
    153