import sys import math from kivy.uix.widget import Widget from kivy.core.window import Window from point import Point from polyomino import Square from polyomino import Square_element from tools import remove_fromlist # painter class class Painter(Widget): def __init__(self,app,**kwargs): # list of particles self.particles=[] # particle under mouse self.undermouse=None # list of selected particles self.selected=[] # complement self.unselected=self.particles # relative position of mouse when moving self.offset=Point(0,0) # app is used to share information between widgets self.app=app # modifiers self.modifiers=[] # init Widget super(Painter,self).__init__(**kwargs) # init keyboard self.keyboard = Window.request_keyboard(None,self,"text") self.keyboard.bind(on_key_down=self.on_key_down,on_key_up=self.on_key_up) def reset(self): self.particles=[] self.undermouse=None self.draw() # draw all particles def draw(self): self.canvas.clear() with self.canvas: for particle in self.particles: particle.draw() # respond to keyboard def on_key_down(self, keyboard, keycode, text, modifiers): # check that command_prompt is not focused if not self.app.command_prompt.insert: if keycode[1]=="shift": if not 's' in self.modifiers: self.modifiers.append('s') self.modifiers.sort() def on_key_up(self, keyboard, keycode): if keycode[1]=="shift": if 's' in self.modifiers: # remove self.modifiers[self.modifiers.index('s')]=self.modifiers[len(self.modifiers)-1] self.modifiers=self.modifiers[:len(self.modifiers)-1] self.modifiers.sort() # respond to mouse down def on_touch_down(self,touch): # only respond to touch in drawing area if self.collide_point(*touch.pos): # create new cross if touch.button=="right": new=Square(touch.x/Square_element.size,touch.y/Square_element.size) if not self.check_interaction_any(new,Point(0,0)): # add to list self.particles.append(new) # unselect all particles for sel in self.selected: sel.selected=False self.selected=[] self.unselected=self.particles self.draw() # select particle if touch.button=="left": # find particle under touch self.undermouse=self.find_particle(Point(touch.x/Square_element.size,touch.y/Square_element.size)) # record relative position of click with respect to reference if self.undermouse!=None: self.offset=Point(touch.x/Square_element.size,touch.y/Square_element.size)-self.undermouse.squares[0].pos # no modifiers if self.modifiers==[]: if self.undermouse==None: # unselect all particles for sel in self.selected: sel.selected=False self.selected=[] self.unselected=self.particles # select undermouse elif not self.undermouse in self.selected: for sel in self.selected: sel.selected=False self.selected=[self.undermouse] self.unselected=self.particles.copy() self.unselected=remove_fromlist(self.unselected,self.undermouse) self.undermouse.selected=True # shift-click elif self.modifiers==['s']: if self.undermouse!=None: if self.undermouse not in self.selected: self.selected.append(self.undermouse) self.undermouse.selected=True # remove from unselected self.unselected=remove_fromlist(self.unselected,self.undermouse) else: # remove self.selected=remove_fromlist(self.selected,self.undermouse) self.undermouse.selected=False # add to unselected self.unselected.append(self.undermouse) self.draw() # respond to drag def on_touch_move(self,touch): # only respond to touch in drawing area if self.collide_point(*touch.pos): # only move on left click if touch.button=="left" and self.modifiers==[] and self.undermouse!=None: # attempted move determined by the relative position to the relative position of click within self.undermouse delta=self.adjust_move(Point(touch.x/Square_element.size,touch.y/Square_element.size)-(self.offset+self.undermouse.squares[0].pos)) for particle in self.selected: particle.move(delta) # redraw self.draw() # find the particle at position pos def find_particle(self,pos): for particle in self.particles: if particle.in_support(pos): return particle # none found return None # check whether a candidate particle intersects with any of the particles def check_interaction_any(self,candidate,offset): for particle in self.particles: # do not check interaction if candidate=particle if candidate!=particle and particle.check_interaction(candidate,offset): return True return False # check whether shifting a list of particles by offset makes them interact with all particles def check_interaction_list(self,array,offset): for candidate in array: if self.check_interaction_any(candidate,offset): return True return False # check whether shifting a list of particles by offset makes them interact with the unselected particles def check_interaction_unselected_list(self,array,offset): for candidate in array: for particle in self.unselected: if particle.check_interaction(candidate,offset): return True return False # check whether a candidate particle element with any of the unselected particles def check_interaction_unselected_element(self,element,offset): for particle in self.unselected: for square in particle.squares: if square.check_interaction(element.pos+offset): return True return False # try to move all selected particles by delta, adjust if needed to avoid overlap with unselected particles # we only track whether these elements collide with unselected particles, not with each other def adjust_move(self,delta): # actual_delta is the smallest (componentwise) of all the computed delta's actual_delta=Point(math.inf,math.inf) for particle in self.selected: for element in particle.squares: # compute adjustment move due to unselected obstacles adjusted_delta=self.adjust_move_element(delta,element) # only keep the smallest delta's (in absolute value) if abs(adjusted_delta.x)2: print("warning: ignoring line "+str(i)+" in file '"+file+"': more than two ';' spearated entries in '"+line+"'",file=sys.stderr) continue # position pos_str=entries[0].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) 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) continue # color color=(0,0,1) if len(entries)==2: color_str=entries[1].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) 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) continue if self.check_interaction_any(pos,None): # add to list self.particles.append(Cross(pos.x,pos.y,color=color)) else: print("warning: ignoring line "+str(i)+" in file '"+file+"': particle overlaps with existing particles",file=sys.stderr) ff.close() self.draw()