UIScale9Sprite.h 20.9 KB
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 116 117 118 119 120 121 122 123 124 125 126 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/****************************************************************************
 Copyright (c) 2013-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

 http://www.cocos2d-x.org

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#ifndef __cocos2d_libs__UIScale9Sprite__
#define __cocos2d_libs__UIScale9Sprite__

#include "2d/CCSprite.h"
#include "2d/CCSpriteFrame.h"
#include "2d/CCSpriteBatchNode.h"
#include "platform/CCPlatformMacros.h"
#include "ui/GUIExport.h"
#include "renderer/CCTrianglesCommand.h"

/**
 * @addtogroup ui
 * @{
 */
NS_CC_BEGIN
class DrawNode;
class Texture2D;

namespace ui {

    /**
     *@brief A 9-slice sprite for cocos2d-x.
     *
     * 9-slice scaling allows you to specify how scaling is applied
     * to specific areas of a sprite. With 9-slice scaling (3x3 grid),
     * you can ensure that the sprite does not become distorted when
     * scaled.
     * Note: When you set _scale9Enabled to false,
     * then you could call `scale9Sprite->getSprite()` to return the inner Sprite pointer.
     * Then you could call any methods of Sprite class with the return pointers.
     *
     */
    class CC_GUI_DLL Scale9Sprite : public Sprite
    {
    public:
        /**
         * Default constructor.
         * @js ctor
         * @lua new
         */
        Scale9Sprite();

        /**
         * Default destructor.
         * @js NA
         * @lua NA
         */
        virtual ~Scale9Sprite();

        /**
         * Builtin shader state.
         * Currently support Normal and Gray state.
         */
        enum class State
        {
            NORMAL,
            GRAY
        };

        enum class RenderingType
        {
            SIMPLE,
            SLICE
        };

    public:

        /**
         * @brief Create an empty Scale9Sprite.
         *
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* create();

        /**
         * Creates a 9-slice sprite with a texture file, a delimitation zone and
         * with the specified cap insets.
         *
         * @see initWithFile(const char *file, const Rect& rect, const Rect& capInsets)
         * @param file A texture file name.
         * @param rect A delimitation zone.
         * @param capInsets A specified cap insets.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* create(const std::string& file, const Rect& rect,  const Rect& capInsets);

        /**
         * Creates a 9-slice sprite with a texture file. The whole texture will be
         * broken down into a 3×3 grid of equal blocks.
         *
         * @see initWithFile(const Rect& capInsets, const char *file)
         * @param capInsets A specified cap insets.
         * @param file A texture file name.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* create(const Rect& capInsets, const std::string& file);

        /**
         * Creates a 9-slice sprite with a texture file and a delimitation zone. The
         * texture will be broken down into a 3×3 grid of equal blocks.
         *
         * @see initWithFile(const char *file, const Rect& rect)
         * @param file A texture file name.
         * @param rect A delimitation zone.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* create(const std::string& file, const Rect& rect);

        /**
         * Creates a 9-slice sprite with a texture file. The whole texture will be
         * broken down into a 3×3 grid of equal blocks.
         *
         * @see initWithFile(const char *file)
         * @param file A texture file name.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* create(const std::string& file);

        /**
         * Creates a 9-slice sprite with an sprite frame.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @see initWithSpriteFrame(SpriteFrame *spriteFrame)
         * @param spriteFrame  A sprite frame pointer.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* createWithSpriteFrame(SpriteFrame* spriteFrame);

        /**
         * Creates a 9-slice sprite with an sprite frame and the centre of its zone.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @see initWithSpriteFrame(SpriteFrame *spriteFrame, const Rect& capInsets)
         * @param spriteFrame A sprite frame pointer.
         * @param capInsets  A delimitation zone.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* createWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets);

        /**
         * Creates a 9-slice sprite with an sprite frame name.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @see initWithSpriteFrameName(const char *spriteFrameName)
         * @param spriteFrameName A sprite frame name.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);

        /**
         * Creates a 9-slice sprite with an sprite frame name and the centre of its zone.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @see initWithSpriteFrameName(const char *spriteFrameName, const Rect& capInsets)
         * @param spriteFrameName A sprite frame name.
         * @param capInsets A delimitation zone.
         * @return A Scale9Sprite instance.
         */
        static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);

        // overridden methods that takes different parameters
        using Sprite::initWithFile;
        using Sprite::initWithSpriteFrame;
        using Sprite::initWithSpriteFrameName;
        using Sprite::setSpriteFrame;

