• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Juliean

Gui window: code design issue

2 posts in this topic

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?

 

 

 

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0