Skip to content

Commit

Permalink
wraparound works in nudge mode
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnovak committed Mar 2, 2024
1 parent c269d02 commit 4c434c8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 79 deletions.
27 changes: 21 additions & 6 deletions src/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ proc pasteSelection*(map; loc, undoLoc: Location, sb: SelectionBuffer,
let destRect = destRect.get
var loc = Location(level: loc.level)

# TODO
# - BUG: move selection severs links (but nudge works)
# - add support for wraparound in paste & move mode

# Erase existing map links in the paste area (taking selection into
# account)
for r in destRect.r1..<destRect.r2:
Expand Down Expand Up @@ -743,7 +747,7 @@ proc resizeLevel*(map; loc: Location, newRows, newCols: Natural,
colOffs = destCol.int - copyRect.c1

newLinks = oldLinks.shiftLinksInLevel(loc.level, rowOffs, colOffs,
newLevelRect)
newLevelRect, wraparound=false)

let action = proc (m: var Map): UndoStateData =
alias(l, m.levels[loc.level])
Expand All @@ -761,7 +765,7 @@ proc resizeLevel*(map; loc: Location, newRows, newCols: Natural,
for src in oldLinks.sources: m.links.delBySrc(src)
m.links.addAll(newLinks)

# Adjut regions
# Adjust regions
let (regionOffsRow, regionOffsCol) = calcRegionResizeOffsets(
m, loc.level, newRows, newCols, anchor
)
Expand Down Expand Up @@ -813,7 +817,7 @@ proc cropLevel*(map; loc: Location, cropRect: Rect[Natural]; um): Location =
colOffs = -cropRect.c1

newLinks = oldLinks.shiftLinksInLevel(loc.level, rowOffs, colOffs,
newLevelRect)
newLevelRect, wraparound=false)

let action = proc (m: var Map): UndoStateData =
m.levels[loc.level] = m.newLevelFrom(loc.level, cropRect)
Expand Down Expand Up @@ -850,7 +854,7 @@ proc cropLevel*(map; loc: Location, cropRect: Rect[Natural]; um): Location =
# }}}
# {{{ nudgeLevel*()
proc nudgeLevel*(map; loc: Location, rowOffs, colOffs: int,
sb: SelectionBuffer; um): Location =
sb: SelectionBuffer, wraparound: bool; um): Location =

