05 Jul

drag-selection in kfm

I didn’t get anything done on KFM over the weekend (was concentrating on figuring out that IMAP problem), so I felt a bit guilty and did some work today.

I wrote a drag-selection method, which allows you to select files by dragging a rectangle around them.

It’s surprising how many different variables need to be considered, to decide whether a file is selected or not!

For instance, here is the if block which decides whether a file is contained in a selection area, or is touched by it:

		if(
			kfm_isPointInBox(x3,y3,x1,y1,x2,y2)||
			kfm_isPointInBox(x4,y3,x1,y1,x2,y2)||
			kfm_isPointInBox(x3,y4,x1,y1,x2,y2)||
			kfm_isPointInBox(x4,y4,x1,y1,x2,y2)||
			kfm_isPointInBox(x1,y1,x3,y3,x4,y4)||
			kfm_isPointInBox(x2,y1,x3,y3,x4,y4)||
			kfm_isPointInBox(x1,y2,x3,y3,x4,y4)||
			kfm_isPointInBox(x2,y2,x3,y3,x4,y4)||
			(x1>=x3&&x2<=x4&&y1<=y3&&y2>=y4)||
			(x1<=x3&&x2>=x4&&y1>=y3&&y2<=y4)
		)kfm_addToSelection(file);

In the above, [x1,y1] and [x2,y2] are the top-left and bottom-right of the selection area, and [x3,y3] and [x4,y4] are the top-left and bottom-right of the file icons.

I’m sure there must be a more efficient way than that!

Actually, the code I wrote can easily be made more efficient by applying a binary tree algorithm to find the first and last icons which should be considered at all (if the selection starts at [100,343] for example, then all icons with y4<343 can be confidently pruned).

That’s the visible work for today. I’m going to take a short break, then get onto a bit of files house-keeping.

Oh – demo (use Firefox for best effect), and source.