<commit_before>package lime._backend.html5;


import js.html.KeyboardEvent;
import js.Browser;
import lime.app.Application;
import lime.app.Config;
import lime.audio.AudioManager;
import lime.graphics.Renderer;
import lime.ui.KeyCode;
import lime.ui.KeyModifier;
import lime.ui.Window;

@:access(lime._backend.html5.HTML5Window)
@:access(lime.app.Application)
@:access(lime.graphics.Renderer)
@:access(lime.ui.Window)


class HTML5Application {
	
	
	private var currentUpdate:Float;
	private var deltaTime:Float;
	private var framePeriod:Float;
	private var lastUpdate:Float;
	private var nextUpdate:Float;
	private var parent:Application;
	#if stats
	private var stats:Dynamic;
	#end
	
	
	public inline function new (parent:Application) {
		
		this.parent = parent;
		
		currentUpdate = 0;
		lastUpdate = 0;
		nextUpdate = 0;
		framePeriod = -1;
		
		AudioManager.init ();
		
	}
	
	
	private function convertKeyCode (keyCode:Int):KeyCode {
		
		if (keyCode >= 65 && keyCode <= 90) {
			
			return keyCode + 32;
			
		}
		
		switch (keyCode) {
			
			case 16: return KeyCode.LEFT_SHIFT;
			case 17: return KeyCode.LEFT_CTRL;
			case 18: return KeyCode.LEFT_ALT;
			case 20: return KeyCode.CAPS_LOCK;
			case 144: return KeyCode.NUM_LOCK;
			case 37: return KeyCode.LEFT;
			case 38: return KeyCode.UP;
			case 39: return KeyCode.RIGHT;
			case 40: return KeyCode.DOWN;
			case 45: return KeyCode.INSERT;
			case 46: return KeyCode.DELETE;
			case 36: return KeyCode.HOME;
			case 35: return KeyCode.END;
			case 33: return KeyCode.PAGE_UP;
			case 34: return KeyCode.PAGE_DOWN;
			case 112: return KeyCode.F1;
			case 113: return KeyCode.F2;
			case 114: return KeyCode.F3;
			case 115: return KeyCode.F4;
			case 116: return KeyCode.F5;
			case 117: return KeyCode.F6;
			case 118: return KeyCode.F7;
			case 119: return KeyCode.F8;
			case 120: return KeyCode.F9;
			case 121: return KeyCode.F10;
			case 122: return KeyCode.F11;
			case 123: return KeyCode.F12;
			
		}
		
		return keyCode;
		
	}
	
	
	public function create (config:Config):Void {
		
		parent.config = config;
		
		if (config != null) {
			
			setFrameRate (config.fps);
			var window = new Window (config);
			var renderer = new Renderer (window);
			parent.addWindow (window);
			parent.addRenderer (renderer);
			parent.init (renderer.context);
			
		}
		
	}
	
	
	public function exec ():Int {
		
		Browser.window.addEventListener ("keydown", handleKeyEvent, false);
		Browser.window.addEventListener ("keyup", handleKeyEvent, false);
		Browser.window.addEventListener ("focus", handleWindowEvent, false);
		Browser.window.addEventListener ("blur", handleWindowEvent, false);
		Browser.window.addEventListener ("resize", handleWindowEvent, false);
		Browser.window.addEventListener ("beforeunload", handleWindowEvent, false);
		
		#if stats
		stats = untyped __js__("new Stats ()");
		stats.domElement.style.position = "absolute";
		stats.domElement.style.top = "0px";
		Browser.document.body.appendChild (stats.domElement);
		#end
		
		untyped __js__ ("
			var lastTime = 0;
			var vendors = ['ms', 'moz', 'webkit', 'o'];
			for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
				window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
				window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] 
										   || window[vendors[x]+'CancelRequestAnimationFrame'];
			}
			
