using AfwDynamicGraphics; using AfwDynamicGraphics.Media; using AfwExpressionHandling; using System; using System.Globalization; using System.Windows; using System.Windows.Media; namespace G778Special { [PrimitiveItemAttribute("{9C0B1FED-6857-49f9-BCEA-0BD39AE5B865}", "G778:ScrollEngine", "ScrollEngine", "G778 Special", "Hintergrund für Scrollketten")] public class ScrollEngine : FrameItem, ITimerUpdateable { private static readonly PropertyDesc[] textProperties = new PropertyDesc[4] { new PropertyDesc("OutputYPos", PropertyRefType.Singleton, 17, "Ausgangsvariable mit der aktuellen Position", "Interface"), new PropertyDesc("InputYPos", PropertyRefType.Singleton, 18, "Eingangsvariable mit der Endposition", "Interface"), new PropertyDesc("MaxYValue", IntegerType.Singleton, 19, "Maximale Position", "Behavior"), new PropertyDesc("MinYValue", IntegerType.Singleton, 20, "Minimale Position", "Behavior") }; private static readonly StateVarDesc[] stateVarDescs = new StateVarDesc[0]; private static readonly PropertyDesc[] propertyDescriptions = new PropertyDesc[ScrollEngine.textProperties.Length + FrameItem.FiGetNumberOfProps(true, true, true, true, false)]; private IElementView elementView; private DrawingVisual textVisual; private static readonly StateVarDesc[] stateVarDescriptions; private PropertyRefValue OutPosYRef = null; private PropertyRefValue InPosYRef = null; private long InPosY; private long OutPosY; private long MaxYValue; private long MinYValue; private bool MouseIsDown; private double MouseX; private double MouseY; static ScrollEngine() { int n = 0; FrameItem.FiFillInPropertyDescriptions(ScrollEngine.propertyDescriptions, ref n, true, true, true, true, false); foreach (PropertyDesc propertyDesc in ScrollEngine.textProperties) { ScrollEngine.propertyDescriptions[n++] = propertyDesc; } ScrollEngine.stateVarDescriptions = StateVarDesc.NewEmptyVector; } public ScrollEngine() : base(true, true, true) { InPosY = -1; OutPosY = 0; MaxYValue = 100000; MinYValue = 0; linePen = new SimplePen(Colors.Black, 1.0); frame.Width = 3.0; } public ScrollEngine(ScrollEngine other, GraphicItemVisual otherVisual, IElementView elementView) : base((FrameItem)other, otherVisual) { OutPosYRef = PropertyRefValue.GetLeftSideValue(other.OutPosYRef, elementView); InPosYRef = PropertyRefValue.GetLeftSideValue(other.InPosYRef, elementView); MaxYValue = other.MaxYValue; MinYValue = other.MinYValue; InPosY = other.InPosY; OutPosY = other.OutPosY; linePen = other.linePen; } protected override StateVarDesc[] OnGetStateVarDescriptions() { return ScrollEngine.stateVarDescriptions; } protected override PropertyDesc[] GetPropertyDescriptions() { return ScrollEngine.propertyDescriptions; } public override GraphicItem GetRunTimeInstance(IElementView elementView, GraphicItemVisual visual) { return new ScrollEngine(this, visual, elementView); } protected override Size GetDefaultSize() { return new Size(150.0, 30.0); } protected override void TransferValue(IDataAccess accessor, int accessIndex, int propertyIndex, bool writeOperation) { switch (propertyIndex) { case 17: accessor.TransferPropertyReferenceValue(writeOperation, accessIndex, ref OutPosYRef); break; case 18: accessor.TransferPropertyReferenceValue(writeOperation, accessIndex, ref InPosYRef); break; case 19: accessor.TransferInteger(writeOperation, accessIndex, ref MaxYValue); break; case 20: accessor.TransferInteger(writeOperation, accessIndex, ref MinYValue); break; default: base.TransferValue(accessor, accessIndex, propertyIndex, writeOperation); break; } } protected override void InitVisual(IElementView elementView, ulong noValueEffects) { this.elementView = elementView; this.textVisual = new GraphicItemVisual(this); this.ItemDrawingVisual.Children.Add(textVisual); DrawItem(); } protected override void UpdateVisual(IElementView elementView, ulong updateReason, ulong noValueEffects) { DrawItem(); } private void DrawItem() { using (DrawingContext drawingContext = this.textVisual.RenderOpen()) { System.Windows.Media.Pen pen = new System.Windows.Media.Pen(Brushes.Black, 1); drawingContext.DrawRectangle(Brushes.LightGray, pen, ClientArea); } } public override bool IsMouseEventConsumer(IElementView elementView) { return true; } public override void MouseWheel(MouseEventContext mouseEventContext, IElementView elementView) { if (mouseEventContext.Delta != 0) { InPosY = InPosY + (mouseEventContext.Delta / -2); writeInputValue(); } base.MouseWheel(mouseEventContext, elementView); } public override void MouseDown(MouseEventContext mouseEventContext, IElementView elementView) { base.MouseDown(mouseEventContext, elementView); MouseIsDown = true; MouseX = mouseEventContext.X; MouseY = mouseEventContext.Y; } public override void MouseMove(MouseEventContext mouseEventContext, IElementView elementView) { base.MouseMove(mouseEventContext, elementView); if (MouseIsDown) { if (Math.Abs(MouseY - mouseEventContext.Y) > 100) { InPosY = InPosY + (long)(MouseY - mouseEventContext.Y)*3; } else { InPosY = InPosY + (long)(MouseY - mouseEventContext.Y); } MouseX = mouseEventContext.X; MouseY = mouseEventContext.Y; writeInputValue(); } } public override void MouseUp(MouseEventContext mouseEventContext, IElementView elementView) { base.MouseUp(mouseEventContext, elementView); MouseIsDown = false; } public override void Leave(MouseEventContext mouseEventContext, IElementView elementView) { base.Leave(mouseEventContext, elementView); MouseIsDown = false; } private void writeInputValue() { if (elementView != null && InPosYRef != null) { elementView.WriteProperty(new RealVariantValue(InPosY), InPosYRef); StateVariablesChanged(elementView); } } private void writeOutputValue() { if (elementView != null && OutPosYRef != null) { elementView.WriteProperty(new RealVariantValue(OutPosY), OutPosYRef); StateVariablesChanged(elementView); } } public void OnTimerUpdate(IElementView elementView) { if (elementView != null && InPosYRef != null) { bool a; VariantValue v = elementView.GetReferencedPropertysValue(InPosYRef, out a); InPosY = (long)v.AsInteger; if (InPosY > MaxYValue) { InPosY = MaxYValue; writeInputValue(); } if (InPosY < MinYValue) { InPosY = MinYValue; writeInputValue(); } if (OutPosY == -1) { OutPosY = InPosY; writeOutputValue(); } if (InPosY != OutPosY) { if (Math.Abs(InPosY - OutPosY) < 3) OutPosY = InPosY; else OutPosY = OutPosY + ((InPosY - OutPosY) / 5); writeOutputValue(); } } } } }