        /**
         * Initializes a 9-slice sprite with a texture file, a delimitation zone and
         * with the specified cap insets.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param file The name of the texture file.
         * @param rect The rectangle that describes the sub-part of the texture that
         * is the whole image. If the shape is the whole texture, set this to the
         * texture's full rect.
         * @param capInsets The values to use for the cap insets.
         * @return True if initialize success, false otherwise.
         */
        virtual bool initWithFile(const std::string& file, const Rect& rect,  const Rect& capInsets);

        /**
         * Initializes a 9-slice sprite with a texture file and with the specified cap
         * insets.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param file The name of the texture file.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithFile(const Rect& capInsets, const std::string& file);

        /**
         * Initializes a 9-slice sprite with an sprite frame and with the specified
         * cap insets.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param spriteFrame The sprite frame object.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithSpriteFrame(SpriteFrame* spriteFrame, const Rect& capInsets);

        /**
         * Initializes a 9-slice sprite with an sprite frame name and with the specified
         * cap insets.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param spriteFrameName The sprite frame name.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);

        //override function

        /**
         * Initializes a 9-slice sprite with a texture file and a delimitation zone. The
         * texture will be broken down into a 3×3 grid of equal blocks.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param file The name of the texture file.
         * @param rect The rectangle that describes the sub-part of the texture that
         * is the whole image. If the shape is the whole texture, set this to the
         * texture's full rect.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithFile(const std::string& file, const Rect& rect) override;

        /**
         * Initializes a 9-slice sprite with a texture file. The whole texture will be
         * broken down into a 3×3 grid of equal blocks.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param file The name of the texture file.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithFile(const std::string& file) override;

        /**
         * Initializes a 9-slice sprite with an sprite frame name.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param spriteFrameName The sprite frame name.
         * @return True if initializes success, false otherwise.
         */
        virtual bool initWithSpriteFrameName(const std::string& spriteFrameName) override;

        virtual bool init() override;

        /**
         * @brief Initializes a 9-slice sprite with an sprite instance.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param sprite The sprite instance.
         * @param rect A delimitation zone.
         * @param rotated Whether the sprite is rotated or not.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool init(Sprite* sprite, const Rect& rect, bool rotated, const Rect& capInsets);

        /**
         * @brief Initializes a 9-slice sprite with an sprite instance.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param sprite The sprite instance.
         * @param rect A delimitation zone.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool init(Sprite* sprite, const Rect& rect, const Rect& capInsets);

        /**
         * @brief Initializes a 9-slice sprite with an sprite instance.
         * Once the sprite is created, you can then call its "setContentSize:" method
         * to resize the sprite will all it's 9-slice goodness interact.
         * It respects the anchorPoint too.
         *
         * @param sprite The sprite instance.
         * @param rect A delimitation zone.
         * @param rotated Whether the sprite is rotated or not.
         * @param offset The offset when slice the sprite.
         * @param originalSize The original size of sprite.
         * @param capInsets The values to use for the cap insets.
         * @return True if initializes success, false otherwise.
         */
        virtual bool init(Sprite* sprite,
                          const Rect& rect,
                          bool rotated,
                          const Vec2 &offset,
                          const Size &originalSize,
                          const Rect& capInsets);

        /**
         * Creates and returns a new sprite object with the specified cap insets.
         * You use this method to add cap insets to a sprite or to change the existing
         * cap insets of a sprite. In both cases, you get back a new image and the
         * original sprite remains untouched.
         *
         * @param capInsets The values to use for the cap insets.
         * @return A Scale9Sprite instance.
         */
        Scale9Sprite* resizableSpriteWithCapInsets(const Rect& capInsets) const;


        /**
         * @brief Update Scale9Sprite with a specified sprite.
         *
         * @param sprite A sprite pointer.
         * @param rect A delimitation zone.
         * @param rotated Whether the sprite is rotated or not.
         * @param capInsets The Values to use for the cap insets.
         * @return True if update success, false otherwise.
         * @js NA
         */
        virtual bool updateWithSprite(Sprite* sprite,
                                      const Rect& rect,
                                      bool rotated,
                                      const Rect& capInsets);

        /**
         * @brief Update Scale9Sprite with a specified sprite.
         *
         * @param sprite A sprite pointer.
         * @param rect A delimitation zone.
         * @param rotated Whether the sprite is rotated or not.
         * @param offset The offset when slice the sprite.
         * @param originalSize The original size of the sprite.
         * @param capInsets The Values to use for the cap insets.
         * @return True if update success, false otherwise.
         * @js NA
         */
        virtual bool updateWithSprite(Sprite* sprite,
                                      const Rect& rect,
                                      bool rotated,
                                      const Vec2 &offset,
                                      const Size &originalSize,
                                      const Rect& capInsets);

