|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const general = require('./utility/general') |
|
const Enum = require('./enum') |
|
const psapi = require('./psapi') |
|
const layer_util = require('./utility/layer') |
|
const thumbnail = require('./thumbnail') |
|
const ViewerObjState = { |
|
Delete: 'delete', |
|
Unlink: 'unlink', |
|
} |
|
|
|
class ViewerImage { |
|
constructor() { |
|
this.img_html = null |
|
this.thumbnail_container = null |
|
this.is_highlighted = false |
|
this.can_highlight = true |
|
this.is_active = false |
|
this.state = ViewerObjState['Unlink'] |
|
|
|
|
|
|
|
this.autoDelete = false |
|
this.viewerManager = null |
|
this.viewerObjectType |
|
this.objectId |
|
} |
|
info() { |
|
console.log('state: ', this.state) |
|
} |
|
visible(visibleOn) {} |
|
select() {} |
|
isActive() { |
|
return this.is_active |
|
} |
|
active(isActive) { |
|
if (isActive) { |
|
|
|
|
|
|
|
this.img_html.classList.add('viewerImgActive') |
|
} else { |
|
if (this.getHighlight() === false) { |
|
|
|
|
|
} else { |
|
|
|
} |
|
this.img_html.classList.remove('viewerImgActive') |
|
} |
|
this.is_active = isActive |
|
} |
|
setAutoDelete(auto_delete) { |
|
this.autoDelete = auto_delete |
|
} |
|
|
|
isSameLayer(layer_id) {} |
|
setHighlight(is_highlighted) { |
|
if (this.can_highlight) { |
|
this.is_highlighted = is_highlighted |
|
if (this.is_highlighted) { |
|
|
|
this.img_html.classList.add('viewerImgSelected') |
|
} else { |
|
this.img_html.classList.remove('viewerImgSelected') |
|
|
|
} |
|
} |
|
} |
|
getHighlight() { |
|
return this.is_highlighted |
|
} |
|
toggleHighlight() { |
|
const toggle_value = !this.getHighlight() |
|
this.setHighlight(toggle_value) |
|
|
|
|
|
} |
|
setImgHtml() {} |
|
|
|
async delete() { |
|
try { |
|
if (this.img_html) { |
|
this.img_html.remove() |
|
} |
|
if (this.thumbnail_container) { |
|
this.thumbnail_container.remove() |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.can_highlight && (this.getHighlight() || this.is_active)) { |
|
|
|
this.state = ViewerObjState['Unlink'] |
|
} else { |
|
|
|
this.state = ViewerObjState['Delete'] |
|
} |
|
|
|
if (this.autoDelete) { |
|
|
|
this.state = ViewerObjState['Delete'] |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
unlink() { |
|
|
|
try { |
|
this.img_html.remove() |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
createThumbnailNew(img, _) { |
|
this.img_html = img |
|
this.thumbnail_container = thumbnail.Thumbnail.wrapImgInContainer( |
|
img, |
|
'viewer-image-container' |
|
) |
|
thumbnail.Thumbnail.addSPButtonToContainer( |
|
this.thumbnail_container, |
|
'svg_sp_btn', |
|
'Use this as an initial image', |
|
|
|
this.useOutputImageAsInitImage, |
|
img |
|
) |
|
} |
|
useOutputImageAsInitImage = async () => { |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
await executeAsModal(async () => { |
|
await this.click(Enum.clickTypeEnum['Click']) |
|
}) |
|
|
|
const layer = layer_util.Layer.doesLayerExist(this.layer) |
|
? this.layer |
|
: await app.activeDocument.activeLayers[0] |
|
|
|
|
|
const image_info = await psapi.silentSetInitImage( |
|
layer, |
|
random_session_id |
|
) |
|
const image_name = image_info['name'] |
|
const path = `./server/python_server/init_images/${image_name}` |
|
g_viewer_manager.addInitImageLayers(layer, path, false) |
|
await g_viewer_manager.loadInitImageViewerObject(path) |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
createThumbnail(img, b_button_visible = true) { |
|
this.img_html = img |
|
|
|
this.thumbnail_container = document.createElement('div') |
|
|
|
this.thumbnail_container.className = 'viewer-image-container' |
|
|
|
const elem = document.getElementById('svg_sp_btn') |
|
|
|
|
|
const clone = elem.cloneNode(true) |
|
const button = clone |
|
button.style.display = null |
|
button.setAttribute( |
|
'title', |
|
'use this image to generate more variance like it' |
|
) |
|
|
|
|
|
|
|
button.className = 'viewer-image-button' |
|
|
|
if (!b_button_visible) { |
|
button.style.display = 'none' |
|
} |
|
button.addEventListener('click', async () => { |
|
await useOutputImageAsInitImage() |
|
}) |
|
|
|
|
|
this.thumbnail_container.appendChild(this.img_html) |
|
this.thumbnail_container.appendChild(button) |
|
|
|
|
|
} |
|
} |
|
|
|
class OutputImage extends ViewerImage { |
|
constructor(layer, path, viewer_manager) { |
|
super() |
|
this.layer = layer |
|
this.path = path |
|
this.img_html = null |
|
this.viewerManager = viewer_manager |
|
this.viewerObjectType = Enum.ViewerObjectTypeEnum['OutputImage'] |
|
this.objectId = path |
|
} |
|
async click(click_type) { |
|
console.log('click_type: ', click_type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (click_type === Enum.clickTypeEnum['Click']) { |
|
|
|
|
|
|
|
this.visible(true) |
|
await this.select(true) |
|
this.active(true) |
|
} else if (click_type === Enum.clickTypeEnum['ShiftClick']) { |
|
this.visible(true) |
|
await this.select(true) |
|
this.setHighlight(true) |
|
this.active(true) |
|
|
|
|
|
|
|
|
|
} else if (click_type === Enum.clickTypeEnum['AltClick']) { |
|
|
|
this.setHighlight(false) |
|
this.visible(false) |
|
this.active(false) |
|
|
|
await psapi.unselectActiveLayersExe() |
|
} else if (click_type === Enum.clickTypeEnum['SecondClick']) { |
|
|
|
|
|
|
|
this.visible(false) |
|
await this.select(false) |
|
this.active(false) |
|
} |
|
this.viewerManager.replaceLastSelection(click_type, this) |
|
} |
|
visible(visibleOn) { |
|
|
|
try { |
|
super.visible(visibleOn) |
|
if (layer_util.Layer.doesLayerExist(this.layer)) { |
|
this.layer.visible = visibleOn |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
async select() { |
|
|
|
super.select() |
|
if (layer_util.Layer.doesLayerExist(this.layer)) { |
|
await psapi.selectLayersExe([this.layer]) |
|
|
|
} |
|
} |
|
|
|
isSameLayer(layer_id) { |
|
super.isSameLayer(layer_id) |
|
const is_same = this.layer.id == layer_id |
|
return is_same |
|
} |
|
isSameObject(object) { |
|
if ( |
|
layer_util.Layer.doesLayerExist(this.layer) && |
|
layer_util.Layer.doesLayerExist(object.layer) |
|
) { |
|
return this.layer.id === object.layer.id |
|
} |
|
return false |
|
} |
|
|
|
setImgHtml(img_html) { |
|
super.setImgHtml() |
|
this.img_html = img_html |
|
} |
|
async delete() { |
|
try { |
|
await super.delete() |
|
|
|
if (this.state === ViewerObjState['Delete']) { |
|
await psapi.cleanLayers([this.layer]) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
info() { |
|
super.info() |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
class InitImage extends ViewerImage { |
|
constructor(init_group, init_snapshot, solid_layer, path, viewer_manager) { |
|
super() |
|
this.init_group = init_group |
|
this.init_snapshot = init_snapshot |
|
this.solid_layer = solid_layer |
|
|
|
this.path = path |
|
this.can_highlight = false |
|
this.viewerManager = viewer_manager |
|
this.viewerObjectType = Enum.ViewerObjectTypeEnum['InitImage'] |
|
this.objectId = path |
|
|
|
|
|
|
|
} |
|
async click(click_type) { |
|
if (click_type === Enum.clickTypeEnum['Click']) { |
|
|
|
|
|
|
|
this.visible(true) |
|
await this.select(true) |
|
this.active(true) |
|
click_type = Enum.clickTypeEnum['Click'] |
|
this.viewerManager.replaceLastSelection(click_type, this) |
|
} else if (click_type === Enum.clickTypeEnum['ShiftClick']) { |
|
this.visible(true) |
|
await this.select(true) |
|
this.active(true) |
|
|
|
|
|
|
|
|
|
click_type = Enum.clickTypeEnum['Click'] |
|
this.viewerManager.replaceLastSelection(click_type, this) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
visible(visibleOn) { |
|
try { |
|
super.visible(visibleOn) |
|
|
|
let visibleValues = [] |
|
if (visibleOn) { |
|
visibleValues = [true, true, true] |
|
} else { |
|
visibleValues = [false, false, false] |
|
} |
|
|
|
if (layer_util.Layer.doesLayerExist(this.init_group)) { |
|
this.init_group.visible = visibleValues[0] |
|
} |
|
if (layer_util.Layer.doesLayerExist(this.init_snapshot)) { |
|
this.init_snapshot.visible = visibleValues[1] |
|
} |
|
|
|
if (layer_util.Layer.doesLayerExist(this.solid_layer)) { |
|
this.solid_layer.visible = visibleValues[2] |
|
} |
|
|
|
if (!this.autoDelete) { |
|
|
|
|
|
if (layer_util.Layer.doesLayerExist(this.solid_layer)) { |
|
this.solid_layer.visible = false |
|
} |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async select() { |
|
super.select() |
|
|
|
const selectLayers = [] |
|
if (layer_util.Layer.doesLayerExist(this.init_group)) { |
|
selectLayers.push(this.init_group) |
|
} |
|
|
|
await psapi.selectLayersExe(selectLayers) |
|
|
|
} |
|
|
|
isSameLayer(layer_id) { |
|
super.isSameLayer(layer_id) |
|
let is_same = false |
|
if (layer_util.Layer.doesLayerExist(this.init_group)) { |
|
is_same = this.init_group.id == layer_id |
|
} |
|
return is_same |
|
} |
|
setImgHtml(img_html) { |
|
super.setImgHtml() |
|
this.img_html = img_html |
|
} |
|
async delete() { |
|
try { |
|
await super.delete() |
|
|
|
|
|
|
|
if (!this.autoDelete && !this.can_highlight) { |
|
|
|
this.state = ViewerObjState['Unlink'] |
|
} |
|
|
|
if (this.state === ViewerObjState['Delete']) { |
|
await psapi.cleanLayers([ |
|
this.init_group, |
|
this.init_snapshot, |
|
this.solid_layer, |
|
]) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
} |
|
|
|
class InitMaskImage extends ViewerImage { |
|
constructor(mask_group, white_mark, solid_black, path, viewer_manager) { |
|
super() |
|
this.mask_group = mask_group |
|
this.white_mark = white_mark |
|
this.solid_black = solid_black |
|
|
|
this.path = path |
|
this.can_highlight = false |
|
this.viewerManager = viewer_manager |
|
this.viewerObjectType = Enum.ViewerObjectTypeEnum['MaskImage'] |
|
this.objectId = path |
|
} |
|
async click(click_type) { |
|
if (click_type === Enum.clickTypeEnum['Click']) { |
|
|
|
|
|
|
|
this.visible(true) |
|
await this.select(true) |
|
this.active(true) |
|
click_type = Enum.clickTypeEnum['Click'] |
|
this.viewerManager.replaceLastSelection(click_type, this) |
|
} else if (click_type === Enum.clickTypeEnum['ShiftClick']) { |
|
this.visible(true) |
|
await this.select(true) |
|
this.active(true) |
|
|
|
|
|
|
|
|
|
click_type = Enum.clickTypeEnum['Click'] |
|
this.viewerManager.replaceLastSelection(click_type, this) |
|
} |
|
} |
|
visible(visibleOn) { |
|
try { |
|
super.visible(visibleOn) |
|
|
|
let visibleValues = [] |
|
if (visibleOn) { |
|
visibleValues = [true, true, false] |
|
} else { |
|
visibleValues = [false, false, false] |
|
} |
|
|
|
if (layer_util.Layer.doesLayerExist(this.mask_group)) { |
|
this.mask_group.visible = visibleValues[0] |
|
} |
|
if (layer_util.Layer.doesLayerExist(this.white_mark)) { |
|
this.white_mark.visible = visibleValues[1] |
|
} |
|
if (layer_util.Layer.doesLayerExist(this.solid_black)) { |
|
this.solid_black.visible = visibleValues[2] |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
async select() { |
|
super.select() |
|
|
|
const selectLayers = [] |
|
|
|
if (layer_util.Layer.doesLayerExist(this.white_mark)) { |
|
selectLayers.push(this.white_mark) |
|
} |
|
|
|
await psapi.selectLayersExe(selectLayers) |
|
|
|
} |
|
|
|
isSameLayer(layer_id) { |
|
super.isSameLayer(layer_id) |
|
let is_same = false |
|
if (layer_util.Layer.doesLayerExist(this.mask_group)) { |
|
is_same = this.mask_group.id == layer_id |
|
} |
|
return is_same |
|
} |
|
setImgHtml(img_html) { |
|
super.setImgHtml() |
|
this.img_html = img_html |
|
} |
|
async delete() { |
|
try { |
|
await super.delete() |
|
|
|
if (this.state === ViewerObjState['Delete']) { |
|
await psapi.cleanLayers([ |
|
this.mask_group, |
|
this.white_mark, |
|
this.solid_black, |
|
]) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
createThumbnailNew(img, _) { |
|
this.img_html = img |
|
this.thumbnail_container = thumbnail.Thumbnail.wrapImgInContainer( |
|
img, |
|
'viewer-image-container' |
|
) |
|
thumbnail.Thumbnail.addSPButtonToContainer( |
|
this.thumbnail_container, |
|
'svg_sp_btn', |
|
'update the mask', |
|
setMaskViewer, |
|
img |
|
) |
|
} |
|
} |
|
|
|
class initImageLayers { |
|
|
|
constructor(group, snapshot, solid_background, autoDelete) { |
|
this.group = group |
|
this.snapshot = snapshot |
|
this.solid_background = solid_background |
|
this.autoDelete = autoDelete |
|
} |
|
} |
|
class maskLayers { |
|
|
|
constructor(group, white_mark, solid_background, autoDelete) { |
|
this.group = group |
|
this.white_mark = white_mark |
|
this.solid_background = solid_background |
|
this.autoDelete = autoDelete |
|
} |
|
} |
|
class ViewerManager { |
|
|
|
|
|
constructor() { |
|
this.outputImages = [] |
|
this.initImages = [] |
|
this.initMaskImage |
|
this.pathToViewerImage = {} |
|
this.initImageLayersJson = {} |
|
|
|
this.selectedOutputImages = {} |
|
|
|
this.mask_layer |
|
this.maskLayersJson = {} |
|
|
|
|
|
this.initGroup |
|
this.init_solid_background |
|
this.maskGroup |
|
this.mask_solid_background |
|
|
|
|
|
this.last_selected_viewer_obj |
|
this.thumbnail_scaler = 1 |
|
this.isSquareThumbnail = false |
|
this.init_image_container = document.getElementById( |
|
'divInitImageViewerContainer' |
|
) |
|
} |
|
|
|
replaceLastSelection(click_type, clicked_object) { |
|
if ( |
|
this.last_selected_viewer_obj && |
|
this.last_selected_viewer_obj.objectId !== clicked_object.objectId |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
this.last_selected_viewer_obj.visible(false) |
|
this.last_selected_viewer_obj.active(false) |
|
} |
|
if (click_type === Enum.clickTypeEnum['Click']) { |
|
this.last_selected_viewer_obj = clicked_object |
|
} else if (click_type === Enum.clickTypeEnum['ShiftClick']) { |
|
if (this.last_selected_viewer_obj) { |
|
|
|
this.last_selected_viewer_obj.setHighlight(true) |
|
} |
|
this.last_selected_viewer_obj = clicked_object |
|
} else if (click_type === Enum.clickTypeEnum['AltClick']) { |
|
this.last_selected_viewer_obj = null |
|
} else if (click_type === Enum.clickTypeEnum['SecondClick']) { |
|
this.last_selected_viewer_obj = null |
|
} |
|
} |
|
initializeInitImage(group, snapshot, solid_background, path) { |
|
console.warn('this method is deprecated, use the session.js method ') |
|
this.initGroup = group |
|
this.init_solid_background = solid_background |
|
this.addInitImageLayers(snapshot, path, true) |
|
} |
|
initializeMask(group, white_mark, solid_background, path, base64) { |
|
this.maskGroup = group |
|
this.mask_solid_background = solid_background |
|
this.addMaskLayers(white_mark, path, true, base64) |
|
} |
|
addMaskLayers(white_mark, path, auto_delete, base64) { |
|
try { |
|
if (!this.maskLayersJson.hasOwnProperty(path)) { |
|
|
|
|
|
const mask_layers = new maskLayers( |
|
this.maskGroup, |
|
white_mark, |
|
this.mask_solid_background, |
|
auto_delete |
|
) |
|
this.maskLayersJson[path] = mask_layers |
|
} else { |
|
|
|
|
|
|
|
const new_path = `${path}?t=${new Date().getTime()}` |
|
console.log('new mask path: ', new_path) |
|
|
|
|
|
|
|
this.pathToViewerImage[path].img_html.src = base64ToSrc(base64) |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
updateMaskLayer() {} |
|
addInitImageLayers(snapshot, path, auto_delete) { |
|
try { |
|
if (!this.initImageLayersJson.hasOwnProperty(path)) { |
|
|
|
|
|
const init_image_layers = new initImageLayers( |
|
this.initGroup, |
|
snapshot, |
|
this.init_solid_background, |
|
auto_delete |
|
) |
|
this.initImageLayersJson[path] = init_image_layers |
|
} |
|
} catch (e) { |
|
console.warn(e) |
|
} |
|
} |
|
|
|
hasViewerImage(path) { |
|
if (this.pathToViewerImage.hasOwnProperty(path)) { |
|
return true |
|
} |
|
return false |
|
} |
|
addOutputImage(layer, path) { |
|
const outputImage = new OutputImage(layer, path, this) |
|
|
|
this.outputImages.push(outputImage) |
|
this.pathToViewerImage[path] = outputImage |
|
return outputImage |
|
} |
|
addInitImage(group, snapshot, solid_background, path, auto_delete) { |
|
const initImage = new InitImage( |
|
group, |
|
snapshot, |
|
solid_background, |
|
path, |
|
this |
|
) |
|
initImage.setAutoDelete(auto_delete) |
|
this.initImages.push(initImage) |
|
this.pathToViewerImage[path] = initImage |
|
return initImage |
|
} |
|
addMask(group, white_mark, solid_background, path) { |
|
const mask = new InitMaskImage( |
|
group, |
|
white_mark, |
|
solid_background, |
|
path, |
|
this |
|
) |
|
|
|
this.initMaskImage = mask |
|
this.pathToViewerImage[path] = mask |
|
return mask |
|
} |
|
|
|
scaleThumbnails( |
|
original_width, |
|
original_height, |
|
min_width, |
|
min_height, |
|
scaler |
|
) { |
|
|
|
|
|
const image_width = this.isSquareThumbnail |
|
? 100 |
|
: g_generation_session.last_settings.width |
|
const image_height = this.isSquareThumbnail |
|
? 100 |
|
: g_generation_session.last_settings.height |
|
|
|
const [new_width, new_height] = general.scaleToClosestKeepRatio( |
|
image_width, |
|
image_height, |
|
100, |
|
100 |
|
) |
|
const [scaled_width, scaled_height] = [ |
|
new_width * scaler, |
|
new_height * scaler, |
|
] |
|
|
|
for (let outputImage of this.outputImages) { |
|
|
|
const img = outputImage.img_html |
|
const img_container = img.parentElement |
|
|
|
img_container.style.width = scaled_width |
|
img_container.style.height = scaled_height |
|
img.style.width = scaled_width |
|
img.style.height = scaled_height |
|
|
|
} |
|
} |
|
onSessionEnd() { |
|
this.outputImages = [] |
|
this.initImages = [] |
|
this.initMaskImage = null |
|
|
|
this.pathToViewerImage = {} |
|
this.initImageLayersJson = {} |
|
|
|
this.selectedOutputImages = {} |
|
|
|
this.mask_layer = null |
|
this.maskLayersJson = {} |
|
|
|
|
|
this.initGroup = null |
|
this.init_solid_background = null |
|
this.maskGroup = null |
|
this.mask_solid_background = null |
|
|
|
|
|
this.last_selected_viewer_obj = null |
|
|
|
|
|
} |
|
|
|
async loadInitImageViewerObject(path) { |
|
if (!g_viewer_manager.hasViewerImage(path)) { |
|
const group = this.initImageLayersJson[path].group |
|
const snapshot = this.initImageLayersJson[path].snapshot |
|
const solid_background = |
|
this.initImageLayersJson[path].solid_background |
|
const auto_delete = this.initImageLayersJson[path].autoDelete |
|
const base64_image = g_generation_session.base64initImages[path] |
|
await loadInitImageViewerObject( |
|
group, |
|
snapshot, |
|
solid_background, |
|
path, |
|
auto_delete, |
|
base64_image |
|
) |
|
} |
|
} |
|
deleteAll() {} |
|
keepAll() {} |
|
keepSelected() {} |
|
deleteSelected() {} |
|
deleteInitImages() {} |
|
deleteMask() {} |
|
} |
|
|
|
module.exports = { |
|
OutputImage, |
|
InitImage, |
|
InitMaskImage, |
|
ViewerObjState, |
|
ViewerManager, |
|
} |
|
|