PolyUIElement.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Copyright (C) 2012 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. #include "PolyUIElement.h"
  20. #include "PolyRenderer.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyTexture.h"
  23. #include "PolyConfig.h"
  24. using namespace Polycode;
  25. UIElement *UIElement::globalFocusedChild = NULL;
  26. UILabel::UILabel(const String& text, int size, const String& fontName, int amode) : UIElement() {
  27. Config *conf = CoreServices::getInstance()->getConfig();
  28. label = new SceneLabel(text, size, fontName, amode);
  29. label->snapToPixels = true;
  30. color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
  31. addChild(label);
  32. bBox = label->bBox;
  33. }
  34. void UILabel::setText(const String& text) {
  35. label->setText(text);
  36. bBox = label->bBox;
  37. }
  38. String UILabel::getText() {
  39. return label->getText();
  40. }
  41. UILabel::~UILabel() {
  42. delete label;
  43. }
  44. Label *UILabel::getLabel() {
  45. return label->getLabel();
  46. }
  47. UIRect::UIRect(String fileName) : UIElement() {
  48. texture = NULL;
  49. loadTexture(fileName);
  50. if(texture) {
  51. initRect(texture->getWidth(), texture->getHeight());
  52. imageWidth = texture->getWidth();
  53. imageHeight = texture->getHeight();
  54. } else {
  55. initRect(1, 1);
  56. imageWidth = 0;
  57. imageHeight = 0;
  58. }
  59. }
  60. UIRect::UIRect(Number width, Number height) : UIElement() {
  61. texture = NULL;
  62. initRect(width, height);
  63. imageWidth = 0;
  64. imageHeight = 0;
  65. }
  66. void UIRect::setImageCoordinates(Number x, Number y, Number width, Number height) {
  67. Vertex *vertex;
  68. Number pixelSizeX = 1/imageWidth;
  69. Number pixelSizeY = 1/imageHeight;
  70. setWidth(width);
  71. setHeight(height);
  72. Number whalf = width/2.0f;
  73. Number hhalf = height/2.0f;
  74. Number xFloat = x * pixelSizeX;
  75. Number yFloat = (y * pixelSizeY);
  76. Number wFloat = width * pixelSizeX;
  77. Number hFloat = height * pixelSizeY;
  78. Polygon *imagePolygon = rectMesh->getPolygon(0);
  79. vertex = imagePolygon->getVertex(0);
  80. vertex->set(-whalf,-hhalf,0);
  81. vertex->setTexCoord(xFloat, (1.0-yFloat) - hFloat);
  82. vertex = imagePolygon->getVertex(1);
  83. vertex->set(-whalf+width,-hhalf,0);
  84. vertex->setTexCoord(xFloat + wFloat, (1.0-yFloat) - hFloat);
  85. vertex = imagePolygon->getVertex(2);
  86. vertex->set(-whalf+width,-hhalf+height,0);
  87. vertex->setTexCoord(xFloat + wFloat, 1.0-yFloat);
  88. vertex = imagePolygon->getVertex(3);
  89. vertex->set(-whalf,-hhalf+height,0);
  90. vertex->setTexCoord(xFloat, 1.0-yFloat);
  91. rectMesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  92. rectMesh->arrayDirtyMap[RenderDataArray::TEXCOORD_DATA_ARRAY] = true;
  93. rebuildTransformMatrix();
  94. matrixDirty = true;
  95. }
  96. Number UIRect::getImageWidth() const {
  97. return imageWidth;
  98. }
  99. Number UIRect::getImageHeight() const {
  100. return imageHeight;
  101. }
  102. void UIRect::initRect(Number width, Number height) {
  103. rectMesh = new Mesh(Mesh::QUAD_MESH);
  104. processInputEvents = true;
  105. setAnchorPoint(-1.0, -1.0, 0.0);
  106. setWidth(width);
  107. setHeight(height);
  108. Number whalf = width/2.0f;
  109. Number hhalf = height/2.0f;
  110. Polygon *poly = new Polygon();
  111. poly->addVertex(-whalf,-hhalf,0,0,0);
  112. poly->addVertex(-whalf+width,-hhalf,0, 1, 0);
  113. poly->addVertex(-whalf+width,-hhalf+height,0, 1, 1);
  114. poly->addVertex(-whalf,-hhalf+height,0,0,1);
  115. rectMesh->addPolygon(poly);
  116. }
  117. UIRect::~UIRect() {
  118. delete rectMesh;
  119. }
  120. void UIRect::loadTexture(String fileName) {
  121. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  122. texture = materialManager->createTextureFromFile(fileName, materialManager->clampDefault, false);
  123. }
  124. void UIRect::setTexture(Texture *texture) {
  125. this->texture = texture;
  126. }
  127. Texture *UIRect::getTexture() {
  128. return texture;
  129. }
  130. void UIRect::Render() {
  131. renderer->clearShader();
  132. renderer->setTexture(texture);
  133. renderer->pushDataArrayForMesh(rectMesh, RenderDataArray::VERTEX_DATA_ARRAY);
  134. renderer->pushDataArrayForMesh(rectMesh, RenderDataArray::TEXCOORD_DATA_ARRAY);
  135. renderer->drawArrays(Mesh::QUAD_MESH);
  136. }
  137. void UIRect::Resize(Number width, Number height) {
  138. setWidth(width);
  139. setHeight(height);
  140. Number whalf = width/2.0f;
  141. Number hhalf = height/2.0f;
  142. Polygon *polygon;
  143. Vertex *vertex;
  144. polygon = rectMesh->getPolygon(0);
  145. vertex = polygon->getVertex(0);
  146. vertex->set(-whalf,-hhalf,0);
  147. vertex = polygon->getVertex(1);
  148. vertex->set(-whalf+width,-hhalf,0);
  149. vertex = polygon->getVertex(2);
  150. vertex->set(-whalf+width,-hhalf+height,0);
  151. vertex = polygon->getVertex(3);
  152. vertex->set(-whalf,-hhalf+height,0);
  153. rectMesh->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  154. }
  155. UIImage::UIImage(String imagePath) : UIRect(imagePath) {
  156. }
  157. UIElement::UIElement() : Entity() {
  158. snapToPixels = true;
  159. setAnchorPoint(-1.0, -1.0, 0.0);
  160. processInputEvents = true;
  161. depthTest = false;
  162. hasFocus = false;
  163. focusable = false;
  164. focusParent = NULL;
  165. hasDragLimits = false;
  166. dragged = false;
  167. depthTest = false;
  168. depthWrite = false;
  169. }
  170. UIElement::UIElement(Number width, Number height) : Entity() {
  171. setAnchorPoint(-1.0, -1.0, 0.0);
  172. processInputEvents = true;
  173. focusParent = NULL;
  174. hasFocus = false;
  175. hasDragLimits = false;
  176. dragged = false;
  177. depthTest = false;
  178. depthWrite = false;
  179. setWidth(width);
  180. setHeight(height);
  181. }
  182. void UIElement::addChild(Entity *child) {
  183. UIElement* uiChild = dynamic_cast<UIElement*>(child);
  184. if(uiChild) {
  185. addFocusChild(uiChild);
  186. }
  187. Entity::addChild(child);
  188. }
  189. void UIElement::setDragLimits(Rectangle rect) {
  190. dragLimits.x = rect.x;
  191. dragLimits.y = rect.y;
  192. dragLimits.w = rect.w;
  193. dragLimits.h = rect.h;
  194. hasDragLimits = true;
  195. }
  196. void UIElement::clearDragLimits() {
  197. hasDragLimits = false;
  198. }
  199. bool UIElement::isDragged() {
  200. return dragged;
  201. }
  202. void UIElement::startDrag(Number xOffset, Number yOffset) {
  203. dragged = true;
  204. dragOffsetX = xOffset;
  205. dragOffsetY = yOffset;
  206. }
  207. void UIElement::stopDrag() {
  208. dragged = false;
  209. }
  210. bool UIElement::isFocusable() {
  211. return focusable;
  212. }
  213. MouseEventResult UIElement::onMouseMove(const Ray &ray, int timestamp) {
  214. if(dragged) {
  215. Vector3 localCoordinate = Vector3(ray.origin.x, ray.origin.y,0);
  216. if(parentEntity) {
  217. Matrix4 inverse = parentEntity->getConcatenatedMatrix().Inverse();
  218. localCoordinate = inverse * localCoordinate;
  219. }
  220. setPosition(localCoordinate.x-dragOffsetX,-localCoordinate.y-dragOffsetY);
  221. if(hasDragLimits) {
  222. if(position.x < dragLimits.x)
  223. position.x = dragLimits.x;
  224. if(position.x > dragLimits.x + dragLimits.w)
  225. position.x = dragLimits.x + dragLimits.w;
  226. if(position.y < dragLimits.y)
  227. position.y = dragLimits.y;
  228. if(position.y > dragLimits.y + dragLimits.h)
  229. position.y = dragLimits.y + dragLimits.h;
  230. }
  231. }
  232. return Entity::onMouseMove(ray, timestamp);
  233. }
  234. UIElement::~UIElement() {
  235. if(UIElement::globalFocusedChild == this) {
  236. UIElement::globalFocusedChild = NULL;
  237. }
  238. }
  239. void UIElement::focusNextChild() {
  240. int j = 0;
  241. bool hasFocusedChild = false;
  242. if(UIElement::globalFocusedChild) {
  243. for(int i=0; i < focusChildren.size(); i++) {
  244. if(focusChildren[i] == UIElement::globalFocusedChild) {
  245. j = i;
  246. hasFocusedChild = true;
  247. }
  248. }
  249. }
  250. if(!hasFocusedChild)
  251. return;
  252. for(int i=0; i < focusChildren.size(); i++) {
  253. if(focusChildren[j]->isFocusable() && focusChildren[j] != UIElement::globalFocusedChild) {
  254. focusChild(focusChildren[j]);
  255. return;
  256. }
  257. j++;
  258. if(j == focusChildren.size())
  259. j = 0;
  260. }
  261. }
  262. void UIElement::focusSelf() {
  263. if(UIElement::globalFocusedChild) {
  264. UIElement::globalFocusedChild->onLoseFocus();
  265. UIElement::globalFocusedChild->hasFocus = false;
  266. }
  267. UIElement::globalFocusedChild = this;
  268. onGainFocus();
  269. hasFocus = true;
  270. }
  271. void UIElement::focusChild(UIElement *child) {
  272. if(UIElement::globalFocusedChild) {
  273. UIElement::globalFocusedChild->onLoseFocus();
  274. UIElement::globalFocusedChild->hasFocus = false;
  275. }
  276. UIElement::globalFocusedChild = child;
  277. if(child) {
  278. UIElement::globalFocusedChild->onGainFocus();
  279. UIElement::globalFocusedChild->hasFocus = true;
  280. }
  281. }
  282. void UIElement::addFocusChild(UIElement *element) {
  283. element->setFocusParent(element);
  284. focusChildren.push_back(element);
  285. }
  286. void UIElement::setFocusParent(UIElement *element) {
  287. focusParent = element;
  288. }
  289. void UIElement::Resize(Number width, Number height) {
  290. setWidth(width);
  291. setHeight(height);
  292. dirtyMatrix(true);
  293. }