From bc8ef01eca8b9baedea143bf5c06f487ce6884ad Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 1 Dec 2021 15:37:07 -0500 Subject: [PATCH] grid --- command_prompt.py | 22 +++++++++++++++++++++- painter.py | 35 +++++++++++++++++++++++++++++++++++ polyomino.py | 3 +++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/command_prompt.py b/command_prompt.py index 2b112e0..b1cd71d 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -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 '" 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 '" + 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): diff --git a/painter.py b/painter.py index b8feba6..8546c43 100644 --- a/painter.py +++ b/painter.py @@ -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): diff --git a/polyomino.py b/polyomino.py index a255856..7bfe366 100644 --- a/polyomino.py +++ b/polyomino.py @@ -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