Some more work for widget adding support. Basically the system works, just need to make sure the proper widget is instantiated, and the defaults are loaded ( so it shows up ).

This commit is contained in:
dfighter1985 2013-05-09 05:53:14 +02:00
parent 80c6617ccc
commit 954bb6a533
9 changed files with 165 additions and 14 deletions

View file

@ -382,6 +382,7 @@ namespace NLGUI
bool serializeProcs( xmlNodePtr parentNode ) const; bool serializeProcs( xmlNodePtr parentNode ) const;
bool serializePointerSettings( xmlNodePtr parentNode ) const; bool serializePointerSettings( xmlNodePtr parentNode ) const;
bool serializeKeySettings( xmlNodePtr parentNode ) const; bool serializeKeySettings( xmlNodePtr parentNode ) const;
CViewBase* createClass( const std::string &name );
}; };
} }

View file

@ -27,6 +27,7 @@
namespace NLGUI namespace NLGUI
{ {
class CInterfaceElement; class CInterfaceElement;
class CViewBase;
class CInterfaceGroup; class CInterfaceGroup;
class CInterfaceAnim; class CInterfaceAnim;
class CCtrlSheetSelection; class CCtrlSheetSelection;
@ -86,6 +87,7 @@ namespace NLGUI
virtual bool serializeProcs( xmlNodePtr parentNode ) const = 0; virtual bool serializeProcs( xmlNodePtr parentNode ) const = 0;
virtual bool serializePointerSettings( xmlNodePtr parentNode ) const = 0; virtual bool serializePointerSettings( xmlNodePtr parentNode ) const = 0;
virtual bool serializeKeySettings( xmlNodePtr parentNode ) const = 0; virtual bool serializeKeySettings( xmlNodePtr parentNode ) const = 0;
virtual CViewBase* createClass( const std::string &name ) = 0;
}; };
} }

View file

@ -0,0 +1,16 @@
#ifndef WIDGET_ADD_WATCHER
#define WIDGET_ADD_WATCHER
#include <string>
namespace NLGUI
{
class IWidgetAdditionWatcher
{
public:
virtual void widgetAdded( const std::string &name ) = 0;
};
}
#endif

View file

