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
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def combine(cls, diff_a, diff_b):
|
||||
diff = cls()
|
||||
diff.apply(diff_a)
|
||||
diff.apply(diff_b)
|
||||
return diff
|
||||
|
||||
def __init__(self):
|
||||
self._chars = {}
|
||||
|
||||
|
|
@ -27,6 +20,12 @@ class ChunkDiff():
|
|||
return diff
|
||||
#self._chars = d.copy()
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, s):
|
||||
diff = cls()
|
||||
#for c in string
|
||||
pass
|
||||
|
||||
def copy(self):
|
||||
return ChunkDiff.from_dict(self.to_dict().copy())
|
||||
|
||||
|
|
@ -40,12 +39,14 @@ class ChunkDiff():
|
|||
#return self._chars.copy()
|
||||
|
||||
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):
|
||||
pos = x + y*CHUNK_WIDTH
|
||||
if pos in self._chars:
|
||||
del self._chars[x+y*CHUNK_WIDTH]
|
||||
self.set(x, y, " ")
|
||||
|
||||
def clear_deletions(self):
|
||||
self._chars = {i: v for i, v in self._chars.items() if v != " "}
|
||||
|
||||
def apply(self, diff):
|
||||
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))
|
||||
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():
|
||||
"""
|
||||
|
|
@ -74,13 +78,17 @@ class Chunk():
|
|||
|
||||
def set(self, x, y, character):
|
||||
self._modifications.set(x, y, character)
|
||||
self.touch()
|
||||
|
||||
def delete(self, x, y):
|
||||
self._modifications.delete(x, y)
|
||||
self.touch()
|
||||
|
||||
def commit_changes(self):
|
||||
self._content.apply(self._modifications)
|
||||
self._content.clear_deletions()
|
||||
self._modifications = ChunkDiff()
|
||||
self.touch()
|
||||
|
||||
def drop_changes(self):
|
||||
self._modifications = ChunkDiff()
|
||||
|
|
@ -96,6 +104,12 @@ class Chunk():
|
|||
window.addstr(y, x, line)
|
||||
y += 1
|
||||
|
||||
def modified(self):
|
||||
return not self._modifications.empty()
|
||||
|
||||
def empty(self):
|
||||
return self._content.empty() and self._modifications.empty()
|
||||
|
||||
class ChunkPool():
|
||||
"""
|
||||
Is a collection of chunks.
|
||||
|
|
|
|||
|
|
@ -44,8 +44,8 @@ class Client():
|
|||
while True:
|
||||
i = scr.getkey()
|
||||
|
||||
if i == "q": self.stop()
|
||||
elif i == "r": self.map_.redraw()
|
||||
if i == "\x1b": self.stop()
|
||||
#elif i == "r": self.map_.redraw()
|
||||
# normal cursor movement
|
||||
elif i == "KEY_UP": 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 + " ":
|
||||
self.map_.write(i)
|
||||
elif i == "\x7f": self.map_.delete()
|
||||
elif i == "\n": self.map_.newline()
|
||||
|
||||
else: sys.stdout.write(repr(i) + "\n")
|
||||
|
||||
|
|
|
|||
28
maps.py
28
maps.py
|
|
@ -20,6 +20,8 @@ class Map():
|
|||
self.worldy = 0
|
||||
self.cursorx = self.cursorpadding
|
||||
self.cursory = self.cursorpadding
|
||||
self.lastcurx = self.cursorx
|
||||
self.lastcury = self.cursory
|
||||
|
||||
self.chunkpool = chunkpool
|
||||
self.drawevent = drawevent
|
||||
|
|
@ -106,7 +108,7 @@ class Map():
|
|||
|
||||
chunk.set(inchunkx(self.cursorx), inchunky(self.cursory), char)
|
||||
|
||||
self.move_cursor(1, 0)
|
||||
self.move_cursor(1, 0, False)
|
||||
|
||||
def delete(self):
|
||||
with self.chunkpool as pool:
|
||||
|
|
@ -114,11 +116,18 @@ class Map():
|
|||
if chunk:
|
||||
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):
|
||||
self.cursorx += dx
|
||||
self.cursory += dy
|
||||
def newline(self):
|
||||
self.set_cursor(self.lastcurx, self.lastcury+1)
|
||||
|
||||
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.cursorx - self.cursorpadding,
|
||||
|
|
@ -138,13 +147,16 @@ class Map():
|
|||
|
||||
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):
|
||||
self.worldx += dx
|
||||
self.worldy += dy
|
||||
|
||||
# new scrolling code: The cursor stays on the same screen position while scrolling
|
||||
self.cursorx += dx
|
||||
self.cursory += dy
|
||||
self.move_cursor(dx, dy)
|
||||
|
||||
# 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.
|
||||
|
|
@ -164,7 +176,7 @@ class Map():
|
|||
#)
|
||||
#)
|
||||
|
||||
self.load_visible()
|
||||
#self.load_visible()
|
||||
|
||||
|
||||
class ChunkMap():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue