// Loop over the constraint properties public void loop_over_constraintVM(SnapViewDirector svd) { // Loop over properties in ViewModel looking for BaseConstraintViewModel objects foreach (var prop in typeof(PlayerStatViewModel).GetProperties()) { if (prop.GetValue(this) is BaseConstraintViewModel) { BaseConstraintViewModel bvm = (BaseConstraintViewModel)prop.GetValue(this); // Generate a concrete ConstraintMC based on this viewmodel // Two purposes for this: // i) Use it to search for an exisiting Constraint in the svd // ii) If this constraint is active in the viewmodel, this new MC will be attached to the svd ConstraintMC cmc_vm = bvm.generate_ConstraintMC(svd); // Search for ConstraintMC matching this one - look for underlying action type matching. ConstraintMC cmc_svd = svd.get_matching_ConstraintMC(cmc_vm); // If we found a ConstraintMC with the same underlying Constraint // then detach it from the svd to prepare for the new one. // Or we don't need a constraint of this type at all if this constraint is disabled in the viewmodel // In either case we need to detach it. if (cmc_svd != null) { svd.Detach(cmc_svd); } // Now the existing svd version is detached if found, we attach the new one if needed if (bvm.active) { svd.Attach(cmc_vm); } } //if BaseConstraintViewModel } //foreach } //method
public ConstraintMC get_matching_ConstraintMC(ConstraintMC target_cmc) { foreach (MediatorColleague mc in mcList) { if (mc is ConstraintMC) { ConstraintMC cmc = (ConstraintMC)mc; if (cmc.get_action().GetType().Name == target_cmc.get_action().GetType().Name) return cmc; } } return null; }