<commit_before>package flambe.platform.flash;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;

import haxe.FastList;

import flambe.display.Texture;
import flambe.math.FMath;

class FlashDrawingContext
    implements DrawingContext
{
    public function new (buffer :BitmapData)
    {
        _buffer = buffer;
        _stack = new FastList<DrawingState>();
        _shape = new Shape();
    }

    public function save ()
    {
        var copy = new DrawingState();

        if (_stack.isEmpty()) {
            copy.matrix = new Matrix();
        } else {
            var state = getTopState();
            copy.matrix = state.matrix.clone();
            copy.color = state.color;
        }

        _stack.add(copy);
    }

    public function translate (x :Float, y :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.translate(x, y);
        matrix.concat(copy);
    }

    public function scale (x :Float, y :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.scale(x, y);
        matrix.concat(copy);
    }

    public function rotate (rotation :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.rotate(FMath.toRadians(rotation));
        matrix.concat(copy);
    }

    public function restore ()
    {
        flushGraphics();
        _stack.pop();
    }

    public function drawImage (texture :Texture, x :Int, y :Int)
    {
        flushGraphics();

        var state = getTopState();
        var matrix = state.matrix;

        // Use the faster copyPixels() if possible
        // TODO: Use approximately equals?
        if (matrix.a == 1 && matrix.b == 0 && matrix.c == 0 && matrix.d == 1
                && state.color == null) {
            _buffer.copyPixels(texture, new Rectangle(0, 0, texture.width, texture.height),
                new Point(matrix.tx + x, matrix.ty + y));

        } else {
            state.matrix.tx += x;
            state.matrix.ty += y;
            _buffer.draw(texture, state.matrix, state.color, null, null, true);
            state.matrix.tx -= x;
            state.matrix.ty -= y;
        }
    }

    public function drawPattern (texture :Texture, x :Int, y :Int, width :Float, height :Float)
    {
        beginGraphics();

        _graphics.beginBitmapFill(texture);
        _graphics.drawRect(x, y, width, height);

        /*throw "up";*/
        //_graphics.endFill();

        /*flushGraphics();*/
        /*throw 0;*/
    }

    public function multiplyAlpha (factor :Float)
    {
        flushGraphics();

        var state = getTopState();
        if (state.color == null) {
            state.color = new ColorTransform(1, 1, 1, factor);
        } else {
            state.color.alphaMultiplier *= factor;
        }
    }

    inline private function getTopState ()
    {
        return _stack.head.elt;
    }

    private function flushGraphics ()
    {
        // If we're in vector graphics mode, push it out to the screen buffer
        if (_graphics != null) {
            var state = getTopState();
            _buffer.draw(_shape, state.matrix, state.color, null, null, true);
            _graphics.clear();
            _graphics = null;
        }
    }<commit_msg>Cleaned out debugging crap.<commit_after>package flambe.platform.flash;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;

import haxe.FastList;

import flambe.display.Texture;
import flambe.math.FMath;

class FlashDrawingContext
    implements DrawingContext
{
    public function new (buffer :BitmapData)
    {
        _buffer = buffer;
        _stack = new FastList<DrawingState>();
        _shape = new Shape();
    }

    public function save ()
    {
        var copy = new DrawingState();

        if (_stack.isEmpty()) {
            copy.matrix = new Matrix();
        } else {
            var state = getTopState();
            copy.matrix = state.matrix.clone();
            copy.color = state.color;
        }

        _stack.add(copy);
    }

    public function translate (x :Float, y :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.translate(x, y);
        matrix.concat(copy);
    }

    public function scale (x :Float, y :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.scale(x, y);
        matrix.concat(copy);
    }

    public function rotate (rotation :Float)
    {
        flushGraphics();

        // TODO: Optimize
        var matrix = getTopState().matrix;
        var copy = matrix.clone();
        matrix.identity();
        matrix.rotate(FMath.toRadians(rotation));
        matrix.concat(copy);
    }

    public function restore ()
    {
        flushGraphics();
        _stack.pop();
    }

    public function drawImage (texture :Texture, x :Int, y :Int)
    {
        flushGraphics();

        var state = getTopState();
        var matrix = state.matrix;

        // Use the faster copyPixels() if possible
        // TODO: Use approximately equals?
        if (matrix.a == 1 && matrix.b == 0 && matrix.c == 0 && matrix.d == 1
                && state.color == null) {
            _buffer.copyPixels(texture, new Rectangle(0, 0, texture.width, texture.height),
                new Point(matrix.tx + x, matrix.ty + y));

        } else {
            state.matrix.tx += x;
            state.matrix.ty += y;
            _buffer.draw(texture, state.matrix, state.color, null, null, true);
            state.matrix.tx -= x;
            state.matrix.ty -= y;
        }
    }

    public function drawPattern (texture :Texture, x :Int, y :Int, width :Float, height :Float)
    {
        beginGraphics();

        _graphics.beginBitmapFill(texture);
        _graphics.drawRect(x, y, width, height);
    }

    public function multiplyAlpha (factor :Float)
    {
        flushGraphics();

        var state = getTopState();
        if (state.color == null) {
            state.color = new ColorTransform(1, 1, 1, factor);
        } else {
            state.color.alphaMultiplier *= factor;
        }
    }

    inline private function getTopState ()
    {
        return _stack.head.elt;
    }

    private function flushGraphics ()
    {
        // If we're in vector graphics mode, push it out to the screen buffer
        if (_graphics != null) {
            var state = getTopState();
            _buffer.draw(_shape, state.matrix, state.color, null, null, true);
            _graphics.clear();
            _graphics = null;
        }
    }
{'fraction_non_alphanumeric': 0.11943953203645763, 'fraction_numerical': 0.0025846823561420213, 'mean_word_length': 1.9105304829770389, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1453812', 'n_tokens_mistral': 2106, 'n_tokens_neox': 2036, 'n_words': 537}