UILayoutComponent.h 12.6 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
/****************************************************************************
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__LayoutComponent__
#define __cocos2d_libs__LayoutComponent__
#include "2d/CCComponent.h"
#include "ui/GUIExport.h"

NS_CC_BEGIN
/**
 * @addtogroup ui
 * @{
 */

namespace ui {
    /**
     *@brief A component class used for layout.
     * The LayoutComponent holds all the data for layouting.
     */
    class CC_GUI_DLL LayoutComponent : public Component
    {
    public:
        /**
         * Default constructor
         *
         * @lua new
         */
        LayoutComponent();

        /**
         * Default destructor
         *
         * @lua NA
         */
        ~LayoutComponent();

        virtual bool init()override;

        /**
         * Create a LayoutComponent instance with default settings.
         */
        CREATE_FUNC(LayoutComponent);

        /**
         * Bind a LayoutComponent to a specified node.
         * If the node has already binded a LayoutComponent named __LAYOUT_COMPONENT_NAME, just return the LayoutComponent.
         * Otherwise, create a new LayoutComponent and bind the LayoutComponent to the node.
         *@param node A Node* instance pointer.
         *@return The binded LayoutComponent instance pointer.
         */
        static LayoutComponent* bindLayoutComponent(Node* node);

        /**
         * Horizontal dock position type.
         */
        enum class HorizontalEdge
        {
            None,
            Left,
            Right,
            Center
        };

        /**
         * Vertical dock position type.
         */
        enum class VerticalEdge
        {
            None,
            Bottom,
            Top,
            Center
        };

        /**
         * Percent content size is used to adapt node's content size based on parent's content size.
         * If set to true then node's content size will be changed based on the value set by @see setPercentContentSize
         *@param isUsed True to enable percent content size, false otherwise.
         */
        void setUsingPercentContentSize(bool isUsed);

        /**
         * Query whether use percent content size or not.
         *@return True if using percent content size, false otherwise.
         */
        bool getUsingPercentContentSize()const;

        /**
         * Set percent content size.
         * The value should be [0-1], 0 means the child's content size will be 0
         * and 1 means the child's content size is the same as its parents.
         *@param percent The percent (x,y) of the node in [0-1] scope.
         */
        void setPercentContentSize(const Vec2 &percent);

        /**
         * Query the percent content size value.
         *@return Percent (x,y) in Vec2.
         */
        Vec2 getPercentContentSize()const;

        /**
         * Query the anchor position.
         *@return Anchor position to it's parent
         */
        const Point& getAnchorPosition()const;

        /**
         * Change the anchor position to it's parent.
         *@param point A value in (x,y) format.
         */
        void setAnchorPosition(const Point& point);

        /**
         * Query the owner's position.
         *@return The owner's position.
         */
        const Point& getPosition()const;

        /**
         * Change the position of component owner.
         * @param position A position in (x,y)
         */ 
        void setPosition(const Point& position);

        /**
         * Whether position percentX is enabled or not. 
         *@return True if position percentX is enable, false otherwise.
         */
        bool isPositionPercentXEnabled()const;
        
        /**
         * Toggle position percentX enabled.
         *@param isUsed  True if enable position percentX, false otherwise.
         */
        void setPositionPercentXEnabled(bool isUsed);

        /**
         * Query the position percent X value.
         *@return Position percent X value in float.
         */
        float getPositionPercentX()const;

        /**
         * Change position percent X value.
         *@param percentMargin Margin in float.
         */
        void setPositionPercentX(float percentMargin);

        /**
         * Whether position percentY is enabled or not.
         *@see `setPositionPercentYEnabled`
         *@return True if position percentY is enabled, false otherwise.
         */
        bool isPositionPercentYEnabled()const;

        /**
         * Toggle position percentY enabled.
         *@param isUsed True if position percentY is enabled, false otherwise.
         */
        void setPositionPercentYEnabled(bool isUsed);

        /**
         * Query the position percentY Y value.
         *@return Position percent Y value in float.
         */
        float getPositionPercentY()const;

        /**
         * Change position percentY value.
         *@param percentMargin Margin in float.
         */
        void setPositionPercentY(float percentMargin);

        /**
         * Query element horizontal dock type.
         *@return Horizontal dock type.
         */
        HorizontalEdge getHorizontalEdge()const;

        /**
         * Change element's horizontal dock type.
         *@param hEage Horizontal dock type @see `HorizontalEdge`
         */
        void setHorizontalEdge(HorizontalEdge hEage);

        /**
         * Query element vertical dock type.
         *@return Vertical dock type.
         */
        VerticalEdge getVerticalEdge()const;

        /**
         * Change element's vertical dock type.
         *@param vEage Vertical dock type @see `VerticalEdge`.
         */
        void setVerticalEdge(VerticalEdge vEage);

