Вход Регистрация
Файл: Space race/libs/Editor_files/CanvasRenderer.js
Строк: 266
<?php
var pgli pgli || {};
pgli.render pgli.render || {};

pgli.render.CanvasRenderer gamecore.Base.extend('CanvasRenderer',
// static

    
SCOPE_KEYWORDS: {
        
'use'0,
        
'repeat'1,
        
'comment'2
    
},
    
SCOPE_XFORMS: {
        
'x'1,
        
'y'2,
        
'width'3,
        
'height'4,
        
'scale'0
    
},
    
REPEAT_TYPES: {
        
'none''no-repeat',
        
'x''repeat-x',
        
'y''repeat-y',
        
'xy''repeat'
    
}

},
// instance

    
domnull,
    
containernull,
    
canvasnull,
    
contextnull,
    
width1024,
    
height768,
    
projectnull,
    
xform: [],
    
xseek: -1,
    
scope: [],
    
sseek: -1,
    
resources: {
        
images: []
    },
    
variablesnull,

    
/**
     * Bind a new renderer to given canvas
     * @param  jQuery.DOMElement domElement [description]
     * @param  Integer width      [description]
     * @param  Integer height     [description]
     */
    
init: function(domElement)
    {
        
this.dom domElement;
        
this.container = $('#'+this.dom);
        
this.canvas document.createElement("Canvas");
        
this.container.append(this.canvas);

        
this.resize();

        
this.variables = new gamecore.Hashtable();
    },

    
draw: function()
    {
        
clearTrace();

        if(!
this.project)
        {
            
trace("#Render failed: No project loaded");
            return;
        }
        
trace("#Starting renderer...");

        
this.context.clearRect(00this.widththis.height);

        var 
ratio this.getRenderRatio();
        var 
root this.project.getRootModule();
        var 
root.width*ratio;
        var 
root.height*ratio;
        var 
Math.round((this.width w)/2);
        var 
Math.round((this.height h)/2);

        
this.sreset();
        
this.xreset(ratioxywh);
        
this.walkModule(root);
    },

    
walkModule: function(module)
    {
        if(!
module) return;

        
trace("#Render: "+module.name);

        if(
"params" in module) for(p in module.params)
            
this.sset(pmodule.params[p]);

        if(
"vars" in module)
            
this.actionVars(module.vars);

        if(
"fill" in module)
            
this.actionFill(module.fill);

        if(
"layers" in module) for(var i=0len=module.layers.lengthi<len; ++i)
        {
            var 
layer module.layers[i];
            if(
"use" in layer)
            {
                if(
"repeat" in layer)
                {
                    var 
iterator;

                    try {
                        
iterator pgli.lang.Parser.parseRepeat(layer.repeatthis.sget());
                        
trace("#  Found repeat layer ["+iterator.varname+" from "+iterator.start+" to "+iterator.end+"]");
                    } catch(
e) {
                        
trace("(!) Syntax error in repeat expression ["+layer.repeat+"]");
                        continue;
                    }
                    
                    while(
iterator.loop())
                    {
                        
this.sset(iterator.varnameiterator.step);
                        
this.walkLayer(layer);
                        
iterator.next();
                    }
                }
                else
                {
                    
this.walkLayer(layer);
                }
            }
        }
    },

    
walkLayer: function(layer)
    {
        var static = 
pgli.render.CanvasRenderer;

        
this.xpush();
        
this.spush();

        for(
k in layer)
        {
            if(
k in static.SCOPE_KEYWORDS)
                continue;
            else if(
k in static.SCOPE_XFORMS)
            {
                var 
val pgli.lang.Parser.parseExpression(layer[k], this.sget(), this.xget());
                if(
typeof(val) == "number")
                {
                    
val Number(val)*this.xgetr();
                }
                else
                {
                    var 
val.search("%");
                    if(
0)
                        
val = (Number(val.substr(Op))/100)*this.xget()[static.SCOPE_XFORMS[k]];
                    else continue;
                }
                if(
== "x" || == "y")
                {
                    
val += this.xget()[static.SCOPE_XFORMS[k]];
                }
                
this.xset(static.SCOPE_XFORMS[k], val);
            }
            else
                
this.sset(kpgli.lang.Parser.parseExpression(layer[k], this.sget()), this.xget());
        }

        
this.walkModule(this.project.getModule(layer.use));
        
        
this.spop();
        
this.xpop();
    },

    
actionVars: function(opts)
    {
        var static = 
pgli.render.CanvasRenderer;

        for(
k in opts)
        {
            if(
k in static.SCOPE_KEYWORDS || k in static.SCOPE_XFORMS)
                continue;
            else
                
this.sset(kpgli.lang.Parser.parseExpression(opts[k], this.sget(), this.xget()));
        }
    },

    
