PolyUIElement.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. #include "PolyCoreInput.h"
  25. #include "PolyInputEvent.h"
  26. using namespace Polycode;
  27. UIElement *UIElement::globalFocusedChild = NULL;
  28. UIMultilineLabel::UIMultilineLabel(const String& text, int size, int spacing, const String& fontName, int amode) : UIElement() {
  29. labelSize = size;
  30. labelFontName = fontName;
  31. labelAAMode = amode;
  32. this->spacing = spacing;
  33. setText(text);
  34. }
  35. void UIMultilineLabel::setText(const String& text) {
  36. clearLabels();
  37. std::vector<String> lines = text.split("\n");
  38. linesCount = lines.size();
  39. Number lineSize = spacing;
  40. Number yPos = 0.0;
  41. for(int i=0; i < lines.size(); i++) {
  42. if(lines[i] == "") {
  43. yPos += lineSize + spacing;
  44. } else {
  45. UILabel *label = new UILabel(lines[i], labelSize, labelFontName, labelAAMode);
  46. lineSize = label->getHeight();
  47. label->setPositionY(yPos);
  48. yPos += label->getHeight() + spacing;
  49. addChild(label);
  50. labels.push_back(label);
  51. }
  52. }
  53. }
  54. String UIMultilineLabel::getText() {
  55. String text;
  56. for(int i=0; i < labels.size(); i++) {
  57. if(i != 0) {
  58. text += "\n";
  59. }
  60. text += labels[i]->getText();
  61. }
  62. return text;
  63. }
  64. void UIMultilineLabel::setColor(Color color) {
  65. for (int i = 0; i < labels.size(); i++) {
  66. labels[i]->color.setColor(&color);
  67. }
  68. }
  69. void UIMultilineLabel::setColorInt(int r, int g, int b, int a) {
  70. for (int i = 0; i < labels.size(); i++) {
  71. labels[i]->color.setColorRGBA(r, g, b, a);
  72. }
  73. }
  74. void UIMultilineLabel::setColor(Number r, Number g, Number b, Number a) {
  75. for (int i = 0; i < labels.size(); i++) {
  76. labels[i]->color.setColor(r, g, b, a);
  77. }
  78. }
  79. void UIMultilineLabel::clearLabels() {
  80. for(int i=0; i < labels.size(); i++) {
  81. removeChild(labels[i]);
  82. delete labels[i];
  83. }
  84. labels.clear();
  85. }
  86. Number UIMultilineLabel::getWidth(){
  87. Number maxWidth = 0;
  88. for (int i = 0; i < labels.size(); i++) {
  89. if (labels[i]->getWidth() > maxWidth){
  90. maxWidth = labels[i]->getWidth();
  91. }
  92. }
  93. return maxWidth;
  94. }
  95. Number UIMultilineLabel::getHeight(){
  96. Number retHeight = 0;
  97. for (int i = 0; i < labels.size(); i++) {
  98. retHeight += labels[i]->getHeight() + spacing;
  99. }
  100. retHeight += (linesCount - labels.size()) * (labelSize + spacing);
  101. return retHeight;
  102. }
  103. UIMultilineLabel::~UIMultilineLabel() {
  104. if(!ownsChildren) {
  105. clearLabels();
  106. }
  107. }
  108. UILabel::UILabel(const String& text, int size, const String& fontName, int amode) : UIElement() {
  109. Config *conf = CoreServices::getInstance()->getConfig();
  110. label = new SceneLabel(text, size, fontName, amode);
  111. label->setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  112. label->positionAtBaseline = true;
  113. label->setAnchorPoint(-1.0, -1.0, 0.0);
  114. label->snapToPixels = true;
  115. color.setColorHexFromString(conf->getStringValue("Polycode", "uiDefaultFontColor"));
  116. addChild(label);
  117. setLocalBoundingBox(label->getLocalBoundingBox());
  118. }
  119. void UILabel::setText(const String& text) {
  120. label->setText(text);
  121. setLocalBoundingBox(label->getLocalBoundingBox());
  122. }
  123. String UILabel::getText() {
  124. return label->getText();
  125. }
  126. UILabel::~UILabel() {
  127. if(!ownsChildren) {
  128. delete label;
  129. }
  130. }
  131. Label *UILabel::getLabel() {
  132. return label->getLabel();
  133. }
  134. UIRect::UIRect(String fileName, Number width, Number height) : UIElement() {
  135. texture = NULL;
  136. loadTexture(fileName);
  137. initRect(width, height);
  138. imageWidth = width;
  139. imageHeight = height;
  140. }
  141. UIRect::UIRect(String fileName) : UIElement() {
  142. texture = NULL;
  143. loadTexture(fileName);
  144. if(texture) {
  145. initRect(texture->getWidth(), texture->getHeight());
  146. imageWidth = texture->getWidth();
  147. imageHeight = texture->getHeight();
  148. } else {
  149. initRect(1, 1);
  150. imageWidth = 0;
  151. imageHeight = 0;
  152. }
  153. setWidth(imageWidth);
  154. setHeight(imageHeight);
  155. }
  156. UIRect::UIRect(Number width, Number height) : UIElement() {
  157. texture = NULL;
  158. initRect(width, height);
  159. imageWidth = 0;
  160. imageHeight = 0;
  161. }
  162. void UIRect::setImageCoordinates(Number x, Number y, Number width, Number height, Number imageScale) {
  163. Number pixelSizeX = 1/imageWidth;
  164. Number pixelSizeY = 1/imageHeight;
  165. setWidth(width);
  166. setHeight(height);
  167. Number whalf = width/2.0f;
  168. Number hhalf = height/2.0f;
  169. Number xFloat = x * pixelSizeX * imageScale;
  170. Number yFloat = (y * pixelSizeY) * imageScale;
  171. Number wFloat = width * pixelSizeX * imageScale;
  172. Number hFloat = height * pixelSizeY * imageScale;
  173. rectMesh->vertexPositionArray.data.clear();
  174. rectMesh->vertexTexCoordArray.data.clear();
  175. rectMesh->addVertex(-whalf,-hhalf,0);
  176. rectMesh->addTexCoord(xFloat, (1.0-yFloat) - hFloat);
  177. rectMesh->addVertex(-whalf+width,-hhalf,0);
  178. rectMesh->addTexCoord(xFloat + wFloat, (1.0-yFloat) - hFloat);
  179. rectMesh->addVertex(-whalf+width,-hhalf+height,0);
  180. rectMesh->addTexCoord(xFloat + wFloat, 1.0-yFloat);
  181. rectMesh->addVertex(-whalf,-hhalf+height,0);
  182. rectMesh->addTexCoord(xFloat, 1.0-yFloat);
  183. rebuildTransformMatrix();
  184. matrixDirty = true;
  185. }
  186. Number UIRect::getImageWidth() const {
  187. return imageWidth;
  188. }
  189. Number UIRect::getImageHeight() const {
  190. return imageHeight;
  191. }
  192. void UIRect::initRect(Number width, Number height) {
  193. rectMesh = new Mesh(Mesh::QUAD_MESH);
  194. processInputEvents = true;
  195. setAnchorPoint(-1.0, -1.0, 0.0);
  196. setWidth(width);
  197. setHeight(height);
  198. Number whalf = width/2.0f;
  199. Number hhalf = height/2.0f;
  200. rectMesh->addVertexWithUV(-whalf,-hhalf,0,0,0);
  201. rectMesh->addVertexWithUV(-whalf+width,-hhalf,0, 1, 0);
  202. rectMesh->addVertexWithUV(-whalf+width,-hhalf+height,0, 1, 1);
  203. rectMesh->addVertexWithUV(-whalf,-hhalf+height,0,0,1);
  204. }
  205. UIRect::~UIRect() {
  206. delete rectMesh;
  207. }
  208. void UIRect::loadTexture(String fileName) {
  209. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  210. texture = materialManager->createTextureFromFile(fileName, materialManager->clampDefault, false);
  211. }
  212. void UIRect::setTexture(Texture *texture) {
  213. this->texture = texture;
  214. }
  215. Texture *UIRect::getTexture() {
  216. return texture;
  217. }
  218. void UIRect::Render() {
  219. renderer->clearShader();
  220. renderer->setTexture(texture);
  221. renderer->pushRenderDataArray(&rectMesh->vertexPositionArray);
  222. renderer->pushRenderDataArray(&rectMesh->vertexTexCoordArray);
  223. renderer->drawArrays(Mesh::QUAD_MESH, NULL);
  224. }
  225. void UIRect::Resize(Number width, Number height) {
  226. setWidth(width);
  227. setHeight(height);
  228. Number whalf = width/2.0f;
  229. Number hhalf = height/2.0f;
  230. rectMesh->vertexPositionArray.data.clear();
  231. rectMesh->addVertex(-whalf,-hhalf,0);
  232. rectMesh->addVertex(-whalf+width,-hhalf,0);
  233. rectMesh->addVertex(-whalf+width,-hhalf+height,0);
  234. rectMesh->addVertex(-whalf,-hhalf+height,0);
  235. }
  236. UIImage::UIImage(String imagePath, int width, int height) : UIRect(imagePath, width, height) {
  237. setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  238. }
  239. UIImage::UIImage(String imagePath) : UIRect(imagePath) {
  240. setBlendingMode(Renderer::BLEND_MODE_NORMAL);
  241. }
  242. UIElement::UIElement() : Entity() {
  243. snapToPixels = true;
  244. setAnchorPoint(-1.0, -1.0, 0.0);
  245. processInputEvents = true;
  246. depthTest = false;
  247. hasFocus = false;
  248. focusable = false;
  249. focusParent = NULL;
  250. hasDragLimits = false;
  251. dragged = false;
  252. depthTest = false;
  253. depthWrite = false;
  254. Services()->getInput()->addEventListenerUnique(this, InputEvent::EVENT_KEYDOWN);
  255. }
  256. UIElement::UIElement(Number width, Number height) : Entity() {
  257. setAnchorPoint(-1.0, -1.0, 0.0);
  258. processInputEvents = true;
  259. focusParent = NULL;
  260. hasFocus = false;
  261. hasDragLimits = false;
  262. focusable = false;
  263. dragged = false;
  264. depthTest = false;
  265. depthWrite = false;
  266. setWidth(width);
  267. setHeight(height);
  268. Services()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
  269. }
  270. void UIElement::handleEvent(Event *event) {
  271. if(event->getDispatcher() == Services()->getInput() && event->getEventCode() == InputEvent::EVENT_KEYDOWN) {
  272. if(hasFocus && focusParent) {
  273. InputEvent *inputEvent = (InputEvent*) event;
  274. if(inputEvent->key == KEY_TAB) {
  275. if(Services()->getInput()->getKeyState(KEY_RSHIFT) || Services()->getInput()->getKeyState(KEY_LSHIFT)) {
  276. focusParent->focusPreviousChild();
  277. } else {
  278. focusParent->focusNextChild();
  279. }
  280. inputEvent->cancelEvent();
  281. }
  282. }
  283. }
  284. }
  285. void UIElement::setDragLimits(Rectangle rect) {
  286. dragLimits.x = rect.x;
  287. dragLimits.y = rect.y;
  288. dragLimits.w = rect.w;
  289. dragLimits.h = rect.h;
  290. hasDragLimits = true;
  291. }
  292. void UIElement::clearDragLimits() {
  293. hasDragLimits = false;
  294. }
  295. bool UIElement::isDragged() {
  296. return dragged;
  297. }
  298. void UIElement::startDrag(Number xOffset, Number yOffset) {
  299. dragged = true;
  300. dragOffsetX = xOffset;
  301. dragOffsetY = yOffset;
  302. }
  303. void UIElement::stopDrag() {
  304. dragged = false;
  305. }
  306. bool UIElement::isFocusable() {
  307. return focusable;
  308. }
  309. MouseEventResult UIElement::onMouseMove(const Ray &ray, int timestamp) {
  310. if(dragged) {
  311. Vector3 localCoordinate = Vector3(ray.origin.x, ray.origin.y,0);
  312. if(parentEntity) {
  313. Matrix4 inverse = parentEntity->getConcatenatedMatrix().Inverse();
  314. localCoordinate = inverse * localCoordinate;
  315. }
  316. setPosition(localCoordinate.x-dragOffsetX,-localCoordinate.y-dragOffsetY);
  317. if(hasDragLimits) {
  318. if(position.x < dragLimits.x)
  319. position.x = dragLimits.x;
  320. if(position.x > dragLimits.x + dragLimits.w)
  321. position.x = dragLimits.x + dragLimits.w;
  322. if(position.y < dragLimits.y)
  323. position.y = dragLimits.y;
  324. if(position.y > dragLimits.y + dragLimits.h)
  325. position.y = dragLimits.y + dragLimits.h;
  326. }
  327. }
  328. return Entity::onMouseMove(ray, timestamp);
  329. }
  330. UIElement::~UIElement() {
  331. Services()->getInput()->removeAllHandlersForListener(this);
  332. if(focusParent) {
  333. focusParent->unregisterFocusChild(this);
  334. }
  335. if(UIElement::globalFocusedChild == this) {
  336. UIElement::globalFocusedChild = NULL;
  337. }
  338. }
  339. void UIElement::focusPreviousChild() {
  340. int j = 0;
  341. bool hasFocusedChild = false;
  342. if(UIElement::globalFocusedChild) {
  343. for(int i=0; i < focusChildren.size(); i++) {
  344. if(focusChildren[i] == UIElement::globalFocusedChild) {
  345. j = i;
  346. hasFocusedChild = true;
  347. }
  348. }
  349. }
  350. if(!hasFocusedChild) {
  351. return;
  352. }
  353. for(int i=0; i < focusChildren.size(); i++) {
  354. if(focusChildren[j]->isFocusable() && focusChildren[j] != UIElement::globalFocusedChild && focusChildren[j]->enabled && focusChildren[j]->visible) {
  355. focusChild(focusChildren[j]);
  356. return;
  357. }
  358. j--;
  359. if(j == -1) {
  360. j = focusChildren.size()-1;
  361. }
  362. }
  363. }
  364. void UIElement::focusNextChild() {
  365. int j = 0;
  366. bool hasFocusedChild = false;
  367. if(UIElement::globalFocusedChild) {
  368. for(int i=0; i < focusChildren.size(); i++) {
  369. if(focusChildren[i] == UIElement::globalFocusedChild) {
  370. j = i;
  371. hasFocusedChild = true;
  372. }
  373. }
  374. }
  375. if(!hasFocusedChild) {
  376. return;
  377. }
  378. for(int i=0; i < focusChildren.size(); i++) {
  379. if(focusChildren[j]->isFocusable() && focusChildren[j] != UIElement::globalFocusedChild && focusChildren[j]->enabled && focusChildren[j]->visible) {
  380. focusChild(focusChildren[j]);
  381. return;
  382. }
  383. j++;
  384. if(j == focusChildren.size()) {
  385. j = 0;
  386. }
  387. }
  388. }
  389. void UIElement::focusSelf() {
  390. if(UIElement::globalFocusedChild) {
  391. UIElement::globalFocusedChild->onLoseFocus();
  392. UIElement::globalFocusedChild->hasFocus = false;
  393. }
  394. UIElement::globalFocusedChild = this;
  395. onGainFocus();
  396. hasFocus = true;
  397. }
  398. void UIElement::focusChild(UIElement *child) {
  399. if(UIElement::globalFocusedChild) {
  400. UIElement::globalFocusedChild->onLoseFocus();
  401. UIElement::globalFocusedChild->hasFocus = false;
  402. }
  403. UIElement::globalFocusedChild = child;
  404. if(child) {
  405. UIElement::globalFocusedChild->onGainFocus();
  406. UIElement::globalFocusedChild->hasFocus = true;
  407. }
  408. }
  409. void UIElement::registerFocusChild(UIElement *element) {
  410. if(element->isFocusable()) {
  411. element->setFocusParent(this);
  412. focusChildren.push_back(element);
  413. }
  414. }
  415. void UIElement::unregisterFocusChild(UIElement *element) {
  416. for(int i=0; i < focusChildren.size(); i++) {
  417. if(focusChildren[i] == element) {
  418. focusChildren.erase(focusChildren.begin()+i);
  419. return;
  420. }
  421. }
  422. }
  423. void UIElement::addFocusChild(UIElement *element) {
  424. registerFocusChild(element);
  425. addChild(element);
  426. }
  427. void UIElement::setFocusParent(UIElement *element) {
  428. focusParent = element;
  429. }
  430. void UIElement::Resize(Number width, Number height) {
  431. setWidth(width);
  432. setHeight(height);
  433. dirtyMatrix(true);
  434. }