blob: c22533aa281b51f954699e9e9d7e80d852e9d51c [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[]);
25
26 static bool PrefSizeQ(const SkEvent&);
27 static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
reed@android.comf2b98d62010-12-20 18:26:13 +000028
29 static bool FastTextQ(const SkEvent&);
30
reed@android.com44177402009-11-23 21:07:51 +000031 static SkMSec GetAnimTime();
reed@android.comf2b98d62010-12-20 18:26:13 +000032 static SkMSec GetAnimTimeDelta();
33 static SkScalar GetAnimSecondsDelta();
reed@android.com44177402009-11-23 21:07:51 +000034 static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
reed@google.com3cec4d72011-07-06 13:59:47 +000035
36 static GrContext* GetGr();
reed@android.com8a1c16f2008-12-17 15:59:43 +000037};
38
39//////////////////////////////////////////////////////////////////////////////
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041typedef SkView* (*SkViewFactory)();
42
43class SkViewRegister : SkNoncopyable {
44public:
deanm@chromium.org1220e1c2009-06-11 12:26:47 +000045 explicit SkViewRegister(SkViewFactory);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046
47 static const SkViewRegister* Head() { return gHead; }
48
49 SkViewRegister* next() const { return fChain; }
50 SkViewFactory factory() const { return fFact; }
51
52private:
53 SkViewFactory fFact;
54 SkViewRegister* fChain;
55
56 static SkViewRegister* gHead;
57};
58
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000059///////////////////////////////////////////////////////////////////////////////
60
61class SampleView : public SkView {
62public:
reed@google.com0faac1e2011-05-11 05:58:58 +000063 SampleView() : fRepeatCount(1), fBGColor(SK_ColorWHITE) {
64 fUsePipe = false;
65 }
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000066
reed@google.comf2183392011-04-22 14:10:48 +000067 void setBGColor(SkColor color) { fBGColor = color; }
68
reed@google.coma6ff4dc2011-05-12 22:08:24 +000069 static bool IsSampleView(SkView*);
reed@google.comf2183392011-04-22 14:10:48 +000070 static bool SetRepeatDraw(SkView*, int count);
reed@google.com0faac1e2011-05-11 05:58:58 +000071 static bool SetUsePipe(SkView*, bool);
yangsu@google.com921091f2011-08-02 13:39:12 +000072
73 //call this to request menu items from a SampleView. A SampleView can
74 //overwrite this method to add new items of various types to the menu and
75 //change its title. The events attached to any new menu items must be
76 //handled in the onEvent method. See SkOSMenu.h for helper functions.
77 virtual void requestMenus(SkOSMenu* menu) {}
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000078
79protected:
80 virtual void onDrawBackground(SkCanvas*);
81 virtual void onDrawContent(SkCanvas*) = 0;
yangsu@google.com921091f2011-08-02 13:39:12 +000082
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000083 // overrides
84 virtual bool onEvent(const SkEvent& evt);
85 virtual bool onQuery(SkEvent* evt);
86 virtual void onDraw(SkCanvas*);
87
88private:
89 int fRepeatCount;
reed@google.comf2183392011-04-22 14:10:48 +000090 SkColor fBGColor;
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000091
reed@google.com0faac1e2011-05-11 05:58:58 +000092 bool fUsePipe;
93
mike@reedtribe.org2eb59522011-04-22 01:59:09 +000094 typedef SkView INHERITED;
95};
96
reed@android.com8a1c16f2008-12-17 15:59:43 +000097#endif
98