axmol/core/2d/AtlasNode.h

157 lines
5.3 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
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/
2019-11-23 20:27:39 +08:00
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 "2d/Node.h"
#include "base/Protocols.h"
#include "base/Types.h"
#include "renderer/QuadCommand.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _2d
* @{
*/
class TextureAtlas;
/** @brief AtlasNode is a subclass of Node that implements the RGBAProtocol and TextureProtocol protocol.
* It knows how to render a TextureAtlas object.
* If you are going to render a TextureAtlas consider subclassing AtlasNode (or a subclass of AtlasNode).
* All features from Node are valid, plus the following features:
* - opacity and RGB colors.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL AtlasNode : public Node, public TextureProtocol
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
public:
2021-12-25 10:04:45 +08:00
/** creates a AtlasNode with an Atlas file the width and height of each item and the quantity of items to render.
2019-11-23 20:27:39 +08:00
*
* @param filename The path of Atlas file.
* @param tileWidth The width of the item.
* @param tileHeight The height of the item.
* @param itemsToRender The quantity of items to render.
*/
static AtlasNode* create(std::string_view filename, int tileWidth, int tileHeight, int itemsToRender);
2019-11-23 20:27:39 +08:00
/** updates the Atlas (indexed vertex array).
2021-12-25 10:04:45 +08:00
* Shall be overridden in subclasses.
*/
2019-11-23 20:27:39 +08:00
virtual void updateAtlasValues();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Overrides
2021-12-25 10:04:45 +08:00
virtual void draw(Renderer* renderer, const Mat4& transform, uint32_t flags) override;
2019-11-23 20:27:39 +08:00
virtual Texture2D* getTexture() const override;
2021-12-25 10:04:45 +08:00
virtual void setTexture(Texture2D* texture) override;
2019-11-23 20:27:39 +08:00
virtual bool isOpacityModifyRGB() const override;
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
virtual const Color3B& getColor() const override;
virtual void setColor(const Color3B& color) override;
virtual void setOpacity(uint8_t opacity) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @code
* When this function bound into js or lua,the parameter will be changed
* In js: var setBlendFunc(var src, var dst)
* @endcode
* @lua NA
*/
virtual void setBlendFunc(const BlendFunc& blendFunc) override;
/**
* @lua NA
*/
virtual const BlendFunc& getBlendFunc() const override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Set an buffer manager of the texture vertex. */
void setTextureAtlas(TextureAtlas* textureAtlas);
2021-12-25 10:04:45 +08:00
/** Return the buffer manager of the texture vertex.
2019-11-23 20:27:39 +08:00
*
* @return Return A TextureAtlas.
*/
TextureAtlas* getTextureAtlas() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void setQuadsToDraw(ssize_t quadsToDraw);
size_t getQuadsToDraw() const;
bool setProgramState(backend::ProgramState* programState, bool needsRetain = true) override;
AtlasNode() = default;
2019-11-23 20:27:39 +08:00
virtual ~AtlasNode();
2021-12-25 10:04:45 +08:00
/** Initializes an AtlasNode with an Atlas file the width and height of each item and the quantity of items to
* render*/
bool initWithTileFile(std::string_view tile, int tileWidth, int tileHeight, int itemsToRender);
2021-12-25 10:04:45 +08:00
/** Initializes an AtlasNode with a texture the width and height of each item measured in points and the quantity
* of items to render*/
2019-11-23 20:27:39 +08:00
bool initWithTexture(Texture2D* texture, int tileWidth, int tileHeight, int itemsToRender);
protected:
friend class Director;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void calculateMaxItems();
void updateBlendFunc();
void updateOpacityModifyRGB();
void setIgnoreContentScaleFactor(bool bIgnoreContentScaleFactor);
/** Chars per row. */
2021-12-25 10:04:45 +08:00
int _itemsPerRow = 0;
2019-11-23 20:27:39 +08:00
/** Chars per column. */
2021-12-25 10:04:45 +08:00
int _itemsPerColumn = 0;
2019-11-23 20:27:39 +08:00
/** Width of each char. */
2021-12-25 10:04:45 +08:00
int _itemWidth = 0;
2019-11-23 20:27:39 +08:00
/** Height of each char. */
2021-12-25 10:04:45 +08:00
int _itemHeight = 0;
Color3B _colorUnmodified;
2019-11-23 20:27:39 +08:00
TextureAtlas* _textureAtlas = nullptr;
/** Protocol variables. */
bool _isOpacityModifyRGB = false;
BlendFunc _blendFunc;
/** Quads to draw. */
size_t _quadsToDraw = 0;
/** This variable is only used for LabelAtlas FPS display. So plz don't modify its value. */
bool _ignoreContentScaleFactor = false;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
QuadCommand _quadCommand;
backend::UniformLocation _mvpMatrixLocation;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(AtlasNode);
2019-11-23 20:27:39 +08:00
};
// end of base_node group
/// @}
NS_AX_END