Started to restructure Widgets of LocalRecords and Dedimania Plugins
This commit is contained in:
		
							
								
								
									
										167
									
								
								plugins/MCTeam/Common/RecordWidget.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								plugins/MCTeam/Common/RecordWidget.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,167 @@ | |||||||
|  | <?php | ||||||
|  |  | ||||||
|  | namespace MCTeam\Common; | ||||||
|  |  | ||||||
|  | use FML\Controls\Frame; | ||||||
|  | use FML\Controls\Label; | ||||||
|  | use FML\Controls\Quad; | ||||||
|  | use FML\Controls\Quads\Quad_Bgs1InRace; | ||||||
|  | use ManiaControl\ManiaControl; | ||||||
|  | use ManiaControl\Players\Player; | ||||||
|  | use ManiaControl\Utils\Formatter; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * ManiaControl Local Records Plugin | ||||||
|  |  * | ||||||
|  |  * @author    ManiaControl Team <mail@maniacontrol.com> | ||||||
|  |  * @copyright 2014-2017 ManiaControl Team | ||||||
|  |  * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||||
|  |  */ | ||||||
|  | class RecordWidget { | ||||||
|  |  | ||||||
|  | 	private $maniaControl; | ||||||
|  | 	private $lineHeight = 4.; | ||||||
|  | 	private $width      = 40.; | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * RecordWidget constructor. | ||||||
|  | 	 * | ||||||
|  | 	 * @param \ManiaControl\ManiaControl $maniaControl | ||||||
|  | 	 */ | ||||||
|  | 	function __construct(ManiaControl $maniaControl) { | ||||||
|  | 		$this->maniaControl = $maniaControl; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Returns a Frame with one line of the Record | ||||||
|  | 	 * | ||||||
|  | 	 * @param                                   $record | ||||||
|  | 	 * @param \ManiaControl\Players\Player|null $player | ||||||
|  | 	 * @return \FML\Controls\Frame | ||||||
|  | 	 */ | ||||||
|  | 	public function generateRecordLineFrame($record, Player $player = null) { | ||||||
|  | 		$width      = $this->width; | ||||||
|  | 		$lineHeight = $this->lineHeight; | ||||||
|  |  | ||||||
|  | 		$recordFrame = new Frame(); | ||||||
|  |  | ||||||
|  | 		$largeNumberDiff = 0; | ||||||
|  | 		if ($record->rank > 999) { | ||||||
|  | 			$largeNumberDiff = 0.03; | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		$rankLabel = new Label(); | ||||||
|  | 		$recordFrame->addChild($rankLabel); | ||||||
|  | 		$rankLabel->setHorizontalAlign($rankLabel::LEFT); | ||||||
|  | 		$rankLabel->setX($width * -0.49); | ||||||
|  | 		$rankLabel->setSize($width * (0.09 + $largeNumberDiff), $lineHeight); | ||||||
|  | 		$rankLabel->setTextSize(1); | ||||||
|  | 		$rankLabel->setTextPrefix('$o'); | ||||||
|  | 		$rankLabel->setText($record->rank); | ||||||
|  | 		$rankLabel->setTextEmboss(true); | ||||||
|  |  | ||||||
|  | 		$nameLabel = new Label(); | ||||||
|  | 		$recordFrame->addChild($nameLabel); | ||||||
|  | 		$nameLabel->setHorizontalAlign($nameLabel::LEFT); | ||||||
|  | 		$nameLabel->setX($width * (-0.39 + $largeNumberDiff)); | ||||||
|  | 		$nameLabel->setSize($width * (0.6 - $largeNumberDiff), $lineHeight); | ||||||
|  | 		$nameLabel->setTextSize(1); | ||||||
|  | 		$nameLabel->setText($record->nickname); | ||||||
|  | 		$nameLabel->setTextEmboss(true); | ||||||
|  |  | ||||||
|  | 		$timeLabel = new Label(); | ||||||
|  | 		$recordFrame->addChild($timeLabel); | ||||||
|  | 		$timeLabel->setHorizontalAlign($timeLabel::RIGHT); | ||||||
|  | 		$timeLabel->setX($width * 0.49); | ||||||
|  | 		$timeLabel->setSize($width * 0.27, $lineHeight); | ||||||
|  | 		$timeLabel->setTextSize(1); | ||||||
|  | 		if (isset($record->time)) { | ||||||
|  | 			$timeLabel->setText(Formatter::formatTime($record->time)); | ||||||
|  | 		} else { | ||||||
|  | 			$timeLabel->setText(Formatter::formatTime($record->best)); | ||||||
|  | 		} | ||||||
|  | 		$timeLabel->setTextEmboss(true); | ||||||
|  |  | ||||||
|  | 		if ($player && $player->index == $record->playerIndex) { | ||||||
|  | 			$quad = new Quad(); | ||||||
|  | 			$recordFrame->addChild($quad); | ||||||
|  | 			$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList); | ||||||
|  | 			$quad->setSize($width, $lineHeight); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return $recordFrame; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Returns a Frame with Records to a given limit | ||||||
|  | 	 * | ||||||
|  | 	 * @param                                   $records | ||||||
|  | 	 * @param                                   $limit | ||||||
|  | 	 * @param \ManiaControl\Players\Player|null $player | ||||||
|  | 	 * @return \FML\Controls\Frame | ||||||
|  | 	 */ | ||||||
|  | 	public function generateRecordsFrame($records, $limit, Player $player = null) { | ||||||
|  | 		$lineHeight = $this->lineHeight; | ||||||
|  |  | ||||||
|  | 		$frame = new Frame(); | ||||||
|  |  | ||||||
|  | 		foreach ($records as $index => $record) { | ||||||
|  | 			if ($index >= $limit) { | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			$y = -8. - $index * $lineHeight; | ||||||
|  |  | ||||||
|  | 			$recordFrame = $this->generateRecordLineFrame($record, $player); | ||||||
|  | 			$frame->addChild($recordFrame); | ||||||
|  | 			$recordFrame->setPosition(0, $y); | ||||||
|  |  | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		return $frame; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * Returns the default separator Quad for the RecordWidget | ||||||
|  | 	 * | ||||||
|  | 	 * @param $width | ||||||
|  | 	 * @return \FML\Controls\Quad | ||||||
|  | 	 */ | ||||||
|  | 	public function getLineSeparatorQuad($width) { | ||||||
|  | 		$quad = new Quad(); | ||||||
|  | 		$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList); | ||||||
|  | 		$quad->setSize($width, 0.4); | ||||||
|  |  | ||||||
|  | 		return $quad; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @return float | ||||||
|  | 	 */ | ||||||
|  | 	public function getLineHeight() { | ||||||
|  | 		return $this->lineHeight; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @param float $lineHeight | ||||||
|  | 	 */ | ||||||
|  | 	public function setLineHeight($lineHeight) { | ||||||
|  | 		$this->lineHeight = $lineHeight; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @return float | ||||||
|  | 	 */ | ||||||
|  | 	public function getWidth() { | ||||||
|  | 		return $this->width; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 * @param float $width | ||||||
|  | 	 */ | ||||||
|  | 	public function setWidth($width) { | ||||||
|  | 		$this->width = $width; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -19,7 +19,10 @@ use ManiaControl\Manialinks\ManialinkManager; | |||||||
| use ManiaControl\Players\Player; | use ManiaControl\Players\Player; | ||||||
| use ManiaControl\Players\PlayerManager; | use ManiaControl\Players\PlayerManager; | ||||||
| use ManiaControl\Plugins\Plugin; | use ManiaControl\Plugins\Plugin; | ||||||
|  | use ManiaControl\Settings\Setting; | ||||||
|  | use ManiaControl\Settings\SettingManager; | ||||||
| use ManiaControl\Utils\Formatter; | use ManiaControl\Utils\Formatter; | ||||||
|  | use MCTeam\Common\RecordWidget; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * ManiaControl Dedimania Plugin |  * ManiaControl Dedimania Plugin | ||||||
| @@ -56,6 +59,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 	/** @var ManiaControl $maniaControl */ | 	/** @var ManiaControl $maniaControl */ | ||||||
| 	private $maniaControl = null; | 	private $maniaControl = null; | ||||||
|  |  | ||||||
|  | 	/** @var \MCTeam\Common\RecordWidget $recordWidget */ | ||||||
|  | 	private $recordWidget = null; | ||||||
|  |  | ||||||
| 	private $checkpoints = array(); | 	private $checkpoints = array(); | ||||||
|  |  | ||||||
| 	/** @var \MCTeam\Dedimania\DedimaniaWebHandler $webHandler */ | 	/** @var \MCTeam\Dedimania\DedimaniaWebHandler $webHandler */ | ||||||
| @@ -100,6 +106,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback'); | 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONWAYPOINT, $this, 'handleCheckpointCallback'); | ||||||
| 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback'); | 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONFINISHLINE, $this, 'handleFinishCallback'); | ||||||
| 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONLAPFINISH, $this, 'handleFinishCallback'); | 		$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::TM_ONLAPFINISH, $this, 'handleFinishCallback'); | ||||||
|  | 		$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged'); | ||||||
|  |  | ||||||
|  |  | ||||||
| 		$this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000); | 		$this->maniaControl->getTimerManager()->registerTimerListening($this, 'updateEverySecond', 1000); | ||||||
| 		$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryHalfMinute', 1000 * 30); | 		$this->maniaControl->getTimerManager()->registerTimerListening($this, 'handleEveryHalfMinute', 1000 * 30); | ||||||
| @@ -129,6 +137,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 		$this->webHandler = new DedimaniaWebHandler($this->maniaControl); | 		$this->webHandler = new DedimaniaWebHandler($this->maniaControl); | ||||||
| 		$this->webHandler->setDedimaniaData($dedimaniaData); | 		$this->webHandler->setDedimaniaData($dedimaniaData); | ||||||
| 		$this->webHandler->openDedimaniaSession(true); | 		$this->webHandler->openDedimaniaSession(true); | ||||||
|  |  | ||||||
|  | 		$this->recordWidget = new RecordWidget($this->maniaControl); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -174,6 +184,7 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 		$height = 7. + $lines * $lineHeight; | 		$height = 7. + $lines * $lineHeight; | ||||||
| 		$backgroundQuad->setSize($width * 1.05, $height); | 		$backgroundQuad->setSize($width * 1.05, $height); | ||||||
| 		$backgroundQuad->setStyles($quadStyle, $quadSubstyle); | 		$backgroundQuad->setStyles($quadStyle, $quadSubstyle); | ||||||
|  | 		$backgroundQuad->setAction(self::ACTION_SHOW_DEDIRECORDSLIST); | ||||||
|  |  | ||||||
| 		$titleLabel = new Label(); | 		$titleLabel = new Label(); | ||||||
| 		$frame->addChild($titleLabel); | 		$frame->addChild($titleLabel); | ||||||
| @@ -184,54 +195,8 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 		$titleLabel->setText($title); | 		$titleLabel->setText($title); | ||||||
| 		$titleLabel->setTranslate(true); | 		$titleLabel->setTranslate(true); | ||||||
|  |  | ||||||
| 		foreach ($records as $index => $record) { | 		$recordsFrame = $this->recordWidget->generateRecordsFrame($records,$lines); | ||||||
| 			/** @var RecordData $record */ | 		$frame->addChild($recordsFrame); | ||||||
| 			if ($index >= $lines) { |  | ||||||
| 				break; |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			$y = -8. - $index * $lineHeight; |  | ||||||
|  |  | ||||||
| 			$recordFrame = new Frame(); |  | ||||||
| 			$frame->addChild($recordFrame); |  | ||||||
| 			$recordFrame->setPosition(0, $y); |  | ||||||
|  |  | ||||||
| 			/*$backgroundQuad = new Quad(); |  | ||||||
| 			$recordFrame->addChild($backgroundQuad); |  | ||||||
| 			$backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4); |  | ||||||
| 			$backgroundQuad->setStyles($quadStyle, $quadSubstyle);*/ |  | ||||||
|  |  | ||||||
| 			//Rank |  | ||||||
| 			$rankLabel = new Label(); |  | ||||||
| 			$recordFrame->addChild($rankLabel); |  | ||||||
| 			$rankLabel->setHorizontalAlign($rankLabel::LEFT); |  | ||||||
| 			$rankLabel->setX($width * -0.47); |  | ||||||
| 			$rankLabel->setSize($width * 0.06, $lineHeight); |  | ||||||
| 			$rankLabel->setTextSize(1); |  | ||||||
| 			$rankLabel->setTextPrefix('$o'); |  | ||||||
| 			$rankLabel->setText($record->rank); |  | ||||||
| 			$rankLabel->setTextEmboss(true); |  | ||||||
|  |  | ||||||
| 			//Name |  | ||||||
| 			$nameLabel = new Label(); |  | ||||||
| 			$recordFrame->addChild($nameLabel); |  | ||||||
| 			$nameLabel->setHorizontalAlign($nameLabel::LEFT); |  | ||||||
| 			$nameLabel->setX($width * -0.4); |  | ||||||
| 			$nameLabel->setSize($width * 0.6, $lineHeight); |  | ||||||
| 			$nameLabel->setTextSize(1); |  | ||||||
| 			$nameLabel->setText($record->nickName); |  | ||||||
| 			$nameLabel->setTextEmboss(true); |  | ||||||
|  |  | ||||||
| 			//Time |  | ||||||
| 			$timeLabel = new Label(); |  | ||||||
| 			$recordFrame->addChild($timeLabel); |  | ||||||
| 			$timeLabel->setHorizontalAlign($timeLabel::RIGHT); |  | ||||||
| 			$timeLabel->setX($width * 0.47); |  | ||||||
| 			$timeLabel->setSize($width * 0.25, $lineHeight); |  | ||||||
| 			$timeLabel->setTextSize(1); |  | ||||||
| 			$timeLabel->setText(Formatter::formatTime($record->best)); |  | ||||||
| 			$timeLabel->setTextEmboss(true); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		$this->maniaControl->getManialinkManager()->sendManialink($manialink); | 		$this->maniaControl->getManialinkManager()->sendManialink($manialink); | ||||||
| 	} | 	} | ||||||
| @@ -626,13 +591,13 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 				$recordFrame->addChild($lineQuad); | 				$recordFrame->addChild($lineQuad); | ||||||
| 				$lineQuad->setSize($width, 4); | 				$lineQuad->setSize($width, 4); | ||||||
| 				$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig); | 				$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig); | ||||||
| 				$lineQuad->setZ(0.001); | 				$lineQuad->setZ(-0.001); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (strlen($listRecord->nickName) < 2) { | 			if (strlen($listRecord->nickname) < 2) { | ||||||
| 				$listRecord->nickName = $listRecord->login; | 				$listRecord->nickname = $listRecord->login; | ||||||
| 			} | 			} | ||||||
| 			$array = array($listRecord->rank => $posX + 5, '$fff' . $listRecord->nickName => $posX + 18, $listRecord->login => $posX + 70, Formatter::formatTime($listRecord->best) => $posX + 101); | 			$array = array($listRecord->rank => $posX + 5, '$fff' . $listRecord->nickname => $posX + 18, $listRecord->login => $posX + 70, Formatter::formatTime($listRecord->best) => $posX + 101); | ||||||
| 			$this->maniaControl->getManialinkManager()->labelLine($recordFrame, $array); | 			$this->maniaControl->getManialinkManager()->labelLine($recordFrame, $array); | ||||||
|  |  | ||||||
| 			$recordFrame->setY($posY); | 			$recordFrame->setY($posY); | ||||||
| @@ -654,6 +619,26 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene | |||||||
| 		return $this->webHandler->getDedimaniaData()->records; | 		return $this->webHandler->getDedimaniaData()->records; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 *  Update the RecordWidget variables | ||||||
|  | 	 */ | ||||||
|  | 	private function updateRecordWidget() { | ||||||
|  | 		$width              = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH); | ||||||
|  | 		$lineHeight         = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINE_HEIGHT); | ||||||
|  |  | ||||||
|  | 		$this->recordWidget->setWidth($width); | ||||||
|  | 		$this->recordWidget->setLineHeight($lineHeight); | ||||||
|  |  | ||||||
|  | 		$this->webHandler->maniaLinkUpdateNeeded(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	public function handleSettingChanged(Setting $setting){ | ||||||
|  | 		if (!$setting->belongsToClass($this)) { | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
|  | 		$this->updateRecordWidget(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * @see \ManiaControl\Plugins\Plugin::unload() | 	 * @see \ManiaControl\Plugins\Plugin::unload() | ||||||
| 	 */ | 	 */ | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ class RecordData { | |||||||
| 	 */ | 	 */ | ||||||
| 	public $nullRecord = false; | 	public $nullRecord = false; | ||||||
| 	public $login = ''; | 	public $login = ''; | ||||||
| 	public $nickName = ''; | 	public $nickname = ''; | ||||||
| 	public $best = -1; | 	public $best = -1; | ||||||
| 	public $rank = -1; | 	public $rank = -1; | ||||||
| 	public $maxRank = -1; | 	public $maxRank = -1; | ||||||
| @@ -38,7 +38,7 @@ class RecordData { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		$this->login       = $record['Login']; | 		$this->login       = $record['Login']; | ||||||
| 		$this->nickName    = Formatter::stripDirtyCodes($record['NickName']); | 		$this->nickname    = Formatter::stripDirtyCodes($record['NickName']); | ||||||
| 		$this->best        = $record['Best']; | 		$this->best        = $record['Best']; | ||||||
| 		$this->rank        = $record['Rank']; | 		$this->rank        = $record['Rank']; | ||||||
| 		$this->maxRank     = $record['MaxRank']; | 		$this->maxRank     = $record['MaxRank']; | ||||||
| @@ -54,10 +54,10 @@ class RecordData { | |||||||
| 	 * @param int    $checkpoints | 	 * @param int    $checkpoints | ||||||
| 	 * @param bool   $newRecord | 	 * @param bool   $newRecord | ||||||
| 	 */ | 	 */ | ||||||
| 	public function constructNewRecord($login, $nickName, $best, $checkpoints, $newRecord = false) { | 	public function constructNewRecord($login, $nickname, $best, $checkpoints, $newRecord = false) { | ||||||
| 		$this->nullRecord  = false; | 		$this->nullRecord  = false; | ||||||
| 		$this->login       = $login; | 		$this->login       = $login; | ||||||
| 		$this->nickName    = $nickName; | 		$this->nickname    = $nickname; | ||||||
| 		$this->best        = $best; | 		$this->best        = $best; | ||||||
| 		$this->checkpoints = $checkpoints; | 		$this->checkpoints = $checkpoints; | ||||||
| 		$this->newRecord   = $newRecord; | 		$this->newRecord   = $newRecord; | ||||||
|   | |||||||
| @@ -29,6 +29,7 @@ use ManiaControl\Plugins\Plugin; | |||||||
| use ManiaControl\Settings\Setting; | use ManiaControl\Settings\Setting; | ||||||
| use ManiaControl\Settings\SettingManager; | use ManiaControl\Settings\SettingManager; | ||||||
| use ManiaControl\Utils\Formatter; | use ManiaControl\Utils\Formatter; | ||||||
|  | use MCTeam\Common\RecordWidget; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * ManiaControl Local Records Plugin |  * ManiaControl Local Records Plugin | ||||||
| @@ -67,6 +68,8 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 	 */ | 	 */ | ||||||
| 	/** @var ManiaControl $maniaControl */ | 	/** @var ManiaControl $maniaControl */ | ||||||
| 	private $maniaControl = null; | 	private $maniaControl = null; | ||||||
|  | 	/** @var \MCTeam\Common\RecordWidget $recordWidget */ | ||||||
|  | 	private $recordWidget    = null; | ||||||
| 	private $updateManialink = false; | 	private $updateManialink = false; | ||||||
| 	private $checkpoints     = array(); | 	private $checkpoints     = array(); | ||||||
|  |  | ||||||
| @@ -120,6 +123,8 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 		$this->maniaControl = $maniaControl; | 		$this->maniaControl = $maniaControl; | ||||||
| 		$this->initTables(); | 		$this->initTables(); | ||||||
|  |  | ||||||
|  | 		$this->recordWidget = new RecordWidget($this->maniaControl); | ||||||
|  |  | ||||||
| 		// Settings | 		// Settings | ||||||
| 		$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records'); | 		$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records'); | ||||||
| 		$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSX, -139.); | 		$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_WIDGET_POSX, -139.); | ||||||
| @@ -267,11 +272,15 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 		$titleLabel->setTranslate(true); | 		$titleLabel->setTranslate(true); | ||||||
|  |  | ||||||
| 		$topRecordsCount                    = $lines - $recordsBeforeAfter * 2 - 1; | 		$topRecordsCount                    = $lines - $recordsBeforeAfter * 2 - 1; | ||||||
| 		$preGeneratedRecordsFrame = $this->generateRecordsFrame($records, $topRecordsCount); | 		$preGeneratedTopRecordsFrame        = $this->recordWidget->generateRecordsFrame($records, $topRecordsCount); | ||||||
|  | 		$preGeneratedRecordsFrameWithRecord = $this->recordWidget->generateRecordsFrame($records, $lines - 1); | ||||||
|  |  | ||||||
| 		$players              = $this->maniaControl->getPlayerManager()->getPlayers(); | 		$players              = $this->maniaControl->getPlayerManager()->getPlayers(); | ||||||
|  | 		$playersWithoutRecord = array(); | ||||||
|  |  | ||||||
| 		foreach ($players as $player) { | 		foreach ($players as $player) { | ||||||
|  | 			$sendManiaLink = true; | ||||||
|  |  | ||||||
| 			$maniaLink = new ManiaLink(self::MLID_RECORDS); | 			$maniaLink = new ManiaLink(self::MLID_RECORDS); | ||||||
| 			$maniaLink->addChild($frame); | 			$maniaLink->addChild($frame); | ||||||
|  |  | ||||||
| @@ -280,117 +289,69 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 			$listFrame->setPosition($posX, $posY); | 			$listFrame->setPosition($posX, $posY); | ||||||
|  |  | ||||||
| 			if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] >= $topRecordsCount) { | 			if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] >= $topRecordsCount) { | ||||||
| 				$listFrame->addChild($preGeneratedRecordsFrame); | 				$listFrame->addChild($preGeneratedTopRecordsFrame); | ||||||
|  |  | ||||||
| 				$y           = -8 - $topRecordsCount * $lineHeight; | 				$y           = -8 - $topRecordsCount * $lineHeight; | ||||||
| 				$playerIndex = $playerRecords[$player->index]; | 				$playerIndex = $playerRecords[$player->index]; | ||||||
|  |  | ||||||
| 				//Line separator | 				//Line separator | ||||||
| 				$quad = new Quad(); | 				$quad = $this->recordWidget->getLineSeparatorQuad($width); | ||||||
| 				$listFrame->addChild($quad); | 				$listFrame->addChild($quad); | ||||||
| 				$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList); |  | ||||||
| 				$quad->setSize($width, 0.4); |  | ||||||
| 				$quad->setY($y + $lineHeight / 2); | 				$quad->setY($y + $lineHeight / 2); | ||||||
|  |  | ||||||
| 				//Generate the Records around a player and display below topRecords | 				//Generate the Records around a player and display below topRecords | ||||||
| 				for ($i = $playerIndex - $recordsBeforeAfter; $i <= $playerIndex + $recordsBeforeAfter; $i++) { | 				for ($i = $playerIndex - $recordsBeforeAfter; $i <= $playerIndex + $recordsBeforeAfter; $i++) { | ||||||
| 					if (array_key_exists($i, $records)) { //If there are no records behind you | 					if (array_key_exists($i, $records)) { //If there are no records behind you | ||||||
| 						$recordFrame = $this->generateRecordLineFrame($records[$i], $player); | 						$recordFrame = $this->recordWidget->generateRecordLineFrame($records[$i], $player); | ||||||
| 						$recordFrame->setY($y); | 						$recordFrame->setY($y); | ||||||
| 						$listFrame->addChild($recordFrame); | 						$listFrame->addChild($recordFrame); | ||||||
| 						$y -= $lineHeight; | 						$y -= $lineHeight; | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
|  | 			} else if (isset($playerRecords[$player->index]) && $playerRecords[$player->index] < $topRecordsCount) { | ||||||
|  | 				$playersWithoutRecord[] = $player; | ||||||
|  | 				$sendManiaLink          = false; | ||||||
| 			} else { | 			} else { | ||||||
| 				$listFrame->addChild($this->generateRecordsFrame($records, $lines, $player)); //TODO Collect all players with this case and send a common manialink to save resources | 				if ($record = $this->getLocalRecord($map, $player)) { | ||||||
|  | 					$record->nickname = $player->nickname; | ||||||
|  | 					$recordFrame      = $preGeneratedRecordsFrameWithRecord; | ||||||
|  | 					$listFrame->addChild($recordFrame); | ||||||
|  |  | ||||||
|  | 					$y = -8 - ($lines - 1) * $lineHeight; | ||||||
|  |  | ||||||
|  | 					//Line separator | ||||||
|  | 					$quad = $this->recordWidget->getLineSeparatorQuad($width); | ||||||
|  | 					$listFrame->addChild($quad); | ||||||
|  | 					$quad->setY($y + $lineHeight / 2); | ||||||
|  |  | ||||||
|  | 					$frame = $this->recordWidget->generateRecordLineFrame($record, $player); | ||||||
|  | 					$listFrame->addChild($frame); | ||||||
|  | 					$frame->setY($y); | ||||||
|  |  | ||||||
|  | 				} else { | ||||||
|  | 					$playersWithoutRecord[] = $player; | ||||||
|  | 					$sendManiaLink          = false; | ||||||
|  | 				} | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			if ($sendManiaLink) { | ||||||
| 				$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player); | 				$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 	/** | 		if ($playersWithoutRecord) { | ||||||
| 	 * Returns a Frame with one line of the Record | 			$maniaLink = new ManiaLink(self::MLID_RECORDS); | ||||||
| 	 * | 			$maniaLink->addChild($frame); | ||||||
| 	 * @param                                   $record |  | ||||||
| 	 * @param \ManiaControl\Players\Player|null $player |  | ||||||
| 	 * @return \FML\Controls\Frame |  | ||||||
| 	 */ |  | ||||||
| 	private function generateRecordLineFrame($record, Player $player = null) { |  | ||||||
| 		$width      = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH); |  | ||||||
| 		$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT); |  | ||||||
|  |  | ||||||
| 		$recordFrame = new Frame(); | 			$listFrame = $this->recordWidget->generateRecordsFrame($records, $lines); | ||||||
|  | 			$maniaLink->addChild($listFrame); | ||||||
|  | 			$listFrame->setPosition($posX, $posY); | ||||||
|  |  | ||||||
| 		$rankLabel = new Label(); | 			$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $playersWithoutRecord); | ||||||
| 		$recordFrame->addChild($rankLabel); |  | ||||||
| 		$rankLabel->setHorizontalAlign($rankLabel::LEFT); |  | ||||||
| 		$rankLabel->setX($width * -0.49); |  | ||||||
| 		$rankLabel->setSize($width * 0.09, $lineHeight); |  | ||||||
| 		$rankLabel->setTextSize(1); |  | ||||||
| 		$rankLabel->setTextPrefix('$o'); |  | ||||||
| 		$rankLabel->setText($record->rank); |  | ||||||
| 		$rankLabel->setTextEmboss(true); |  | ||||||
|  |  | ||||||
| 		$nameLabel = new Label(); |  | ||||||
| 		$recordFrame->addChild($nameLabel); |  | ||||||
| 		$nameLabel->setHorizontalAlign($nameLabel::LEFT); |  | ||||||
| 		$nameLabel->setX($width * -0.39); |  | ||||||
| 		$nameLabel->setSize($width * 0.6, $lineHeight); |  | ||||||
| 		$nameLabel->setTextSize(1); |  | ||||||
| 		$nameLabel->setText($record->nickname); |  | ||||||
| 		$nameLabel->setTextEmboss(true); |  | ||||||
|  |  | ||||||
| 		$timeLabel = new Label(); |  | ||||||
| 		$recordFrame->addChild($timeLabel); |  | ||||||
| 		$timeLabel->setHorizontalAlign($timeLabel::RIGHT); |  | ||||||
| 		$timeLabel->setX($width * 0.49); |  | ||||||
| 		$timeLabel->setSize($width * 0.27, $lineHeight); |  | ||||||
| 		$timeLabel->setTextSize(1); |  | ||||||
| 		$timeLabel->setText(Formatter::formatTime($record->time)); |  | ||||||
| 		$timeLabel->setTextEmboss(true); |  | ||||||
|  |  | ||||||
| 		if ($player && $player->index == $record->playerIndex) { |  | ||||||
| 			$quad = new Quad(); |  | ||||||
| 			$recordFrame->addChild($quad); |  | ||||||
| 			$quad->setStyles(Quad_Bgs1InRace::STYLE, Quad_Bgs1InRace::SUBSTYLE_BgCardList); |  | ||||||
| 			$quad->setSize($width, $lineHeight); |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		return $recordFrame; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** |  | ||||||
| 	 * Returns a Frame with Records to a given limit |  | ||||||
| 	 * |  | ||||||
| 	 * @param                                   $records |  | ||||||
| 	 * @param                                   $limit |  | ||||||
| 	 * @param \ManiaControl\Players\Player|null $player |  | ||||||
| 	 * @return \FML\Controls\Frame |  | ||||||
| 	 */ |  | ||||||
| 	private function generateRecordsFrame($records, $limit, Player $player = null) { |  | ||||||
| 		$lineHeight = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT); |  | ||||||
|  |  | ||||||
| 		$frame = new Frame(); |  | ||||||
|  |  | ||||||
| 		foreach ($records as $index => $record) { |  | ||||||
| 			if ($index >= $limit) { |  | ||||||
| 				break; |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			$y = -8. - $index * $lineHeight; |  | ||||||
|  |  | ||||||
| 			$recordFrame = $this->generateRecordLineFrame($record, $player); |  | ||||||
| 			$frame->addChild($recordFrame); |  | ||||||
| 			$recordFrame->setPosition(0, $y); |  | ||||||
|  |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		return $frame; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Fetch local records for the given map | 	 * Fetch local records for the given map | ||||||
| 	 * | 	 * | ||||||
| @@ -442,7 +403,7 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 				break; | 				break; | ||||||
| 			} | 			} | ||||||
| 			default: | 			default: | ||||||
| 				$this->updateManialink = true; | 				$this->updateRecordWidget(); | ||||||
| 				break; | 				break; | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -765,4 +726,16 @@ class LocalRecordsPlugin implements ManialinkPageAnswerListener, CallbackListene | |||||||
| 		$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null); | 		$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null); | ||||||
| 		$this->maniaControl->getChat()->sendInformation('Record no. $<$fff' . $recordId . '$> has been removed!'); | 		$this->maniaControl->getChat()->sendInformation('Record no. $<$fff' . $recordId . '$> has been removed!'); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	/** | ||||||
|  | 	 *  Update the RecordWidget variables | ||||||
|  | 	 */ | ||||||
|  | 	private function updateRecordWidget() { | ||||||
|  | 		$width              = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_WIDTH); | ||||||
|  | 		$lineHeight         = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_WIDGET_LINEHEIGHT); | ||||||
|  |  | ||||||
|  | 		$this->recordWidget->setWidth($width); | ||||||
|  | 		$this->recordWidget->setLineHeight($lineHeight); | ||||||
|  | 		$this->updateManialink = true; | ||||||
|  | 	} | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user