blob: 2db1955b644373a8156be39ae6d90bcb299330fa [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SampleCode_DEFINED
9#define SampleCode_DEFINED
10
reed@google.comf2183392011-04-22 14:10:48 +000011#include "SkColor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkEvent.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000013#include "SkKey.h"
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000014#include "SkView.h"
yangsu@google.com921091f2011-08-02 13:39:12 +000015class SkOSMenu;
reed@google.com3cec4d72011-07-06 13:59:47 +000016class GrContext;
17
reed@android.com8a1c16f2008-12-17 15:59:43 +000018class SampleCode {
19public:
reed@android.comf2b98d62010-12-20 18:26:13 +000020 static bool KeyQ(const SkEvent&, SkKey* outKey);
21 static bool CharQ(const SkEvent&, SkUnichar* outUni);
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 static bool TitleQ(const SkEvent&);
24 static void TitleR(SkEvent*, const char title[]);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000025 static bool RequestTitle(SkView* view, SkString* title);
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
27 static bool PrefSizeQ(const SkEvent&);
28 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
reed@android.comf2b98d62010-12-20 18:26:13 +000029
30 static bool FastTextQ(const SkEvent&);
31
reed@android.com44177402009-11-23 21:07:51 +000032 static SkMSec GetAnimTime();
reed@android.comf2b98d62010-12-20 18:26:13 +000033 static SkMSec GetAnimTimeDelta();
34 static SkScalar GetAnimSecondsDelta();
reed@android.com44177402009-11-23 21:07:51 +000035 static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
reed@google.com3cec4d72011-07-06 13:59:47 +000036
37 static GrContext* GetGr();
reed@android.com8a1c16f2008-12-17 15:59:43 +000038};
39
40//////////////////////////////////////////////////////////////////////////////
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042typedef SkView* (*SkViewFactory)();
43
44class SkViewRegister : SkNoncopyable {
45public:
deanm@chromium.org1220e1c2009-06-11 12:26:47 +000046 explicit SkViewRegister(SkViewFactory);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 static const SkViewRegister* Head() { return gHead; }
49
50 SkViewRegister* next() const { return fChain; }
51 SkViewFactory factory() const { return fFact; }
52
53private:
54 SkViewFactory fFact;
55 SkViewRegister* fChain;
56
57 static SkViewRegister* gHead;
58};
59
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000060///////////////////////////////////////////////////////////////////////////////
61
62class SampleView : public SkView {
63public:
reed@google.com0faac1e2011-05-11 05:58:58 +000064 SampleView() : fRepeatCount(1), fBGColor(SK_ColorWHITE) {
65 fUsePipe = false;
66 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000067
reed@google.comf2183392011-04-22 14:10:48 +000068 void setBGColor(SkColor color) { fBGColor = color; }
69
reed@google.coma6ff4dc2011-05-12 22:08:24 +000070 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +000071 static bool SetRepeatDraw(SkView*, int count);
reed@google.com0faac1e2011-05-11 05:58:58 +000072 static bool SetUsePipe(SkView*, bool);
yangsu@google.com921091f2011-08-02 13:39:12 +000073
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000074 /**
75 * Call this to request menu items from a SampleView.
76 * Subclassing notes: A subclass of SampleView can overwrite this method
77 * to add new items of various types to the menu and change its title.
78 * The events attached to any new menu items must be handled in its onEvent
79 * method. See SkOSMenu.h for helper functions.
80 */
81 virtual void requestMenu(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000082
83protected:
84 virtual void onDrawBackground(SkCanvas*);
85 virtual void onDrawContent(SkCanvas*) = 0;
yangsu@google.com921091f2011-08-02 13:39:12 +000086
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000087 // overrides
88 virtual bool onEvent(const SkEvent& evt);
89 virtual bool onQuery(SkEvent* evt);
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000090 virtual void draw(SkCanvas*);
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000091 virtual void onDraw(SkCanvas*);
92
yangsu@google.comdb03eaa2011-08-08 15:37:23 +000093 bool fUsePipe;
94 SkColor fBGColor;
95
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000096private:
97 int fRepeatCount;
reed@google.com0faac1e2011-05-11 05:58:58 +000098
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000099 typedef SkView INHERITED;
100};
101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102#endif
103