Example #1
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     else if (value is string)
     {
         return(DDSConverter.Convert((string)value));
     }
     else
     {
         throw new NotSupportedException(string.Format("{0} cannot convert from {1}.", this.GetType().FullName, value.GetType().FullName));
     }
 }
Example #2
0
        void AddControl(UIControl control)
        {
            if (control.ImageIndex >= images.Count)
            {
                return;
            }

            var imagebit = images[control.ImageIndex];
            var texture  = textures[(int)imagebit.TextureIndex];
            var text     = (control.TextIndex < texts.Count) ? texts[control.TextIndex] : null;

            control.Content = text;
            var image = new Image();

            image.Stretch = Stretch.Fill;
            image.SetBinding(Image.WidthProperty, CreateTwoWayBinding("Width", control));
            image.SetBinding(Image.HeightProperty, CreateTwoWayBinding("Height", control));

            var matrix = image.RenderTransform.Value;

            switch (control.Dock)
            {
            // TBD

            /*case DockingMode.CenterCenter:
             *      matrix.OffsetX = (canvas.ActualWidth - image.Width) / 2;
             *      matrix.OffsetY = (canvas.ActualHeight - image.Height) / 2;
             *      break;*/

            default:
                matrix.OffsetX = control.Rectangle.Left;
                matrix.OffsetY = control.Rectangle.Top;
                break;
            }

            image.SetBinding(Image.RenderTransformProperty, CreateTwoWayBinding("Rectangle", control, new RectConverter()));

            if (control.ImageState != State.Hidden)
            {
                /*var bitmap = new BitmapImage();
                 * bitmap.BeginInit();
                 * bitmap.UriSource = new Uri(dir + '\\' + texture.Name);
                 * bitmap.DecodePixelWidth = (int)texture.Width;
                 * bitmap.DecodePixelHeight = (int)texture.Height;
                 * bitmap.EndInit();*/

                var bitmap = (BitmapSource)DDSConverter.Convert(dir + '\\' + texture.Name);

                var rect = new Int32Rect(imagebit.TopLeft.X, imagebit.TopLeft.Y, imagebit.Width, imagebit.Height);

                if (rect.X + rect.Width > texture.Width)
                {
                    rect.Width = (int)texture.Width - rect.X;
                }
                if (rect.Y + rect.Height > texture.Height)
                {
                    rect.Height = (int)texture.Height - rect.Y;
                }

                image.Source = new CroppedBitmap(bitmap, rect);

                bitmap = null;

                #region Memory Leak!

                /*var write = new WriteableBitmap(bitmap);
                 *
                 * write.Lock();
                 *
                 * if (rect.Height != 0 && rect.Width != 0)
                 * {
                 *      if (rect.X + rect.Width > write.PixelWidth)
                 *      {
                 *              var pixels = new byte[write.PixelHeight * write.PixelWidth * write.Format.BitsPerPixel];
                 *
                 *              write.CopyPixels(pixels, write.BackBufferStride, 0);
                 *              write = new WriteableBitmap(write.PixelWidth * 2, write.PixelHeight, write.DpiX, write.DpiY, write.Format, write.Palette);
                 *              write.WritePixels(new Int32Rect(0, 0, write.PixelWidth, write.PixelHeight), pixels, write.BackBufferStride, 0);
                 *              write.WritePixels(new Int32Rect(write.PixelWidth, 0, write.PixelWidth, write.PixelHeight), pixels, write.BackBufferStride, 0);
                 *
                 *              pixels = null;
                 *      }
                 *
                 *      if (rect.Y + rect.Height > write.PixelHeight)
                 *      {
                 *              var pixels = new byte[write.PixelHeight * write.PixelWidth * write.Format.BitsPerPixel];
                 *
                 *              var blah = new BitmapImage();
                 *
                 *              write.CopyPixels(pixels, write.BackBufferStride, 0);
                 *              write = new WriteableBitmap(write.PixelWidth, write.PixelHeight * 2, write.DpiX, write.DpiY, write.Format, write.Palette);
                 *              write.WritePixels(new Int32Rect(0, 0, write.PixelWidth, write.PixelHeight / 2), pixels, write.BackBufferStride, 0);
                 *              write.WritePixels(new Int32Rect(0, write.PixelHeight / 2, write.PixelWidth, write.PixelHeight / 2), pixels, write.BackBufferStride, 0);
                 *
                 *              pixels = null;
                 *      }
                 *
                 *      image.Source = new CroppedBitmap(write, rect);
                 * }
                 *
                 * write.Unlock();
                 *
                 * bitmap = null;
                 * write = null;
                 *
                 * GC.Collect();
                 * GC.WaitForPendingFinalizers();*/

                #endregion
            }

            var behaviours = Interaction.GetBehaviors(image);

            var resize = new ResizeBehavior();
            resize.MaxWidth              = 1024;
            resize.MaxHeight             = 768;
            resize.MinWidth              = 2;
            resize.MinHeight             = 2;
            resize.StayInParent          = true;
            resize.DragSpace             = 4;
            resize.DragIndicatorsOpacity = 0.75;
            resize.DragIndicatorsFill    = Brushes.White;
            behaviours.Add(resize);

            var drag = new MouseDragElementBehavior();
            drag.ConstrainToParentBounds = true;
            behaviours.Add(drag);

            image.MouseDown += control_MouseDown;

            image.Tag = control;

            canvas.Children.Add(image);

            if (text == null || text.DTSIndex == -1 || control.TextLines == 0)
            {
                return;
            }

            var stack = new StackPanel();
            stack.SetBinding(StackPanel.RenderTransformProperty, CreateTwoWayBinding("Location", text, new PointConverter()));

            var lineWidthBinding  = CreateTwoWayBinding("Width", text);
            var lineHeightBinding = CreateTwoWayBinding("LineHeight", text);
            var hAlignBinding     = CreateTwoWayBinding("HAlign", text);
            var vAlignBinding     = CreateTwoWayBinding("VAlign", text);

            var brush = new SolidColorBrush();
            BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, CreateTwoWayBinding("NormalColour", text, new ColourConverter()));

            for (int i = 0; i < control.TextLines; i++)
            {
                var label = new Label();

                var contentBinding = new Binding();
                contentBinding.Source = dts[text.DTSIndex + i];
                contentBinding.Mode   = BindingMode.OneWay;
                contentBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

                label.SetBinding(Label.ContentProperty, contentBinding);

                label.Foreground = brush;

                label.SetBinding(Label.WidthProperty, lineWidthBinding);
                label.SetBinding(Label.HeightProperty, lineHeightBinding);

                label.SetBinding(Label.HorizontalContentAlignmentProperty, hAlignBinding);
                label.SetBinding(Label.VerticalContentAlignmentProperty, vAlignBinding);

                label.IsHitTestVisible = false;
                label.FontFamily       = new FontFamily("Tahoma");
                label.FontSize         = 11.0d;
                label.Padding          = new Thickness(0);

                label.Tag = image;

                stack.Children.Add(label);
            }

            canvas.Children.Add(stack);
        }