public static void EnableEffectDialogTheme(EffectConfigDialog dialog)
        {
            try
            {
                Version pdnVersion = dialog.Services.GetService <PaintDotNet.AppModel.IAppInfoService>().AppVersion;

                if (pdnVersion >= PluginThemingMinVersion)
                {
                    if (!initAppThemeSetter)
                    {
                        initAppThemeSetter = true;

                        PropertyInfo propertyInfo = typeof(EffectConfigDialog).GetProperty("UseAppThemeColors");
                        if (propertyInfo != null)
                        {
                            useAppThemeSetter = propertyInfo.GetSetMethod();
                        }
                    }


                    if (useAppThemeSetter != null)
                    {
                        useAppThemeSetter.Invoke(dialog, new object[] { true });
                    }
                }
            }
            catch
            {
                // Ignore any exceptions that are thrown when trying to enable the dialog theming.
                // The dialog should be shown to the user even if theming could not be enabled.
            }
        }
Beispiel #2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int                type       = this.cbEffect.SelectedIndex;
            Effect             effect     = null;
            EffectConfigDialog dialog     = null;
            PropertyCollection collection = null;

            effect = (Effect)available[type].GetConstructor(Type.EmptyTypes).Invoke(new object[0]);

            if ((effect.Options.Flags & EffectFlags.Configurable) != 0)
            {
                dialog        = effect.CreateConfigDialog();
                dialog.Effect = effect;

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    if (dialog.EffectToken is PropertyBasedEffectConfigToken)
                    {
                        collection = ((PropertyBasedEffectConfigToken)dialog.EffectToken).Properties;
                    }
                }
                else
                {
                    return;
                }
            }

            this.lbEffect.Items.Add(this.cbEffect.Text);
            this.types.Add(type);
            this.effects.Add(effect);
            this.dialogs.Add(dialog);
            this.collections.Add(collection);
            FinishTokenUpdate();
        }
        public ConfigToken ToToken(Dictionary <string, Type> effects, IServiceProvider services, Surface effectSourceSurface)
        {
            ConfigToken token = new ConfigToken();

            for (int i = 0; i < Effects.Count; i++)
            {
                EffectConfigToken stepToken = null;
                Type type;
                if (effects.TryGetValue(Effects[i], out type))
                {
                    Effect effect = (Effect)(type.GetConstructor(Type.EmptyTypes).Invoke(new object[0]));
                    effect.Services = services;
                    effect.EnvironmentParameters = new EffectEnvironmentParameters(allcolors[i].First, allcolors[i].Second, 2, new PdnRegion(effectSourceSurface.Bounds), effectSourceSurface);
                    if (effect.Options.Flags.HasFlag(EffectFlags.Configurable))
                    {
                        try
                        {
                            EffectConfigDialog dialog = effect.CreateConfigDialog();
                            stepToken = dialog.EffectToken;
                            if (effect is PropertyBasedEffect)
                            {
                                PropertyBasedEffectConfigToken pbect      = stepToken as PropertyBasedEffectConfigToken;
                                IEnumerator <Property>         enumerator = pbect.Properties.Properties.GetEnumerator();
                                for (int ii = 0; ii < Properties[i].Length; ii++)
                                {
                                    try
                                    {
                                        enumerator.MoveNext();
                                        enumerator.Current.ReadOnly = false;
                                        if (enumerator.Current.Value is FontFamily)
                                        {
                                            enumerator.Current.Value = new FontFamily((string)Properties[i][ii]);
                                        }
                                        else
                                        {
                                            enumerator.Current.Value = Properties[i][ii];
                                        }
                                    }
                                    catch (ReadOnlyException) { }
                                }
                            }
                            else
                            {
                                Type t = stepToken.GetType();
                                SetObjectPropertiesAndFields(stepToken, Properties[i], Fields[i], effects, services, effectSourceSurface);
                            }
                        }
                        catch (Exception) { }
                    }
                    token.effects.Add(new ScriptStep(effect.Name, effect.Image, type, stepToken, Colors[i].First, Colors[i].Second));
                }
                else
                {
                    token.effects.Add(new ScriptStep(Effects[i], null, null, null, Colors[i].First, Colors[i].Second));
                }
            }

            return(token);
        }
