Changeset - 50bbf5233575
[Not reviewed]
default
0 1 0
Jason Maltzen (jmaltzen) - 6 years ago 2018-05-18 18:06:20
jason.maltzen@unsanctioned.net
Make the parameters to recipe generation more readable in code.
1 file changed with 12 insertions and 1 deletions:
0 comments (0 inline, 0 general)
UI/RecipeGeneratorView.cs
Show inline comments
...
 
@@ -337,257 +337,268 @@ namespace DesertPaintLab
 

	
 
            colorStore.Clear();
 
            foreach (KeyValuePair<string, PaintRecipe> pair in profile.RibbonRecipes)
 
            {
 
                if (pair.Value.IsValidForConcentration(PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION))
 
                {
 
                    string colorName = pair.Key;
 
                    colorStore.AppendValues(colorName);
 
                }
 
            }
 
            recipeList.Show();
 
        }
 

	
 
        public PlayerProfile Profile {
 
            set {
 
                if (profile != value) {
 
                    // TODO: ensure not running
 
                    profile = value;
 
                    profile.LoadRecipes();
 

	
 
                    generator = new RecipeGenerator(profile.Reactions);
 
                    int threads;
 
                    DesertPaintLab.AppSettings.Get("GeneratorThreads", out threads);
 
                    if (threads <= 0) { threads = 15; }
 
                    generator.MaxThreads = (uint)threads;
 
        
 
                    generator.Progress += OnProgress;
 
                    generator.Finished += OnFinished;
 
                    generator.NewRecipe += OnNewRecipe;
 

	
 
                    if (checkButtonRibbon.Active)
 
                    {
 
                        InitStateForRibbons();
 
                    }
 
                    else
 
                    {
 
                        InitStateForPaint();
 
                    }
 
                }
 
            }
 
        }
 

	
 
        protected void OnMinIngredientsChanged(object sender, EventArgs e)
 
        {
 
            Gtk.SpinButton button = (Gtk.SpinButton) sender;
 
            if (button.ValueAsInt > maxIngredientsSpinButton.ValueAsInt)
 
            {
 
                maxIngredientsSpinButton.Value = button.ValueAsInt;
 
            }
 
            maxIngredientsSpinButton.SetRange(button.ValueAsInt, maxIngredientsSpinButton.Adjustment.Upper);
 
        }
 

	
 
        protected void OnMaxIngredientsChanged(object sender, EventArgs e)
 
        {
 
            Gtk.SpinButton button = (Gtk.SpinButton) sender;
 
            if (button.ValueAsInt < minIngredientsSpinButton.ValueAsInt)
 
            {
 
                minIngredientsSpinButton.Value = button.ValueAsInt;
 
            }
 
            minIngredientsSpinButton.SetRange(1, button.ValueAsInt);
 
            // TODO: save profile setting
 
            // TODO: no longer permit resume
 
        }
 

	
 
        protected void OnMaxRecipeChanged(object sender, EventArgs e)
 
        {
 
            // TODO: save profile setting
 
            // TODO: no longer permit resume
 
        }
 

	
 
        protected void OnFullQuantityDepthChanged(object sender, EventArgs e)
 
        {
 
            // TODO: save profile setting
 
            // TODO: no longer permit resume
 
        }
 

	
 
        protected void OnFullQuantityChanged(object sender, EventArgs e)
 
        {
 
            // TODO: save profile setting
 
            // TODO: no longer permit resume
 
        }
 

	
 
        protected void OnBegin(object sender, EventArgs e)
 
        {
 
            minIngredientsSpinButton.Sensitive = false;
 
            maxIngredientsSpinButton.Sensitive = false;
 
            //TODO ExportToWikiAction.Sensitive = false;
 
            maxRecipeSpinButton.Sensitive = false;
 
            beginButton.Sensitive = false; // TODO: change to "pause"?
 
            stopResumeButton.Sensitive = true;
 
            fullQuantitySpinButton.Sensitive = false;
 
            fullQuantityDepthSpinButton.Sensitive = false;
 
            reagentListView.Sensitive = false;
 
            checkButtonRibbon.Sensitive = false;
 

	
 
            countLabel.Text = String.Format("{0} / {1}", checkButtonRibbon.Active ? profile.RibbonCount : profile.RecipeCount, Palette.Count);
 

	
 
            // TODO: hook up event notifications
 
            // - progress
 
            // - complete
 
            // - new recipe / recipe update
 

	
 
            // Total recipe search count
 
            //int current = ReagentManager.Names.Count;
 
            //long recipePermutations = 1;
 
            //for (int i = 0; i < maxIngredientsSpinButton.ValueAsInt; ++i)
 
            //{
 
            //    recipePermutations *= current;
 
            //    --current;
 
            //}
 
            //System.Console.WriteLine("Will search {0} reagent permutations.", recipePermutations);
 

	
 
            lastProgressUpdate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
 
            lastStatusUpdate = lastProgressUpdate;
 

	
 
            lastProfileSave = lastProgressUpdate;
 
            lastCheckpoint = lastProgressUpdate;
 

	
 
            running = true;
 
            canceling = false;
 
            pauseForCheckpoint = false;
 
            stopResumeButton.Label = "Pause";
 

	
 
            if (Started != null)
 
            {
 
                Started(this, null);
 
            }
 

	
 
            generator.BeginRecipeGeneration((uint)(checkButtonRibbon.Active ? PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION : PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION), (uint)maxRecipeSpinButton.ValueAsInt, (uint)minIngredientsSpinButton.Value, (uint)maxIngredientsSpinButton.ValueAsInt, (uint)fullQuantityDepthSpinButton.ValueAsInt, (uint)fullQuantitySpinButton.ValueAsInt);
 
            uint minConcentration = (uint)(checkButtonRibbon.Active ? PaintRecipe.RIBBON_RECIPE_MIN_CONCENTRATION : PaintRecipe.PAINT_RECIPE_MIN_CONCENTRATION);
 
            uint maxConcentration = (uint)maxRecipeSpinButton.ValueAsInt;
 
            uint minIngredients = (uint)minIngredientsSpinButton.Value;
 
            uint maxIngredients = (uint)maxIngredientsSpinButton.ValueAsInt;
 
            uint fullQuantityDepth = (uint)fullQuantityDepthSpinButton.ValueAsInt;
 
            uint fullQuantity = (uint)fullQuantitySpinButton.ValueAsInt;
 
            generator.BeginRecipeGeneration(minConcentration, 
 
                                            maxConcentration, 
 
                                            minIngredients, 
 
                                            maxIngredients, 
 
                                            fullQuantityDepth, 
 
                                            fullQuantity);
 
        }
 

	
 
        protected void OnStopResume(object sender, EventArgs e)
 
        {
 
            if (generator != null)
 
            {
 
                if (running)
 
                {
 
                    canceling = true;
 
                    pauseForCheckpoint = false;
 
                    generator.Stop();
 
                }
 
                else
 
                {
 
                    // Resume previous run
 
                    //TODO ExportToWikiAction.Sensitive = false;
 
                    reagentListView.Sensitive = false;
 
                    minIngredientsSpinButton.Sensitive = false;
 
                    maxIngredientsSpinButton.Sensitive = false;
 
                    maxRecipeSpinButton.Sensitive = false;
 
                    beginButton.Sensitive = false;
 
                    stopResumeButton.Sensitive = true;
 
                    fullQuantitySpinButton.Sensitive = false;
 
                    fullQuantityDepthSpinButton.Sensitive = false;
 
                    reagentListView.Sensitive = false;
 
                    checkButtonRibbon.Sensitive = false;
 

	
 
                    lastProgressUpdate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
 
                    lastStatusUpdate = lastProgressUpdate;
 
                    lastProfileSave = lastProgressUpdate;
 
                    lastCheckpoint = lastProgressUpdate;
 

	
 
                    canceling = false;
 
                    pauseForCheckpoint = false;
 
                    running = true;
 
        
 
                    stopResumeButton.Label = "Pause";
 

	
 
                    if (Started != null)
 
                    {
 
                        Started(this, null);
 
                    }
 
                    generator.ResumeRecipeGeneration();
 
                }
 
            }
 
        }
 

	
 
        private void HandleFinished()
 
        {
 
            generator.Wait();
 
            if (pauseForCheckpoint)
 
            {
 
                pauseForCheckpoint = false;
 
                generator.SaveState(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
 
                generator.ResumeRecipeGeneration();
 
            }
 
            else
 
            {
 
                running = false;
 
                beginButton.Sensitive = true;
 
                //TODO ExportToWikiAction.Sensitive = true;
 
                stopResumeButton.Sensitive = false;
 
                minIngredientsSpinButton.Sensitive = true;
 
                maxIngredientsSpinButton.Sensitive = true;
 
                maxRecipeSpinButton.Sensitive = true;
 
                fullQuantitySpinButton.Sensitive = true;
 
                fullQuantityDepthSpinButton.Sensitive = true;
 
                reagentListView.Sensitive = true;
 
                checkButtonRibbon.Sensitive = true;
 

	
 
                //generator = null; // don't. Hang on to generator for resume.
 
                profile.SaveRecipes();
 
                if (canceling)
 
                {
 
                    generator.SaveState(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
 
                    stopResumeButton.Label = "Resume";
 
                    stopResumeButton.Sensitive = true;
 
                    beginButton.Label = "Restart";
 
                }
 
                else
 
                {
 
                    System.IO.File.Delete(System.IO.Path.Combine(profile.Directory, checkButtonRibbon.Active ? RIBBON_STATE_FILE : PAINT_STATE_FILE));
 
                }
 
                if (Stopped != null)
 
                {
 
                    Stopped(this, null);
 
                }
 
            }
 
        }
 

	
 
        protected void OnFinished(object sender, EventArgs args)
 
        {
 
            notifyFinished.WakeupMain();
 
            //Gtk.Application.Invoke(delegate {
 
            //    HandleFinished();
 
            //});
 
        }
 

	
 
        private void HandleNewRecipe()
 
        {
 
            progressBar.Pulse();
 
            
 
            PaintRecipe recipe = null;
 
            if (pendingNewRecipes.TryDequeue(out recipe))
 
            {
 
                string recipeColor = Palette.FindNearest(recipe.ReactedColor);
 
                // TODO: Add item to recipe list only if not already listed
 
                bool exists = false;
 
                Gtk.TreeIter iter;
 
                if (colorStore.GetIterFirst(out iter))
 
                {
 
                    do
 
                    {
 
                        string color = (string)colorStore.GetValue(iter, 0);
 
                        if (color.Equals(recipeColor))
 
                        {
 
                            exists = true;
 
                            break;
 
                        }
 
                    } while (colorStore.IterNext(ref iter));
 
                }
 
                if (!exists)
 
                {
 
                    //Console.WriteLine("Add new recipe for {0}", recipeColor);
 
                    //    bool isMissingReactions = args.Recipe.CheckMissingReactions(ref missingReactions);
 
                    //    string missingReactionLabel = isMissingReactions ? "X" : "";
 
                    colorStore.AppendValues(recipeColor); // , missingReactionLabel);
 
                    countLabel.Text = String.Format("{0} / {1}", (checkButtonRibbon.Active ? profile.RibbonCount : profile.RecipeCount)+1, Palette.Count);
0 comments (0 inline, 0 general)