Implement canvas view moving
This commit is contained in:
parent
751e63e4e7
commit
d3b2bcce3b
41
canvas.py
41
canvas.py
@ -14,8 +14,14 @@ class EditorCanvas(tkinter.Canvas):
|
||||
self.current_char_modified=False
|
||||
self.width=0
|
||||
self.height=0
|
||||
self.prev_mouse_x=0
|
||||
self.prev_mouse_y=0
|
||||
self.view_x=0
|
||||
self.view_y=0
|
||||
self.bind("<B1-Motion>",self.handle_draw)
|
||||
self.bind("<Button-1>",self.handle_draw)
|
||||
self.bind("<Button-3>",self.handle_move_start)
|
||||
self.bind("<B3-Motion>",self.handle_move)
|
||||
|
||||
|
||||
def draw(self):
|
||||
@ -23,30 +29,45 @@ class EditorCanvas(tkinter.Canvas):
|
||||
|
||||
# draw grid
|
||||
for x in range(self.project.char_res[0] + 1):
|
||||
x = x * self.grid_size
|
||||
self.create_line((x,0),(x,self.height),width=1,fill=GRID_COLOR)
|
||||
x = x * self.grid_size+self.view_x
|
||||
self.create_line((x,self.view_y),(x,self.view_y+self.height),width=1,fill=GRID_COLOR)
|
||||
for y in range(self.project.char_res[1] + 1):
|
||||
y = y * self.grid_size
|
||||
self.create_line((0,y),(self.width,y),width=1,fill=GRID_COLOR)
|
||||
y = y * self.grid_size+self.view_y
|
||||
self.create_line((self.view_x,y),(self.view_x+self.width,y),width=1,fill=GRID_COLOR)
|
||||
|
||||
if self.project.does_char_exist(self.current_char) or self.current_char_modified:
|
||||
for i in range(len(self.current_char_pixels)):
|
||||
x=i//self.project.char_res[1]*self.grid_size
|
||||
y=i%self.project.char_res[1]*self.grid_size
|
||||
x=i//self.project.char_res[1]*self.grid_size+self.view_x
|
||||
y=i%self.project.char_res[1]*self.grid_size+self.view_y
|
||||
if self.current_char_pixels[i]>0:
|
||||
self.create_rectangle((x,y),(x+self.grid_size,y+self.grid_size),fill="white")
|
||||
else:
|
||||
self.create_line((0,0),(self.width,self.height),width=3,fill="red")
|
||||
self.create_line((self.width,0),(0,self.height),width=3,fill="red")
|
||||
self.create_line((self.view_x,self.view_y),(self.view_x+self.width,self.view_y+self.height),width=3,fill="red")
|
||||
self.create_line((self.view_x+self.width,self.view_y),(self.view_x,self.view_y+self.height),width=3,fill="red")
|
||||
|
||||
|
||||
def handle_draw(self,event):
|
||||
self.current_char_modified=True
|
||||
pixel_pos=self.cursor_pos_to_pixel((event.x,event.y))
|
||||
if not pixel_pos:
|
||||
return
|
||||
self.current_char_pixels[pixel_pos[0]*self.project.char_res[1]+pixel_pos[1]]=1
|
||||
self.draw()
|
||||
|
||||
|
||||
def handle_move_start(self,event):
|
||||
self.prev_mouse_x=event.x
|
||||
self.prev_mouse_y=event.y
|
||||
|
||||
|
||||
def handle_move(self,event):
|
||||
self.view_x+=event.x-self.prev_mouse_x
|
||||
self.view_y+=event.y-self.prev_mouse_y
|
||||
self.prev_mouse_x=event.x
|
||||
self.prev_mouse_y=event.y
|
||||
self.draw()
|
||||
|
||||
|
||||
def load_char(self):
|
||||
self.current_char_modified=False
|
||||
if not self.project.does_char_exist(self.current_char):
|
||||
@ -114,7 +135,9 @@ class EditorCanvas(tkinter.Canvas):
|
||||
|
||||
|
||||
def cursor_pos_to_pixel(self,pos):
|
||||
pixel_pos = (int(pos[0] // self.grid_size), int(pos[1] // self.grid_size))
|
||||
pixel_pos = ((pos[0]-self.view_x) // self.grid_size, (pos[1]-self.view_y) // self.grid_size)
|
||||
if pixel_pos[0]<0 or pixel_pos[0]>=self.project.char_res[0] or pixel_pos[1]<0 or pixel_pos[1]>=self.project.char_res[1]:
|
||||
return None
|
||||
return pixel_pos
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user