Skip to content

Commit

Permalink
fix delay to create directory
Browse files Browse the repository at this point in the history
  • Loading branch information
zehfernandes committed Jan 17, 2018
1 parent f458183 commit ff61dc4
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 564 deletions.
Binary file modified app/assets/icon.icns
Binary file not shown.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<meta charset="UTF-8">
<title>Select the mockup theme</title>
<title>Screeener</title>
<link rel="stylesheet" href="./node_modules/tachyons/css/tachyons.min.css">
<link rel="stylesheet" href="./static/css/photon-overrides.css">
</head>
Expand Down
196 changes: 109 additions & 87 deletions lib/checkPNG.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,136 @@
const _ = require('lodash')
const Jimp = require('jimp')
const path = require('path')
const { thumbsPath } = require('./getTemplates.js')


const _ = require("lodash");
const Jimp = require("jimp");
const path = require("path");
const { thumbsPath } = require("./getTemplates.js");

function getAlphaInfos(filePath) {
return new Promise((resolve, reject) => {
var areaAlpha = []
var countRow = 0
var countCol = 0
var lastY, lastIndex, lastX
var imageIndex = 0

Jimp.read(filePath, function (err, image) {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {

var alpha = this.bitmap.data[idx + 3]
var areaAlpha = [];
var countRow = 0;
var countCol = 0;
var lastY, lastIndex, lastX;
var imageIndex = 0;

Jimp.read(filePath, function(err, image) {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(
x,
y,
idx
) {
var alpha = this.bitmap.data[idx + 3];

if (alpha === 0) {
// Perform checks
if (distanceTo({ x: x, y: y }, { x: lastX, y: lastY }) > 2) {
if (distanceTo({ x: x, y: y }, { x: areaAlpha[imageIndex][countRow][0].x, y: areaAlpha[imageIndex][countRow][0].y }) > 2) {
imageIndex = findIndexArray(x, y, areaAlpha)
if (
distanceTo(
{ x: x, y: y },
{
x: areaAlpha[imageIndex][countRow][0].x,
y: areaAlpha[imageIndex][countRow][0].y
}
) > 2
) {
imageIndex = findIndexArray(x, y, areaAlpha);
}
}

if ((lastIndex !== imageIndex)) {
if (typeof areaAlpha[imageIndex] === 'undefined') {
lastY = undefined
countCol = 0
countRow = 0
areaAlpha[imageIndex] = new Array()
areaAlpha[imageIndex][countRow] = new Array()
if (lastIndex !== imageIndex) {
if (typeof areaAlpha[imageIndex] === "undefined") {
lastY = undefined;
countCol = 0;
countRow = 0;
areaAlpha[imageIndex] = new Array();
areaAlpha[imageIndex][countRow] = new Array();
} else {
countRow = areaAlpha[imageIndex].length - 1
countCol = areaAlpha[imageIndex][countRow].length - 1
countRow = areaAlpha[imageIndex].length - 1;
countCol = areaAlpha[imageIndex][countRow].length - 1;

lastY = areaAlpha[imageIndex][countRow][countCol].y
lastY = areaAlpha[imageIndex][countRow][countCol].y;
}
}

if ((lastY !== y) && (lastY !== undefined)) {
countRow = countRow + 1
countCol = 0
areaAlpha[imageIndex][countRow] = new Array()
if (lastY !== y && lastY !== undefined) {
countRow = countRow + 1;
countCol = 0;
areaAlpha[imageIndex][countRow] = new Array();
}

areaAlpha[imageIndex][countRow][countCol] = { x: x, y: y }
countCol = countCol + 1
areaAlpha[imageIndex][countRow][countCol] = { x: x, y: y };
countCol = countCol + 1;

lastY = y
lastX = x
lastIndex = imageIndex
lastY = y;
lastX = x;
lastIndex = imageIndex;
}
})
});

resolve(areaAlpha)
})
})
resolve(areaAlpha);
});
});
}

function getMockupInfos(filePath) {
return new Promise((resolve, reject) => {
Jimp.read(filePath, function (err, image) {
Jimp.read(filePath, function(err, image) {
resolve({
width: image.bitmap.width,
height: image.bitmap.height,
x: 0,
y: 0,
})
})
})
y: 0
});
});
});
}

function extractData(data) {
return {
width: data[0].length,
height: data.length,
x: data[0][0].x,
y: data[0][0].y,
}
y: data[0][0].y
};
}

function generatePreviewImages(data, filePath, name) {
return new Promise((resolve, reject) => {
let color = Jimp.rgbaToInt(80, 227, 194, 255);
Jimp.read(filePath, function (err, image) {
image.brightness(-0.8)
Jimp.read(filePath, function(err, image) {
image.brightness(-0.8);

for (let i = 0; i < data.length; i++) {
for (let a = 0; a < data[i].length; a++) {
image.setPixelColor(color, parseInt(data[i][a].x), parseInt(data[i][a].y))
image.setPixelColor(
color,
parseInt(data[i][a].x),
parseInt(data[i][a].y)
);
}
}

image.write(`${thumbsPath}/${name}.png`)
image.write(`${thumbsPath}/${name}.png`);

const objData = Object.assign(extractData(data), { path: `${thumbsPath}/${name}.png` })
resolve(objData)
})
})
const objData = Object.assign(extractData(data), {
path: `${thumbsPath}/${name}.png`
});
resolve(objData);
});
});
}

