|
| 1 | +/* |
| 2 | + Copyright (C) 2022-2023 Xortrox dpeGit |
| 3 | +*/ |
| 4 | +/* |
| 5 | + This file is part of GenLite. |
| 6 | +
|
| 7 | + GenLite is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + GenLite is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | +
|
| 11 | + You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>. |
| 12 | +*/ |
| 13 | + |
| 14 | + |
| 15 | +import { GenLitePlugin } from '../core/interfaces/plugin.class'; |
| 16 | + |
| 17 | +export class GenliteFilterPlugin extends GenLitePlugin { |
| 18 | + static pluginName = 'GenliteFilterPlugin'; |
| 19 | + |
| 20 | + isPluginEnabled: boolean = false; |
| 21 | + |
| 22 | + filters = { |
| 23 | + brightness: 1, |
| 24 | + contrast: 1, |
| 25 | + saturate: 1, |
| 26 | + grayscale: 0, |
| 27 | + invert: 0, |
| 28 | + sepia: 0 |
| 29 | + } |
| 30 | + |
| 31 | + elementsToFilter: HTMLCollectionOf<HTMLCanvasElement>; |
| 32 | + |
| 33 | + pluginSettings: Settings = { |
| 34 | + "Brightness (default 1)": { |
| 35 | + type: "range", |
| 36 | + value: this.filters.brightness, |
| 37 | + stateHandler: this.handleBrightness.bind(this), |
| 38 | + min: 0, |
| 39 | + max: 2, |
| 40 | + step: 0.1 |
| 41 | + }, |
| 42 | + "Contrast (default 1)": { |
| 43 | + type: "range", |
| 44 | + value: this.filters.contrast, |
| 45 | + stateHandler: this.handleContrast.bind(this), |
| 46 | + min: 0, |
| 47 | + max: 2, |
| 48 | + step: 0.1 |
| 49 | + }, |
| 50 | + "Saturate (default 1)": { |
| 51 | + type: "range", |
| 52 | + value: this.filters.saturate, |
| 53 | + stateHandler: this.handleSaturate.bind(this), |
| 54 | + min: 0, |
| 55 | + max: 2, |
| 56 | + step: 0.1 |
| 57 | + }, |
| 58 | + "Grayscale (default 0)": { |
| 59 | + type: "range", |
| 60 | + value: this.filters.grayscale, |
| 61 | + stateHandler: this.handleGrayscale.bind(this), |
| 62 | + min: 0, |
| 63 | + max: 1, |
| 64 | + step: 0.1 |
| 65 | + }, |
| 66 | + "Invert (default 0)": { |
| 67 | + type: "range", |
| 68 | + value: this.filters.invert, |
| 69 | + stateHandler: this.handleInvert.bind(this), |
| 70 | + min: 0, |
| 71 | + max: 1, |
| 72 | + step: 0.1 |
| 73 | + }, |
| 74 | + "Sepia (default 0)": { |
| 75 | + type: "range", |
| 76 | + value: this.filters.sepia, |
| 77 | + stateHandler: this.handleSepia.bind(this), |
| 78 | + min: 0, |
| 79 | + max: 1, |
| 80 | + step: 0.1 |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + async init() { |
| 85 | + document.genlite.registerPlugin(this); |
| 86 | + this.elementsToFilter = document.getElementsByTagName("canvas"); |
| 87 | + } |
| 88 | + |
| 89 | + async postInit() { |
| 90 | + document.genlite.ui.registerPlugin("Camera Filters", null, this.handlePluginState.bind(this), this.pluginSettings); |
| 91 | + } |
| 92 | + |
| 93 | + handlePluginState(state: boolean): void { |
| 94 | + this.isPluginEnabled = state; |
| 95 | + if (state) { |
| 96 | + this.loginOK(); |
| 97 | + } else { |
| 98 | + for(let element of Object.keys(this.elementsToFilter)) |
| 99 | + this.elementsToFilter[element].style.filter = ''; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + handleBrightness(value) { |
| 104 | + this.filters.brightness = value; |
| 105 | + this.updateFilters(); |
| 106 | + } |
| 107 | + |
| 108 | + handleContrast(value) { |
| 109 | + this.filters.contrast = value; |
| 110 | + this.updateFilters(); |
| 111 | + } |
| 112 | + |
| 113 | + handleSaturate(value) { |
| 114 | + this.filters.saturate = value; |
| 115 | + this.updateFilters(); |
| 116 | + } |
| 117 | + |
| 118 | + handleGrayscale(value) { |
| 119 | + this.filters.grayscale = value; |
| 120 | + this.updateFilters(); |
| 121 | + } |
| 122 | + |
| 123 | + handleInvert(value) { |
| 124 | + this.filters.invert = value; |
| 125 | + this.updateFilters(); |
| 126 | + } |
| 127 | + |
| 128 | + handleSepia(value) { |
| 129 | + this.filters.sepia = value; |
| 130 | + this.updateFilters(); |
| 131 | + } |
| 132 | + |
| 133 | + updateFilters() { |
| 134 | + if(!this.isPluginEnabled) |
| 135 | + return; |
| 136 | + let filterString = ""; |
| 137 | + for (let filter of Object.keys(this.filters)) { |
| 138 | + filterString += ` ${filter}(${this.filters[filter]})` |
| 139 | + } |
| 140 | + for(let element of Object.keys(this.elementsToFilter)) |
| 141 | + this.elementsToFilter[element].style.filter = filterString; |
| 142 | + } |
| 143 | + |
| 144 | + loginOK(): void { |
| 145 | + /* mod to the game canvas */ |
| 146 | + this.elementsToFilter[3].style.position = 'absolute'; |
| 147 | + this.elementsToFilter[3].style.zIndex = '-1'; |
| 148 | + this.updateFilters(); |
| 149 | + } |
| 150 | +} |
0 commit comments