let usd = UndoStateData(
actionName: "Nudge level", location: loc, undoLocation: loc
Expand All @@ -860,7 +864,7 @@ proc nudgeLevel*(map; loc: Location, rowOffs, colOffs: int,

let oldLinks = map.links.filterByLevel(loc.level)
let newLinks = oldLinks.shiftLinksInLevel(loc.level, rowOffs, colOffs,
levelRect)
levelRect, wraparound)

# Do action
#
Expand All @@ -875,7 +879,18 @@ proc nudgeLevel*(map; loc: Location, rowOffs, colOffs: int,
sb.level.notes,
initRegions=false
)
discard l.paste(rowOffs, colOffs, sb.level, sb.selection, pasteTrail=true)

if wraparound:
discard l.pasteWithWraparound(destRow=rowOffs, destCol=colOffs,
srcLevel=sb.level, sb.selection,
pasteTrail=true,
levelRows=sb.level.rows,
levelCols=sb.level.cols,
selStartRow=rowOffs,
selStartCol=colOffs)
else:
discard l.paste(rowOffs, colOffs, sb.level, sb.selection,
pasteTrail=true)

m.levels[loc.level] = l

Expand Down
8 changes: 3 additions & 5 deletions src/annotations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ proc newAnnotations*(rows, cols: Natural): Annotations =

# {{{ coordsToKey()
template coordsToKey(a; r,c: Natural): Natural =
let h = a.rows
let w = a.cols
assert r < h
assert c < w
r*w + c
assert r < a.rows
assert c < a.cols
(r * a.cols) + c

# }}}
# {{{ keyToCoords()
Expand Down
9 changes: 5 additions & 4 deletions src/drawlevel.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2691,10 +2691,11 @@ proc mergeSelectionAndOutlineBuffers(level, viewBuf: Level,
srcLevel=selBufLevel, sel=dp.selectionBuffer.get.selection,
levelRows=level.rows, levelCols=level.cols,
selStartRow=dp.selStartRow, selStartCol=dp.selStartCol,
destBufStartRow=dp.viewStartRow,
destBufStartCol=dp.viewStartCol,
destBufRowOffset=ViewBufBorder,
destBufColOffset=ViewBufBorder

destStartRow=dp.viewStartRow,
destStartCol=dp.viewStartCol,
destRowOffset=ViewBufBorder,
destColOffset=ViewBufBorder
)

if outlineBuf.isSome:
Expand Down
64 changes: 17 additions & 47 deletions src/level.nim
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ proc copyCell(destLevel: Level, destRow, destCol: Natural,
srcLevel.annotations.getAnnotation(srcRow, srcCol).get
)

# }}}
# {{{ paste*()

proc paste*(l; destRow, destCol: int, srcLevel: Level, sel: Selection,
pasteTrail: bool = false): Option[Rect[Natural]] =

Expand Down Expand Up @@ -377,49 +377,22 @@ proc paste*(l; destRow, destCol: int, srcLevel: Level, sel: Selection,

# }}}
# {{{ pasteWithWraparound*()

proc pasteWithWraparound*(l; destRow, destCol: int, srcLevel: Level,
sel: Selection, pasteTrail: bool = false,
levelRows, levelCols: Natural,
selStartRow, selStartCol: int,
destBufStartRow, destBufStartCol: Natural,
destBufRowOffset, destBufColOffset: Natural): Option[Rect[Natural]] =

echo "*** 2"
echo "---------------------------------"
echo fmt"destLevel: {l.rows} x {l.cols}, destRow: {destRow}, destCol: {destCol}"
echo fmt"srcLevel: {srcLevel.rows} x {srcLevel.cols}, selection: {sel.rows} x {sel.cols}"
echo fmt"levelRows: {levelRows}, levelCols: {levelCols}"
echo fmt"selStartRow: {selStartRow}, selStartCol: {selStartCol}"
echo fmt"destBufStartRow: {destBufStartRow}, destBufStartCol: {destBufStartCol}"
echo fmt"destBufRowOffset: {destBufRowOffset}, destBufColOffset: {destBufColOffset}"

#[ var dr = destRow
if dr < destRect.r1: inc(dr, destRect.rows)
var dc = destCol
if dc < destRect.c1: inc(dc, destRect.cols)
assert destRect.contains(dr,dc)
result = rectN(0, 0, l.cols, l.rows).some
# echo fmt"dr: {dr}, dc: {dc}"
for srcRow in 0..<srcLevel.rows:
for srcCol in 0..<srcLevel.cols:
copyCell(dr, dc, srcRow, srcCol)
inc(dc)
if dc == destRect.c2: dc = destRect.c1
dc = destCol
if dc < destRect.c1: inc(dc, destRect.cols)
inc(dr)
if dr == destRect.r2: dr = destRect.r1
]#

destStartRow: Natural = 0,
destStartCol: Natural = 0,
destRowOffset: Natural = 0,
destColOffset: Natural = 0): Option[Rect[Natural]] =

# echo "---------------------------------"
# echo fmt"destLevel: {l.rows} x {l.cols}, destRow: {destRow}, destCol: {destCol}"
# echo fmt"srcLevel: {srcLevel.rows} x {srcLevel.cols}, selection: {sel.rows} x {sel.cols}"
# echo fmt"levelRows: {levelRows}, levelCols: {levelCols}"
# echo fmt"selStartRow: {selStartRow}, selStartCol: {selStartCol}"
# echo fmt"destStartRow: {destStartRow}, destBufStartCol: {destBufStartCol}"
# echo fmt"destRowOffset: {destRowOffset}, destBufColOffset: {destBufColOffset}"