function getImageData(alphaInfo, filePath) {
return new Promise((resolve, reject) => {
let pathFile = []
let pathFile = [];

for (let i = 0; i < alphaInfo.length; i++) {
const name = `${path.basename(filePath).slice(0, -4)}-${i}`
pathFile.push( generatePreviewImages(alphaInfo[i], filePath, name) )
const name = `${path.basename(filePath).slice(0, -4)}-${i}`;
pathFile.push(generatePreviewImages(alphaInfo[i], filePath, name));
}

Promise.all(pathFile).then((imagesData) => {
resolve(imagesData)
})

})
Promise.all(pathFile).then(imagesData => {
resolve(imagesData);
});
});
}

/* -----------
Expand All @@ -125,53 +139,61 @@ function getImageData(alphaInfo, filePath) {

function constructData(filePath) {
return new Promise((resolve, reject) => {
getAlphaInfos(filePath).then((alphaInfo) => {

let promises = [getMockupInfos(filePath), getImageData(alphaInfo, filePath)]
Promise.all(promises).then((values) => {
const mockupObj = values[0]
const imageObj = values[1]
getAlphaInfos(filePath).then(alphaInfo => {
let promises = [
getMockupInfos(filePath),
getImageData(alphaInfo, filePath)
];
Promise.all(promises).then(values => {
const mockupObj = values[0];
const imageObj = values[1];

resolve({
mockup: mockupObj,
images: imageObj,
})
})
})
})
images: imageObj
});
});
});
});
}

module.exports = {
getImageInfo: constructData
}
};

/* -----------
Helpers
--------- */

function distanceTo(from, to) {
var distance = Math.sqrt((Math.pow(from.x - to.x, 2)) + (Math.pow(from.y - to.y, 2)))
return distance
var distance = Math.sqrt(
Math.pow(from.x - to.x, 2) + Math.pow(from.y - to.y, 2)
);
return distance;
}

function findIndexArray(x, y, areaAlpha) {
let imageIndex = areaAlpha.length
let imageIndex = areaAlpha.length;

areaAlpha.forEach((obj, i) => {
let arrCheck = _.flatten(obj[[obj.length - 1]])
let arrCheck = _.flatten(obj[[obj.length - 1]]);

let maxY = _.maxBy(arrCheck, function (o) { return o.y })
let maxX = _.maxBy(arrCheck, function (o) { return o.x })
let maxY = _.maxBy(arrCheck, function(o) {
return o.y;
});
let maxX = _.maxBy(arrCheck, function(o) {
return o.x;
});

let arrDist = [
distanceTo(maxY, { x: x, y: y }),
distanceTo(maxX, { x: x, y: y }),
]
distanceTo(maxX, { x: x, y: y })
];

if (Math.min(...arrDist) < 2) {
imageIndex = i
imageIndex = i;
}
})
});

return imageIndex
return imageIndex;
}
58 changes: 31 additions & 27 deletions lib/getTemplates.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
const path = require('path')
const fs = require('fs')
const electron = require('electron')
const decache = require('decache')
const path = require("path");
const fs = require("fs");
const electron = require("electron");
const decache = require("decache");

const userDataPath = (electron.app || electron.remote.app)
.getPath('userData')
const thumbsPath = `${userDataPath}/templates/thumbs`
const mockupsPath = `${userDataPath}/templates/mockups`
const userDataPath = (electron.app || electron.remote.app).getPath("userData");
const thumbsPath = `${userDataPath}/templates/thumbs`;
const mockupsPath = `${userDataPath}/templates/mockups`;

if (!fs.existsSync(`${userDataPath}/templates`)) fs.mkdir(`${userDataPath}/templates`)
if (!fs.existsSync(`${mockupsPath}`)) fs.mkdir(mockupsPath)
if (!fs.existsSync(`${thumbsPath}`)) fs.mkdir(thumbsPath)
if (!fs.existsSync(`${userDataPath}`)) fs.mkdir(`${userDataPath}`);
if (!fs.existsSync(`${userDataPath}/templates`))
fs.mkdir(`${userDataPath}/templates`);
if (!fs.existsSync(`${mockupsPath}`)) fs.mkdir(mockupsPath);
if (!fs.existsSync(`${thumbsPath}`)) fs.mkdir(thumbsPath);

function getAllTemplatesFiles() {
return fs
.readdirSync(`${userDataPath}/templates`)
.filter(fileName => /\.(json)/.test(fileName))
.filter(fileName => /\.(json)/.test(fileName));
}

function createTemplateDirectory() {
fs.mkdirSync(`${userDataPath}/templates`)
fs.mkdirSync(thumbsPath)
fs.mkdirSync(mockupsPath)
fs.mkdirSync(`${userDataPath}/templates`);
fs.mkdirSync(thumbsPath);
fs.mkdirSync(mockupsPath);
}

function getLoadTemplateObj() {
return new Promise((resolve, reject) => {
const templates = getAllTemplatesFiles()
const obj = templates.map((filePath) => {
decache(`${userDataPath}/templates/${filePath}`)
let content = require(`${userDataPath}/templates/${filePath}`)
content.fileName = filePath
const templates = getAllTemplatesFiles();
const obj = templates.map(filePath => {
decache(`${userDataPath}/templates/${filePath}`);
let content = require(`${userDataPath}/templates/${filePath}`);
content.fileName = filePath;
if (!path.isAbsolute(content.mockup.path)) {
content.mockup.path = path.join(`${userDataPath}/templates/`, content.mockup.path)
content.mockup.path = path.join(
`${userDataPath}/templates/`,
content.mockup.path
);
}
return content
})
return content;
});

resolve({ 'yourMockups': obj })
})
resolve({ yourMockups: obj });
});
}

module.exports = {
getAllTemplatesFiles,
userDataPath,
thumbsPath,
mockupsPath,
getLoadTemplateObj,
}
getLoadTemplateObj
};
Loading

0 comments on commit ff61dc4

Please sign in to comment.