			if (!window.requestAnimationFrame)
				window.requestAnimationFrame = function(callback, element) {
					var currTime = new Date().getTime();
					var timeToCall = Math.max(0, 16 - (currTime - lastTime));
					var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
					  timeToCall);
					lastTime = currTime + timeToCall;
					return id;
				};
			
			if (!window.cancelAnimationFrame)
				window.cancelAnimationFrame = function(id) {
					clearTimeout(id);
				};
			
			window.requestAnimFrame = window.requestAnimationFrame;
		");
		
		lastUpdate = Date.now ().getTime ();
		
		handleUpdateEvent ();
		
		return 0;
		
	}
	
	
	public function getFrameRate ():Float {
		
		if (framePeriod < 0) {
			
			return 60;
			
		} else if (framePeriod == 1000) {
			
			return 0;
			
		} else {
			
			return 1000 / framePeriod;
			
		}
		
	}
	
	
	private function handleKeyEvent (event:KeyboardEvent):Void {
		
		if (parent.window != null) {
			
			// space and arrow keys
			
			//switch (event.keyCode) {
				//
				//case 32, 37, 38, 39, 40: event.preventDefault ();
				//
			//}
			
			var keyCode = cast convertKeyCode (event.keyCode != null ? event.keyCode : event.which);
			var modifier = (event.shiftKey ? (KeyModifier.SHIFT) : 0) | (event.ctrlKey ? (KeyModifier.CTRL) : 0) | (event.altKey ? (KeyModifier.ALT) : 0) | (event.metaKey ? (KeyModifier.META) : 0);
			
			if (event.type == "keydown") {
				
				parent.window.onKeyDown.dispatch (keyCode, modifier);
				
			} else {
				
				parent.window.onKeyUp.dispatch (keyCode, modifier);
				
			}
			
		}
		
		
	}
	
	
	private function handleUpdateEvent (?__):Void {
		
		currentUpdate = Date.now ().getTime ();
		
		if (currentUpdate >= nextUpdate) {
			
			#if stats
			stats.begin ();
			#end
			
			deltaTime = currentUpdate - lastUpdate;
			
			parent.onUpdate.dispatch (Std.int (deltaTime));
			
			if (parent.renderer != null) {
				
				parent.renderer.onRender.dispatch (parent.renderer.context);
				parent.renderer.flip ();
				
			}
			
			#if stats
			stats.end ();
			#end
			
			if (framePeriod < 0) {
				
				nextUpdate = currentUpdate;
				nextUpdate = currentUpdate;
				
			} else {
				
				nextUpdate = currentUpdate + framePeriod;
				
				//while (nextUpdate <= currentUpdate) {
					//
					//nextUpdate += framePeriod;
					//
				//}
				
				
			}
			
			lastUpdate = currentUpdate;
			
		}
		
		Browser.window.requestAnimationFrame (cast handleUpdateEvent);
		
	}
	
	
	private function handleWindowEvent (event:js.html.Event):Void {
		
		if (parent.window != null) {
			
			switch (event.type) {
				
				case "focus":
					
					parent.window.onWindowFocusIn.dispatch ();
					parent.window.onWindowActivate.dispatch ();
				
				case "blur":
					
					parent.window.onWindowFocusOut.dispatch ();
					parent.window.onWindowDeactivate.dispatch ();
				
				case "resize":
					
					var cacheWidth = parent.window.width;
					var cacheHeight = parent.window.height;
					
					parent.window.backend.handleResize ();
					
					if (parent.window.width != cacheWidth || parent.window.height != cacheHeight) {
						
						parent.window.onWindowResize.dispatch (parent.window.width, parent.window.height);
						
					}
				
				case "beforeunload":
					
					parent.window.onWindowClose.dispatch ();
				
			}
			
		}
		
	}
	
	
	public function setFrameRate (value:Float):Float {
		
		if (value >= 60) {
			
			framePeriod = -1;
			
		} else if (value > 0) {
			
			framePeriod = 1000 / value;
			
		} else {
			
			framePeriod = 1000;
			
		}
		
		return value;
		
	}
	
	
}<commit_msg>Prevent default on arrows keys again<commit_after>package lime._backend.html5;


