Variable mesh in grid

This commit is contained in:
Ian Jauslin 2021-12-01 18:49:35 -05:00
parent bc8ef01eca
commit 4b0352f5d7
3 changed files with 31 additions and 22 deletions

View File

@ -368,12 +368,20 @@ class Command_prompt(Label):
if len(argv)==2:
# no argument: set to toggle
self.app.painter.set_grid(-1)
elif argv[2]=="on" or argv[2]=="1":
elif argv[2]=="on":
self.app.painter.set_grid(1)
elif argv[2]=="off" or argv[2]=="0":
elif argv[2]=="off":
self.app.painter.set_grid(0)
else:
self.message="error: unrecognized argument '"+argv[2]+"' -- usage 'set grid [on|off]'"
try:
mesh=float(argv[2])
except:
self.message="error: unrecognized argument '"+argv[2]+"' -- usage 'set grid [on|off|<float>]'"
return
if mesh<0:
self.message="error: grid size cannot be negative: '"+argv[2]+"'"
return
self.app.painter.set_grid(mesh)

View File

@ -55,27 +55,27 @@ class Painter(Widget):
with self.canvas:
# draw grids
for particle in self.particles:
if particle.grid:
self.draw_grid(particle.squares[0].pos)
if particle.grid>0:
self.draw_grid(particle.squares[0].pos,particle.grid)
for particle in self.particles:
particle.draw()
def draw_grid(self,pos):
def draw_grid(self,pos,mesh):
# height offset due to status bar and command prompt
height_offset=self.app.status_bar.height+self.app.command_prompt.height
# vertical lines
# offest wrt 0
offset=(pos.x-0.5)%1
for i in range(math.floor(self.width/Square_element.size-offset)+1):
offset=(pos.x-mesh/2)%mesh
for i in range(math.floor((self.width/Square_element.size-offset)/mesh)+1):
Color(1,1,1)
Line(points=((i+offset)*Square_element.size,height_offset,(i+offset)*Square_element.size,self.height+height_offset))
Line(points=((i*mesh+offset)*Square_element.size,height_offset,(i*mesh+offset)*Square_element.size,self.height+height_offset))
# horizontal lines
# offset wrt 0
offset=(pos.y-0.5)%1-height_offset/Square_element.size
for i in range(math.floor(self.height/Square_element.size-offset)+1):
offset=(pos.y-mesh/2)%1-height_offset/Square_element.size
for i in range(math.floor((self.height/Square_element.size-offset)/mesh)+1):
Color(1,1,1)
Line(points=(0,(i+offset)*Square_element.size+height_offset,self.width,(i+offset)*Square_element.size+height_offset))
Line(points=(0,(i*mesh+offset)*Square_element.size+height_offset,self.width,(i*mesh+offset)*Square_element.size+height_offset))
# respond to keyboard
@ -280,15 +280,16 @@ class Painter(Widget):
self.draw()
# set grid for selected particles
# onoff: 1:on, 0:off, -1:toggle
def set_grid(self,onoff):
# set mesh to -1 to toggle on/off
def set_grid(self,mesh):
for particle in self.selected:
if onoff==1:
particle.grid=True
elif onoff==0:
particle.grid=False
elif onoff==-1:
particle.grid=not particle.grid
if mesh==-1:
if particle.grid==0:
particle.grid=1
else:
particle.grid=-particle.grid
else:
particle.grid=mesh
# redraw
self.draw()

View File

@ -14,8 +14,8 @@ class Polyomino():
self.color=kwargs.get("color",(0,0,1))
self.selected=False
# whether to draw a background grid
self.grid=kwargs.get("grid",False)
# mesh of background grid (no grid for mesh size 0)
self.grid=kwargs.get("grid",0)
# draw function
def draw(self):