Skip to content

Commit

Permalink
can select many loopouts and change all of their lengths at once (or …
Browse files Browse the repository at this point in the history
…convert all to crossovers by setting length 0)
  • Loading branch information
dave-doty committed Nov 4, 2020
1 parent 421b66f commit 0191ce1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/src/actions/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,26 @@ abstract class LoopoutLengthChange
static Serializer<LoopoutLengthChange> get serializer => _$loopoutLengthChangeSerializer;
}

abstract class LoopoutsLengthChange
with BuiltJsonSerializable, UndoableAction
implements Built<LoopoutsLengthChange, LoopoutsLengthChangeBuilder> {
BuiltList<Loopout> get loopouts;

int get length;

/************************ begin BuiltValue boilerplate ************************/
factory LoopoutsLengthChange(Iterable<Loopout> loopouts, int length) => LoopoutsLengthChange.from((b) => b
..loopouts.replace(loopouts)
..length = length);

factory LoopoutsLengthChange.from([void Function(LoopoutsLengthChangeBuilder) updates]) =
_$LoopoutsLengthChange;

LoopoutsLengthChange._();

static Serializer<LoopoutsLengthChange> get serializer => _$loopoutsLengthChangeSerializer;
}

abstract class ConvertCrossoverToLoopout
with BuiltJsonSerializable, UndoableAction
implements StrandPartAction, Built<ConvertCrossoverToLoopout, ConvertCrossoverToLoopoutBuilder> {
Expand Down
55 changes: 55 additions & 0 deletions lib/src/reducers/change_loopout_length.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Strand convert_crossover_to_loopout_reducer(Strand strand, actions.ConvertCrosso
var substrands_builder = strand.substrands.toBuilder();
substrands_builder.insert(action.crossover.next_domain_idx, loopout_new);
strand = strand.rebuild((s) => s..substrands = substrands_builder);
strand = strand.initialize();
return strand;
}

Expand Down Expand Up @@ -64,6 +65,59 @@ BuiltList<Strand> convert_crossovers_to_loopouts_reducer(BuiltList<Strand> stran
return strands_builder.build();
}

BuiltList<Strand> loopouts_length_change_reducer(BuiltList<Strand> strands,
AppState state, actions.LoopoutsLengthChange action) {
Map<String, List<Loopout>> loopouts_on_strand_id = {};
for (var loopout in action.loopouts) {
String strand_id = loopout.strand_id;
if (!loopouts_on_strand_id.containsKey(strand_id)) {
loopouts_on_strand_id[strand_id] = [];
}
loopouts_on_strand_id[strand_id].add(loopout);
}

var strands_builder = strands.toBuilder();
for (String strand_id in loopouts_on_strand_id.keys) {
Strand strand = state.design.strands_by_id[strand_id];
int strand_idx = strands.indexOf(strand);
var substrands = strand.substrands.toList();

List<Loopout> loopouts = loopouts_on_strand_id[strand_id];
// must sort by crossover order for this logic to work with num_crossovers_processed_on_strand variable
loopouts.sort((c1, c2) => c1.prev_domain_idx - c2.prev_domain_idx);
for (var loopout in loopouts) {
int loopout_idx = substrands.indexOf(loopout);
if (action.length > 0) {
// shorten length of existing loopout
Loopout loopout_new = loopout.rebuild((l) => l..loopout_length = action.length);
substrands[loopout_idx] = loopout_new;
} else if (action.length == 0) {
// convert to crossover by removing loopout
substrands.removeAt(loopout_idx);
}
}
var new_strand = strand.rebuild((s) => s..substrands.replace(substrands));
new_strand = new_strand.initialize();
strands_builder[strand_idx] = new_strand;
}

return strands_builder.build();

// var substrands_builder = strand.substrands.toBuilder();
// int loopout_idx = strand.substrands.indexOf(action.loopout);
// var substrands_builder = strand.substrands.toBuilder();
// if (action.length > 0) {
// // shorten length of existing loopout
// Loopout loopout_new = action.loopout.rebuild((l) => l..loopout_length = action.length);
// substrands_builder[loopout_idx] = loopout_new;
// } else if (action.length == 0) {
// // convert to crossover by removing loopout
// substrands_builder.removeAt(loopout_idx);
// }
// strand = strand.rebuild((s) => s..substrands = substrands_builder);
// return strand;
}

Strand loopout_length_change_reducer(Strand strand, actions.LoopoutLengthChange action) {
int loopout_idx = strand.substrands.indexOf(action.loopout);
var substrands_builder = strand.substrands.toBuilder();
Expand All @@ -76,5 +130,6 @@ Strand loopout_length_change_reducer(Strand strand, actions.LoopoutLengthChange
substrands_builder.removeAt(loopout_idx);
}
strand = strand.rebuild((s) => s..substrands = substrands_builder);
strand = strand.initialize();
return strand;
}
2 changes: 2 additions & 0 deletions lib/src/reducers/strands_reducer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GlobalReducer<BuiltList<Strand>, AppState> strands_global_reducer = combineGloba
join_strands_by_crossover_reducer),
TypedGlobalReducer<BuiltList<Strand>, AppState, actions.ConvertCrossoversToLoopouts>(
convert_crossovers_to_loopouts_reducer),
TypedGlobalReducer<BuiltList<Strand>, AppState, actions.LoopoutsLengthChange>(
loopouts_length_change_reducer),
]);

BuiltList<Strand> replace_strands_reducer(BuiltList<Strand> strands, actions.ReplaceStrands action) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ part 'serializers.g.dart';
Select,
Loopout,
LoopoutLengthChange,
LoopoutsLengthChange,
ConvertCrossoverToLoopout,
ConvertCrossoversToLoopouts,
EditModeChoice,
Expand Down
9 changes: 8 additions & 1 deletion lib/src/view/design_main_strand_loopout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ class DesignMainLoopoutComponent extends UiStatefulComponent2<DesignMainLoopoutP
if (new_length == null || new_length == props.loopout.loopout_length) {
return;
}
app.dispatch(actions.LoopoutLengthChange(props.loopout, new_length));
var selected_loopouts = app.state.ui_state.selectables_store.selected_loopouts;
actions.UndoableAction action;
if (selected_loopouts.length > 0) {
action = actions.LoopoutsLengthChange(selected_loopouts, new_length);
} else {
action = actions.LoopoutLengthChange(props.loopout, new_length);
}
app.dispatch(action);
}

String loopout_path_description_between_groups() {
Expand Down

0 comments on commit 0191ce1

Please sign in to comment.