import js.html.KeyboardEvent;
import js.Browser;
import lime.app.Application;
import lime.app.Config;
import lime.audio.AudioManager;
import lime.graphics.Renderer;
import lime.ui.KeyCode;
import lime.ui.KeyModifier;
import lime.ui.Window;

@:access(lime._backend.html5.HTML5Window)
@:access(lime.app.Application)
@:access(lime.graphics.Renderer)
@:access(lime.ui.Window)


class HTML5Application {
	
	
	private var currentUpdate:Float;
	private var deltaTime:Float;
	private var framePeriod:Float;
	private var lastUpdate:Float;
	private var nextUpdate:Float;
	private var parent:Application;
	#if stats
	private var stats:Dynamic;
	#end
	
	
	public inline function new (parent:Application) {
		
		this.parent = parent;
		
		currentUpdate = 0;
		lastUpdate = 0;
		nextUpdate = 0;
		framePeriod = -1;
		
		AudioManager.init ();
		
	}
	
	
	private function convertKeyCode (keyCode:Int):KeyCode {
		
		if (keyCode >= 65 && keyCode <= 90) {
			
			return keyCode + 32;
			
		}
		
		switch (keyCode) {
			
			case 16: return KeyCode.LEFT_SHIFT;
			case 17: return KeyCode.LEFT_CTRL;
			case 18: return KeyCode.LEFT_ALT;
			case 20: return KeyCode.CAPS_LOCK;
			case 144: return KeyCode.NUM_LOCK;
			case 37: return KeyCode.LEFT;
			case 38: return KeyCode.UP;
			case 39: return KeyCode.RIGHT;
			case 40: return KeyCode.DOWN;
			case 45: return KeyCode.INSERT;
			case 46: return KeyCode.DELETE;
			case 36: return KeyCode.HOME;
			case 35: return KeyCode.END;
			case 33: return KeyCode.PAGE_UP;
			case 34: return KeyCode.PAGE_DOWN;
			case 112: return KeyCode.F1;
			case 113: return KeyCode.F2;
			case 114: return KeyCode.F3;
			case 115: return KeyCode.F4;
			case 116: return KeyCode.F5;
			case 117: return KeyCode.F6;
			case 118: return KeyCode.F7;
			case 119: return KeyCode.F8;
			case 120: return KeyCode.F9;
			case 121: return KeyCode.F10;
			case 122: return KeyCode.F11;
			case 123: return KeyCode.F12;
			
		}
		
		return keyCode;
		
	}
	
	
	public function create (config:Config):Void {
		
		parent.config = config;
		
		if (config != null) {
			
			setFrameRate (config.fps);
			var window = new Window (config);
			var renderer = new Renderer (window);
			parent.addWindow (window);
			parent.addRenderer (renderer);
			parent.init (renderer.context);
			
		}
		
	}
	
	
	public function exec ():Int {
		
		Browser.window.addEventListener ("keydown", handleKeyEvent, false);
		Browser.window.addEventListener ("keyup", handleKeyEvent, false);
		Browser.window.addEventListener ("focus", handleWindowEvent, false);
		Browser.window.addEventListener ("blur", handleWindowEvent, false);
		Browser.window.addEventListener ("resize", handleWindowEvent, false);
		Browser.window.addEventListener ("beforeunload", handleWindowEvent, false);
		
		#if stats
		stats = untyped __js__("new Stats ()");
		stats.domElement.style.position = "absolute";
		stats.domElement.style.top = "0px";
		Browser.document.body.appendChild (stats.domElement);
		#end
		
		untyped __js__ ("
			var lastTime = 0;
			var vendors = ['ms', 'moz', 'webkit', 'o'];
			for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
				window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
				window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] 
										   || window[vendors[x]+'CancelRequestAnimationFrame'];
			}
			
