Dragging circles

I am restructuring my code as I was told; now I want to drag and drop my drawn circles as follows: each single circle must be draggable and droppable independently. The following code is only working for the last circle one time (see circles.size()-1). On dragging again, a circle underneath it is staying at the old place. Besides this, on dragging an earlier circle, the last circle is also removed which should not occur. Any ideas on how to get this code working properly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
case WM_MOUSEMOVE: {

	            if (wParam & MK_LBUTTON)
	            {


	                for(auto c: circles){
	                	if(is_point_in_circle( x, y, c )){
	                		x = LOWORD(lParam);
       			    		y = HIWORD(lParam)/6*6;
       			    		int k = circles.size()-1;
       			    		while (k--){
       			    			if (is_point_in_circle( x, y, c )){
       			    				circles.erase( std::next( circles.begin(), k ) );
       			    				break;
       			    			}
       			    		}
       			    		X = x-6;
       			    		Y = (y-6)/6*6;
	                		circles.push_back( { X, Y, X2, Y2, duration } );

	                	}
	                }
I am concerned that you are still struggling with this. I think you are looking at stuff the wrong way.

Programming never goes very well when you just sit down in front of your text editor and write code. Before you write any code at all, you must first develop a clear idea of what you want the code to do.

You do not have a clear idea of how your code works.

Before you go any further, take time to think through things like:

  • When you press the LMB, what should happen?

  • When you release it, what should happen?

  • Does what happen change if you dragged the mouse around while LMB pressed?

  • Does what happen change for any other reason?
    (Are we adding things? Deleting things? Changing things?)

I am not trying to be rude or condescending. You need to learn how to do this, and you are at a point where just hacking out a program without some prior planning Just Won't Work™.

The exact content of your posts change, but you are having the same problem in every one. Get out a piece of paper and a pencil and think through how everything works, then you will better see how to structure your event handling.

I hope this helps.
Topic archived. No new replies allowed.