        /**
         * @brief Change inner sprite's sprite frame.
         *
         * @param spriteFrame A sprite frame pointer.
         * @param capInsets The values to use for the cap insets.
         */
        virtual void setSpriteFrame(SpriteFrame * spriteFrame, const Rect& capInsets);

        /**
         * Change the state of 9-slice sprite.
         * @see `State`
         * @param state A enum value in State.
         * @since v3.4
         */
        void setState(State state);

        /**
         * Query the current bright state.
         * @return @see `State`
         * @since v3.7
         */
        State getState() const;

        /**
         * @brief Query the sprite's original size.
         *
         * @return Sprite size.
         */
        Size getOriginalSize() const;

        /**
         * @brief Change the preferred size of Scale9Sprite.
         *
         * @param size A delimitation zone.
         */
        void setPreferredSize(const Size& size);

        /**
         * @brief Query the Scale9Sprite's preferred size.
         *
         * @return Scale9Sprite's preferred size.
         */
        Size getPreferredSize() const;

        /**
         * @brief Change the left sprite's cap inset.
         *
         * @param leftInset The values to use for the cap inset.
         */
        void setInsetLeft(float leftInset);

        /**
         * @brief Query the left sprite's cap inset.
         *
         * @return The left sprite's cap inset.
         */
        float getInsetLeft() const;

        /**
         * @brief Change the top sprite's cap inset.
         *
         * @param topInset The values to use for the cap inset.
         */
        void setInsetTop(float topInset);

        /**
         * @brief Query the top sprite's cap inset.
         *
         * @return The top sprite's cap inset.
         */
        float getInsetTop() const;

        /**
         * @brief Change the right sprite's cap inset.
         *
         * @param rightInset The values to use for the cap inset.
         */
        void setInsetRight(float rightInset);

        /**
         * @brief Query the right sprite's cap inset.
         *
         * @return The right sprite's cap inset.
         */
        float getInsetRight() const;

        /**
         * @brief Change the bottom sprite's cap inset.
         *
         * @param bottomInset The values to use for the cap inset.

         */
        void setInsetBottom(float bottomInset);

        /**
         * @brief Query the bottom sprite's cap inset.
         *
         * @return The bottom sprite's cap inset.
         */
        float getInsetBottom() const;

        /**
         * @brief Toggle 9-slice feature.
         * If Scale9Sprite is 9-slice disabled, the Scale9Sprite will rendered as a normal sprite.
         * @warning: Don't use setScale9Enabled(false), use setRenderingType(RenderingType::SIMPLE) instead.
         *        The setScale9Enabled(false) is kept only for back back compatibility.
         * @param enabled True to enable 9-slice, false otherwise.
         * @js NA
         */
        void setScale9Enabled(bool enabled);

        /**
         * @brief Query whether the Scale9Sprite is enable 9-slice or not.
         *
         * @return True if 9-slice is enabled, false otherwise.
         * @js NA
         */
        bool isScale9Enabled() const;

        /// @} end of Children and Parent

        /**
         * @brief Get the original no 9-sliced sprite
         *
         * @return A sprite instance.
         */
        Sprite* getSprite();

        /**
         * @brief copies self to copy
         */
        void copyTo(Scale9Sprite* copy) const;


        /**
         * Set the slice sprite rendering type.
         * When setting to SIMPLE, only 4 vertexes is used to rendering.
         * otherwise 16 vertexes will be used to rendering.
         * @see RenderingType
         */
        void setRenderingType(RenderingType type);

        /**
         * Return the slice sprite rendering type.
         */
        RenderingType getRenderingType() const;

        /**
         * Set the Cap Insets in Points using the untrimmed size as reference
         */
        void setCapInsets(const Rect& insets);
        /**
         * Returns the Cap Insets
         */
        Rect getCapInsets() const;

        void resetRender();

    protected:
        void updateCapInset();
        void setupSlice9(Texture2D* texture, const Rect& capInsets);

        bool _isPatch9;

        float _insetLeft;
        float _insetRight;
        float _insetTop;
        float _insetBottom;

        Scale9Sprite::State _brightState;
        Scale9Sprite::RenderingType _renderingType;
    };
    
}}  //end of namespace
// end of ui group
/// @}

#endif /* defined(__cocos2d_libs__UIScale9Sprite__) */