Skip to content

Commit

Permalink
Notes list basic region support & general refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnovak committed Apr 3, 2024
1 parent 07dff71 commit 696e5fa
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 180 deletions.
4 changes: 2 additions & 2 deletions src/annotations.nim
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ proc getNote*(a; r,c: Natural): Option[Annotation] =
if a.get.isNote: result = a

# }}}
# {{{ allNotes*()
iterator allNotes*(a): tuple[row, col: Natural, annotation: Annotation] =
# {{{ notes*()
iterator notes*(a): tuple[row, col: Natural, annotation: Annotation] =
for k, annot in a.annotations:
if annot.isNote:
let (r,c) = a.keyToCoords(k)
Expand Down
7 changes: 2 additions & 5 deletions src/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,15 @@ type

Regions* = object
regionsByCoords*: OrderedTable[RegionCoords, Region]
# sortedRegionIds*: seq[Natural]
# sortedRegionNames*: seq[string]
sortedRegionIds*: seq[Natural]
sortedRegionNames*: seq[string]


# The top-left region has region coordinate (0,0)
RegionCoords* = object
row*, col*: Natural

Region* = object
# Internal ID, never written to disk
id*: Natural

name*: string
notes*: string

Expand Down
1 change: 0 additions & 1 deletion src/converters.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
converter toCfloat*(x: SomeInteger): cfloat = x.cfloat
converter tcFloat*(x: SomeInteger): float = x.float

7 changes: 4 additions & 3 deletions src/level.nim
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ proc getNote*(l; r,c: Natural): Option[Annotation] =
# }}}
# {{{ allNotes*()
template allNotes*(l): tuple[row, col: Natural, annotation: Annotation] =
l.annotations.allNotes()
l.annotations.notes()

# }}}
# {{{ reindexNotes*()
Expand Down Expand Up @@ -212,8 +212,9 @@ proc initRegionsFrom*(srcLevel: Option[Level] = Level.none, destLevel: Level,
let srcRegion = if srcLevel.isNone or srcRegionRow < 0 or srcRegionCol < 0:
Region.none
else:
srcLevel.get.regions[RegionCoords(row: srcRegionRow,
col: srcRegionCol)]
let rc = RegionCoords(row: srcRegionRow,
col: srcRegionCol)
srcLevel.get.regions[rc]

if srcRegion.isSome and not srcRegion.get.isUntitledRegion():
destRegions[destRegionCoord] = srcRegion.get
Expand Down
119 changes: 73 additions & 46 deletions src/main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,22 @@ type
grouping: int
cache: seq[NotesListCacheEntry]
sectionStates: Table[int, bool]
regionStates: Table[int, bool]
regionStates: Table[RegionCoords, bool]
activeId: Option[ItemId]

NotesListCacheEntryKind = enum
nckNote, nckLevel, nckRegion

NotesListCacheEntry = object
id: ItemId
location: Location
height: float
case kind*: NotesListCacheEntryKind
of nckNote:
id: ItemId
location: Location
height: float
of nckLevel:
levelId: Natural
of nckRegion:
regionCoords: RegionCoords

NotesListFilter = object
scope: NoteScopeFilter
Expand Down Expand Up @@ -8364,13 +8373,17 @@ proc renderNotesListPane(x, y, w, h: float; a) =
style = a.theme.buttonStyle):
for id in nls.sectionStates.keys:
nls.sectionStates[id] = true
nls.regionStates[id] = true

for rc in nls.regionStates.keys:
nls.regionStates[rc] = true

if koi.button(wx+244, wy, w=24, wh, IconMinusSmall, tooltip = "Collapse all",
style = a.theme.buttonStyle):
for id in nls.sectionStates.keys:
nls.sectionStates[id] = false
nls.regionStates[id] = false

for rc in nls.regionStates.keys:
nls.regionStates[rc] = false


# Sort functions
Expand All @@ -8392,8 +8405,8 @@ proc renderNotesListPane(x, y, w, h: float; a) =

# Rebuild/reset caches if needed
const
NoteHorizOffs = -8
NoteVertPad = 18
NoteHorizOffs = -8
NoteVertPad = 18

let
markerX = LeftPad + NoteHorizOffs
Expand Down Expand Up @@ -8423,7 +8436,8 @@ proc renderNotesListPane(x, y, w, h: float; a) =
height = textBounds.y2 - textBounds.y1 + NoteVertPad

s.add(
NotesListCacheEntry(id: id, location: loc, height: height)
NotesListCacheEntry(kind: nckNote, id: id, location: loc,
height: height)
)

