blob: 871d47eedd7533e1357a24f9374741c08b78ba62 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
Scroggo2c8208f2011-06-15 16:49:08 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Skia
Scroggo2c8208f2011-06-15 16:49:08 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
Scroggo2c8208f2011-06-15 16:49:08 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
Scroggo2c8208f2011-06-15 16:49:08 +000010#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"
yangsu@google.com921091f2011-08-02 13:39:12 +000021#include "SkOSMenu.h"
Scroggo2c8208f2011-06-15 16:49:08 +000022
23class GrContext;
reed@google.com29038ed2011-07-06 17:56:47 +000024class GrRenderTarget;
Scroggo2c8208f2011-06-15 16:49:08 +000025
26class SkEvent;
27class SkCanvas;
Scroggo2c8208f2011-06-15 16:49:08 +000028class SkPicture;
29class SkTypeface;
yangsu@google.com501775e2011-06-24 16:04:50 +000030class SkData;
Scroggo2c8208f2011-06-15 16:49:08 +000031
Scroggo2c8208f2011-06-15 16:49:08 +000032class SampleWindow : public SkOSWindow {
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000033 SkTDArray<const SkViewFactory*> fSamples;
Scroggo2c8208f2011-06-15 16:49:08 +000034public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000035 enum DeviceType {
36 kRaster_DeviceType,
37 kPicture_DeviceType,
bsalomon@google.com74913722011-10-27 20:44:19 +000038 kGPU_DeviceType,
39 kNullGPU_DeviceType
bsalomon@google.com098e96d2011-07-14 14:30:46 +000040 };
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
bsalomon@google.com74913722011-10-27 20:44:19 +000074 virtual GrContext* getGrContext(DeviceType dType) = 0;
bsalomon@google.com098e96d2011-07-14 14:30:46 +000075 };
76
77 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000078 virtual ~SampleWindow();
79
80 virtual void draw(SkCanvas* canvas);
81
yangsu@google.com921091f2011-08-02 13:39:12 +000082 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000083 void toggleRendering();
84 void toggleSlideshow();
85 void toggleFPS();
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000086 void showOverview();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000087
bsalomon@google.com74913722011-10-27 20:44:19 +000088 GrContext* getGrContext() const { return fDevManager->getGrContext(fDeviceType); }
bsalomon@google.com098e96d2011-07-14 14:30:46 +000089
Scroggo2c8208f2011-06-15 16:49:08 +000090 void setZoomCenter(float x, float y);
91 void changeZoomLevel(float delta);
92 bool nextSample();
93 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +000094 bool goToSample(int i);
95 SkString getSampleTitle(int i);
96 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +000097 bool handleTouch(int ownerId, float x, float y,
98 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +000099 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000100 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000101 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000102
103protected:
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
Scroggod3aed392011-06-22 13:26:56 +0000117 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000118 virtual bool onClick(Click* click);
119 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
120
121private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000122 class DefaultDeviceManager;
123
Scroggo2c8208f2011-06-15 16:49:08 +0000124 int fCurrIndex;
125
126 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000127 SkPath fClipPath;
128
129 SkTouchGesture fGesture;
130 SkScalar fZoomLevel;
131 SkScalar fZoomScale;
132
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000133 DeviceType fDeviceType;
134 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000135
Scroggo8ac0d542011-06-21 14:44:57 +0000136 bool fSaveToPdf;
137 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000138 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000139
Scroggo2c8208f2011-06-15 16:49:08 +0000140 bool fUseClip;
141 bool fNClip;
Scroggo2c8208f2011-06-15 16:49:08 +0000142 bool fAnimating;
143 bool fRotate;
bsalomon@google.come8f09102011-09-08 18:48:12 +0000144 bool fPerspAnim;
145 SkScalar fPerspAnimTime;
Scroggo2c8208f2011-06-15 16:49:08 +0000146 bool fScale;
147 bool fRequestGrabImage;
Scroggo2c8208f2011-06-15 16:49:08 +0000148 bool fMeasureFPS;
149 SkMSec fMeasureFPS_Time;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000150 bool fMagnify;
151
yangsu@google.comef7bdfa2011-08-12 14:27:47 +0000152
153 bool fUsePipe;
154 int fUsePipeMenuItemID;
155 bool fDebugger;
156
Scroggo2c8208f2011-06-15 16:49:08 +0000157 // 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;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000164
165 SkOSMenu::TriState fLCDState;
166 SkOSMenu::TriState fAAState;
167 SkOSMenu::TriState fFilterState;
168 SkOSMenu::TriState fHintingState;
Scroggo2c8208f2011-06-15 16:49:08 +0000169 unsigned fFlipAxis;
170
171 int fScrollTestX, fScrollTestY;
172 SkScalar fZoomCenterX, fZoomCenterY;
173
yangsu@google.com921091f2011-08-02 13:39:12 +0000174 //Stores global settings
175 SkOSMenu fAppMenu;
176 //Stores slide specific settings
177 SkOSMenu fSlideMenu;
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000178 int fTransitionNext;
179 int fTransitionPrev;
yangsu@google.com921091f2011-08-02 13:39:12 +0000180
Scroggo2c8208f2011-06-15 16:49:08 +0000181 void loadView(SkView*);
182 void updateTitle();
183
Scroggo2c8208f2011-06-15 16:49:08 +0000184 bool zoomIn();
185 bool zoomOut();
186 void updatePointer(int x, int y);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +0000187 void magnify(SkCanvas* canvas);
Scroggo2c8208f2011-06-15 16:49:08 +0000188 void showZoomer(SkCanvas* canvas);
reed@google.comf03bb562011-11-11 21:42:12 +0000189 void updateMatrix();
Scroggo2c8208f2011-06-15 16:49:08 +0000190 void postAnimatingEvent();
reed@google.come23f1942011-12-08 19:36:00 +0000191 void installDrawFilter(SkCanvas*);
mike@reedtribe.org6f6e8c32011-12-27 22:33:50 +0000192 int findByTitle(const char*);
Scroggo2c8208f2011-06-15 16:49:08 +0000193
Scroggo2c8208f2011-06-15 16:49:08 +0000194 typedef SkOSWindow INHERITED;
195};
196
197#endif