• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2019, The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef LIBTEEUI_BUTTON_H_
19 #define LIBTEEUI_BUTTON_H_
20 
21 #include "font_rendering.h"
22 #include "utils.h"
23 
24 // #define DRAW_DEBUG_MARKERS
25 
26 namespace teeui {
27 
28 class ButtonImpl {
29 
30   public:
31     struct ConvexObjectInfo {
32         const PxPoint* begin;
33         const PxPoint* end;
34     };
35 
ButtonImpl()36     ButtonImpl()
37         : radius_(0), color_(0), convexObjectColor_(0), roundTopLeft_(false), roundTopRight_(false),
38           roundBottomLeft_(false), roundBottomRight_(false) {}
ButtonImpl(pxs radius,Color color,Color convexObjectColor,bool roundTopLeft,bool roundTopRight,bool roundBottomLeft,bool roundBottomRight)39     ButtonImpl(pxs radius, Color color, Color convexObjectColor, bool roundTopLeft,
40                bool roundTopRight, bool roundBottomLeft, bool roundBottomRight)
41         : radius_(radius), color_(color), convexObjectColor_(convexObjectColor),
42           roundTopLeft_(roundTopLeft), roundTopRight_(roundTopRight),
43           roundBottomLeft_(roundBottomLeft), roundBottomRight_(roundBottomRight) {}
44 
setColor(Color color)45     void setColor(Color color) { color_ = color; }
setConvexObjectColor(Color color)46     void setConvexObjectColor(Color color) { convexObjectColor_ = color; }
47 
48     Error draw(const PixelDrawer& drawPixel, const Box<pxs>& bounds,
49                const ConvexObjectInfo* coBegin, const ConvexObjectInfo* coEnd);
50 
51   private:
52     pxs radius_;
53     Color color_;
54     Color convexObjectColor_;
55     bool roundTopLeft_;
56     bool roundTopRight_;
57     bool roundBottomLeft_;
58     bool roundBottomRight_;
59 };
60 
61 /**
62  * Button is a LayoutElement and should be used as second argument in the BEGIN_ELEMENT() macro.
63  * The template argument Derived is the new class derived from Button, that is created by the
64  * BEGIN_ELEMENT() macro. The arguments convexElemCount and convexObjectCapacity denote the number
65  * of convex objects and the capacity (maximum number of vertexes) each of the objects should have.
66  * This is used to reserve enough space at compile time.
67  */
68 template <typename Derived, typename convexElemCount = std::integral_constant<size_t, 0>,
69           typename convexObjectCapacity = std::integral_constant<size_t, 10>>
70 class Button : public LayoutElement<Derived>, public ButtonImpl {
71   public:
72     static const constexpr bool button_round_top_left = false;
73     static const constexpr bool button_round_top_right = false;
74     static const constexpr bool button_round_bottom_left = false;
75     static const constexpr bool button_round_bottom_right = false;
76     static const constexpr std::tuple<> button_drawable_objects = {};
77     static const constexpr Color button_drawable_object_color = 0xff000000;
78 
79   private:
80     ConvexObject<convexObjectCapacity::value> convex_objects_[convexElemCount::value];
81 
82   public:
83     Button() = default;
84     template <typename Context>
Button(const Context & context)85     explicit Button(const Context& context)
86         : LayoutElement<Derived>(context),
87           ButtonImpl(context = Derived::button_radius, context = Derived::button_color,
88                      context = Derived::button_drawable_object_color,
89                      Derived::button_round_top_left, Derived::button_round_top_right,
90                      Derived::button_round_bottom_left, Derived::button_round_bottom_right) {
91         static_assert(
92             convexElemCount::value >=
93                 std::tuple_size<decltype(Derived::button_drawable_objects)>::value,
94             "Reserved convex element count must be greater or equal to the number of given convex "
95             "objects. Set count by passing ConvexObjectCount(n) to BEGIN_ELEMENT as third "
96             "argument");
97         initConvexObjectArray(context, convex_objects_, Derived::button_drawable_objects);
98     }
99 
draw(const PixelDrawer & drawPixel)100     Error draw(const PixelDrawer& drawPixel) {
101         constexpr const size_t convex_object_count =
102             std::tuple_size<decltype(Derived::button_drawable_objects)>::value;
103         ButtonImpl::ConvexObjectInfo coInfo[convex_object_count];
104         for (size_t i = 0; i < convex_object_count; ++i) {
105             coInfo[i].begin = convex_objects_[i].begin();
106             coInfo[i].end = convex_objects_[i].end();
107         }
108         return ButtonImpl::draw(drawPixel, this->bounds_, &coInfo[0], &coInfo[convex_object_count]);
109     }
110 };
111 
112 }  //  namespace teeui
113 
114 #define CornerRadius(radius) static const constexpr auto button_radius = radius
115 
116 #define ButtonColor(color) static const constexpr auto button_color = color
117 
118 #define RoundTopLeft static const constexpr bool button_round_top_left = true
119 #define RoundTopRight static const constexpr bool button_round_top_right = true
120 #define RoundBottomLeft static const constexpr bool button_round_bottom_left = true
121 #define RoundBottomRight static const constexpr bool button_round_bottom_right = true
122 
123 /*
124  * ConvexObjecCount may be passed to BEGIN_ELEMENT as third argument to layout elements that
125  * draw convex objects, such as teeui::Button. It informs the underlying implementation
126  * how much memory to reserve for convex objects.
127  */
128 #define ConvexObjectCount(n) std::integral_constant<size_t, n>
129 
130 #define ConvexObjects(convex_objects)                                                              \
131     static constexpr const auto button_drawable_objects = convex_objects
132 
133 #define ConvexObjectColor(color) static constexpr const auto button_drawable_object_color = color
134 
135 #endif  // LIBTEEUI_BUTTON_H_
136