Move down one line when enter is pressed, use ESC to quit
This commit is contained in:
parent
704ff9b3d1
commit
42d72422c6
3 changed files with 48 additions and 21 deletions
36
chunks.py
36
chunks.py
|
|
@ -10,13 +10,6 @@ class ChunkDiff():
|
||||||
Todo: Implement delete diff
|
Todo: Implement delete diff
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def combine(cls, diff_a, diff_b):
|
|
||||||
diff = cls()
|
|
||||||
diff.apply(diff_a)
|
|
||||||
diff.apply(diff_b)
|
|
||||||
return diff
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._chars = {}
|
self._chars = {}
|
||||||
|
|
||||||
|
|
@ -27,6 +20,12 @@ class ChunkDiff():
|
||||||
return diff
|
return diff
|
||||||
#self._chars = d.copy()
|
#self._chars = d.copy()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_string(cls, s):
|
||||||
|
diff = cls()
|
||||||
|
#for c in string
|
||||||
|
pass
|
||||||
|
|
||||||
def copy(self):
|
def copy(self):
|
||||||
return ChunkDiff.from_dict(self.to_dict().copy())
|
return ChunkDiff.from_dict(self.to_dict().copy())
|
||||||
|
|
||||||
|
|
@ -40,12 +39,14 @@ class ChunkDiff():
|
||||||
#return self._chars.copy()
|
#return self._chars.copy()
|
||||||
|
|
||||||
def set(self, x, y, character):
|
def set(self, x, y, character):
|
||||||
self._chars[x+y*CHUNK_WIDTH] = character
|
pos = x+y*CHUNK_WIDTH
|
||||||
|
self._chars[pos] = character
|
||||||
|
|
||||||
def delete(self, x, y):
|
def delete(self, x, y):
|
||||||
pos = x + y*CHUNK_WIDTH
|
self.set(x, y, " ")
|
||||||
if pos in self._chars:
|
|
||||||
del self._chars[x+y*CHUNK_WIDTH]
|
def clear_deletions(self):
|
||||||
|
self._chars = {i: v for i, v in self._chars.items() if v != " "}
|
||||||
|
|
||||||
def apply(self, diff):
|
def apply(self, diff):
|
||||||
for i, c in diff._chars.items():
|
for i, c in diff._chars.items():
|
||||||
|
|
@ -56,6 +57,9 @@ class ChunkDiff():
|
||||||
s = "".join(d.get(i, " ") for i in range(CHUNK_WIDTH*CHUNK_HEIGHT))
|
s = "".join(d.get(i, " ") for i in range(CHUNK_WIDTH*CHUNK_HEIGHT))
|
||||||
return [s[i:i+CHUNK_WIDTH] for i in range(0, CHUNK_WIDTH*CHUNK_HEIGHT, CHUNK_WIDTH)]
|
return [s[i:i+CHUNK_WIDTH] for i in range(0, CHUNK_WIDTH*CHUNK_HEIGHT, CHUNK_WIDTH)]
|
||||||
|
|
||||||
|
def empty(self):
|
||||||
|
return bool(self._chars)
|
||||||
|
|
||||||
|
|
||||||
class Chunk():
|
class Chunk():
|
||||||
"""
|
"""
|
||||||
|
|
@ -74,13 +78,17 @@ class Chunk():
|
||||||
|
|
||||||
def set(self, x, y, character):
|
def set(self, x, y, character):
|
||||||
self._modifications.set(x, y, character)
|
self._modifications.set(x, y, character)
|
||||||
|
self.touch()
|
||||||
|
|
||||||
def delete(self, x, y):
|
def delete(self, x, y):
|
||||||
self._modifications.delete(x, y)
|
self._modifications.delete(x, y)
|
||||||
|
self.touch()
|
||||||
|
|
||||||
def commit_changes(self):
|
def commit_changes(self):
|
||||||
self._content.apply(self._modifications)
|
self._content.apply(self._modifications)
|
||||||
|
self._content.clear_deletions()
|
||||||
self._modifications = ChunkDiff()
|
self._modifications = ChunkDiff()
|
||||||
|
self.touch()
|
||||||
|
|
||||||
def drop_changes(self):
|
def drop_changes(self):
|
||||||
self._modifications = ChunkDiff()
|
self._modifications = ChunkDiff()
|
||||||
|
|
@ -95,6 +103,12 @@ class Chunk():
|
||||||
for line in self._content.combine(self._modifications).lines():
|
for line in self._content.combine(self._modifications).lines():
|
||||||
window.addstr(y, x, line)
|
window.addstr(y, x, line)
|
||||||
y += 1
|
y += 1
|
||||||
|
|
||||||
|
def modified(self):
|
||||||
|
return not self._modifications.empty()
|
||||||
|
|
||||||
|
def empty(self):
|
||||||
|
return self._content.empty() and self._modifications.empty()
|
||||||
|
|
||||||
class ChunkPool():
|
class ChunkPool():
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,8 @@ class Client():
|
||||||
while True:
|
while True:
|
||||||
i = scr.getkey()
|
i = scr.getkey()
|
||||||
|
|
||||||
if i == "q": self.stop()
|
if i == "\x1b": self.stop()
|
||||||
elif i == "r": self.map_.redraw()
|
#elif i == "r": self.map_.redraw()
|
||||||
# normal cursor movement
|
# normal cursor movement
|
||||||
elif i == "KEY_UP": self.map_.move_cursor(0, -1)
|
elif i == "KEY_UP": self.map_.move_cursor(0, -1)
|
||||||
elif i == "KEY_DOWN": self.map_.move_cursor(0, 1)
|
elif i == "KEY_DOWN": self.map_.move_cursor(0, 1)
|
||||||
|
|
@ -65,6 +65,7 @@ class Client():
|
||||||
elif i in string.digits + string.ascii_letters + string.punctuation + " ":
|
elif i in string.digits + string.ascii_letters + string.punctuation + " ":
|
||||||
self.map_.write(i)
|
self.map_.write(i)
|
||||||
elif i == "\x7f": self.map_.delete()
|
elif i == "\x7f": self.map_.delete()
|
||||||
|
elif i == "\n": self.map_.newline()
|
||||||
|
|
||||||
else: sys.stdout.write(repr(i) + "\n")
|
else: sys.stdout.write(repr(i) + "\n")
|
||||||
|
|
||||||
|
|
|
||||||
28
maps.py
28
maps.py
|
|
@ -20,6 +20,8 @@ class Map():
|
||||||
self.worldy = 0
|
self.worldy = 0
|
||||||
self.cursorx = self.cursorpadding
|
self.cursorx = self.cursorpadding
|
||||||
self.cursory = self.cursorpadding
|
self.cursory = self.cursorpadding
|
||||||
|
self.lastcurx = self.cursorx
|
||||||
|
self.lastcury = self.cursory
|
||||||
|
|
||||||
self.chunkpool = chunkpool
|
self.chunkpool = chunkpool
|
||||||
self.drawevent = drawevent
|
self.drawevent = drawevent
|
||||||
|
|
@ -106,7 +108,7 @@ class Map():
|
||||||
|
|
||||||
chunk.set(inchunkx(self.cursorx), inchunky(self.cursory), char)
|
chunk.set(inchunkx(self.cursorx), inchunky(self.cursory), char)
|
||||||
|
|
||||||
self.move_cursor(1, 0)
|
self.move_cursor(1, 0, False)
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
with self.chunkpool as pool:
|
with self.chunkpool as pool:
|
||||||
|
|
@ -114,11 +116,18 @@ class Map():
|
||||||
if chunk:
|
if chunk:
|
||||||
chunk.delete(inchunkx(self.cursorx-1), inchunky(self.cursory))
|
chunk.delete(inchunkx(self.cursorx-1), inchunky(self.cursory))
|
||||||
|
|
||||||
self.move_cursor(-1, 0)
|
self.move_cursor(-1, 0, False)
|
||||||
|
|
||||||
def move_cursor(self, dx, dy):
|
def newline(self):
|
||||||
self.cursorx += dx
|
self.set_cursor(self.lastcurx, self.lastcury+1)
|
||||||
self.cursory += dy
|
|
||||||
|
def set_cursor(self, x, y, explicit=True):
|
||||||
|
self.cursorx = x
|
||||||
|
self.cursory = y
|
||||||
|
|
||||||
|
if explicit:
|
||||||
|
self.lastcurx = self.cursorx
|
||||||
|
self.lastcury = self.cursory
|
||||||
|
|
||||||
self.worldx = min(
|
self.worldx = min(
|
||||||
self.cursorx - self.cursorpadding,
|
self.cursorx - self.cursorpadding,
|
||||||
|
|
@ -137,14 +146,17 @@ class Map():
|
||||||
)
|
)
|
||||||
|
|
||||||
self.load_visible()
|
self.load_visible()
|
||||||
|
|
||||||
|
|
||||||
|
def move_cursor(self, dx, dy, explicit=True):
|
||||||
|
self.set_cursor(self.cursorx+dx, self.cursory+dy, explicit)
|
||||||
|
|
||||||
def scroll(self, dx, dy):
|
def scroll(self, dx, dy):
|
||||||
self.worldx += dx
|
self.worldx += dx
|
||||||
self.worldy += dy
|
self.worldy += dy
|
||||||
|
|
||||||
# new scrolling code: The cursor stays on the same screen position while scrolling
|
# new scrolling code: The cursor stays on the same screen position while scrolling
|
||||||
self.cursorx += dx
|
self.move_cursor(dx, dy)
|
||||||
self.cursory += dy
|
|
||||||
|
|
||||||
# old scrolling code: The cursor would stay on the same world coordinates while scrolling,
|
# old scrolling code: The cursor would stay on the same world coordinates while scrolling,
|
||||||
# and only if it was at the edge of the screen, it would get carried with the window.
|
# and only if it was at the edge of the screen, it would get carried with the window.
|
||||||
|
|
@ -164,7 +176,7 @@ class Map():
|
||||||
#)
|
#)
|
||||||
#)
|
#)
|
||||||
|
|
||||||
self.load_visible()
|
#self.load_visible()
|
||||||
|
|
||||||
|
|
||||||
class ChunkMap():
|
class ChunkMap():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue