1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H 18 19 #include "base/geometry/dimension.h" 20 #include "base/geometry/ng/offset_t.h" 21 #include "base/geometry/ng/size_t.h" 22 #include "core/components/common/properties/alignment.h" 23 #include "core/components/common/properties/border.h" 24 #include "core/components/common/properties/edge.h" 25 #include "core/components/common/properties/placement.h" 26 #include "core/components/common/properties/shadow.h" 27 #include "core/components/common/properties/shadow_config.h" 28 #include "core/components_ng/render/canvas_image.h" 29 #include "core/components_ng/render/drawing.h" 30 #include "core/components_ng/render/node_paint_method.h" 31 #include "core/components_ng/render/paint_wrapper.h" 32 33 namespace OHOS::Ace::NG { 34 35 class ACE_EXPORT BubblePaintMethod : public NodePaintMethod { 36 DECLARE_ACE_TYPE(BubblePaintMethod, NodePaintMethod) 37 public: 38 BubblePaintMethod() = default; 39 ~BubblePaintMethod() override = default; 40 GetContentDrawFunction(PaintWrapper * paintWrapper)41 CanvasDrawFunction GetContentDrawFunction(PaintWrapper* paintWrapper) override 42 { 43 return [weak = WeakClaim(this), paintWrapper](RSCanvas& canvas) { 44 auto bubble = weak.Upgrade(); 45 if (bubble) { 46 bubble->PaintMask(canvas, paintWrapper); 47 bubble->ClipBubble(paintWrapper); 48 bubble->PaintBorder(canvas, paintWrapper); 49 } 50 }; 51 } 52 GetOverlayDrawFunction(PaintWrapper * paintWrapper)53 CanvasDrawFunction GetOverlayDrawFunction(PaintWrapper* paintWrapper) override 54 { 55 return [weak = WeakClaim(this), paintWrapper](RSCanvas& canvas) { 56 auto bubble = weak.Upgrade(); 57 if (bubble) { 58 bubble->PaintSingleBorder(canvas, paintWrapper); 59 bubble->PaintInnerBorder(canvas, paintWrapper); 60 bubble->PaintOuterBorder(canvas, paintWrapper); 61 } 62 }; 63 } 64 SetShowArrow(bool flag)65 void SetShowArrow(bool flag) 66 { 67 showArrow_ = flag; 68 } 69 SetChildOffset(const OffsetF & offset)70 void SetChildOffset(const OffsetF& offset) 71 { 72 childOffset_ = offset; 73 } 74 SetChildSize(const SizeF & size)75 void SetChildSize(const SizeF& size) 76 { 77 childSize_ = size; 78 } 79 SetArrowPosition(const OffsetF & offset)80 void SetArrowPosition(const OffsetF& offset) 81 { 82 arrowPosition_ = offset; 83 } 84 SetClipPath(const std::string & clipPath)85 void SetClipPath(const std::string& clipPath) 86 { 87 clipPath_ = clipPath; 88 } 89 SetBorder(const Border & border)90 void SetBorder(const Border& border) 91 { 92 border_ = border; 93 } 94 SetClipFrameNode(RefPtr<FrameNode> & clipFrameNode)95 void SetClipFrameNode(RefPtr<FrameNode>& clipFrameNode) 96 { 97 clipFrameNode_ = clipFrameNode; 98 } 99 SetArrowOffsetsFromClip(const std::vector<std::vector<float>> & arrowOffsetsFromClip)100 void SetArrowOffsetsFromClip(const std::vector<std::vector<float>>& arrowOffsetsFromClip) 101 { 102 arrowOffsetsFromClip_ = arrowOffsetsFromClip; 103 } 104 SetArrowWidth(const float arrowWidth)105 void SetArrowWidth(const float arrowWidth) 106 { 107 arrowWidth_ = arrowWidth; 108 } 109 SetArrowHeight(const float arrowHeight)110 void SetArrowHeight(const float arrowHeight) 111 { 112 arrowHeight_ = arrowHeight; 113 } 114 SetOuterBorderWidth(const Dimension outerBorderWidth)115 void SetOuterBorderWidth(const Dimension outerBorderWidth) 116 { 117 outerBorderWidth_ = outerBorderWidth.ConvertToPx(); 118 } 119 SetInnerBorderWidth(const Dimension innerBorderWidth)120 void SetInnerBorderWidth(const Dimension innerBorderWidth) 121 { 122 innerBorderWidth_ = innerBorderWidth.ConvertToPx(); 123 } 124 SetArrowBuildPlacement(const Placement arrowBuildPlacement)125 void SetArrowBuildPlacement(const Placement arrowBuildPlacement) 126 { 127 arrowBuildPlacement_ = arrowBuildPlacement; 128 } 129 130 void PaintBubble(RSCanvas& canvas, PaintWrapper* paintWrapper); 131 void PaintMask(RSCanvas& canvas, PaintWrapper* paintWrapper); 132 void PaintBorder(RSCanvas& canvas, PaintWrapper* paintWrapper); 133 void ClipBubble(PaintWrapper* paintWrapper); 134 void PaintSingleBorder(RSCanvas& canvas, PaintWrapper* paintWrapper); 135 void PaintDoubleBorder(RSCanvas& canvas, PaintWrapper* paintWrapper); 136 void PaintOuterBorder(RSCanvas& canvas, PaintWrapper* paintWrapper); 137 void PaintInnerBorder(RSCanvas& canvas, PaintWrapper* paintWrapper); 138 bool IsPaintDoubleBorder(PaintWrapper* paintWrapper); 139 void DrawDashedBorder(RSCanvas& canvas, RSPen& paint); 140 141 private: 142 void PaintBubbleWithArrow(RSCanvas& canvas, PaintWrapper* paintWrapper); 143 void PaintDoubleBorderWithArrow(RSCanvas& canvas, PaintWrapper* paintWrapper); 144 void PaintNonCustomPopup(RSCanvas& canvas, PaintWrapper* wrapper); 145 146 void PaintTopBubble(RSCanvas& canvas); 147 void PaintBottomBubble(RSCanvas& canvas); 148 void PaintDefaultBubble(RSCanvas& canvas); 149 150 void UpdateArrowOffset(const std::optional<Dimension>& offset, const Placement& placement); 151 152 RSRoundRect MakeRRect(); 153 float GetArrowOffset(const Placement& placement); 154 void InitEdgeSize(Edge& edge); 155 156 void BuildCompletePath(RSPath& path); 157 void BuildCornerPath(RSPath& path, const Placement& placement, float radius); 158 void BuildTopLinePath(RSPath& path, float arrowOffset, float radius); 159 void BuildRightLinePath(RSPath& path, float arrowOffset, float radius); 160 void BuildBottomLinePath(RSPath& path, float arrowOffset, float radius); 161 void BuildLeftLinePath(RSPath& path, float arrowOffset, float radius); 162 void PaintShadow(const RSPath& path, const Shadow& shadow, RSCanvas& canvas); 163 void ClipBubbleWithPath(const RefPtr<FrameNode>& frameNode); 164 165 void BuildDoubleBorderPath(RSPath& path); 166 void BuildTopDoubleBorderPath(RSPath& path, float radius); 167 void BuildRightDoubleBorderPath(RSPath& path, float radius); 168 void BuildBottomDoubleBorderPath(RSPath& path, float radius); 169 void BuildLeftDoubleBorderPath(RSPath& path, float radius); 170 171 float GetInnerBorderOffset(); 172 float GetBorderOffset(); 173 float outerBorderWidth_ = Dimension(0.8_vp).ConvertToPx(); 174 float innerBorderWidth_ = Dimension(0.6_vp).ConvertToPx(); 175 bool needPaintOuterBorder_ = false; 176 bool isPaintBubble_ = false; 177 std::vector<std::vector<float>> arrowOffsetsFromClip_ 178 = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} }; 179 float arrowWidth_ = Dimension(16.0_vp).ConvertToPx(); 180 float arrowHeight_ = Dimension(8.0_vp).ConvertToPx(); 181 182 // Get from RenderProp 183 bool isTips_ = false; 184 bool useCustom_ = false; 185 Placement arrowPlacement_ = Placement::BOTTOM; 186 bool enableArrow_ = false; 187 Dimension arrowOffset_; 188 Color maskColor_; 189 Color backgroundColor_; 190 191 // Get from pattern 192 OffsetF childOffset_; 193 OffsetF arrowPosition_; 194 SizeF childSize_; 195 bool showArrow_ = false; 196 std::string clipPath_; 197 RefPtr<FrameNode> clipFrameNode_; 198 Placement arrowBuildPlacement_ = Placement::BOTTOM; 199 // Get from theme 200 Border border_; 201 Edge padding_; 202 // top right bottom left 203 std::vector<float> arrowOffsetByClips_ = { 0.0f, 0.0f, 0.0f, 0.0f }; 204 205 #ifndef USE_ROSEN_DRAWING 206 RSPath path_; 207 #else 208 RSRecordingPath path_; 209 #endif 210 211 ACE_DISALLOW_COPY_AND_MOVE(BubblePaintMethod); 212 }; 213 } // namespace OHOS::Ace::NG 214 215 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H