animation: Load sliced image using resource scale, and reload on change

Also make sure that the textures size is matching the container size.

https://bugzilla.gnome.org/show_bug.cgi?id=765011
This commit is contained in:
Marco Trevisan (Treviño) 2017-11-30 02:36:05 +01:00 committed by Jonas Ådahl
parent 26e3ccda49
commit b6ec02cef2

View File

@ -12,7 +12,12 @@ var SPINNER_ANIMATION_DELAY = 1.0;
var Animation = class { var Animation = class {
constructor(file, width, height, speed) { constructor(file, width, height, speed) {
this.actor = new St.Bin(); this.actor = new St.Bin();
this.actor.set_size(width, height);
this.actor.connect('destroy', this._onDestroy.bind(this)); this.actor.connect('destroy', this._onDestroy.bind(this));
this.actor.connect('notify::size', this._syncAnimationSize.bind(this));
this.actor.connect('resource-scale-changed',
this._loadFile.bind(this, file, width, height));
this._speed = speed; this._speed = speed;
this._isLoaded = false; this._isLoaded = false;
@ -20,10 +25,7 @@ var Animation = class {
this._timeoutId = 0; this._timeoutId = 0;
this._frame = 0; this._frame = 0;
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor; this._loadFile(file, width, height);
this._animations = St.TextureCache.get_default().load_sliced_image (file, width, height, scaleFactor,
this._animationsLoaded.bind(this));
this.actor.set_child(this._animations);
} }
play() { play() {
@ -47,6 +49,23 @@ var Animation = class {
this._isPlaying = false; this._isPlaying = false;
} }
_loadFile(file, width, height) {
let [validResourceScale, resourceScale] = this.actor.get_resource_scale();
this._isLoaded = false;
this.actor.destroy_all_children();
if (!validResourceScale)
return;
let texture_cache = St.TextureCache.get_default();
let scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
this._animations = texture_cache.load_sliced_image(file, width, height,
scaleFactor, resourceScale,
this._animationsLoaded.bind(this));
this.actor.set_child(this._animations);
}
_showFrame(frame) { _showFrame(frame) {
let oldFrameActor = this._animations.get_child_at_index(this._frame); let oldFrameActor = this._animations.get_child_at_index(this._frame);
if (oldFrameActor) if (oldFrameActor)
@ -64,9 +83,21 @@ var Animation = class {
return GLib.SOURCE_CONTINUE; return GLib.SOURCE_CONTINUE;
} }
_syncAnimationSize() {
if (!this._isLoaded)
return;
let [width, height] = this.actor.get_size();
for (let i = 0; i < this._animations.get_n_children(); ++i)
this._animations.get_child_at_index(i).set_size(width, height);
}
_animationsLoaded() { _animationsLoaded() {
this._isLoaded = this._animations.get_n_children() > 0; this._isLoaded = this._animations.get_n_children() > 0;
this._syncAnimationSize();
if (this._isPlaying) if (this._isPlaying)
this.play(); this.play();
} }