private void ProductionGraphViewer_MouseUp(object sender, MouseEventArgs e)
        {
            Focus();

            GraphElement element = GetElementsAtPoint(ScreenToGraph(e.Location)).FirstOrDefault();

            if (element != null)
            {
                element.MouseUp(Point.Add(ScreenToGraph(e.Location), new Size(-element.X, -element.Y)), e.Button);
            }

            DraggedElement = null;

            switch (e.Button)
            {
            case MouseButtons.Middle:
                IsBeingDragged = false;
                break;
            }
        }
Example #2
0
        public void LoadFromJson(JObject json)
        {
            Graph.Nodes.Clear();
            Elements.Clear();

            //Has to go first, as all other data depends on which mods are loaded
            List <String> EnabledMods = json["EnabledMods"].Select(t => (String)t).ToList();

            foreach (Mod mod in DataCache.Mods)
            {
                mod.Enabled = EnabledMods.Contains(mod.Name);
            }
            List <String> enabledMods = DataCache.Mods.Where(m => m.Enabled).Select(m => m.Name).ToList();


            using (ProgressForm form = new ProgressForm(enabledMods))
            {
                form.ShowDialog();
            }

            Graph.SelectedAmountType = (AmountType)(int)json["AmountType"];
            Graph.SelectedUnit       = (RateUnit)(int)json["Unit"];

            List <JToken> nodes = json["Nodes"].ToList <JToken>();

            foreach (var node in nodes)
            {
                ProductionNode newNode = null;

                switch ((String)node["NodeType"])
                {
                case "Consumer":
                {
                    String itemName = (String)node["ItemName"];
                    if (DataCache.Items.ContainsKey(itemName))
                    {
                        Item item = DataCache.Items[itemName];
                        newNode = ConsumerNode.Create(item, Graph);
                    }
                    else
                    {
                        Item missingItem = new Item(itemName);
                        missingItem.IsMissingItem = true;
                        newNode = ConsumerNode.Create(missingItem, Graph);
                    }
                    break;
                }

                case "Supply":
                {
                    String itemName = (String)node["ItemName"];
                    if (DataCache.Items.ContainsKey(itemName))
                    {
                        Item item = DataCache.Items[itemName];
                        newNode = SupplyNode.Create(item, Graph);
                    }
                    else
                    {
                        Item missingItem = new Item(itemName);
                        missingItem.IsMissingItem = true;
                        DataCache.Items.Add(itemName, missingItem);
                        newNode = SupplyNode.Create(missingItem, Graph);
                    }
                    break;
                }

                case "PassThrough":
                {
                    String itemName = (String)node["ItemName"];
                    if (DataCache.Items.ContainsKey(itemName))
                    {
                        Item item = DataCache.Items[itemName];
                        newNode = PassthroughNode.Create(item, Graph);
                    }
                    else
                    {
                        Item missingItem = new Item(itemName);
                        missingItem.IsMissingItem = true;
                        DataCache.Items.Add(itemName, missingItem);
                        newNode = PassthroughNode.Create(missingItem, Graph);
                    }
                    break;
                }

                case "Recipe":
                {
                    String recipeName = (String)node["RecipeName"];
                    if (DataCache.Recipes.ContainsKey(recipeName))
                    {
                        Recipe recipe = DataCache.Recipes[recipeName];
                        newNode = RecipeNode.Create(recipe, Graph);
                    }
                    else
                    {
                        Recipe missingRecipe = new Recipe(recipeName, 0f, new Dictionary <Item, float>(), new Dictionary <Item, float>());
                        missingRecipe.IsMissingRecipe = true;
                        DataCache.Recipes.Add(recipeName, missingRecipe);
                        newNode = RecipeNode.Create(missingRecipe, Graph);
                    }

                    if (node["Assembler"] != null)
                    {
                        var assemblerKey = (String)node["Assembler"];
                        if (DataCache.Assemblers.ContainsKey(assemblerKey))
                        {
                            (newNode as RecipeNode).Assembler = DataCache.Assemblers[assemblerKey];
                        }
                    }

                    (newNode as RecipeNode).NodeModules = ModuleSelector.Load(node);
                    break;
                }

                default:
                {
                    Trace.Fail("Unknown node type: " + node["NodeType"]);
                    break;
                }
                }

                if (newNode != null)
                {
                    newNode.rateType = (RateType)(int)node["RateType"];
                    if (newNode.rateType == RateType.Manual)
                    {
                        if (node["DesiredRate"] != null)
                        {
                            newNode.desiredRate = (float)node["DesiredRate"];
                        }
                        else
                        {
                            // Legacy data format stored desired rate in actual
                            newNode.desiredRate = (float)node["ActualRate"];
                        }
                    }
                    if (node["SpeedBonus"] != null)
                    {
                        newNode.SpeedBonus = Math.Round((float)node["SpeedBonus"], 4);
                    }
                    if (node["ProductivityBonus"] != null)
                    {
                        newNode.ProductivityBonus = Math.Round((float)node["ProductivityBonus"], 4);
                    }
                }
            }

            List <JToken> nodeLinks = json["NodeLinks"].ToList <JToken>();

            foreach (var nodelink in nodeLinks)
            {
                ProductionNode supplier = Graph.Nodes[(int)nodelink["Supplier"]];
                ProductionNode consumer = Graph.Nodes[(int)nodelink["Consumer"]];

                String itemName = (String)nodelink["Item"];
                if (!DataCache.Items.ContainsKey(itemName))
                {
                    Item missingItem = new Item(itemName);
                    missingItem.IsMissingItem = true;
                    DataCache.Items.Add(itemName, missingItem);
                }
                Item item = DataCache.Items[itemName];
                NodeLink.Create(supplier, consumer, item);
            }

            IEnumerable <String> EnabledAssemblers = json["EnabledAssemblers"].Select(t => (String)t);

            foreach (Assembler assembler in DataCache.Assemblers.Values)
            {
                assembler.Enabled = EnabledAssemblers.Contains(assembler.Name);
            }

            IEnumerable <String> EnabledMiners = json["EnabledMiners"].Select(t => (String)t);

            foreach (Miner miner in DataCache.Miners.Values)
            {
                miner.Enabled = EnabledMiners.Contains(miner.Name);
            }

            IEnumerable <String> EnabledModules = json["EnabledModules"].Select(t => (String)t);

            foreach (Module module in DataCache.Modules.Values)
            {
                module.Enabled = EnabledModules.Contains(module.Name);
            }

            JToken enabledRecipesToken;

            if (json.TryGetValue("EnabledRecipes", out enabledRecipesToken))
            {
                IEnumerable <String> EnabledRecipes = enabledRecipesToken.Select(t => (String)t);
                foreach (Recipe recipe in DataCache.Recipes.Values)
                {
                    recipe.Enabled = EnabledRecipes.Contains(recipe.Name);
                }
            }

            Graph.UpdateNodeValues();
            AddRemoveElements();

            List <String> ElementLocations = json["ElementLocations"].Select(l => (String)l).ToList();

            for (int i = 0; i < ElementLocations.Count; i++)
            {
                int[]        splitPoint = ElementLocations[i].Split(',').Select(s => Convert.ToInt32(s)).ToArray();
                GraphElement element    = GetElementForNode(Graph.Nodes[i]);
                element.Location = new Point(splitPoint[0], splitPoint[1]);
            }

            LimitViewToBounds();
        }