xref: /aosp_15_r20/external/skia/tools/viewer/ClickHandlerSlide.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef ClickHandlerSlide_DEFINED
9 #define ClickHandlerSlide_DEFINED
10 
11 #include "tools/SkMetaData.h"
12 #include "tools/viewer/Slide.h"
13 
14 #include "include/core/SkPoint.h"
15 
16 #include <functional>
17 
18 /**
19  * Provides a higher level abstraction for click handling than the Slide base class. A Click object
20  * is is used to track the state of the mouse over time.
21  */
22 class ClickHandlerSlide : public Slide {
23 public:
24     // Click handling
25     class Click {
26     public:
Click()27         Click() {}
Click(std::function<bool (Click *)> f)28         Click(std::function<bool(Click*)> f) : fFunc(std::move(f)), fHasFunc(true) {}
29         virtual ~Click() = default;
30 
31         SkPoint fOrig = {0, 0};
32         SkPoint fPrev = {0, 0};
33         SkPoint fCurr = {0, 0};
34 
35         skui::InputState  fState        = skui::InputState::kDown;
36         skui::ModifierKey fModifierKeys = skui::ModifierKey::kNone;
37 
38         SkMetaData fMeta;
39 
40         std::function<bool(Click*)> fFunc;
41 
42         bool fHasFunc = false;
43     };
44 
45     bool onMouse(SkScalar x, SkScalar y,
46                  skui::InputState clickState,
47                  skui::ModifierKey modifierKeys) final;
48 
49 protected:
50     /**
51      * Return a Click object to handle the click. onClick will be called repeatedly with the latest
52      * mouse state tracked on the Click object until it returns false.
53      */
54     virtual Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) = 0;
55 
56     /** Override to track clicks. Return true as long as you want to track the pen/mouse. */
57     virtual bool onClick(Click*) = 0;
58 
59 private:
60     std::unique_ptr<Click> fClick;
61 };
62 
63 #endif
64