Focusproblem with MOJO_AUTO_SUSPEND_ENABLED = fals

Monkey Targets Forums/HTML5/Focusproblem with MOJO_AUTO_SUSPEND_ENABLED = fals

PhillipK(Posted 2015) [#1]
Hey all,

While creating some stuff for my HTML target, i've found a little issue and i cant tell if i miss something.

Given code:
Strict

#MOJO_AUTO_SUSPEND_ENABLED=False

Import mojo

Function Main:Int()
	New Game()
	Return 0
End

Class Game Extends App
	Field x:Int = 0
	
	Method OnCreate:Int()
		SetUpdateRate(60)
		
		Return 0
	End
	
	Method OnUpdate:Int()
		x += KeyDown(KEY_D) - KeyDown(KEY_A)
		
		Return 0
	End
	
	Method OnRender:Int()
		Cls()
		
		DrawText("X: " + x, 5, 5)
		
		Return 0
	End
End


My Opera / Chrome behave unexpected. If my GameCanvas looses the focus, i cant re-focus it with my mouse again.
Howevery, if i use the javascript console to do so or even change some vars of my game, everything works.

To test it, just do the following steps:
- click on the canvas
- use A and D to change the variable and watch the drawn text
- click somewhere outside the game canvas (e.g. the output console at the bottom)
- click on the canvas again and use A / D again.

The variable wont change for me (the canvas never gets the focus back by clicking it)

At first i thought its a bug that i created with my code, but even with this very clean gamecode i can recreate the bug.
Haven't tested it on firefox yet.

But i know that #MOJO_AUTO_SUSPEND_ENABLED = false worked on html5 targets in the past with previous versions of monkey. Currently i'm running MonkeyXPro84e,

Is there anything new that i miss out? Or is this a bug?
If its a bug, does anyone knows a little workarround for me? :/ I'd really need to avoid suspend for my project, because i interact with the browser itself while using the canvas to draw some stuff.


k.o.g.(Posted 2015) [#2]
@German
Kenn Ich :D
War aber zu faul es zu beschreiben :D

Good luck


PhillipK(Posted 2015) [#3]
Okay, i've found the issue... It looks like a bug to me!

in the file "\targets\modules\natvie\html5game.js" the default events for the canvas are attached.

The following should be self-explaining:
	canvas.onclick=function( e ){
		if( game.Suspended() ){
			canvas.focus();
		}
		eatEvent( e );
		return;
	}


ONLY if the game is suspended, the canvas will get the focus.
I dont get it, why the browser refuses to focus a clicked element like the canvas, but this part looks also wrong.
.focus() is a javascript function to focus the element (oh, obviously.. Phillip, you genius..). However, if we use MOJO_AUTO_SUSPEND_ENABLED = false, the game wont be ever Suspended and the if-statment will fail. canvas.focus() will never be fired and thus, the user cant reactivate the canvas ever again.

To fix it, change the code as the following:
	canvas.onclick=function( e ){
		if(document.activeElement != canvas){
			canvas.focus();
		}
		
//		if( game.Suspended() ){
//			canvas.focus();
//		}
		eatEvent( e );
		return;
	}


if keeps the old idea for the code: Only focus once, whenever the canvas is not focussed. I dont know if there is another way, like attaching the handler after the whole game is loaded or something (include external js file to monkey maybe?) and my tests failed here. Anyways, hacking the target code helps yet and hopefully mark will find a better way to fix this issue :)