for srcRow in 0..<srcLevel.rows:
for srcCol in 0..<srcLevel.cols:
Expand All @@ -428,14 +401,11 @@ proc pasteWithWraparound*(l; destRow, destCol: int, srcLevel: Level,
wrappedRow = (selStartRow + srcRow).floorMod(levelRows)
wrappedCol = (selStartCol + srcCol).floorMod(levelCols)

dr = wrappedRow.int + destBufRowOffset - destBufStartRow
dc = wrappedCol.int + destBufColOffset - destBufStartCol

echo fmt" wrapped: {wrappedRow},{wrappedCol}, dr: {dr}, dc: {dc}"
dr = wrappedRow.int + destRowOffset - destStartRow
dc = wrappedCol.int + destColOffset - destStartCol

# if dr >= (destBufStartRow + destBufRowOffset) and
# dc >= (destBufStartCol + destBufColOffset):
if dr >= 0 and dc >= 0:
if wrappedRow >= destStartRow and dr < l.rows and
wrappedCol >= destStartCol and dc < l.cols:

copyCell(destLevel=l, destRow=dr, destCol=dc,
srcLevel, srcRow, srcCol, pasteTrail)
Expand Down
29 changes: 20 additions & 9 deletions src/links.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import std/math
import std/options
import std/sets
import std/tables
Expand Down Expand Up @@ -198,7 +199,7 @@ proc remapLevelIndex*(vl; oldIndex, newIndex: Natural) =
# }}}
# {{{ shiftLinksInLevel*()
proc shiftLinksInLevel*(l; level: Natural, rowOffs, colOffs: int,
levelRect: Rect[int]): Links =
levelRect: Rect[int], wraparound: bool): Links =
result = initLinks()

for src, dest in l:
Expand All @@ -208,20 +209,30 @@ proc shiftLinksInLevel*(l; level: Natural, rowOffs, colOffs: int,
if src.level == level:
var r = src.row.int + rowOffs
var c = src.col.int + colOffs
if levelRect.contains(r,c):
src.row = r
src.col = c

if wraparound:
src.row = r.floorMod(levelRect.rows)
src.col = c.floorMod(levelRect.cols)
else:
continue
if levelRect.contains(r,c):
src.row = r
src.col = c
else:
continue

if dest.level == level:
var r = dest.row.int + rowOffs
var c = dest.col.int + colOffs
if levelRect.contains(r,c):
dest.row = r
dest.col = c

if wraparound:
dest.row = r.floorMod(levelRect.rows)
dest.col = c.floorMod(levelRect.cols)
else:
continue
if levelRect.contains(r,c):
dest.row = r
dest.col = c
else:
continue

result.set(src, dest)

Expand Down
11 changes: 3 additions & 8 deletions src/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1659,9 +1659,6 @@ proc copySelection(buf: var Option[SelectionBuffer]; a): Option[Rect[Natural]] =
if bbox.isSome:
let bbox = bbox.get

echo bbox
echo bbox.w, bbox.h

buf = some(SelectionBuffer(
selection: newSelectionFrom(sel, bbox),
level: newLevelFrom(currLevel(a), bbox)
Expand Down Expand Up @@ -6135,10 +6132,8 @@ proc handleGlobalKeyEvents(a) =
let sel = newSelection(l.rows, l.cols)
sel.fill(true)

# For the duration of the nudge operation, we move the reference
# of the current level to the nudge buffer. The current level then
# gets manipulated via the nudge buffer then the reference gets moved
# back at the end of the nudge operation.
# The level is cleared for the duration of the nudge operation and it
# is stored temporarily in the SelectionBuffer.
ui.nudgeBuf = SelectionBuffer(level: l, selection: sel).some

map.levels[cur.level] = newLevel(
Expand Down Expand Up @@ -6766,7 +6761,7 @@ proc handleGlobalKeyEvents(a) =
elif ke.isShortcutDown(scAccept):
let newCur = actions.nudgeLevel(map, cur,
dp.selStartRow, dp.selStartCol,
ui.nudgeBuf.get, um)
ui.nudgeBuf.get, wraparound=true, um)
moveCursorTo(newCur, a)
ui.editMode = emNormal
setStatusMessage(IconArrowsAll, "Nudged map", a)
Expand Down

0 comments on commit 4c434c8

Please sign in to comment.