<commit_before>// DarkFO
// Copyright (c) 2014 darkf
// Licensed under the terms of the zlib license

// TODO: reduce code duplication, circular references,
//       and general badness/unmaintainability.
// TODO: console UI on main bar
// TODO: combat UI on main bar
// TODO: stats/info view in inventory screen
// TODO: fix inventory image size
// TODO: fix style for inventory image amount
// TODO: use the grid area for replies on dialogue screen
// TODO: center the camera around the target while on the dialogue screen
// TODO: loot UI
// TODO: option for scaling the UI

function initUI() {
	makeDropTarget($("#inventoryBoxList"), function(data) { uiMoveSlot(data, "inventory") })
	makeDropTarget($("#inventoryBoxItem1"), function(data) { uiMoveSlot(data, "leftHand") })
	makeDropTarget($("#inventoryBoxItem2"), function(data) { uiMoveSlot(data, "rightHand") })

	$("#inventoryButton").click(drawInventoryScreen)
	$("#inventoryDoneButton").click(function() {
		$("#inventoryBox").css("visibility", "hidden")
	})

	function makeScrollable($el) {
		$el.bind("mousewheel DOMMouseScroll", function(e) {
			var delta = (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) ? -1 : 1
	        $el.scrollTop($el.scrollTop() + 60*delta)
		})
	}

	makeScrollable($("#inventoryBoxList"))

	makeScrollable($("#barterBoxInventoryLeft"))
	makeScrollable($("#barterBoxInventoryRight"))
	makeScrollable($("#barterBoxLeft"))
	makeScrollable($("#barterBoxRight"))

	drawHP(critterGetStat(player, "HP"))
}

function uiMoveSlot(data, target) {
	var obj = null
	if(data[0] === "i") {
		if(target === "inventory")
			return // disallow inventory -> inventory

		var idx = parseInt(data.slice(1))
		console.log("idx: " + idx)
		obj = player.inventory[idx]
		player.inventory.splice(idx, 1) // remove object from inventory
	}
	else {
		obj = player[data]
		player[data] = null // remove object from slot
	}

	console.log("obj: " + obj + " (data: " + data + ", target: " + target + ")")

	if(target === "inventory")
		player.inventory.push(obj)
	else {
		if(player[target] !== undefined && player[target] !== null) {
			// perform a swap
			if(data[0] === "i")
				player.inventory.push(player[target]) // inventory -> slot
			else
				player[data] = player[target] // slot -> slot
		}

		player[target] = obj // move the object over
	}
}

function makeDropTarget($el, dropCallback) {
	$el.on("drop", function(e) {
		var data = e.originalEvent.dataTransfer.getData("text/plain")
		dropCallback(data, e)
		return false
	}).on("dragenter", function() { return false }).
	   on("dragover",  function() { return false })
}

function makeDraggable($el, data, endCallback) {
	$el.attr("draggable", "true").on("dragstart", function(e) {
		e.originalEvent.dataTransfer.setData('text/plain', data)
		console.log("start drag")
	}).on("dragend", function(e) {<commit_msg>ui: fix inventory screen not updating<commit_after>// DarkFO
// Copyright (c) 2014 darkf
// Licensed under the terms of the zlib license

// TODO: reduce code duplication, circular references,
//       and general badness/unmaintainability.
// TODO: console UI on main bar
// TODO: combat UI on main bar
// TODO: stats/info view in inventory screen
// TODO: fix inventory image size
// TODO: fix style for inventory image amount
// TODO: use the grid area for replies on dialogue screen
// TODO: center the camera around the target while on the dialogue screen
// TODO: loot UI
// TODO: option for scaling the UI

function initUI() {
	makeDropTarget($("#inventoryBoxList"), function(data) { uiMoveSlot(data, "inventory") })
	makeDropTarget($("#inventoryBoxItem1"), function(data) { uiMoveSlot(data, "leftHand") })
	makeDropTarget($("#inventoryBoxItem2"), function(data) { uiMoveSlot(data, "rightHand") })

	$("#inventoryButton").click(drawInventoryScreen)
	$("#inventoryDoneButton").click(function() {
		$("#inventoryBox").css("visibility", "hidden")
	})

	function makeScrollable($el) {
		$el.bind("mousewheel DOMMouseScroll", function(e) {
			var delta = (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) ? -1 : 1
	        $el.scrollTop($el.scrollTop() + 60*delta)
		})
	}

	makeScrollable($("#inventoryBoxList"))

	makeScrollable($("#barterBoxInventoryLeft"))
	makeScrollable($("#barterBoxInventoryRight"))
	makeScrollable($("#barterBoxLeft"))
	makeScrollable($("#barterBoxRight"))

	drawHP(critterGetStat(player, "HP"))
}

function uiMoveSlot(data, target) {
	var obj = null
	if(data[0] === "i") {
		if(target === "inventory")
			return // disallow inventory -> inventory

		var idx = parseInt(data.slice(1))
		console.log("idx: " + idx)
		obj = player.inventory[idx]
		player.inventory.splice(idx, 1) // remove object from inventory
	}
	else {
		obj = player[data]
		player[data] = null // remove object from slot
	}

	console.log("obj: " + obj + " (data: " + data + ", target: " + target + ")")

	if(target === "inventory")
		player.inventory.push(obj)
	else {
		if(player[target] !== undefined && player[target] !== null) {
			// perform a swap
			if(data[0] === "i")
				player.inventory.push(player[target]) // inventory -> slot
			else
				player[data] = player[target] // slot -> slot
		}

		player[target] = obj // move the object over
	}

	drawInventoryScreen()
}

function makeDropTarget($el, dropCallback) {
	$el.on("drop", function(e) {
		var data = e.originalEvent.dataTransfer.getData("text/plain")
		dropCallback(data, e)
		return false
	}).on("dragenter", function() { return false }).
	   on("dragover",  function() { return false })
}

function makeDraggable($el, data, endCallback) {
	$el.attr("draggable", "true").on("dragstart", function(e) {
		e.originalEvent.dataTransfer.setData('text/plain', data)
		console.log("start drag")
	}).on("dragend", function(e) {
{'fraction_non_alphanumeric': 0.1653913043478261, 'fraction_numerical': 0.005565217391304348, 'mean_word_length': 4.440870387890255, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6967638', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1821, 'n_words': 458}