@ -48,6 +48,7 @@ namespace NLGUI
class CInterfaceAnim; class CInterfaceAnim;
class CProcedure; class CProcedure;
class IEditorSelectionWatcher; class IEditorSelectionWatcher;
class IWidgetAdditionWatcher;
/** /**
GUI Widget Manager GUI Widget Manager
@ -491,6 +492,12 @@ namespace NLGUI
void registerSelectionWatcher( IEditorSelectionWatcher *watcher ); void registerSelectionWatcher( IEditorSelectionWatcher *watcher );
void unregisterSelectionWatcher( IEditorSelectionWatcher *watcher ); void unregisterSelectionWatcher( IEditorSelectionWatcher *watcher );
void notifyAdditionWatchers( const std::string &widgetName );
void registerAdditionWatcher( IWidgetAdditionWatcher *watcher );
void unregisterAdditionWatcher( IWidgetAdditionWatcher *watcher );
CInterfaceElement* addWidgetToGroup( std::string &group, std::string &widgetClass, std::string &widgetName );
private: private:
CWidgetManager(); CWidgetManager();
~CWidgetManager(); ~CWidgetManager();
@ -570,6 +577,7 @@ namespace NLGUI
std::vector< INewScreenSizeHandler* > newScreenSizeHandlers; std::vector< INewScreenSizeHandler* > newScreenSizeHandlers;
std::vector< IOnWidgetsDrawnHandler* > onWidgetsDrawnHandlers; std::vector< IOnWidgetsDrawnHandler* > onWidgetsDrawnHandlers;
std::vector< IEditorSelectionWatcher* > selectionWatchers; std::vector< IEditorSelectionWatcher* > selectionWatchers;
std::vector< IWidgetAdditionWatcher* > additionWatchers;
std::string currentEditorSelection; std::string currentEditorSelection;

View file

@ -3148,5 +3148,10 @@ namespace NLGUI
return true; return true;
} }
CViewBase* CInterfaceParser::createClass( const std::string &name )
{
return NLMISC_GET_FACTORY( CViewBase, std::string ).createObject( std::string( name ) , CViewBase::TCtorParam() );
}
} }

View file

@ -34,6 +34,7 @@
#include "nel/gui/interface_expr.h" #include "nel/gui/interface_expr.h"
#include "nel/gui/reflect_register.h" #include "nel/gui/reflect_register.h"
#include "nel/gui/editor_selection_watcher.h" #include "nel/gui/editor_selection_watcher.h"
#include "nel/gui/widget_addition_watcher.h"
#include "nel/misc/events.h" #include "nel/misc/events.h"
namespace NLGUI namespace NLGUI
@ -3236,6 +3237,78 @@ namespace NLGUI
selectionWatchers.erase( itr ); selectionWatchers.erase( itr );
} }
void CWidgetManager::notifyAdditionWatchers( const std::string &widgetName )
{
std::vector< IWidgetAdditionWatcher* >::const_iterator itr = additionWatchers.begin();
while( itr != additionWatchers.end() )
{
(*itr)->widgetAdded( widgetName );
++itr;
}
}
void CWidgetManager::registerAdditionWatcher( IWidgetAdditionWatcher *watcher )
{
std::vector< IWidgetAdditionWatcher* >::const_iterator itr
= std::find( additionWatchers.begin(), additionWatchers.end(), watcher );
// already exists
if( itr != additionWatchers.end() )
return;
additionWatchers.push_back( watcher );
}
void CWidgetManager::unregisterAdditionWatcher( IWidgetAdditionWatcher *watcher )
{
std::vector< IWidgetAdditionWatcher* >::iterator itr
= std::find( additionWatchers.begin(), additionWatchers.end(), watcher );
// doesn't exist
if( itr == additionWatchers.end() )
return;
additionWatchers.erase( itr );
}
CInterfaceElement* CWidgetManager::addWidgetToGroup( std::string &group, std::string &widgetClass, std::string &widgetName )
{
// Check if this group exists
CInterfaceElement *e = getElementFromId( group );
if( e == NULL )
return NULL;
CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( e );
if( g == NULL )
return NULL;
// Check if an element already exists with that name
if( g->getElement( widgetName ) != NULL )
return NULL;
// Create and add the new widget
CViewBase *v = getParser()->createClass( "button" );
if( v == NULL )
return NULL;
v->setId( std::string( g->getId() + ":" + widgetName ) );
v->setParentPosRef( Hotspot_TL );
v->setPosRef( Hotspot_TL );
if( v->isGroup() )
g->addGroup( dynamic_cast< CInterfaceGroup* >( v ) );
else
if( v->isCtrl() )
g->addCtrl( dynamic_cast< CCtrlBase* >( v ) );
else
g->addView( v );
// Invalidate so it shows up!
v->invalidateCoords();
notifyAdditionWatchers( v->getId() );
return v;
}
CWidgetManager::CWidgetManager() CWidgetManager::CWidgetManager()
{ {

View file

@ -57,21 +57,22 @@ namespace GUIEditor
void CEditorMessageProcessor::onAdd( const QString &parentGroup, const QString &widgetType, const QString &name ) void CEditorMessageProcessor::onAdd( const QString &parentGroup, const QString &widgetType, const QString &name )
{ {
// Check if this group exists CInterfaceElement *e =
CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( std::string( parentGroup.toAscii() ) ); CWidgetManager::getInstance()->addWidgetToGroup(
std::string( parentGroup.toUtf8() ),
std::string( widgetType.toUtf8() ),
std::string( name.toUtf8() )
);
if( e == NULL ) if( e == NULL )
return; {
CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( e ); QMessageBox::critical(
if( g == NULL ) NULL,
return; tr( "Error" ),
tr( "Error adding the new widget!" ),
// Check if an element already exists with that name QMessageBox::Ok
if( g->getElement( std::string( name.toAscii() ) ) != NULL ) );
return; }
// Create and add the new widget
//CViewBase *v = NULL;
//g->addView( v );
} }
} }

View file

@ -18,6 +18,7 @@
#include "widget_hierarchy.h" #include "widget_hierarchy.h"
#include "nel/gui/interface_group.h" #include "nel/gui/interface_group.h"
#include "nel/gui/widget_manager.h" #include "nel/gui/widget_manager.h"
#include "nel/gui/widget_addition_watcher.h"
namespace namespace
{ {
@ -75,7 +76,26 @@ namespace
GUIEditor::WidgetHierarchy *h; GUIEditor::WidgetHierarchy *h;
}; };
class CWidgetAdditionWatcher : public IWidgetAdditionWatcher
{
public:
CWidgetAdditionWatcher(){ h = NULL; }
~CWidgetAdditionWatcher(){}
void widgetAdded( const std::string &name )
{
if( h != NULL )
h->onWidgetAdded( name );
}
void setWidgetHierarchy( GUIEditor::WidgetHierarchy *h ){ this->h = h; }
private:
GUIEditor::WidgetHierarchy *h;
};
CWidgetDeletionWatcher deletionWatcher; CWidgetDeletionWatcher deletionWatcher;
CWidgetAdditionWatcher additionWatcher;
} }
namespace GUIEditor namespace GUIEditor
@ -87,6 +107,7 @@ namespace GUIEditor
connect( widgetHT, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), connect( widgetHT, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
this, SLOT( onItemDblClicked( QTreeWidgetItem* ) ) ); this, SLOT( onItemDblClicked( QTreeWidgetItem* ) ) );
deletionWatcher.setWidgetHierarchy( this ); deletionWatcher.setWidgetHierarchy( this );
additionWatcher.setWidgetHierarchy( this );
} }
WidgetHierarchy::~WidgetHierarchy() WidgetHierarchy::~WidgetHierarchy()
@ -96,6 +117,7 @@ namespace GUIEditor
void WidgetHierarchy::clearHierarchy() void WidgetHierarchy::clearHierarchy()
{ {
CInterfaceElement::unregisterDeletionWatcher( &deletionWatcher ); CInterfaceElement::unregisterDeletionWatcher( &deletionWatcher );
CWidgetManager::getInstance()->unregisterAdditionWatcher( &additionWatcher );
widgetHT->clear(); widgetHT->clear();
widgetHierarchyMap.clear(); widgetHierarchyMap.clear();
} }
@ -104,6 +126,7 @@ namespace GUIEditor
{ {
clearHierarchy(); clearHierarchy();
CInterfaceElement::registerDeletionWatcher( &deletionWatcher ); CInterfaceElement::registerDeletionWatcher( &deletionWatcher );
CWidgetManager::getInstance()->registerAdditionWatcher( &additionWatcher );
CInterfaceGroup *mg = CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup ); CInterfaceGroup *mg = CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup );
if( mg != NULL ) if( mg != NULL )
@ -187,6 +210,27 @@ namespace GUIEditor
widgetHierarchyMap.erase( itr ); widgetHierarchyMap.erase( itr );
} }
void WidgetHierarchy::onWidgetAdded( const std::string &id )
{
// Get the parent's name
std::string::size_type p = id.find_last_of( ':' );
if( p == std::string::npos )
return;
std::string parentId = id.substr( 0, p );
// Do we have the parent in the hierarchy?
std::map< std::string, QTreeWidgetItem* >::iterator itr
= widgetHierarchyMap.find( parentId );
if( itr == widgetHierarchyMap.end() )
return;
// Add the new widget to the hierarchy
QTreeWidgetItem *parent = itr->second;
QTreeWidgetItem *item = new QTreeWidgetItem( parent );
item->setText( 0, makeNodeName( id ).c_str() );
widgetHierarchyMap[ id ] = item;
}
void WidgetHierarchy::getCurrentGroup( QString &g ) void WidgetHierarchy::getCurrentGroup( QString &g )
{ {
std::string s = CWidgetManager::getInstance()->getCurrentEditorSelection(); std::string s = CWidgetManager::getInstance()->getCurrentEditorSelection();

View file

@ -43,6 +43,7 @@ namespace GUIEditor
void buildHierarchy( std::string &masterGroup ); void buildHierarchy( std::string &masterGroup );
void onWidgetDeleted( const std::string &id ); void onWidgetDeleted( const std::string &id );
void onWidgetAdded( const std::string &id );
void getCurrentGroup( QString &g ); void getCurrentGroup( QString &g );