PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/ManiaLivePlugins/MLEPP/ForceMods/Gui/Windows/EnvironmentWindow.php

http://mlepp.googlecode.com/
PHP | 250 lines | 114 code | 41 blank | 95 comment | 13 complexity | d2198d8663f26f6a504a81b449b9485c MD5 | raw file
  1. <?php
  2. /**
  3. * MLEPP - ManiaLive Extending Plugin Pack
  4. *
  5. * -- MLEPP Plugin --
  6. * @name ForceMods
  7. * @date $Date: 2011-07-09 19:18:31 +0200 (za, 09 jul 2011) $
  8. * @version $Revision: 858 $
  9. * @website mlepp.trackmania.nl
  10. * @package MLEPP
  11. *
  12. * @author Klaus "schmidi" Schmidhuber <schmidi.tm@gmail.com>
  13. * @copyright 2010 - 2011
  14. *
  15. * ---------------------------------------------------------------------
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. * ---------------------------------------------------------------------
  29. * You are allowed to change things of use this in other projects, as
  30. * long as you leave the information at the top (name, date, version,
  31. * website, package, author, copyright) and publish the code under
  32. * the GNU General Public License version 3.
  33. * ---------------------------------------------------------------------
  34. */
  35. namespace ManiaLivePlugins\MLEPP\ForceMods\Gui\Windows;
  36. use ManiaLivePlugins\MLEPP\ForceMods\Gui\Elements\Button;
  37. use ManiaLivePlugins\MLEPP\ForceMods\Structures\FModList;
  38. use ManiaLib\Gui\Elements\Label;
  39. use ManiaLive\Gui\Windowing\Controls\Frame;
  40. use ManiaLive\Gui\Windowing\Controls\PageNavigator;
  41. class EnvironmentWindow extends \ManiaLive\Gui\Windowing\ManagedWindow {
  42. protected $currentPage = 1;
  43. protected $lastPage = 3;
  44. protected $name = '';
  45. protected $modList;
  46. protected $navigator;
  47. protected $frame;
  48. /**
  49. * @fn initializeComponents()
  50. * @brief Function called on initialisation.
  51. *
  52. * @return void
  53. */
  54. function initializeComponents() {
  55. $this->setSize(72, 72);
  56. $this->modList = new FModList();
  57. // frame
  58. $this->frame = new Frame($this->getSizeX() - 4, $this->getSizeY() - 18);
  59. $this->frame->setPosition(2, 6);
  60. $this->addComponent($this->frame);
  61. // navigator
  62. $this->navigator = new PageNavigator();
  63. $this->addComponent($this->navigator);
  64. }
  65. /**
  66. * @fn onDraw()
  67. * @brief Function called on draw.
  68. *
  69. * @return void
  70. */
  71. function onDraw() {
  72. $itemCount = 10;
  73. $count = $this->modList->countMods();
  74. $this->setTitle($this->name.' ('.$count.')');
  75. $this->frame->clearComponents();
  76. $count = $this->modList->countMods();
  77. $this->lastPage = ceil($count / $itemCount);
  78. $start = ($this->currentPage - 1) * $itemCount;
  79. $end = Min($start + $itemCount - 1, $count - 1);
  80. for($i = 0; $start <= $end; $start++, $i++) {
  81. // label name
  82. $label = new Label(40, 20);
  83. $label->setPosition(10, 23);
  84. $label->setHAlign('right');
  85. $label->setText($this->modList->getName($start));
  86. $this->frame->addComponent($label);
  87. // on button
  88. $button = new Button(5, 5, 'On', array($this, 'onClick'), $start, !$this->modList->isEnabled($start));
  89. $button->setPosition(20, 23);
  90. $this->frame->addComponent($button);
  91. // off button
  92. $button = new Button(5, 5, 'Off', array($this, 'onClick'), $start, $this->modList->isEnabled($start));
  93. $button->setPosition(30, 23);
  94. $this->frame->addComponent($button);
  95. }
  96. // navigator
  97. $this->navigator->setPositionX($this->getSizeX() / 2);
  98. $this->navigator->setPositionY($this->getSizeY() - 4);
  99. $this->navigator->setCurrentPage($this->currentPage);
  100. $this->navigator->setPageNumber($this->lastPage);
  101. $this->navigator->showText(true);
  102. $this->navigator->showLast(true);
  103. if($this->currentPage < $this->lastPage) {
  104. $this->navigator->arrowNext->setAction($this->callback('nextPage'));
  105. $this->navigator->arrowLast->setAction($this->callback('lastPage'));
  106. }
  107. else {
  108. $this->navigator->arrowNext->setAction(NULL);
  109. $this->navigator->arrowLast->setAction(NULL);
  110. }
  111. if($this->currentPage > 1) {
  112. $this->navigator->arrowPrev->setAction($this->callback('prevPage'));
  113. $this->navigator->arrowFirst->setAction($this->callback('firstPage'));
  114. }
  115. else {
  116. $this->navigator->arrowPrev->setAction(NULL);
  117. $this->navigator->arrowFirst->setAction(NULL);
  118. }
  119. }
  120. /**
  121. * @fn setEnvironment()
  122. * @brief Function to set current environment.
  123. *
  124. * @param mixed $name
  125. * @param mixed $modList
  126. * @return void
  127. */
  128. function setEnvironment($name, &$modList) {
  129. if(get_class($modList) == 'ManiaLivePlugins\MLEPP\ForceMods\Structures\FModList') {
  130. $this->name = $name;
  131. $this->modList = $modList;
  132. $this->currentPage = 1;
  133. }
  134. }
  135. /**
  136. * @fn nextPage()
  137. * @brief Function called on nextPage click.
  138. *
  139. * @param mixed $login
  140. * @return void
  141. */
  142. function nextPage($login = NULL) {
  143. $this->currentPage = Min(++$this->currentPage, $this->lastPage);
  144. if($login) {
  145. $this->show();
  146. }
  147. }
  148. /**
  149. * @fn prevPage()
  150. * @brief Function called on prevPage click.
  151. *
  152. * @param mixed $login
  153. * @return void
  154. */
  155. function prevPage($login = NULL) {
  156. $this->currentPage = Max(--$this->currentPage, 0);
  157. if($login) {
  158. $this->show();
  159. }
  160. }
  161. /**
  162. * @fn firstPage()
  163. * @brief Function called on firstPage click.
  164. *
  165. * @param mixed $login
  166. * @return void
  167. */
  168. function firstPage($login = NULL) {
  169. $this->currentPage = 0;
  170. if($login) {
  171. $this->show();
  172. }
  173. }
  174. /**
  175. * @fn lastPage()
  176. * @brief Function called on lastPage click.
  177. *
  178. * @param mixed $login
  179. * @return void
  180. */
  181. function lastPage($login = NULL) {
  182. $this->currentPage = $this->lastPage;
  183. if($login) {
  184. $this->show();
  185. }
  186. }
  187. /**
  188. * @fn onClick()
  189. * @brief Function called on click.
  190. *
  191. * @param mixed $login
  192. * @param mixed $index
  193. * @return void
  194. */
  195. function onClick($login, $index) {
  196. if($this->modList->isEnabled($index)) {
  197. $this->modList->disable($index);
  198. }
  199. else {
  200. $this->modList->enable($index);
  201. }
  202. $this->show();
  203. }
  204. function destroy()
  205. {
  206. parent::destroy();
  207. }
  208. }
  209. ?>