Added support for grouping widgets.

This commit is contained in:
dfighter1985 2014-10-11 00:42:43 +02:00
parent 0aead6db70
commit 6c05ced558
7 changed files with 127 additions and 4 deletions

View file

@ -333,6 +333,12 @@ namespace NLGUI
// Blows up the group, moves it's children to it's parent
bool explode();
/// Adjusts the group's size so that all elements are fully inside the borders
void spanElements();
/// Aligns the elements - used for forming groups
void alignElements();
protected:
void makeNewClip (sint32 &oldClipX, sint32 &oldClipY, sint32 &oldClipW, sint32 &oldClipH);

View file

@ -524,7 +524,8 @@ namespace NLGUI
CInterfaceElement* addWidgetToGroup( std::string &group, std::string &widgetClass, std::string &widgetName );
void setGroupSelection( bool b ){ groupSelection = b; }
void setGroupSelection( bool b ){ _GroupSelection = b; }
bool groupSelection();
bool unGroupSelection();
void setMultiSelection( bool b ){ multiSelection = b; }
@ -620,8 +621,9 @@ namespace NLGUI
std::vector< IWidgetWatcher* > widgetWatchers;
std::vector< std::string > editorSelection;
bool groupSelection;
bool _GroupSelection;
bool multiSelection;
uint32 _WidgetCount;
};
}

View file

@ -2594,5 +2594,51 @@ namespace NLGUI
return true;
}
void CInterfaceGroup::spanElements()
{
sint32 minx = std::numeric_limits< sint32 >::max();
sint32 miny = std::numeric_limits< sint32 >::max();
sint32 maxx = std::numeric_limits< sint32 >::min();
sint32 maxy = std::numeric_limits< sint32 >::min();
sint32 tlx,tly,brx,bry;
// Find the min and max coordinates of the elements
for( int i = 0; i < _EltOrder.size(); i++ )
{
CViewBase *v = _EltOrder[ i ];
v->getHSCoords( Hotspot_TL, tlx, tly );
v->getHSCoords( Hotspot_BR, brx, bry );
if( tlx < minx )
minx = tlx;
if( brx > maxx )
maxx = brx;
if( bry < miny )
miny = bry;
if( tly > maxy )
maxy = tly;
}
// Set the position and the width and height based on these coords
setW( maxx - minx );
setH( maxy - miny );
_WReal = getW();
_HReal = getH();
_XReal = minx;
_YReal = miny;
}
void CInterfaceGroup::alignElements()
{
for( int i = 0; i < _EltOrder.size(); i++ )
{
CViewBase *v = _EltOrder[ i ];
v->alignTo( this );
}
}
}

View file

@ -2408,7 +2408,7 @@ namespace NLGUI
// This may happen when alt-tab has been used => the sheet is dragged but the left button is up
if (!CCtrlDraggable::getDraggedSheet())
{
if( CInterfaceElement::getEditorMode() && groupSelection )
if( CInterfaceElement::getEditorMode() && _GroupSelection )
{
for( sint32 i = _GroupsUnderPointer.size() - 1; i >= 0; i-- )
{
@ -3510,6 +3510,64 @@ namespace NLGUI
return v;
}
bool CWidgetManager::groupSelection()
{
std::vector< CInterfaceElement* > elms;
// Resolve the widget names
for( int i = 0; i < editorSelection.size(); i++ )
{
CInterfaceElement *e = getElementFromId( editorSelection[ i ] );
if( e != NULL )
elms.push_back( e );
}
editorSelection.clear();
if( elms.empty() )
return false;
// Create the group as the subgroup of the top window
CInterfaceGroup *g = static_cast< CInterfaceGroup* >( getParser()->createClass( "interface_group" ) );
getTopWindow()->addGroup( g );
g->setParent( getTopWindow() );
g->setIdRecurse( std::string( "group" ) + NLMISC::toString( _WidgetCount ) );
_WidgetCount++;
onWidgetAdded( g->getId() );
std::string oldId;
// Reparent the widgets to the new group
for( int i = 0; i < elms.size(); i++ )
{
CInterfaceElement *e = elms[ i ];
oldId = e->getId();
CInterfaceGroup *p = e->getParent();
if( p != NULL )
p->takeElement( e );
g->addElement( e );
e->setParent( g );
e->setParentPos( g );
e->setParentSize( g );
e->setIdRecurse( e->getShortId() );
onWidgetMoved( oldId, e->getId() );
}
elms.clear();
// Make sure widgets aren't clipped because the group isn't big enough
g->spanElements();
// Make sure widgets are aligned
g->alignElements();
// Align the new group to the top window
g->alignTo( getTopWindow() );
g->setActive( true );
return true;
}
bool CWidgetManager::unGroupSelection()
{
if( editorSelection.size() != 1 )
@ -3582,8 +3640,9 @@ namespace NLGUI
setScreenWH( 0, 0 );
groupSelection = false;
_GroupSelection = false;
multiSelection = false;
_WidgetCount = 0;
}
CWidgetManager::~CWidgetManager()

View file

@ -141,6 +141,11 @@ namespace GUIEditor
CWidgetManager::getInstance()->setGroupSelection( b );
}
void CEditorMessageProcessor::onGroup()
{
CWidgetManager::getInstance()->groupSelection();
}
void CEditorMessageProcessor::onUngroup()
{
bool ok = CWidgetManager::getInstance()->unGroupSelection();

View file

@ -39,6 +39,7 @@ namespace GUIEditor
void onDelete();
void onAdd( const QString &parentGroup, const QString &widgetType, const QString &name );
void onSetGroupSelection( bool b );
void onGroup();
void onUngroup();
void onSetMultiSelection( bool b );

View file

@ -399,6 +399,10 @@ namespace GUIEditor
connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onAddWidgetClicked() ) );
m->addAction( a );
a = new QAction( "Group", this );
connect( a, SIGNAL( triggered() ), messageProcessor, SLOT( onGroup() ) );
m->addAction( a );
a = new QAction( "Ungroup", this );
connect( a, SIGNAL( triggered() ), messageProcessor, SLOT( onUngroup() ) );
m->addAction( a );