CmViewport.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmViewport.h"
  25. #include "CmException.h"
  26. #include "CmRenderTarget.h"
  27. #include "CmMath.h"
  28. #include "CmRenderSystem.h"
  29. namespace CamelotFramework
  30. {
  31. Viewport::Viewport()
  32. :mTarget(nullptr)
  33. , mRelLeft(0)
  34. , mRelTop(0)
  35. , mRelWidth(0)
  36. , mRelHeight(0)
  37. // Actual dimensions will update later
  38. , mZOrder(0)
  39. {
  40. // Calculate actual dimensions
  41. updateDimensions();
  42. }
  43. Viewport::Viewport(RenderTargetPtr target, float left, float top, float width, float height, int ZOrder)
  44. :mTarget(target)
  45. , mRelLeft(left)
  46. , mRelTop(top)
  47. , mRelWidth(width)
  48. , mRelHeight(height)
  49. // Actual dimensions will update later
  50. , mZOrder(ZOrder)
  51. {
  52. if(target != nullptr)
  53. {
  54. mTargetConn = target->onMovedOrResized.connect(boost::bind(&Viewport::targetResized, this, _1));
  55. }
  56. // Calculate actual dimensions
  57. updateDimensions();
  58. }
  59. Viewport::~Viewport()
  60. {
  61. mTargetConn.disconnect();
  62. }
  63. void Viewport::targetResized(RenderTarget* target)
  64. {
  65. updateDimensions();
  66. }
  67. void Viewport::updateDimensions(void)
  68. {
  69. if(mTarget != nullptr)
  70. {
  71. float height = (float) mTarget->getHeight();
  72. float width = (float) mTarget->getWidth();
  73. mDimensions.x = (int) (mRelLeft * width);
  74. mDimensions.y = (int) (mRelTop * height);
  75. mDimensions.width = (int) (mRelWidth * width);
  76. mDimensions.height = (int) (mRelHeight * height);
  77. }
  78. }
  79. int Viewport::getZOrder(void) const
  80. {
  81. return mZOrder;
  82. }
  83. RenderTargetPtr Viewport::getTarget(void) const
  84. {
  85. return mTarget;
  86. }
  87. float Viewport::getNormalizedLeft(void) const
  88. {
  89. return mRelLeft;
  90. }
  91. float Viewport::getNormalizedTop(void) const
  92. {
  93. return mRelTop;
  94. }
  95. float Viewport::getNormalizedWidth(void) const
  96. {
  97. return mRelWidth;
  98. }
  99. float Viewport::getNormalizedHeight(void) const
  100. {
  101. return mRelHeight;
  102. }
  103. int Viewport::getLeft(void) const
  104. {
  105. return mDimensions.x;
  106. }
  107. int Viewport::getTop(void) const
  108. {
  109. return mDimensions.y;
  110. }
  111. int Viewport::getWidth(void) const
  112. {
  113. return mDimensions.width;
  114. }
  115. int Viewport::getHeight(void) const
  116. {
  117. return mDimensions.height;
  118. }
  119. void Viewport::setDimensions(float left, float top, float width, float height)
  120. {
  121. mRelLeft = left;
  122. mRelTop = top;
  123. mRelWidth = width;
  124. mRelHeight = height;
  125. updateDimensions();
  126. }
  127. }