Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles/src/ceguilua/ceguilua-0.6.2/ceguilua/CEGUILua.h @ 3050

Last change on this file since 3050 was 2710, checked in by rgrieder, 16 years ago

Merged buildsystem3 containing buildsystem2 containing Adi's buildsystem branch back to the trunk.
Please update the media directory if you were not using buildsystem3 before.

  • Property svn:eol-style set to native
File size: 23.8 KB
Line 
1/***********************************************************************
2        filename: CEGUILua.h
3        created:  16/3/2005
4        author:   Tomas Lindquist Olsen
5       
6        purpose:  Defines interface for LuaScriptModule class
7*************************************************************************/
8/***************************************************************************
9 *   Copyright (C) 2004 - 2008 Paul D Turner & The CEGUI Development Team
10 *
11 *   Permission is hereby granted, free of charge, to any person obtaining
12 *   a copy of this software and associated documentation files (the
13 *   "Software"), to deal in the Software without restriction, including
14 *   without limitation the rights to use, copy, modify, merge, publish,
15 *   distribute, sublicense, and/or sell copies of the Software, and to
16 *   permit persons to whom the Software is furnished to do so, subject to
17 *   the following conditions:
18 *
19 *   The above copyright notice and this permission notice shall be
20 *   included in all copies or substantial portions of the Software.
21 *
22 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 *   OTHER DEALINGS IN THE SOFTWARE.
29 ***************************************************************************/
30#ifndef _CEGUILua_h_
31#define _CEGUILua_h_
32
33
34/*************************************************************************
35        Import / Export control macros
36*************************************************************************/
37#if (defined( __WIN32__ ) || defined( _WIN32 )) && !defined(CEGUI_STATIC)
38#   ifdef CEGUILUA_EXPORTS
39#       define CEGUILUA_API __declspec(dllexport)
40#   else
41#       if defined( __MINGW32__ )
42#           define CEGUILUA_API
43#       else
44#           define CEGUILUA_API __declspec(dllimport)
45#       endif
46#   endif
47#else
48#   define CEGUILUA_API
49#endif
50
51
52#include "CEGUIScriptModule.h"
53
54struct lua_State;
55
56// Start of CEGUI namespace section
57namespace CEGUI
58{
59
60/*!
61\brief
62        Interface for the LuaScriptModule class
63*/
64class CEGUILUA_API LuaScriptModule : public CEGUI::ScriptModule
65{
66public:
67        /*************************************************************************
68                Construction and Destruction
69        *************************************************************************/
70        /*!
71        \brief
72                Constructor for LuaScriptModule class which create a lua_State
73        */
74        LuaScriptModule();
75
76
77        /*!
78        \brief
79                Constructor for LuaScriptModule class which takes a lua_State
80
81        \param state
82                Pointer to the lua_State that the script module should attach to.
83        */
84        LuaScriptModule(lua_State* state);
85
86
87        /*!
88        \brief
89                Destructor for LuaScriptModule class.
90        */
91        ~LuaScriptModule();
92
93
94        /*************************************************************************
95                Script Execution Functions
96        *************************************************************************/
97    /*!
98    \brief
99        Execute a script file.
100
101    \param filename
102        String object holding the filename of the script file that is to be
103        executed.
104
105    \param resourceGroup
106        Resource group idendifier to be passed to the ResourceProvider when
107        loading the script file.
108    */
109    void executeScriptFile(const String& filename, const String& resourceGroup);
110
111    /*!
112    \brief
113        Execute a script file.
114
115    \param filename
116        String object holding the filename of the script file that is to be
117        executed.
118
119    \param resourceGroup
120        Resource group idendifier to be passed to the ResourceProvider when
121        loading the script file.
122
123    \param error_handler
124        String containing the name of the script fuction that will be called
125        if an error occurs while executing the handler function.  NB: This is
126        the function passed as the error handler to lua_pcall.
127    */
128    void executeScriptFile(const String& filename,
129                           const String& resourceGroup,
130                           const String& error_handler);
131
132    /*!
133    \brief
134        Execute a script file.
135
136    \param filename
137        String object holding the filename of the script file that is to be
138        executed.
139
140    \param resourceGroup
141        Resource group idendifier to be passed to the ResourceProvider when
142        loading the script file.
143
144    \param error_handler
145        integer value describing a lua registry reference to the function that
146        will be called if an error occurs while executing the handler function.
147        NB: This is the function passed as the error handler to lua_pcall.
148    */
149    void executeScriptFile(const String& filename,
150                           const String& resourceGroup,
151                           const int error_handler);
152
153    /*!
154    \brief
155        Execute a scripted global function.  The function should not take any
156        parameters and should return an integer.
157
158    \param function_name
159        String object holding the name of the function, in the global script
160        environment, that is to be executed.
161
162    \return
163        The integer value returned from the script function.
164    */
165    int executeScriptGlobal(const String& function_name);
166
167    /*!
168    \brief
169        Execute a scripted global function.  The function should not take any
170        parameters and should return an integer.
171
172    \param function_name
173        String object holding the name of the function, in the global script
174        environment, that is to be executed.
175
176    \param error_handler
177        String containing the name of the script fuction that will be called
178        if an error occurs while executing the handler function.  NB: This is
179        the function passed as the error handler to lua_pcall.
180
181    \return
182        The integer value returned from the script function.
183    */
184    int executeScriptGlobal(const String& function_name,
185                            const String& error_handler);
186
187    /*!
188    \brief
189        Execute a scripted global function.  The function should not take any
190        parameters and should return an integer.
191
192    \param function_name
193        String object holding the name of the function, in the global script
194        environment, that is to be executed.
195
196    \param error_handler
197        integer value describing a lua registry reference to the function that
198        will be called if an error occurs while executing the handler function.
199        NB: This is the function passed as the error handler to lua_pcall.
200
201    \return
202        The integer value returned from the script function.
203    */
204    int executeScriptGlobal(const String& function_name,
205                            const int error_handler);
206
207
208    /*!
209    \brief
210        Execute a scripted global 'event handler' function by looking it up by
211        name.
212
213    \param handler_name
214        String object holding the name of the scripted handler function.
215        If this string contains dots '.' it will be parsed as tables containing
216        a function field.  For example: 'mytable.subtable.func'
217
218    \param e
219        EventArgs based object that should be passed, by any appropriate means,
220        to the scripted function.
221
222    \return
223        - true if the event was handled.
224        - false if the event was not handled.
225    */
226    bool executeScriptedEventHandler(const String& handler_name,
227                                     const EventArgs& e);
228
229    /*!
230    \brief
231        Execute a scripted global 'event handler' function by looking it up by
232        name.
233
234    \param handler_name
235        String object holding the name of the scripted handler function.
236        If this string contains dots '.' it will be parsed as tables containing
237        a function field.  For example: 'mytable.subtable.func'
238
239    \param e
240        EventArgs based object that should be passed, by any appropriate means,
241        to the scripted function.
242
243    \param error_handler
244        String containing the name of the script fuction that will be called
245        if an error occurs while executing the handler function.  NB: This is
246        the function passed as the error handler to lua_pcall.
247
248    \return
249        - true if the event was handled.
250        - false if the event was not handled.
251    */
252    bool executeScriptedEventHandler(const String& handler_name,
253                                     const EventArgs& e,
254                                     const String& error_handler);
255
256    /*!
257    \brief
258        Execute a scripted global 'event handler' function by looking it up by
259        name.
260
261    \param handler_name
262        String object holding the name of the scripted handler function.
263        If this string contains dots '.' it will be parsed as tables containing
264        a function field.  For example: 'mytable.subtable.func'
265
266    \param e
267        EventArgs based object that should be passed, by any appropriate means,
268        to the scripted function.
269
270    \param error_handler
271        integer value describing a lua registry reference to the function that
272        will be called if an error occurs while executing the handler function.
273        NB: This is the function passed as the error handler to lua_pcall.
274
275    \return
276        - true if the event was handled.
277        - false if the event was not handled.
278    */
279    bool executeScriptedEventHandler(const String& handler_name,
280                                     const EventArgs& e,
281                                     const int error_handler);
282
283    /*!
284    \brief
285        Execute script code contained in the given CEGUI::String object.
286
287    \param str
288        String object holding the valid script code that should be executed.
289
290    \return
291        Nothing.
292    */
293    void executeString(const String& str);
294
295    /*!
296    \brief
297        Execute script code contained in the given CEGUI::String object.
298
299    \param str
300        String object holding the valid script code that should be executed.
301
302    \param error_handler
303        String containing the name of the script fuction that will be called
304        if an error occurs while executing the handler function.  NB: This is
305        the function passed as the error handler to lua_pcall.
306
307    \return
308        Nothing.
309    */
310    void executeString(const String& str, const String& error_handler);
311
312    /*!
313    \brief
314        Execute script code contained in the given CEGUI::String object.
315
316    \param str
317        String object holding the valid script code that should be executed.
318
319    \param error_handler
320        integer value describing a lua registry reference to the function that
321        will be called if an error occurs while executing the handler function.
322        NB: This is the function passed as the error handler to lua_pcall.
323
324    \return
325        Nothing.
326    */
327    void executeString(const String& str, const int error_handler);
328
329    /*************************************************************************
330        Event subscription
331    *************************************************************************/
332    /*!
333    \brief
334        Subscribes the named Event to a scripted funtion
335
336    \param target
337        The target EventSet for the subscription.
338
339    \param name
340        String object containing the name of the Event to subscribe to.
341
342    \param subscriber_name
343        String object containing the name of the script funtion that is to be
344        subscribed to the Event.
345
346    \return
347        Connection object that can be used to check the status of the Event
348        connection and to disconnect (unsubscribe) from the Event.
349    */
350    Event::Connection subscribeEvent(EventSet* target, const String& name,
351                                     const String& subscriber_name);
352
353    /*!
354    \brief
355        Subscribes the named Event to a scripted funtion
356
357    \param target
358        The target EventSet for the subscription.
359
360    \param name
361        String object containing the name of the Event to subscribe to.
362
363    \param subscriber_name
364        String object containing the name of the script funtion that is to be
365        subscribed to the Event.
366
367    \param error_handler
368        String containing the name of the script fuction that will be called
369        if an error occurs while executing the handler function.  NB: This is
370        the function passed as the error handler to lua_pcall.
371
372    \return
373        Connection object that can be used to check the status of the Event
374        connection and to disconnect (unsubscribe) from the Event.
375    */
376    Event::Connection subscribeEvent(EventSet* target, const String& name,
377                                     const String& subscriber_name,
378                                     const String& error_handler);
379
380    /*!
381    \brief
382        Subscribes the named Event to a scripted funtion
383
384    \param target
385        The target EventSet for the subscription.
386
387    \param name
388        String object containing the name of the Event to subscribe to.
389
390    \param subscriber_name
391        String object containing the name of the script funtion that is to be
392        subscribed to the Event.
393
394    \param error_handler
395        integer value describing a lua registry reference to the function that
396        will be called if an error occurs while executing the handler function.
397        NB: This is the function passed as the error handler to lua_pcall.
398
399    \return
400        Connection object that can be used to check the status of the Event
401        connection and to disconnect (unsubscribe) from the Event.
402    */
403    Event::Connection subscribeEvent(EventSet* target, const String& name,
404                                     const String& subscriber_name,
405                                     const int error_handler);
406
407    /*!
408    \brief
409        Subscribes the specified group of the named Event to a scripted funtion.
410
411    \param target
412        The target EventSet for the subscription.
413
414    \param name
415        String object containing the name of the Event to subscribe to.
416
417    \param group
418        Group which is to be subscribed to.  Subscription groups are called in
419        ascending order.
420
421    \param subscriber_name
422        String object containing the name of the script funtion that is to be
423        subscribed to the Event.
424
425    \return
426        Connection object that can be used to check the status of the Event
427        connection and to disconnect (unsubscribe) from the Event.
428    */
429    Event::Connection   subscribeEvent(EventSet* target, const String& name,
430                                       Event::Group group,
431                                       const String& subscriber_name);
432
433    /*!
434    \brief
435        Subscribes the specified group of the named Event to a scripted funtion.
436
437    \param target
438        The target EventSet for the subscription.
439
440    \param name
441        String object containing the name of the Event to subscribe to.
442
443    \param group
444        Group which is to be subscribed to.  Subscription groups are called in
445        ascending order.
446
447    \param subscriber_name
448        String object containing the name of the script funtion that is to be
449        subscribed to the Event.
450
451    \param error_handler
452        String containing the name of the script fuction that will be called
453        if an error occurs while executing the handler function.  NB: This is
454        the function passed as the error handler to lua_pcall.
455
456    \return
457        Connection object that can be used to check the status of the Event
458        connection and to disconnect (unsubscribe) from the Event.
459    */
460    Event::Connection subscribeEvent(EventSet* target, const String& name,
461                                     Event::Group group,
462                                     const String& subscriber_name,
463                                     const String& error_handler);
464
465    /*!
466    \brief
467        Subscribes the specified group of the named Event to a scripted funtion.
468
469    \param target
470        The target EventSet for the subscription.
471
472    \param name
473        String object containing the name of the Event to subscribe to.
474
475    \param group
476        Group which is to be subscribed to.  Subscription groups are called in
477        ascending order.
478
479    \param subscriber_name
480        String object containing the name of the script funtion that is to be
481        subscribed to the Event.
482
483    \param error_handler
484        integer value describing a lua registry reference to the function that
485        will be called if an error occurs while executing the handler function.
486        NB: This is the function passed as the error handler to lua_pcall.
487
488    \return
489        Connection object that can be used to check the status of the Event
490        connection and to disconnect (unsubscribe) from the Event.
491    */
492    Event::Connection subscribeEvent(EventSet* target, const String& name,
493                                     Event::Group group,
494                                     const String& subscriber_name,
495                                     const int error_handler);
496
497    /*************************************************************************
498        Bindings creation / destruction
499    *************************************************************************/
500    /*!
501    \brief
502        Method called during system initialisation, prior to running any scripts
503        via the ScriptModule, to enable the ScriptModule to perform any
504        operations required to complete initialisation or binding of the script
505        language to the gui system objects.
506
507    \return
508        Nothing.
509    */
510    void createBindings(void);
511
512    /*!
513    \brief
514        Method called during system destruction, after all scripts have been run
515        via the ScriptModule, to enable the ScriptModule to perform any
516        operations required to cleanup bindings of the script language to the
517        gui system objects, as set-up in the earlier createBindings call.
518
519    \return
520        Nothing.
521    */
522    void destroyBindings(void);
523
524    /*************************************************************************
525        Accessor type functions
526    *************************************************************************/
527    /*!
528    \brief
529        Method used to get a pointer to the lua_State that the script module is
530        attached to.
531
532    \return
533        A pointer to the lua_State that the script module is attached to.
534    */
535    lua_State* getLuaState(void) const {return d_state;}
536
537    /*************************************************************************
538        Lua error handler related functions
539    *************************************************************************/
540    /*!
541    \brief
542        Set the name of the lua function that will be passed as the error
543        handler in calls to lua_pcall, unless an alternative is specified
544        in some other function call.
545
546    \param error_handler_function
547        Name of the lua function to be called.  This is looked up / bound on
548        first usage.
549    */
550    void setDefaultPCallErrorHandler(const String& error_handler_function);
551
552    /*!
553    \brief
554        Set the function that will be passed as the error handler in calls to
555        lua_pcall, unless an alternative is specified in some other function
556        call.
557
558    \param function_reference
559        Lua function registry index of the function to be called.
560    */
561    void setDefaultPCallErrorHandler(int function_reference);
562
563    /*!
564    \brief
565        Return the function name string of the active error handler function.
566
567        The 'active' error handler for the LuaScriptModule is either the
568        default as specified by calling setDefaultPCallErrorHandler, or
569        whatever might have been set up by a call to the internal
570        initErrorHandlerFunc function.
571
572    \note
573        This function is really intended for use internally by other parts of
574        the lua scripting support.  Although it could be useful elsewhere for
575        advanced uses, so long as you're careful!
576    */
577    const String& getActivePCallErrorHandlerString() const;
578
579    /*!
580    \brief
581        return the lua registry index of the active error handler function.
582
583        The 'active' error handler for the LuaScriptModule is either the
584        default as specified by calling setDefaultPCallErrorHandler, or
585        whatever might have been set up by a call to the internal
586        initErrorHandlerFunc function.
587        \par This may return a value previously set by the user, or the value as
588        bound internally by the script module for an error handler specified by
589        function name.
590
591    \note
592        This function is really intended for use internally by other parts of
593        the lua scripting support.  Although it could be useful elsewhere for
594        advanced uses, so long as you're careful!
595
596    \warning
597        You should never call luaL_unref on the value returned by this function
598        unless you created tge reference in the first place, and even then only
599        after having called setDefaultPCallErrorHandler passing LUA_NOREF.
600    */
601    int getActivePCallErrorHandlerReference() const;
602
603private:
604    /*************************************************************************
605        Implementation Functions
606    *************************************************************************/
607    void setModuleIdentifierString();
608    /** Init the error handler function.  Return the lua stack index that
609     *  should be passed to lua_pcall.  NB: This should be called prior to
610     *  pushing any function parameters onto the lua stack.
611     */
612    int initErrorHandlerFunc();
613    /** Init the error handler function.  Return the lua stack index that
614     *  should be passed to lua_pcall.  NB: This should be called prior to
615     *  pushing any function parameters onto the lua stack.
616     */
617    int initErrorHandlerFunc(const String func_name);
618    /** Init the error handler function.  Return the lua stack index that
619     *  should be passed to lua_pcall.  NB: This should be called prior to
620     *  pushing any function parameters onto the lua stack.
621     */
622    int initErrorHandlerFunc(int func);
623
624    /** do any needed cleanup after having called initErrorHandlerFunc and
625     * (possible) executed a script.
626     *
627     * NB: This probably does less than you think ;)
628     */
629    void cleanupErrorHandlerFunc();
630
631    //! release any reference we might have made to an error handling function.
632    void unrefErrorFunc();
633
634    //! Implementation function that executes a scipt file.
635    void executeScriptFile_impl(const String& filename,
636                                const String& resourceGroup,
637                                const int err_idx, const int top);
638
639    //! Implementation function that executes a global script function.
640    int executeScriptGlobal_impl(const String& function_name,
641                                 const int err_idx, const int top);
642
643    //! Implementation function that executes a scripted event handler.
644    bool executeScriptedEventHandler_impl(const String& handler_name,
645                                          const EventArgs& e,
646                                          const int err_idx, const int top);
647
648    //! Implementation function that executes script contained in a String.
649    void executeString_impl(const String& str, const int err_idx, const int top);
650
651    /*************************************************************************
652        Implementation Data
653    *************************************************************************/
654    //! true when the attached lua_State was created by this script module
655    bool d_ownsState;
656    //! The lua_State that this script module uses.
657    lua_State* d_state;
658    //! Default error handler function to pass to lua_pcall.
659    String d_errFuncName;
660    //! Default registry index of the function to pass to lua_pcall.
661    int d_errFuncIndex;
662    /** Error handler function that is currently in use (as setup via a call
663        to initErrorHandlerFunc)
664    */
665    String d_activeErrFuncName;
666    /** Registry index of the function that is currently in use (as setup via a
667        call to initErrorHandlerFunc)
668    */
669    int d_activeErrFuncIndex;
670};
671
672} // namespace CEGUI
673
674#endif // end of guard _CEGUILua_h_
Note: See TracBrowser for help on using the repository browser.