actionFill: function(opts)
    {
        var static = 
pgli.render.CanvasRenderer;

        if(
opts.type == "image")
        {
            
//trace("#  Fill(image)...")
            
var image this.loadImage(
                    
pgli.lang.Parser.parseExpression(opts.valuethis.sget(), this.xget())
                );

            if(
"repeat" in opts && opts.repeat in static.REPEAT_TYPES)
            {
                var 
pattern this.context.createPattern(image, static.REPEAT_TYPES[opts.repeat]);

                
this.context.rect(this.xgetx(), this.xgety(), this.xgetw(), this.xgeth());
                
this.context.fillStyle pattern;
                
this.context.fill();

                
//pattern = null;
            
}
            else
            {
                
this.context.drawImage(imagethis.xgetx(), this.xgety(), this.xgetw(), this.xgeth());
            }
        }
        else if(
opts.type == "color")
        {
            
//trace("#  Fill(color)...")
            
this.context.beginPath();
            
this.context.rect(this.xgetx(), this.xgety(), this.xgetw(), this.xgeth());
            
this.context.fillStyle pgli.lang.Parser.parseExpression(opts.valuethis.sget(), this.xget());
            
this.context.fill();
        }
        else if(
opts.type == "line")
        {
            
//trace("#  Fill(line)...")
            
this.context.beginPath();
            
this.context.rect(this.xgetx(), this.xgety(), this.xgetw(), this.xgeth());
            
this.context.lineWidth 1;
            
this.context.strokeStyle pgli.lang.Parser.parseExpression(opts.valuethis.sget(), this.xget());
            
this.context.stroke();
        }
    },

    
loadImage: function(url)
    {
        var 
self this;
        if(
url in this.resources.images)
        {
            return 
this.resources.images[url];
        }
        else
        {
            
trace("/!\ Image is loading, will redraw onload.");
            var 
img = new Image();
            
img.onload = function(){
                
trace("#Image loaded, redrawing...");
                
self.draw();
            };
            
img.src url;
            
this.resources.images[url] = img;
            return 
img;
        }
    },

    
spush: function()
    {
        
this.scope.push(this.cloneScope(this.sget()));
        
this.sseek++;
    },

    
spop: function()
    {
        
this.sseek--;
        return 
this.scope.pop();
    },

    
sreset: function()
    {
        
this.scope = [{}];
        
this.sseek 0;
    },

    
sget: function()
    {
        return 
this.scope[this.sseek];
    },

    
sset: function(keyvalue)
    {
        
this.scope[this.sseek][key] = value;
    },

    
xpush: function(ratioxyhw)
    {
        if(
ratio == undefined)
            if(
this.xseek == -1)
                
this.xform.push([1.000this.widththis.height]);
            else
                
this.xform.push(this.xget().slice(0))
        else
            
this.xform.push([ratioxyhw]);
        
        
this.xseek++;
    },

    
xpop: function()
    {
        
this.xseek--;
        return 
this.xform.pop();
    },

    
xget: function()
    {
        return 
this.xform[this.xseek];
    },

    
xset: function(keyvalue)
    {
        
this.xform[this.xseek][key] = value;
    },

    
xreset: function(ratioxyhw)
    {
        
this.xseek = -1;
        
this.xform = [];
        
this.xpush(ratioxyhw);
    },

    
xgetr: function()
    {
        return 
this.xform[this.xseek][0];
    },

    
xgetx: function()
    {
        return 
this.xform[this.xseek][1];
    },

    
xgety: function()
    {
        return 
this.xform[this.xseek][2];
    },

    
xgetw: function()
    {
        return 
this.xform[this.xseek][3];
    },

    
xgeth: function()
    {
        return 
this.xform[this.xseek][4];
    },

    
cloneScope: (function()
    { 
        return function (
obj) { Clone.prototype=obj; return new Clone() };
        function Clone(){}
    }()),

    
bindProject: function(project)
    {
        
this.project project;
    },

    
resize: function()
    {
        var 
this.container.width();
        var 
this.container.height();

        var 
size 0.9*Math.min(wh);
        
this.width size;
        
this.height size;
        var 
$canvas = $(this.canvas);
        
$canvas.width(size);
        
$canvas.height(size);
        
this.canvas.width size;
        
this.canvas.height size;
        
$canvas.css('marginTop'this.container.height() / size 2);
        
$canvas.css('marginLeft'this.container.width() / size 2);

        
this.context this.canvas.getContext("2d");

        
/*var ctx = this.canvas.getContext("2d");
        ctx.clearRect(0, 0, size, size);
        ctx.canvas.width = ctx.canvas.height = size;
        ctx.font = 'bold 40px Arial';
        ctx.textAlign = 'center';
        ctx.fillStyle = '#444';
        ctx.fillText("Preview", size/2, size/2);*/
    
},

    
getRenderRatio: function()
    {
        var 
root this.project.getRootModule();
        if(!
root) return;
        return 
Math.min(this.width/root.widththis.height/root.height);
    }

});
?>
Онлайн: 1
Реклама