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 24c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverth#define MAX_ZOOM_LEVEL 8 25c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverth#define MIN_ZOOM_LEVEL -8 26c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverth 27c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gCharEvtName[] = "SampleCode_Char_Event"; 28c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gKeyEvtName[] = "SampleCode_Key_Event"; 29c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gTitleEvtName[] = "SampleCode_Title_Event"; 30c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gPrefSizeEvtName[] = "SampleCode_PrefSize_Event"; 31c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gFastTextEvtName[] = "SampleCode_FastText_Event"; 32c7027ab03f2e8bab4c74bc1b047642622d3d682bjvanverthstatic const char gUpdateWindowTitleEvtName[] = "SampleCode_UpdateWindowTitle"; 33a7a8b10134f78d4ed55ff0370a54d8141daa7cacreed 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SampleCode { 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 36f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool KeyQ(const SkEvent&, SkKey* outKey); 37f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool CharQ(const SkEvent&, SkUnichar* outUni); 38f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static bool TitleQ(const SkEvent&); 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static void TitleR(SkEvent*, const char title[]); 41db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com static bool RequestTitle(SkView* view, SkString* title); 42ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static bool PrefSizeQ(const SkEvent&); 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height); 45f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 46f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com static bool FastTextQ(const SkEvent&); 47f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com 48d9adfe6a223955bc69c8c7661ab8e0a078afbc32reed friend class SampleWindow; 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 5348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// interface that constructs SkViews 5448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewFactory : public SkRefCnt { 55ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.compublic: 5648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com virtual SkView* operator() () const = 0; 5748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 5848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 5948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef SkView* (*SkViewCreateFunc)(); 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 6148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// wraps SkViewCreateFunc in SkViewFactory interface 6248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkFuncViewFactory : public SkViewFactory { 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 6448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkFuncViewFactory(SkViewCreateFunc func); 6536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein SkView* operator() () const override; 66ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 6748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate: 6848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkViewCreateFunc fCreateFunc; 6948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 7048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 7148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comnamespace skiagm { 7248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass GM; 7348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com} 7448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 7548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// factory function that creates a skiagm::GM 7648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comtypedef skiagm::GM* (*GMFactoryFunc)(void*); 7748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 78ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com// Takes a GM factory function and implements the SkViewFactory interface 7948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// by making the GM and wrapping it in a GMSampleView. GMSampleView bridges 8048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com// the SampleView interface to skiagm::GM. 8148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkGMSampleViewFactory : public SkViewFactory { 8248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic: 8348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkGMSampleViewFactory(GMFactoryFunc func); 8436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein SkView* operator() () const override; 8548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comprivate: 8648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com GMFactoryFunc fFunc; 8748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com}; 8848dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 8948dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.comclass SkViewRegister : public SkRefCnt { 9048dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.compublic: 9148dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(SkViewFactory*); 9248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(SkViewCreateFunc); 9348dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com explicit SkViewRegister(GMFactoryFunc); 9448dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com 9548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com ~SkViewRegister() { 9648dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com fFact->unref(); 9748dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com } 98ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static const SkViewRegister* Head() { return gHead; } 100ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkViewRegister* next() const { return fChain; } 10248dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com const SkViewFactory* factory() const { return fFact; } 103ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 10548dd1a26ec07c5baa04856202e4e7e2a53e4d7e5bsalomon@google.com SkViewFactory* fFact; 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkViewRegister* fChain; 107ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com static SkViewRegister* gHead; 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1112eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org/////////////////////////////////////////////////////////////////////////////// 1122eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 1132eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgclass SampleView : public SkView { 1142eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgpublic: 1154d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com SampleView() 1164d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com : fPipeState(SkOSMenu::kOffState) 1174d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com , fBGColor(SK_ColorWHITE) 1184d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com , fRepeatCount(1) 11963c684a8a609d39da11b4a656223cebf52ca85dccaryclark , fHaveCalledOnceBeforeDraw(false) 120868074b50b0fc3e460d2aa97c1096827fe0a1935reed {} 1212eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 122f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com void setBGColor(SkColor color) { fBGColor = color; } 12376113a9b7716748c70ea0ecf7aacbabe4cce5009reed bool animate(const SkAnimTimer& timer) { return this->onAnimate(timer); } 124f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com 125a6ff4dc22d5006dc588769aa20c8b0bb4fde6d43reed@google.com static bool IsSampleView(SkView*); 126f21833999d90ad9c8c584cff3238797b39cfc00breed@google.com static bool SetRepeatDraw(SkView*, int count); 127b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com static bool SetUsePipe(SkView*, SkOSMenu::TriState); 128ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 129db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com /** 130db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * Call this to request menu items from a SampleView. 131ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com * Subclassing notes: A subclass of SampleView can overwrite this method 132db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * to add new items of various types to the menu and change its title. 133db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com * The events attached to any new menu items must be handled in its onEvent 134ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com * method. See SkOSMenu.h for helper functions. 135db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com */ 136db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com virtual void requestMenu(SkOSMenu* menu) {} 1372eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 138bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org virtual void onTileSizeChanged(const SkSize& tileSize) {} 139bbe43a9ce0513d37cbd5dca583c391b7b1e39b3acommit-bot@chromium.org 1402eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprotected: 1412eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDrawBackground(SkCanvas*); 1422eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDrawContent(SkCanvas*) = 0; 14376113a9b7716748c70ea0ecf7aacbabe4cce5009reed virtual bool onAnimate(const SkAnimTimer&) { return false; } 14463c684a8a609d39da11b4a656223cebf52ca85dccaryclark virtual void onOnceBeforeDraw() {} 145ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1462eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org // overrides 1472eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual bool onEvent(const SkEvent& evt); 1482eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual bool onQuery(SkEvent* evt); 1492eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org virtual void onDraw(SkCanvas*); 1502eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 151b073d920c90bc95f6c216327acd321a4fc809bfascroggo@google.com SkOSMenu::TriState fPipeState; 152db03eaa37111a63835ab7f61cb550971e609a621yangsu@google.com SkColor fBGColor; 153ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1542eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.orgprivate: 1552eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org int fRepeatCount; 15663c684a8a609d39da11b4a656223cebf52ca85dccaryclark bool fHaveCalledOnceBeforeDraw; 1572eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org typedef SkView INHERITED; 1582eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org}; 1592eb595292b9624371d1698ae9da7accb4c09c26cmike@reedtribe.org 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 161