using AfwDynamicGraphics; using AfwDynamicGraphics.Media; using AfwExpressionHandling; using AfwExpressionHandling.AfwExpressionDefinitions; using System; using System.Globalization; using System.Text; using System.Windows; using System.Windows.Media; using System.Collections.Generic; namespace G778Special { [PrimitiveItemAttribute("{24296A87-6981-4bff-AF67-0B566D41B732}", "G778:Schritt", "Schritt", "G778 Special", "Schritt Mittelteil")] public class Schritt : FrameItem { private static readonly PropertyDesc[] textProperties = new PropertyDesc[] { new PropertyDesc("Text", StringType.Singleton, 17, "Beschriftungstext", "Appearance"), new PropertyDesc("TargetYPos", PropertyRefType.Singleton, 18, "In diese Variable wird die Y-Position geschrieben.", "Interface"), new PropertyDesc("CurrentStep", IntegerType.Singleton, 19, "Aktuelle Schrittnummer", "Interface"), new PropertyDesc("ThisSteps", IntegerArrayType.Singleton, 20, "Nummern dieses Schrittes", "Appearance"), new PropertyDesc("ShowSteps", BooleanType.Singleton, 21, "Zeige die Nummern dieses Schritts", "Appearance") }; private static readonly StateVarDesc[] stateVarDescs = new StateVarDesc[0]; private static readonly PropertyDesc[] propertyDescriptions = new PropertyDesc[Schritt.textProperties.Length + RectangularItem.RiGetNumberOfProps(true, false)]; private Brush GreyBrush = new SolidColorBrush(Color.FromRgb(0x9c, 0xa9, 0xc2)); private Brush GreenBrush = new SolidColorBrush(Color.FromRgb(0x00, 0xff, 0x00)); private IElementView elementView; private LogicalFont font; private DrawingVisual textVisual; private static readonly StateVarDesc[] stateVarDescriptions; private PropertyRefValue TargetYPos = null; private string text; private long CurrentStep = 10; private long[] ThisStep = new long[] {0}; private bool currState = false; private bool showSteps = true; static Schritt() { int n = 0; RectangularItem.RiFillInPropertyDescriptions(Schritt.propertyDescriptions, ref n, true, false); foreach (PropertyDesc propertyDesc in Schritt.textProperties) Schritt.propertyDescriptions[n++] = propertyDesc; Schritt.stateVarDescriptions = StateVarDesc.NewEmptyVector; } public Schritt() : base(false, false, false) { this.text = "Schritt"; this.font = new LogicalFont("Arial", 20.0, AfwDynamicGraphics.Media.FontStyle.Regular); } public Schritt(Schritt other, GraphicItemVisual otherVisual, IElementView elementView) : base((FrameItem)other, otherVisual) { this.text = other.text; this.font = other.font; this.TargetYPos = PropertyRefValue.GetLeftSideValue(other.TargetYPos,elementView); this.CurrentStep = other.CurrentStep; this.ThisStep = other.ThisStep; this.showSteps = other.showSteps; } protected override StateVarDesc[] OnGetStateVarDescriptions() { return Schritt.stateVarDescriptions; } protected override PropertyDesc[] GetPropertyDescriptions() { return Schritt.propertyDescriptions; } public override GraphicItem GetRunTimeInstance(IElementView elementView, GraphicItemVisual visual) { return new Schritt(this, visual, elementView); } protected override Size GetDefaultSize() { return new Size(150.0, 80.0); } protected override void TransferValue(IDataAccess accessor, int accessIndex, int propertyIndex, bool writeOperation) { switch (propertyIndex) { case 17: accessor.TransferString(writeOperation, accessIndex, ref text); break; case 18: accessor.TransferPropertyReferenceValue(writeOperation, accessIndex, ref TargetYPos); checkStep(); break; case 19: accessor.TransferInteger(writeOperation, accessIndex, ref CurrentStep); checkStep(); break; case 20: object o = ThisStep; accessor.TransferObject(writeOperation, accessIndex, ref o); ThisStep = (long[])o; checkStep(); break; case 21: accessor.TransferBoolean(writeOperation, accessIndex, ref showSteps); if (elementView != null) { DrawItemText(); } break; default: base.TransferValue(accessor, accessIndex, propertyIndex, writeOperation); break; } } private void checkStep() { if (elementView != null && !TargetYPos.IsNull) { bool newState = false; foreach (long a in ThisStep) { if (a == CurrentStep) newState = true; } if (newState) { elementView.WriteProperty(new RealVariantValue(Position.Y), TargetYPos); } if (newState != currState) { currState = newState; DrawItemText(); } } } protected override void InitVisual(IElementView elementView, ulong noValueEffects) { this.elementView = elementView; this.textVisual = new GraphicItemVisual(this); this.ItemDrawingVisual.Children.Add(textVisual); this.DrawItemText(); checkStep(); } protected override void UpdateVisual(IElementView elementView, ulong updateReason, ulong noValueEffects) { DrawItemText(); } private void DrawItemText() { using (DrawingContext drawingContext = this.textVisual.RenderOpen()) { WPFFont font = this.font.GetFont(elementView); System.Windows.Media.Pen pen = new System.Windows.Media.Pen(Brushes.Black, 1); if (currState) { drawingContext.DrawRectangle(GreenBrush, pen, new Rect(0, 0, Size.Width, Size.Height)); } else { drawingContext.DrawRectangle(GreyBrush, pen, new Rect(0, 0, Size.Width, Size.Height)); } if (showSteps) { string s = null; foreach (long a in ThisStep) { if (s == null) { s = "" + a; } else { s += "," + a; } } FormattedText fText = new FormattedText(s, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, font.Typeface, 10, Brushes.Red); drawingContext.DrawText(fText, new Point(Size.Width - fText.Width - 1, Size.Height - 11)); } string[] texte = text.Split('\n'); for (int i = 0; i < texte.Length; i++) { FormattedText fText = new FormattedText(texte[i], CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, font.Typeface, font.Size, Brushes.Black); double left = (Size.Width / 2) - (fText.Width / 2); double top = (Size.Height / 2) - ((texte.Length - 1) * fText.Height * 0.6) + (i * fText.Height * 1.2) - (fText.Height / 2); drawingContext.DrawText(fText, new Point(left, top)); } } } } }