source: UI/ui_lua.cpp @ 69

Revision 69, 3.5 KB checked in by knowknowledge, 4 years ago (diff)

ACTUALLY adds basic UI/Lua bridge - Creation of Widgets and sub-widgets.

Line 
1/*
2 * Filename      : UI/ui_lua.cpp
3 * Author(s)     : Matt Zweig (thezweig@gmail.com)
4 * Date Created  : Thursday, October 29, 2009
5 * Last Modified : Thursday, October 29, 2009
6 * Purpose       : Lua bridge for UI objects
7 * Notes         :
8 */
9 
10#include "UI/ui.h"
11#include "ui_lua.h"
12#include "ui_label.h"
13#include "ui_window.h"
14#include "ui_button.h"
15
16void UI_Lua::RegisterUI(lua_State *luaVM){
17        static const luaL_Reg uiFunctions[] = {
18                // Creation
19                {"newWindow", &UI_Lua::newWindow},
20                {"newButton", &UI_Lua::newButton},
21                {"newLabel", &UI_Lua::newLabel},
22                {"close", &UI_Lua::close},
23                {"add", &UI_Lua::add},
24                {NULL, NULL}
25        };
26        luaL_newmetatable(luaVM, "EpiarLua.UI");
27        luaL_openlib(luaVM, "EpiarLua.UI", uiFunctions,0); 
28}
29
30int UI_Lua::newWindow(lua_State *luaVM){
31        int arg;
32        int n = lua_gettop(luaVM);  // Number of arguments
33        if (n < 6)
34                return luaL_error(luaVM, "Got %d arguments expected 6 (class, x, y, w, h, caption, [ Widgets ... ])", n);
35
36        double x = luaL_checknumber (luaVM, 2);
37        double y = luaL_checknumber (luaVM, 3);
38        double w = luaL_checknumber (luaVM, 4);
39        double h = luaL_checknumber (luaVM, 5);
40        string caption = luaL_checkstring (luaVM, 6);
41
42        // Allocate memory for a pointer to object
43        Window **win = (Window**)lua_newuserdata(luaVM, sizeof(Window*));
44        *win = new Window(x,y,w,h,caption);
45        //(*win)->AddChild( new Button( 152, 262, 96, 25, "OK" ) );
46
47        // Add this Window
48        UI::Add(*win);
49
50        // Collect 'extra' widgets and Add them as children
51        for(arg=7; arg<=n;arg++){
52                Widget** widget= (Widget**)lua_touserdata(luaVM,arg);
53                (*win)->AddChild(*widget);
54        }
55       
56        return 1;
57}
58
59int UI_Lua::close(lua_State *luaVM){
60        int n = lua_gettop(luaVM);  // Number of arguments
61
62        if (n == 1) {
63                UI::Close();
64        }
65        else {
66                luaL_error(luaVM, "Got %d arguments expected 1 (class)", n);
67        }
68        return 0;
69}
70
71int UI_Lua::newButton(lua_State *luaVM){
72        int n = lua_gettop(luaVM);  // Number of arguments
73        if (n != 6)
74                return luaL_error(luaVM, "Got %d arguments expected 5 (class, x, y, w, h, caption)", n);
75
76        double x = luaL_checknumber (luaVM, 2);
77        double y = luaL_checknumber (luaVM, 3);
78        double w = luaL_checknumber (luaVM, 4);
79        double h = luaL_checknumber (luaVM, 5);
80        string caption = luaL_checkstring (luaVM, 6);
81
82        // Allocate memory for a pointer to object
83        Button **button= (Button**)lua_newuserdata(luaVM, sizeof(Button*));
84        *button = new Button(x,y,w,h,caption);
85
86        // Note: We're not putting this button anywhere!
87        //       Lua will have to do that for us.
88        //       This may be a bad idea (memory leaks from bad lua scripts)
89
90        return 1;
91}
92
93int UI_Lua::newLabel(lua_State *luaVM){
94        int n = lua_gettop(luaVM);  // Number of arguments
95        if (n != 4)
96                return luaL_error(luaVM, "Got %d arguments expected 4 (class, x, y, caption)", n);
97
98        double x = luaL_checknumber (luaVM, 2);
99        double y = luaL_checknumber (luaVM, 3);
100        string caption = luaL_checkstring (luaVM, 4);
101
102        // Allocate memory for a pointer to object
103        Label **label= (Label**)lua_newuserdata(luaVM, sizeof(Label*));
104        *label = new Label(x,y,caption);
105
106        // Note: We're not putting this Label anywhere!
107        //       Lua will have to do that for us.
108        //       This may be a bad idea (memory leaks from bad lua scripts)
109
110        return 1;
111}
112
113int UI_Lua::add(lua_State *luaVM){
114        int n = lua_gettop(luaVM);  // Number of arguments
115        if (n == 2){
116                Widget** ptrOuter = (Widget**)lua_touserdata(luaVM,1);
117                Widget** ptrInner = (Widget**)lua_touserdata(luaVM,2);
118                (*ptrOuter)->AddChild(*ptrInner);
119        } else {
120                luaL_error(luaVM, "Got %d arguments expected 2 (self, widget)", n);
121        }
122        return 0;
123}
Note: See TracBrowser for help on using the repository browser.