multiple selection

This commit is contained in:
Ian Jauslin 2021-10-20 11:35:09 -04:00
parent 0fe82c1006
commit fcd15da3f3

View File

@ -2,6 +2,7 @@ import math
import sys import sys
from kivy.graphics import Color,Line,Rectangle from kivy.graphics import Color,Line,Rectangle
from kivy.uix.widget import Widget from kivy.uix.widget import Widget
from kivy.core.window import Window
from point import Point from point import Point
from tools import isint_nonzero,sgn from tools import isint_nonzero,sgn
@ -223,12 +224,22 @@ class Cross_painter(Widget):
# cross under mouse # cross under mouse
self.undermouse=None self.undermouse=None
# list of selected crosses
self.selected=[]
# app is used to share information between widgets # app is used to share information between widgets
self.app=app self.app=app
# modifiers
self.modifiers=[]
# init Widget # init Widget
super(Cross_painter,self).__init__(**kwargs) 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): def reset(self):
self.crosses=[] self.crosses=[]
@ -244,29 +255,63 @@ class Cross_painter(Widget):
cross.draw() 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 # respond to mouse down
def on_touch_down(self,touch): def on_touch_down(self,touch):
# only respond to touch in area # only respond to touch in area
if self.collide_point(*touch.pos): 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 # create new cross
if touch.button=="right": if touch.button=="right":
if self.check_interaction_any(Point(touch.x/Cross.size,touch.y/Cross.size),None): 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) new=Cross(touch.x/Cross.size,touch.y/Cross.size)
with self.canvas:
new.draw()
# add to list # add to list
self.crosses.append(new) self.crosses.append(new)
self.draw()
# select cross # select cross
if touch.button=="left": if touch.button=="left":
# unselect # no modifiers
if self.undermouse!=None: if self.modifiers==[]:
self.undermouse.selected=False
# find cross under touch # find cross under touch
self.undermouse=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size)) self.undermouse=self.find_cross(Point(touch.x/Cross.size,touch.y/Cross.size))
if self.undermouse!=None:
self.undermouse.selected=True # 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
self.draw() self.draw()