        /**
         * Query left margin of owner relative to its parent.
         *@return Left margin in float.
         */
        float getLeftMargin()const;

        /**
         * Change left margin of owner relative to its parent.
         *@param margin Margin in float.
         */
        void setLeftMargin(float margin);

        /**
         * Query the right margin of owner relative to its parent.
         *@return Right margin in float.
         */
        float getRightMargin()const;

        /**
         * Change right margin of owner relative to its parent.
         *@param margin Margin in float.
         */
        void setRightMargin(float margin);

        /**
         * Query the top margin of owner relative to its parent.
         *@return Top margin in float.
         */
        float getTopMargin()const;

        /**
         * Change the top margin of owner relative to its parent.
         *@param margin Margin in float.
         */
        void setTopMargin(float margin);

        /**
         * Query the bottom margin of owner relative to its parent.
         *@return Bottom margin in float.
         */
        float getBottomMargin()const;

        /**
         * Change the bottom margin of owner relative to its parent.
         *@param margin in float.
         */
        void setBottomMargin(float margin);

        /**
         * Query owner's content size.
         *@return Owner's content size.
         */
        const Size& getSize()const;

        /**
         * Change the content size of owner.
         *@param size Content size in @see `Size`.
         */
        void setSize(const Size& size);

        /**
         * Query whether percent width is enabled or not.
         *@return True if percent width is enabled, false, otherwise.
         */
        bool isPercentWidthEnabled()const;

        /**
         * Toggle enable percent width.
         *@param isUsed True if percent width is enabled, false otherwise.
         */
        void setPercentWidthEnabled(bool isUsed);

        /**
         * Query content size width of owner.
         *@return Content size width in float.
         */
        float getSizeWidth()const;

        /**
         * Change content size width of owner.
         *@param width Content size width in float.
         */
        void setSizeWidth(float width);

        /**
         * Query percent width of owner.
         *@return percent width in float.
         */
        float getPercentWidth()const;

        /**
         * Change percent width of owner.
         *@param percentWidth Percent Width in float.
         */
        void setPercentWidth(float percentWidth);

        /**
         * Query whether percent height is enabled or not.
         *@return True if percent height is enabled, false otherwise.
         */
        bool isPercentHeightEnabled()const;

        /**
         * Toggle enable percent height.
         *@param isUsed True if percent height is enabled, false otherwise.
         */
        void setPercentHeightEnabled(bool isUsed);

        /**
         * Query size height of owner.
         *@return Size height in float.
         */
        float getSizeHeight()const;

        /**
         * Change size height of owner.
         *@param height Size height in float.
         */
        void setSizeHeight(float height);

        /**
         * Query percent height of owner.         
         *@return Percent height in float.
         */
        float getPercentHeight()const;

        /**
         * Change percent height value of owner.
         *@param percentHeight Percent height in float.
         */
        void setPercentHeight(float percentHeight);

        /**
         * Query whether stretch width is enabled or not.
         *@return True if stretch width is enabled, false otherwise.
         */
        bool isStretchWidthEnabled()const;

        /**
         * Toggle enable stretch width.
         *@param isUsed True if enable stretch width, false otherwise.
         */
        void setStretchWidthEnabled(bool isUsed);

        /**
         * Query whether stretch height is enabled or not.
         *@return True if stretch height is enabled, false otherwise.
         */
        bool isStretchHeightEnabled()const;

        /**
         * Toggle enable stretch height.
         *@param isUsed True if stretch height is enabled, false otherwise.
         */
        void setStretchHeightEnabled(bool isUsed);
        
        /**
         * Toggle enable percent only.
         *@param enable True if percent only is enabled, false otherwise.
         */
        void setPercentOnlyEnabled(bool enable);

        /**
         * Toggle active enabled of LayoutComponent's owner.
         *@param enable True if active layout component, false otherwise.
         */
        void setActiveEnabled(bool enable);

        /**
         * Refresh layout of the owner.
         */
        void refreshLayout();

    protected:
        Node* getOwnerParent();
        void refreshHorizontalMargin();
        void refreshVerticalMargin();
    protected:
        HorizontalEdge  _horizontalEdge;
        VerticalEdge    _verticalEdge;

        float           _leftMargin;
        float           _rightMargin;
        float           _bottomMargin;
        float           _topMargin;

        bool            _usingPositionPercentX;
        float           _positionPercentX;
        bool            _usingPositionPercentY;
        float           _positionPercentY;

        bool            _usingStretchWidth;
        bool            _usingStretchHeight;

        float           _percentWidth;
        bool            _usingPercentWidth;
        
        float           _percentHeight;
        bool            _usingPercentHeight;

        bool            _actived;
        bool            _isPercentOnly;
    };
}

// end of ui group
/// @}
NS_CC_END
#endif /* defined(__cocos2d_libs__LayoutComponent__) */