ui_arc_label.h 9.3 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup UI_Components
 * @{
 *
 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
 *
 * @since 1.0
 * @version 1.0
 */

/**
 * @file ui_arc_label.h
 *
 * @brief Defines the attributes of an arc label.
 *
 * The attributes include the center and radius of an arc, angle range, and text orientation.
 *
 * @since 1.0
 * @version 1.0
 */

#ifndef GRAPHIC_LITE_UI_ARC_LABEL_H
#define GRAPHIC_LITE_UI_ARC_LABEL_H

#include "common/text.h"
#include "components/ui_view.h"

namespace OHOS {
/**
 * @brief Defines functions related to an arc label.
 *
 * @since 1.0
 * @version 1.0
 */
class UIArcLabel : public UIView {
public:
    /**
     * @brief Enumerates text orientations.
     */
    enum class TextOrientation : uint8_t {
        /** Inside */
        INSIDE,
        /** Outside */
        OUTSIDE,
    };

    /**
     * @brief A default constructor used to create a <b>UIArcLabel</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UIArcLabel();

    /**
     * @brief A destructor used to delete the <b>UIArcLabel</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UIArcLabel();

    /**
     * @brief Obtains the view type.
     *
     * @return Returns <b>UI_ARC_LABEL</b>, as defined in {link UIViewType}.
     * @since 1.0
     * @version 1.0
     */
    UIViewType GetViewType() const override
    {
        return UI_ARC_LABEL;
    }

    /**
     * @brief Obtains the width of this arc text.
     *
     * @return Returns the width of this arc text.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetWidth() override
    {
        ReMeasure();
        return UIView::GetWidth();
    }

    /**
     * @brief Obtains the height of this arc text.
     *
     * @return Returns the height of this arc text.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetHeight() override
    {
        ReMeasure();
        return UIView::GetHeight();
    }

116 117 118 119 120 121 122 123 124 125 126
    /**
     * @brief Sets the view style.
     * @param style Indicates the view style.
     * @since 1.0
     * @version 1.0
     */
    virtual void SetStyle(Style& style) override
    {
        UIView::SetStyle(style);
    }

M
mamingshuai 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    /**
     * @brief Sets a style.
     *
     * @param key Indicates the key of the style to set.
     * @param value Indicates the value matching the key.
     * @since 1.0
     * @version 1.0
     */
    void SetStyle(uint8_t key, int64_t value) override;

    /**
     * @brief Sets the text content for this arc label.
     *
     * @param text Indicates the pointer to the text content.
     * @since 1.0
     * @version 1.0
     */
    void SetText(const char* text);

    /**
     * @brief Obtains the text of this arc label.
     *
     * @return Returns the text.
     * @since 1.0
     * @version 1.0
     */
    const char* GetText() const;

    /**
     * @brief Sets the alignment mode for this text.
     *
     * @param horizontalAlign Indicates the horizontal alignment mode to set,
     *                        which can be {@link TEXT_ALIGNMENT_LEFT},
     *                        {@link TEXT_ALIGNMENT_CENTER}, or {@link TEXT_ALIGNMENT_RIGHT}.
     * @since 1.0
     * @version 1.0
     */
    void SetAlign(UITextLanguageAlignment horizontalAlign);

    /**
     * @brief Obtains the horizontal alignment mode.
     *
     * @return Returns the horizontal alignment mode.
     * @since 1.0
     * @version 1.0
     */
    UITextLanguageAlignment GetHorAlign();

    /**
     * @brief Obtains the direction of this text.
     *
     * @return Returns the text direction, as defined in {@link UITextLanguageDirect}.
     * @since 1.0
     * @version 1.0
     */
    UITextLanguageDirect GetDirect();

    /**
     * @brief Sets the font ID for this arc label.
     *
     * @param fontId Indicates the font ID composed of font name and size.
     * @since 1.0
     * @version 1.0
     */
    void SetFontId(uint8_t fontId);

    /**
     * @brief Obtains the font ID composed of font name and size.
     *
     * @return Returns the front ID of this arc label.
     * @since 1.0
     * @version 1.0
     */
    uint8_t GetFontId();

    /**
     * @brief Sets the font for this arc label.
     *
     * @param name Indicates the pointer to the font name.
     * @param size Indicates the font size to set.
     * @since 1.0
     * @version 1.0
     */
    void SetFont(const char* name, uint8_t size);

    /**
     * @brief Sets the center position for this arc text.
     *
     * @param x Indicates the x-coordinate to set.
     * @param y Indicates the y-coordinate to set.
     * @since 1.0
     * @version 1.0
     */
    void SetArcTextCenter(int16_t x, int16_t y)
    {
        if ((arcCenter_.x != x) || (arcCenter_.y != y)) {
            arcCenter_.x = x;
            arcCenter_.y = y;
            RefreshArcLabel();
        }
    }

    /**
     * @brief Obtains the center position of this arc text.
     *
     * @return Returns the center position of this arc text.
     * @since 1.0
     * @version 1.0
     */
    Point GetArcTextCenter() const
    {
        return arcCenter_;
    }

    /**
     * @brief Sets the radius for this arc text.
     *
     * @param radius Indicates the radius to set.
     * @since 1.0
     * @version 1.0
     */
    void SetArcTextRadius(uint16_t radius)
    {
        if (radius_ != radius) {
            radius_ = radius;
            RefreshArcLabel();
        }
    }

    /**
     * @brief Obtains the radius of this arc text.
     *
     * @return Returns the radius of this arc text.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetArcTextRadius() const
    {
        return radius_;
    }

    /**
     * @brief Sets the start angle and end angle for this arc text.
     *
     * The angle in 12 o'clock direction is 0 degrees, and the value increases clockwise.
     * The text direction is clockwise when the end angle is greater than the start angle, and the text direction is
     * counterclockwise otherwise.
     *
     * @param startAngle Indicates the start angle to set.
     * @param endAngle Indicates the end angle to set.
     * @since 1.0
     * @version 1.0
     */
    void SetArcTextAngle(int16_t startAngle, int16_t endAngle)
    {
        if ((startAngle_ != startAngle) || (endAngle_ != endAngle)) {
            startAngle_ = startAngle;
            endAngle_ = endAngle;
            RefreshArcLabel();
        }
    }

    /**
     * @brief Obtains the start angle of this arc text.
     *
     * @return Returns the start angle of this arc text.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetArcTextStartAngle() const
    {
        return startAngle_;
    }

    /**
     * @brief Obtains the end angle of this arc text.
     *
     * @return Returns the end angle of this arc text.
     * @since 1.0
     * @version 1.0
     */
    int16_t GetArcTextEndAngle() const
    {
        return endAngle_;
    }

    /**
     * @brief Sets the orientation for this arc text.
     *
     * @param orientation Indicates the text orientation to set.
     * @since 1.0
     * @version 1.0
     */
    void SetArcTextOrientation(TextOrientation orientation)
    {
        if (orientation_ != orientation) {
            orientation_ = orientation;
            RefreshArcLabel();
        }
    }

    /**
     * @brief Obtains the orientation of this arc text.
     *
     * @return Returns the orientation of this arc text.
     * @since 1.0
     * @version 1.0
     */
    TextOrientation GetArcTextOrientation() const
    {
        return orientation_;
    }

    /**
     * @brief Draws an arc text.
     *
     * @param invalidatedArea Indicates the area to draw.
     * @since 1.0
     * @version 1.0
     */
N
niulihua 已提交
347
    virtual void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
M
mamingshuai 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

    /**
     * @brief Stores the attribute information about this arc text to draw.
     */
    struct ArcTextInfo {
        uint16_t radius;
        float startAngle;
        Point arcCenter;
        uint32_t lineStart;
        uint32_t lineEnd;
        UITextLanguageDirect direct;
    };

protected:
    Text* arcLabelText_;

364 365 366 367 368 369 370 371 372 373
    virtual void InitArcLabelText()
    {
        if (arcLabelText_ == nullptr) {
            arcLabelText_ = new Text();
            if (arcLabelText_ == nullptr) {
                HILOG_ERROR(HILOG_MODULE_GRAPHIC, "new Text fail");
                return;
            }
        }
    }
M
mamingshuai 已提交
374 375 376 377 378
    void RefreshArcLabel();

private:
    virtual void ReMeasure() override;
    void MeasureArcTextInfo();
N
niulihua 已提交
379
    void DrawArcText(BufferInfo& gfxDstBuffer, const Rect& mask, OpacityType opaScale);
M
mamingshuai 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392

    bool needRefresh_;
    Point textSize_;
    uint16_t radius_;
    int16_t startAngle_;
    int16_t endAngle_;
    Point arcCenter_;
    TextOrientation orientation_;

    ArcTextInfo arcTextInfo_;
};
} // namespace OHOS
#endif // GRAPHIC_LITE_UI_ARC_LABEL_H