1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc. 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SampleCode_DEFINED 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SampleCode_DEFINED 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 11f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com#include "SkColor.h" 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkEvent.h" 13f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com#include "SkKey.h" 142eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org#include "SkView.h" 15b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com#include "SkOSMenu.h" 1676113a9b7716748c70ea0ecf7aacbabe4cce5009reed 173cec4d71e6f53fda99705427f9820d025cfbf64breed@google.comclass GrContext; 1876113a9b7716748c70ea0ecf7aacbabe4cce5009reedclass SkAnimTimer; 193cec4d71e6f53fda99705427f9820d025cfbf64breed@google.com 20a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed#define DEF_SAMPLE(code) \ 21a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed static SkView* SK_MACRO_APPEND_LINE(F_)() { code } \ 22a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed static SkViewRegister SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_)); 23a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed 24a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SampleCode { 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 27f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool KeyQ(const SkEvent&, SkKey* outKey); 28f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool CharQ(const SkEvent&, SkUnichar* outUni); 29f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static bool TitleQ(const SkEvent&); 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static void TitleR(SkEvent*, const char title[]); 32db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com static bool RequestTitle(SkView* view, SkString* title); 33ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static bool PrefSizeQ(const SkEvent&); 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height); 36f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 37f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool FastTextQ(const SkEvent&); 38f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 39d9adfe6a223955bc69c8c7661ab8e0a078afbc32reed friend class SampleWindow; 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// interface that constructs SkViews 4548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewFactory : public SkRefCnt { 46ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.compublic: 4748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com virtual SkView* operator() () const = 0; 4848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 4948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 5048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef SkView* (*SkViewCreateFunc)(); 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// wraps SkViewCreateFunc in SkViewFactory interface 5348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkFuncViewFactory : public SkViewFactory { 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 5548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkFuncViewFactory(SkViewCreateFunc func); 5636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein SkView* operator() () const override; 57ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 5848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate: 5948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkViewCreateFunc fCreateFunc; 6048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 6148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 6248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comnamespace skiagm { 6348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass GM; 6448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com} 6548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 6648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// factory function that creates a skiagm::GM 6748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef skiagm::GM* (*GMFactoryFunc)(void*); 6848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 69ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com// Takes a GM factory function and implements the SkViewFactory interface 7048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges 7148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// the SampleView interface to skiagm::GM. 7248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkGMSampleViewFactory : public SkViewFactory { 7348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic: 7448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkGMSampleViewFactory(GMFactoryFunc func); 7536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein SkView* operator() () const override; 7648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate: 7748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com GMFactoryFunc fFunc; 7848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 7948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 8048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewRegister : public SkRefCnt { 8148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic: 8248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(SkViewFactory*); 8348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(SkViewCreateFunc); 8448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(GMFactoryFunc); 8548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 8648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com ~SkViewRegister() { 8748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com fFact->unref(); 8848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com } 89ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static const SkViewRegister* Head() { return gHead; } 91ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkViewRegister* next() const { return fChain; } 9348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com const SkViewFactory* factory() const { return fFact; } 94ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 9648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkViewFactory* fFact; 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkViewRegister* fChain; 98ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static SkViewRegister* gHead; 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1022eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org/////////////////////////////////////////////////////////////////////////////// 1032eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 1042eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgclass SampleView : public SkView { 1052eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgpublic: 1064d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com SampleView() 1074d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com : fPipeState(SkOSMenu::kOffState) 1084d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com , fBGColor(SK_ColorWHITE) 1094d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com , fRepeatCount(1) 11063c684a8a609d39da11b4a656223cebf52ca85dccaryclark , fHaveCalledOnceBeforeDraw(false) 111868074b50b0fc3e460d2aa97c1096827fe0a1935reed {} 1122eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 113f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com void setBGColor(SkColor color) { fBGColor = color; } 11476113a9b7716748c70ea0ecf7aacbabe4cce5009reed bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); } 115f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com 116a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com static bool IsSampleView(SkView*); 117f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com static bool SetRepeatDraw(SkView*, int count); 118b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com static bool SetUsePipe(SkView*, SkOSMenu::TriState); 119ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 120db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com /** 121db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * Call this to request menu items from a SampleView. 122ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com * Subclassing notes: A subclass of SampleView can overwrite this method 123db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * to add new items of various types to the menu and change its title. 124db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * The events attached to any new menu items must be handled in its onEvent 125ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com * method. See SkOSMenu.h for helper functions. 126db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com */ 127db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com virtual void requestMenu(SkOSMenu* menu) {} 1282eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 129bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org virtual void onTileSizeChanged(const SkSize& tileSize) {} 130bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org 1312eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprotected: 1322eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDrawBackground(SkCanvas*); 1332eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDrawContent(SkCanvas*) = 0; 13476113a9b7716748c70ea0ecf7aacbabe4cce5009reed virtual bool onAnimate(const SkAnimTimer&) { return false; } 13563c684a8a609d39da11b4a656223cebf52ca85dccaryclark virtual void onOnceBeforeDraw() {} 136ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1372eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org // overrides 1382eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual bool onEvent(const SkEvent& evt); 1392eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual bool onQuery(SkEvent* evt); 140db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com virtual void draw(SkCanvas*); 1412eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDraw(SkCanvas*); 1422eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 143b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com SkOSMenu::TriState fPipeState; 144db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com SkColor fBGColor; 145ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1462eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprivate: 1472eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org int fRepeatCount; 14863c684a8a609d39da11b4a656223cebf52ca85dccaryclark bool fHaveCalledOnceBeforeDraw; 1492eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org typedef SkView INHERITED; 1502eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}; 1512eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 153