mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-20 23:05:15 +03:00
SonarLint fixes
This commit is contained in:
parent
801ba71258
commit
c70cb50bda
1 changed files with 118 additions and 104 deletions
|
@ -1,6 +1,84 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var LogoManager = LogoManager || {
|
(function () {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes an ImageData object and returns an MCM symbol as an array of strings.
|
||||||
|
*
|
||||||
|
* @param {ImageData} data
|
||||||
|
*/
|
||||||
|
function imageToCharacter(data) {
|
||||||
|
const char = [];
|
||||||
|
let line = "";
|
||||||
|
for (let i = 0, I = data.length; i < I; i += 4) {
|
||||||
|
const rgbPixel = data.slice(i, i + 3),
|
||||||
|
colorKey = rgbPixel.join("-");
|
||||||
|
line += this.constants.MCM_COLORMAP[colorKey]
|
||||||
|
|| this.constants.MCM_COLORMAP['default'];
|
||||||
|
if (line.length === 8) {
|
||||||
|
char.push(line);
|
||||||
|
line = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fieldSize = this.font.constants.SIZES.MAX_NVM_FONT_CHAR_FIELD_SIZE;
|
||||||
|
if (char.length < fieldSize) {
|
||||||
|
const pad = this.constants.MCM_COLORMAP['default'].repeat(4);
|
||||||
|
for (let i = 0, I = fieldSize - char.length; i < I; i++) {
|
||||||
|
char.push(pad);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return char;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes an OSD symbol as an array of strings and replaces the in-memory character at charAddress with it.
|
||||||
|
*
|
||||||
|
* @param {Array<String>} lines
|
||||||
|
* @param {Number} charAddress
|
||||||
|
*/
|
||||||
|
function replaceChar(lines, charAddress) {
|
||||||
|
const characterBits = [],
|
||||||
|
characterBytes = [];
|
||||||
|
for (let n = 0, N = lines.length; n < N; n++) {
|
||||||
|
const line = lines[n];
|
||||||
|
for (let y = 0; y < 8; y = y + 2) {
|
||||||
|
characterBits.push(parseInt(line.slice(y, y + 2), 2));
|
||||||
|
}
|
||||||
|
characterBytes.push(parseInt(line, 2));
|
||||||
|
}
|
||||||
|
this.font.data.characters[charAddress] = characterBits;
|
||||||
|
this.font.data.characters_bytes[charAddress] = characterBytes;
|
||||||
|
this.font.data.character_image_urls[charAddress] = null;
|
||||||
|
this.font.draw(charAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate image using defined constraints and display results on the UI.
|
||||||
|
*
|
||||||
|
* @param {HTMLImageElement} img
|
||||||
|
*/
|
||||||
|
function validateImage(img) {
|
||||||
|
return new Promise((resolveValidateImage, rejectValidateImage) => {
|
||||||
|
this.resetImageInfo();
|
||||||
|
for (const key in this.constraints) {
|
||||||
|
if (!this.constraints.hasOwnProperty(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const constraint = this.constraints[key],
|
||||||
|
satisfied = constraint.test(img);
|
||||||
|
if (satisfied) {
|
||||||
|
this.showConstraintSatisfied(constraint);
|
||||||
|
} else {
|
||||||
|
this.showConstraintNotSatisfied(constraint);
|
||||||
|
rejectValidateImage("Boot logo image constraint violation");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolveValidateImage();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const LogoManager = {
|
||||||
// dependencies set by init()
|
// dependencies set by init()
|
||||||
font: null,
|
font: null,
|
||||||
logoStartIndex: null,
|
logoStartIndex: null,
|
||||||
|
@ -51,9 +129,9 @@ LogoManager.init = function (font, logoStartIndex) {
|
||||||
* @param {HTMLImageElement} img
|
* @param {HTMLImageElement} img
|
||||||
*/
|
*/
|
||||||
test: img => {
|
test: img => {
|
||||||
var constraint = this.constraints.imageSize;
|
const constraint = this.constraints.imageSize;
|
||||||
if (img.width != constraint.expectedWidth
|
if (img.width !== constraint.expectedWidth
|
||||||
|| img.height != constraint.expectedHeight) {
|
|| img.height !== constraint.expectedHeight) {
|
||||||
GUI.log(i18n.getMessage("osdSetupCustomLogoImageSizeError", {
|
GUI.log(i18n.getMessage("osdSetupCustomLogoImageSizeError", {
|
||||||
width: img.width,
|
width: img.width,
|
||||||
height: img.height,
|
height: img.height,
|
||||||
|
@ -70,14 +148,14 @@ LogoManager.init = function (font, logoStartIndex) {
|
||||||
* @param {HTMLImageElement} img
|
* @param {HTMLImageElement} img
|
||||||
*/
|
*/
|
||||||
test: img => {
|
test: img => {
|
||||||
var canvas = document.createElement('canvas'),
|
const canvas = document.createElement('canvas'),
|
||||||
ctx = canvas.getContext('2d');
|
ctx = canvas.getContext('2d');
|
||||||
canvas.width = img.width;
|
canvas.width = img.width;
|
||||||
canvas.height = img.height;
|
canvas.height = img.height;
|
||||||
ctx.drawImage(img, 0, 0);
|
ctx.drawImage(img, 0, 0);
|
||||||
for (var y = 0, Y = canvas.height; y < Y; y++) {
|
for (let y = 0, Y = canvas.height; y < Y; y++) {
|
||||||
for (var x = 0, X = canvas.width; x < X; x++) {
|
for (let x = 0, X = canvas.width; x < X; x++) {
|
||||||
var rgbPixel = ctx.getImageData(x, y, 1, 1).data.slice(0, 3),
|
const rgbPixel = ctx.getImageData(x, y, 1, 1).data.slice(0, 3),
|
||||||
colorKey = rgbPixel.join("-");
|
colorKey = rgbPixel.join("-");
|
||||||
if (!this.constants.MCM_COLORMAP[colorKey]) {
|
if (!this.constants.MCM_COLORMAP[colorKey]) {
|
||||||
GUI.log(i18n.getMessage("osdSetupCustomLogoColorMapError", {
|
GUI.log(i18n.getMessage("osdSetupCustomLogoColorMapError", {
|
||||||
|
@ -101,12 +179,12 @@ LogoManager.init = function (font, logoStartIndex) {
|
||||||
this.logoStartIndex = logoStartIndex;
|
this.logoStartIndex = logoStartIndex;
|
||||||
// inject logo size variables for dynamic translation strings
|
// inject logo size variables for dynamic translation strings
|
||||||
i18n.addResources({
|
i18n.addResources({
|
||||||
logoWidthPx: "" + this.constraints.imageSize.expectedWidth,
|
logoWidthPx: "" + this.constraints.imageSize.expectedWidth, // NOSONAR
|
||||||
logoHeightPx: "" + this.constraints.imageSize.expectedHeight,
|
logoHeightPx: "" + this.constraints.imageSize.expectedHeight, // NOSONAR
|
||||||
});
|
});
|
||||||
// find/cache DOM elements
|
// find/cache DOM elements
|
||||||
Object.keys(this.elements).forEach(key => {
|
Object.keys(this.elements).forEach(key => {
|
||||||
this.elements["$" + key] = $(this.elements[key]);
|
this.elements[`$${key}`] = $(this.elements[key]);
|
||||||
});
|
});
|
||||||
Object.keys(this.constraints).forEach(key => {
|
Object.keys(this.constraints).forEach(key => {
|
||||||
this.constraints[key].$el = $(this.constraints[key].el);
|
this.constraints[key].$el = $(this.constraints[key].el);
|
||||||
|
@ -121,7 +199,7 @@ LogoManager.init = function (font, logoStartIndex) {
|
||||||
LogoManager.resetImageInfo = function () {
|
LogoManager.resetImageInfo = function () {
|
||||||
this.hideUploadHint();
|
this.hideUploadHint();
|
||||||
Object.values(this.constraints).forEach(constraint => {
|
Object.values(this.constraints).forEach(constraint => {
|
||||||
var $el = constraint.$el;
|
const $el = constraint.$el;
|
||||||
$el.toggleClass("invalid", false);
|
$el.toggleClass("invalid", false);
|
||||||
$el.toggleClass("valid", false);
|
$el.toggleClass("valid", false);
|
||||||
});
|
});
|
||||||
|
@ -149,35 +227,10 @@ LogoManager.hideUploadHint = function () {
|
||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
LogoManager.openImage = function () {
|
LogoManager.openImage = function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolveOpenImage, rejectOpenImage) => {
|
||||||
/**
|
const dialogOptions = {
|
||||||
* Validate image using defined constraints and display results on the UI.
|
|
||||||
*
|
|
||||||
* @param {HTMLImageElement} img
|
|
||||||
*/
|
|
||||||
var validateImage = img => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.resetImageInfo();
|
|
||||||
for (var key in this.constraints) {
|
|
||||||
if (!this.constraints.hasOwnProperty(key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var constraint = this.constraints[key],
|
|
||||||
satisfied = constraint.test(img);
|
|
||||||
if (satisfied) {
|
|
||||||
this.showConstraintSatisfied(constraint);
|
|
||||||
} else {
|
|
||||||
this.showConstraintNotSatisfied(constraint);
|
|
||||||
reject("Boot logo image constraint violation");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
var dialogOptions = {
|
|
||||||
type: 'openFile',
|
type: 'openFile',
|
||||||
accepts: this.acceptFileTypes
|
accepts: this.acceptFileTypes,
|
||||||
};
|
};
|
||||||
chrome.fileSystem.chooseEntry(dialogOptions, fileEntry => {
|
chrome.fileSystem.chooseEntry(dialogOptions, fileEntry => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
|
@ -185,14 +238,14 @@ LogoManager.openImage = function () {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// load and validate selected image
|
// load and validate selected image
|
||||||
var img = new Image();
|
const img = new Image();
|
||||||
img.onload = () => {
|
img.onload = () => {
|
||||||
validateImage(img)
|
validateImage.apply(this, [img])
|
||||||
.then(() => resolve(img))
|
.then(() => resolveOpenImage(img))
|
||||||
.catch(error => reject(error));
|
.catch(error => rejectOpenImage(error));
|
||||||
};
|
};
|
||||||
img.onerror = error => reject(error);
|
img.onerror = error => rejectOpenImage(error);
|
||||||
fileEntry.file(file => img.src = "file://" + file.path);
|
fileEntry.file(file => img.src = `file://${file.path}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -203,66 +256,23 @@ LogoManager.openImage = function () {
|
||||||
* @param {HTMLImageElement} img
|
* @param {HTMLImageElement} img
|
||||||
*/
|
*/
|
||||||
LogoManager.replaceLogoInFont = function (img) {
|
LogoManager.replaceLogoInFont = function (img) {
|
||||||
/**
|
|
||||||
* Takes an ImageData object and returns an MCM symbol as an array of strings.
|
|
||||||
*
|
|
||||||
* @param {ImageData} data
|
|
||||||
*/
|
|
||||||
var imageToCharacter = data => {
|
|
||||||
var char = [],
|
|
||||||
line = "";
|
|
||||||
for (var i = 0, I = data.length; i < I; i += 4) {
|
|
||||||
var rgbPixel = data.slice(i, i + 3),
|
|
||||||
colorKey = rgbPixel.join("-");
|
|
||||||
line += this.constants.MCM_COLORMAP[colorKey]
|
|
||||||
|| this.constants.MCM_COLORMAP['default'];
|
|
||||||
if (line.length == 8) {
|
|
||||||
char.push(line);
|
|
||||||
line = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var fieldSize = this.font.constants.SIZES.MAX_NVM_FONT_CHAR_FIELD_SIZE;
|
|
||||||
if (char.length < fieldSize) {
|
|
||||||
var pad = this.constants.MCM_COLORMAP['default'].repeat(4);
|
|
||||||
for (var i = 0, I = fieldSize - char.length; i < I; i++)
|
|
||||||
char.push(pad);
|
|
||||||
}
|
|
||||||
return char;
|
|
||||||
};
|
|
||||||
// takes an OSD symbol as an array of strings and replaces the in-memory character at charAddress with it
|
|
||||||
var replaceChar = (lines, charAddress) => {
|
|
||||||
var characterBits = [];
|
|
||||||
var characterBytes = [];
|
|
||||||
for (var n = 0, N = lines.length; n < N; n++) {
|
|
||||||
var line = lines[n];
|
|
||||||
for (var y = 0; y < 8; y = y + 2) {
|
|
||||||
var v = parseInt(line.slice(y, y + 2), 2);
|
|
||||||
characterBits.push(v);
|
|
||||||
}
|
|
||||||
characterBytes.push(parseInt(line, 2));
|
|
||||||
}
|
|
||||||
this.font.data.characters[charAddress] = characterBits;
|
|
||||||
this.font.data.characters_bytes[charAddress] = characterBytes;
|
|
||||||
this.font.data.character_image_urls[charAddress] = null;
|
|
||||||
this.font.draw(charAddress);
|
|
||||||
};
|
|
||||||
// loop through an image and replace font symbols
|
// loop through an image and replace font symbols
|
||||||
var canvas = document.createElement('canvas'),
|
const canvas = document.createElement('canvas'),
|
||||||
ctx = canvas.getContext('2d'),
|
ctx = canvas.getContext('2d');
|
||||||
charAddr = this.logoStartIndex;
|
let charAddr = this.logoStartIndex;
|
||||||
canvas.width = img.width;
|
canvas.width = img.width;
|
||||||
canvas.height = img.height;
|
canvas.height = img.height;
|
||||||
ctx.drawImage(img, 0, 0);
|
ctx.drawImage(img, 0, 0);
|
||||||
for (var y = 0; y < this.constants.TILES_NUM_VERT; y++) {
|
for (let y = 0; y < this.constants.TILES_NUM_VERT; y++) {
|
||||||
for (var x = 0; x < this.constants.TILES_NUM_HORIZ; x++) {
|
for (let x = 0; x < this.constants.TILES_NUM_HORIZ; x++) {
|
||||||
var imageData = ctx.getImageData(
|
const imageData = ctx.getImageData(
|
||||||
x * this.font.constants.SIZES.CHAR_WIDTH,
|
x * this.font.constants.SIZES.CHAR_WIDTH,
|
||||||
y * this.font.constants.SIZES.CHAR_HEIGHT,
|
y * this.font.constants.SIZES.CHAR_HEIGHT,
|
||||||
this.font.constants.SIZES.CHAR_WIDTH,
|
this.font.constants.SIZES.CHAR_WIDTH,
|
||||||
this.font.constants.SIZES.CHAR_HEIGHT
|
this.font.constants.SIZES.CHAR_HEIGHT
|
||||||
),
|
),
|
||||||
newChar = imageToCharacter(imageData.data);
|
newChar = imageToCharacter.apply(this, [imageData.data]);
|
||||||
replaceChar(newChar, charAddr);
|
replaceChar.apply(this, [newChar, charAddr]);
|
||||||
charAddr++;
|
charAddr++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,9 +282,13 @@ LogoManager.replaceLogoInFont = function (img) {
|
||||||
* Draw the logo using the loaded font data.
|
* Draw the logo using the loaded font data.
|
||||||
*/
|
*/
|
||||||
LogoManager.drawPreview = function () {
|
LogoManager.drawPreview = function () {
|
||||||
var $el = this.elements.$preview.empty();
|
const $el = this.elements.$preview.empty();
|
||||||
for (var i = this.logoStartIndex, I = this.font.constants.MAX_CHAR_COUNT; i < I; i++) {
|
for (let i = this.logoStartIndex, I = this.font.constants.MAX_CHAR_COUNT; i < I; i++) {
|
||||||
var url = this.font.data.character_image_urls[i];
|
const url = this.font.data.character_image_urls[i];
|
||||||
$el.append('<img src="' + url + '" title="0x' + i.toString(16) + '"></img>');
|
$el.append(`<img src="${url}" title="0x${i.toString(16)}"></img>`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
window.LogoManager = LogoManager;
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue