About Monkey 2 › Forums › Monkey 2 Code Library › A simple Applet framework
This topic contains 2 replies, has 2 voices, and was last updated by
Simon Armstrong 1 year, 9 months ago.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
July 2, 2017 at 12:12 am #9076Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224' applet class' manage preferences by json in sdl generated prefpath' remember window dimensions' fullscreen toggle on F1' long press on Escape to close#Import "<std>"#Import "<mojo>"#Import "<sdl2>"Using std..Using mojo..Using sdl2..ExternFunction SDL_GetPrefPath:Byte Ptr(org:CString,app:CString)PublicGlobal AppletName:="MyNewApplet"Global AppletAuthor:="nitrologic"Global prefsPath:=String.FromCString(SDL_GetPrefPath(AppletAuthor,AppletName))Global prefsFile:=AppletName+".prefs.json"Global DefaultWindowFlags:=WindowFlags.Resizable|WindowFlags.HighDPIClass AppletField hasPrefs:BoolField windowRect:RectiField windowFullscreen:BoolField window:AppWindowField defaults:JsonObjectMethod New()LoadPrefs()Local title:=AppletNameIf hasPrefswindow=New AppWindow(Self, windowRect, windowFullscreen, title)Elsewindow=New AppWindow(Self, title)EndifEndMethod KeyDown(key:Key)EndMethod KeyUp(key:Key)EndFunction TrueFalse:String(b:Bool)If b Return "true"Return "false"EndMethod DefaultNumber:Double(name:String)Return defaults?defaults.GetNumber(name) Else 0EndMethod LoadPrefs()defaults=JsonObject.Load(prefsPath+prefsFile)Local prefs:=defaultsIf Not prefsPrint "prefs not loaded from "+prefsPathLocal raw:=LoadString(prefsPath)Print "raw="+rawReturnEndifIf prefs.Contains("winRect")Local a:=prefs.GetArray("winRect")Local f:=prefs.GetBool("winFullscreen")Local x:=a.GetNumber(0)Local y:=a.GetNumber(1)Local w:=a.GetNumber(2)Local h:=a.GetNumber(3)If w<240 w=240If h<120 h=120windowRect=New Recti(x,y,x+w,y+h)windowFullscreen=fhasPrefs=TruePrint "prefs loaded r="+x+","+y+","+w+","+h+" f="+TrueFalse(f)EndifEndMethod OnFrame(window:Window)If window.FullscreenwindowFullscreen=TrueElsewindowFullscreen=FalsewindowRect=window.FrameEndifEndFunction JsonString:String(args:Stack<String>)Local s:StringFor Local i:=0 Until args.Length Step 2Local a:=args[i+0]Local b:=args[i+1]If i>0 s+=","s+="~q"+a+"~q:"+bNextReturn "{"+s+"}"EndMethod OnClose()Local winRect:="["+windowRect.X+","+windowRect.Y+","+windowRect.Width+","+windowRect.Height+"]"Local winFS:=windowFullscreen?"true"else"false"Local json:=New Stack<String>json.Add("winRect")json.Add(winRect)json.Add("winFullscreen")json.Add(winFS)' add app specific serialisation from prefs hereIf GetFileType(prefsPath)=FileType.None CreateDir(prefsPath)Local js:=JsonString(json)If Not SaveString(js, prefsPath+prefsFile)'Notify("Warning", "Unable to open "+path, False)Print "Unable to save prefs to "+prefsPathReturnEndifPrint "saved prefs as "+js+" in "+prefsPathApp.Terminate()EndEndClass AppWindow Extends WindowField applet:AppletField goFullscreen:BoolMethod New(host:Applet, rect:Recti, fullscreen:bool, title:String)Super.New(title,rect, DefaultWindowFlags)applet=hostgoFullscreen=fullscreenCreate()EndMethod New(host:Applet, title:String)Super.New(title,800,600,DefaultWindowFlags)applet=hostCreate()EndMethod OnRender( display:Canvas ) OverrideIf goFullscreengoFullscreen=FalseFullscreen=TrueEndifEndMethod Create()applet.OnFrame(Self)EndMethod OnWindowEvent(event:WindowEvent) OverrideSelect event.TypeCase EventType.WindowCloseapplet.OnClose()Case EventType.WindowResizedapplet.OnFrame(Self)Case EventType.WindowMovedapplet.OnFrame(Self)EndEndField EscapeTime:IntMethod EscapeDown()EscapeTime=App.MillisecsEndMethod EscapeUp()Local duration:=App.Millisecs-EscapeTimeIf duration>300applet.OnClose()EndifEndMethod OnKeyEvent( event:KeyEvent ) OverrideSelect event.TypeCase EventType.KeyDownSelect event.KeyCase Key.F1Fullscreen = Not Fullscreenapplet.OnFrame(Self)Case Key.EscapeEscapeDown()Defaultapplet.KeyDown(event.Key)EndCase EventType.KeyUpSelect event.KeyCase Key.EscapeEscapeUp()Defaultapplet.KeyUp(event.Key)EndEndEndEndFunction Main()New AppInstanceNew AppletApp.Run()EndJuly 2, 2017 at 5:14 am #9079
interesting stuff, especially the bits about SDL_GetPrefPath. Something I was wanting to know how to do
July 3, 2017 at 9:33 pm #9111I was thinking version control would be a great feature with the base class calling itch.io with httprequest and automatically downloading any new builds.
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.