diff --git a/painter.py b/painter.py index a532b73..81d35c5 100644 --- a/painter.py +++ b/painter.py @@ -23,7 +23,7 @@ class Painter(Widget): # list of selected particles self.selected=[] # complement - self.unselected=self.particles + self.unselected=[] # relative position of mouse when moving self.offset=Point(0,0) @@ -250,15 +250,15 @@ class Painter(Widget): return self.adjust_move_element(newdelta,element,recursion_depth+1) - # TODO adapt # write configuration to file def write(self,file): ff=open(file,"w") for particle in self.particles: - ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(particle.pos.x,particle.pos.y,particle.color[0],particle.color[1],particle.color[2])) + if type(particle)==Cross: + ff.write("{:d};".format(CROSS_INDEX)) + ff.write("{:05.2f},{:05.2f};{:3.1f},{:3.1f},{:3.1f}\n".format(particle.squares[0].pos.x,particle.squares[0].pos.y,particle.color[0],particle.color[1],particle.color[2])) ff.close() - # TODO adapt # read configuration from file def read(self,file): self.reset() @@ -292,40 +292,55 @@ class Painter(Widget): entries=line.split(";") # skip line if improperly formatted - if len(entries)>2: - print("warning: ignoring line "+str(i)+" in file '"+file+"': more than two ';' spearated entries in '"+line+"'",file=sys.stderr) + if len(entries)>3: + print("warning: ignoring line "+str(i)+" in file '"+file+"': more than three ';' spearated 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) continue # position - pos_str=entries[0].split(",") + pos_str=entries[1].split(",") # skip line if improperly formatted if len(pos_str)!=2: - print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' does not have two components",file=sys.stderr) + print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[1]+"' does not have two components",file=sys.stderr) continue try: pos=Point(float(pos_str[0]),float(pos_str[1])) except: - print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[0]+"' cannot be read",file=sys.stderr) + print("warning: ignoring line "+str(i)+" in file '"+file+"': position '"+entries[1]+"' cannot be read",file=sys.stderr) continue # color color=(0,0,1) - if len(entries)==2: - color_str=entries[1].split(",") + if len(entries)==3: + color_str=entries[2].split(",") # skip line if improperly formatted if len(color_str)!=3: - print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' does not have three components",file=sys.stderr) + print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[2]+"' does not have three components",file=sys.stderr) continue try: color=(float(color_str[0]),float(color_str[1]),float(color_str[2])) except: - print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[1]+"' cannot be read",file=sys.stderr) + print("warning: ignoring line "+str(i)+" in file '"+file+"': color '"+entries[2]+"' cannot be read",file=sys.stderr) continue - if self.check_interaction_any(pos,None): + # candidate particle + try: + particle_type=int(entries[0]) + except: + print("warning: ignoring line "+str(i)+" in file '"+file+"': particle type '"+entries[0]+"' is not an integer",file=sys.stderr) + continue + if particle_type==CROSS_INDEX: + candidate=Cross(pos.x,pos.y,color=color) + else: + print("warning: ignoring line "+str(i)+" in file '"+file+"': unrecognized particle type: '"+entries[0]+"'",file=sys.stderr) + continue + + if not self.check_interaction_any(candidate,Point(0,0)): # add to list - self.particles.append(Cross(pos.x,pos.y,color=color)) + self.particles.append(candidate) + self.unselected.append(candidate) else: print("warning: ignoring line "+str(i)+" in file '"+file+"': particle overlaps with existing particles",file=sys.stderr) @@ -333,3 +348,7 @@ class Painter(Widget): self.draw() +# global variables (used like precompiler variables) +CROSS_INDEX=1 + +