			if (!window.requestAnimationFrame)
				window.requestAnimationFrame = function(callback, element) {
					var currTime = new Date().getTime();
					var timeToCall = Math.max(0, 16 - (currTime - lastTime));
					var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
					  timeToCall);
					lastTime = currTime + timeToCall;
					return id;
				};
			
			if (!window.cancelAnimationFrame)
				window.cancelAnimationFrame = function(id) {
					clearTimeout(id);
				};
			
			window.requestAnimFrame = window.requestAnimationFrame;
		");
		
		lastUpdate = Date.now ().getTime ();
		
		handleUpdateEvent ();
		
		return 0;
		
	}
	
	
	public function getFrameRate ():Float {
		
		if (framePeriod < 0) {
			
			return 60;
			
		} else if (framePeriod == 1000) {
			
			return 0;
			
		} else {
			
			return 1000 / framePeriod;
			
		}
		
	}
	
	
	private function handleKeyEvent (event:KeyboardEvent):Void {
		
		if (parent.window != null) {
			
			// space and arrow keys
			
			switch (event.keyCode) {
				
				case 32, 37, 38, 39, 40: event.preventDefault ();
				
			}
			
			var keyCode = cast convertKeyCode (event.keyCode != null ? event.keyCode : event.which);
			var modifier = (event.shiftKey ? (KeyModifier.SHIFT) : 0) | (event.ctrlKey ? (KeyModifier.CTRL) : 0) | (event.altKey ? (KeyModifier.ALT) : 0) | (event.metaKey ? (KeyModifier.META) : 0);
			
			if (event.type == "keydown") {
				
				parent.window.onKeyDown.dispatch (keyCode, modifier);
				
			} else {
				
				parent.window.onKeyUp.dispatch (keyCode, modifier);
				
			}
			
		}
		
		
	}
	
	
	private function handleUpdateEvent (?__):Void {
		
		currentUpdate = Date.now ().getTime ();
		
		if (currentUpdate >= nextUpdate) {
			
			#if stats
			stats.begin ();
			#end
			
			deltaTime = currentUpdate - lastUpdate;
			
			parent.onUpdate.dispatch (Std.int (deltaTime));
			
			if (parent.renderer != null) {
				
				parent.renderer.onRender.dispatch (parent.renderer.context);
				parent.renderer.flip ();
				
			}
			
			#if stats
			stats.end ();
			#end
			
			if (framePeriod < 0) {
				
				nextUpdate = currentUpdate;
				nextUpdate = currentUpdate;
				
			} else {
				
				nextUpdate = currentUpdate + framePeriod;
				
				//while (nextUpdate <= currentUpdate) {
					//
					//nextUpdate += framePeriod;
					//
				//}
				
				
			}
			
			lastUpdate = currentUpdate;
			
		}
		
		Browser.window.requestAnimationFrame (cast handleUpdateEvent);
		
	}
	
	
	private function handleWindowEvent (event:js.html.Event):Void {
		
		if (parent.window != null) {
			
			switch (event.type) {
				
				case "focus":
					
					parent.window.onWindowFocusIn.dispatch ();
					parent.window.onWindowActivate.dispatch ();
				
				case "blur":
					
					parent.window.onWindowFocusOut.dispatch ();
					parent.window.onWindowDeactivate.dispatch ();
				
				case "resize":
					
					var cacheWidth = parent.window.width;
					var cacheHeight = parent.window.height;
					
					parent.window.backend.handleResize ();
					
					if (parent.window.width != cacheWidth || parent.window.height != cacheHeight) {
						
						parent.window.onWindowResize.dispatch (parent.window.width, parent.window.height);
						
					}
				
				case "beforeunload":
					
					parent.window.onWindowClose.dispatch ();
				
			}
			
		}
		
	}
	
	
	public function setFrameRate (value:Float):Float {
		
		if (value >= 60) {
			
			framePeriod = -1;
			
		} else if (value > 0) {
			
			framePeriod = 1000 / value;
			
		} else {
			
			framePeriod = 1000;
			
		}
		
		return value;
		
	}
	
	
}
{'fraction_non_alphanumeric': 0.11899835913533566, 'fraction_numerical': 0.020261111507455232, 'mean_word_length': 2.7968580715059588, 'pattern_counts': {'":': 8, '<': 13, '<?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': '6874121', 'n_tokens_mistral': 5981, 'n_tokens_neox': 4358, 'n_words': 1005}