private void NotificationMessageReceived(NotificationMessage msg) { //If this is not the intended target if (msg.Target != null && msg.Target != this) { return; } //If this is a dead tab waiting to be destroyed if (VisibleName == null) { return; } FocusModel Model = msg.Sender as FocusModel; switch (msg.Notification) { case "CloseEditFocus": switch (ModeType) { case RelationMode.Add: ManageFocusViewModel viewModel = msg.Sender as ManageFocusViewModel; if (viewModel != null) { addFocusToList(viewModel.Focus); } DrawOnCanvas(); break; case RelationMode.Edit: EditGridDefinition(); DrawOnCanvas(); break; } ModeType = RelationMode.None; break; case "DeleteFocus": if (Model == null) { return; } //Check if model is in the selected foci if (Model.IsSelected && SelectedFocuses.Contains(Model)) { //Delete everyone foreach (FocusModel item in SelectedFocuses) { DeleteFocus(item); } break; } //Otherwise, kill the current focus DeleteFocus(Model); break; case "AddFocusMutually": ModeType = RelationMode.MutuallyExclusive; if (Model != null) { ChosenFocusForLink = Model; Model.IsWaiting = true; } break; case "FinishAddFocusMutually": if (ChosenFocusForLink != null && ChosenFocusForLink != Model && FociList.Any(f => f == Model)) { UndoService.Current[GetUndoRoot()] .BeginChangeSetBatch("AddMutuallyExclusive", false); ModeType = RelationMode.None; ChosenFocusForLink.IsWaiting = false; MutuallyExclusiveSetModel tempo = new MutuallyExclusiveSetModel(ChosenFocusForLink, Model); ChosenFocusForLink.MutualyExclusive.Add(tempo); Model?.MutualyExclusive.Add(tempo); ChosenFocusForLink = null; Messenger.Default.Send(new NotificationMessage(this, new ViewModelLocator().StatusBar, "Clear_message")); UndoService.Current[GetUndoRoot()].EndChangeSetBatch(); RaisePropertyChanged(() => FociList); DrawOnCanvas(); } break; case "AddFocusPrerequisite": ModeType = RelationMode.Prerequisite; if (Model != null) { ChosenFocusForLink = Model; Model.IsWaiting = true; } break; case "FinishAddFocusPrerequisite": if (ChosenFocusForLink != null && ChosenFocusForLink != Model && FociList.Any(f => f == Model)) { UndoService.Current[GetUndoRoot()] .BeginChangeSetBatch("AddPrerequisite", false); ModeType = RelationMode.None; if (ModeParam == RelationModeParam.Required) { //Create new set PrerequisitesSetModel set = new PrerequisitesSetModel(ChosenFocusForLink); set.FociList.Add(Model); ChosenFocusForLink.Prerequisite.Add(set); } else if (ModeParam == RelationModeParam.Optional) { //Create new set if no exist if (!ChosenFocusForLink.Prerequisite.Any()) { PrerequisitesSetModel set = new PrerequisitesSetModel(ChosenFocusForLink); ChosenFocusForLink.Prerequisite.Add(set); } //Add Model to last Set ChosenFocusForLink.Prerequisite.Last().FociList.Add(Model); } ChosenFocusForLink.IsWaiting = false; ChosenFocusForLink = null; Messenger.Default.Send(new NotificationMessage(this, new ViewModelLocator().StatusBar, "Clear_message")); UndoService.Current[GetUndoRoot()].EndChangeSetBatch(); RaisePropertyChanged(() => FociList); DrawOnCanvas(); } break; case "MakeRelativeTo": ModeType = RelationMode.RelativeTo; if (Model != null) { ChosenFocusForLink = Model; Model.IsWaiting = true; } break; case "FinishMakeRelativeTo": if (ChosenFocusForLink != null && ChosenFocusForLink != Model && FociList.Any(f => f == Model)) { if (Model != null) { UndoService.Current[GetUndoRoot()] .BeginChangeSetBatch("MakeRelativeTo", false); ModeType = RelationMode.None; ChosenFocusForLink.IsWaiting = false; ChosenFocusForLink.X = ChosenFocusForLink.DisplayX - Model.DisplayX; ChosenFocusForLink.Y = ChosenFocusForLink.DisplayY - Model.DisplayY; ChosenFocusForLink.CoordinatesRelativeTo = Model; ChosenFocusForLink = null; Messenger.Default.Send(new NotificationMessage(this, new ViewModelLocator().StatusBar, "Clear_message")); UndoService.Current[GetUndoRoot()].EndChangeSetBatch(); RaisePropertyChanged(() => FociList); DrawOnCanvas(); } } break; case "PositionChanged": if (Model != null) { foreach (FocusModel focus in FociList.Where(f => f.CoordinatesRelativeTo == Model)) { focus.RaisePropertyChanged(() => focus.DisplayX); focus.RaisePropertyChanged(() => focus.DisplayY); } DrawOnCanvas(); } UndoService.Current[GetUndoRoot()].EndChangeSetBatch(); break; } if (msg.Target == this) { //Resend to the tutorial View model if this was the target Messenger.Default.Send(new NotificationMessage(msg.Sender, new ViewModelLocator().Tutorial, msg.Notification)); } }
public static FocusGridModel CreateTreeFromScript(string fileName, Script script) { if (script.Logger.hasErrors()) { return(null); } FocusGridModel container = new FocusGridModel(script.TryParse(script, "id")); //Get content of Modifier block Assignation modifier = script.FindAssignation("modifier"); container.TAG = script.TryParse(modifier, "tag", null, false); if (container.TAG != null) { container.AdditionnalMods = modifier.GetContentAsScript(new[] { "add", "tag" }) .Parse(script.Comments, 0); } //Run through all foci foreach (ICodeStruct codeStruct in script.FindAllValuesOfType <CodeBlock>("focus")) { CodeBlock block = (CodeBlock)codeStruct; try { //Create the focus FocusModel newFocus = new FocusModel { UniqueName = script.TryParse(block, "id"), Text = script.TryParse(block, "text", null, false), Image = script.TryParse(block, "icon").Replace("GFX_", ""), X = int.Parse(script.TryParse(block, "x")), Y = int.Parse(script.TryParse(block, "y")), Cost = GetDouble(script.TryParse(block, "cost"), 10) }; //Get all core scripting elements Script InternalFocusScript = block. GetContentAsScript(ALL_PARSED_ELEMENTS.ToArray(), script.Comments); newFocus.InternalScript = InternalFocusScript; if (script.Logger.hasErrors()) { new ViewModelLocator().ErrorDawg.AddError( string.Join("\n", script.Logger.getErrors())); continue; } container.FociList.Add(newFocus); } catch (Exception) { //TODO: Add language support new ViewModelLocator().ErrorDawg.AddError(script.Logger.ErrorsToString()); new ViewModelLocator().ErrorDawg.AddError("Invalid syntax for focus " + script.TryParse(block, "id") + ", please double-check the syntax."); } } //Run through all foci again for mutually exclusives and prerequisites foreach (ICodeStruct codeStruct in script.FindAllValuesOfType <CodeBlock>("focus")) { CodeBlock block = (CodeBlock)codeStruct; string id = block.FindValue("id") != null?block.FindValue("id").Parse() : ""; FocusModel newFocus = container.FociList.FirstOrDefault(f => f.UniqueName == id); if (newFocus == null) { //Check if we removed this focus because of a syntax error continue; } //Try to find its relative to string relativeToId = script.TryParse(block, "relative_position_id", null, false); if (!string.IsNullOrEmpty(relativeToId)) { newFocus.CoordinatesRelativeTo = container.FociList.FirstOrDefault(f => f.UniqueName == relativeToId); } //Recreate its mutually exclusives foreach (ICodeStruct exclusives in block.FindAllValuesOfType <ICodeStruct> ("mutually_exclusive")) { foreach (ICodeStruct focuses in exclusives .FindAllValuesOfType <ICodeStruct>("focus")) { //Check if focus exists in list FocusModel found = container.FociList.FirstOrDefault(f => f.UniqueName == focuses.Parse()); //If we have found something if (found == null) { continue; } MutuallyExclusiveSetModel set = new MutuallyExclusiveSetModel(newFocus, found); //Check if the set already exists in this focus if (newFocus.MutualyExclusive.Contains(set) || found.MutualyExclusive.Contains(set)) { continue; } newFocus.MutualyExclusive.Add(set); found.MutualyExclusive.Add(set); } } //Recreate its prerequisites foreach (ICodeStruct prerequistes in block .FindAllValuesOfType <ICodeStruct>("prerequisite")) { if (!prerequistes.FindAllValuesOfType <ICodeStruct>("focus").Any()) { break; } PrerequisitesSetModel set = new PrerequisitesSetModel(newFocus); foreach (ICodeStruct focuses in prerequistes.FindAllValuesOfType <ICodeStruct> ("focus")) { //Add the focus as a prerequisites in the current existing focuses FocusModel search = container.FociList.FirstOrDefault((f) => f.UniqueName == focuses.Parse()); if (search != null) { set.FociList.Add(search); } } //If any prerequisite was added (Poland has empty prerequisite blocks...) if (set.FociList.Any()) { newFocus.Prerequisite.Add(set); } } } return(container); }