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
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>'"
return
if argv[1]=="color":
self.run_set_color(argv)
return
elif argv[1]=="grid":
self.run_set_grid(argv)
return
else:
self.message="error: unrecognized command '"+argv[1]+"'"
return
# set color
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":
self.app.painter.set_color((0,0,1))
elif argv[2]=="green":
@ -356,6 +363,19 @@ class Command_prompt(Label):
return
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
def run_write(self,argv):

View File

@ -2,6 +2,7 @@ import sys
import math
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color,Line
from point import Point
from polyomino import Cross
@ -52,9 +53,30 @@ class Painter(Widget):
def draw(self):
self.canvas.clear()
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:
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
def on_key_down(self, keyboard, keycode, text, modifiers):
@ -257,6 +279,19 @@ class Painter(Widget):
# redraw
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
def write(self,file):

View File

@ -14,6 +14,9 @@ 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)
# draw function
def draw(self):
# set color