TrackManiaControl/application/core/Maps/DirectoryBrowser.php

210 lines
5.7 KiB
PHP
Raw Normal View History

2014-06-29 23:51:01 +02:00
<?php
namespace ManiaControl\Maps;
2014-06-30 00:57:29 +02:00
use FML\Controls\Frame;
use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_BgsPlayerCard;
2014-06-29 23:51:01 +02:00
use FML\ManiaLink;
2014-06-30 00:57:29 +02:00
use FML\Script\Features\Paging;
2014-06-29 23:51:01 +02:00
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
2014-06-30 00:18:57 +02:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
2014-06-29 23:51:01 +02:00
use ManiaControl\Players\Player;
/**
* Maps Directory Browser
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2014-06-30 00:18:57 +02:00
class DirectoryBrowser implements ManialinkPageAnswerListener {
/*
* Constants
*/
2014-06-30 00:57:29 +02:00
const ACTION_SHOW = 'Maps.DirectoryBrowser.Show';
const ACTION_ADD_FILE = 'Maps.DirectoryBrowser.AddFile.';
const ACTION_ERASE_FILE = 'Maps.DirectoryBrowser.EraseFile';
const WIDGET_NAME = 'Maps.DirectoryBrowser.Widget';
2014-06-30 00:18:57 +02:00
2014-06-29 23:51:01 +02:00
/*
* Private properties
*/
private $maniaControl = null;
/**
* Create a new Directory Browser Instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-06-30 00:18:57 +02:00
// Register for ManiaLink Actions
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_SHOW, $this, 'handleActionShow');
}
/**
2014-06-30 00:57:29 +02:00
* Handle 'Show' Page Action
2014-06-30 00:18:57 +02:00
*
* @param array $actionCallback
* @param Player $player
*/
public function handleActionShow(array $actionCallback, Player $player) {
$this->showManiaLink($player);
2014-06-29 23:51:01 +02:00
}
/**
* Build and show the Browser ManiaLink to the given Player
*
* @param Player $player
*/
public function showManiaLink(Player $player) {
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
2014-06-30 00:57:29 +02:00
$script = $maniaLink->getScript();
$paging = new Paging();
$script->addFeature($paging);
$frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging);
$maniaLink->add($frame);
2014-06-29 23:51:01 +02:00
2014-06-30 00:57:29 +02:00
$mapFiles = $this->getFilteredMapFiles();
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
$index = 0;
$posY = $height / 2 - 10;
$pageFrame = null;
foreach ($mapFiles as $fileName) {
if ($index % 15 === 0) {
// New Page
$pageFrame = new Frame();
$frame->add($pageFrame);
$posY = $height / 2 - 10;
$paging->addPage($pageFrame);
}
// Map Frame
$mapFrame = new Frame();
$pageFrame->add($mapFrame);
$mapFrame->setPosition(0, $posY, 0.1);
if ($index % 2 !== 0) {
// Striped background line
$lineQuad = new Quad_BgsPlayerCard();
$mapFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
}
// File name Label
$nameLabel = new Label_Text();
$mapFrame->add($nameLabel);
$nameLabel->setX($width * -0.2);
$nameLabel->setText($fileName);
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)) {
// Add file button
$addButton = new Label_Button();
$mapFrame->add($addButton);
$addButton->setPosition($width / 2 - 9, 0, 0.2);
$addButton->setSize(3, 3);
$addButton->setTextSize(2);
$addButton->setText('Add');
$addButton->setAction(self::ACTION_ADD_FILE);
}
if ($this->maniaControl->authenticationManager->checkPermission($player, MapManager::SETTING_PERMISSION_REMOVE_MAP)) {
// Erase file button
$eraseButton = new Label_Button();
$mapFrame->add($eraseButton);
$eraseButton->setPosition($width / 2 - 9, 0, 0.2);
$eraseButton->setSize(3, 3);
$eraseButton->setTextSize(2);
$eraseButton->setText('Add');
$eraseButton->setAction(self::ACTION_ERASE_FILE);
}
$posY -= 4;
$index++;
}
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, self::WIDGET_NAME);
2014-06-29 23:51:01 +02:00
}
2014-06-30 00:18:57 +02:00
/**
* Get the list of not yet added Map files
*
* @return array|bool
*/
protected function getFilteredMapFiles() {
$mapFiles = $this->getMapFiles();
if (!is_array($mapFiles)) {
return false;
}
$filteredMapFiles = array();
foreach ($mapFiles as $mapFile) {
// TODO: filter already added maps
array_push($filteredMapFiles, $mapFile);
}
return $filteredMapFiles;
}
/**
* Get the list of Map files
*
* @return array|bool
*/
protected function getMapFiles() {
$mapsDirectory = $this->maniaControl->server->directory->getMapsFolder();
return $this->scanMapFiles($mapsDirectory);
}
/**
* Scan the given directory for Map files
*
* @param string $directory
* @return array|bool
*/
protected function scanMapFiles($directory) {
if (!is_readable($directory) || !is_dir($directory)) {
return false;
}
$mapFiles = array();
$dirFiles = scandir($directory);
foreach ($dirFiles as $fileName) {
2014-06-30 00:57:29 +02:00
if (substr($fileName, 0, 1) === '.') {
continue;
}
$fullFileName = $directory . $fileName;
if (is_dir($fullFileName)) {
$subDirectory = $fullFileName . DIRECTORY_SEPARATOR;
2014-06-30 00:18:57 +02:00
$subMapFiles = $this->scanMapFiles($subDirectory);
if (is_array($subMapFiles)) {
$mapFiles = array_merge($mapFiles, $subMapFiles);
}
} else {
if ($this->isMapFileName($fileName)) {
array_push($mapFiles, $fullFileName);
}
}
}
return $mapFiles;
}
/**
* Check if the given file name represents a Map file
*
* @param string $fileName
* @return bool
*/
protected function isMapFileName($fileName) {
$mapFileNameEnding = '.map.gbx';
$fileNameEnding = strtolower(substr($fileName, -strlen($mapFileNameEnding)));
return ($fileNameEnding === $mapFileNameEnding);
}
2014-06-29 23:51:01 +02:00
}