<commit_before>package h2d;

@:allow(h2d.Graphics)
class GraphicsContext {
	
	var g : Graphics;
	var mc : flash.display.Sprite;
	var mcg : flash.display.Graphics;
	
	function new(g) {
		this.g = g;
		this.mc = new flash.display.Sprite();
		mcg = mc.graphics;
	}
	
	public inline function beginFill( color : Int, alpha = 1. ) {
		mcg.beginFill(color, alpha);
	}
	
	public inline function drawCircle(cx, cy, radius) {
		mcg.drawCircle(cx, cy, radius);
	}
	
	public inline function drawRect(x,y,width,height) {
		mcg.drawRect(x, y, width, height);
	}
	
	public inline function endFill() {
		mcg.endFill();
	}
	
}

class Graphics extends Drawable {

	var tile : Tile;
	var ctx : GraphicsContext;
	
	public function new(?parent) {
		super(parent);
	}
		
	public function beginDraw() {
		return (ctx = new GraphicsContext(this));
	}
	
	override function onDelete() {
		if( tile != null ) {
			tile.dispose();
			tile = null;
		}
		super.onDelete();
	}
	
	public function endDraw( ?allocPos : h3d.impl.AllocPos ) {
		if( ctx == null ) return;
		if( tile != null ) tile.dispose();
		tile = Tile.fromSprites([ctx.mc],allocPos)[0];
	}
	
	override function draw(ctx:RenderContext) {
		if( tile == null ) endDraw();
		if( tile == null ) return;
		drawTile(ctx.engine, tile);
	}

}<commit_msg>added restoreOnContextLost<commit_after>package h2d;

@:allow(h2d.Graphics)
class GraphicsContext {
	
	var g : Graphics;
	var mc : flash.display.Sprite;
	var mcg : flash.display.Graphics;
	
	function new(g) {
		this.g = g;
		this.mc = new flash.display.Sprite();
		mcg = mc.graphics;
	}
	
	public inline function beginFill( color : Int, alpha = 1. ) {
		mcg.beginFill(color, alpha);
	}
	
	public inline function drawCircle(cx, cy, radius) {
		mcg.drawCircle(cx, cy, radius);
	}
	
	public inline function drawRect(x,y,width,height) {
		mcg.drawRect(x, y, width, height);
	}
	
	public inline function endFill() {
		mcg.endFill();
	}
	
}

class Graphics extends Drawable {

	var tile : Tile;
	var ctx : GraphicsContext;
	
	public function new(?parent) {
		super(parent);
	}
		
	public function beginDraw() {
		return (ctx = new GraphicsContext(this));
	}
	
	override function onDelete() {
		if( tile != null ) {
			tile.dispose();
			tile = null;
		}
		super.onDelete();
	}
	
	public function endDraw( restoreOnContextLost = false, ?allocPos : h3d.impl.AllocPos ) {
		if( ctx == null ) return;
		if( tile != null ) tile.dispose();
		tile = Tile.fromSprites([ctx.mc], allocPos)[0];
		if( restoreOnContextLost ) {
			var ctx = ctx;
			tile.getTexture().onContextLost = function() {
				this.ctx = ctx;
				endDraw();
			};
		}
	}
	
	override function draw(ctx:RenderContext) {
		if( tile == null ) endDraw();
		if( tile == null ) return;
		drawTile(ctx.engine, tile);
	}

}
{'fraction_non_alphanumeric': 0.14192139737991266, 'fraction_numerical': 0.00363901018922853, 'mean_word_length': 3.4626623376623376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7610398', 'n_tokens_mistral': 1098, 'n_tokens_neox': 974, 'n_words': 242}