Compare commits

...

6 Commits

Author SHA1 Message Date
Ian Jauslin 0070094b94 Save lattice in conf file 2024-02-21 17:10:33 -05:00
Ian Jauslin 37f8d181c2 Error and warning messages 2024-02-21 17:01:52 -05:00
Ian Jauslin 5384d9a964 Save color in conf 2024-02-21 16:56:53 -05:00
Ian Jauslin ff11c1ce84 Set color of next particle 2024-02-21 16:49:48 -05:00
Ian Jauslin 0a8a0e3c53 Save shape and zoom level to conf 2024-02-21 16:00:15 -05:00
Ian Jauslin b1d56fea04 Set shape from command 2024-02-21 15:30:52 -05:00
3 changed files with 112 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import os.path
import filecheck
import colors
from polyomino import Cross,Disk
class Command_prompt(Label):
@ -338,6 +339,9 @@ class Command_prompt(Label):
if argv[1]=="color":
self.run_set_color(argv)
return
elif argv[1]=="shape":
self.run_set_shape(argv)
return
elif argv[1]=="grid":
self.run_set_grid(argv)
return
@ -350,7 +354,7 @@ class Command_prompt(Label):
# 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>'"
self.message="error: 'set color' command was run with without an argument -- usage: 'set color <color_descriptor>'"
return
# find color name in list
for color in colors.xcolor_names:
@ -372,6 +376,19 @@ class Command_prompt(Label):
return
self.app.painter.set_color(color)
# set particle shape
def run_set_shape(self,argv):
if len(argv)<3:
self.message="error: 'set shape' command was run with without an argument -- usage: 'set shape <shape_descriptor>'"
return
elif argv[2]=="cross":
self.app.painter.shape=Cross
elif argv[2]=="disk":
self.app.painter.shape=Disk
else:
self.message="error: unrecognized shape '"+argv[2]+"'; supported shapes are cross|disk"
return
# toggle grid
def run_set_grid(self,argv):
if len(argv)==2:

50
src/jam
View File

@ -40,6 +40,10 @@ def read_cli():
# lattice
if c=='L':
flag="lattice"
else:
print("error: unrecognized option '"+c+"'\n", file=sys.stderr)
exit(-1)
else:
# read lattice argument
@ -57,10 +61,56 @@ def read_cli():
else:
openfile=arg
(ret,message)=filecheck.check_edit(openfile)
preread_conf(openfile)
if ret<0:
print(message,file=sys.stderr)
exit(-1)
# read command line arguments from configuration file
def preread_conf(file):
global lattice
try:
ff=open(file,"r")
except:
print("error: could not read file '"+file+"' (this should not happen and is probably a bug)", file=sys.stderr)
exit(-1)
# counter
i=0
try:
lines=ff.readlines()
except:
print("error: could not read the contents of file '"+file+"'", file=sys.stderr)
exit(-1)
for line in lines:
i+=1
# remove newline
line=line[:len(line)-1]
# ignore comments
if '#' in line:
line=line[:line.find('#')]
# ignore empty lines
if len(line)==0:
continue
# read options
if line[0]=='%':
# ignore empty line
if len(line)==1:
continue
[key,val]=line[1:].split('=',1)
if key=="lattice":
# test the specification
(obj,message)=Lattice.new(val)
if obj==None:
print("error: line "+str(i)+" in file '"+file+"': "+message,file=sys.stderr)
exit(-1)
lattice=val
ff.close()
# read cli
read_cli()

View File

@ -35,6 +35,12 @@ class Painter(Widget):
# list of particles
self.particles=[]
# shape of particle to add next
self.shape=Cross
# color of particle to add next
self.color=(0,0,1)
# underlying lattice
self.lattice=None
@ -229,8 +235,7 @@ class Painter(Widget):
# create new particle
if touch.button=="right":
new=Cross(touchx,touchy)
#new=Disk(touchx,touchy)
new=self.shape(touchx,touchy,color=self.color)
# snap to lattice
if self.lattice!=None:
new.move(self.lattice.nearest_delta(new.elements[0].pos))
@ -438,6 +443,9 @@ class Painter(Widget):
# set color of selected particles
def set_color(self,color):
# set color for next particles
self.color=color
# set color of selected particles
for particle in self.selected:
particle.color=color
# redraw
@ -461,6 +469,15 @@ class Painter(Widget):
# write configuration to file
def write(self,file):
ff=open(file,"w")
# save state (particle shape, zoom, lattice)
if self.shape==Cross:
ff.write("%shape=cross\n")
else:
ff.write("%shape=disk\n")
ff.write("%zoom={:1.1f}\n".format(self.base_size/50))
ff.write("%color={:d},{:d},{:d}\n".format(self.color[0],self.color[1],self.color[2]))
if self.lattice != None:
ff.write("%lattice="+self.lattice.type+':'+str(self.lattice.spacing)+"\n")
for particle in self.particles:
if type(particle)==Cross:
ff.write("{:d};".format(CROSS_INDEX))
@ -500,12 +517,35 @@ class Painter(Widget):
if len(line)==0:
continue
# read options
if line[0]=='%':
# ignore empty line
if len(line)==1:
continue
[key,val]=line[1:].split('=',1)
if key=="shape":
self.app.command_prompt.run_set_shape(["set","shape",val])
elif key=="zoom":
self.app.command_prompt.run_set_zoom(["set","zoom",val])
elif key=="color":
color_str=val.split(',')
try:
self.set_color((float(color_str[0]),float(color_str[1]),float(color_str[2])))
except:
print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+color_str+"' cannot be read",file=sys.stderr)
# lattice is handled by main function
elif key=="lattice":
continue
else:
print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized option '"+key+"'",file=sys.stderr)
continue
entries=line.split(";")
# skip line if improperly formatted
if len(entries)>3:
print("warning: ignoring line "+str(i)+" in file '"+file+"': more than three ';' spearated entries in '"+line+"'",file=sys.stderr)
print("warning: ignoring line "+str(i)+" in file '"+file+"': more than three ';' separated entries in '"+line+"'",file=sys.stderr)
if len(entries)<2:
print("warning: ignoring line "+str(i)+" in file '"+file+"': fewer than two ';' spearated entries in '"+line+"'",file=sys.stderr)
print("warning: ignoring line "+str(i)+" in file '"+file+"': fewer than two ';' separated entries in '"+line+"'",file=sys.stderr)
continue
# position