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

View File

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