proc sortCacheEntries(s: var seq[NotesListCacheEntry],
Expand All @@ -8441,6 +8455,11 @@ proc renderNotesListPane(x, y, w, h: float; a) =
for _, l in map.levels:
if l.id notin nls.sectionStates:
nls.sectionStates[l.id] = true

if l.regionOpts.enabled:
for regionCoords, _ in l.regions.sortedRegions:
nls.regionStates[regionCoords] = true

map.levelsDirty = false

if l.annotations.dirty:
Expand All @@ -8459,31 +8478,46 @@ proc renderNotesListPane(x, y, w, h: float; a) =
for levelId in map.sortedLevelIds:
let level = map.levels[levelId]
var s = newSeq[NotesListCacheEntry]()

for r,c, note in level.allNotes:
s.maybeAddCacheEntry(
Location(levelId: levelId, row: r, col: c),
note, vg
)

sortCacheEntries(s, nls.currFilter.ordering)
nls.cache.add(s)
if s.len > 0:
nls.cache.add(NotesListCacheEntry(kind: nckLevel, levelId: levelId))

sortCacheEntries(s, nls.currFilter.ordering)
nls.cache.add(s)
# TODO regions

of nsfLevel:
for r,c, note in l.allNotes:
# if l.regionOpts.enabled:
# var s = newSeq[NotesListCacheEntry]()
# for region in l.regions
# else:
nls.cache.maybeAddCacheEntry(
Location(levelId: a.ui.cursor.levelId, row: r, col: c),
note, vg
)
let levelId = l.id

# if not l.regionOpts.enabled:
sortCacheEntries(nls.cache, nls.currFilter.ordering)
if l.regionOpts.enabled:
for regionCoords, region in l.regions.sortedRegions:
var s = newSeq[NotesListCacheEntry]()

# TODO regions
for loc, note in map.regionNotes(levelId, regionCoords):
s.maybeAddCacheEntry(
Location(levelId: levelId, row: loc.row, col: loc.col),
note, vg
)
if s.len > 0:
nls.cache.add(NotesListCacheEntry(kind: nckRegion,
regionCoords: regionCoords))

sortCacheEntries(s, nls.currFilter.ordering)
nls.cache.add(s)

else:
for r,c, note in l.allNotes:
nls.cache.maybeAddCacheEntry(
Location(levelId: levelId, row: r, col: c),
note, vg
)
sortCacheEntries(nls.cache, nls.currFilter.ordering)

of nsfRegion:
# TODO
Expand Down Expand Up @@ -8512,16 +8546,6 @@ proc renderNotesListPane(x, y, w, h: float; a) =
# Render note buttons
setFont()

var
prevEntry: Option[NotesListCacheEntry]
addNote = false

let (levelSections, regionSections) =
case nls.currFilter.scope:
of nsfMap: (true, true)
of nsfLevel: (false, true)
of nsfRegion: (false, false)

# Sync current note to cursor
let currNote = map.getNote(ui.cursor)

Expand All @@ -8533,23 +8557,28 @@ proc renderNotesListPane(x, y, w, h: float; a) =

var startY, itemHeight: float

if syncToCursor and levelSections:
if syncToCursor and nls.currFilter.scope == nsfMap:
nls.sectionStates[l.id] = true

var addNote = true

for e in nls.cache:
let note = map.getNote(e.location).get
case e.kind
of nckLevel:
let l = map.levels[e.levelId]
addNote = koi.sectionHeader(l.getDetailedName(short=true),
nls.sectionStates[l.id])

if levelSections:
if prevEntry.isNone or
e.location.levelId != prevEntry.get.location.levelId:
of nckRegion:
let region = l.regions[e.regionCoords].get
addNote = koi.subsectionHeader(region.name,
nls.regionStates[e.regionCoords])

let l = map.levels[e.location.levelId]
addNote = koi.sectionHeader(l.getDetailedName(short=true),
nls.sectionStates[l.id])
else:
addNote = true
of nckNote:
if not addNote: continue

let note = map.getNote(e.location).get

if addNote:
const MinHeight = 32
let height = max(MinHeight, e.height)

Expand All @@ -8575,7 +8604,6 @@ proc renderNotesListPane(x, y, w, h: float; a) =
if syncToCursor and ui.cursor == e.location:
nls.activeId = e.id.some

prevEntry = e.some

koi.endScrollView()

Expand Down Expand Up @@ -10121,7 +10149,6 @@ proc initApp(configFile: Option[string], mapFile: Option[string],
with a.ui.notesListState:
currFilter.noteType = @[ntfNone, ntfNumber, ntfId, ntfIcon]
currFilter.ordering = noType
sectionStates = initTable[int, bool]()

loadFonts(a)
loadAndSetIcon(a)
Expand Down
Loading

0 comments on commit 696e5fa

Please sign in to comment.