├── .editorconfig ├── .gitignore ├── .poggit.yml ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── sample ├── plugin.yml └── src │ └── tokyo │ └── pmmp │ └── sample │ └── libform │ └── Core.php ├── src └── tokyo │ └── pmmp │ └── libform │ ├── EventListener.php │ ├── FormApi.php │ ├── element │ ├── Button.php │ ├── Dropdown.php │ ├── Element.php │ ├── Image.php │ ├── Input.php │ ├── Label.php │ ├── Slider.php │ ├── StepSlider.php │ └── Toggle.php │ └── form │ ├── CustomForm.php │ ├── Form.php │ ├── ListForm.php │ └── ModalForm.php └── virion.yml /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = yes 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .history/ 3 | 4 | /vendor 5 | /composer.lock -------------------------------------------------------------------------------- /.poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/fuyutsuki/libform 2 | branches: 3 | - master 4 | projects: 5 | libform: 6 | path: "" 7 | type: library 8 | model: virion 9 | libform-sample: 10 | path: "/sample" 11 | ... 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 yuko fuyutsuki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## libform 2 | Select language: [English](#english), [日本語](#日本語) 3 | 4 | *** 5 | ### English 6 | It is a virion library that can handle various forms easily. 7 | 8 | #### Installation 9 | Composer: 10 | > Please use the attached pmmp/PocketMine-MP's php binary to execute the composer command in the folder plugin you want to use this library. 11 | ```bash 12 | composer require fuyutsuki/libform # API3.x 13 | composer require fuyutsuki/libform:dev-4.0 # API4.x 14 | ``` 15 | 16 | Virion: 17 | > Edit the .poggit.yml of the plugin you want to use this library as follows. 18 | ```yaml 19 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/author/YourProject 20 | branches: 21 | - master 22 | projects: 23 | YourProject: 24 | path: "" 25 | icon: "" 26 | libs: 27 | - src: fuyutsuki/libform/libform 28 | version: 0.4.0 29 | ... 30 | ``` 31 | 32 | #### Usage 33 | Please see the [sample plugin](https://github.com/fuyutsuki/libform/tree/master/sample). 34 | 35 | *** 36 | ### 日本語 37 | 様々なフォームを簡単に扱うためのvirionライブラリです。 38 | 39 | #### インストール方法 40 | Composer: 41 | > pmmp/PocketMine-MP付属のphpバイナリを使って、このライブラリを使いたいフォルダプラグインの中でcomposerコマンドを実行して下さい。 42 | ```bash 43 | composer require fuyutsuki/libform # API3.x用 44 | composer require fuyutsuki/libform:dev-4.0 # API4.x用 45 | ``` 46 | 47 | Virion: 48 | > このライブラリを使いたいプラグインの.poggit.ymlを以下のように編集します。 49 | ```yaml 50 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/author/YourProject 51 | branches: 52 | - master 53 | projects: 54 | YourProject: 55 | path: "" 56 | icon: "" 57 | libs: 58 | - src: fuyutsuki/libform/libform 59 | version: 0.4.0 60 | ... 61 | ``` 62 | 63 | #### 使い方 64 | [サンプルプラグイン](https://github.com/fuyutsuki/libform/tree/master/sample)をご覧ください。 -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fuyutsuki/libform", 3 | "description": "It is a virion library that can handle various forms easily.", 4 | "type": "project", 5 | "homepage": "https://github.com/fuyutsuki/libform", 6 | "license": "MIT", 7 | "require": { 8 | "php": ">=7.2.0", 9 | "ext-bcmath": "*", 10 | "ext-curl": "*", 11 | "ext-hash": "*", 12 | "ext-json": "*", 13 | "ext-mbstring": "*", 14 | "ext-openssl": "*", 15 | "ext-pcre": "*", 16 | "ext-phar": "*", 17 | "ext-pthreads": ">=3.1.7dev", 18 | "ext-reflection": "*", 19 | "ext-sockets": "*", 20 | "ext-spl": "*", 21 | "ext-yaml": ">=2.0.0", 22 | "ext-zip": "*", 23 | "ext-zlib": ">=1.2.11", 24 | "pocketmine/pocketmine-mp": "^3.0", 25 | "pocketmine/raklib": "^0.12.0", 26 | "pocketmine/spl": "^0.3.0", 27 | "pocketmine/binaryutils": "^0.1.0", 28 | "pocketmine/nbt": "^0.2.1", 29 | "pocketmine/math": "^0.2.0", 30 | "pocketmine/snooze": "^0.1.0" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "": [ 35 | "/src", 36 | "/sample/src" 37 | ] 38 | } 39 | }, 40 | "repositories": [ 41 | { 42 | "type": "vcs", 43 | "url": "https://github.com/pmmp/PocketMine-MP" 44 | }, 45 | { 46 | "type": "vcs", 47 | "url": "https://github.com/pmmp/RakLib" 48 | }, 49 | { 50 | "type": "vcs", 51 | "url": "https://github.com/pmmp/SPL" 52 | }, 53 | { 54 | "type": "vcs", 55 | "url": "https://github.com/pmmp/BinaryUtils" 56 | }, 57 | { 58 | "type": "vcs", 59 | "url": "https://github.com/pmmp/NBT" 60 | }, 61 | { 62 | "type": "vcs", 63 | "url": "https://github.com/pmmp/Math" 64 | }, 65 | { 66 | "type": "vcs", 67 | "url": "https://github.com/pmmp/Snooze" 68 | } 69 | ] 70 | } -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "f51bd90423a5512d89506f26db2d8419", 8 | "packages": [ 9 | { 10 | "name": "pocketmine/binaryutils", 11 | "version": "0.1.1", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/pmmp/BinaryUtils.git", 15 | "reference": "54efeb978be0ff9335022729fe63c1e2077bf1be" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/pmmp/BinaryUtils/zipball/54efeb978be0ff9335022729fe63c1e2077bf1be", 20 | "reference": "54efeb978be0ff9335022729fe63c1e2077bf1be", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "php": ">=7.2", 25 | "php-64bit": "*" 26 | }, 27 | "type": "library", 28 | "autoload": { 29 | "psr-4": { 30 | "pocketmine\\utils\\": "src/" 31 | } 32 | }, 33 | "license": [ 34 | "LGPL-3.0" 35 | ], 36 | "description": "Classes and methods for conveniently handling binary data", 37 | "support": { 38 | "source": "https://github.com/pmmp/BinaryUtils/tree/0.1.1", 39 | "issues": "https://github.com/pmmp/BinaryUtils/issues" 40 | }, 41 | "time": "2018-08-26T18:11:05+00:00" 42 | }, 43 | { 44 | "name": "pocketmine/math", 45 | "version": "0.2.1", 46 | "source": { 47 | "type": "git", 48 | "url": "https://github.com/pmmp/Math.git", 49 | "reference": "ee299f5c9c444ca526c9c691b920f321458cf0b6" 50 | }, 51 | "dist": { 52 | "type": "zip", 53 | "url": "https://api.github.com/repos/pmmp/Math/zipball/ee299f5c9c444ca526c9c691b920f321458cf0b6", 54 | "reference": "ee299f5c9c444ca526c9c691b920f321458cf0b6", 55 | "shasum": "" 56 | }, 57 | "require": { 58 | "php": ">=7.2.0", 59 | "php-64bit": "*" 60 | }, 61 | "type": "library", 62 | "autoload": { 63 | "psr-4": { 64 | "pocketmine\\math\\": "src/" 65 | } 66 | }, 67 | "license": [ 68 | "LGPL-3.0" 69 | ], 70 | "description": "PHP library containing math related code used in PocketMine-MP", 71 | "support": { 72 | "source": "https://github.com/pmmp/Math/tree/0.2.1", 73 | "issues": "https://github.com/pmmp/Math/issues" 74 | }, 75 | "time": "2018-08-15T15:43:27+00:00" 76 | }, 77 | { 78 | "name": "pocketmine/nbt", 79 | "version": "0.2.2", 80 | "source": { 81 | "type": "git", 82 | "url": "https://github.com/pmmp/NBT.git", 83 | "reference": "474f0cf0a47656d0122b4f3f71302e694ed6977b" 84 | }, 85 | "dist": { 86 | "type": "zip", 87 | "url": "https://api.github.com/repos/pmmp/NBT/zipball/474f0cf0a47656d0122b4f3f71302e694ed6977b", 88 | "reference": "474f0cf0a47656d0122b4f3f71302e694ed6977b", 89 | "shasum": "" 90 | }, 91 | "require": { 92 | "php": ">=7.2.0", 93 | "php-64bit": "*", 94 | "pocketmine/binaryutils": "^0.1.0" 95 | }, 96 | "type": "library", 97 | "autoload": { 98 | "psr-4": { 99 | "pocketmine\\nbt\\": "src/" 100 | } 101 | }, 102 | "autoload-dev": { 103 | "psr-4": { 104 | "pocketmine\\nbt\\": "tests/phpunit/" 105 | } 106 | }, 107 | "license": [ 108 | "LGPL-3.0" 109 | ], 110 | "description": "PHP library for working with Named Binary Tags", 111 | "support": { 112 | "source": "https://github.com/pmmp/NBT/tree/0.2.2", 113 | "issues": "https://github.com/pmmp/NBT/issues" 114 | }, 115 | "time": "2018-10-12T08:26:44+00:00" 116 | }, 117 | { 118 | "name": "pocketmine/pocketmine-mp", 119 | "version": "3.3.2", 120 | "source": { 121 | "type": "git", 122 | "url": "https://github.com/pmmp/PocketMine-MP.git", 123 | "reference": "200de3fe8460d180a4fd9f5a0e0768bd3085fd50" 124 | }, 125 | "dist": { 126 | "type": "zip", 127 | "url": "https://api.github.com/repos/pmmp/PocketMine-MP/zipball/200de3fe8460d180a4fd9f5a0e0768bd3085fd50", 128 | "reference": "200de3fe8460d180a4fd9f5a0e0768bd3085fd50", 129 | "shasum": "" 130 | }, 131 | "require": { 132 | "ext-bcmath": "*", 133 | "ext-ctype": "*", 134 | "ext-curl": "*", 135 | "ext-date": "*", 136 | "ext-hash": "*", 137 | "ext-json": "*", 138 | "ext-mbstring": "*", 139 | "ext-openssl": "*", 140 | "ext-pcre": "*", 141 | "ext-phar": "*", 142 | "ext-pthreads": ">=3.1.7dev", 143 | "ext-reflection": "*", 144 | "ext-sockets": "*", 145 | "ext-spl": "*", 146 | "ext-yaml": ">=2.0.0", 147 | "ext-zip": "*", 148 | "ext-zlib": ">=1.2.11", 149 | "php": ">=7.2.0", 150 | "php-64bit": "*", 151 | "pocketmine/binaryutils": "^0.1.0", 152 | "pocketmine/math": "^0.2.0", 153 | "pocketmine/nbt": "^0.2.1", 154 | "pocketmine/raklib": "^0.12.0", 155 | "pocketmine/snooze": "^0.1.0", 156 | "pocketmine/spl": "^0.3.0" 157 | }, 158 | "type": "project", 159 | "autoload": { 160 | "psr-4": { 161 | "": [ 162 | "src" 163 | ] 164 | } 165 | }, 166 | "autoload-dev": { 167 | "psr-4": { 168 | "pocketmine\\": "tests/phpunit/" 169 | } 170 | }, 171 | "license": [ 172 | "LGPL-3.0" 173 | ], 174 | "description": "A server software for Minecraft: Pocket Edition written in PHP", 175 | "homepage": "https://pmmp.io", 176 | "support": { 177 | "source": "https://github.com/pmmp/PocketMine-MP/tree/3.3.2", 178 | "issues": "https://github.com/pmmp/PocketMine-MP/issues" 179 | }, 180 | "time": "2018-11-11T11:58:25+00:00" 181 | }, 182 | { 183 | "name": "pocketmine/raklib", 184 | "version": "0.12.0", 185 | "source": { 186 | "type": "git", 187 | "url": "https://github.com/pmmp/RakLib.git", 188 | "reference": "922da28efd828e2af6c19db1676ea9b6a267071c" 189 | }, 190 | "dist": { 191 | "type": "zip", 192 | "url": "https://api.github.com/repos/pmmp/RakLib/zipball/922da28efd828e2af6c19db1676ea9b6a267071c", 193 | "reference": "922da28efd828e2af6c19db1676ea9b6a267071c", 194 | "shasum": "" 195 | }, 196 | "require": { 197 | "ext-bcmath": "*", 198 | "ext-pthreads": ">=3.1.7dev", 199 | "ext-sockets": "*", 200 | "php": ">=7.2.0", 201 | "php-64bit": "*", 202 | "php-ipv6": "*", 203 | "pocketmine/binaryutils": "^0.1.0", 204 | "pocketmine/snooze": "^0.1.0", 205 | "pocketmine/spl": "^0.3.0" 206 | }, 207 | "type": "library", 208 | "autoload": { 209 | "psr-4": { 210 | "raklib\\": "src/" 211 | } 212 | }, 213 | "license": [ 214 | "GPL-3.0" 215 | ], 216 | "description": "A RakNet server implementation written in PHP", 217 | "support": { 218 | "source": "https://github.com/pmmp/RakLib/tree/0.12.0", 219 | "issues": "https://github.com/pmmp/RakLib/issues" 220 | }, 221 | "time": "2018-06-13T10:06:14+00:00" 222 | }, 223 | { 224 | "name": "pocketmine/snooze", 225 | "version": "0.1.0", 226 | "source": { 227 | "type": "git", 228 | "url": "https://github.com/pmmp/Snooze.git", 229 | "reference": "3cc9d0164230889acb08e22cc126133809e9d346" 230 | }, 231 | "dist": { 232 | "type": "zip", 233 | "url": "https://api.github.com/repos/pmmp/Snooze/zipball/3cc9d0164230889acb08e22cc126133809e9d346", 234 | "reference": "3cc9d0164230889acb08e22cc126133809e9d346", 235 | "shasum": "" 236 | }, 237 | "require": { 238 | "ext-pthreads": ">=3.1.7dev", 239 | "php-64bit": ">=7.2.0" 240 | }, 241 | "type": "library", 242 | "autoload": { 243 | "psr-4": { 244 | "pocketmine\\snooze\\": "src/" 245 | } 246 | }, 247 | "license": [ 248 | "LGPL-3.0" 249 | ], 250 | "description": "Thread notification management library for code using the pthreads extension", 251 | "support": { 252 | "source": "https://github.com/pmmp/Snooze/tree/0.1.0", 253 | "issues": "https://github.com/pmmp/Snooze/issues" 254 | }, 255 | "time": "2018-06-13T09:36:11+00:00" 256 | }, 257 | { 258 | "name": "pocketmine/spl", 259 | "version": "0.3.2", 260 | "source": { 261 | "type": "git", 262 | "url": "https://github.com/pmmp/SPL.git", 263 | "reference": "7fd53857cd000491ba69e8db865792a024dd2c49" 264 | }, 265 | "dist": { 266 | "type": "zip", 267 | "url": "https://api.github.com/repos/pmmp/SPL/zipball/7fd53857cd000491ba69e8db865792a024dd2c49", 268 | "reference": "7fd53857cd000491ba69e8db865792a024dd2c49", 269 | "shasum": "" 270 | }, 271 | "type": "library", 272 | "autoload": { 273 | "exclude-from-classmap": [ 274 | "stubs" 275 | ], 276 | "classmap": [ 277 | "./" 278 | ] 279 | }, 280 | "license": [ 281 | "LGPL-3.0" 282 | ], 283 | "description": "Standard library files required by PocketMine-MP and related projects", 284 | "support": { 285 | "source": "https://github.com/pmmp/SPL/tree/master" 286 | }, 287 | "time": "2018-08-12T15:17:39+00:00" 288 | } 289 | ], 290 | "packages-dev": [], 291 | "aliases": [], 292 | "minimum-stability": "stable", 293 | "stability-flags": { 294 | "ext-pthreads": 20 295 | }, 296 | "prefer-stable": false, 297 | "prefer-lowest": false, 298 | "platform": { 299 | "php": ">=7.2.0", 300 | "ext-bcmath": "*", 301 | "ext-curl": "*", 302 | "ext-hash": "*", 303 | "ext-json": "*", 304 | "ext-mbstring": "*", 305 | "ext-openssl": "*", 306 | "ext-pcre": "*", 307 | "ext-phar": "*", 308 | "ext-pthreads": ">=3.1.7dev", 309 | "ext-reflection": "*", 310 | "ext-sockets": "*", 311 | "ext-spl": "*", 312 | "ext-yaml": ">=2.0.0", 313 | "ext-zip": "*", 314 | "ext-zlib": ">=1.2.11" 315 | }, 316 | "platform-dev": [] 317 | } 318 | -------------------------------------------------------------------------------- /sample/plugin.yml: -------------------------------------------------------------------------------- 1 | name: sample-libform 2 | main: tokyo\pmmp\sample\libform\Core 3 | version: 1.0.0 4 | api: 3.0.0 # pmmp/PocketMine-MP 5 | softdepend: 6 | - DEVirion 7 | author: yuko_fuyutsuki 8 | website: https://github.com/fuyutsuki/libform 9 | 10 | commands: 11 | testform: 12 | usage: /testform 13 | description: test forms! 14 | 15 | permissions: 16 | sample.command.testform: 17 | default: true 18 | description: permission that allows player to test forms -------------------------------------------------------------------------------- /sample/src/tokyo/pmmp/sample/libform/Core.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * sample-libformは、libformのサンプルプラグインです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | declare(strict_types = 1); 27 | 28 | namespace tokyo\pmmp\sample\libform; 29 | 30 | use pocketmine\command\Command; 31 | use pocketmine\command\CommandSender; 32 | use pocketmine\Player; 33 | use pocketmine\plugin\PluginBase; 34 | use pocketmine\utils\TextFormat; 35 | use tokyo\pmmp\libform\element\Button; 36 | use tokyo\pmmp\libform\element\Dropdown; 37 | use tokyo\pmmp\libform\element\Input; 38 | use tokyo\pmmp\libform\element\Label; 39 | use tokyo\pmmp\libform\element\Slider; 40 | use tokyo\pmmp\libform\element\StepSlider; 41 | use tokyo\pmmp\libform\element\Toggle; 42 | use tokyo\pmmp\libform\form\ListForm; 43 | use tokyo\pmmp\libform\FormApi; 44 | 45 | /** 46 | * Class Core 47 | * @package tokyo\pmmp\sample\libform 48 | */ 49 | class Core extends PluginBase { 50 | 51 | /** @var ListForm */ 52 | private $list; 53 | 54 | public function onEnable() { 55 | FormApi::register($this);// Register a class that extends PluginBase before using it 56 | } 57 | 58 | public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool { 59 | if (!$command->testPermission($sender)) return false; 60 | if ($sender instanceof Player) { 61 | if (!empty($args[0])) { 62 | switch (strtolower($args[0])) { 63 | case 'custom': 64 | $this->customFormProcess($sender); 65 | return true; 66 | case 'list': 67 | $this->listFormProcess($sender); 68 | return true; 69 | case 'modal': 70 | $this->modalFormProcess($sender); 71 | return true; 72 | } 73 | } 74 | return false; 75 | }else { 76 | $this->getLogger()->info(TextFormat::RED . "Please use this command in game"); 77 | } 78 | return true; 79 | } 80 | 81 | /** 82 | * Create a customizable form element. 83 | * In this example, the result of the form is received in closures. 84 | * @param Player $player 85 | */ 86 | private function customFormProcess(Player $player) { 87 | FormApi::makeCustomForm(function (Player $player, ?array $response) { 88 | if (!FormApi::formCancelled($response)) { 89 | $this->getLogger()->info(TextFormat::GREEN . "CustomForm response:" . PHP_EOL . var_export($response, true)); 90 | }else { 91 | $this->getLogger()->info(TextFormat::RED . "CustomForm is cancelled!"); 92 | } 93 | }) 94 | ->addElement(new Dropdown("Dropdown", ["Awesome!", "Soso", "Bad..."])) 95 | ->addElement(new Input("Input", "placeholder", "defaultText")) 96 | ->addElement(new Label("Label")) 97 | ->addElement(new Slider("Slider", 0, 64)) 98 | ->addElement(new StepSlider("StepSlider", ["Sunny", "Cloudy", "Rainy"])) 99 | ->addElement(new Toggle("Toggle")) 100 | ->setTitle("CustomForm elements") 101 | ->sendToPlayer($player); 102 | } 103 | 104 | /** 105 | * Create a form that can list strings. 106 | + In this example, the result of the form is received by a callback function. 107 | * @param Player $player 108 | */ 109 | private function listFormProcess(Player $player) { 110 | $this->list = FormApi::makeListForm([$this, "receiveResponse"]) 111 | ->addButton(new Button("A")) 112 | ->addButton((new Button("B"))->setImage("http://example.com/example.jpg", Button::IMAGE_TYPE_URL)) 113 | ->addButton((new Button("C"))->setImage("Doesn't work this!")) 114 | ->setTitle("ListForm Buttons") 115 | ->sendToPlayer($player); 116 | } 117 | 118 | public function receiveResponse(Player $player, ?int $key) { 119 | if (!FormApi::formCancelled($key)) { 120 | $buttons = $this->list->getButtons(); 121 | $chosen = $buttons[$key]; 122 | /** @var Button $chosen */ 123 | $this->getLogger()->info(TextFormat::GREEN . "ListForm: You choose this! => {$chosen->text}"); 124 | } 125 | } 126 | 127 | /** 128 | * @param Player $player 129 | */ 130 | private function modalFormProcess(Player $player) { 131 | FormApi::makeModalForm(function (Player $player, ?bool $response) { 132 | if (!FormApi::formCancelled($response)) { 133 | if ($response) $this->listFormProcess($player); 134 | } 135 | }) 136 | ->setButtonText(true, "true") 137 | ->setButtonText(false, "false") 138 | ->setContent("Hey! Do you want to use ListForm?") 139 | ->setTitle("ModalForm") 140 | ->sendToPlayer($player); 141 | } 142 | } -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/EventListener.php: -------------------------------------------------------------------------------- 1 | getPacket(); 13 | if ($pk instanceof ModalFormResponsePacket) { 14 | $player = $event->getPlayer(); 15 | $formId = $pk->formId; 16 | $response = json_decode($pk->formData, true); 17 | if (($form = FormApi::getForm($formId)) !== null) { 18 | if (!$form->isRecipient($player)) { 19 | return false; 20 | } 21 | $callable = $form->getCallable(); 22 | if ($callable !== null) { 23 | $callable($player, $response); 24 | } 25 | FormApi::removeForm($formId); 26 | $event->setCancelled(); 27 | } 28 | } 29 | } 30 | 31 | public function onQuit(PlayerQuitEvent $event) { 32 | $player = $event->getPlayer(); 33 | foreach (FormApi::getForms() as $formId => $form) { 34 | if ($form->isRecipient($player)) { 35 | FormApi::removeForm($formId); 36 | break; 37 | } 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/FormApi.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform; 27 | 28 | // pocketmine 29 | use pocketmine\plugin\PluginBase; 30 | use pocketmine\Server; 31 | 32 | // libform 33 | use tokyo\pmmp\libform\form\CustomForm; 34 | use tokyo\pmmp\libform\form\Form; 35 | use tokyo\pmmp\libform\form\ListForm; 36 | use tokyo\pmmp\libform\form\ModalForm; 37 | 38 | /** 39 | * FormApiClass 40 | */ 41 | class FormApi { 42 | 43 | /** @var bool */ 44 | private static $activated = false; 45 | /** @var Form[] */ 46 | private static $forms = []; 47 | 48 | private function __construct() { 49 | // DONT USE THIS METHOD! 50 | } 51 | 52 | /** 53 | * @param PluginBase $plugin 54 | */ 55 | public static function register(PluginBase $plugin): void { 56 | if (!self::$activated) { 57 | Server::getInstance() 58 | ->getPluginManager() 59 | ->registerEvents(new EventListener, $plugin); 60 | } 61 | } 62 | 63 | /** 64 | * Generate a custom form 65 | * @param callable $callable 66 | * @return CustomForm 67 | */ 68 | public static function makeCustomForm(callable $callable = null): CustomForm { 69 | $formId = self::makeRandomFormId(); 70 | $form = new CustomForm($formId, $callable); 71 | if ($callable !== null) self::$forms[$formId] = $form; 72 | return $form; 73 | } 74 | 75 | /** 76 | * Generate a list form 77 | * @param callable $callable 78 | * @return ListForm 79 | */ 80 | public static function makeListForm(callable $callable = null): ListForm { 81 | $formId = self::makeRandomFormId(); 82 | $form = new ListForm($formId, $callable); 83 | if ($callable !== null) self::$forms[$formId] = $form; 84 | return $form; 85 | } 86 | 87 | /** 88 | * Generate a modal form 89 | * @param callable $callable 90 | * @return ModalForm 91 | */ 92 | public static function makeModalForm(callable $callable = null): ModalForm { 93 | $formId = self::makeRandomFormId(); 94 | $form = new ModalForm($formId, $callable); 95 | if ($callable !== null) self::$forms[$formId] = $form; 96 | return $form; 97 | } 98 | 99 | /** 100 | * Generate random formId 101 | * @return int formId 102 | */ 103 | public static function makeRandomFormId(): int { 104 | return mt_rand(0, mt_getrandmax()); 105 | } 106 | 107 | /** 108 | * @return array 109 | */ 110 | public static function getForms(): array { 111 | return self::$forms; 112 | } 113 | 114 | /** 115 | * @param int $formId 116 | * @return Form|null 117 | */ 118 | public static function getForm(int $formId): ?Form { 119 | if (array_key_exists($formId, self::$forms)) { 120 | return self::$forms[$formId]; 121 | }else { 122 | return null; 123 | } 124 | } 125 | 126 | /** 127 | * @param int $formId 128 | */ 129 | public static function removeForm(int $formId): void { 130 | if (array_key_exists($formId, self::$forms)) { 131 | unset(self::$forms[$formId]); 132 | } 133 | } 134 | 135 | /** 136 | * Check if the form has been canceled 137 | * @param mixed $response 138 | * @return bool 139 | */ 140 | public static function formCancelled($response): bool { 141 | return $response === null? true : false; 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Button.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * ButtonClass 35 | */ 36 | class Button extends Element { 37 | 38 | /** @var string */ 39 | public const IMAGE_TYPE_PATH = "path"; 40 | public const IMAGE_TYPE_URL = "url"; 41 | 42 | /** @var string */ 43 | protected $imageType = ""; 44 | /** @var string */ 45 | protected $imagePath = ""; 46 | /** @var string */ 47 | public $text = ""; 48 | 49 | public function setImage(string $imagePath, string $imageType = self::IMAGE_TYPE_PATH): Button { 50 | switch ($imageType) { 51 | case self::IMAGE_TYPE_PATH: 52 | case self::IMAGE_TYPE_URL: 53 | $this->imagePath = $imagePath; 54 | $this->imageType = $imageType; 55 | break; 56 | 57 | default: 58 | throw new \InvalidArgumentException("The image type must be an string value Button::IMAGE_TYPE_PATH or Button::IMAGE_TYPE_URL"); 59 | break; 60 | } 61 | return $this; 62 | } 63 | 64 | final public function format(): array { 65 | $data = [ 66 | Form::KEY_TEXT => $this->text 67 | ]; 68 | if (!empty($this->imageType)) { 69 | $data[Form::KEY_IMAGE] = [ 70 | Form::KEY_TYPE => $this->imageType, 71 | Form::KEY_DATA => $this->imagePath 72 | ]; 73 | } 74 | return $data; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Dropdown.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * DropdownClass 35 | */ 36 | class Dropdown extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "dropdown"; 40 | 41 | /** @var string[] */ 42 | protected $options = []; 43 | /** @var int */ 44 | protected $defaultKey = 0; 45 | 46 | public function __construct(string $text, array $options = []) { 47 | parent::__construct($text); 48 | $this->options = $options; 49 | } 50 | 51 | public function getOption(int $key): ?string { 52 | return isset($this->options[$key])? $this->options[$key] : null; 53 | } 54 | 55 | public function addOption(string $option, $isDefault = false): Dropdown { 56 | $this->defaultKey = $isDefault ? count($this->options) : $this->defaultKey; 57 | $this->options[] = $option; 58 | return $this; 59 | } 60 | 61 | public function removeOption(string $option): Dropdown { 62 | $flip = array_flip($this->options); 63 | if (array_key_exists($option, $flip)) { 64 | $key = $flip[$option]; 65 | unset($this->options[$key]); 66 | } 67 | return $this; 68 | } 69 | 70 | public function getDefault(): string { 71 | return empty($this->options)? "" : $this->options[$this->defaultKey]; 72 | } 73 | 74 | public function setDefault(string $option): Dropdown { 75 | $flip = array_flip($this->options); 76 | if (array_key_exists($option, $flip)) { 77 | $key = $flip[$option]; 78 | $this->defaultKey = $key; 79 | }else { 80 | throw new \OutOfRangeException("Invalid option " . $option); 81 | } 82 | } 83 | 84 | final public function format(): array { 85 | $data = [ 86 | Form::KEY_TYPE => self::ELEMENT_NAME, 87 | Form::KEY_TEXT => $this->text, 88 | Form::KEY_OPTIONS => $this->options, 89 | Form::KEY_DEFAULT => $this->defaultKey 90 | ]; 91 | return $data; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Element.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | /** 29 | * AbstractElementClass 30 | */ 31 | abstract class Element { 32 | 33 | /** @var string */ 34 | protected const ELEMENT_NAME = ""; 35 | 36 | /** @var string */ 37 | protected $text = ""; 38 | 39 | public function __construct(string $text) { 40 | $this->text = $text; 41 | } 42 | 43 | abstract public function format(): array; 44 | } 45 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Image.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * ImageClass 35 | */ 36 | class Image extends Element { 37 | // TODO: #BlameMojang DOES NOT WORK 38 | 39 | final public function format(): array { 40 | return []; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Input.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * InputClass 35 | */ 36 | class Input extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "input"; 40 | 41 | /** @var string */ 42 | protected $placeholder = ""; 43 | /** @var string */ 44 | protected $defaultText = ""; 45 | 46 | public function __construct(string $text, string $placeholder, string $defaultText = "") { 47 | parent::__construct($text); 48 | $this->placeholder = $placeholder; 49 | $this->defaultText = $defaultText; 50 | } 51 | 52 | final public function format(): array { 53 | $data = [ 54 | Form::KEY_TYPE => self::ELEMENT_NAME, 55 | Form::KEY_TEXT => $this->text, 56 | Form::KEY_PLACEHOLDER => $this->placeholder, 57 | Form::KEY_DEFAULT => $this->defaultText 58 | ]; 59 | return $data; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Label.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * LabelClass 35 | */ 36 | class Label extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "label"; 40 | 41 | final public function format(): array { 42 | $data = [ 43 | Form::KEY_TYPE => self::ELEMENT_NAME, 44 | Form::KEY_TEXT => $this->text 45 | ]; 46 | return $data; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Slider.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * SliderClass 35 | */ 36 | class Slider extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "slider"; 40 | 41 | /** @var number */ 42 | protected $min = 0; 43 | /** @var number */ 44 | protected $max = 0; 45 | /** @var number */ 46 | protected $step = 0; 47 | /** @var number */ 48 | protected $defaultValue; 49 | 50 | /** 51 | * @param string $text 52 | * @param number $min 53 | * @param number $max 54 | * @param int $step 55 | */ 56 | public function __construct(string $text, $min, $max, $step = 0) { 57 | if ($min > $max) throw new \InvalidArgumentException("Minimum value must be less than maximum value"); 58 | parent::__construct($text); 59 | $this->min = $min; 60 | $this->max = $max; 61 | $this->defaultValue = $min; 62 | $this->setStep($step); 63 | } 64 | 65 | public function setStep($step): Slider { 66 | if ($step < 0) throw new \InvalidArgumentException("The value of the step must be a positive value"); 67 | $this->step = $step; 68 | return $this; 69 | } 70 | 71 | public function setDefaultValue($value): Slider { 72 | if ($value < $this->min || $value > $this->max) { 73 | throw new \InvalidArgumentException("The default value must be between the minimum and maximum values"); 74 | } 75 | $this->defaultValue = $value; 76 | return $this; 77 | } 78 | 79 | final public function format(): array { 80 | $data = [ 81 | Form::KEY_TYPE => self::ELEMENT_NAME, 82 | Form::KEY_TEXT => $this->text, 83 | Form::KEY_MIN => $this->min, 84 | Form::KEY_MAX => $this->max, 85 | ]; 86 | if ($this->step > 0) $data[Form::KEY_STEP] = $this->step; 87 | if ($this->defaultValue !== $this->min) { 88 | $data[Form::KEY_DEFAULT] = $this->defaultValue; 89 | } 90 | return $data; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/StepSlider.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * StepSliderClass 35 | */ 36 | class StepSlider extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "step_slider"; 40 | 41 | /** @var string[] */ 42 | protected $steps = []; 43 | /** @var int */ 44 | protected $defaultKey = 0; 45 | 46 | public function __construct(string $text, array $steps = []) { 47 | parent::__construct($text); 48 | $this->steps = $steps; 49 | } 50 | 51 | public function getStep(int $key): ?string { 52 | return isset($this->steps[$key])? $this->steps[$key] : null; 53 | } 54 | 55 | public function addStep(string $stepText, bool $isDefault = false): StepSlider { 56 | if ($isDefault) $this->defaultKey = count($this->steps); 57 | $this->steps[] = $stepText; 58 | return $this; 59 | } 60 | 61 | public function removeStep(string $stepText): StepSlider { 62 | $flip = array_flip($this->steps); 63 | if (array_key_exists($stepText, $flip)) { 64 | $key = $flip[$stepText]; 65 | unset($this->steps[$key]); 66 | } 67 | return $this; 68 | } 69 | 70 | public function getDefaultStep(): string { 71 | return empty($this->steps)? "" : $this->steps[$this->defaultKey]; 72 | } 73 | 74 | public function setDefaultStep(string $stepText): StepSlider { 75 | $flip = array_flip($this->steps); 76 | if (array_key_exists($stepText, $flip)) { 77 | $key = $flip[$stepText]; 78 | $this->defaultKey = $key; 79 | }else { 80 | throw new \OutOfRangeException("Invalid step text " . $stepText); 81 | } 82 | return $this; 83 | } 84 | 85 | public function format(): array { 86 | $data = [ 87 | Form::KEY_TYPE => self::ELEMENT_NAME, 88 | Form::KEY_TEXT => $this->text, 89 | Form::KEY_STEPS => $this->steps, 90 | Form::KEY_DEFAULT => $this->defaultKey 91 | ]; 92 | return $data; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/element/Toggle.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\element; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | form\Form 31 | }; 32 | 33 | /** 34 | * ToggleClass 35 | */ 36 | class Toggle extends Element { 37 | 38 | /** @var string */ 39 | protected const ELEMENT_NAME = "toggle"; 40 | 41 | /** @var bool */ 42 | protected $defaultValue = false; 43 | 44 | public function __construct(string $text, bool $value = false) { 45 | parent::__construct($text); 46 | $this->defaultValue = $value; 47 | } 48 | 49 | public function setDefaultValue(bool $value): Toggle { 50 | $this->defaultValue = $value; 51 | return $this; 52 | } 53 | 54 | final public function format(): array { 55 | $data = [ 56 | Form::KEY_TYPE => self::ELEMENT_NAME, 57 | Form::KEY_TEXT => $this->text, 58 | Form::KEY_DEFAULT => $this->defaultValue 59 | ]; 60 | return $data; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/form/CustomForm.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\form; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | element\Element 31 | }; 32 | 33 | /** 34 | * CustomForm 35 | */ 36 | class CustomForm extends Form implements \JsonSerializable { 37 | 38 | /** @var string */ 39 | private const FORM_TYPE = "custom_form"; 40 | /** @var array */ 41 | protected $data = [ 42 | Form::KEY_TYPE => self::FORM_TYPE, 43 | Form::KEY_TITLE => "", 44 | Form::KEY_CONTENT => [] 45 | ]; 46 | 47 | public function getElements(): array { 48 | return $this->data[Form::KEY_CONTENT]; 49 | } 50 | 51 | public function getElement(int $key): ?Element { 52 | if (array_key_exists($key, $this->data[Form::KEY_CONTENT])) { 53 | return $this->data[Form::KEY_CONTENT][$key]; 54 | } 55 | return null; 56 | } 57 | 58 | public function addElement(Element $element): CustomForm { 59 | if (!($element instanceof Button)) {// Button is for a ListForm 60 | $this->data[Form::KEY_CONTENT][] = $element; 61 | } 62 | return $this; 63 | } 64 | 65 | final public function jsonSerialize(): array { 66 | $data = $this->data; 67 | unset($data[Form::KEY_CONTENT]); 68 | foreach ($this->getElements() as $element) { 69 | $data[Form::KEY_CONTENT][] = $element->format(); 70 | } 71 | return $data; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/form/Form.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\form; 27 | 28 | // pocketmine 29 | use pocketmine\{ 30 | Player, 31 | network\mcpe\protocol\ModalFormRequestPacket 32 | }; 33 | 34 | /** 35 | * abstractFormClass 36 | */ 37 | abstract class Form implements \JsonSerializable { 38 | 39 | /** @var string */ 40 | public const KEY_TYPE = "type"; 41 | public const KEY_CONTENT = "content"; 42 | public const KEY_DATA = "data"; 43 | public const KEY_IMAGE = "image"; 44 | public const KEY_TITLE = "title"; 45 | public const KEY_TEXT = "text"; 46 | public const KEY_OPTIONS = "options"; 47 | public const KEY_DEFAULT = "default"; 48 | public const KEY_PLACEHOLDER = "placeholder"; 49 | public const KEY_STEP = "step"; 50 | public const KEY_STEPS = "steps"; 51 | public const KEY_BUTTON1 = "button1"; 52 | public const KEY_BUTTON2 = "button2"; 53 | public const KEY_BUTTONS = "buttons"; 54 | public const KEY_MIN = "min"; 55 | public const KEY_MAX = "max"; 56 | 57 | /** @var int */ 58 | protected $id = 0; 59 | /** @var ?callable */ 60 | private $callable = null; 61 | /** @var array */ 62 | protected $data = []; 63 | /** @var string */ 64 | protected $playerName = ""; 65 | 66 | public function __construct(int $id, callable $callable = null) { 67 | $this->id = $id; 68 | $this->callable = $callable; 69 | } 70 | 71 | /** 72 | * @return int 73 | */ 74 | public function getId(): int { 75 | return $this->id; 76 | } 77 | 78 | /** 79 | * @param int $id 80 | * @return Form 81 | */ 82 | public function setId(int $id): Form { 83 | $this->id = $id; 84 | return $this; 85 | } 86 | 87 | /** 88 | * @return string 89 | */ 90 | public function getTitle(): string { 91 | return $this->data[self::KEY_TITLE]; 92 | } 93 | 94 | /** 95 | * @param string $title 96 | * @return Form 97 | */ 98 | public function setTitle(string $title): Form { 99 | $this->data[self::KEY_TITLE] = $title; 100 | return $this; 101 | } 102 | 103 | /** 104 | * @return ?callable 105 | */ 106 | public function getCallable(): ?callable { 107 | return $this->callable; 108 | } 109 | 110 | /** 111 | * @param Player $player 112 | * @return bool 113 | */ 114 | public function isRecipient(Player $player): bool { 115 | return $player->getName() === $this->playerName; 116 | } 117 | 118 | /** 119 | * @param Player $player 120 | * @return Form 121 | */ 122 | public function sendToPlayer(Player $player): Form { 123 | $pk = new ModalFormRequestPacket; 124 | $pk->formId = $this->id; 125 | $pk->formData = json_encode($this); 126 | $this->playerName = $player->getName(); 127 | $player->dataPacket($pk); 128 | return $this; 129 | } 130 | 131 | /** 132 | * @return array 133 | */ 134 | abstract public function jsonSerialize(): array; 135 | } 136 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/form/ListForm.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\form; 27 | 28 | // libform 29 | use tokyo\pmmp\libform\{ 30 | element\Button 31 | }; 32 | 33 | /** 34 | * ListFormClass 35 | */ 36 | class ListForm extends Form implements \JsonSerializable { 37 | 38 | /** @var string */ 39 | private const FORM_TYPE = "form"; 40 | /** @var array */ 41 | protected $data = [ 42 | Form::KEY_TYPE => self::FORM_TYPE, 43 | Form::KEY_TITLE => "", 44 | Form::KEY_CONTENT => "", 45 | Form::KEY_BUTTONS => [] 46 | ]; 47 | 48 | public function getContent(): string { 49 | return $this->data[Form::KEY_CONTENT]; 50 | } 51 | 52 | public function setContent(string $content): ListForm { 53 | $this->data[Form::KEY_CONTENT] = $content; 54 | return $this; 55 | } 56 | 57 | public function getButtons(): array { 58 | return $this->data[Form::KEY_BUTTONS]; 59 | } 60 | 61 | public function addButton(Button $button): ListForm { 62 | $this->data[Form::KEY_BUTTONS][] = $button; 63 | return $this; 64 | } 65 | 66 | final public function jsonSerialize(): array { 67 | $data = $this->data; 68 | if (!empty(($buttons = $this->getButtons()))) { 69 | unset($data[Form::KEY_BUTTONS]); 70 | foreach ($this->getButtons() as $button) { 71 | $data[Form::KEY_BUTTONS][] = $button->format(); 72 | } 73 | } 74 | return $data; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/tokyo/pmmp/libform/form/ModalForm.php: -------------------------------------------------------------------------------- 1 | 8 | * 9 | * This software is distributed under "MIT license". 10 | * You should have received a copy of the MIT license 11 | * along with this program. If not, see 12 | * < https://opensource.org/licenses/mit-license >. 13 | * 14 | * --------------------------------------------------------------------- 15 | * // 日本語 16 | * 17 | * libformは、フォームを簡単に操作するためのpocketmine-MP向けライブラリです 18 | * Copyright (c) 2018 yuko fuyutsuki < https://github.com/fuyutsuki > 19 | * 20 | * このソフトウェアは"MITライセンス"下で配布されています。 21 | * あなたはこのプログラムと共にMITライセンスのコピーを受け取ったはずです。 22 | * 受け取っていない場合、下記のURLからご覧ください。 23 | * < https://opensource.org/licenses/mit-license > 24 | */ 25 | 26 | namespace tokyo\pmmp\libform\form; 27 | 28 | /** 29 | * ModalFormClass 30 | */ 31 | class ModalForm extends Form implements \JsonSerializable { 32 | 33 | /** @var string */ 34 | private const FORM_TYPE = "modal"; 35 | /** @var array */ 36 | protected $data = [ 37 | Form::KEY_TYPE => self::FORM_TYPE, 38 | Form::KEY_TITLE => "", 39 | Form::KEY_CONTENT => "", 40 | Form::KEY_BUTTON1 => "", 41 | Form::KEY_BUTTON2 => "" 42 | ]; 43 | 44 | public function setButtonText(bool $button, string $text): ModalForm { 45 | if ($button) { 46 | $this->data[Form::KEY_BUTTON1] = $text; 47 | }else { 48 | $this->data[Form::KEY_BUTTON2] = $text; 49 | } 50 | return $this; 51 | } 52 | 53 | public function getContent(): string { 54 | return $this->data[Form::KEY_CONTENT]; 55 | } 56 | 57 | public function setContent(string $content): ModalForm { 58 | $this->data[Form::KEY_CONTENT] = $content; 59 | return $this; 60 | } 61 | 62 | final public function jsonSerialize(): array { 63 | return $this->data; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /virion.yml: -------------------------------------------------------------------------------- 1 | name: libform 2 | antigen: tokyo\pmmp\libform 3 | version: 0.4.0 4 | php: 5 | - 7.2 6 | author: yuko_fuyutsuki 7 | description: It is a library that can handle various forms easily. 8 | --------------------------------------------------------------------------------