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 #define MAX_ZOOM_LEVEL  8
     25 #define MIN_ZOOM_LEVEL  -8
     26 
     27 static const char gCharEvtName[] = "SampleCode_Char_Event";
     28 static const char gKeyEvtName[] = "SampleCode_Key_Event";
     29 static const char gTitleEvtName[] = "SampleCode_Title_Event";
     30 static const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event";
     31 static const char gFastTextEvtName[] = "SampleCode_FastText_Event";
     32 static const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle";
     33 
     34 class SampleCode {
     35 public:
     36     static bool KeyQ(const SkEvent&, SkKey* outKey);
     37     static bool CharQ(const SkEvent&, SkUnichar* outUni);
     38 
     39     static bool TitleQ(const SkEvent&);
     40     static void TitleR(SkEvent*, const char title[]);
     41     static bool RequestTitle(SkView* view, SkString* title);
     42 
     43     static bool PrefSizeQ(const SkEvent&);
     44     static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
     45 
     46     static bool FastTextQ(const SkEvent&);
     47 
     48     friend class SampleWindow;
     49 };
     50 
     51 //////////////////////////////////////////////////////////////////////////////
     52 
     53 // interface that constructs SkViews
     54 class SkViewFactory : public SkRefCnt {
     55 public:
     56     virtual SkView* operator() () const = 0;
     57 };
     58 
     59 typedef SkView* (*SkViewCreateFunc)();
     60 
     61 // wraps SkViewCreateFunc in SkViewFactory interface
     62 class SkFuncViewFactory : public SkViewFactory {
     63 public:
     64     SkFuncViewFactory(SkViewCreateFunc func);
     65     SkView* operator() () const override;
     66 
     67 private:
     68     SkViewCreateFunc fCreateFunc;
     69 };
     70 
     71 namespace skiagm {
     72 class GM;
     73 }
     74 
     75 // factory function that creates a skiagm::GM
     76 typedef skiagm::GM* (*GMFactoryFunc)(void*);
     77 
     78 // Takes a GM factory function and implements the SkViewFactory interface
     79 // by making the GM and wrapping it in a GMSampleView. GMSampleView bridges
     80 // the SampleView interface to skiagm::GM.
     81 class SkGMSampleViewFactory : public SkViewFactory {
     82 public:
     83     SkGMSampleViewFactory(GMFactoryFunc func);
     84     SkView* operator() () const override;
     85 private:
     86     GMFactoryFunc fFunc;
     87 };
     88 
     89 class SkViewRegister : public SkRefCnt {
     90 public:
     91     explicit SkViewRegister(SkViewFactory*);
     92     explicit SkViewRegister(SkViewCreateFunc);
     93     explicit SkViewRegister(GMFactoryFunc);
     94 
     95     ~SkViewRegister() {
     96         fFact->unref();
     97     }
     98 
     99     static const SkViewRegister* Head() { return gHead; }
    100 
    101     SkViewRegister* next() const { return fChain; }
    102     const SkViewFactory*   factory() const { return fFact; }
    103 
    104 private:
    105     SkViewFactory*  fFact;
    106     SkViewRegister* fChain;
    107 
    108     static SkViewRegister* gHead;
    109 };
    110 
    111 ///////////////////////////////////////////////////////////////////////////////
    112 
    113 class SampleView : public SkView {
    114 public:
    115     SampleView()
    116         : fPipeState(SkOSMenu::kOffState)
    117         , fBGColor(SK_ColorWHITE)
    118         , fRepeatCount(1)
    119         , fHaveCalledOnceBeforeDraw(false)
    120     {}
    121 
    122     void setBGColor(SkColor color) { fBGColor = color; }
    123     bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); }
    124 
    125     static bool IsSampleView(SkView*);
    126     static bool SetRepeatDraw(SkView*, int count);
    127     static bool SetUsePipe(SkView*, SkOSMenu::TriState);
    128 
    129     /**
    130      *  Call this to request menu items from a SampleView.
    131      *  Subclassing notes: A subclass of SampleView can overwrite this method
    132      *  to add new items of various types to the menu and change its title.
    133      *  The events attached to any new menu items must be handled in its onEvent
    134      *  method. See SkOSMenu.h for helper functions.
    135      */
    136     virtual void requestMenu(SkOSMenu* menu) {}
    137 
    138     virtual void onTileSizeChanged(const SkSize& tileSize) {}
    139 
    140 protected:
    141     virtual void onDrawBackground(SkCanvas*);
    142     virtual void onDrawContent(SkCanvas*) = 0;
    143     virtual bool onAnimate(const SkAnimTimer&) { return false; }
    144     virtual void onOnceBeforeDraw() {}
    145 
    146     // overrides
    147     virtual bool onEvent(const SkEvent& evt);
    148     virtual bool onQuery(SkEvent* evt);
    149     virtual void onDraw(SkCanvas*);
    150 
    151     SkOSMenu::TriState fPipeState;
    152     SkColor fBGColor;
    153 
    154 private:
    155     int fRepeatCount;
    156     bool fHaveCalledOnceBeforeDraw;
    157     typedef SkView INHERITED;
    158 };
    159 
    160 #endif
    161