• Create Account

## Gui window: code design issue

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Juliean  GDNet+

6214
Like
0Likes
Like

Posted 28 February 2013 - 07:49 AM

Hello,

for some time I've been writing on my gui-framework. It uses a signal/slot-mechanic and composition of some base-objects to display a gui using a custom render (in my case directx9). Now I've come to the point where I want my windows to being resizeable. A window would look like the one attached. If you drag on one of the left, right or bottom borders the window should change its size. I got it to work without much of a problem, but I'm concerned about whether the design for this is good or not. This is how I'm doing it right now:

//Mouse-Drag-Event
void GuiWindow::OnDrag(Vector2 vDistance)
{
//check if window is maximized?
if(HasStyleFlags(Maximized))
return;

//check for horizontal resize
if(m_vDragPosition.x <= GetX()+8)
{
//left border resize
OnResizeH(vDistance.x,true);
m_isResizing = true;
}
else if (m_vDragPosition.x >= GetX()+ m_width - 16)
{
//right border resize
OnResizeH(-vDistance.x);
m_isResizing = true;
}

//check for vertical resize
if(m_vDragPosition.y >= GetY() + m_height - 32)
{
//bottom border resize
m_height -= vDistance.y;
m_isResizing = true;
}

//if no resize, drag
if (!m_isResizing)
{
m_x -= vDistance.x;
m_y -= vDistance.y;
SigDrag(vDistance);
}

}

//Horizontal resize event
void GuiWindow::OnResizeH(int amount, bool left)
{
//if new size is too small
if(m_width + amount <= 192)
{
//adjust resize amount to fit minimal size
if ( amount < 0)
amount = m_width - 192;
if ( amount > 0)
amount = 0;
}

m_width += amount;
//if left border drag, adjust position
if (left)
m_x -= amount;
//emit resize event
SigResize(amount,0);
}


I have the mouse-drag event, which checks if the mouse is in the area of one of the borders. If it is, it will call the resize-event which will perform the resize-action. Appeared to be fine, until I wanted to implement a minimal window size (192 px). The problem now was that, upon hitting the minimal size, the mouse would move off the border area, and therefore cancel the resizing (it should only be cancelled when the mouse is released) an start dragging if the mouse is still on the window. I solved this by adding the "bool m_isResizing" attribute, which locks the dragging action until the mouse is released.

But still, if I e.g. start resizing on the right border, the move the mouse left till I hit the minimal size, and then move further left till I'm over the left border it will start resizing on that border, and so on. So I'd need a "int m_resizeState" member variable to keep track of which border I first activated, as well as some more if {} else {} chaos.

I've got a second idea though, which would involve a new "GuiWindowBorder"-object, that I would create for every border and connect its drag signal with the "OnResize"-event. The only downsides here is that it would require another object that basically has no use other than act as a "boilerplate"-object to distinguish the area I dragged. Plus, I would need to define a few new events and signals, since I'd need to split left and right resize (OnResizeLeftH, OnResizeRightH) as well as two additional signals (SigDragX, SigDragY).

Now my question: What would you consider the better design? Should I rather keep the algorythmical way I used here, or should I solve this problem using the composition approach I mentioned? I see up and downsides to both of those approaches. Its probably a trivial question, but since I'm trying to get it "right" this time (I want that gui framework to be reusable, and there for written as good as possibly and maintainable), I might as well get some experienced oppinions. What do you say?

### #2Amr0  Members

2097
Like
1Likes
Like

Posted 28 February 2013 - 04:25 PM

I don't know which signals you are defining in your system, or who exactly would receive mouse input notifications (the window object or the components which make up the window), but I will briefly describe how it's done in Windows, and hopefully this will at least give you ideas.

A window has basically two areas: client and non-client. The non-client area consists of the title bar if it has one (along with the close/minimize/maximize buttons and other stuff in the title bar), the window borders if it has any, and even the scrollbar. The client area is everything else, which is basically the "content" area of the window. When the user presses down the left mouse button, windows will inspect the window to determine whether the cursor is in the client area or the non-client area of the window. If it's in the client area, it sends a "mouse down" notification to the window, and if it's in the non-client area, it sends "non client mouse down" notification.

The default implementation in Windows of the message handler for the "non-client mouse down" event is what should be interesting to you. It goes something like this:



switch( Msg )
{
// This is the non-client mouse down notification sent
// by the OS.
case WM_NCLBUTTONDOWN:
{
HitLocation hl = DoHitTest( GetCursorLocation() );
if( hl == CLOSE_BUTTON )
CloseWindow( this );
else if( hl == MINIMIZE_BUTTON )
MinizeWindow( this );
else if( hl == BORDER_TOP || hl == BORDER_BOTTOM ||
hl == BORDER_LEFT || hl == BORDER_RIGHT )
{
// Tell the OS (in your case the GUI manager??) that we want
// mouse messages to be sent to this window even if the cursor
// is outside the window.
CaptureMouse( this );
m_dragBorder = hl;
}
}
return;

case WM_MOUSEMOVE:
if( m_dragBorder != NONE )
{
int dx = GetCursorLocation().x - prevCursorLocation.x;
int dy = GetCursorLocation().y - prevCursorLocation.y;
if( m_dragBorder == BORDER_TOP )
SetWindowPos( this, oldWindowPos.x, oldWindowPos.y - dy, oldWidth, oldHeight + dy );
else if( m_dragBorder == BORDER_BOTTOM )
SetWindowPos( this, oldWindowPos.x, oldWindowPos.y, oldWidth, oldHeight + dy );
// etc.
}
return;
}


It would be very helpful if your GUI library implements some mouse capture functionality, which enables a window to receive mouse event notifications even when the cursor is moved outside that window.

Again, I'm not saying this is how you should do it, but this is basically how it's done in Windows. I hope you will be able to get some useful ideas from this. Good luck anyway.

Edited by Amr0, 28 February 2013 - 04:28 PM.

### #3Juliean  GDNet+

6214
Like
0Likes
Like

Posted 28 February 2013 - 05:25 PM

Thanks a lot, this definately helped me out a lot. I was getting too deep in this whole signal thing I was already overusing it.

Just if you (or someone) is interested or might have some additional advice, let me briefly explain how the system works:

Mouse input is distributed via events, handled by my gui manager. On e.g. a mouse move the "OnMouseMove(Vector2 vDistance)" - Event is called for the object the mouse is currently on. Like in your example it is indeed possible to "lock" the mouse to an object even if it is not currently on it. Signals basically act as a way of binding my gui objects together - for example, when resizing the window a "SigResize(amountX, amount Y)" - signal is emitted to all registered children like the close button - in this case, to tell it to move by the X-amount to account for the resize so that it stays at the same position in the top right corner.

I see I'm not too far off, like I said, I would have overused the object/signal - thingy a little too hard, I implemented the resizing now similar to what the code you posted to me - seems just fine.

Thanks again!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.