From 704ff9b3d18be947bdb9ef312279eda9cc02cbce Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 6 Apr 2017 13:08:18 +0000 Subject: [PATCH] Delete characters with backspace --- chunks.py | 4 +++- client.py | 1 + maps.py | 29 ++++++++++++++++------------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/chunks.py b/chunks.py index 0154a66..4d4763a 100644 --- a/chunks.py +++ b/chunks.py @@ -43,7 +43,9 @@ class ChunkDiff(): self._chars[x+y*CHUNK_WIDTH] = character def delete(self, x, y): - del self._chars[x+y*CHUNK_WIDTH] + pos = x + y*CHUNK_WIDTH + if pos in self._chars: + del self._chars[x+y*CHUNK_WIDTH] def apply(self, diff): for i, c in diff._chars.items(): diff --git a/client.py b/client.py index 85b6d5a..18cb5e2 100644 --- a/client.py +++ b/client.py @@ -64,6 +64,7 @@ class Client(): # edit world elif i in string.digits + string.ascii_letters + string.punctuation + " ": self.map_.write(i) + elif i == "\x7f": self.map_.delete() else: sys.stdout.write(repr(i) + "\n") diff --git a/maps.py b/maps.py index 4a2970b..2e6dd1e 100644 --- a/maps.py +++ b/maps.py @@ -14,12 +14,12 @@ class Map(): def __init__(self, width, height, chunkpool, drawevent): self._lock = threading.RLock() - self.worldx = 0 - self.worldy = 0 - self.cursorx = 0 - self.cursory = 0 self.chunkpreload = 0 self.cursorpadding = 2 + self.worldx = 0 + self.worldy = 0 + self.cursorx = self.cursorpadding + self.cursory = self.cursorpadding self.chunkpool = chunkpool self.drawevent = drawevent @@ -67,6 +67,12 @@ class Map(): s = "."*CHUNK_WIDTH self._pad.addstr(y+dy, x, s) + def load_visible(self): + with self.chunkpool as pool: + pool.load_list(self.visible_chunk_coords()) + + self.drawevent.set() + def resize(self, width, height): self.width = width self.height = height @@ -76,18 +82,15 @@ class Map(): (chunkx(width) + 2)*CHUNK_WIDTH + 1 # workaround for addwstr issue when drawing ) - with self.chunkpool as pool: - pool.load_list(self.visible_chunk_coords()) - - self.drawevent.set() + self.load_visible() def visible_chunk_coords(self): coords = [] xstart = chunkx(self.worldx) - self.chunkpreload ystart = chunky(self.worldy) - self.chunkpreload - xend = xstart + chunkx(self.width) + 1 + 2*self.chunkpreload - yend = ystart + chunky(self.height) + 1 + 2*self.chunkpreload + xend = xstart + chunkx(self.width)+2 + 2*self.chunkpreload + yend = ystart + chunky(self.height)+2 + 2*self.chunkpreload for x in range(xstart, xend): for y in range(ystart, yend): @@ -109,7 +112,7 @@ class Map(): with self.chunkpool as pool: chunk = pool.get(Position(chunkx(self.cursorx-1), chunky(self.cursory))) if chunk: - chunk.delete(inchunkx(self.cursorx-1), inchunky(self.cursory), char) + chunk.delete(inchunkx(self.cursorx-1), inchunky(self.cursory)) self.move_cursor(-1, 0) @@ -133,7 +136,7 @@ class Map(): ) ) - self.drawevent.set() + self.load_visible() def scroll(self, dx, dy): self.worldx += dx @@ -161,7 +164,7 @@ class Map(): #) #) - self.drawevent.set() + self.load_visible() class ChunkMap():