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