UI: move-able windows (z-order)

Started by
4 comments, last by Nausea 11 years ago

Hey!

I've run into a problem with my noobish UI setup. I have a Vector that stores all my windows. When the mouse is clicked I go through the windows vector and check if the mouse is inside any of the windows. If it is i move that window relative to the mouse position.

Now my problem is: How do I order the window selected to be in front? and since the mouse can be inside 2 windows during my check the last window it's inside will always be the one selected.

I know I should probably be using a z-order of some kind, but I just can't work out where to set it for a window and how to adjust the other accordingly.

I'll just post some code of the way I check if the mouse is inside a window.


void CGame::OnLButtonDown(int _mousex, int _mousey)
{
	

	for(unsigned int i=0; i < WindowList.size(); i++)
	{
	
		if(!WindowList->GetShow()) //dont do any checks if the window is hidden.
		{
		
			continue;
		
		}

		if(_mousex >= WindowList->GetX() && _mousex <= (WindowList->GetX() + WindowList->GetW() ))
		{
		
			if(_mousey >= WindowList->GetY() && _mousey <= (WindowList->GetY() + WindowList->GetH()))
			{


				CurrentWindow = i;
				WindowList->SetMouseDown(true);
				WindowList->SetDifference(_mousex - WindowList->GetX(), _mousey - WindowList->GetY());

				
				for(unsigned int itemnum = 0; itemnum < WindowList->WindowItemList.size(); itemnum++)
				{
				
					if(_mousex >= WindowList->WindowItemList[itemnum]->GetX() && _mousex <= (WindowList->WindowItemList[itemnum]->GetX() + WindowList->WindowItemList[itemnum]->GetW()))
					{
					
						if(_mousey >= WindowList->WindowItemList[itemnum]->GetY() && _mousey <= (WindowList->WindowItemList[itemnum]->GetY() + WindowList->WindowItemList[itemnum]->GetH()))
						{
							
								if(!WindowList->WindowItemList[itemnum]->GetActive()) //if the item is not an active item (such as a divider), don't do any checks for it.
								{
									continue;
								}
								else
								{
									
									OverActiveItem = true;

									if(WindowList->WindowItemList[itemnum]->GetType() == ITEM_TYPE_CHECKBOX)
									{
										WindowList->WindowItemList[itemnum]->Toggle();
									}

								
								
								}
								

						
						}
					
					}

				
				}

				

			}
		
		}
		
	}


}


 

Advertisement

I would, in each window, store a index to the window in front of it.

W1

v

W2

v

W3

So if you were to click on W2, you should swap the index of W2 and W3, and make W1 point to W3

W1

v

W3

v

W2

So its kind of a linked list. If you make it doubly linked (indices to the other dir. too) you could start from the topmost window (perhaps always store the top window index somewhere) and stop on the first window which the mouse is on top of to find the window you click.

If you want to use the same system for buttons and stuff, i would make each of those W's able to contain many same-depth elements (eg. buttons on the same window)

I suggest you also google for how to handle this.

EDIT:

Another way to handle it would be to have a separate list which contains indices to the list of the windows in depth order. This way you dont need the windows to know about their depth.

So

-List of windows

-List of indices to windows, in depth order

To move a window in depth, you just remove the index, move all the indices above down by 1, and place it at the top. However this requires you to find the window from the depth sorted list first, but if you just used the depth list to find which window you clicked on, you should have that information available.

o3o

Thank you for your answer. Could you go into more detail on the "list of indices" thing? Sorry my first language is not English so I don't fully understand what you mean by it.

Especially the "indices" part :P

Instead of storing the window pointers in a vector, store them in the list. Mouse testing is then done from top to bottom, and drawing is done from bottom to top. Moving a window to the top requires that the window be removed and added to the front of the list.

On an unrelated note, the code you posted is somewhat hard to follow because of all the nested code blocks. Try to make it more flat. For example,


if(!WindowList->WindowItemList[itemnum]->GetActive())
{
continue;
}
else
{/*do stuff*/}
 

is equivalent to


if(!WindowList->WindowItemList[itemnum]->GetActive())
    continue;
/*do stuff*/

Ok, so lets say you have the vector of windows

Vector<Window> windows; //9001 windows here

Now, you have another list, which stores the indices of the windows (to the window vector) in depth order

Vector<int> windowsSortedByDepth;

So lets say you have a window at windows[30]. As such, the index of the window is 30.

If this windows is at the top of windowsSortedByDepth, it means its the top window

windowsSortedByDepth[windowsSortedByDepth.size() - 1] = 30 //window 30 is in front

So, to find the top window itself, you can do

windows[ windowsSortedByDepth[windowsSortedByDepth.size() - 1] ]

for example to get its size and position so you can check against the mouse.

You would then iterate this depth sorted list when finding out where you clicked, and the first window you find will be the top window your mouse is on top of, so now you can move the index of the window in the depth list to the front of the depth list (and fill the hole you created)

o3o

Instead of storing the window pointers in a vector, store them in the list. Mouse testing is then done from top to bottom, and drawing is done from bottom to top. Moving a window to the top requires that the window be removed and added to the front of the list.

On an unrelated note, the code you posted is somewhat hard to follow because of all the nested code blocks. Try to make it more flat. For example,


if(!WindowList->WindowItemList[itemnum]->GetActive()){continue;}else{/*do stuff*/}
is equivalent to
if(!WindowList->WindowItemList[itemnum]->GetActive())    continue;/*do stuff*/

Thank you so much for your answer :) I will try to make this work. And thanks for your critique on my code blocks.

edit: I got it working thanks to you :) Happy times!

This topic is closed to new replies.

Advertisement