Ejemplo n.º 1
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            var preset = (IOverlayPreset)cbPreset.SelectedItem;
            var name   = textBox1.Text;

            if (NameValidator(name))
            {
                if (preset == null)
                {
                    MessageBox.Show(this, Resources.PromptSelectPreset, "OverlayPlugin", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.None;
                    return;
                }

                if (preset.Url == "special:custom")
                {
                    if (cbType.SelectedItem == null)
                    {
                        MessageBox.Show(this, Resources.PromptSelectOverlayType, "OverlayPlugin", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        DialogResult = DialogResult.None;
                        return;
                    }

                    var overlayType = ((KeyValuePair <string, Type>)cbType.SelectedItem).Value;
                    var parameters  = new NamedParameterOverloads();
                    parameters["config"] = null;
                    parameters["name"]   = name;

                    SelectedOverlay = (IOverlay)container.Resolve(overlayType, parameters);
                }
                else
                {
                    // Store the current preview position and size in the config object...
                    preview.SavePositionAndSize();

                    // ... and update the name as well.
                    preview.Config.Name = name;

                    if (preview.GetType() == typeof(Overlays.MiniParseOverlay))
                    {
                        // Reconstruct the overlay config to get rid of any event handlers the previous overlay
                        // registered. I should probably write a proper Dispose() implementation in MiniParseOverlay instead
                        // but this is much shorter and does the job just as well.
                        var config = JsonConvert.DeserializeObject <Overlays.MiniParseOverlayConfig>(JsonConvert.SerializeObject(preview.Config));

                        // Reconstruct the overlay to reset the preview state.
                        SelectedOverlay = new Overlays.MiniParseOverlay(config, name, container);
                        if (config.Url == "" || config.Url.Contains("loading.html"))
                        {
                            // If the preview didn't load, we try again here to avoid ending up with an empty overlay.
#if DEBUG
                            var resourcesPath = "file:///" + pluginMain.PluginDirectory.Replace('\\', '/') + "/libs/resources";
#else
                            var resourcesPath = "file:///" + pluginMain.PluginDirectory.Replace('\\', '/') + "/resources";
#endif
                            SelectedOverlay.Navigate(preset.Url.Replace("%%", resourcesPath));
                        }
                    }
                    else
                    {
                        SelectedOverlay = preview;
                    }
                }

                DialogResult = DialogResult.OK;
            }
            else
            {
                DialogResult = DialogResult.None;
            }
        }