PatternBlocks170fTests.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Round 170f (issue #60): FL's legacy Pattern Blocks — one row per pattern, a click places a whole instance of the row's
  2. // pattern on a bar; blocks play, export, save and undo like clips (docs/analise/REA_rodada170f.md)
  3. #include "core/PatternBlocks.h"
  4. namespace pb170f
  5. {
  6. // patterns P1 (1 bar, a note per beat on channel 0), P2 (2 bars), P3 (1 bar, empty)
  7. inline lynx::Song blockSong()
  8. {
  9. lynx::Song s;
  10. s.channels.resize (1);
  11. s.patterns.resize (3);
  12. for (int i = 0; i < 3; ++i)
  13. s.patterns[(std::size_t) i].name = "P" + std::to_string (i + 1);
  14. for (int step : { 0, 4, 8, 12 })
  15. s.patterns[0].toggleStep (0, step);
  16. s.patterns[1].lengthTicks = 8 * lynx::ppq;
  17. s.patterns[1].toggleStep (0, 0);
  18. s.patternBlocksOn = true;
  19. s.mode = lynx::PlayMode::song;
  20. return s;
  21. }
  22. constexpr std::int64_t bar = 4 * lynx::ppq;
  23. inline std::vector<int> noteOns (const lynx::Song& s, int samples)
  24. {
  25. const auto tps = lynx::ticksPerSample (120.0, 48000.0); // 1 beat = 24000 samples
  26. lynx::EventBuffer ev;
  27. lynx::scheduleSong (s, 0.0, samples, tps, ev, false, nullptr, false);
  28. std::vector<int> ons;
  29. for (auto& e : ev)
  30. if (e.type == lynx::NoteEvent::Type::on)
  31. ons.push_back (e.sampleOffset);
  32. return ons;
  33. }
  34. } // namespace pb170f
  35. TEST (pb170f_click_places_whole_pattern_on_the_bar)
  36. {
  37. using namespace lynx;
  38. using pb170f::bar;
  39. auto s = pb170f::blockSong();
  40. // a click anywhere in bar 3 places P1 at bar 3's start (no pattern to choose: the row is the pattern)
  41. CHECK (patternblocks::place (s, 0, 2 * bar + 300));
  42. CHECK (s.blocks.size() == 1 && s.blocks[0].pattern == 0 && s.blocks[0].start == 2 * bar);
  43. // the same bar again, or inside the block: nothing (blocks never stack)
  44. CHECK (! patternblocks::place (s, 0, 2 * bar + 10));
  45. // a 2-bar pattern: a block on bar 1 covers bars 1–2, so bar 2 is taken, bar 3 is free
  46. CHECK (patternblocks::place (s, 1, 0));
  47. CHECK (! patternblocks::place (s, 1, bar + 5));
  48. CHECK (! patternblocks::fits (s, 1, bar));
  49. CHECK (patternblocks::place (s, 1, 2 * bar));
  50. // the length is the pattern's: never stored, never cropped
  51. CHECK (patternblocks::lengthOf (s, 1) == 2 * bar);
  52. const auto clips = patternblocks::asClips (s);
  53. CHECK (clips.size() == 3);
  54. for (const auto& c : clips)
  55. CHECK (c.offset == 0 && c.length == patternblocks::lengthOf (s, c.pattern) && c.track == patternblocks::blockTrack);
  56. // missing patterns can't be painted
  57. CHECK (! patternblocks::place (s, 7, 0) && ! patternblocks::place (s, -1, 0));
  58. // blocks are kept sorted by row, then start
  59. CHECK (std::is_sorted (s.blocks.begin(), s.blocks.end(), [] (const PatternBlock& a, const PatternBlock& b) {
  60. return a.pattern != b.pattern ? a.pattern < b.pattern : a.start < b.start;
  61. }));
  62. }
  63. TEST (pb170f_drag_paints_at_pattern_length_and_right_drag_erases)
  64. {
  65. using namespace lynx;
  66. using pb170f::bar;
  67. auto s = pb170f::blockSong();
  68. // dragging a 2-bar row over bars 1–4 paints bars 1 and 3 (each bar where a whole block fits), either direction
  69. CHECK (patternblocks::paintRange (s, 1, 4 * bar - 1, 0) == 2);
  70. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 0 && s.blocks[1].start == 2 * bar);
  71. // a 1-bar row: every bar
  72. CHECK (patternblocks::paintRange (s, 0, 0, 3 * bar + 20) == 4);
  73. // right-click erases the block under the mouse (anywhere inside it), a right-drag every block crossed
  74. CHECK (patternblocks::blockAt (s, 1, 3 * bar + 7) >= 0);
  75. CHECK (patternblocks::erase (s, 1, 3 * bar + 7));
  76. CHECK (! patternblocks::erase (s, 1, 3 * bar + 7));
  77. CHECK (patternblocks::eraseRange (s, 0, bar + 1, 2 * bar + 1) == 2);
  78. int left = 0;
  79. for (const auto& b : s.blocks)
  80. left += b.pattern == 0 ? 1 : 0;
  81. CHECK (left == 2);
  82. }
  83. TEST (pb170f_blocks_play_in_song_mode_and_set_the_song_length)
  84. {
  85. using namespace lynx;
  86. using pb170f::bar;
  87. auto s = pb170f::blockSong();
  88. CHECK (patternblocks::place (s, 0, bar)); // P1 on bar 2
  89. CHECK (patternblocks::place (s, 1, 2 * bar)); // P2 on bars 3–4
  90. // the song ends at the last block's end (FL: Song mode loops at the end of the last item)
  91. CHECK (s.songLength() == 4 * bar);
  92. // what the audio engine plays: the rack's song copy holds them as plain clips
  93. auto state = RackState::build (s, nullptr, 48000.0, [] (const std::string&) { return std::shared_ptr<const SampleData>(); });
  94. CHECK (state != nullptr);
  95. if (state == nullptr)
  96. return;
  97. CHECK (state->song.blocks.empty() && ! state->song.patternBlocksOn);
  98. CHECK (state->song.playlist.size() == 2);
  99. const auto ons = pb170f::noteOns (state->song, 16 * 24000);
  100. // P1's 4 beats in bar 2 (beats 4–7), P2's single note at bar 3 (beat 8)
  101. CHECK (ons.size() == 5);
  102. CHECK (ons.size() == 5 && ons[0] == 4 * 24000 && ons[3] == 7 * 24000 && ons[4] == 8 * 24000);
  103. // the option off: the blocks stay in the project but don't play (and don't lengthen the song)
  104. s.patternBlocksOn = false;
  105. CHECK (! patternblocks::active (s));
  106. auto off = RackState::build (s, nullptr, 48000.0, [] (const std::string&) { return std::shared_ptr<const SampleData>(); });
  107. CHECK (off != nullptr && off->song.playlist.empty());
  108. CHECK (s.songLength() == bar); // an empty arrangement: the current pattern's length
  109. }
  110. TEST (pb170f_blocks_and_clips_play_together)
  111. {
  112. using namespace lynx;
  113. using pb170f::bar;
  114. auto s = pb170f::blockSong();
  115. Clip c;
  116. c.pattern = 0;
  117. c.start = 0;
  118. c.length = bar;
  119. s.playlist.push_back (c); // P1 as a clip on bar 1
  120. CHECK (patternblocks::place (s, 0, bar)); // and as a block on bar 2
  121. auto copy = s;
  122. patternblocks::expandSong (copy);
  123. CHECK (copy.playlist.size() == 2 && copy.blocks.empty());
  124. CHECK (pb170f::noteOns (copy, 8 * 24000).size() == 8);
  125. patternblocks::expandSong (copy); // twice is harmless
  126. CHECK (copy.playlist.size() == 2);
  127. }
  128. TEST (pb170f_a_grown_pattern_is_cut_at_its_next_block)
  129. {
  130. using namespace lynx;
  131. using pb170f::bar;
  132. auto s = pb170f::blockSong();
  133. CHECK (patternblocks::paintRange (s, 0, 0, bar + 1) == 2); // P1 on bars 1 and 2
  134. s.patterns[0].lengthTicks = 2 * bar; // P1 grows to 2 bars
  135. CHECK (patternblocks::playedEnd (s, s.blocks, 0) == bar); // the first stops where the second starts
  136. CHECK (patternblocks::playedEnd (s, s.blocks, 1) == 3 * bar);
  137. const auto clips = patternblocks::asClips (s);
  138. CHECK (clips.size() == 2 && clips[0].length == bar && clips[1].length == 2 * bar);
  139. CHECK (patternblocks::blockAt (s, 0, bar + 3) == 1);
  140. CHECK (s.songLength() == 3 * bar);
  141. }
  142. TEST (pb170f_bars_follow_time_signature_changes)
  143. {
  144. using namespace lynx;
  145. auto s = pb170f::blockSong();
  146. // 3/4 from bar 2 (tick 4 beats): bar 2 = [4, 7) beats, bar 3 = [7, 10)
  147. Marker m;
  148. m.tick = 4 * ppq;
  149. m.numerator = 3;
  150. m.denominator = 4;
  151. s.addMarker (m);
  152. s.patterns[2].lengthTicks = 3 * ppq; // P3: a 3-beat pattern
  153. CHECK (patternblocks::place (s, 2, 8 * ppq)); // inside bar 3
  154. CHECK (s.blocks.back().start == 7 * ppq);
  155. CHECK (patternblocks::paintRange (s, 2, 4 * ppq, 6 * ppq) == 1);
  156. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 4 * ppq);
  157. }
  158. TEST (pb170f_pattern_list_edits_keep_blocks_on_their_pattern)
  159. {
  160. using namespace lynx;
  161. using pb170f::bar;
  162. auto s = pb170f::blockSong();
  163. s.addArrangement ("Arrangement 2");
  164. CHECK (patternblocks::place (s, 2, 0)); // P3 in arrangement 2
  165. s.selectArrangement (0);
  166. CHECK (s.blocks.empty() && s.arrangements[1].blocks.size() == 1);
  167. CHECK (patternblocks::place (s, 1, 0)); // P2 in arrangement 1
  168. CHECK (patternblocks::place (s, 2, bar));
  169. // a pattern inserted before them: the rows follow their pattern (every arrangement)
  170. insertPattern (s, 0, "New");
  171. CHECK (s.blocks.size() == 2 && s.blocks[0].pattern == 2 && s.blocks[1].pattern == 3);
  172. CHECK (s.arrangements[1].blocks[0].pattern == 3);
  173. // moved: swapped with its neighbour
  174. CHECK (movePattern (s, 2, 1));
  175. CHECK (s.blocks[0].pattern == 3 || s.blocks[1].pattern == 3);
  176. // deleted: its blocks go, later rows move up
  177. s.removePattern (3); // P2 (now at 3)
  178. CHECK (s.blocks.size() == 1 && s.blocks[0].pattern == 2 && s.blocks[0].start == bar);
  179. CHECK (s.arrangements[1].blocks.size() == 1 && s.arrangements[1].blocks[0].pattern == 2);
  180. // a cloned pattern lands after its source and shifts the rows below it
  181. clonePattern (s, 0);
  182. CHECK (s.blocks[0].pattern == 3);
  183. // switching arrangements swaps the blocks like the clips
  184. s.selectArrangement (1);
  185. CHECK (s.blocks.size() == 1 && s.blocks[0].start == 0);
  186. s.selectArrangement (0);
  187. CHECK (s.blocks.size() == 1 && s.blocks[0].start == bar);
  188. }
  189. TEST (pb170f_turn_blocks_into_clips_one_locked_track_per_pattern)
  190. {
  191. using namespace lynx;
  192. using pb170f::bar;
  193. auto s = pb170f::blockSong();
  194. Clip c;
  195. c.pattern = 2;
  196. c.track = 1;
  197. c.length = bar;
  198. s.playlist.push_back (c); // a clip on track 2 already
  199. CHECK (patternblocks::paintRange (s, 0, 0, bar + 1) == 2);
  200. CHECK (patternblocks::place (s, 1, 2 * bar));
  201. const auto before = s;
  202. CHECK (patternblocks::toClips (s) == 3);
  203. CHECK (s.blocks.empty() && s.patternBlocksOn); // the option stays
  204. int onTrack2 = 0, onTrack3 = 0;
  205. for (const auto& clip : s.playlist)
  206. {
  207. if (clip.track == 2)
  208. {
  209. ++onTrack2;
  210. CHECK (clip.pattern == 0 && clip.length == bar);
  211. }
  212. if (clip.track == 3)
  213. {
  214. ++onTrack3;
  215. CHECK (clip.pattern == 1 && clip.start == 2 * bar && clip.length == 2 * bar);
  216. }
  217. }
  218. CHECK (onTrack2 == 2 && onTrack3 == 1);
  219. CHECK (s.playlistTracks.size() >= 4 && s.playlistTracks[2].name == "P1" && s.playlistTracks[3].name == "P2");
  220. CHECK (s.playlistTracks[2].lockedToContent && s.playlistTracks[3].lockedToContent);
  221. // it sounds the same
  222. auto a = before, b = s;
  223. patternblocks::expandSong (a);
  224. patternblocks::expandSong (b);
  225. CHECK (pb170f::noteOns (a, 16 * 24000) == pb170f::noteOns (b, 16 * 24000));
  226. CHECK (std::string (describeChange (before, s).text) == "Turn pattern blocks into clips");
  227. // the option going off with blocks in two arrangements: each arrangement's blocks become its own clips
  228. auto two = pb170f::blockSong();
  229. CHECK (patternblocks::place (two, 0, 0));
  230. two.addArrangement ("B");
  231. CHECK (patternblocks::place (two, 1, bar));
  232. CHECK (patternblocks::toClipsInEveryArrangement (two) == 2);
  233. CHECK (two.currentArrangement == 1 && two.blocks.empty() && two.playlist.size() == 1 && two.playlist[0].pattern == 1);
  234. two.selectArrangement (0);
  235. CHECK (two.blocks.empty() && two.playlist.size() == 1 && two.playlist[0].pattern == 0 && two.playlist[0].track == 0);
  236. }
  237. TEST (pb170f_export_renders_blocks)
  238. {
  239. using namespace lynx;
  240. using pb170f::bar;
  241. auto s = pb170f::blockSong();
  242. CHECK (patternblocks::place (s, 1, bar));
  243. std::int64_t music = 0;
  244. const auto out = makeExportSong (s, true, 0, music);
  245. CHECK (music == 3 * bar); // the whole song: up to the block's end
  246. CHECK (out.playlist.size() == 1 && out.playlist[0].pattern == 1 && out.playlist[0].start == bar && out.blocks.empty());
  247. // the MIDI export and the stems see them too
  248. const auto notes = exportopts::midiExportNotes (s);
  249. CHECK (notes.size() == 1 && notes.size() == 1 && notes[0].start == bar);
  250. CHECK (exportopts::stemChannels (s, exportopts::Range::song) == std::vector<int> { 0 });
  251. }
  252. TEST (pb170f_blocks_undo_labels_and_arrangement_copies)
  253. {
  254. using namespace lynx;
  255. using pb170f::bar;
  256. auto s = pb170f::blockSong();
  257. s.patterns[2].toggleStep (0, 0);
  258. const auto empty = s;
  259. CHECK (patternblocks::paintRange (s, 1, 0, 3 * bar) == 2);
  260. CHECK (std::string (describeChange (empty, s).text) == "Paint pattern blocks");
  261. auto erased = s;
  262. CHECK (patternblocks::erase (erased, 1, 0));
  263. CHECK (std::string (describeChange (s, erased).text) == "Erase pattern blocks");
  264. auto off = s;
  265. off.patternBlocksOn = false;
  266. CHECK (std::string (describeChange (s, off).text) == "Pattern blocks");
  267. s.addArrangement ("Second", true); // a copy of the current arrangement holds the blocks too
  268. CHECK (s.blocks.size() == 2);
  269. CHECK (patternblocks::place (s, 2, 5 * bar));
  270. s.selectArrangement (0);
  271. // the option and every arrangement's blocks are saved in .lynx (LYNX.exe --selftest: pattern_blocks=OK)
  272. CHECK (s.arrangements[1].blocks.size() == 3);
  273. }
  274. TEST (pb170f_key_map_actions)
  275. {
  276. using namespace lynx;
  277. CHECK (keys::findAction ("playlist.patternBlocks") != nullptr);
  278. CHECK (keys::findAction ("playlist.blocksToClips") != nullptr);
  279. keys::KeyMap map;
  280. CHECK (map.conflicts().empty());
  281. }
  282. TEST (pb170f_move_block_within_its_row)
  283. {
  284. using namespace lynx;
  285. using pb170f::bar;
  286. auto s = pb170f::blockSong();
  287. CHECK (patternblocks::place (s, 0, bar)); // P1 on bars 2 and 4
  288. CHECK (patternblocks::place (s, 0, 3 * bar));
  289. // dragging the bar-2 block to bar 3 moves it there, keeping the row and the order
  290. CHECK (patternblocks::move (s, 0, bar, 2 * bar) == 2 * bar);
  291. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 2 * bar && s.blocks[1].start == 3 * bar);
  292. CHECK (s.blocks[0].pattern == 0 && s.blocks[1].pattern == 0);
  293. // the same bar: a no-op returning the current start
  294. CHECK (patternblocks::move (s, 0, 2 * bar, 2 * bar + 10) == 2 * bar);
  295. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 2 * bar && s.blocks[1].start == 3 * bar);
  296. // the source tick may be anywhere inside the block's played span, not just its start
  297. CHECK (patternblocks::move (s, 0, 2 * bar + 10, 2 * bar + 500) == 2 * bar);
  298. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 2 * bar && s.blocks[1].start == 3 * bar);
  299. // onto the occupied bar: it stays where it was
  300. CHECK (patternblocks::move (s, 0, 2 * bar, 3 * bar) == 2 * bar);
  301. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 2 * bar && s.blocks[1].start == 3 * bar);
  302. // no block of that row there: -1, and nothing changes
  303. CHECK (patternblocks::move (s, 0, 0, bar) == -1);
  304. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 2 * bar && s.blocks[1].start == 3 * bar);
  305. // not a row at all: -1 as well
  306. CHECK (patternblocks::move (s, 7, 2 * bar, bar) == -1);
  307. CHECK (patternblocks::move (s, -1, 2 * bar, bar) == -1);
  308. // a negative target clamps to bar 1, which is free here
  309. CHECK (patternblocks::move (s, 0, 2 * bar, -500) == 0);
  310. CHECK (s.blocks.size() == 2 && s.blocks[0].start == 0 && s.blocks[1].start == 3 * bar);
  311. }
  312. TEST (pb170f_move_cut_blocks_and_order)
  313. {
  314. using namespace lynx;
  315. using pb170f::bar;
  316. auto s = pb170f::blockSong();
  317. CHECK (patternblocks::place (s, 0, 0)); // P1 on bars 1 and 2
  318. CHECK (patternblocks::place (s, 0, bar));
  319. CHECK (patternblocks::move (s, 0, 0, 2 * bar) == 2 * bar);
  320. CHECK (s.blocks.size() == 2 && s.blocks[0].start == bar && s.blocks[1].start == 2 * bar);
  321. CHECK (patternblocks::playedEnd (s, s.blocks, 0) == 2 * bar); // the block left behind still plays its whole bar
  322. CHECK (patternblocks::playedEnd (s, s.blocks, 1) == 3 * bar);
  323. // a grown 2-bar pattern on bars 1 and 3, grown to 3 bars: the first is cut at the second
  324. auto grown = pb170f::blockSong();
  325. CHECK (patternblocks::place (grown, 1, 0));
  326. CHECK (patternblocks::place (grown, 1, 2 * bar));
  327. grown.patterns[1].lengthTicks = 12 * ppq; // 3 bars
  328. CHECK (patternblocks::playedEnd (grown, grown.blocks, 0) == 2 * bar);
  329. CHECK (patternblocks::move (grown, 1, 0, 5 * bar) == 5 * bar); // to bar 6, past the other block's span
  330. CHECK (grown.blocks.size() == 2 && grown.blocks[0].start == 2 * bar && grown.blocks[1].start == 5 * bar);
  331. CHECK (patternblocks::playedEnd (grown, grown.blocks, 0) == 5 * bar); // whole again: nothing cuts it now
  332. CHECK (patternblocks::playedEnd (grown, grown.blocks, 1) == 8 * bar);
  333. }
  334. TEST (pb170f_erase_any_row)
  335. {
  336. using namespace lynx;
  337. using pb170f::bar;
  338. auto s = pb170f::blockSong();
  339. CHECK (patternblocks::place (s, 0, bar)); // P1 on bar 2, P2 on bars 3–4
  340. CHECK (patternblocks::place (s, 1, 2 * bar));
  341. // a click-width range on bar 3 meets only P2's span (P1 ends where bar 3 starts)
  342. CHECK (patternblocks::eraseAnyRow (s, 2 * bar, 2 * bar) == 1);
  343. CHECK (s.blocks.size() == 1 && s.blocks[0].pattern == 0 && s.blocks[0].start == bar);
  344. // the row-filtered erase removes the same block from the same setup
  345. auto row = pb170f::blockSong();
  346. CHECK (patternblocks::place (row, 0, bar));
  347. CHECK (patternblocks::place (row, 1, 2 * bar));
  348. CHECK (patternblocks::eraseRange (row, 1, 2 * bar, 2 * bar) == 1);
  349. CHECK (row.blocks.size() == 1 && row.blocks[0].pattern == 0);
  350. // a reversed range erases the same intersecting blocks, on every row (the range is swapped first)
  351. auto rev = pb170f::blockSong();
  352. CHECK (patternblocks::place (rev, 0, bar)); // P1 on [bar, 2*bar), P2 on [2*bar, 4*bar)
  353. CHECK (patternblocks::place (rev, 1, 2 * bar));
  354. CHECK (patternblocks::eraseAnyRow (rev, 2 * bar, bar) == 2);
  355. CHECK (rev.blocks.empty());
  356. // and the row-filtered erase swaps too
  357. auto revRow = pb170f::blockSong();
  358. CHECK (patternblocks::place (revRow, 0, bar));
  359. CHECK (patternblocks::place (revRow, 1, 2 * bar));
  360. CHECK (patternblocks::eraseRange (revRow, 0, 2 * bar, bar) == 1);
  361. CHECK (revRow.blocks.size() == 1 && revRow.blocks[0].pattern == 1);
  362. // a negative row erases nothing (it is not "any row": that is eraseAnyRow's job)
  363. CHECK (patternblocks::eraseRange (revRow, -1, 0, 4 * bar) == 0);
  364. CHECK (revRow.blocks.size() == 1 && revRow.blocks[0].pattern == 1);
  365. // a range over every row takes them all, whatever the row
  366. auto all = pb170f::blockSong();
  367. CHECK (patternblocks::place (all, 0, 0));
  368. CHECK (patternblocks::place (all, 1, bar));
  369. CHECK (patternblocks::place (all, 2, 2 * bar));
  370. CHECK (patternblocks::eraseAnyRow (all, 0, 3 * bar) == 3);
  371. CHECK (all.blocks.empty());
  372. // a range over a gap removes nothing and leaves the list bit-identical
  373. auto gap = pb170f::blockSong();
  374. CHECK (patternblocks::place (gap, 0, bar));
  375. const auto kept = gap.blocks;
  376. CHECK (patternblocks::eraseAnyRow (gap, 5 * bar, 6 * bar) == 0);
  377. CHECK (gap.blocks == kept);
  378. // a click inside a block erases it, but exactly at its played end there is nothing to erase
  379. // (a block meets [from, to] while from < playedEnd: the end itself belongs to the next bar)
  380. auto edge = pb170f::blockSong();
  381. CHECK (patternblocks::place (edge, 0, 0)); // P1 on [0, bar)
  382. CHECK (patternblocks::eraseAnyRow (edge, bar, bar) == 0);
  383. CHECK (edge.blocks.size() == 1);
  384. CHECK (patternblocks::eraseAnyRow (edge, bar / 2, bar / 2) == 1);
  385. CHECK (edge.blocks.empty());
  386. }
  387. TEST (pb170f_move_erase_labels)
  388. {
  389. using namespace lynx;
  390. using pb170f::bar;
  391. auto s = pb170f::blockSong();
  392. CHECK (patternblocks::place (s, 0, bar));
  393. CHECK (patternblocks::place (s, 0, 3 * bar));
  394. const auto before = s;
  395. CHECK (patternblocks::move (s, 0, bar, 2 * bar) == 2 * bar);
  396. CHECK (std::string (describeChange (before, s).text) == "Edit pattern blocks"); // same count: an edit, not a paint
  397. const auto moved = s;
  398. CHECK (patternblocks::eraseAnyRow (s, 2 * bar, 3 * bar) == 2);
  399. CHECK (std::string (describeChange (moved, s).text) == "Erase pattern blocks");
  400. }
  401. // Our follow-up to PR #132 (issue #127): what the view's gestures use.
  402. TEST (pb170f_erase_takes_only_the_row_under_the_mouse)
  403. {
  404. using namespace lynx;
  405. using pb170f::bar;
  406. auto s = pb170f::blockSong();
  407. CHECK (patternblocks::place (s, 0, 2 * bar)); // P1 and P3 on bar 3, P2 over bars 3-4
  408. CHECK (patternblocks::place (s, 1, 2 * bar));
  409. CHECK (patternblocks::place (s, 2, 2 * bar));
  410. // a right-click on P1's block: the view erases row 0 only, the other rows keep their blocks on that bar
  411. CHECK (patternblocks::anyInRange (s, 0, 2 * bar + 5, 2 * bar + 5));
  412. CHECK (patternblocks::eraseRange (s, 0, 2 * bar + 5, 2 * bar + 5) == 1);
  413. CHECK (s.blocks.size() == 2 && s.blocks[0].pattern == 1 && s.blocks[1].pattern == 2);
  414. // the drag goes down a row: now P2's block goes, P3's stays
  415. CHECK (patternblocks::anyInRange (s, 1, 2 * bar + 5, 2 * bar + 9));
  416. CHECK (patternblocks::eraseRange (s, 1, 2 * bar + 5, 2 * bar + 9) == 1);
  417. CHECK (s.blocks.size() == 1 && s.blocks[0].pattern == 2);
  418. // nothing to erase: an empty row, a span beside the block, either order, no row
  419. CHECK (! patternblocks::anyInRange (s, 0, 0, 8 * bar));
  420. CHECK (! patternblocks::anyInRange (s, 2, 0, 2 * bar - 1));
  421. CHECK (patternblocks::anyInRange (s, 2, 3 * bar - 1, 0));
  422. CHECK (! patternblocks::anyInRange (s, 2, 3 * bar, 4 * bar)); // its played end belongs to the next bar
  423. CHECK (! patternblocks::anyInRange (s, -1, 0, 8 * bar));
  424. CHECK (! patternblocks::anyInRange (s, 9, 0, 8 * bar));
  425. }
  426. TEST (pb170f_move_keeps_the_grabbed_bar_under_the_mouse)
  427. {
  428. using namespace lynx;
  429. using pb170f::bar;
  430. auto s = pb170f::blockSong();
  431. CHECK (patternblocks::place (s, 1, 2 * bar)); // the 2-bar P2 over bars 3-4
  432. // grabbed on its first bar: no offset; on its second bar: one bar; beside it: none
  433. CHECK (patternblocks::grabOffset (s, 1, 2 * bar + 10) == 0);
  434. CHECK (patternblocks::grabOffset (s, 1, 3 * bar + 10) == bar);
  435. CHECK (patternblocks::grabOffset (s, 1, 4 * bar) == 0);
  436. CHECK (patternblocks::grabOffset (s, 0, 3 * bar + 10) == 0);
  437. // pressed on the second bar: a move inside that bar leaves the block where it is (it used to jump a bar)
  438. const auto offset = patternblocks::grabOffset (s, 1, 3 * bar + 10);
  439. CHECK (patternblocks::dragTarget (s, 3 * bar + 700, offset) == 2 * bar);
  440. // one bar to the right, the block follows by one bar; to the left the same; never before bar 1
  441. CHECK (patternblocks::dragTarget (s, 4 * bar + 1, offset) == 3 * bar);
  442. CHECK (patternblocks::move (s, 1, 2 * bar, patternblocks::dragTarget (s, 4 * bar + 1, offset)) == 3 * bar);
  443. CHECK (s.blocks.size() == 1 && s.blocks[0].start == 3 * bar);
  444. CHECK (patternblocks::dragTarget (s, 2 * bar + 1, offset) == bar);
  445. CHECK (patternblocks::dragTarget (s, 5, offset) == 0);
  446. CHECK (patternblocks::dragTarget (s, -900, offset) == 0);
  447. // with no offset the target is the bar under the mouse, as for a block placed by the pencil
  448. CHECK (patternblocks::dragTarget (s, 6 * bar + 3, 0) == 6 * bar);
  449. }