UI/UI.js

  1. /**
  2. * Менеджер интерфейса.
  3. * Создает и обновляет позиции всех элементов интерфейса: кнопки, курсор, таймер и т.д.
  4. * Создает менеджер слоев интерфейса {@link UI#layers} и добавляет элементы интерфейса,
  5. * а также существующие группы в него.
  6. * @class
  7. */
  8. var UI = function(){
  9. this.actionButtons = null;
  10. this.cornerButtons = null;
  11. this.cursor = null;
  12. this.background = null;
  13. this.logo = null;
  14. this.rope = null;
  15. this.feed = null;
  16. this.eventFeed = null;
  17. this.announcer = null;
  18. this.modalManager = null;
  19. this.menus = null;
  20. /**
  21. * Заранее заданные цвета для использования в игровых элементах.
  22. * @type {Object}
  23. */
  24. this.colors = {
  25. orange: 0xFF8300,
  26. green: 0x68C655,
  27. red: 0xC93F3F,
  28. white: 0xFeFeFe,
  29. lightGray: 0xC6C6C6,
  30. lightBlue: 0x0072C4,
  31. menu: {
  32. grey: {
  33. outer: '#999999',
  34. inner: '#FFFFFF',
  35. background: '#EEEEEE'
  36. },
  37. orange: {
  38. outer: '#CD5D12',
  39. inner: '#FA8132',
  40. background: '#E86A17'
  41. },
  42. green: {
  43. outer: '#5FB13A',
  44. inner: '#88E060',
  45. background: '#73CD4B'
  46. },
  47. yellow: {
  48. outer: '#CDA400',
  49. inner: '#FFD948',
  50. background: '#FFCC00'
  51. },
  52. blue: {
  53. outer: '#1989B8',
  54. inner: '#35BAF3',
  55. background: '#1EA7E1'
  56. },
  57. red: {
  58. outer: '#E00505',
  59. inner: '#FF2F2F',
  60. background: '#F91E1E'
  61. }
  62. }
  63. };
  64. /**
  65. * Менеджер "слоев" элементов интерфейса.
  66. * @type {UI.Layers}
  67. */
  68. this.layers = new UI.Layers();
  69. };
  70. /**
  71. * Инициализирует интерфейс, создавая все элементы интерфейса
  72. * и добавляя их в менеджер слоев.
  73. */
  74. UI.prototype.initialize = function(){
  75. /**
  76. * Курсор.
  77. * @type {UI.Cursor}
  78. */
  79. this.cursor = new UI.Cursor('cursor_orange');
  80. /**
  81. * Фон.
  82. * @type {UI.Background}
  83. */
  84. this.background = new UI.Background();
  85. /**
  86. * Лого игры.
  87. * @type {Phaser.Image}
  88. */
  89. this.logo = new UI.Logo(function(){
  90. return {x: game.screenWidth/2, y: game.screenHeight/2 - 225};
  91. }, 0.75, 'logo');
  92. /**
  93. * Таймер хода.
  94. * @type {UI.Rope}
  95. */
  96. this.rope = new UI.Rope();
  97. /**
  98. * Фид системных сообщений.
  99. * @type {MessageFeed}
  100. */
  101. this.feed = new MessageFeed(game);
  102. /**
  103. * Фид важных сообщений.
  104. * @type {MessageFeed.AnnounceFeed}
  105. */
  106. this.announcer = new MessageFeed.AnnounceFeed(game);
  107. /**
  108. * Фид событий.
  109. * @type {MessageFeed.EventFeed}
  110. */
  111. this.eventFeed = new MessageFeed.EventFeed(game);
  112. this.eventFeed.zIndexBelowCards = 3;
  113. this.eventFeed.zIndexAboveCards = 7;
  114. /**
  115. * Менеджер модальных меню.
  116. * @type {UI.ModalManager}
  117. */
  118. this.modalManager = new UI.ModalManager();
  119. this.popupManager = new UI.PopupManager();
  120. this.credits = new UI.Credits(creditsText, game.state.change.bind(game.state, 'menu'));
  121. this._createButtons();
  122. this.menus = this._createMenus();
  123. this.layers.addExistingLayers([
  124. [this.background, 0],
  125. // this.actionButtons, 1
  126. [fieldManager, 2],
  127. [this.rope, 3],
  128. // this.eventFeed, 3 (zIndexBelowCards)
  129. [cardManager, 4],
  130. [cardEmitter, 5],
  131. // this.menus.main, 6
  132. // this.menus.broswer, 6
  133. [this.logo, 6],
  134. [this.credits, 6],
  135. [this.feed, 7],
  136. [this.eventFeed, this.eventFeed.zIndexAboveCards],
  137. [this.announcer, 8],
  138. // игровые меню, -6
  139. [this.modalManager, -5],
  140. // модальные меню, -4
  141. // this.cornerButtons, -3
  142. [this.popupManager, -2],
  143. [this.cursor, -1]
  144. ]);
  145. this.layers.hideLayer(this.actionButtons, true);
  146. this.layers.positionLayers();
  147. this.activateHotkeys();
  148. };
  149. /** Обновляет позиции всех элементов UI. */
  150. UI.prototype.updatePositions = function(){
  151. this.layers.positionElements();
  152. };
  153. UI.prototype.applySkin = function(){
  154. ui.background.setTexture(skinManager.skin.background);
  155. ui.background.vignette.visible = skinManager.skin.uiVignette;
  156. ui.menus.more_options.applyCheckbox('vignette', skinManager.skin.uiVignette);
  157. };
  158. /** Возвращает phaser пиксель для превращения в текстуру. */
  159. UI.prototype.newPixel = function(){
  160. var pixel = game.make.graphics(0, 0);
  161. pixel.beginFill(ui.colors.white);
  162. pixel.drawRect(0, 0, 1, 1);
  163. pixel.endFill();
  164. return pixel;
  165. };
  166. UI.prototype.setDebugButtonText = function(name, text, on){
  167. ui.menus.debug.getByName(name).label.setText(text + ': ' + (on ? 'on' : 'off'), true);
  168. };
  169. UI.prototype.activateHotkeys = function(){
  170. var esc = game.input.keyboard.addKey(Phaser.Keyboard.ESC);
  171. esc.onDown.add(function(){
  172. if(this.modalManager.openModals.length){
  173. this.modalManager.closeModal();
  174. }
  175. else if(game.state.currentSync == 'credits'){
  176. game.state.change('menu');
  177. }
  178. else {
  179. this.modalManager.openModal('options');
  180. }
  181. }, this);
  182. function sendButtonAction(){
  183. var button = actionHandler.actionButton;
  184. if(game.state.currentSync == 'play' && button && button.serverAction){
  185. connection.server.sendButtonAction(button.serverAction);
  186. }
  187. }
  188. var enter = game.input.keyboard.addKey(Phaser.Keyboard.ENTER);
  189. enter.onDown.add(sendButtonAction, this);
  190. var space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
  191. space.onDown.add(sendButtonAction, this);
  192. };
  193. UI.prototype.toggleDebugButtons = function(){
  194. this.debugButtons.visible = !this.debugButtons.visible;
  195. this.debugButtons.forEach(function(b){
  196. if(this.debugButtons.visible){
  197. b.show();
  198. }
  199. else{
  200. b.hide();
  201. }
  202. }, this);
  203. gameOptions.set('debug_buttons', this.debugButtons.visible);
  204. gameOptions.save();
  205. };
  206. UI.prototype.openRules = function(){
  207. if(this.menus.rules){
  208. this.menus.rules.proxyOpen();
  209. }
  210. else{
  211. var message = this.feed.newMessage('Loading rules...');
  212. this.menus.rules = new Rules({message: message});
  213. this.layers.positionLayers();
  214. }
  215. };
  216. //@include:UIMenus
  217. //@include:UIButtons
  218. //@include:Menu
  219. //@include:PopupManager
  220. //@include:Layers
  221. //@include:Background
  222. //@include:Button
  223. //@include:Stepper
  224. //@include:Text
  225. //@include:Checkbox
  226. //@include:InputField
  227. //@include:Image
  228. //@include:Rope
  229. //@include:Logo
  230. //@include:Cursor
  231. //@include:ModalManager
  232. //@include:Credits