فهرست منبع

More jsonifier experiments.

Mark Sibly 7 سال پیش
والد
کامیت
8100ed1b9e
3فایلهای تغییر یافته به همراه220 افزوده شده و 6 حذف شده
  1. 10 0
      src/jsonifier/invocation.monkey2
  2. 5 4
      src/jsonifier/jsonifier.monkey2
  3. 205 2
      src/jsonifier/test.monkey2

+ 10 - 0
src/jsonifier/invocation.monkey2

@@ -74,7 +74,17 @@ Class Invocation
 			Local match:=True
 			Local match:=True
 			For Local i:=0 Until args.Length
 			For Local i:=0 Until args.Length
 				If args[i]
 				If args[i]
+					
 					If args[i].Type.ExtendsType( ftype.ParamTypes[i] ) Continue
 					If args[i].Type.ExtendsType( ftype.ParamTypes[i] ) Continue
+					
+					If args[i].Type.Kind="Class" And ftype.ParamTypes[i].Kind="Class"
+						
+						Local obj:=Cast<Object>( args[i] )
+						
+						If Not obj Or obj.DynamicType.ExtendsType( ftype.ParamTypes[i] ) Continue
+						
+					Endif
+					
 				Else
 				Else
 					If ftype.ParamTypes[i].Kind="Class" Continue
 					If ftype.ParamTypes[i].Kind="Class" Continue
 				Endif
 				Endif

+ 5 - 4
src/jsonifier/jsonifier.monkey2

@@ -8,7 +8,7 @@ Class Jsonifier
 		
 		
 		Local inst:=New Instance
 		Local inst:=New Instance
 		inst.obj=obj
 		inst.obj=obj
-		inst.id=_insts.Length
+		inst.id="@"+String(_insts.Length)
 		inst.ctor=ctor
 		inst.ctor=ctor
 		inst.initialState=JsonifyState( obj )
 		inst.initialState=JsonifyState( obj )
 		
 		
@@ -39,6 +39,7 @@ Class Jsonifier
 			Local inst:=_insts[i]
 			Local inst:=_insts[i]
 			Local jobj:=New JsonObject
 			Local jobj:=New JsonObject
 			
 			
+			jobj["id"]=New JsonString( inst.id )
 			jobj["type"]=New JsonString( inst.obj.DynamicType.Name )
 			jobj["type"]=New JsonString( inst.obj.DynamicType.Name )
 			jobj["ctor"]=Jsonify( inst.ctor )
 			jobj["ctor"]=Jsonify( inst.ctor )
 			
 			
@@ -137,7 +138,7 @@ Class Jsonifier
 			If jvalue.IsNull
 			If jvalue.IsNull
 				Return type.NullValue
 				Return type.NullValue
 			Elseif jvalue.IsString
 			Elseif jvalue.IsString
-				Local id:=Int( jvalue.ToString() )
+				Local id:=Int( jvalue.ToString().Slice( 1 ) )
 				Assert( id>=0 And id<_dejsonified.Length,"Dejsonify error" )
 				Assert( id>=0 And id<_dejsonified.Length,"Dejsonify error" )
 				Return _dejsonified[id]
 				Return _dejsonified[id]
 			Endif
 			Endif
@@ -180,7 +181,7 @@ Class Jsonifier
 	
 	
 	Class Instance
 	Class Instance
 		Field obj:Object
 		Field obj:Object
-		Field id:Int
+		Field id:String
 		Field ctor:Invocation
 		Field ctor:Invocation
 		Field initialState:JsonObject
 		Field initialState:JsonObject
 	End
 	End
@@ -188,7 +189,7 @@ Class Jsonifier
 	Field _insts:=New Stack<Instance>
 	Field _insts:=New Stack<Instance>
 	
 	
 	Field _instsByObj:=New Map<Object,Instance>
 	Field _instsByObj:=New Map<Object,Instance>
-	Field _instsById:=New Map<Int,Instance>
+	Field _instsById:=New StringMap<Instance>
 	
 	
 	Field _dejsonified:=New Stack<Object>
 	Field _dejsonified:=New Stack<Object>
 	
 	

+ 205 - 2
src/jsonifier/test.monkey2

@@ -18,12 +18,19 @@ Global jsonifier:=New Jsonifier
 
 
 Class Component
 Class Component
 	
 	
+	'simple contructor
 	Method New( entity:Entity )
 	Method New( entity:Entity )
 		
 		
 		_entity=entity
 		_entity=entity
 		
 		
 		_entity.AddComponent( Self )
 		_entity.AddComponent( Self )
 	End
 	End
