♻️ Add base classes for the effects and shaders

This commit is contained in:
Simon Schneegans
2022-05-13 20:39:45 +02:00
parent 1ccc34f766
commit cb4b87811e
17 changed files with 955 additions and 1349 deletions
+64 -116
View File
@@ -20,6 +20,7 @@ const _ = imports.gettext.domain('burn-my-windows').gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const utils = Me.imports.src.utils;
const Effect = Me.imports.src.Effect.Effect;
//////////////////////////////////////////////////////////////////////////////////////////
// This effect is a homage to the good old Compiz days. However, it is implemented //
@@ -29,23 +30,15 @@ const utils = Me.imports.src.utils;
// there are a couple of moving gradients which fade-in or fade-out the fire effect. //
//////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect is registered further down in this file. When this
// effect is used for the first time, an instance of this shader class is created. Once
// the effect is finished, the shader will be stored in the freeShaders array and will
// then be reused if a new shader is requested. ShaderClass which will be used whenever
// this effect is used.
let ShaderClass = null;
let freeShaders = [];
// The effect class is completely static. It can be used to get some metadata (like the
// effect's name or supported GNOME Shell versions), to initialize the respective page of
// the settings dialog, as well as to create the actual shader for the effect.
var Fire = class Fire {
// The effect class can be used to get some metadata (like the effect's name or supported
// GNOME Shell versions), to initialize the respective page of the settings dialog, as
// well as to create the actual shader for the effect.
var Fire = class Fire extends Effect {
// ---------------------------------------------------------------------------- metadata
// The effect is available on all GNOME Shell versions supported by this extension.
static getMinShellVersion() {
getMinShellVersion() {
return [3, 36];
}
@@ -53,22 +46,21 @@ var Fire = class Fire {
// required. It should match the prefix of the settings keys which store whether the
// effect is enabled currently (e.g. '*-close-effect'), and its animation time
// (e.g. '*-animation-time').
static getNick() {
getNick() {
return 'fire';
}
// This will be shown in the sidebar of the preferences dialog as well as in the
// drop-down menus where the user can choose the effect.
static getLabel() {
getLabel() {
return _('Fire');
}
// -------------------------------------------------------------------- API for prefs.js
// This is called by the preferences dialog. It loads the settings page for this effect,
// binds all properties to the settings and appends the page to the main stack of the
// preferences dialog.
static getPreferences(dialog) {
// and binds all properties to the settings.
getPreferences(dialog) {
// Add the settings page to the builder.
dialog.getBuilder().add_from_resource(`/ui/${utils.getGTKString()}/Fire.ui`);
@@ -94,7 +86,7 @@ var Fire = class Fire {
});
// Initialize the fire-preset dropdown.
Fire._createFirePresets(dialog);
this._createFirePresets(dialog);
// Finally, return the new settings page.
return dialog.getBuilder().get_object('fire-prefs');
@@ -102,40 +94,67 @@ var Fire = class Fire {
// ---------------------------------------------------------------- API for extension.js
// This is called from extension.js whenever a window is opened or closed with this
// effect. It returns an instance of the shader class, trying to reuse previously
// created shaders.
static getShader(actor, settings, forOpening) {
let shader;
// This is called by the effect's base class whenever a new shader is required. Since
// this shader depends on classes by GNOME Shell, we register it locally in this method
// as this file is also included from the preferences dialog where those classes would
// not be available.
createShader() {
if (freeShaders.length == 0) {
shader = new ShaderClass();
} else {
shader = freeShaders.pop();
// Only register the shader class when this method is called for the first time.
if (!this._ShaderClass) {
const Clutter = imports.gi.Clutter;
const Shader = Me.imports.src.Shader.Shader;
this._ShaderClass = GObject.registerClass({}, class ShaderClass extends Shader {
// We use the constructor of the shader to store all required uniform locations.
_init(effect) {
super._init(effect);
this._uGradient = [
this.get_uniform_location('uGradient1'),
this.get_uniform_location('uGradient2'),
this.get_uniform_location('uGradient3'),
this.get_uniform_location('uGradient4'),
this.get_uniform_location('uGradient5'),
];
this._u3DNoise = this.get_uniform_location('u3DNoise');
this._uScale = this.get_uniform_location('uScale');
this._uMovementSpeed = this.get_uniform_location('uMovementSpeed');
}
// This is called once each time the shader is used. This can be used to retrieve
// the configuration from the settings and update all uniforms accordingly.
beginAnimation(actor, settings, forOpening) {
super.beginAnimation(actor, settings, forOpening);
// Load the gradient values from the settings.
for (let i = 1; i <= 5; i++) {
const c =
Clutter.Color.from_string(settings.get_string('fire-color-' + i))[1];
this.set_uniform_float(
this._uGradient[i - 1], 4,
[c.red / 255, c.green / 255, c.blue / 255, c.alpha / 255]);
}
// clang-format off
this.set_uniform_float(this._u3DNoise, 1, [settings.get_boolean('flame-3d-noise')]);
this.set_uniform_float(this._uScale, 1, [settings.get_double('flame-scale')]);
this.set_uniform_float(this._uMovementSpeed, 1, [settings.get_double('flame-movement-speed')]);
// clang-format on
}
});
}
shader.setUniforms(actor, settings, forOpening);
return shader;
}
// The getActorScale() is called from extension.js to adjust the actor's size during the
// animation. This is useful if the effect requires drawing something beyond the usual
// bounds of the actor. This only works for GNOME 3.38+.
static getActorScale(settings) {
return {x: 1.0, y: 1.0};
}
// This is called from extension.js if the extension is disabled. This should free all
// static resources.
static cleanUp() {
freeShaders = [];
// Finally, return a new instance of the shader class.
return new this._ShaderClass(this);
}
// ----------------------------------------------------------------------- private stuff
// This populates the preset dropdown menu for the fire options.
static _createFirePresets(dialog) {
_createFirePresets(dialog) {
dialog.getBuilder().get_object('fire-prefs').connect('realize', (widget) => {
const presets = [
{
@@ -221,74 +240,3 @@ var Fire = class Fire {
});
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// The shader class for this effect will only be registered in GNOME Shell's process //
// (not in the preferences process). It's done this way as Clutter may not be installed //
// on the system and therefore the preferences would crash. //
//////////////////////////////////////////////////////////////////////////////////////////
if (utils.isInShellProcess()) {
const {Clutter, Shell} = imports.gi;
ShaderClass = GObject.registerClass({}, class ShaderClass extends Shell.GLSLEffect {
// This is called when the effect is used for the first time. This can be used to
// store all required uniform locations.
_init() {
super._init();
this._uGradient = [
this.get_uniform_location('uGradient1'),
this.get_uniform_location('uGradient2'),
this.get_uniform_location('uGradient3'),
this.get_uniform_location('uGradient4'),
this.get_uniform_location('uGradient5'),
];
this._u3DNoise = this.get_uniform_location('u3DNoise');
this._uScale = this.get_uniform_location('uScale');
this._uMovementSpeed = this.get_uniform_location('uMovementSpeed');
}
// This is called each time the effect is used. This can be used to retrieve the
// configuration from the settings and update all uniforms accordingly.
setUniforms(actor, settings, forOpening) {
// Load the gradient values from the settings.
for (let i = 1; i <= 5; i++) {
const c = Clutter.Color.from_string(settings.get_string('fire-color-' + i))[1];
this.set_uniform_float(this._uGradient[i - 1], 4,
[c.red / 255, c.green / 255, c.blue / 255, c.alpha / 255]);
}
// clang-format off
this.set_uniform_float(this._u3DNoise, 1, [settings.get_boolean('flame-3d-noise')]);
this.set_uniform_float(this._uScale, 1, [settings.get_double('flame-scale')]);
this.set_uniform_float(this._uMovementSpeed, 1, [settings.get_double('flame-movement-speed')]);
// clang-format on
}
// This is called by extension.js when the shader is not used anymore. We will store
// this instance of the shader so that it can be re-used in th future.
free() {
freeShaders.push(this);
}
// This is called by the constructor. This means, it's only called when the effect
// is used for the first time.
vfunc_build_pipeline() {
const code = utils.loadGLSLResource(`/shaders/${Fire.getNick()}.glsl`);
// Match anything between the curly brackets of "void main() {...}".
const regex = RegExp('void main *\\(\\) *\\{([\\S\\s]+)\\}');
const match = regex.exec(code);
const declarations = code.substr(0, match.index);
const main = match[1];
this.add_glsl_snippet(Shell.SnippetHook.FRAGMENT, declarations, main, true);
}
});
}