blob: b3f3c9334d988ed8cea237aa050b1187d6831f6c [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
32enum SkTriState {
yangsu@google.com921091f2011-08-02 13:39:12 +000033 kFalse_SkTriState = SkOSMenu::kOffState,
34 kTrue_SkTriState = SkOSMenu::kOnState,
35 kUnknown_SkTriState = SkOSMenu::kMixedState,
Scroggo2c8208f2011-06-15 16:49:08 +000036};
37
38class SampleWindow : public SkOSWindow {
39 SkTDArray<SkViewFactory> fSamples;
40public:
bsalomon@google.com098e96d2011-07-14 14:30:46 +000041 enum DeviceType {
42 kRaster_DeviceType,
43 kPicture_DeviceType,
44 kGPU_DeviceType
45 };
46 /**
47 * SampleApp ports can subclass this manager class if they want to:
48 * * filter the types of devices supported
49 * * customize plugging of SkDevice objects into an SkCanvas
50 * * customize publishing the results of draw to the OS window
51 * * manage GrContext / GrRenderTarget lifetimes
52 */
53 class DeviceManager : public SkRefCnt {
54 public:
55 // called at end of SampleWindow cons
56 virtual void init(SampleWindow* win) = 0;
57
58 // called when selecting a new device type
59 // can disallow a device type by returning false.
60 virtual bool supportsDeviceType(DeviceType dType) = 0;
61
62 // called before drawing. should install correct device
63 // type on the canvas. Will skip drawing if returns false.
64 virtual bool prepareCanvas(DeviceType dType,
65 SkCanvas* canvas,
66 SampleWindow* win) = 0;
67
68 // called after drawing, should get the results onto the
69 // screen.
70 virtual void publishCanvas(DeviceType dType,
71 SkCanvas* canvas,
72 SampleWindow* win) = 0;
73
74 // called when window changes size, guaranteed to be called
75 // at least once before first draw (after init)
76 virtual void windowSizeChanged(SampleWindow* win) = 0;
77
78 // return the GrContext backing gpu devices
79 virtual GrContext* getGrContext() = 0;
80 };
81
82 SampleWindow(void* hwnd, int argc, char** argv, DeviceManager*);
Scroggo2c8208f2011-06-15 16:49:08 +000083 virtual ~SampleWindow();
84
85 virtual void draw(SkCanvas* canvas);
86
yangsu@google.com921091f2011-08-02 13:39:12 +000087 void setDeviceType(DeviceType type);
Scroggo2c8208f2011-06-15 16:49:08 +000088 void toggleRendering();
89 void toggleSlideshow();
90 void toggleFPS();
yangsu@google.com921091f2011-08-02 13:39:12 +000091 void togglePipe();
bsalomon@google.com098e96d2011-07-14 14:30:46 +000092
93 GrContext* getGrContext() const { return fDevManager->getGrContext(); }
94
Scroggo2c8208f2011-06-15 16:49:08 +000095 void setZoomCenter(float x, float y);
96 void changeZoomLevel(float delta);
97 bool nextSample();
98 bool previousSample();
yangsu@google.com501775e2011-06-24 16:04:50 +000099 bool goToSample(int i);
100 SkString getSampleTitle(int i);
101 int sampleCount();
Scroggoa54e2f62011-06-17 12:46:17 +0000102 bool handleTouch(int ownerId, float x, float y,
103 SkView::Click::State state);
Scroggo8ac0d542011-06-21 14:44:57 +0000104 void saveToPdf();
yangsu@google.com501775e2011-06-24 16:04:50 +0000105 SkData* getPDFData() { return fPDFData; }
Scroggo62b65b02011-06-21 16:01:26 +0000106 void postInvalDelay();
Scroggo2c8208f2011-06-15 16:49:08 +0000107
108protected:
109 virtual void onDraw(SkCanvas* canvas);
110 virtual bool onHandleKey(SkKey key);
111 virtual bool onHandleChar(SkUnichar);
112 virtual void onSizeChange();
113
114 virtual SkCanvas* beforeChildren(SkCanvas*);
115 virtual void afterChildren(SkCanvas*);
116 virtual void beforeChild(SkView* child, SkCanvas* canvas);
117 virtual void afterChild(SkView* child, SkCanvas* canvas);
118
119 virtual bool onEvent(const SkEvent& evt);
120 virtual bool onQuery(SkEvent* evt);
121
Scroggod3aed392011-06-22 13:26:56 +0000122 virtual bool onDispatchClick(int x, int y, Click::State, void* owner);
Scroggo2c8208f2011-06-15 16:49:08 +0000123 virtual bool onClick(Click* click);
124 virtual Click* onFindClickHandler(SkScalar x, SkScalar y);
125
126private:
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000127 class DefaultDeviceManager;
128
Scroggo2c8208f2011-06-15 16:49:08 +0000129 int fCurrIndex;
130
131 SkPicture* fPicture;
Scroggo2c8208f2011-06-15 16:49:08 +0000132 SkPath fClipPath;
133
134 SkTouchGesture fGesture;
135 SkScalar fZoomLevel;
136 SkScalar fZoomScale;
137
bsalomon@google.com098e96d2011-07-14 14:30:46 +0000138 DeviceType fDeviceType;
139 DeviceManager* fDevManager;
Scroggo2c8208f2011-06-15 16:49:08 +0000140
Scroggo8ac0d542011-06-21 14:44:57 +0000141 bool fSaveToPdf;
142 SkCanvas* fPdfCanvas;
yangsu@google.com501775e2011-06-24 16:04:50 +0000143 SkData* fPDFData;
Scroggo8ac0d542011-06-21 14:44:57 +0000144
Scroggo2c8208f2011-06-15 16:49:08 +0000145 bool fUseClip;
146 bool fNClip;
147 bool fRepeatDrawing;
148 bool fAnimating;
149 bool fRotate;
150 bool fScale;
151 bool fRequestGrabImage;
152 bool fUsePipe;
153 bool fMeasureFPS;
154 SkMSec fMeasureFPS_Time;
155
156 // The following are for the 'fatbits' drawing
157 // Latest position of the mouse.
158 int fMouseX, fMouseY;
159 int fFatBitsScale;
160 // Used by the text showing position and color values.
161 SkTypeface* fTypeface;
162 bool fShowZoomer;
163
164 SkTriState fLCDState;
165 SkTriState fAAState;
166 SkTriState fFilterState;
167 SkTriState fHintingState;
168 unsigned fFlipAxis;
169
170 int fScrollTestX, fScrollTestY;
171 SkScalar fZoomCenterX, fZoomCenterY;
172
yangsu@google.com921091f2011-08-02 13:39:12 +0000173 //Stores global settings
174 SkOSMenu fAppMenu;
175 //Stores slide specific settings
176 SkOSMenu fSlideMenu;
177
Scroggo2c8208f2011-06-15 16:49:08 +0000178 void loadView(SkView*);
179 void updateTitle();
180
181 void toggleZoomer();
182 bool zoomIn();
183 bool zoomOut();
184 void updatePointer(int x, int y);
185 void showZoomer(SkCanvas* canvas);
186
187 void postAnimatingEvent();
188
Scroggo2c8208f2011-06-15 16:49:08 +0000189 typedef SkOSWindow INHERITED;
190};
191
192#endif