Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to export a scaled spritesheet. #300

Merged
merged 10 commits into from
Sep 29, 2015
23 changes: 22 additions & 1 deletion src/css/settings-export.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,25 @@
-moz-box-sizing:border-box;
background: rgba(0,0,0,0.5);
color: white;
}
}

.scaling-factor {
margin-bottom: 10px;
}

.scaling-factor-input {
margin: 5px;
vertical-align: middle;
width: 145px;
}

.scaling-factor-text {
height: 31px;
display: inline-block;
line-height: 30px;
width: 40px;
border: 1px solid grey;
box-sizing: border-box;
border-radius: 3px;
text-align: center;
}
1 change: 0 additions & 1 deletion src/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@

.settings-description {
margin : 0 0 10px 0;
display : inline-block;
}

.settings-form-section {
Expand Down
24 changes: 24 additions & 0 deletions src/js/controller/settings/exportimage/ImageExportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
this.gifExportController = new ns.GifExportController(piskelController);
};

pskl.utils.inherit(ns.ImageExportController, pskl.controller.settings.AbstractSettingController);

ns.ImageExportController.prototype.init = function () {
// Output Scaling Factor
var scalingFactorInput = document.querySelector('.scaling-factor-input');
scalingFactorInput.value = pskl.UserSettings.get(pskl.UserSettings.EXPORT_SCALING);
this.addEventListener(scalingFactorInput, 'change', this.onScalingFactorChange_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a listener on the 'input' event as well :

    this.addEventListener(scalingFactorInput, 'change', this.onScalingFactorChange_);
    this.addEventListener(scalingFactorInput, 'input', this.onScalingFactorChange_);

On browsers that support the 'input' event properly on range inputs, this will fire everytime the value is updated, so you will see the number change in real time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure the event listeners added are removed when the panel is closed. This is handled by pskl.controller.settings.AbstractSettingController#destroy . PngExportController and GifExportController both extend AbstractSettingController without overriding the destroy method. All their events are automatically cleaned up.

Here you can either add a call to this.superclass.destroy.call(this); at the end of the destroy method. Or you can move the event's logic to the PngExportController.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, went with the superclass call for now.

this.updateScalingFactorText_(scalingFactorInput.value);

this.pngExportController.init();
this.gifExportController.init();
};
Expand All @@ -16,4 +24,20 @@
this.pngExportController.destroy();
this.gifExportController.destroy();
};

ns.ImageExportController.prototype.onScalingFactorChange_ = function (evt) {
var target = evt.target;
var value = Math.round(parseFloat(target.value));
if (!isNaN(value)) {
this.updateScalingFactorText_(value);
pskl.UserSettings.set(pskl.UserSettings.EXPORT_SCALING, value);
} else {
target.value = pskl.UserSettings.get(pskl.UserSettings.EXPORT_SCALING);
}
};

ns.ImageExportController.prototype.updateScalingFactorText_ = function (scale) {
var scalingFactorText = document.querySelector('.scaling-factor-text');
scalingFactorText.innerHTML = scale + 'x';
};
})();
12 changes: 11 additions & 1 deletion src/js/controller/settings/exportimage/PngExportController.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@

ns.PngExportController.prototype.onPngDownloadButtonClick_ = function (evt) {
var fileName = this.getPiskelName_() + '.png';
pskl.utils.BlobUtils.canvasToBlob(this.getFramesheetAsCanvas(), function(blob) {

var outputCanvas = this.getFramesheetAsCanvas();

var scalingFactor = pskl.UserSettings.get(pskl.UserSettings.EXPORT_SCALING);
if (scalingFactor > 1) {
var width = outputCanvas.width * scalingFactor;
var height = outputCanvas.height * scalingFactor;
outputCanvas = pskl.utils.ImageResizer.resize(outputCanvas, width, height, false);
}

pskl.utils.BlobUtils.canvasToBlob(outputCanvas, function(blob) {
pskl.utils.FileUtils.downloadAsFile(blob, fileName);
});
};
Expand Down
4 changes: 3 additions & 1 deletion src/js/utils/UserSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ONION_SKIN : 'ONION_SKIN',
LAYER_PREVIEW : 'LAYER_PREVIEW',
LAYER_OPACITY : 'LAYER_OPACITY',
EXPORT_SCALING: 'EXPORT_SCALING',

KEY_TO_DEFAULT_VALUE_MAP_ : {
'GRID_WIDTH' : 0,
Expand All @@ -24,7 +25,8 @@
'TILED_PREVIEW' : false,
'ONION_SKIN' : false,
'LAYER_OPACITY' : 0.2,
'LAYER_PREVIEW' : true
'LAYER_PREVIEW' : true,
'EXPORT_SCALING' : 1
},

/**
Expand Down
11 changes: 8 additions & 3 deletions src/templates/settings/export.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
Export Spritesheet
</div>
<div class="settings-item">
<span class="settings-description">PNG with all frames side by side.</span>
<div class="settings-description">PNG with all frames side by side.</div>
<div class="settings-description"><label for="scaling-factor">Output Scaling Factor</label></div>
<div class="scaling-factor">
<input type="range" class="scaling-factor-input" name="scaling-factor" min="1" max="32" step="1"/>
<span class="scaling-factor-text"></span>
</div>
<button type="button" class="button button-primary png-download-button">Download PNG</button>
</div>
<div class="settings-title">
Export as ZIP
</div>
<div class="settings-item">
<span class="settings-description">ZIP with one PNG file per frame.</span>
<span class="settings-description" style="display:block">File names will start with the prefix below.</span>
<div class="settings-description">ZIP with one PNG file per frame.</div>
<div class="settings-description">File names will start with the prefix below.</div>
<div class="settings-item">
<label>Prefix</label>
<input class="zip-prefix-name textfield" type="text" placeholder="PNG file prefix ...">
Expand Down