00001 #include "gui_manager.hpp" 00002 #include "gui_uiobject.hpp" 00003 #include "gui_frame.hpp" 00004 #include "gui_out.hpp" 00005 #include "input.hpp" 00006 00007 #include <luapp_function.hpp> 00008 00009 namespace gui 00010 { 00014 class lua_manager 00015 { 00016 public : 00017 00018 lua_manager(lua_State* luaVM); 00019 virtual ~lua_manager(); 00020 00021 void set_manager(manager* pMgr); 00022 manager* get_manager(); 00023 00024 int get_data_table(lua_State *L); 00025 00026 static const char className[]; 00027 static const char* classList[]; 00028 static Lunar<lua_manager>::RegType methods[]; 00029 00030 protected : 00031 00032 lua_State* pLua_; 00033 int iRef_; 00034 00035 manager* pMgr_; 00036 }; 00037 00041 void manager::register_lua_manager_() 00042 { 00043 pLua_->reg<lua_manager>(); 00044 lua_manager* pLuaMgr = pLua_->push_new<lua_manager>(); 00045 pLuaMgr->set_manager(this); 00046 pLua_->set_global("_MGR"); 00047 } 00048 00049 manager* manager::get_manager(lua::state* pState) 00050 { 00051 pState->get_global("_MGR"); 00052 manager* pMgr = pState->get<lua_manager>()->get_manager(); 00053 pState->pop(); 00054 return pMgr; 00055 } 00056 00057 lua_manager::lua_manager(lua_State* pLua) 00058 { 00059 lua_newtable(pLua); 00060 iRef_ = luaL_ref(pLua, LUA_REGISTRYINDEX); 00061 pLua_ = pLua; 00062 } 00063 00064 lua_manager::~lua_manager() 00065 { 00066 luaL_unref(pLua_, LUA_REGISTRYINDEX, iRef_); 00067 } 00068 00069 void lua_manager::set_manager(manager* pMgr) 00070 { 00071 pMgr_ = pMgr; 00072 } 00073 00074 manager* lua_manager::get_manager() 00075 { 00076 return pMgr_; 00077 } 00078 00079 int lua_manager::get_data_table(lua_State* pLua) 00080 { 00081 lua_getref(pLua, iRef_); 00082 return 1; 00083 } 00084 00085 const char lua_manager::className[] = "manager"; 00086 const char* lua_manager::classList[] = {"manager", 0}; 00087 Lunar<lua_manager>::RegType lua_manager::methods[] = { 00088 {"dt", &lua_manager::get_data_table}, 00089 {0,0} 00090 }; 00091 00092 int l_set_key_binding(lua_State* pLua) 00093 { 00094 lua::function mFunc("set_key_binding", pLua); 00095 mFunc.add(0, "key", lua::TYPE_NUMBER); 00096 mFunc.add(1, "code", lua::TYPE_STRING); 00097 mFunc.add(1, "nil", lua::TYPE_NIL); 00098 mFunc.new_param_set(); 00099 mFunc.add(0, "key", lua::TYPE_NUMBER); 00100 mFunc.add(1, "modifier", lua::TYPE_NUMBER); 00101 mFunc.add(2, "code", lua::TYPE_STRING); 00102 mFunc.add(2, "nil", lua::TYPE_NIL); 00103 mFunc.new_param_set(); 00104 mFunc.add(0, "key", lua::TYPE_NUMBER); 00105 mFunc.add(1, "modifier1", lua::TYPE_NUMBER); 00106 mFunc.add(2, "modifier2", lua::TYPE_NUMBER); 00107 mFunc.add(3, "code", lua::TYPE_STRING); 00108 mFunc.add(3, "nil", lua::TYPE_NIL); 00109 00110 if (mFunc.check()) 00111 { 00112 lua::state* pState = mFunc.get_state(); 00113 pState->get_global("_MGR"); 00114 manager* pGUIMgr = pState->get<lua_manager>()->get_manager(); 00115 pState->pop(); 00116 00117 uint uiKey = mFunc.get(0)->get_number(); 00118 00119 if (mFunc.get_param_set_rank() == 0) 00120 { 00121 if (mFunc.is_provided(1) && mFunc.get(1)->get_type() == lua::TYPE_STRING) 00122 pGUIMgr->set_key_binding(uiKey, mFunc.get(1)->get_string()); 00123 else 00124 pGUIMgr->remove_key_binding(uiKey); 00125 } 00126 else if (mFunc.get_param_set_rank() == 1) 00127 { 00128 uint uiModifier = mFunc.get(1)->get_number(); 00129 00130 if (mFunc.is_provided(2) && mFunc.get(2)->get_type() == lua::TYPE_STRING) 00131 pGUIMgr->set_key_binding(uiKey, uiModifier, mFunc.get(2)->get_string()); 00132 else 00133 pGUIMgr->remove_key_binding(uiKey, uiModifier); 00134 } 00135 else 00136 { 00137 uint uiModifier1 = mFunc.get(1)->get_number(); 00138 uint uiModifier2 = mFunc.get(2)->get_number(); 00139 00140 if (mFunc.is_provided(3) && mFunc.get(3)->get_type() == lua::TYPE_STRING) 00141 pGUIMgr->set_key_binding(uiKey, uiModifier1, uiModifier2, mFunc.get(3)->get_string()); 00142 else 00143 pGUIMgr->remove_key_binding(uiKey, uiModifier1, uiModifier2); 00144 } 00145 } 00146 00147 return mFunc.on_return(); 00148 } 00149 00150 int l_create_frame(lua_State* pLua) 00151 { 00152 lua::function mFunc("create_frame", pLua, 1); 00153 mFunc.add(0, "type", lua::TYPE_STRING); 00154 mFunc.add(1, "name", lua::TYPE_STRING); 00155 mFunc.add(2, "parent", lua::TYPE_USERDATA, true); 00156 mFunc.add(2, "parent", lua::TYPE_NIL, true); 00157 mFunc.add(3, "inherits", lua::TYPE_STRING, true); 00158 00159 if (mFunc.check()) 00160 { 00161 lua::state* pState = mFunc.get_state(); 00162 std::string sType = mFunc.get(0)->get_string(); 00163 std::string sName = mFunc.get(1)->get_string(); 00164 00165 pState->get_global("_MGR"); 00166 manager* pGUIMgr = pState->get<lua_manager>()->get_manager(); 00167 pState->pop(); 00168 00169 frame* pParent = nullptr; 00170 if (mFunc.is_provided(2) && mFunc.get(2)->get_type() == lua::TYPE_USERDATA) 00171 { 00172 lua_uiobject* pObj = mFunc.get(2)->get<lua_uiobject>(); 00173 if (pObj) 00174 { 00175 pParent = dynamic_cast<frame*>(pObj->get_parent()); 00176 if (!pParent) 00177 { 00178 gui::out << gui::warning << mFunc.get_name() 00179 << "The second argument of " << mFunc.get_name() << " must be a frame " 00180 << "(got a " << pObj->get_parent()->get_object_type() << ")." << std::endl; 00181 } 00182 } 00183 } 00184 00185 std::string sInheritance; 00186 if (mFunc.get(3)->is_provided()) 00187 sInheritance = mFunc.get(3)->get_string(); 00188 00189 frame* pNewFrame = pGUIMgr->create_frame(sType, sName, pParent, sInheritance); 00190 00191 if (pNewFrame) 00192 { 00193 pNewFrame->push_on_lua(pState); 00194 mFunc.notify_pushed(); 00195 } 00196 else 00197 mFunc.push_nil(); 00198 } 00199 00200 return mFunc.on_return(); 00201 } 00202 00203 int l_delete_frame(lua_State* pLua) 00204 { 00205 lua::function mFunc("delete_frame", pLua); 00206 mFunc.add(0, "frame", lua::TYPE_USERDATA); 00207 00208 if (mFunc.check()) 00209 { 00210 lua_uiobject* pLuaObj = mFunc.get(0)->get<lua_uiobject>(); 00211 if (pLuaObj) 00212 { 00213 frame* pFrame = dynamic_cast<frame*>(pLuaObj->get_parent()); 00214 if (pFrame) 00215 { 00216 std::vector<uiobject*> lList = pFrame->clear_links(); 00217 00218 lua::state* pState = mFunc.get_state(); 00219 pState->get_global("_MGR"); 00220 manager* pGUIMgr = pState->get<lua_manager>()->get_manager(); 00221 pState->pop(); 00222 00223 std::vector<uiobject*>::iterator iterObject; 00224 foreach (iterObject, lList) 00225 pGUIMgr->remove_uiobject(*iterObject); 00226 } 00227 else 00228 { 00229 gui::out << gui::error << mFunc.get_name() << "Argument 1 must be a frame." << std::endl; 00230 return mFunc.on_return(); 00231 } 00232 } 00233 else 00234 { 00235 gui::out << gui::error << mFunc.get_name() << "Argument 1 must be a frame." << std::endl; 00236 return mFunc.on_return(); 00237 } 00238 } 00239 00240 return mFunc.on_return(); 00241 } 00242 00243 int l_get_locale(lua_State* pLua) 00244 { 00245 lua::function mFunc("get_locale", pLua, 1); 00246 00247 lua::state* pState = mFunc.get_state(); 00248 pState->get_global("_MGR"); 00249 manager* pGUIMgr = pState->get<lua_manager>()->get_manager(); 00250 pState->pop(); 00251 00252 mFunc.push(pGUIMgr->get_locale()); 00253 00254 return mFunc.on_return(); 00255 } 00256 00257 int l_log(lua_State* pLua) 00258 { 00259 lua::function mFunc("log", pLua); 00260 mFunc.add(0, "message", lua::TYPE_STRING); 00261 00262 if (mFunc.check()) 00263 gui::out << mFunc.get(0)->get_string() << std::endl; 00264 00265 return mFunc.on_return(); 00266 } 00267 }