Forráskód Böngészése

Added mojox.GridView

Mark Sibly 8 éve
szülő
commit
d1b61571e5

+ 5 - 2
modules/mojox/assets/themes/default.json

@@ -193,7 +193,7 @@
 			"backgroundColor":"content",
 			"border":[ 8,8,7,7 ]
 		},
-
+		
 		"TabView":{
 		},
 		
@@ -240,7 +240,10 @@
 				}
 			}
 		},
-		
+
+		"GridView":{
+		},
+
 		"TableView":{
 			"extends":"DockingView"
 		},

+ 133 - 0
modules/mojox/gridview.monkey2

@@ -0,0 +1,133 @@
+
+Namespace mojox
+
+Class GridView Extends View
+	
+	Method New( width:Int,height:Int )
+		_gridw=width
+		_gridh=height
+		Style=GetStyle( "GridView" )
+	End
+	
+	Property GridWidth:Int()
+		
+		Return _gridw
+		
+	Setter( gridw:Int )
+		If gridw=_gridw Return
+		
+		_gridw=gridw
+		
+		RequestRender()
+	End
+	
+	Property GridHeight:Int()
+		
+		Return _gridh
+		
+	Setter( gridh:Int )
+		If gridh=_gridh Return
+		
+		_gridh=gridh
+
+		RequestRender()
+	End
+	
+	Method Clear()
+		
+		For Local cell:=Eachin _cells
+			RemoveChildView( cell.view )
+		Next
+		_cells.Clear()
+		RequestRender()
+	End
+	
+	Method AddView( view:View,x:Int,y:Int,width:Int=1,height:Int=1 )
+		
+		_cells.Add( New Cell( view,x,y,width,height ) )
+		AddChildView( view )
+		RequestRender()
+	End
+	
+	Method ReplaceView( view:View,with:View )
+		
+		For Local cell:=Eachin _cells
+			If cell.view<>view Continue
+			RemoveChildView( view )
+			AddChildView( with )
+			cell.view=with
+			RequestRender()
+			Return
+		Next
+		
+	End
+	
+	Method RemoveView( view:View )
+		
+		For Local cell:=Eachin _cells
+			If cell.view<>view Continue
+			RemoveChildView( view )
+			_cells.Remove( cell )
+			RequestRender()
+			Return
+		Next
+		
+	End
+	
+	Protected
+	
+	Method OnMeasure:Vec2i() Override
+
+		Local maxw:=0,maxh:=0
+		
+		For Local cell:=Eachin _cells
+			
+			Local size:=cell.view.MeasuredSize
+			
+			maxw=Max( maxw,size.x/cell.w )
+			maxh=Max( maxh,size.y/cell.h )
+		Next
+		
+		Return New Vec2i( maxw * _gridw,maxh * _gridh )
+	End
+	
+	Method OnLayout() Override
+		
+		For Local cell:=Eachin _cells
+			
+			Local x0:=cell.x * Width / _gridw
+			Local x1:=(cell.x+cell.w) * Width / _gridw
+
+			Local y0:=cell.y * Height / _gridw
+			Local y1:=(cell.y+cell.h) * Height / _gridh
+			
+			cell.view.Frame=New Recti( x0,y0,x1,y1 )
+		Next
+			
+	End
+	
+	Private
+	
+	Struct Cell
+		Field view:View
+		Field x:Int
+		Field y:Int
+		Field w:Int
+		Field h:Int
+		
+		Method New( view:View,x:Int,y:Int,w:Int,h:Int )
+			Self.view=view
+			Self.x=x
+			Self.y=y
+			Self.w=w
+			Self.h=h
+		End
+	End
+	
+	Field _cellw:Int
+	Field _cellh:Int
+	Field _gridw:Int
+	Field _gridh:Int
+	Field _cells:=New Stack<Cell>
+End
+

+ 2 - 0
modules/mojox/mojox.monkey2

@@ -25,6 +25,8 @@ Namespace mojox
 
 #Import "textview"
 
+#Import "gridview"
+
 #Import "tableview"
 
 #Import "textfield"

+ 65 - 0
modules/mojox/tests/gridview_test.monkey2

@@ -0,0 +1,65 @@
+
+#Import "<std>"
+#Import "<mojo>"
+#Import "<mojox>"
+
+Using std..
+Using mojo..
+Using mojox..
+
+Class MyWindow Extends Window
+	
+	Field mainView:GridView
+	
+	Field gameView:Label
+	Field view1:Label
+	Field view2:Label
+	
+	Method New()
+		Super.New( "GridView",640,480,WindowFlags.Resizable )
+
+		'Create GameView		
+		gameView=New Label
+		gameView.Layout="fill"
+		gameView.Text="GameView"
+		gameView.TextGravity=New Vec2f( .5 )
+		Local gameViewStyle:=gameView.Style.Copy()
+		gameViewStyle.BackgroundColor=Color.Red
+		gameView.Style=gameViewStyle
+		
+		'Create View1
+		view1=New Label
+		view1.Layout="fill"
+		view1.Text="View1"
+		view1.TextGravity=New Vec2f( .5 )
+		Local view1Style:=view1.Style.Copy()
+		view1Style.BackgroundColor=Color.Green
+		view1.Style=view1Style
+		
+		'Create View2
+		view2=New Label
+		view2.Layout="fill"
+		view2.Text="View2"
+		view2.TextGravity=New Vec2f( .5 )
+		Local view2Style:=view2.Style.Copy()
+		view2Style.BackgroundColor=Color.Blue
+		view2.Style=view2Style
+
+		'Create Main GridView
+		mainView=New GridView( 8,8 )
+		mainView.AddView( gameView,0,0,8,4 )
+		mainView.AddView( view1,0,4,4,4 )
+		mainView.AddView( view2,4,4,4,4 )
+		
+		ContentView=mainView
+	End
+	
+End
+
+Function Main()
+	
+	New AppInstance
+	New MyWindow
+	App.Run()
+	
+End