+
+	'copy contructor
+	Method New( component:Component,entity:Entity )
+		
+		Self.New( entity )
+	End
 	
 	
 	Property Entity:Entity()
 	Property Entity:Entity()
 		
 		
@@ -32,11 +39,20 @@ Class Component
 	
 	
 	Protected
 	Protected
 	
 	
+	Method OnCopy( entity:Entity ) Virtual
+		
+	End
+	
 	Method SaveInitialState()
 	Method SaveInitialState()
 		
 		
 		If editing jsonifier.AddInstance( Self,New Variant[]( _entity ) )
 		If editing jsonifier.AddInstance( Self,New Variant[]( _entity ) )
 	End
 	End
 	
 	
+	Method SaveInitialState( component:Component )
+		
+		If editing jsonifier.AddInstance( Self,New Variant[]( component,_entity ) )
+	End
+	
 	Private
 	Private
 	
 	
 	Field _entity:Entity
 	Field _entity:Entity
@@ -45,13 +61,26 @@ End
 
 
 Class Behaviour Extends Component
 Class Behaviour Extends Component
 	
 	
+	'simple contructor
 	Method New( entity:Entity )
 	Method New( entity:Entity )
 		
 		
 		Super.New( entity )
 		Super.New( entity )
+		
+		Color=graphics.Color.White
 			
 			
 		SaveInitialState()
 		SaveInitialState()
 	End
 	End
 	
 	
+	'copy constructor
+	Method New( behaviour:Behaviour,entity:Entity )
+		
+		Super.New( entity )
+		
+		Color=behaviour.Color
+		
+		SaveInitialState( behaviour )
+	End
+	
 	Property Color:Color()
 	Property Color:Color()
 		
 		
 		Return _color
 		Return _color
@@ -61,17 +90,32 @@ Class Behaviour Extends Component
 		_color=color
 		_color=color
 	End
 	End
 	
 	
-	Private
+	Internal
+	
+	Method OnCopy( entity:Entity ) Override
+		
+		New Behaviour( Self,entity )
+	End
 	
 	
-	Field _color:Color=graphics.Color.White
+	Private
 	
 	
+	Field _color:Color
 End
 End
 
 
 Class Entity
 Class Entity
 	
 	
+	'simple ctor
 	Method New( parent:Entity )
 	Method New( parent:Entity )
 		
 		
 		_parent=parent
 		_parent=parent
+		
+		If _parent _parent._children.Add( Self )
+	End
+	
+	'copy ctor
+	Method New( entity:Entity,parent:Entity )
+		
+		Self.New( parent )
 	End
 	End
 	
 	
 	Property Visible:Bool()
 	Property Visible:Bool()
@@ -83,6 +127,15 @@ Class Entity
 		_visible=visible
 		_visible=visible
 	End
 	End
 	
 	
+	Method Copy:Entity( parent:Entity ) Virtual
+		
+		Local copy:=OnCopy( parent )
+		
+		CopyTo( copy )
+		
+		Return copy
+	End
+	
 	Method AddComponent<T>:T()
 	Method AddComponent<T>:T()
 		
 		
 		Local component:=New T( Self )
 		Local component:=New T( Self )
@@ -92,15 +145,40 @@ Class Entity
 	
 	
 	Protected
 	Protected
 	
 	
+	Method OnCopy:Entity( parent:Entity ) Virtual
+		
+		Return New Entity( Self,parent )
+	End
+	
+	Method CopyTo( copy:Entity )
+		
+		For Local child:=Eachin _children
+			
+			child.CopyTo( child.OnCopy( copy ) )
+		Next
+		
+		For Local c:=Eachin _components
+			
+			c.OnCopy( copy )
+		Next
+	End
+	
 	Method SaveInitialState()
 	Method SaveInitialState()
 		
 		
 		If editing jsonifier.AddInstance( Self,New Variant[]( _parent ) )
 		If editing jsonifier.AddInstance( Self,New Variant[]( _parent ) )
 	End
 	End
+
+	Method SaveInitialState( entity:Entity )
+		
+		If editing jsonifier.AddInstance( Self,New Variant[]( entity,_parent ) )
+	End
 	
 	
 	Private
 	Private
 	
 	
 	Field _parent:Entity
 	Field _parent:Entity
 	
 	
+	Field _children:=New Stack<Entity>
+	
 	Field _visible:Bool
 	Field _visible:Bool
 	
 	
 	Field _components:=New Stack<Component>
 	Field _components:=New Stack<Component>
@@ -118,11 +196,53 @@ Class Camera Extends Entity
 
 
 		Super.New( parent )
 		Super.New( parent )
 		
 		
