<commit_before>// This file is required by the application.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.

// Imports
const electron = require('electron')
const modal = new (require('vanilla-modal').default)()
const editor = require('./editor.js')
const controller = require('./controller.js')
const network = require('./network.js')
const settings = electron.remote.require('./main-process/settings')
const path = require('path')
const fs = require('fs-extra')

let project
let view

exports.init = function() {
	project = electron.remote.getGlobal('project').project

	// Window input events
	window.onkeydown = keyDown
	window.onkeyup = keyUp
	window.onbeforeunload = beforeUnload

	// DOM listeners
	for (let i = 0; i < 9; i++) {
		let element = document.getElementById('char ' + i)
		element.i = i
		element.addEventListener('click', charClick)
		element.addEventListener('contextmenu', charContextMenu)
		if (project.project.hotbar[i]) {
			element.getElementsByClassName('desc')[0].innerHTML = project.characters[project.project.hotbar[i]].name
			element.style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', project.project.hotbar[i] + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		}
	}
	document.getElementById('char null').addEventListener('click', updateHotbar)
	document.getElementById('char null').puppet = ''
	document.getElementById('char search').addEventListener('keyup', updateCharSearch)
	document.getElementById('char search').addEventListener('search', updateCharSearch)
	document.getElementById('char selected').addEventListener('click', charSelectedClick)
	document.getElementById('greenscreen').addEventListener('click', toggleGreenScreen)
	for (let i = 0; i < 12; i++) {
		let element = document.getElementById(i)
		element.emote = i
		element.addEventListener('click', emoteClick)
	}
	document.getElementById('babble').addEventListener('mousedown', controller.startBabblingLocal)
	document.getElementById('babble').addEventListener('mouseup', controller.stopBabblingLocal)
	document.getElementById('popout').addEventListener('click', controller.togglePopout)
	document.getElementById('settings').addEventListener('click', toggleSettings)
	document.getElementById('multiplayer').addEventListener('click', toggleMultiplayer)
	document.getElementById('colorpicker').addEventListener('change', colorpickerChange)
	document.getElementById('alwaysontop').addEventListener('click', toggleAlwaysOnTop)
	document.getElementById('puppetscale').addEventListener('change', puppetscaleChange)
	document.getElementById('numslots').addEventListener('change', numslotsChange)
	document.getElementById('nickname').addEventListener('change', nicknameChange)
	document.getElementById('randomNickname').addEventListener('click', randomNickname)
	document.getElementById('ip').addEventListener('change', (e) => { project.project.ip = e.target.value })
	document.getElementById('port').addEventListener('change', (e) => { project.project.port = parseInt(e.target.value) })
	document.getElementById('banish').addEventListener('click', controller.banishLocal)
	document.getElementById('connect').addEventListener('click', network.connect)
	document.getElementById('joinRoom').addEventListener('click', network.join)
	document.getElementById('createRoom').addEventListener('click', network.create)
	document.getElementById('roomName').addEventListener('change', (e) => { project.project.roomName = e.target.value })
	document.getElementById('roomPassword').addEventListener('change', roomPasswordChange)
	document.getElementById('roomPasswordVisibility').addEventListener('click', togglePasswordVisibility)
	document.getElementById('roomPuppetscale').addEventListener('change', roomPuppetscaleChange)
	document.getElementById('roomNumslots').addEventListener('change', roomNumslotsChange)
	document.getElementById('autocrop-btn').addEventListener('click', startAutocrop)
	document.getElementById('prune-btn').addEventListener('click', pruneAssets)

	// Handle input events from popout
	electron.ipcRenderer.on('keyDown', (event, key) => {
		window.onkeydown({"keyCode": key})
	})
	electron.ipcRenderer.on('keyUp', (event, key) => {
		window.onkeyup({"keyCode": key})
	})
	electron.ipcRenderer.on('init', () => {
		controller.initPopout()
	})
	electron.ipcRenderer.on('save', () => {
		project.saveProject()
	})
	electron.ipcRenderer.on('close', () => {
		project.closeProject()
	})
	electron.ipcRenderer.on('view', (event, newView) => {
		exports.setView(newView)
	})
	electron.ipcRenderer.on('togglePopout', () => {
		controller.togglePopout()
	})
	electron.ipcRenderer.on('toggleInstructions', () => {
		exports.toggleModal("#instructions")
	})
	electron.ipcRenderer.on('toggleAbout', () => {
		exports.toggleModal("#about")
	})
	electron.ipcRenderer.on('autocrop', () => {
		openAutocrop()
	})
	electron.ipcRenderer.on('reload', () => {
		controller.reloadAssets()
	})
	electron.ipcRenderer.on('prune', () => {
		openPrune()
	})
	electron.ipcRenderer.on('loaded', controller.setupPopout)

	// Load settings values
	document.getElementById('colorpicker').value = project.project.greenScreen
	document.getElementById('alwaysontop').checked = project.project.alwaysOnTop
	document.getElementById('puppetscale').value = project.project.puppetScale
	document.getElementById('numslots').value = project.project.numCharacters
	document.getElementById('nickname').value = project.project.nickname
	document.getElementById('ip').value = project.project.ip
	document.getElementById('port').value = project.project.port
	document.getElementById('roomName').value = project.project.roomName
	document.getElementById('roomPassword').value = project.project.roomPassword
	document.getElementById('roomPuppetscale').value = project.project.roomPuppetScale
	document.getElementById('roomNumslots').value = project.project.roomNumCharacters
}

// Update UI when user selects a new puppet
// Specifically, tint the character in the hotbar,
//  and tint available emotes for this puppet
exports.setPuppet = function(i) {
	let available = document.getElementById('emotes').getElementsByClassName("available")
	while (available.length)
		available[0].classList.remove("available")

	let selected = document.getElementsByClassName("char selected")
	while (selected.length)
		selected[0].classList.remove("selected")
	
	let character = JSON.parse(JSON.stringify(project.characters[project.project.hotbar[i]]))
	for (let i = 0; i < character.emotes.length; i++) {
		document.getElementById(i).getElementsByClassName('desc')[0].innerHTML = character.emotes[i].name
		if (character.emotes[i].enabled) {
			document.getElementById(i).className += " available"
			if (fs.existsSync(path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id, character.emotes[i] + '.png')))
				document.getElementById(i).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id, i + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
			else
				document.getElementById(i).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', "" + character.id, i + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		} else document.getElementById(i).style.backgroundImage = ''
	}

	document.getElementById('char ' + i).className += " selected"
}

// Change which emote is highlighted in the UI
exports.setEmote = function(emote) {
	let selected = document.getElementsByClassName("emote selected")
	while (selected.length)
		selected[0].classList.remove("selected")

	document.getElementById(emote).className += " selected"
}

// Change whether the babble button is highlighted in the UI
exports.setBabble = function(babbling) {
	document.getElementById('babble').className = "babble" + (babbling ? " selected" : "")
}

// Update the hotbar button for a character
exports.updateCharacter = function(character, updateThumbnail) {
	let index = project.project.hotbar.indexOf(character.id)
	if (index > -1) {
		document.getElementById('char ' + index).getElementsByClassName('desc')[0].innerHTML = character.name
		if (updateThumbnail) {
			controller.updateCharacter(index, character)
			document.getElementById('char ' + index).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		}
		if (('' + document.getElementById('char ' + index).className).indexOf('selected') > -1) {
			controller.setPuppetLocal(index)
			if (updateThumbnail) {
				let emotes = Object.keys(character.emotes)
				for (let i = 0; i < emotes.length; i++) {
					if (character.emotes[emotes[i]] && character.emotes[emotes[i]].enabled)
						document.getElementById(emotes[i]).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id, emotes[i] + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
				}
			}
		}
	}
	if (updateThumbnail) {
		let charPath
		if (fs.existsSync(path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id + '.png')))
			charPath = path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/')
		else charPath = path.join(project.assetsPath, '..', 'thumbnails', character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/')
		
		let selector = document.getElementById('char selected')
		let i = selector.i
		if (selector && i == character.id)
			selector.style.backgroundImage = 'url(' + charPath + ')'
		
		selector = document.getElementById('char list').querySelector("[puppet='" + character.id + "']")<commit_msg>Fixed some?? issues with thumbnails<commit_after>// This file is required by the application.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.

// Imports
const electron = require('electron')
const modal = new (require('vanilla-modal').default)()
const editor = require('./editor.js')
const controller = require('./controller.js')
const network = require('./network.js')
const settings = electron.remote.require('./main-process/settings')
const path = require('path')
const fs = require('fs-extra')

let project
let view

exports.init = function() {
	project = electron.remote.getGlobal('project').project

	// Window input events
	window.onkeydown = keyDown
	window.onkeyup = keyUp
	window.onbeforeunload = beforeUnload

	// DOM listeners
	for (let i = 0; i < 9; i++) {
		let element = document.getElementById('char ' + i)
		element.i = i
		element.addEventListener('click', charClick)
		element.addEventListener('contextmenu', charContextMenu)
		if (project.project.hotbar[i]) {
			element.getElementsByClassName('desc')[0].innerHTML = project.characters[project.project.hotbar[i]].name
			element.style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', project.project.hotbar[i] + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		}
	}
	document.getElementById('char null').addEventListener('click', updateHotbar)
	document.getElementById('char null').puppet = ''
	document.getElementById('char search').addEventListener('keyup', updateCharSearch)
	document.getElementById('char search').addEventListener('search', updateCharSearch)
	document.getElementById('char selected').addEventListener('click', charSelectedClick)
	document.getElementById('greenscreen').addEventListener('click', toggleGreenScreen)
	for (let i = 0; i < 12; i++) {
		let element = document.getElementById(i)
		element.emote = i
		element.addEventListener('click', emoteClick)
	}
	document.getElementById('babble').addEventListener('mousedown', controller.startBabblingLocal)
	document.getElementById('babble').addEventListener('mouseup', controller.stopBabblingLocal)
	document.getElementById('popout').addEventListener('click', controller.togglePopout)
	document.getElementById('settings').addEventListener('click', toggleSettings)
	document.getElementById('multiplayer').addEventListener('click', toggleMultiplayer)
	document.getElementById('colorpicker').addEventListener('change', colorpickerChange)
	document.getElementById('alwaysontop').addEventListener('click', toggleAlwaysOnTop)
	document.getElementById('puppetscale').addEventListener('change', puppetscaleChange)
	document.getElementById('numslots').addEventListener('change', numslotsChange)
	document.getElementById('nickname').addEventListener('change', nicknameChange)
	document.getElementById('randomNickname').addEventListener('click', randomNickname)
	document.getElementById('ip').addEventListener('change', (e) => { project.project.ip = e.target.value })
	document.getElementById('port').addEventListener('change', (e) => { project.project.port = parseInt(e.target.value) })
	document.getElementById('banish').addEventListener('click', controller.banishLocal)
	document.getElementById('connect').addEventListener('click', network.connect)
	document.getElementById('joinRoom').addEventListener('click', network.join)
	document.getElementById('createRoom').addEventListener('click', network.create)
	document.getElementById('roomName').addEventListener('change', (e) => { project.project.roomName = e.target.value })
	document.getElementById('roomPassword').addEventListener('change', roomPasswordChange)
	document.getElementById('roomPasswordVisibility').addEventListener('click', togglePasswordVisibility)
	document.getElementById('roomPuppetscale').addEventListener('change', roomPuppetscaleChange)
	document.getElementById('roomNumslots').addEventListener('change', roomNumslotsChange)
	document.getElementById('autocrop-btn').addEventListener('click', startAutocrop)
	document.getElementById('prune-btn').addEventListener('click', pruneAssets)

	// Handle input events from popout
	electron.ipcRenderer.on('keyDown', (event, key) => {
		window.onkeydown({"keyCode": key})
	})
	electron.ipcRenderer.on('keyUp', (event, key) => {
		window.onkeyup({"keyCode": key})
	})
	electron.ipcRenderer.on('init', () => {
		controller.initPopout()
	})
	electron.ipcRenderer.on('save', () => {
		project.saveProject()
	})
	electron.ipcRenderer.on('close', () => {
		project.closeProject()
	})
	electron.ipcRenderer.on('view', (event, newView) => {
		exports.setView(newView)
	})
	electron.ipcRenderer.on('togglePopout', () => {
		controller.togglePopout()
	})
	electron.ipcRenderer.on('toggleInstructions', () => {
		exports.toggleModal("#instructions")
	})
	electron.ipcRenderer.on('toggleAbout', () => {
		exports.toggleModal("#about")
	})
	electron.ipcRenderer.on('autocrop', () => {
		openAutocrop()
	})
	electron.ipcRenderer.on('reload', () => {
		controller.reloadAssets()
	})
	electron.ipcRenderer.on('prune', () => {
		openPrune()
	})
	electron.ipcRenderer.on('loaded', controller.setupPopout)

	// Load settings values
	document.getElementById('colorpicker').value = project.project.greenScreen
	document.getElementById('alwaysontop').checked = project.project.alwaysOnTop
	document.getElementById('puppetscale').value = project.project.puppetScale
	document.getElementById('numslots').value = project.project.numCharacters
	document.getElementById('nickname').value = project.project.nickname
	document.getElementById('ip').value = project.project.ip
	document.getElementById('port').value = project.project.port
	document.getElementById('roomName').value = project.project.roomName
	document.getElementById('roomPassword').value = project.project.roomPassword
	document.getElementById('roomPuppetscale').value = project.project.roomPuppetScale
	document.getElementById('roomNumslots').value = project.project.roomNumCharacters
}

// Update UI when user selects a new puppet
// Specifically, tint the character in the hotbar,
//  and tint available emotes for this puppet
exports.setPuppet = function(i) {
	let available = document.getElementById('emotes').getElementsByClassName("available")
	while (available.length)
		available[0].classList.remove("available")

	let selected = document.getElementsByClassName("char selected")
	while (selected.length)
		selected[0].classList.remove("selected")
	
	let character = JSON.parse(JSON.stringify(project.characters[project.project.hotbar[i]]))
	for (let i = 0; i < character.emotes.length; i++) {
		document.getElementById(i).getElementsByClassName('desc')[0].innerHTML = character.emotes[i].name
		if (character.emotes[i].enabled) {
			document.getElementById(i).className += " available"
			if (fs.existsSync(path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id, i + '.png')))
				document.getElementById(i).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id, i + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
			else
				document.getElementById(i).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', "" + character.id, i + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		} else document.getElementById(i).style.backgroundImage = ''
	}

	document.getElementById('char ' + i).className += " selected"
}

// Change which emote is highlighted in the UI
exports.setEmote = function(emote) {
	let selected = document.getElementsByClassName("emote selected")
	while (selected.length)
		selected[0].classList.remove("selected")

	document.getElementById(emote).className += " selected"
}

// Change whether the babble button is highlighted in the UI
exports.setBabble = function(babbling) {
	document.getElementById('babble').className = "babble" + (babbling ? " selected" : "")
}

// Update the hotbar button for a character
exports.updateCharacter = function(character, updateThumbnail) {
	let index = project.project.hotbar.indexOf(character.id)
	if (index > -1) {
		document.getElementById('char ' + index).getElementsByClassName('desc')[0].innerHTML = character.name
		if (updateThumbnail) {
			controller.updateCharacter(index, character)
			document.getElementById('char ' + index).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
		}
		if (('' + document.getElementById('char ' + index).className).indexOf('selected') > -1) {
			controller.setPuppetLocal(index)
			if (updateThumbnail) {
				let emotes = Object.keys(character.emotes)
				for (let i = 0; i < emotes.length; i++) {
					if (character.emotes[emotes[i]] && character.emotes[emotes[i]].enabled)
						document.getElementById(emotes[i]).style.backgroundImage = 'url(' + path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id, i + '.png?random=' + new Date().getTime()).replace(/\\/g, '/') + ')'
				}
			}
		}
	}
	if (updateThumbnail) {
		let charPath
		if (fs.existsSync(path.join(project.assetsPath, '..', 'thumbnails', "new-" + character.id + '.png')))
			charPath = path.join(project.assetsPath, '..', 'thumbnails', 'new-' + character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/')
		else charPath = path.join(project.assetsPath, '..', 'thumbnails', character.id + '.png?random=' + new Date().getTime()).replace(/\\/g, '/')
		
		let selector = document.getElementById('char selected')
		let i = selector.i
		if (selector && i == character.id)
			selector.style.backgroundImage = 'url(' + charPath + ')'
		
		selector = document.getElementById('char list').querySelector("[puppet='" + character.id + "']")
{'fraction_non_alphanumeric': 0.16671772875816993, 'fraction_numerical': 0.0015318627450980392, 'mean_word_length': 5.85749299719888, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5921735', 'n_tokens_mistral': 5856, 'n_tokens_neox': 5569, 'n_words': 962}