About Monkey 2 › Forums › Monkey 2 Code Library › Updated joystick banana
This topic contains 2 replies, has 2 voices, and was last updated by AdamStrange
1 year, 1 month ago.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
December 27, 2017 at 8:19 am #12457
Here’s an updated version of the joystick code that operates with hats (sometimes d-pads are hats!) and only shows what the joypad is listing.
Also the canvas matrix stuff is removedMonkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141[crayon-5c6bbc2760c3a561129277 inline="true" ]Namespace test#Import "<std>"#Import "<mojo>"Using std..Using mojo..Function Format:String( v:Float )Local s:float =Sgn(v)Local n:int = Int( Abs(v)*100 )Return (s>=0 ? "+" Else "-")+(n/100)+"."+(n Mod 100)EndClass PlayerField id:IntField joystick:JoystickMethod New( id:Int )Self.id = idSelf.joystick = Joystick.Open( id )EndMethod Update:int( canvas:Canvas, ypos:int )ypos += 16canvas.DrawText( "Joystick "+id, 0, ypos )'update joystick stateIf joystick And Not joystick.Attachedjoystick.Close()joystick = NullEndifIf Not joystickjoystick=Joystick.Open( id )If Not joystickypos += 16canvas.DrawText( "No Joystick available", 0, ypos )Return yposEndifEndif'draw joystick info.ypos += 16canvas.DrawText( "Name = "+joystick.Name, 0, ypos )ypos += 16canvas.DrawText( "GUID = "+joystick.GUID, 0, ypos )If joystick.NumAxes > 0 Thenypos += 16canvas.DrawText( "Analog Axes = "+joystick.NumAxes, 0, ypos )ypos += 16For Local axis:int = 0 Until joystick.NumAxesLocal v:=Format( joystick.GetAxis( axis ) )canvas.DrawText( " A"+axis+"="+v, axis * 80, ypos )NextEnd ifIf joystick.NumButtons > 0 Thenypos += 16canvas.DrawText( "Buttons = "+joystick.NumButtons, 0, ypos )ypos += 16For Local button:int = 0 Until joystick.NumButtonscanvas.DrawText( " B"+button+"="+Int( joystick.ButtonDown( button ) ), (button Mod 8)*80, ypos + (button/8)*16 )Nextypos += int(joystick.NumButtons/8)*16End IfIf joystick.NumHats > 0 Thenypos += 16canvas.DrawText( "Hats = "+joystick.NumHats, 0, ypos )For Local hat:int = 0 To joystick.NumHatsypos += 16local str:string = ""Select joystick.GetHat( hat )Case JoystickHat.Centeredstr = "Centered"Case JoystickHat.Upstr = "Up"Case JoystickHat.Downstr = "Down"Case JoystickHat.Rightstr = "Right"Case JoystickHat.Leftstr = "Left"Case JoystickHat.RightUpstr = "RightUp"Case JoystickHat.RightDownstr = "RightDown"Case JoystickHat.LeftUpstr = "LeftUp"Case JoystickHat.LeftDownstr = "LeftDown"End selectcanvas.DrawText( " H"+hat+"="+str, 0, ypos )NextEnd IfReturn yposEndEnd'***** MainWindow *****Class MainWindow Extends WindowField players:Player[] = New Player[4]Method New()Super.New( "Joystick test", 640, 480 )For Local i:int = 0 Until 4players[i]=New Player( i )NextEndMethod OnRender( canvas:Canvas ) OverrideRequestRender()canvas.DrawText( "Number of Joysticks = "+Joystick.NumJoysticks(),0,0 )local ypos:int = 16For Local i:int = 0 Until Joystick.NumJoysticks()ypos = players[i].Update( canvas, ypos )+16NextEndEndFunction Main()New AppInstanceNew MainWindowApp.Run()EndJanuary 3, 2018 at 6:50 pm #12610Am I right that joystick always works the same when compiled for the browser? I bought one and want to start adding this to my games.
January 7, 2018 at 7:27 am #12723I would do some check first. I would have thought so, but not tested any web stuff
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.