+		FOV=90
+		
 		SaveInitialState()
 		SaveInitialState()
 		
 		
 		Visible=True
 		Visible=True
 	End
 	End
 	
 	
+	Method New( camera:Camera,parent:Entity )
+		
+		Super.New( camera,parent )
+		
+		FOV=camera.FOV
+		
+		SaveInitialState( camera )
+		
+		Visible=True
+	End
+	
+	Property FOV:Float()
+	
+		Return _fov
+	
+	Setter( fov:Float )
+		
+		_fov=fov
+	End
+	
+	Method Copy:Camera( parent:Entity ) Override
+		
+		Local camera:=OnCopy( parent )
+		
+		CopyTo( camera )
+		
+		Return camera
+	End
+
+	Protected
+		
+	Method OnCopy:Camera( parent:Entity ) Override
+		
+		Return New Camera( Self,parent )
+	End
+	
+	Private
+	
+	Field _fov:Float
+	
 End
 End
 
 
 Class Light Extends Entity
 Class Light Extends Entity
@@ -136,6 +256,31 @@ Class Light Extends Entity
 		Visible=True
 		Visible=True
 	End
 	End
 
 
+	Method New( light:Light,parent:Entity )
+		
+		Super.New( parent )
+		
+		SaveInitialState( light )
+		
+		Visible=True
+	End
+
+	Method Copy:Light( parent:Entity ) Override
+		
+		Local light:=OnCopy( parent )
+		
+		CopyTo( light )
+		
+		Return light
+	End
+
+	Protected
+		
+	Method OnCopy:Light( parent:Entity ) Override
+		
+		Return New Light( Self,parent )
+	End
+	
 End
 End
 
 
 Class Model Extends Entity
 Class Model Extends Entity
@@ -144,22 +289,64 @@ Class Model Extends Entity
 		
 		
 		Super.New( parent )
 		Super.New( parent )
 		
 		
+		Mesh=""
+		
 		SaveInitialState()
 		SaveInitialState()
 		
 		
 		Visible=True
 		Visible=True
 	End
 	End
 	
 	
+	Method New( model:Model,parent:Entity )
+		
+		Super.New( parent )
+		
+		Mesh=model.Mesh
+		
+		SaveInitialState( model )
+		
+		Visible=True
+	End
+	
+	Property Mesh:String()
+		
+		Return _mesh
+		
+	Setter( mesh:String )
+		
+		_mesh=mesh
+	End
+	
 	Function Load:Model( path:String,parent:Entity )
 	Function Load:Model( path:String,parent:Entity )
 		
 		
 		Local model:=New Model( parent,True )
 		Local model:=New Model( parent,True )
 		
 		
+		model.Mesh="<"+path+">"
+		
 		If editing jsonifier.AddInstance( model,"Load",New Variant[]( path,parent ) )
 		If editing jsonifier.AddInstance( model,"Load",New Variant[]( path,parent ) )
 		
 		
 		Return model
 		Return model
 	End
 	End
 	
 	
+	Method Copy:Model( parent:Entity ) Override
+		
+		Local model:=OnCopy( parent )
+		
+		CopyTo( model )
+		
+		Return model
+	End
+
+	Protected
+		
+	Method OnCopy:Model( parent:Entity ) Override
+		
+		Return New Model( Self,parent )
+	End
+	
 	Private
 	Private
 	
 	
+	Field _mesh:String
+	
 	Method New( parent:Entity,loading:Bool )
 	Method New( parent:Entity,loading:Bool )
 		
 		
 		Super.New( parent )
 		Super.New( parent )
@@ -173,6 +360,20 @@ Function CreateScene()
 
 
 	jsonifier=New Jsonifier
 	jsonifier=New Jsonifier
 	
 	
+	Local model1:=Model.Load( "model1.png",Null )
+	
+	Local behavour1:=New Behaviour( model1 )
+	
+	Local model2:=model1.Copy( Null )
+	
+End
+
+Function CreateScene2()
+	
+	Print "CreateScene"
+
+	jsonifier=New Jsonifier
+	
 	Local camera:=New Camera( Null )
 	Local camera:=New Camera( Null )
 	
 	
 	Local light:=New Light( Null )
 	Local light:=New Light( Null )
@@ -186,6 +387,8 @@ Function CreateScene()
 		Local component:=New Behaviour( model )
 		Local component:=New Behaviour( model )
 	Next
 	Next
 	
 	
+	Local copy:=root.Copy( Null )
+	
 End
 End
 
 
 Function SaveScene:JsonObject()
 Function SaveScene:JsonObject()