Ero sivun ”Järjestelmäviesti:Common.js” versioiden välillä

Seikkailuoppaasta
Rivi 207: Rivi 207:
 
        
 
        
 
         if (tile == '') return;
 
         if (tile == '') return;
       
 
        console.log(tile);
 
 
                      
 
                      
 
                     if (!images[tile]) {
 
                     if (!images[tile]) {
 
                         images[tile] = new Image();
 
                         images[tile] = new Image();
                         loadImages.push(new Promise(function(reject, resolve){
+
                         loadImages.push(new Promise(function(resolve, reject){
 
                         images[tile].onload = function() {
 
                         images[tile].onload = function() {
 
                                 resolve();
 
                                 resolve();

Versio 14. kesäkuuta 2022 kello 21.11

function createItemDisplay(itemData) {
	
	if (!itemData || itemData.trim() == '') {
		return $('<div class="x-item"/>');
	}
	
	itemData = itemData.trim();
	
	const $it = $('<div class="x-item"/>');
	const dataParts = itemData.split(',');
	if (dataParts[0].indexOf('item:') == 0) {
		if (dataParts[0].indexOf('.png') == -1) dataParts[0] += '.png';
		$it.css('background-image', 'url("https://cubisti.xeno.fi/' + dataParts[0].substring(5) + '")');
		if (dataParts[0].indexOf('vanilla/') != -1) {
			$it.attr('data-vanilla-item', 'true');
		}
	} else if (dataParts[0].indexOf('http') == 0) {
		$it.css('background-image', 'url("' + dataParts[0] + '")');
	}
	
	if (dataParts.length > 1) {
		$it.attr('data-item-title', dataParts[1]);
	}
	
	if (dataParts.length > 2) {
		$it.append($('<span/>').text(''+dataParts[2]));
	}
	
	return $it;
	
}

function renderCubeStructure(blocks, images) {
        
    const cubeSideWidth = 108;
    const cubeSideHeight = 54;
    const cubeHeight = 242;
    const cubeLongHeight = 133;
    const scale = 0.5;
    
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    
    const drawCommands = [];
    
    for (var y = blocks.length-1; y >= 0; y--) {
        const layer = blocks[y];
        for (var z = layer.length-1; z >= 0; z--) {
            const row = layer[z];
            for (var x = 0; x < row.length; x++) {
                
                const tile = row[x];
                if (tile == '') continue;
                
                drawCommands.push([images[tile],
                    (x*cubeSideWidth + z*cubeSideWidth),
                    (y*cubeLongHeight + x*cubeSideHeight - z*cubeSideHeight),
                    images[tile].naturalWidth, images[tile].naturalHeight]);
                
            }
        }
    }
    
    var minX = 100000;
    var minY = 100000;
    var maxX = 0;
    var maxY = 0;
    for (var i = 0; i < drawCommands.length; i++) {
        var x = drawCommands[i][1]*scale;
        var y = drawCommands[i][2]*scale;
        var w = drawCommands[i][3]*scale;
        var h = drawCommands[i][4]*scale;
        if (x < minX) minX = x;
        if (x + w > maxX) maxX = x + w;
        if (y < minY) minY = y;
        if (y + h > maxY) maxY = y + h;
    }
    
    canvas.width = maxX-minX;
    canvas.height = maxY-minY;
    
    for (var i = 0; i < drawCommands.length; i++) {
        ctx.drawImage(
            drawCommands[i][0], // image
            -minX + (drawCommands[i][1])*scale, // x
            -minY + (drawCommands[i][2])*scale, // y
            (drawCommands[i][3])*scale, // width
            (drawCommands[i][4])*scale  // height
        );
    }
    
    ctx.font = '10px Arial';
    ctx.fillStyle = '#000000';
    ctx.globalAlpha = 0.1;
    ctx.textBaseline = 'top';
    ctx.fillText('Xeno ' + (new Date()).getFullYear(), 5, canvas.height - 10 - 5);
    
    return canvas;
    
}

$(function(){
	
	// hide the stupid navigation options under the title
	$('#mw-page-header-links').hide();
	$('#p-views li').each(function(){
		if (!$(this).is('#ca-more')) {
			$('#p-cactions ul').prepend($(this));
		}
	});
	
	// title-less pages
	if ($('[data-x-hide-title]').length > 0) {
		$('#firstHeading').hide();
		$('#bodyContentOuter').css('margin-top', '-1.5em');
	}
	
	const $itemOverlay = $('<div/>')
		.css({
			'position': 'fixed',
			'z-index': '1000',
			'padding': '5px 10px',
			'font-family': 'monospace',
			'color': '#ffffff',
			'background-color': '#2a114f',
			'border': '3px solid rgba(255, 255, 255, 0.2)',
			'border-radius': '5px',
			'box-shadow': '-2px -2px 0 black, 2px -2px 0 black, -2px 2px 0 black, 2px 2px 0 black',
			'top': '0',
			'left': '0'
		});
	
	$('body').append($itemOverlay);
	$itemOverlay.hide();
	
	// crafting grids
	$('.x-crafting').each(function(){
		
		const craftItems = ($(this).attr('data-crafting-data') || '').split(';');
		const resultItem = craftItems[craftItems.length-1];
		
		const $grid = $('<div class="x-crafting-grid"/>');
		const $result = $('<div class="x-crafting-result"/>');
		$result.append(createItemDisplay(resultItem));
		
		$.each(craftItems.slice(0, -1), function(key, it){
			it = it.trim();
			$grid.append(createItemDisplay(it));
		});
		
		if ($(this).is('.furnace')) {
			$grid.append($('<div class="x-crafting-flame"/>'));
			$grid.append(createItemDisplay('item:vanilla/coal'));
		}
		
		$(this).append($grid);
		$(this).append($('<div class="x-crafting-arrow"/>'));
		$(this).append($result);
		
	});
	
	var isItemOverlay = false;
	
	$('body').on('mousemove', function(e){
		if (isItemOverlay) {
			$itemOverlay.css('left', (e.clientX+15)+'px');
			$itemOverlay.css('top', (e.clientY+15)+'px');
		}
	})
	
	$('.x-item[data-item-title]')
		.css('cursor', 'pointer')
		.on('mouseover', function(e){
			$itemOverlay.show();
			isItemOverlay = true;
			$itemOverlay.html('');
			$itemOverlay.append($('<div style="font-weight: bold;"/>').text($(this).attr('data-item-title') || '?'));
		})
		.on('mouseout', function(e){
			$itemOverlay.hide();
			isItemOverlay = false;
		})
		.on('mouseup', function(e){
			if ($(this).is('[data-vanilla-item]')) {
				window.open('https://www.google.com/search?q=' + encodeURIComponent('minecraft wiki ' + $(this).attr('data-item-title').toLowerCase()));
				return;
			}
			if (e.button == 1) {
				window.open('/index.php/' + $(this).attr('data-item-title'));
			} else {
				window.location.href = '/index.php/' + $(this).attr('data-item-title');
			}
		});
		
	
	// block structure rendering
	$('[data-block-structure]').each(function(){
        
        const $this = $(this);
        const blocks = $(this).attr('data-block-structure').split('!').map(function(r){ return r.trim().split(';').map(function(c){ return c.trim().split(','); }); });
        
        const images = {};
        const loadImages = [];
        $.each(blocks, function(key, layer){
        	$.each(layer, function(key, row){
        		$.each(row, function(key, tile){
        			
        			if (tile == '') return;
                    
                    if (!images[tile]) {
                        images[tile] = new Image();
                        loadImages.push(new Promise(function(resolve, reject){
                        	images[tile].onload = function() {
                                resolve();
                            }
                            images[tile].onerror = function(err) {
                            	console.error('image error:', err);
                                reject(err);
                            }
                            images[tile].src = 'https://cubisti.xeno.fi/' + tile + '.png';
                        }));
                    }
        			
        		})
        	})
        })
        
        console.log(loadImages);
        
        Promise.all(loadImages)
            .then(function(){
            	console.log('!')
                const canvas = renderCubeStructure(blocks, images);
                $this.replaceWith($(canvas).attr('style', $this.attr('data-style') || ''));
            })
            .catch(function(err){
            	console.error(err);
            	$this.css('max-width', '200px').append($('<span style="color: red;"/>').text(''+err));
            });
            
    });
	
});