/// <summary> /// Creates a new segment. The start and endpoints must be different. /// </summary> /// <param name="start">The starting point.</param> /// <param name="end">The ending point.</param> public Segment2D(TVertex start, TVertex end) { Start = start; End = end; Delta = EndVector.Subtract(StartVector); DeltaInverse = new Vec2D(1.0f / Delta.X, 1.0f / Delta.Y); Box = MakeBox(Start, End); Direction = CalculateDirection(Delta); }
internal string GetNameFromType(Type type, SegmentType component, SegmentDirection direction, Type referenceType) { if (direction.Equals(SegmentDirection.None)) { return GetNameFromType(type, component, direction, !GetNameSpace(component, type).Equals(GetNameSpace(component, referenceType))); } else { return GetNameFromType(type, component, direction, true); } }
internal string GetNameFromType(Type type, SegmentType component, SegmentDirection direction, Type referenceType) { if (direction.Equals(SegmentDirection.None)) { return(GetNameFromType(type, component, direction, !GetNameSpace(component, type).Equals(GetNameSpace(component, referenceType)))); } else { return(GetNameFromType(type, component, direction, true)); } }
internal string GetNameFromType(Type type, SegmentType component, SegmentDirection direction) { return(GetNameFromType(type, component, direction, true)); }
internal string GetStaticAdapterMethodNameName(Type type, SegmentType component, SegmentDirection direction) { switch (direction) { case SegmentDirection.ContractToView: return("ContractToViewAdapter"); case SegmentDirection.ViewToContract: return("ViewToContractAdapter"); default: throw new InvalidOperationException("Must be either incoming our outgoing"); } }
/// <summary> /// Creates MouseMoveSegment and finds out it's Direction property /// </summary> /// <param name="p1">Starting point</param> /// <param name="p2">Final point</param> public MouseMoveSegment(Point p1, Point p2) { direction = GetDirection(p2.X - p1.X, p1.Y - p2.Y); }
private CodeMethodInvokeExpression CallListAdapter(SegmentDirection direction, SegmentType componentType, SegmentType viewType, CodeExpression source, Type genericParamType) { String genericParamViewName = _symbols.GetNameFromType(genericParamType, viewType, SegmentDirection.None, true); CodeMethodReferenceExpression ContractToViewAdapter = GetStaticAdapter(componentType, genericParamType, SegmentDirection.ContractToView); CodeMethodReferenceExpression ViewToContractAdapter = GetStaticAdapter(componentType, genericParamType, SegmentDirection.ViewToContract); CodeMethodInvokeExpression adapterExpr = new CodeMethodInvokeExpression(); adapterExpr.Method = new CodeMethodReferenceExpression(); adapterExpr.Method.TargetObject = new CodeTypeReferenceExpression(typeof(System.AddIn.Pipeline.CollectionAdapters)); adapterExpr.Parameters.Add(source); if (direction == SegmentDirection.ViewToContract) { if (TypeNeedsAdapting(genericParamType)) { adapterExpr.Method.TypeArguments.Add(genericParamViewName); adapterExpr.Method.TypeArguments.Add(genericParamType); adapterExpr.Parameters.Add(ViewToContractAdapter); adapterExpr.Parameters.Add(ContractToViewAdapter); } else { adapterExpr.Method.TypeArguments.Add(genericParamType); } adapterExpr.Method.MethodName = "ToIListContract"; } else { if (TypeNeedsAdapting(genericParamType)) { adapterExpr.Method.TypeArguments.Add(genericParamType); adapterExpr.Method.TypeArguments.Add(genericParamViewName); adapterExpr.Parameters.Add(ContractToViewAdapter); adapterExpr.Parameters.Add(ViewToContractAdapter); } else { adapterExpr.Method.TypeArguments.Add(genericParamType); } adapterExpr.Method.MethodName = "ToIList"; } return adapterExpr; }
internal string GetStaticAdapterMethodNameName(Type type, SegmentType component, SegmentDirection direction) { switch (direction) { case SegmentDirection.ContractToView: return "ContractToViewAdapter"; case SegmentDirection.ViewToContract: return "ViewToContractAdapter"; default: throw new InvalidOperationException("Must be either incoming our outgoing"); } }
private CodeMethodReferenceExpression GetStaticAdapter(SegmentType componentType, Type contractType, SegmentDirection direction) { CodeTypeReferenceExpression adapterType = new CodeTypeReferenceExpression(_symbols.GetNameFromType(contractType, componentType, SegmentDirection.None, true)); String adapterMethodName = _symbols.GetStaticAdapterMethodNameName(contractType, componentType, direction); return new CodeMethodReferenceExpression(adapterType, adapterMethodName); }
internal static CodeTypeReference GetNativeHandleViewType(SegmentDirection direction) { return new CodeTypeReference(typeof(System.Windows.FrameworkElement)); }
private CodeTypeReference GetIListContractTypeRef(SegmentType componentType, SegmentDirection direction, Type contractGenericType,Type referenceType) { try { Type genericParameter = contractGenericType.GetGenericArguments()[0]; CodeTypeReference returnType = new CodeTypeReference(typeof(IList<>)); returnType.TypeArguments.Add(new CodeTypeReference(_symbols.GetNameFromType(genericParameter, componentType, direction, referenceType))); return returnType; } catch (IndexOutOfRangeException) { throw new InvalidOperationException("Tried to get the generic arguments for a type that does not have them: " + contractGenericType.FullName); } }
private CodeMemberMethod CreateStructStaticAdapter(Type contractType, CodeTypeReference source, CodeTypeReference destination, String paramName,SegmentType segment,SegmentDirection direction) { CodeMemberMethod adapter = new CodeMemberMethod(); adapter.Attributes = MemberAttributes.Public | MemberAttributes.Static; adapter.ReturnType = destination; adapter.Parameters.Add( new CodeParameterDeclarationExpression(source, paramName)); CodeObjectCreateExpression constructor = new CodeObjectCreateExpression(); constructor.CreateType = destination; foreach (ParameterInfo pi in contractType.GetConstructors()[0].GetParameters()) { CodePropertyReferenceExpression prop = new CodePropertyReferenceExpression( new CodeVariableReferenceExpression(paramName), ConvertParamNameToProperty(pi.Name)); if (!TypeNeedsAdapting(pi.ParameterType)) { constructor.Parameters.Add(prop); } else { constructor.Parameters.Add( CallStaticAdapter(segment, pi.ParameterType, prop, direction)); } } adapter.Statements.Add( new CodeMethodReturnStatement(constructor)); return adapter; }
private CodeMemberMethod CreateArrayStaticAdapter(Type contractType, CodeTypeReference source, CodeTypeReference destination, String paramName, SegmentType segment, SegmentDirection direction) { CodeMemberMethod adapter = new CodeMemberMethod(); adapter.Attributes = MemberAttributes.Public | MemberAttributes.Static; adapter.ReturnType = destination; adapter.Parameters.Add( new CodeParameterDeclarationExpression(source, paramName)); CodeVariableDeclarationStatement result = new CodeVariableDeclarationStatement(destination, "result"); CodeVariableReferenceExpression input = new CodeVariableReferenceExpression(paramName); CodeConditionStatement nullContractCheck = new CodeConditionStatement(); nullContractCheck.Condition = new CodeBinaryOperatorExpression(input, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)); nullContractCheck.TrueStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null))); adapter.Statements.Add(nullContractCheck); result.InitExpression = new CodeArrayCreateExpression(destination, new CodePropertyReferenceExpression(input, "Length")); //for (int i = 0;i < paramName.Length;i = i + 1) CodeIterationStatement init = new CodeIterationStatement(); init.InitStatement = new CodeVariableDeclarationStatement(typeof(int), "i", new CodePrimitiveExpression(0)); CodeVariableReferenceExpression i = new CodeVariableReferenceExpression("i"); init.IncrementStatement = new CodeAssignStatement(i,new CodeBinaryOperatorExpression(i,CodeBinaryOperatorType.Add,new CodePrimitiveExpression(1))); init.TestExpression = new CodeBinaryOperatorExpression(i, CodeBinaryOperatorType.LessThan, new CodePropertyReferenceExpression(input, "Length")); //result[i] = SourceAdapter.DirectionAdapter(input[i]) CodeAssignStatement valInit = new CodeAssignStatement(); valInit.Left = new CodeArrayIndexerExpression(new CodeVariableReferenceExpression("result"), i); valInit.Right = CallStaticAdapter(segment, contractType.GetElementType(), new CodeArrayIndexerExpression(input, i), direction); init.Statements.Add(valInit); CodeMethodReturnStatement ret = new CodeMethodReturnStatement(new CodeVariableReferenceExpression("result")); adapter.Statements.Add(result); adapter.Statements.Add(init); adapter.Statements.Add(ret); return adapter; }
private CodeMethodInvokeExpression CallStaticAdapter(SegmentType componentType, Type contractType, CodeExpression obj, SegmentDirection direction) { SegmentType viewType = SegmentType.VIEW; if (!ShouldShareViews()) { if (componentType.Equals(SegmentType.HSA)) { viewType = SegmentType.HAV; } else { viewType = SegmentType.AIB; } } if (IsNativeHandle(contractType)) { CodeMethodInvokeExpression adapterInvoke = new CodeMethodInvokeExpression(); if (direction == SegmentDirection.ContractToView) { adapterInvoke.Method = new CodeMethodReferenceExpression(); adapterInvoke.Method.MethodName = "ContractToViewAdapter"; adapterInvoke.Method.TargetObject = new CodeVariableReferenceExpression("System.AddIn.Pipeline.FrameworkElementAdapters"); adapterInvoke.Parameters.Add(obj); } else { adapterInvoke.Method = new CodeMethodReferenceExpression(); adapterInvoke.Method.MethodName = "ViewToContractAdapter"; adapterInvoke.Method.TargetObject = new CodeVariableReferenceExpression("System.AddIn.Pipeline.FrameworkElementAdapters"); adapterInvoke.Parameters.Add(obj); } return adapterInvoke; } else if (IsIListContract(contractType)) { Type genericParam = GetListGenericParamterType(contractType); return CallListAdapter(direction, componentType, viewType, obj, genericParam); } else { CodeMethodInvokeExpression adaptExpr = new CodeMethodInvokeExpression(); CodeTypeReferenceExpression adapterType = new CodeTypeReferenceExpression(_symbols.GetNameFromType(contractType, componentType, SegmentDirection.None, true)); String adapterMethodName = _symbols.GetStaticAdapterMethodNameName(contractType, componentType, direction); CodeMethodReferenceExpression adaptMethod = new CodeMethodReferenceExpression(adapterType, adapterMethodName); adaptExpr.Method = adaptMethod; adaptExpr.Parameters.Add(obj); return adaptExpr; } }
//private CodeTypeReference GetViewTypeReference(SegmentType componentType, Type contractType, Type declaringType) //{ // //If the return value does not need adapting set the return value as the actual type. // //If it needs adapting but is not an IlistContract set the return value as the view type for the specified return value, // //otherwise set the return type as IList<TView> for the IListContract<TContract> // contractType = GetCannonicalContractType(contractType); // if (!TypeNeedsAdapting(contractType)) // { // return new CodeTypeReference(contractType); // } // else // { // if (IsIListContract(contractType)) // { // CodeTypeReference returnType = GetIListContractTypeRef(componentType, SegmentDirection.None, contractType, declaringType); // return returnType; // } // else if (IsNativeHandle(contractType)) // { // return GetNativeHandleViewType(SegmentDirection.ViewToContract); // } // else // { // return new CodeTypeReference(_symbols.GetNameFromType(contractType, componentType, SegmentDirection.None,declaringType)); // } // } //} private CodeTypeReference GetViewTypeReference(SegmentType componentType, Type contractType, Type declaringType,SegmentDirection direction) { //If the return value does not need adapting set the return value as the actual type. //If it needs adapting but is not an IlistContract set the return value as the view type for the specified return value, //otherwise set the return type as IList<TView> for the IListContract<TContract> contractType = GetCannonicalContractType(contractType); if (!TypeNeedsAdapting(contractType)) { return new CodeTypeReference(contractType); } else { if (IsIListContract(contractType)) { CodeTypeReference returnType = GetIListContractTypeRef(componentType, direction, contractType, declaringType); return returnType; } else if (IsNativeHandle(contractType)) { return GetNativeHandleViewType(SegmentDirection.ViewToContract); } else { return new CodeTypeReference(_symbols.GetNameFromType(contractType, componentType, direction,declaringType)); } } }
internal string GetNameFromType(Type type, SegmentType segmentType, SegmentDirection direction, bool prefix) { Type underlyingType; if (type.IsArray) { underlyingType = type.GetElementType(); } else { underlyingType = type; } if (type.Equals(typeof(System.AddIn.Contract.INativeHandleContract))) { if (segmentType.Equals(SegmentType.View) || segmentType.Equals(SegmentType.AddInView) || segmentType.Equals(SegmentType.HostAddInView)) { return(typeof(System.Windows.FrameworkElement).FullName); } else { return(typeof(System.AddIn.Pipeline.FrameworkElementAdapters).FullName); } } if (!type.Assembly.Equals(this._rootAssembly)) { return(type.FullName); } String refPrefix = ""; String refSuffix = ""; if (direction == SegmentDirection.ContractToView) { refSuffix = "ContractToView"; } else if (direction == SegmentDirection.ViewToContract) { refSuffix = "ViewToContract"; } String typeName = NormalizeContractName(type); if (PipelineBuilder.IsViewInterface(type)) { typeName = "I" + typeName; } if (prefix) { if (_sharedView && (segmentType.Equals(SegmentType.HostAddInView) || segmentType.Equals(SegmentType.AddInView) || segmentType.Equals(SegmentType.View))) { refPrefix = GetNameSpace(SegmentType.View, underlyingType) + "."; } else { refPrefix = GetNameSpace(segmentType, underlyingType) + "."; } } if (type.IsArray) { if (segmentType.Equals(SegmentType.AddInSideAdapter) || segmentType.Equals(SegmentType.HostSideAdapter)) { typeName += "Array"; } else { typeName += "[]"; } } switch (segmentType) { case SegmentType.HostAddInView: return(refPrefix + typeName); case SegmentType.HostSideAdapter: return(refPrefix + typeName + refSuffix + "HostAdapter"); case SegmentType.AddInSideAdapter: return(refPrefix + typeName + refSuffix + "AddInAdapter"); case SegmentType.AddInView: return(refPrefix + typeName); case SegmentType.View: return(refPrefix + typeName); default: throw new InvalidOperationException("No segment type specified: " + segmentType); } }
/// <summary> /// Creates MouseMoveSegment and finds out it's Direction property /// </summary> /// <param name="p1">Starting point</param> /// <param name="p2">Final point</param> public MouseMoveSegment(Point p1, Point p2) { direction = GetDirection((int)(p2.X - p1.X), (int)(p1.Y - p2.Y)); }
internal string GetNameFromType(Type type, SegmentType component,SegmentDirection direction) { return GetNameFromType(type,component,direction,true); }
internal string GetNameFromType(Type type, SegmentType segmentType,SegmentDirection direction, bool prefix) { Type underlyingType; if (type.IsArray) { underlyingType = type.GetElementType(); } else { underlyingType = type; } if (type.Equals(typeof(System.AddIn.Contract.INativeHandleContract))) { if (segmentType.Equals(SegmentType.VIEW) || segmentType.Equals(SegmentType.AIB) || segmentType.Equals(SegmentType.HAV)) { return typeof(System.Windows.FrameworkElement).FullName; } else { return typeof(System.AddIn.Pipeline.FrameworkElementAdapters).FullName; } } if (!type.Assembly.Equals(this._rootAssembly)) { return type.FullName; } String refPrefix = ""; String refSuffix = ""; if (direction == SegmentDirection.ContractToView) { refSuffix = "ContractToView"; } else if (direction == SegmentDirection.ViewToContract) { refSuffix = "ViewToContract"; } String typeName = NormalizeContractName(type); if (PipelineBuilder.IsViewInterface(type)) { typeName = "I" + typeName; } if (prefix) { if (_sharedView && (segmentType.Equals(SegmentType.HAV) || segmentType.Equals(SegmentType.AIB) || segmentType.Equals(SegmentType.VIEW))) { refPrefix = GetNameSpace(SegmentType.VIEW,underlyingType) + "."; } else { refPrefix = GetNameSpace(segmentType, underlyingType) + "."; } } if (type.IsArray) { if (segmentType.Equals(SegmentType.ASA) || segmentType.Equals(SegmentType.HSA)) { typeName += "Array"; } else { typeName += "[]"; } } switch (segmentType) { case SegmentType.HAV: return refPrefix + typeName; case SegmentType.HSA: return refPrefix + typeName + refSuffix + "HostAdapter"; case SegmentType.ASA: return refPrefix + typeName + refSuffix + "AddInAdapter"; case SegmentType.AIB: return refPrefix + typeName; case SegmentType.VIEW: return refPrefix + typeName; default: throw new InvalidOperationException("No segment type specified: " + segmentType); } }
//Since multiple methods on the contract correspond to one property type on the view (in the case of a property with a getter and a setter we need to keep //track of which properties have been created before to decide whether we need to create a new one or add either a gettter or setter to an existing one. //This method looks in a global hashtable for the property it needs and if it doesn't find one it creates one and inserts it into the table. internal CodeMemberProperty GetProperyDecl(Type contractType, CodeTypeDeclaration type, MethodInfo mi, Dictionary<String, CodeMemberProperty> props, SegmentType componentType, SegmentDirection direction, bool prefix) { CodeMemberProperty prop; if (!props.TryGetValue(GetViewNameFromMethod(mi), out prop)) { PropertyMethodInfo attr = GetPropertyAttribute(mi); prop = new CodeMemberProperty(); prop.Name = GetViewNameFromMethod(mi); if (direction == SegmentDirection.None) { prop.Attributes = MemberAttributes.Abstract | MemberAttributes.Public; } else if (direction == SegmentDirection.ContractToView) { if (!IsViewInterface(contractType)) { prop.Attributes = MemberAttributes.Public | MemberAttributes.Override; } else { prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; } } else { prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; } type.Members.Add(prop); props.Add(GetViewNameFromMethod(mi), prop); Type propertyType = GetPropertyType(mi); if (direction.Equals(SegmentDirection.ViewToContract)) { prop.Type = new CodeTypeReference(propertyType); } else { if (IsIListContract(propertyType)) { prop.Type = GetIListContractTypeRef(componentType, direction, propertyType,mi.DeclaringType); } else if (IsNativeHandle(propertyType)) { prop.Type = GetNativeHandleViewType(SegmentDirection.ViewToContract); } else { prop.Type = new CodeTypeReference(_symbols.GetNameFromType(propertyType, componentType, direction, contractType)); } } } return prop; }