UILayoutParameter.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UILayoutParameter.h"
  22. #include "ui/UILayout.h"
  23. NS_CC_BEGIN
  24. namespace ui {
  25. const Margin Margin::ZERO = Margin(0,0,0,0);
  26. Margin::Margin(void) : left(0), top(0), right(0), bottom(0)
  27. {
  28. }
  29. Margin::Margin(float l, float t, float r, float b) : left(l), top(t), right(r), bottom(b)
  30. {
  31. }
  32. Margin::Margin(const Margin& other) : left(other.left), top(other.top), right(other.right), bottom(other.bottom)
  33. {
  34. }
  35. Margin& Margin::operator= (const Margin& other)
  36. {
  37. setMargin(other.left, other.top, other.right, other.bottom);
  38. return *this;
  39. }
  40. void Margin::setMargin(float l, float t, float r, float b)
  41. {
  42. left = l;
  43. top = t;
  44. right = r;
  45. bottom = b;
  46. }
  47. bool Margin::equals(const Margin &target) const
  48. {
  49. return (left == target.left && top == target.top && right == target.right && bottom == target.bottom);
  50. }
  51. LayoutParameter* LayoutParameter::create()
  52. {
  53. LayoutParameter* parameter = new (std::nothrow) LayoutParameter();
  54. if (parameter)
  55. {
  56. parameter->autorelease();
  57. return parameter;
  58. }
  59. CC_SAFE_DELETE(parameter);
  60. return nullptr;
  61. }
  62. void LayoutParameter::setMargin(const Margin &margin)
  63. {
  64. _margin = margin;
  65. }
  66. const Margin& LayoutParameter::getMargin() const
  67. {
  68. return _margin;
  69. }
  70. LayoutParameter::Type LayoutParameter::getLayoutType() const
  71. {
  72. return _layoutParameterType;
  73. }
  74. LayoutParameter* LayoutParameter::clone()
  75. {
  76. LayoutParameter* clonedParameter = createCloneInstance();
  77. clonedParameter->copyProperties(this);
  78. return clonedParameter;
  79. }
  80. LayoutParameter* LayoutParameter::createCloneInstance()
  81. {
  82. return LayoutParameter::create();
  83. }
  84. void LayoutParameter::copyProperties(LayoutParameter *model)
  85. {
  86. _margin = model->_margin;
  87. }
  88. LinearLayoutParameter* LinearLayoutParameter::create()
  89. {
  90. LinearLayoutParameter* parameter = new (std::nothrow) LinearLayoutParameter();
  91. if (parameter)
  92. {
  93. parameter->autorelease();
  94. return parameter;
  95. }
  96. CC_SAFE_DELETE(parameter);
  97. return nullptr;
  98. }
  99. void LinearLayoutParameter::setGravity(LinearGravity gravity)
  100. {
  101. _linearGravity = gravity;
  102. }
  103. LinearLayoutParameter::LinearGravity LinearLayoutParameter::getGravity() const
  104. {
  105. return _linearGravity;
  106. }
  107. LayoutParameter* LinearLayoutParameter::createCloneInstance()
  108. {
  109. return LinearLayoutParameter::create();
  110. }
  111. void LinearLayoutParameter::copyProperties(LayoutParameter *model)
  112. {
  113. LayoutParameter::copyProperties(model);
  114. LinearLayoutParameter* parameter = dynamic_cast<LinearLayoutParameter*>(model);
  115. if (parameter)
  116. {
  117. setGravity(parameter->_linearGravity);
  118. }
  119. }
  120. RelativeLayoutParameter* RelativeLayoutParameter::create()
  121. {
  122. RelativeLayoutParameter* parameter = new (std::nothrow) RelativeLayoutParameter();
  123. if (parameter)
  124. {
  125. parameter->autorelease();
  126. return parameter;
  127. }
  128. CC_SAFE_DELETE(parameter);
  129. return nullptr;
  130. }
  131. void RelativeLayoutParameter::setAlign(RelativeAlign align)
  132. {
  133. _relativeAlign = align;
  134. }
  135. RelativeLayoutParameter::RelativeAlign RelativeLayoutParameter::getAlign() const
  136. {
  137. return _relativeAlign;
  138. }
  139. void RelativeLayoutParameter::setRelativeToWidgetName(const std::string& name)
  140. {
  141. _relativeWidgetName = name;
  142. }
  143. const std::string& RelativeLayoutParameter::getRelativeToWidgetName() const
  144. {
  145. return _relativeWidgetName;
  146. }
  147. void RelativeLayoutParameter::setRelativeName(const std::string& name)
  148. {
  149. _relativeLayoutName = name;
  150. }
  151. const std::string& RelativeLayoutParameter::getRelativeName() const
  152. {
  153. return _relativeLayoutName;
  154. }
  155. LayoutParameter* RelativeLayoutParameter::createCloneInstance()
  156. {
  157. return RelativeLayoutParameter::create();
  158. }
  159. void RelativeLayoutParameter::copyProperties(LayoutParameter *model)
  160. {
  161. LayoutParameter::copyProperties(model);
  162. RelativeLayoutParameter* parameter = dynamic_cast<RelativeLayoutParameter*>(model);
  163. if (parameter)
  164. {
  165. setAlign(parameter->_relativeAlign);
  166. setRelativeName(parameter->_relativeLayoutName);
  167. setRelativeToWidgetName(parameter->_relativeWidgetName);
  168. }
  169. }
  170. }
  171. NS_CC_END