This commit is contained in:
Ian Jauslin 2021-12-01 15:37:07 -05:00
parent c17226cd44
commit bc8ef01eca
3 changed files with 59 additions and 1 deletions

View File

@ -313,18 +313,25 @@ class Command_prompt(Label):
# set properties of particles # set properties of particles
def run_set(self,argv): def run_set(self,argv):
if len(argv)<3: if len(argv)<2:
self.message="error: 'set' command was run with too few arguments -- usage: 'set <property> <value>'" self.message="error: 'set' command was run with too few arguments -- usage: 'set <property> <value>'"
return return
if argv[1]=="color": if argv[1]=="color":
self.run_set_color(argv) self.run_set_color(argv)
return return
elif argv[1]=="grid":
self.run_set_grid(argv)
return
else: else:
self.message="error: unrecognized command '"+argv[1]+"'" self.message="error: unrecognized command '"+argv[1]+"'"
return return
# set color
def run_set_color(self,argv): def run_set_color(self,argv):
if len(argv)<3:
self.message="error: 'set color' command was run with without anargument -- usage: 'set color <color_descriptor>'"
return
if argv[2]=="blue": if argv[2]=="blue":
self.app.painter.set_color((0,0,1)) self.app.painter.set_color((0,0,1))
elif argv[2]=="green": elif argv[2]=="green":
@ -356,6 +363,19 @@ class Command_prompt(Label):
return return
self.app.painter.set_color(color) self.app.painter.set_color(color)
# toggle grid
def run_set_grid(self,argv):
if len(argv)==2:
# no argument: set to toggle
self.app.painter.set_grid(-1)
elif argv[2]=="on" or argv[2]=="1":
self.app.painter.set_grid(1)
elif argv[2]=="off" or argv[2]=="0":
self.app.painter.set_grid(0)
else:
self.message="error: unrecognized argument '"+argv[2]+"' -- usage 'set grid [on|off]'"
# write to file # write to file
def run_write(self,argv): def run_write(self,argv):

View File

@ -2,6 +2,7 @@ import sys
import math import math
from kivy.uix.widget import Widget from kivy.uix.widget import Widget
from kivy.core.window import Window from kivy.core.window import Window
from kivy.graphics import Color,Line
from point import Point from point import Point
from polyomino import Cross from polyomino import Cross
@ -52,9 +53,30 @@ class Painter(Widget):
def draw(self): def draw(self):
self.canvas.clear() self.canvas.clear()
with self.canvas: with self.canvas:
# draw grids
for particle in self.particles:
if particle.grid:
self.draw_grid(particle.squares[0].pos)
for particle in self.particles: for particle in self.particles:
particle.draw() particle.draw()
def draw_grid(self,pos):
# 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):
Color(1,1,1)
Line(points=((i+offset)*Square_element.size,height_offset,(i+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):
Color(1,1,1)
Line(points=(0,(i+offset)*Square_element.size+height_offset,self.width,(i+offset)*Square_element.size+height_offset))
# respond to keyboard # respond to keyboard
def on_key_down(self, keyboard, keycode, text, modifiers): def on_key_down(self, keyboard, keycode, text, modifiers):
@ -257,6 +279,19 @@ class Painter(Widget):
# redraw # redraw
self.draw() self.draw()
# set grid for selected particles
# onoff: 1:on, 0:off, -1:toggle
def set_grid(self,onoff):
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
# redraw
self.draw()
# write configuration to file # write configuration to file
def write(self,file): def write(self,file):

View File

@ -14,6 +14,9 @@ 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
self.grid=kwargs.get("grid",False)
# draw function # draw function
def draw(self): def draw(self):
# set color # set color