Skip to content

Commit 18cf14c

Browse files
committed
Add incomplete ConcurrentNotesWidgetSizeCalculator
- should be used by ConcurrentNotesWidget to help measure and lay itself out
1 parent d0e616a commit 18cf14c

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.ataulm.notes;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
9+
/*
10+
* This class depends on knowing what the layout looks like. Makes sense if it handles size and position of different notes.
11+
*/
12+
class ConcurrentNotesWidgetSizeCalculator {
13+
14+
private static final Comparator<Note> NOTE_COMPARATOR = new Comparator<Note>() {
15+
@Override
16+
public int compare(Note o1, Note o2) {
17+
if (o1.midi() == o2.midi()) {
18+
return 0;
19+
}
20+
if (o1.midi() > o2.midi()) {
21+
return 1;
22+
} else {
23+
return -1;
24+
}
25+
}
26+
};
27+
28+
private final MusicalSymbolSizes symbolSizes;
29+
private final PositionsApartFromMiddleCCalculator positionCalculator;
30+
31+
ConcurrentNotesWidgetSizeCalculator(MusicalSymbolSizes symbolSizes, PositionsApartFromMiddleCCalculator positionCalculator) {
32+
this.symbolSizes = symbolSizes;
33+
this.positionCalculator = positionCalculator;
34+
}
35+
36+
Size size(Key key, ConcurrentNotes concurrentNotes) {
37+
List<Note> notes = new ArrayList<>(concurrentNotes.notes());
38+
Collections.sort(notes, NOTE_COMPARATOR);
39+
40+
return Size.create(requiredWidth(notes), requiredHeight(key, notes));
41+
}
42+
43+
private int requiredWidth(List<Note> notes) {
44+
// TODO: best case is symbolSizes.note.width()
45+
// also need to account for:
46+
// accidentals,
47+
// notes which are too close to each other to be displayed directly beneath each other,
48+
// and accidentals which are too close to each other to be displayed directly beneath each other
49+
return symbolSizes.note.width();
50+
}
51+
52+
private int requiredHeight(Key key, List<Note> notes) {
53+
// TODO: need to account for accidentals
54+
return symbolSizes.note.height();
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ataulm.notes;
2+
3+
import org.junit.Before;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.mockito.Mock;
7+
import org.mockito.junit.MockitoJUnit;
8+
import org.mockito.junit.MockitoRule;
9+
10+
import static com.ataulm.notes.Fixtures.C4;
11+
import static org.fest.assertions.api.Assertions.assertThat;
12+
13+
public class ConcurrentNotesWidgetSizeCalculatorTest {
14+
15+
private static final int NOTE_WIDTH = 10;
16+
private static final int NOTE_HEIGHT = 10;
17+
18+
private static final int SHARP_WIDTH = 10;
19+
private static final int SHARP_HEIGHT = 10;
20+
21+
private static final int FLAT_WIDTH = 10;
22+
private static final int FLAT_HEIGHT = 10;
23+
24+
private static final int NATURAL_WIDTH = 10;
25+
private static final int NATURAL_HEIGHT = 10;
26+
27+
@Rule
28+
public MockitoRule rule = MockitoJUnit.rule();
29+
30+
@Mock
31+
PositionsApartFromMiddleCCalculator positionCalculator;
32+
33+
ConcurrentNotesWidgetSizeCalculator calculator;
34+
35+
@Before
36+
public void setUp() {
37+
MusicalSymbolSizes symbolSizes = new MusicalSymbolSizes(
38+
Size.create(NOTE_WIDTH, NOTE_HEIGHT),
39+
Size.create(SHARP_WIDTH, SHARP_HEIGHT),
40+
Size.create(FLAT_WIDTH, FLAT_HEIGHT),
41+
Size.create(NATURAL_WIDTH, NATURAL_HEIGHT)
42+
);
43+
calculator = new ConcurrentNotesWidgetSizeCalculator(symbolSizes, positionCalculator);
44+
}
45+
46+
@Test
47+
public void whenSizingSingleNote_thenSizeMatchesSingleNote() {
48+
ConcurrentNotes notes = ConcurrentNotes.create(C4);
49+
50+
Size size = calculator.size(Key.C_MAJ, notes);
51+
52+
assertThat(size).isEqualTo(Size.create(NOTE_WIDTH, NOTE_HEIGHT));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)