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
//TODO: add event
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?