00001 #include "gui_uiobject.hpp"
00002 #include "gui_anchor.hpp"
00003 #include "gui_frame.hpp"
00004 #include "gui_layeredregion.hpp"
00005 #include "gui_manager.hpp"
00006 #include "gui_out.hpp"
00007
00008 #include <utils_string.hpp>
00009 #include <luapp_function.hpp>
00010
00011 namespace gui
00012 {
00013 lua_glue::lua_glue(lua_State* pLua)
00014 {
00015 lua_newtable(pLua);
00016 iRef_ = luaL_ref(pLua, LUA_REGISTRYINDEX);
00017 pLua_ = pLua;
00018 }
00019
00020 lua_glue::~lua_glue()
00021 {
00022 luaL_unref(pLua_, LUA_REGISTRYINDEX, iRef_);
00023 }
00024
00025 lua_virtual_glue::lua_virtual_glue(lua_State* pLua) : lua_glue(pLua)
00026 {
00027 uiID_ = lua_tonumber(pLua, -1);
00028 manager* pGUIMgr = manager::get_manager(lua::state::get_state(pLua));
00029 pParent_ = pGUIMgr->get_uiobject(uiID_);
00030
00031 if (!pParent_)
00032 throw exception("lua_virtual_glue", "Glue missing its parent (\""+utils::to_string(uiID_)+"\") !");
00033 }
00034
00035 lua_virtual_glue::~lua_virtual_glue()
00036 {
00037 }
00038
00039 void lua_virtual_glue::notify_deleted()
00040 {
00041 pParent_ = nullptr;
00042 }
00043
00044 int lua_virtual_glue::_mark_for_copy(lua_State* pLua)
00045 {
00046 lua::function mFunc("VirtualGlue:mark_for_copy", pLua, 1);
00047 mFunc.add(0, "variable", lua::TYPE_STRING);
00048 if (mFunc.check())
00049 pParent_->mark_for_copy(mFunc.get(0)->get_string());
00050
00051 return mFunc.on_return();
00052 }
00053
00054 int lua_virtual_glue::_get_name(lua_State* pLua)
00055 {
00056 lua::function mFunc("VirtualGlue:get_name", pLua, 1);
00057
00058 mFunc.push(pParent_->get_lua_name());
00059
00060 return mFunc.on_return();
00061 }
00062
00063 int lua_virtual_glue::_get_base(lua_State* pLua)
00064 {
00065 lua::function mFunc("VirtualGlue:get_base", pLua, 1);
00066
00067 if (pParent_->get_base())
00068 {
00069 pParent_->get_base()->push_on_lua(mFunc.get_state());
00070 mFunc.notify_pushed();
00071 }
00072 else
00073 mFunc.push_nil();
00074
00075 return mFunc.on_return();
00076 }
00077
00078 lua_uiobject::lua_uiobject(lua_State* pLua) : lua_glue(pLua)
00079 {
00080 sName_ = lua_tostring(pLua, -1);
00081 manager* pGUIMgr = manager::get_manager(lua::state::get_state(pLua));
00082 pParent_ = pGUIMgr->get_uiobject_by_name(sName_);
00083
00084 if (!pParent_)
00085 throw exception("lua_uiobject", "Glue missing its parent (\""+sName_+"\") !");
00086 }
00087
00088 lua_uiobject::~lua_uiobject()
00089 {
00090 }
00091
00092 void lua_uiobject::notify_deleted()
00093 {
00094 pParent_ = nullptr;
00095 }
00096
00097 uiobject* lua_uiobject::get_parent()
00098 {
00099 return pParent_;
00100 }
00101
00102 bool lua_uiobject::check_parent_()
00103 {
00104 if (!pParent_)
00105 {
00106 gui::out << gui::warning << sName_ << " : This widget has been deleted and can no longer be used." << std::endl;
00107 return false;
00108 }
00109
00110 return true;
00111 }
00112
00113 int lua_glue::get_data_table(lua_State * pLua)
00114 {
00115 lua_getref(pLua, iRef_);
00116 return 1;
00117 }
00118
00119 int lua_uiobject::_get_alpha(lua_State* pLua)
00120 {
00121 if (!check_parent_())
00122 return 0;
00123
00124 lua::function mFunc("UIObject:get_alpha", pLua, 1);
00125
00126 mFunc.push(pParent_->get_alpha());
00127
00128 return mFunc.on_return();
00129 }
00130
00131 int lua_uiobject::_get_name(lua_State* pLua)
00132 {
00133 if (!check_parent_())
00134 return 0;
00135
00136 lua::function mFunc("UIObject:get_name", pLua, 1);
00137
00138 mFunc.push(pParent_->get_name());
00139
00140 return mFunc.on_return();
00141 }
00142
00143 int lua_uiobject::_get_object_type(lua_State* pLua)
00144 {
00145 if (!check_parent_())
00146 return 0;
00147
00148 lua::function mFunc("UIObject:get_object_type", pLua, 1);
00149
00150 mFunc.push(pParent_->get_object_type());
00151
00152 return mFunc.on_return();
00153 }
00154
00155 int lua_uiobject::_is_object_type(lua_State* pLua)
00156 {
00157 if (!check_parent_())
00158 return 0;
00159
00160 lua::function mFunc("UIObject:is_object_type", pLua, 1);
00161 mFunc.add(0, "object type", lua::TYPE_STRING);
00162 if (mFunc.check())
00163 {
00164 mFunc.push(pParent_->is_object_type(mFunc.get(0)->get_string()));
00165 }
00166
00167 return mFunc.on_return();
00168 }
00169
00170 int lua_uiobject::_set_alpha(lua_State* pLua)
00171 {
00172 if (!check_parent_())
00173 return 0;
00174
00175 lua::function mFunc("UIObject:set_alpha", pLua);
00176 mFunc.add(0, "alpha", lua::TYPE_NUMBER);
00177 if (mFunc.check())
00178 {
00179 pParent_->set_alpha(mFunc.get(0)->get_number());
00180 }
00181
00182 return mFunc.on_return();
00183 }
00184
00185 int lua_uiobject::_clear_all_points(lua_State* pLua)
00186 {
00187 if (!check_parent_())
00188 return 0;
00189
00190 lua::function mFunc("UIObject:clear_all_points", pLua);
00191
00192 pParent_->clear_all_points();
00193
00194 return mFunc.on_return();
00195 }
00196
00197 int lua_uiobject::_get_bottom(lua_State* pLua)
00198 {
00199 if (!check_parent_())
00200 return 0;
00201
00202 lua::function mFunc("UIObject:get_bottom", pLua, 1);
00203
00204 mFunc.push(pParent_->get_bottom());
00205
00206 return mFunc.on_return();
00207 }
00208
00209 int lua_uiobject::_get_center(lua_State* pLua)
00210 {
00211 if (!check_parent_())
00212 return 0;
00213
00214 lua::function mFunc("UIObject:get_center", pLua, 2);
00215
00216 vector2<int> mP = pParent_->get_center();
00217 mFunc.push(mP.x);
00218 mFunc.push(mP.y);
00219
00220 return mFunc.on_return();
00221 }
00222
00223 int lua_uiobject::_get_height(lua_State* pLua)
00224 {
00225 if (!check_parent_())
00226 return 0;
00227
00228 lua::function mFunc("UIObject:get_height", pLua, 1);
00229
00230 mFunc.push(pParent_->get_abs_height());
00231
00232 return mFunc.on_return();
00233 }
00234
00235 int lua_uiobject::_get_left(lua_State* pLua)
00236 {
00237 if (!check_parent_())
00238 return 0;
00239
00240 lua::function mFunc("UIObject:get_left", pLua, 1);
00241
00242 mFunc.push(pParent_->get_left());
00243
00244 return mFunc.on_return();
00245 }
00246
00247 int lua_uiobject::_get_num_point(lua_State* pLua)
00248 {
00249 if (!check_parent_())
00250 return 0;
00251
00252 lua::function mFunc("UIObject:get_num_point", pLua, 1);
00253
00254 mFunc.push(pParent_->get_num_point());
00255
00256 return mFunc.on_return();
00257 }
00258
00259 int lua_uiobject::_get_parent(lua_State* pLua)
00260 {
00261 if (!check_parent_())
00262 return 0;
00263
00264 lua::function mFunc("UIObject:get_parent", pLua, 1);
00265
00266 if (pParent_->get_parent())
00267 {
00268 pParent_->get_parent()->push_on_lua(mFunc.get_state());
00269 mFunc.notify_pushed();
00270 }
00271 else
00272 mFunc.push_nil();
00273
00274 return mFunc.on_return();
00275 }
00276
00277 int lua_uiobject::_get_base(lua_State* pLua)
00278 {
00279 if (!check_parent_())
00280 return 0;
00281
00282 lua::function mFunc("UIObject:get_base", pLua, 1);
00283
00284 if (pParent_->get_base())
00285 {
00286 pParent_->get_base()->push_on_lua(mFunc.get_state());
00287 mFunc.notify_pushed();
00288 }
00289 else
00290 mFunc.push_nil();
00291
00292 return mFunc.on_return();
00293 }
00294
00295 int lua_uiobject::_get_point(lua_State* pLua)
00296 {
00297 if (!check_parent_())
00298 return 0;
00299
00300 lua::function mFunc("UIObject:get_point", pLua, 5);
00301 mFunc.add(0, "point ID", lua::TYPE_NUMBER, true);
00302 if (mFunc.check())
00303 {
00304 const std::map<anchor_point, anchor>& lanchorList = pParent_->get_point_list();
00305 if (!lanchorList.empty())
00306 {
00307 uint uianchorID = 1;
00308 if (mFunc.is_provided(0))
00309 uianchorID = uint(mFunc.get(0)->get_number());
00310
00311 const anchor* pAnchor = nullptr;
00312 uint uiCounter = 1;
00313 std::map<anchor_point, anchor>::const_iterator iter;
00314 foreach (iter, lanchorList)
00315 {
00316 pAnchor = &iter->second;
00317 if (uiCounter == uianchorID)
00318 break;
00319 else
00320 ++uiCounter;
00321 }
00322
00323 mFunc.push(anchor::get_string_point(pAnchor->get_point()));
00324 if (pAnchor->get_parent())
00325 {
00326 pAnchor->get_parent()->push_on_lua(mFunc.get_state());
00327 mFunc.notify_pushed();
00328 }
00329 else
00330 mFunc.push_nil();
00331
00332 mFunc.push(anchor::get_string_point(pAnchor->get_parent_point()));
00333 mFunc.push(pAnchor->get_abs_offset_x());
00334 mFunc.push(pAnchor->get_abs_offset_y());
00335 }
00336 }
00337
00338 return mFunc.on_return();
00339 }
00340
00341 int lua_uiobject::_get_right(lua_State* pLua)
00342 {
00343 if (!check_parent_())
00344 return 0;
00345
00346 lua::function mFunc("UIObject:get_right", pLua, 1);
00347
00348 mFunc.push(pParent_->get_right());
00349
00350 return mFunc.on_return();
00351 }
00352
00353 int lua_uiobject::_get_top(lua_State* pLua)
00354 {
00355 if (!check_parent_())
00356 return 0;
00357
00358 lua::function mFunc("UIObject:get_top", pLua, 1);
00359
00360 mFunc.push(pParent_->get_top());
00361
00362 return mFunc.on_return();
00363 }
00364
00365 int lua_uiobject::_get_width(lua_State* pLua)
00366 {
00367 if (!check_parent_())
00368 return 0;
00369
00370 lua::function mFunc("UIObject:get_width", pLua, 1);
00371
00372 mFunc.push(pParent_->get_abs_width());
00373
00374 return mFunc.on_return();
00375 }
00376
00377 int lua_uiobject::_hide(lua_State* pLua)
00378 {
00379 if (!check_parent_())
00380 return 0;
00381
00382 lua::function mFunc("UIObject:hide", pLua);
00383
00384 pParent_->hide();
00385
00386 return mFunc.on_return();
00387 }
00388
00389 int lua_uiobject::_is_shown(lua_State* pLua)
00390 {
00391 if (!check_parent_())
00392 return 0;
00393
00394 lua::function mFunc("UIObject:is_shown", pLua, 1);
00395
00396 mFunc.push(pParent_->is_shown());
00397
00398 return mFunc.on_return();
00399 }
00400
00401 int lua_uiobject::_is_visible(lua_State* pLua)
00402 {
00403 if (!check_parent_())
00404 return 0;
00405
00406 lua::function mFunc("UIObject:is_visible", pLua, 1);
00407
00408 mFunc.push(pParent_->is_visible());
00409
00410 return mFunc.on_return();
00411 }
00412
00413 int lua_uiobject::_set_all_points(lua_State* pLua)
00414 {
00415 if (!check_parent_())
00416 return 0;
00417
00418 lua::function mFunc("UIObject:set_all_points", pLua);
00419 mFunc.add(0, "Frame name", lua::TYPE_STRING, true);
00420 mFunc.add(0, "Frame", lua::TYPE_USERDATA, true);
00421 if (mFunc.check())
00422 {
00423 utils::wptr<lua::argument> pArg = mFunc.get(0);
00424 if (pArg->is_provided())
00425 {
00426 uiobject* pFrame = nullptr;
00427 if (pArg->get_type() == lua::TYPE_STRING)
00428 pFrame = pParent_->get_manager()->get_uiobject_by_name(pArg->get_string());
00429 else
00430 {
00431 lua_uiobject* pObj = pArg->get<lua_uiobject>();
00432 if (pObj)
00433 pFrame = pObj->get_parent();
00434 }
00435 pParent_->set_all_points(pFrame);
00436 }
00437 else
00438 pParent_->set_all_points(nullptr);
00439 }
00440
00441 return mFunc.on_return();
00442 }
00443
00444 int lua_uiobject::_set_height(lua_State* pLua)
00445 {
00446 if (!check_parent_())
00447 return 0;
00448
00449 lua::function mFunc("UIObject:set_height", pLua);
00450 mFunc.add(0, "height", lua::TYPE_NUMBER);
00451 if (mFunc.check())
00452 pParent_->set_abs_height(uint(mFunc.get(0)->get_number()));
00453
00454 return mFunc.on_return();
00455 }
00456
00457 int lua_uiobject::_set_parent(lua_State* pLua)
00458 {
00459 if (!check_parent_())
00460 return 0;
00461
00462 lua::function mFunc("UIObject:set_parent", pLua);
00463 mFunc.add(0, "parent name", lua::TYPE_STRING, true);
00464 mFunc.add(0, "parent", lua::TYPE_USERDATA, true);
00465 if (mFunc.check())
00466 {
00467 utils::wptr<lua::argument> pArg = mFunc.get(0);
00468 uiobject* pParent = nullptr;
00469
00470 if (pArg->is_provided())
00471 {
00472 if (pArg->get_type() == lua::TYPE_STRING)
00473 {
00474 pParent = pParent_->get_manager()->get_uiobject_by_name(pArg->get_string());
00475 if (!pParent)
00476 return mFunc.on_return();
00477 }
00478 else
00479 {
00480 lua_uiobject* pObj = pArg->get<lua_uiobject>();
00481 if (pObj)
00482 pParent = pObj->get_parent();
00483 else
00484 return mFunc.on_return();
00485 }
00486 }
00487
00488 frame* pFrame = dynamic_cast<frame*>(pParent);
00489 if (pFrame)
00490 {
00491 frame* pOldParent = dynamic_cast<frame*>(pParent_->get_parent());
00492 pParent_->set_parent(pParent);
00493
00494 if (pParent_->is_object_type("Frame"))
00495 {
00496 frame* pThisFrame = dynamic_cast<frame*>(pParent_);
00497 if (pOldParent)
00498 pOldParent->remove_child(pThisFrame);
00499 pFrame->add_child(pThisFrame);
00500 }
00501 else
00502 {
00503 layered_region* pThisRegion = dynamic_cast<layered_region*>(pParent_);
00504 if (pOldParent)
00505 pOldParent->remove_region(pThisRegion);
00506 pFrame->add_region(pThisRegion);
00507 }
00508 }
00509 else
00510 gui::out << gui::error << mFunc.get_name() << " : Argument 1 must be a frame." << std::endl;
00511 }
00512
00513 return mFunc.on_return();
00514 }
00515
00516 int lua_uiobject::_set_point(lua_State* pLua)
00517 {
00518 if (!check_parent_())
00519 return 0;
00520
00521 lua::function mFunc("UIObject:set_point", pLua);
00522 mFunc.add(0, "point", lua::TYPE_STRING);
00523 mFunc.add(1, "parent name", lua::TYPE_STRING, true);
00524 mFunc.add(1, "parent", lua::TYPE_USERDATA, true);
00525 mFunc.add(2, "relative point", lua::TYPE_STRING, true);
00526 mFunc.add(3, "x offset", lua::TYPE_NUMBER, true);
00527 mFunc.add(4, "y offset", lua::TYPE_NUMBER, true);
00528 if (mFunc.check())
00529 {
00530
00531 anchor_point mPoint = anchor::get_anchor_point(mFunc.get(0)->get_string());
00532
00533
00534 utils::wptr<lua::argument> pArg = mFunc.get(1);
00535 uiobject* pParent = nullptr;
00536 if (pArg->is_provided())
00537 {
00538 if (pArg->get_type() == lua::TYPE_STRING)
00539 {
00540 std::string sParent = pArg->get_string();
00541 if (!utils::has_no_content(sParent))
00542 pParent = pParent_->get_manager()->get_uiobject_by_name(sParent);
00543 }
00544 else
00545 {
00546 lua_uiobject* pLuaObj = pArg->get<lua_uiobject>();
00547 if (pLuaObj)
00548 pParent = pLuaObj->get_parent();
00549 }
00550 }
00551 else
00552 pParent = pParent_->get_parent();
00553
00554
00555 anchor_point mParentPoint = mPoint;
00556 if (mFunc.is_provided(2))
00557 mParentPoint = anchor::get_anchor_point(mFunc.get(2)->get_string());
00558
00559
00560 int iAbsX = 0;
00561 if (mFunc.is_provided(3))
00562 iAbsX = int(mFunc.get(3)->get_number());
00563
00564
00565 int iAbsY = 0;
00566 if (mFunc.is_provided(4))
00567 iAbsY = int(mFunc.get(4)->get_number());
00568
00569 pParent_->set_abs_point(mPoint, pParent ? pParent->get_name() : "", mParentPoint, iAbsX, iAbsY);
00570 }
00571
00572 return mFunc.on_return();
00573 }
00574
00575 int lua_uiobject::_set_rel_point(lua_State* pLua)
00576 {
00577 if (!check_parent_())
00578 return 0;
00579
00580 lua::function mFunc("UIObject:set_rel_point", pLua);
00581 mFunc.add(0, "point", lua::TYPE_STRING);
00582 mFunc.add(1, "parent name", lua::TYPE_STRING, true);
00583 mFunc.add(1, "parent", lua::TYPE_USERDATA, true);
00584 mFunc.add(2, "relative point", lua::TYPE_STRING, true);
00585 mFunc.add(3, "x offset", lua::TYPE_NUMBER, true);
00586 mFunc.add(4, "y offset", lua::TYPE_NUMBER, true);
00587 if (mFunc.check())
00588 {
00589
00590 anchor_point mPoint = anchor::get_anchor_point(mFunc.get(0)->get_string());
00591
00592
00593 utils::wptr<lua::argument> pArg = mFunc.get(1);
00594 uiobject* pParent = nullptr;
00595 if (pArg->is_provided())
00596 {
00597 if (pArg->get_type() == lua::TYPE_STRING)
00598 {
00599 std::string sParent = pArg->get_string();
00600 if (!utils::has_no_content(sParent))
00601 pParent = pParent_->get_manager()->get_uiobject_by_name(sParent);
00602 }
00603 else
00604 {
00605 lua_uiobject* pLuaObj = pArg->get<lua_uiobject>();
00606 if (pLuaObj)
00607 pParent = pLuaObj->get_parent();
00608 }
00609 }
00610 else
00611 pParent = pParent_->get_parent();
00612
00613
00614 anchor_point mParentPoint = mPoint;
00615 if (mFunc.is_provided(2))
00616 mParentPoint = anchor::get_anchor_point(mFunc.get(2)->get_string());
00617
00618
00619 float fRelX = 0.0f;
00620 if (mFunc.is_provided(3))
00621 fRelX = mFunc.get(3)->get_number();
00622
00623
00624 float fRelY = 0.0f;
00625 if (mFunc.is_provided(4))
00626 fRelY = mFunc.get(4)->get_number();
00627
00628 pParent_->set_rel_point(mPoint, pParent ? pParent->get_name() : "", mParentPoint, fRelX, fRelY);
00629 }
00630
00631 return mFunc.on_return();
00632 }
00633
00634 int lua_uiobject::_set_width(lua_State* pLua)
00635 {
00636 if (!check_parent_())
00637 return 0;
00638
00639 lua::function mFunc("UIObject:set_width", pLua);
00640 mFunc.add(0, "width", lua::TYPE_NUMBER);
00641 if (mFunc.check())
00642 pParent_->set_abs_width(mFunc.get(0)->get_number());
00643
00644 return mFunc.on_return();
00645 }
00646
00647 int lua_uiobject::_show(lua_State* pLua)
00648 {
00649 if (!check_parent_())
00650 return 0;
00651
00652 lua::function mFunc("UIObject:show", pLua);
00653
00654 pParent_->show();
00655
00656 return mFunc.on_return();
00657 }
00658 }