About Monkey 2 › Forums › Monkey 2 Code Library › Text Handels
This topic contains 0 replies, has 1 voice, and was last updated by 
 TomToad 1 year, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
September 14, 2017 at 3:31 pm #10468
One difficult aspect of designing a game is proper placement of your text. Usually placement is done with a series of trial and error, or complex math to find relative positioning on the screen. Sometimes changing resolution or changing fonts can mess with our carefully positioned text.
Monkey2 gives us the ability to define handles in the text for better placement. By using reference points and adjusting the handle, we can automatically position our text with greater ease. There is no need to calculate the text size as the handle uses values of 0.0 for left, 1.0 for right, and values in-between, regardless of text length.
Below is 25 examples of text placement, showing the usage of handles, as well as the flexibility they contain.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )Self.ClearColor = Color.BlackEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()Local rect:= New Rectf(100.0,30.0,Width-100.0,Height-30.0)canvas.Color = Color.Greycanvas.DrawRect(rect)canvas.Color = Color.White'Top leftcanvas.DrawText("0 ",rect.Left,rect.Top,1.0,1.0)canvas.DrawText(" 1",rect.Left,rect.Top,0.0,1.0)canvas.DrawText("2 ",rect.Left,rect.Top,1.0,0.0)canvas.DrawText(" 3",rect.Left,rect.Top,0.0,0.0)'Top Rightcanvas.DrawText("0 ",rect.Right,rect.Top,1.0,1.0)canvas.DrawText(" 1",rect.Right,rect.Top,0.0,1.0)canvas.DrawText("2 ",rect.Right,rect.Top,1.0,0.0)canvas.DrawText(" 3",rect.Right,rect.Top,0.0,0.0)'Bottom Leftcanvas.DrawText("0 ",rect.Left,rect.Bottom,1.0,1.0)canvas.DrawText(" 1",rect.Left,rect.Bottom,0.0,1.0)canvas.DrawText("2 ",rect.Left,rect.Bottom,1.0,0.0)canvas.DrawText(" 3",rect.Left,rect.Bottom,0.0,0.0)'Bottom Rightcanvas.DrawText("0 ",rect.Right,rect.Bottom,1.0,1.0)canvas.DrawText(" 1",rect.Right,rect.Bottom,0.0,1.0)canvas.DrawText("2 ",rect.Right,rect.Bottom,1.0,0.0)canvas.DrawText(" 3",rect.Right,rect.Bottom,0.0,0.0)'Leftcanvas.DrawText("Left1 ",rect.Left,rect.Center.y,1.0,.5)canvas.DrawText(" Left2",rect.Left,rect.Center.y,0.0,.5)'Rightcanvas.DrawText("Right1 ",rect.Right,rect.Center.y,1.0,.5)canvas.DrawText(" Right2",rect.Right,rect.Center.y,0.0,.5)'Topcanvas.DrawText("Top1",rect.Center.x,rect.Top,.5,1.0)canvas.DrawText("Top2",rect.Center.x,rect.Top,.5,0.0)'Bottomcanvas.DrawText("Bottom1",rect.Center.x,rect.Bottom,.5,1.0)canvas.DrawText("Bottom2",rect.Center.x,rect.Bottom,.5,0.0)'Centercanvas.DrawText("Center",rect.Center.x,rect.Center.y,.5,.5)EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End - 
		AuthorPosts
 
You must be logged in to reply to this topic.