PolyScreen.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyScreenEntity.h"
  23. #include "PolyRenderer.h"
  24. #include "PolyInputEvent.h"
  25. #include "PolyCoreServices.h"
  26. #include <vector>
  27. #include <algorithm>
  28. #include "PolyScreenEvent.h"
  29. using namespace std;
  30. namespace Polycode {
  31. /**
  32. * 2D rendering base. The Screen is the container for all 2D rendering in Polycode. Screens are automatically rendered and need only be instantiated to immediately add themselves to the rendering pipeline. Each screen has a root entity.
  33. */
  34. class _PolyExport Screen : public EventDispatcher {
  35. public:
  36. /**
  37. * Default constructor.
  38. */
  39. Screen();
  40. ~Screen();
  41. /**
  42. * Adds a ScreenEntity to the 2d rendering pipeline.
  43. * @param newEntity Entity to add.
  44. * @return Returns the same entity for convenience.
  45. */
  46. ScreenEntity* addChild(ScreenEntity *newEntity);
  47. /**
  48. * Removes a ScreenEntity from the screen's render list.
  49. * @param entityToRemove Entity to remove.
  50. * @return Returns the same entity for convenience.
  51. */
  52. ScreenEntity* removeChild(ScreenEntity *entityToRemove);
  53. /**
  54. * Sets the screen's offset. You can also translate the root entity to do the same thing.
  55. * @param x New x offset.
  56. * @param y New y offset.
  57. */
  58. void setScreenOffset(Number x, Number y);
  59. /**
  60. * Returns the screen's offset.
  61. * @return The screen's offset as 2d vector.
  62. */
  63. Vector2 getScreenOffset();
  64. virtual void Shutdown();
  65. virtual void Update();
  66. /**
  67. * Returns the entity at specified point. This is a deprecated method which does not take rotation or scale into account. Please use the 2d physics and collision module for proper collision detection.
  68. */
  69. ScreenEntity *getEntityAt(Number x, Number y);
  70. void Render();
  71. void setRenderer(Renderer *renderer);
  72. /**
  73. * Changes the screen's coordinate system. By default, screens' dimensions are in pixels. To accommodate changing resolutions without changing the dimensions of a screen's content, you can call this method to make it use normalized coordinates.
  74. * @param newVal If true, the screen will use normalized coordinates, if false, it will use pixel coordinates.
  75. * @param yCoordinateSize The normalized size of the screen vertically. The horizontal size will be calculated based on the resolution.
  76. */
  77. void setNormalizedCoordinates(bool newVal, Number yCoordinateSize = 1.0f);
  78. /**
  79. * Sets the shader material to use for post processing on this screen.
  80. * @param shaderName Name of the shader material to use.
  81. */
  82. void setScreenShader(const String& shaderName);
  83. /**
  84. * Removes the current screen shader for this screen.
  85. */
  86. void clearScreenShader();
  87. void handleEvent(Event *event);
  88. int getHighestZIndex();
  89. /**
  90. * Sorts the screen's children based on their z index.
  91. */
  92. void sortChildren();
  93. static bool cmpZindex(const ScreenEntity *left, const ScreenEntity *right);
  94. void handleInputEvent(InputEvent *inputEvent);
  95. /**
  96. * Returns true if the screen has a shader applied to it.
  97. */
  98. bool hasFilterShader();
  99. void drawFilter();
  100. bool usesNormalizedCoordinates() { return useNormalizedCoordinates; }
  101. Number getYCoordinateSize() { return yCoordinateSize; }
  102. /**
  103. * Returns the root entity. The root entity can be used to transform the entire screen and change its color.
  104. * @return The root entity.
  105. */
  106. ScreenEntity *getRootEntity() { return rootEntity; }
  107. /**
  108. * If set to false, the screen will not be rendered or updated.
  109. */
  110. bool enabled;
  111. /**
  112. * Returns the local shader options for the camera post processing material.
  113. */
  114. vector<ShaderBinding*> getLocalShaderOptions() { return localShaderOptions; }
  115. /**
  116. * Returns the shader material applied to the camera.
  117. */
  118. Material *getScreenShaderMaterial() { return filterShaderMaterial; }
  119. protected:
  120. bool useNormalizedCoordinates;
  121. Number yCoordinateSize;
  122. ScreenEntity *rootEntity;
  123. Vector2 offset;
  124. Renderer *renderer;
  125. ScreenEntity *focusChild;
  126. vector <ScreenEntity*> children;
  127. Material *filterShaderMaterial;
  128. Texture *originalSceneTexture;
  129. Texture *zBufferSceneTexture;
  130. vector<ShaderBinding*> localShaderOptions;
  131. bool _hasFilterShader;
  132. };
  133. }