From fcd15da3f3e8eff3232e09dc4c30fc0a84afde74 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Wed, 20 Oct 2021 11:35:09 -0400 Subject: [PATCH] multiple selection --- cross.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/cross.py b/cross.py index 29aeee0..ae9a170 100644 --- a/cross.py +++ b/cross.py @@ -2,6 +2,7 @@ import math import sys from kivy.graphics import Color,Line,Rectangle from kivy.uix.widget import Widget +from kivy.core.window import Window from point import Point from tools import isint_nonzero,sgn @@ -223,12 +224,22 @@ class Cross_painter(Widget): # cross under mouse self.undermouse=None + # list of selected crosses + self.selected=[] + # app is used to share information between widgets self.app=app + # modifiers + self.modifiers=[] + # init Widget super(Cross_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.crosses=[] @@ -244,29 +255,63 @@ class Cross_painter(Widget): cross.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 area if self.collide_point(*touch.pos): + + # unselect + if touch.button!="left" or self.modifiers!=['s']: + for sel in self.selected: + sel.selected=False + self.selected=[] + # create new cross if touch.button=="right": if self.check_interaction_any(Point(touch.x/Cross.size,touch.y/Cross.size),None): new=Cross(touch.x/Cross.size,touch.y/Cross.size) - with self.canvas: - new.draw() # add to list self.crosses.append(new) + self.draw() # select cross if touch.button=="left": - # unselect - if self.undermouse!=None: - self.undermouse.selected=False + # no modifiers + if self.modifiers==[]: + # find cross under touch + self.undermouse=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size)) + + # shift-click + elif self.modifiers==['s']: + clicked=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size)) + if clicked!=None: + if clicked not in self.selected: + self.selected.append(clicked) + clicked.selected=True + else: + # remove + self.selected[self.selected.index(clicked)]=self.selected[len(self.selected)-1] + self.selected=self.selected[:len(self.selected)-1] + clicked.selected=False - # find cross under touch - self.undermouse=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size)) - if self.undermouse!=None: - self.undermouse.selected=True self.draw()