axmol/core/base/NinePatchImageParser.h

122 lines
3.9 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
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.
****************************************************************************/
#pragma once
#include "platform/PlatformMacros.h"
#include "math/Math.h"
NS_AX_BEGIN
class Image;
class SpriteFrame;
/**
* A class for paring Android .9 patch image.
* For more about Android .9 patch image format, please refer to
* http://developer.android.com/tools/help/draw9patch.html
*
* The class could parse a single .9 patch image and produce the capInsets
* as well as a sprite atlas and store all the capInsets infos in a Texture2D.
* Note:
* - Currently only PixelFormat::RGBA8 is supported.
* - TexturePacker Trim mode is not supported at the moment.
*/
2022-07-15 19:17:01 +08:00
class AX_DLL NinePatchImageParser
{
public:
/**
* Determines whether a filename contains ".9.png" suffix.
* @param filename A 9-patch image name.
*
* @return If the filename contains ".9.png", then return true, otherwise false.
*/
2021-12-26 23:26:34 +08:00
static bool isNinePatchImage(std::string_view filename);
/**
* Default constructor.
*
*/
NinePatchImageParser();
/**
* Instantiate a NinePatchImageParser with a Image object.
*
* @param image A Image object pointer.
*
* @return A NinePatchImageParser instance.
*/
explicit NinePatchImageParser(Image* image);
/**
* Instantiate a NinePatchImageParser with a Image object and the spriteFrame info.
* The spriteFrame contains the frame rect in the image atlas and whether it
* is rotated or not.
*
* @param image A Image object pointer.
* @param frameRect The sprite frame rect in the image atlas.
2016-02-18 15:16:36 +08:00
* @param rotated Whether is sprite frame is rotated in the image atlas.
*/
2016-02-18 15:16:36 +08:00
NinePatchImageParser(Image* image, const Rect& frameRect, bool rotated);
/**
* Change the sprite frame info.
* It is useful when parsing multiple sprite frame with only on NinePatchImageParser.
*
* @param frameRect The sprite frame rect in the image atlas.
* @param rotated Whether is sprite frame is rotated in the image atlas.
*/
void setSpriteFrameInfo(Image* image, const Rect& frameRect, bool rotated);
/**
* Default destructor.
*/
virtual ~NinePatchImageParser();
/**
* Parsing the image data and extract the capInsets info.
* @return The capInsets Rect.
*/
2021-12-25 10:04:45 +08:00
Rect parseCapInset() const;
private:
enum class Direction
{
HORIZONTAL,
VERTICAL
};
2021-12-25 10:04:45 +08:00
int getPixelOriginOffset(Direction direction) const;
Vec2 parseHorizontalMargin() const;
Vec2 parseVerticalMargin() const;
int getFrameWidth() const;
int getFrameHeight() const;
Image* _image;
Rect _imageFrame;
bool _isRotated;
};
NS_AX_END