New lua versions
[ryzomcore.git] / studio / src / plugins / gui_editor / nelgui_ctrl.cpp
blob1f9091ae42a294cbdb7b6edbb16eb400469a1d10
1 // Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013-2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "nelgui_ctrl.h"
22 #include "nel/misc/path.h"
23 #include "nel/gui/view_renderer.h"
24 #include "nel/gui/interface_group.h"
25 #include "nel/gui/widget_manager.h"
26 #include "nel/gui/action_handler.h"
27 #include "nel/gui/lua_manager.h"
28 #include "nel/gui/event_listener.h"
29 #include "nel/misc/path.h"
30 #include "nel/misc/i18n.h"
31 #include <set>
32 #include <string>
33 #include <QTimerEvent>
34 #include "editor_selection_watcher.h"
36 #include "../core/Nel3DWidget/nel3d_widget.h"
38 namespace GUIEditor
40 std::set< std::string > hwCursors;
42 NelGUICtrl::NelGUICtrl( QObject *parent ) :
43 QObject( parent )
45 timerID = 0;
46 guiLoaded = false;
47 watcher = NULL;
48 w = new Nel3DWidget();
49 eventListener = new NLGUI::CEventListener();
50 listening = false;
53 NelGUICtrl::~NelGUICtrl()
55 guiLoaded = false;
56 if( timerID != 0 )
57 killTimer( timerID );
59 NLGUI::CViewRenderer::release();
60 NLMISC::CI18N::setNoResolution( false );
62 delete w;
63 w = NULL;
66 void NelGUICtrl::init()
68 NLMISC::CI18N::setNoResolution( true );
69 NLMISC::CPath::remapExtension( "dds", "tga", true );
70 NLMISC::CPath::remapExtension( "dds", "png", true );
71 NLMISC::CPath::remapExtension( "png", "tga", true );
73 w->init();
74 w->createTextContext( "Ryzom.ttf" );
76 NLGUI::CAHManager::setEditorMode( true );
77 NLGUI::CLuaManager::setEditorMode( true );
78 NLGUI::CInterfaceElement::setEditorMode( true );
80 NLGUI::CViewRenderer::setDriver( w->getDriver() );
81 NLGUI::CViewRenderer::setTextContext( w->getTextContext() );
82 NLGUI::CViewRenderer::hwCursors = &hwCursors;
83 NLGUI::CViewRenderer::getInstance()->init();
85 CWidgetManager::getInstance()->getParser()->setEditorMode( true );
87 watcher = new CEditorSelectionWatcher();
90 bool NelGUICtrl::parse( SProjectFiles &files )
92 reset();
93 IParser *parser = CWidgetManager::getInstance()->getParser();
95 if( !loadMapFiles( files.mapFiles ) )
96 return false;
98 if( !parser->parseInterface( files.guiFiles, false ) )
99 return false;
101 CWidgetManager::getInstance()->updateAllLocalisedElements();
102 CWidgetManager::getInstance()->activateMasterGroup( files.masterGroup, true );
104 CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( files.activeGroup );
105 if( e != NULL )
106 e->setActive( true );
108 onGUILoaded();
110 return true;
113 bool NelGUICtrl::loadMapFiles( const std::vector< std::string > &v )
115 std::vector< std::string >::const_iterator itr;
116 for( itr = v.begin(); itr != v.end(); ++itr )
118 const std::string &file = *itr;
119 std::string::size_type i = file.find_last_of( '.' );
120 std::string mapFile = file.substr( 0, i );
121 mapFile.append( ".txt" );
123 if( !CViewRenderer::getInstance()->loadTextures( file, mapFile, false ) )
125 CViewRenderer::getInstance()->reset();
126 return false;
130 return true;
133 bool NelGUICtrl::createNewGUI( const std::string &project, const std::string &window )
135 reset();
136 bool ok = CWidgetManager::getInstance()->createNewGUI( project, window );
137 if( !ok )
138 return false;
140 std::string mg = std::string( "ui:" ) + project;
141 std::string ag = mg + ":" + window;
143 CWidgetManager::getInstance()->updateAllLocalisedElements();
144 CWidgetManager::getInstance()->activateMasterGroup( mg, true );
146 CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( ag );
147 if( e != NULL )
148 e->setActive( true );
150 onGUILoaded();
152 return true;
155 void NelGUICtrl::reset()
157 guiLoaded = false;
158 if( timerID != 0 )
159 killTimer( timerID );
160 timerID = 0;
161 CWidgetManager::getInstance()->unregisterSelectionWatcher( watcher );
162 CWidgetManager::getInstance()->reset();
163 CWidgetManager::getInstance()->getParser()->removeAll();
164 CViewRenderer::getInstance()->reset();
165 w->clear();
168 void NelGUICtrl::draw()
170 w->getDriver()->clearBuffers( NLMISC::CRGBA::Black );
171 CWidgetManager::getInstance()->checkCoords();
172 CWidgetManager::getInstance()->drawViews( 0 );
173 w->getDriver()->swapBuffers();
176 void NelGUICtrl::timerEvent( QTimerEvent *evnt )
178 if( evnt->timerId() == timerID )
180 if( guiLoaded )
182 w->getDriver()->EventServer.pump();
183 draw();
185 else
187 w->clear();
192 void NelGUICtrl::onGUILoaded()
194 timerID = startTimer( 25 );
195 guiLoaded = true;
196 Q_EMIT guiLoadComplete();
198 CWidgetManager::getInstance()->registerSelectionWatcher( watcher );
201 void NelGUICtrl::show()
203 if( timerID == 0 )
204 timerID = startTimer( 200 );
206 if( !listening )
208 eventListener->addToServer( &w->getDriver()->EventServer );
209 listening = true;
213 void NelGUICtrl::hide()
215 if( timerID != 0 )
217 killTimer( timerID );
218 timerID = 0;
221 if( listening )
223 eventListener->removeFromServer();
224 listening = false;
228 void NelGUICtrl::setWorkDir( const QString &dir )
230 IParser *parser = CWidgetManager::getInstance()->getParser();
231 parser->setWorkDir( std::string( dir.toUtf8().constData() ) );
234 QWidget* NelGUICtrl::getViewPort()
236 return w;