Main Dwaddle repository
Revision | b8a632373b5d1191edb5c95e046d301f9c184d70 (tree) |
---|---|
Time | 2021-05-26 14:56:34 |
Author | ![]() |
Commiter | Remilia Scarlet |
Add #pred and #succ methods to Lump class. Also implement moving of
lumps in WadFile.
@@ -164,6 +164,32 @@ | ||
164 | 164 | @dataLoaded = false |
165 | 165 | end |
166 | 166 | |
167 | + def pred : Lump? | |
168 | + raise LumpError.new("Lump has no parent: #{self.name}") unless @parent | |
169 | + idx = @parent.not_nil!.indexOf(self) | |
170 | + if idx == 0 | |
171 | + nil | |
172 | + elsif idx >= 1 | |
173 | + @parent.not_nil!.[idx - 1] | |
174 | + else | |
175 | + raise "Index should not be negative for lump #{@name}" | |
176 | + end | |
177 | + end | |
178 | + | |
179 | + def succ : Lump? | |
180 | + raise LumpError.new("Lump has no parent: #{self.name}") unless @parent | |
181 | + idx = @parent.not_nil!.indexOf(self) | |
182 | + numLumps = @parent.not_nil!.size | |
183 | + | |
184 | + if idx == numLumps - 1 | |
185 | + nil | |
186 | + elsif idx >= 0 | |
187 | + @parent.not_nil!.[idx + 1] | |
188 | + else | |
189 | + raise "Invalid index for lump #{@name}" | |
190 | + end | |
191 | + end | |
192 | + | |
167 | 193 | protected def loadData() |
168 | 194 | @dataMut.synchronize do |
169 | 195 | if @parent.nil? |
@@ -379,6 +405,90 @@ | ||
379 | 405 | end |
380 | 406 | end |
381 | 407 | |
408 | + # Swaps the position of the lumps named `lump1` and `lump2`. Only one lump | |
409 | + # can be named `lump1`, and only one can be named `lump2`, otherwise a | |
410 | + # `MultipleLumpsError` is raised. | |
411 | + def swap(lump1 : String, lump2 : String) | |
412 | + lump1 = self.[lump1] | |
413 | + lump2 = self.[lump2] | |
414 | + swap(lump1, lump2) | |
415 | + end | |
416 | + | |
417 | + # Swaps the position of the lumps `lump1` and `lump2`. If either of these | |
418 | + # lumps is part part of this wad, this raises a `NoSuchLumpError`. | |
419 | + def swap(lump1 : Lump, lump2 : Lump) | |
420 | + idx1 = indexOf(lump1) | |
421 | + idx2 = indexOf(lump2) | |
422 | + raise NoSuchLumpError.new("#{lump1.name} is not part of the wad") if idx1 < 0 | |
423 | + raise NoSuchLumpError.new("#{lump2.name} is not part of the wad") if idx2 < 0 | |
424 | + @entries[idx1] = lump2 | |
425 | + @entries[idx2] = lump1 | |
426 | + end | |
427 | + | |
428 | + # Swaps the position of the lumps at indices `lump1` and `lump2`. If either | |
429 | + # of these indices are out of range,this raises an `IndexError`. | |
430 | + def swap(idx1 : Int, idx2 : Int) | |
431 | + raise IndexError.new("Index out of range: #{idx1}") if idx1 < 0 || idx1 >= self.size | |
432 | + raise IndexError.new("Index out of range: #{idx2}") if idx2 < 0 || idx2 >= self.size | |
433 | + lump1 = self.[idx1] | |
434 | + lump2 = self.[idx2] | |
435 | + @entries[idx2] = lump1 | |
436 | + @entries[idx1] = lump2 | |
437 | + end | |
438 | + | |
439 | + # Moves `lump` up in the wad by one index (that is, the index is decreased | |
440 | + # by one). If the lump is already at index 0, this does nothing. | |
441 | + def moveUp(lump : Lump) | |
442 | + lump.pred.try { |other| swap(lump, other) } | |
443 | + end | |
444 | + | |
445 | + # Moves `lump` down in the wad by one index (that is, the index is increased | |
446 | + # by one). If the lump is already at the end of the wad, this does nothing. | |
447 | + def moveDown(lump : Lump) | |
448 | + lump.succ.try { |other| swap(lump, other) } | |
449 | + end | |
450 | + | |
451 | + # Moves `lump` up in the wad by one index (that is, the index is decreased | |
452 | + # by one). If the lump is already at index 0, this does nothing. | |
453 | + def moveUp(lump : String) | |
454 | + self.[lump].pred.try { |other| swap(lump, other) } | |
455 | + end | |
456 | + | |
457 | + # Moves `lump` down in the wad by one index (that is, the index is increased | |
458 | + # by one). If the lump is already at the end of the wad, this does nothing. | |
459 | + def moveDown(lump : String) | |
460 | + self.[lump].succ.try { |other| swap(lump, other) } | |
461 | + end | |
462 | + | |
463 | + # Moves the lump at `idx` up in the wad by one index (that is, the index is | |
464 | + # decreased by one). If the lump is already at index 0, this does nothing. | |
465 | + def moveUp(idx : Int) | |
466 | + if idx == 0 | |
467 | + raise WadError.new("Cannot move lump at index 0 up") | |
468 | + elsif idx < self.size - 1 && idx > 0 | |
469 | + swap(idx, idx - 1) | |
470 | + else | |
471 | + raise IndexError.new("Index out of range for wad with #{self.size} lump #{self.size == 1 ? "" : "s"}: #{idx}") | |
472 | + end | |
473 | + end | |
474 | + | |
475 | + # Moves the lump at `idx` down in the wad by one index (that is, the index | |
476 | + # is increased by one). If the lump is already at the end of the wad, this | |
477 | + # does nothing. | |
478 | + def moveDown(idx : Int) | |
479 | + if idx == self.size = 1 | |
480 | + raise WadError.new("Cannot move lump at end of wad down") | |
481 | + elsif idx >= 0 && idx < self.size - 1 | |
482 | + swap(idx, idx + 1) | |
483 | + else | |
484 | + raise IndexError.new("Index out of range for wad with #{self.size} lump #{self.size == 1 ? "" : "s"}: #{idx}") | |
485 | + end | |
486 | + end | |
487 | + | |
488 | + def moveDown(lump : Lump) | |
489 | + lump.succ.try { |other| swap(lump, other) } | |
490 | + end | |
491 | + | |
382 | 492 | # Returns the lump with the given name. If no such lump exists, this raises |
383 | 493 | # a `NoSuchLumpError`. If more than one lump with the given name exists, |
384 | 494 | # this raises a `WadError`. |