Beispiel #4
0
        private void EffectConfigTokenChangedHandler(object sender, System.EventArgs e)
        {
            EffectConfigDialog       ecf = (EffectConfigDialog)sender;
            BackgroundEffectRenderer ber = (BackgroundEffectRenderer)ecf.Tag;

            if (ber != null)
            {
                AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBarAsync();
                ber.Start();
            }
        }
Beispiel #5
0
        private void ChangeSelectedEffect()
        {
            if (lbScript.SelectedItem != null)
            {
                ConfigToken token = (ConfigToken)theEffectToken;
                int         i     = lbScript.SelectedIndex;
                ScriptStep  step  = token.effects[i];
                Type        type  = step.EffectType;
                if (type != null)
                {
                    Effect effect = (Effect)type.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                    effect.EnvironmentParameters = Effect.EnvironmentParameters;
                    effect.Services = Services;
                    if (effect.CheckForEffectFlags(EffectFlags.Configurable))
                    {
                        try
                        {
                            EffectConfigDialog dialog = effect.CreateConfigDialog();
                            dialog.EffectSourceSurface = EffectSourceSurface;
                            dialog.Selection           = Selection;
                            dialog.Effect      = effect;
                            dialog.EffectToken = (EffectConfigToken)step.Token.Clone();
                            if (effect.Image != null)
                            {
                                dialog.Icon = Icon.FromHandle(((Bitmap)effect.Image).GetHicon());
                            }

                            if (dialog.ShowDialog(this) == DialogResult.OK)
                            {
                                step.Token = dialog.EffectToken;
                                FinishTokenUpdate();
                            }
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(this,
                                            "Error occurred in configuration dialog for " + effect.Name + ":\n\n" + e.ToString(),
                                            effect.Name + " Error",
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    MessageBox.Show(this, step.Name + " is not installed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Beispiel #6
0
        void AddSelectedEffect()
        {
            if (lbAvailable.SelectedItem != null)
            {
                ConfigToken token = (ConfigToken)theEffectToken;
                string      effectidentifiername = SearchResultEffects[lbAvailable.SelectedIndex];
                Type        t      = AvailableEffects[effectidentifiername];
                Effect      effect = (Effect)t.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                effect.EnvironmentParameters = Effect.EnvironmentParameters;
                effect.Services = Services;
                if (effect.CheckForEffectFlags(EffectFlags.Configurable))
                {
                    try
                    {
                        EffectConfigDialog dialog = effect.CreateConfigDialog();
                        dialog.EffectSourceSurface = EffectSourceSurface;
                        dialog.Selection           = Selection;
                        dialog.Effect = effect;
                        if (effect.Image != null)
                        {
                            dialog.Icon = effect.Image.ToIcon();
                        }

                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            token.effects.Add(new ScriptStep(effect.Name, effect.Image, t, dialog.EffectToken, Effect.EnvironmentParameters.PrimaryColor, Effect.EnvironmentParameters.SecondaryColor));
                            lbScript.Items.Add(effect.Name);
                            FinishTokenUpdate();
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(this,
                                        "Error occurred in configuration dialog for " + effect.Name + ":\n\n" + e.ToString(),
                                        effect.Name + " Error",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    token.effects.Add(new ScriptStep(effect.Name, effect.Image, t, null, Effect.EnvironmentParameters.PrimaryColor, Effect.EnvironmentParameters.SecondaryColor));
                    lbScript.Items.Add(effect.Name);
                    FinishTokenUpdate();
                }
            }
        }
        public static void EnableEffectDialogTheme(EffectConfigDialog dialog)
        {
            try
            {
                if (!initAppThemeSetter)
                {
                    initAppThemeSetter = true;

                    PropertyInfo propertyInfo = typeof(EffectConfigDialog).GetProperty("UseAppThemeColors", BindingFlags.Public | BindingFlags.Instance);
                    if (propertyInfo != null)
                    {
                        useAppThemeSetter = (Action <EffectConfigDialog, bool>)Delegate.CreateDelegate(typeof(Action <EffectConfigDialog, bool>), propertyInfo.GetSetMethod());
                    }
                }

                useAppThemeSetter?.Invoke(dialog, true);
            }
            catch
            {
                // Ignore any exceptions that are thrown when trying to enable the dialog theming.
                // The dialog should be shown to the user even if theming could not be enabled.
            }
        }
Beispiel #8
0
        private bool SaveTxtFile(string path, out Exception exception)
        {
            exception = null;
            ScriptLabScript sls = ScriptLabScript.FromToken((ConfigToken)theEffectToken);
            FileStream      fs  = null;

            try
            {
                fs = new FileStream(path, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                for (int i = 0; i < sls.Effects.Count; i++)
                {
                    sw.Write(sls.Names[i]);
                    sw.Write("\t(");
                    sw.Write(sls.Effects[i]);
                    sw.WriteLine(")");
                    sw.Write("\tPrimary:\t#");
                    sw.WriteLine(sls.Colors[i].First.ToHexString());
                    sw.Write("\tSecondary:\t#");
                    sw.WriteLine(sls.Colors[i].Second.ToHexString());
                    sw.WriteLine();

                    string effectname = sls.Effects[i];
                    Type   effecttype;
                    if (AvailableEffects.TryGetValue(effectname, out effecttype))
                    {
                        Effect effect = (Effect)effecttype.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                        effect.EnvironmentParameters = Effect.EnvironmentParameters;
                        effect.Services = Services;
                        if (effect.CheckForEffectFlags(EffectFlags.Configurable))
                        {
                            EffectConfigDialog dialog = effect.CreateConfigDialog();
                            EffectConfigToken  token  = dialog.EffectToken;
                            if (effect is PropertyBasedEffect)
                            {
                                PropertyBasedEffectConfigToken pbect      = token as PropertyBasedEffectConfigToken;
                                IEnumerator <Property>         enumerator = pbect.Properties.Properties.GetEnumerator();
                                for (int ii = 0; ii < pbect.Properties.Count; ii++)
                                {
                                    enumerator.MoveNext();
                                    sw.Write("\t");
                                    sw.Write(enumerator.Current.Name);
                                    sw.Write(":\t");
                                    sw.WriteLine(sls.Properties[i][ii]);
                                }
                            }
                            else
                            {
                                Type           tokentype = token.GetType();
                                PropertyInfo[] pi        = tokentype.GetProperties();
                                FieldInfo[]    fi        = tokentype.GetFields();
                                for (int ii = 0; ii < pi.Length; ii++)
                                {
                                    sw.Write("\t");
                                    sw.Write(pi[ii].Name);
                                    sw.Write(":\t");
                                    sw.WriteLine(sls.Properties[i][ii]);
                                }
                                for (int ii = 0; ii < fi.Length; ii++)
                                {
                                    sw.Write("\t");
                                    sw.Write(fi[ii].Name);
                                    sw.Write(":\t");
                                    sw.WriteLine(sls.Fields[i][ii]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int ii = 0; ii < sls.Properties.Count; ii++)
                        {
                            sw.Write("\tUnknown Property:\t");
                            sw.WriteLine(sls.Properties[i][ii]);
                        }
                        for (int ii = 0; ii < sls.Fields.Count; ii++)
                        {
                            sw.Write("\tUnknown Field:\t");
                            sw.WriteLine(sls.Fields[i][ii]);
                        }
                    }
                    sw.WriteLine();
                }
                sw.Flush();

                return(true);
            }
            catch (Exception e)
            {
                exception = e;
                return(false);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }
        }
Beispiel #9
0
        public void RunEffect(Type effectType)
        {
            bool oldDirtyValue   = AppWorkspace.ActiveDocumentWorkspace.Document.Dirty;
            bool resetDirtyValue = false;

            AppWorkspace.Update(); // make sure the window is done 'closing'
            AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
            DocumentWorkspace activeDW = AppWorkspace.ActiveDocumentWorkspace;

            PdnRegion selectedRegion;

            if (activeDW.Selection.IsEmpty)
            {
                selectedRegion = new PdnRegion(activeDW.Document.Bounds);
            }
            else
            {
                selectedRegion = activeDW.Selection.CreateRegion();
            }

            BitmapLayer layer = (BitmapLayer)activeDW.ActiveLayer;

            using (new PushNullToolMode(activeDW))
            {
                try
                {
                    Effect effect = (Effect)Activator.CreateInstance(effectType);

                    EffectEnvironmentParameters eep = new EffectEnvironmentParameters(
                        AppWorkspace.AppEnvironment.PrimaryColor,
                        AppWorkspace.AppEnvironment.SecondaryColor,
                        AppWorkspace.AppEnvironment.PenInfo.Width,
                        selectedRegion);

                    string            name         = effect.Name;
                    EffectConfigToken newLastToken = null;
                    effect.EnvironmentParameters = eep;

                    if (!(effect.IsConfigurable))
                    {
                        Surface copy = activeDW.BorrowScratchSurface(this.GetType() + ".RunEffect() using scratch surface for non-configurable rendering");

                        try
                        {
                            using (new WaitCursorChanger(AppWorkspace))
                            {
                                copy.CopySurface(layer.Surface);
                            }

                            DoEffect(effect, null, selectedRegion, selectedRegion, copy);
                        }

                        finally
                        {
                            activeDW.ReturnScratchSurface(copy);
                        }
                    }
                    else
                    {
                        PdnRegion previewRegion = (PdnRegion)selectedRegion.Clone();
                        previewRegion.Intersect(RectangleF.Inflate(activeDW.VisibleDocumentRectangleF, 1, 1));

                        Surface originalSurface = activeDW.BorrowScratchSurface(this.GetType() + ".RunEffect() using scratch surface for rendering during configuration");

                        try
                        {
                            using (new WaitCursorChanger(AppWorkspace))
                            {
                                originalSurface.CopySurface(layer.Surface);
                            }

                            //
                            AppWorkspace.SuspendThumbnailUpdates();
                            //

                            using (EffectConfigDialog configDialog = effect.CreateConfigDialog())
                            {
                                configDialog.Opacity             = 0.9;
                                configDialog.Effect              = effect;
                                configDialog.EffectSourceSurface = originalSurface;
                                configDialog.Selection           = selectedRegion;

                                EventHandler eh = new EventHandler(EffectConfigTokenChangedHandler);
                                configDialog.EffectTokenChanged += eh;

                                if (this.effectTokens.ContainsKey(effectType))
                                {
                                    EffectConfigToken oldToken = (EffectConfigToken)effectTokens[effectType].Clone();
                                    configDialog.EffectToken = oldToken;
                                }

                                BackgroundEffectRenderer ber = new BackgroundEffectRenderer(
                                    effect,
                                    configDialog.EffectToken,
                                    new RenderArgs(layer.Surface),
                                    new RenderArgs(originalSurface),
                                    previewRegion,
                                    tilesPerCpu * renderingThreadCount,
                                    renderingThreadCount);

                                ber.RenderedTile      += new RenderedTileEventHandler(RenderedTileHandler);
                                ber.StartingRendering += new EventHandler(StartingRenderingHandler);
                                ber.FinishedRendering += new EventHandler(FinishedRenderingHandler);
                                configDialog.Tag       = ber;

                                invalidateTimer.Enabled = true;
                                DialogResult dr = Utility.ShowDialog(configDialog, AppWorkspace);
                                invalidateTimer.Enabled = false;

                                this.InvalidateTimer_Tick(invalidateTimer, EventArgs.Empty);

                                if (dr == DialogResult.OK)
                                {
                                    this.effectTokens[effectType] = (EffectConfigToken)configDialog.EffectToken.Clone();
                                }

                                using (new WaitCursorChanger(AppWorkspace))
                                {
                                    ber.Abort();
                                    ber.Join();
                                    ber.Dispose();
                                    ber = null;

                                    if (dr != DialogResult.OK)
                                    {
                                        ((BitmapLayer)activeDW.ActiveLayer).Surface.CopySurface(originalSurface);
                                        activeDW.ActiveLayer.Invalidate();
                                    }

                                    configDialog.EffectTokenChanged -= eh;
                                    configDialog.Hide();
                                    AppWorkspace.Update();
                                    previewRegion.Dispose();
                                }

                                //
                                AppWorkspace.ResumeThumbnailUpdates();
                                //

                                if (dr == DialogResult.OK)
                                {
                                    PdnRegion remainingToRender = selectedRegion.Clone();
                                    PdnRegion alreadyRendered   = PdnRegion.CreateEmpty();

                                    for (int i = 0; i < this.progressRegions.Length; ++i)
                                    {
                                        if (this.progressRegions[i] == null)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            remainingToRender.Exclude(this.progressRegions[i]);
                                            alreadyRendered.Union(this.progressRegions[i]);
                                        }
                                    }

                                    activeDW.ActiveLayer.Invalidate(alreadyRendered);
                                    newLastToken = (EffectConfigToken)configDialog.EffectToken.Clone();
                                    AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
                                    DoEffect(effect, newLastToken, selectedRegion, remainingToRender, originalSurface);
                                }
                                else // if (dr == DialogResult.Cancel)
                                {
                                    using (new WaitCursorChanger(AppWorkspace))
                                    {
                                        activeDW.ActiveLayer.Invalidate();
                                        Utility.GCFullCollect();
                                    }

                                    resetDirtyValue = true;
                                    return;
                                }
                            }
                        }

                        finally
                        {
                            activeDW.ReturnScratchSurface(originalSurface);
                        }
                    }

                    // if it was from the Effects menu, save it as the "Repeat ...." item
                    if (effect.Category == EffectCategory.Effect)
                    {
                        this.lastEffect = effect;

                        if (newLastToken == null)
                        {
                            this.lastEffectToken = null;
                        }
                        else
                        {
                            this.lastEffectToken = (EffectConfigToken)newLastToken.Clone();
                        }

                        PopulateMenu(true);
                    }
                }

                finally
                {
                    selectedRegion.Dispose();
                    AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
                    AppWorkspace.Widgets.StatusBarProgress.EraseProgressStatusBar();
                    AppWorkspace.ActiveDocumentWorkspace.EnableOutlineAnimation = true;

                    for (int i = 0; i < this.progressRegions.Length; ++i)
                    {
                        if (this.progressRegions[i] != null)
                        {
                            this.progressRegions[i].Dispose();
                            this.progressRegions[i] = null;
                        }
                    }

                    if (resetDirtyValue)
                    {
                        AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = oldDirtyValue;
                    }
                }
            }
        }
Beispiel #10
0
        public void RunEffect(Type effectType)
        {
            bool oldDirtyValue   = AppWorkspace.ActiveDocumentWorkspace.Document.Dirty;
            bool resetDirtyValue = false;

            AppWorkspace.Update(); // make sure the window is done 'closing'
            AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
            DocumentWorkspace activeDW = AppWorkspace.ActiveDocumentWorkspace;

            PdnRegion selectedRegion;

            if (activeDW.Selection.IsEmpty)
            {
                selectedRegion = new PdnRegion(activeDW.Document.Bounds);
            }
            else
            {
                selectedRegion = activeDW.Selection.CreateRegion();
            }

            Exception   exception = null;
            Effect      effect    = null;
            BitmapLayer layer     = (BitmapLayer)activeDW.ActiveLayer;

            using (new PushNullToolMode(activeDW))
            {
                try
                {
                    effect = (Effect)Activator.CreateInstance(effectType);

                    string            name         = effect.Name;
                    EffectConfigToken newLastToken = null;

                    if (!(effect.CheckForEffectFlags(EffectFlags.Configurable)))
                    {
                        Surface copy = activeDW.BorrowScratchSurface(GetType() +
                                                                     ".RunEffect() using scratch surface for non-configurable rendering");

                        try
                        {
                            using (new WaitCursorChanger(AppWorkspace))
                            {
                                copy.CopySurface(layer.Surface);
                            }

                            EffectEnvironmentParameters eep = new EffectEnvironmentParameters(
                                AppWorkspace.AppEnvironment.PrimaryColor,
                                AppWorkspace.AppEnvironment.SecondaryColor,
                                AppWorkspace.AppEnvironment.PenInfo.Width,
                                selectedRegion,
                                copy);

                            effect.EnvironmentParameters = eep;

                            DoEffect(effect, null, selectedRegion, selectedRegion, copy, out exception);
                        }

                        finally
                        {
                            activeDW.ReturnScratchSurface(copy);
                        }
                    }
                    else
                    {
                        PdnRegion previewRegion = (PdnRegion)selectedRegion.Clone();
                        previewRegion.Intersect(RectangleF.Inflate(activeDW.VisibleDocumentRectangleF, 1, 1));

                        Surface originalSurface = activeDW.BorrowScratchSurface(GetType() +
                                                                                ".RunEffect() using scratch surface for rendering during configuration");

                        try
                        {
                            using (new WaitCursorChanger(AppWorkspace))
                            {
                                originalSurface.CopySurface(layer.Surface);
                            }

                            EffectEnvironmentParameters eep = new EffectEnvironmentParameters(
                                AppWorkspace.AppEnvironment.PrimaryColor,
                                AppWorkspace.AppEnvironment.SecondaryColor,
                                AppWorkspace.AppEnvironment.PenInfo.Width,
                                selectedRegion,
                                originalSurface);

                            effect.EnvironmentParameters = eep;

                            //
                            IDisposable resumeTUFn = AppWorkspace.SuspendThumbnailUpdates();
                            //

                            using (EffectConfigDialog configDialog = effect.CreateConfigDialog())
                            {
                                configDialog.Opacity             = 0.9;
                                configDialog.Effect              = effect;
                                configDialog.EffectSourceSurface = originalSurface;
                                configDialog.Selection           = selectedRegion;

                                BackgroundEffectRenderer ber = null;

                                void OnEffectTokenChanged(object sender, EventArgs e)
                                {
                                    EffectConfigDialog ecf = (EffectConfigDialog)sender;

                                    if (ber != null)
                                    {
                                        AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBarAsync();

                                        try
                                        {
                                            ber.Start();
                                        }

                                        catch (Exception ex)
                                        {
                                            exception = ex;
                                            ecf.Close();
                                        }
                                    }
                                }

                                configDialog.EffectTokenChanged += OnEffectTokenChanged;

                                if (EffectTokens.ContainsKey(effectType))
                                {
                                    EffectConfigToken oldToken = (EffectConfigToken)EffectTokens[effectType].Clone();
                                    configDialog.EffectToken = oldToken;
                                }

                                int pixelCount  = layer.Surface.Height;
                                int threadCount = effect.CheckForEffectFlags(EffectFlags.SingleThreaded) ? 1 : RenderingThreadCount;
                                int maxTiles    = TilesPerCpu * threadCount;
                                int tileCount   = Math.Min(maxTiles, pixelCount);
                                ber = new BackgroundEffectRenderer(
                                    effect,
                                    configDialog.EffectToken,
                                    new RenderArgs(layer.Surface),
                                    new RenderArgs(originalSurface),
                                    previewRegion,
                                    tileCount,
                                    threadCount);

                                ber.RenderedTile      += new RenderedTileEventHandler(RenderedTileHandler);
                                ber.StartingRendering += new EventHandler(StartingRenderingHandler);
                                ber.FinishedRendering += new EventHandler(FinishedRenderingHandler);

                                InvalidateTimer.Enabled = true;

                                DialogResult dr;

                                try
                                {
                                    dr = Utility.ShowDialog(configDialog, AppWorkspace);
                                }

                                catch (Exception ex)
                                {
                                    dr        = DialogResult.None;
                                    exception = ex;
                                }

                                InvalidateTimer.Enabled = false;

                                InvalidateTimer_Tick(InvalidateTimer, EventArgs.Empty);

                                if (dr == DialogResult.OK)
                                {
                                    EffectTokens[effectType] = (EffectConfigToken)configDialog.EffectToken.Clone();
                                }

                                using (new WaitCursorChanger(AppWorkspace))
                                {
                                    try
                                    {
                                        ber.Abort();
                                        ber.Join();
                                    }

                                    catch (Exception ex)
                                    {
                                        exception = ex;
                                    }

                                    ber.Dispose();
                                    ber = null;

                                    if (dr != DialogResult.OK)
                                    {
                                        ((BitmapLayer)activeDW.ActiveLayer).Surface.CopySurface(originalSurface);
                                        activeDW.ActiveLayer.Invalidate();
                                    }

                                    configDialog.EffectTokenChanged -= OnEffectTokenChanged;
                                    configDialog.Hide();
                                    AppWorkspace.Update();
                                    previewRegion.Dispose();
                                }

                                //
                                resumeTUFn.Dispose();
                                resumeTUFn = null;
                                //

                                if (dr == DialogResult.OK)
                                {
                                    PdnRegion remainingToRender = selectedRegion.Clone();
                                    PdnRegion alreadyRendered   = PdnRegion.CreateEmpty();

                                    for (int i = 0; i < ProgressRegions.Length; ++i)
                                    {
                                        if (ProgressRegions[i] == null)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            remainingToRender.Exclude(ProgressRegions[i]);
                                            alreadyRendered.Union(ProgressRegions[i]);
                                        }
                                    }

                                    activeDW.ActiveLayer.Invalidate(alreadyRendered);
                                    newLastToken = (EffectConfigToken)configDialog.EffectToken.Clone();
                                    AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
                                    DoEffect(effect, newLastToken, selectedRegion, remainingToRender, originalSurface, out exception);
                                }
                                else // if (dr == DialogResult.Cancel)
                                {
                                    using (new WaitCursorChanger(AppWorkspace))
                                    {
                                        activeDW.ActiveLayer.Invalidate();
                                        Utility.GCFullCollect();
                                    }

                                    resetDirtyValue = true;
                                    return;
                                }
                            }
                        }

                        catch (Exception ex)
                        {
                            exception = ex;
                        }

                        finally
                        {
                            activeDW.ReturnScratchSurface(originalSurface);
                        }
                    }

                    // if it was from the Effects menu, save it as the "Repeat ...." item
                    if (effect.Category == EffectCategory.Effect)
                    {
                        LastEffect = effect;

                        LastEffectToken = newLastToken == null ? null :
                                          (EffectConfigToken)newLastToken.Clone();

                        PopulateMenu(true);
                    }
                }

                catch (Exception ex)
                {
                    exception = ex;
                }

                finally
                {
                    selectedRegion.Dispose();
                    AppWorkspace.Widgets.StatusBarProgress.ResetProgressStatusBar();
                    AppWorkspace.Widgets.StatusBarProgress.EraseProgressStatusBar();
                    AppWorkspace.ActiveDocumentWorkspace.EnableOutlineAnimation = true;

                    if (ProgressRegions != null)
                    {
                        for (int i = 0; i < ProgressRegions.Length; ++i)
                        {
                            ProgressRegions[i]?.Dispose();
                            ProgressRegions[i] = null;
                        }
                    }

                    if (resetDirtyValue)
                    {
                        AppWorkspace.ActiveDocumentWorkspace.Document.Dirty = oldDirtyValue;
                    }

                    if (exception != null)
                    {
                        HandleEffectException(AppWorkspace, effect, exception);
                    }
                }
            }
        }
Beispiel #11
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "PDN Preset Files (.pst)|*.pst";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileStream      stream = File.Open(openFileDialog.FileName, FileMode.Open);
                BinaryFormatter reader = new BinaryFormatter();

                this.lbEffect.Items.Clear();
                this.types       = new List <int>();
                this.effects     = new List <Effect>();
                this.dialogs     = new List <EffectConfigDialog>();
                this.collections = new List <PropertyCollection>();

                int                            type       = 0;
                Effect                         effect     = null;
                EffectConfigDialog             dialog     = null;
                PropertyCollection             collection = null;
                PropertyBasedEffectConfigToken token      = null;

                int effectCount = (int)reader.Deserialize(stream);
                for (int i = 0; i < effectCount; i++)
                {
                    type = (int)reader.Deserialize(stream);

                    effect = (Effect)available[type].GetConstructor(Type.EmptyTypes).Invoke(new object[0]);

                    if ((effect.Options.Flags & EffectFlags.Configurable) != 0)
                    {
                        dialog        = effect.CreateConfigDialog();
                        dialog.Effect = effect;

                        if (dialog.EffectToken is PropertyBasedEffectConfigToken)
                        {
                            collection = ((PropertyBasedEffectConfigToken)dialog.EffectToken).Properties;
                            token      = new PropertyBasedEffectConfigToken(collection);

                            IEnumerator <Property> enumerator = collection.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                token.SetPropertyValue(enumerator.Current.Name, reader.Deserialize(stream));
                            }

                            dialog.EffectToken = token;
                            collection         = ((PropertyBasedEffectConfigToken)dialog.EffectToken).Properties;
                        }
                        else
                        {
                            Type           tokenType = dialog.EffectToken.GetType();
                            PropertyInfo[] info      = tokenType.GetProperties();

                            for (int j = 0; j < info.Length; j++)
                            {
                                if (info[j].GetValue(dialog.EffectToken).GetType().IsSerializable == true)
                                {
                                    object property = reader.Deserialize(stream);
                                    info[j].SetValue(dialog.EffectToken, property);
                                }
                            }
                        }
                    }

                    this.lbEffect.Items.Add(this.cbEffect.Items[type]);
                    this.types.Add(type);
                    this.effects.Add(effect);
                    this.dialogs.Add(dialog);
                    this.collections.Add(collection);
                }

                FinishTokenUpdate();
                stream.Close();
            }
        }