TrackManiaControl/core/Manialinks/ToggleInterfaceManager.php

106 lines
3.1 KiB
PHP
Raw Normal View History

2022-03-03 18:00:30 +01:00
<?php
namespace ManiaControl\Manialinks;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
2022-03-03 19:03:02 +01:00
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
2022-03-03 18:00:30 +01:00
use FML\ManiaLink;
/**
* Class managing the Custom UI in ManiaPlanet
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ToggleInterfaceManager implements CallbackListener {
/*
* Constants
*/
const MLID = 'ToggleInterface.KeyListener';
2022-03-03 19:03:02 +01:00
const SETTING_KEYNAME = 'Key Name (or code) to toggle the ManiaControl UI';
2022-03-12 11:26:56 +01:00
const SETTING_DEFAULT_VISIBLE = 'Display by default the UI';
2022-03-03 18:00:30 +01:00
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $manialink = "";
/**
* Create a custom UI manager instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2022-03-03 19:03:02 +01:00
// Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_KEYNAME, "F9");
2022-03-12 11:26:56 +01:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DEFAULT_VISIBLE, True);
2022-03-03 19:03:02 +01:00
// Build Manialink
2022-03-03 18:00:30 +01:00
$this->buildManiaLink();
// Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined');
2022-03-03 19:03:02 +01:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged'); // TODO Setting Change Key
2022-03-03 18:00:30 +01:00
}
/**
* Handle ManiaControl AfterInit callback
*
* @internal
*/
public function handleAfterInit() {
$players = $this->maniaControl->getPlayerManager()->getPlayers();
if (!empty($players)) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink , $players, 0, false, false);
}
}
/**
* Handle PlayerJoined Callback
*
* @param Player $player
*/
public function handlePlayerJoined(Player $player) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink , $player, 0, false, false);
}
2022-03-03 19:03:02 +01:00
/**
* Handle Setting Changed Callback
*
* @param Setting $setting
*/
public function handleSettingChanged(Setting $setting) {
if (!$setting->belongsToClass($this)) {
return;
}
$this->buildManiaLink();
$this->handleAfterInit();
}
2022-03-03 18:00:30 +01:00
/**
* Build the Manialink with only the Toggle Interface script feature
*/
private function buildManiaLink() {
$manialink = new ManiaLink(self::MLID);
2022-03-12 11:26:56 +01:00
$manialink->getScript()->addFeature(new \FML\Script\Features\ToggleInterface($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_KEYNAME), $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DEFAULT_VISIBLE)));
2022-03-03 18:00:30 +01:00
$this->manialink = (string) $manialink;
}
}