Home | History | Annotate | Download | only in samplecode
      1 
      2 /*
      3  * Copyright 2011 Skia
      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 SampleWindow_DEFINED
     11 #define SampleWindow_DEFINED
     12 
     13 #include "SkWindow.h"
     14 
     15 #include "SampleCode.h"
     16 #include "SkPath.h"
     17 #include "SkScalar.h"
     18 #include "SkTDArray.h"
     19 #include "SkTouchGesture.h"
     20 #include "SkWindow.h"
     21 #include "SkOSMenu.h"
     22 
     23 class GrContext;
     24 class GrRenderTarget;
     25 
     26 class SkEvent;
     27 class SkCanvas;
     28 class SkPicture;
     29 class SkTypeface;
     30 class SkData;
     31 
     32 class SampleWindow : public SkOSWindow {
     33     SkTDArray<const SkViewFactory*> fSamples;
     34 public:
     35     enum DeviceType {
     36         kRaster_DeviceType,
     37         kPicture_DeviceType,
     38         kGPU_DeviceType,
     39         kNullGPU_DeviceType
     40     };
     41     /**
     42      * SampleApp ports can subclass this manager class if they want to:
     43      *      * filter the types of devices supported
     44      *      * customize plugging of SkDevice objects into an SkCanvas
     45      *      * customize publishing the results of draw to the OS window
     46      *      * manage GrContext / GrRenderTarget lifetimes
     47      */
     48     class DeviceManager : public SkRefCnt {
     49     public:
     50         // called at end of SampleWindow cons
     51         virtual void init(SampleWindow* win) = 0;
     52 
     53         // called when selecting a new device type
     54         // can disallow a device type by returning false.
     55         virtual bool supportsDeviceType(DeviceType dType) = 0;
     56 
     57         // called before drawing. should install correct device
     58         // type on the canvas. Will skip drawing if returns false.
     59         virtual bool prepareCanvas(DeviceType dType,
     60                                    SkCanvas* canvas,
     61                                    SampleWindow* win) = 0;
     62 
     63         // called after drawing, should get the results onto the
     64         // screen.
     65         virtual void publishCanvas(DeviceType dType,
     66                                    SkCanvas* canvas,
     67                                    SampleWindow* win) = 0;
     68 
     69         // called when window changes size, guaranteed to be called
     70         // at least once before first draw (after init)
     71         virtual void windowSizeChanged(SampleWindow* win) = 0;
     72 
     73         // return the GrContext backing gpu devices
     74         virtual GrContext* getGrContext(DeviceType dType) = 0;
     75     };
     76 
     77     SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
     78     virtual ~SampleWindow();
     79 
     80     virtual void draw(SkCanvas* canvas);
     81 
     82     void setDeviceType(DeviceType type);
     83     void toggleRendering();
     84     void toggleSlideshow();
     85     void toggleFPS();
     86     void showOverview();
     87 
     88     GrContext* getGrContext() const { return fDevManager->getGrContext(fDeviceType); }
     89 
     90     void setZoomCenter(float x, float y);
     91     void changeZoomLevel(float delta);
     92     bool nextSample();
     93     bool previousSample();
     94     bool goToSample(int i);
     95     SkString getSampleTitle(int i);
     96     int  sampleCount();
     97     bool handleTouch(int ownerId, float x, float y,
     98             SkView::Click::State state);
     99     void saveToPdf();
    100     SkData* getPDFData() { return fPDFData; }
    101     void postInvalDelay();
    102 
    103 protected:
    104     virtual void onDraw(SkCanvas* canvas);
    105     virtual bool onHandleKey(SkKey key);
    106     virtual bool onHandleChar(SkUnichar);
    107     virtual void onSizeChange();
    108 
    109     virtual SkCanvas* beforeChildren(SkCanvas*);
    110     virtual void afterChildren(SkCanvas*);
    111     virtual void beforeChild(SkView* child, SkCanvas* canvas);
    112     virtual void afterChild(SkView* child, SkCanvas* canvas);
    113 
    114     virtual bool onEvent(const SkEvent& evt);
    115     virtual bool onQuery(SkEvent* evt);
    116 
    117     virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
    118     virtual bool onClick(Click* click);
    119     virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
    120 
    121 private:
    122     class DefaultDeviceManager;
    123 
    124     int fCurrIndex;
    125 
    126     SkPicture* fPicture;
    127     SkPath fClipPath;
    128 
    129     SkTouchGesture fGesture;
    130     SkScalar fZoomLevel;
    131     SkScalar fZoomScale;
    132 
    133     DeviceType fDeviceType;
    134     DeviceManager* fDevManager;
    135 
    136     bool fSaveToPdf;
    137     SkCanvas* fPdfCanvas;
    138     SkData* fPDFData;
    139 
    140     bool fUseClip;
    141     bool fNClip;
    142     bool fAnimating;
    143     bool fRotate;
    144     bool fPerspAnim;
    145     SkScalar fPerspAnimTime;
    146     bool fScale;
    147     bool fRequestGrabImage;
    148     bool fMeasureFPS;
    149     SkMSec fMeasureFPS_Time;
    150     bool fMagnify;
    151 
    152 
    153     bool fUsePipe;
    154     int  fUsePipeMenuItemID;
    155     bool fDebugger;
    156 
    157     // The following are for the 'fatbits' drawing
    158     // Latest position of the mouse.
    159     int fMouseX, fMouseY;
    160     int fFatBitsScale;
    161     // Used by the text showing position and color values.
    162     SkTypeface* fTypeface;
    163     bool fShowZoomer;
    164 
    165     SkOSMenu::TriState fLCDState;
    166     SkOSMenu::TriState fAAState;
    167     SkOSMenu::TriState fFilterState;
    168     SkOSMenu::TriState fHintingState;
    169     unsigned   fFlipAxis;
    170 
    171     int fScrollTestX, fScrollTestY;
    172     SkScalar fZoomCenterX, fZoomCenterY;
    173 
    174     //Stores global settings
    175     SkOSMenu fAppMenu;
    176     //Stores slide specific settings
    177     SkOSMenu fSlideMenu;
    178     int fTransitionNext;
    179     int fTransitionPrev;
    180 
    181     void loadView(SkView*);
    182     void updateTitle();
    183 
    184     bool zoomIn();
    185     bool zoomOut();
    186     void updatePointer(int x, int y);
    187     void magnify(SkCanvas* canvas);
    188     void showZoomer(SkCanvas* canvas);
    189     void updateMatrix();
    190     void postAnimatingEvent();
    191     void installDrawFilter(SkCanvas*);
    192     int findByTitle(const char*);
    193 
    194     typedef SkOSWindow INHERITED;
    195 };
    196 
    197 #endif
    198