PageRenderTime 78ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/game/client/scripted_controls/lFrame.cpp

http://hl2sb-src.googlecode.com/
C++ | 495 lines | 398 code | 75 blank | 22 comment | 30 complexity | b83e3c158ec26e48a1b2904135f000b5 MD5 | raw file
  1. //===== Copyright Š 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include <cbase.h>
  8. #include <vgui/IVGui.h>
  9. #include <vgui_int.h>
  10. #include <ienginevgui.h>
  11. #include <luamanager.h>
  12. #include <vgui_controls/lPanel.h>
  13. #include <scripted_controls/lFrame.h>
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. using namespace vgui;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Constructor
  19. //-----------------------------------------------------------------------------
  20. LFrame::LFrame(Panel *parent, const char *panelName, bool showTaskbarIcon) : Frame(parent, panelName, showTaskbarIcon)
  21. {
  22. #if defined( LUA_SDK )
  23. m_nTableReference = LUA_NOREF;
  24. m_nRefCount = 0;
  25. #endif
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Destructor
  29. //-----------------------------------------------------------------------------
  30. LFrame::~LFrame()
  31. {
  32. #ifdef LUA_SDK
  33. lua_unref( L, m_nTableReference );
  34. #endif
  35. }
  36. /*
  37. ** access functions (stack -> C)
  38. */
  39. LUA_API lua_Frame *lua_toframe (lua_State *L, int idx) {
  40. PHandle *phPanel = dynamic_cast<PHandle *>((PHandle *)lua_touserdata(L, idx));
  41. if (phPanel == NULL)
  42. return NULL;
  43. return dynamic_cast<lua_Frame *>(phPanel->Get());
  44. }
  45. /*
  46. ** push functions (C -> stack)
  47. */
  48. LUA_API void lua_pushframe (lua_State *L, Frame *pFrame) {
  49. PHandle *phPanel = (PHandle *)lua_newuserdata(L, sizeof(PHandle));
  50. phPanel->Set(pFrame);
  51. luaL_getmetatable(L, "Frame");
  52. lua_setmetatable(L, -2);
  53. }
  54. LUALIB_API lua_Frame *luaL_checkframe (lua_State *L, int narg) {
  55. lua_Frame *d = lua_toframe(L, narg);
  56. if (d == NULL) /* avoid extra test when d is not 0 */
  57. luaL_argerror(L, narg, "Frame expected, got INVALID_PANEL");
  58. return d;
  59. }
  60. static int Frame_Activate (lua_State *L) {
  61. luaL_checkframe(L, 1)->Activate();
  62. return 0;
  63. }
  64. static int Frame_ActivateMinimized (lua_State *L) {
  65. luaL_checkframe(L, 1)->ActivateMinimized();
  66. return 0;
  67. }
  68. static int Frame_CanChainKeysToParent (lua_State *L) {
  69. lua_pushboolean(L, luaL_checkframe(L, 1)->CanChainKeysToParent());
  70. return 1;
  71. }
  72. static int Frame_CanStartDragging (lua_State *L) {
  73. lua_pushboolean(L, luaL_checkframe(L, 1)->CanStartDragging(luaL_checkint(L, 2), luaL_checkint(L, 3), luaL_checkint(L, 4), luaL_checkint(L, 5)));
  74. return 1;
  75. }
  76. static int Frame_ChainToAnimationMap (lua_State *L) {
  77. luaL_checkframe(L, 1)->ChainToAnimationMap();
  78. return 0;
  79. }
  80. static int Frame_ChainToMap (lua_State *L) {
  81. luaL_checkframe(L, 1)->ChainToMap();
  82. return 0;
  83. }
  84. static int Frame_Close (lua_State *L) {
  85. luaL_checkframe(L, 1)->Close();
  86. return 0;
  87. }
  88. static int Frame_CloseModal (lua_State *L) {
  89. luaL_checkframe(L, 1)->CloseModal();
  90. return 0;
  91. }
  92. static int Frame_DeletePanel (lua_State *L) {
  93. luaL_checkframe(L, 1)->DeletePanel();
  94. return 0;
  95. }
  96. static int Frame_DisableFadeEffect (lua_State *L) {
  97. luaL_checkframe(L, 1)->DisableFadeEffect();
  98. return 0;
  99. }
  100. static int Frame_DoModal (lua_State *L) {
  101. luaL_checkframe(L, 1)->DoModal();
  102. return 0;
  103. }
  104. static int Frame_FlashWindow (lua_State *L) {
  105. luaL_checkframe(L, 1)->FlashWindow();
  106. return 0;
  107. }
  108. static int Frame_FlashWindowStop (lua_State *L) {
  109. luaL_checkframe(L, 1)->FlashWindowStop();
  110. return 0;
  111. }
  112. static int Frame_GetBottomRightSize (lua_State *L) {
  113. lua_pushinteger(L, luaL_checkframe(L, 1)->GetBottomRightSize());
  114. return 1;
  115. }
  116. static int Frame_GetCaptionHeight (lua_State *L) {
  117. lua_pushinteger(L, luaL_checkframe(L, 1)->GetCaptionHeight());
  118. return 1;
  119. }
  120. static int Frame_GetClipToParent (lua_State *L) {
  121. lua_pushboolean(L, luaL_checkframe(L, 1)->GetClipToParent());
  122. return 1;
  123. }
  124. static int Frame_GetCornerSize (lua_State *L) {
  125. lua_pushinteger(L, luaL_checkframe(L, 1)->GetCornerSize());
  126. return 1;
  127. }
  128. static int Frame_GetDraggerSize (lua_State *L) {
  129. lua_pushinteger(L, luaL_checkframe(L, 1)->GetDraggerSize());
  130. return 1;
  131. }
  132. static int Frame_GetPanelBaseClassName (lua_State *L) {
  133. lua_pushstring(L, luaL_checkframe(L, 1)->GetPanelBaseClassName());
  134. return 1;
  135. }
  136. static int Frame_GetPanelClassName (lua_State *L) {
  137. lua_pushstring(L, luaL_checkframe(L, 1)->GetPanelClassName());
  138. return 1;
  139. }
  140. static int Frame_GetRefTable (lua_State *L) {
  141. LFrame *plFrame = dynamic_cast<LFrame *>(luaL_checkframe(L, 1));
  142. if (plFrame) {
  143. if (plFrame->m_nTableReference == LUA_NOREF) {
  144. lua_newtable(L);
  145. plFrame->m_nTableReference = luaL_ref(L, LUA_REGISTRYINDEX);
  146. }
  147. lua_getref(L, plFrame->m_nTableReference);
  148. }
  149. else
  150. lua_pushnil(L);
  151. return 1;
  152. }
  153. static int Frame_IsMinimized (lua_State *L) {
  154. lua_pushboolean(L, luaL_checkframe(L, 1)->IsMinimized());
  155. return 1;
  156. }
  157. static int Frame_IsMoveable (lua_State *L) {
  158. lua_pushboolean(L, luaL_checkframe(L, 1)->IsMoveable());
  159. return 1;
  160. }
  161. static int Frame_IsSizeable (lua_State *L) {
  162. lua_pushboolean(L, luaL_checkframe(L, 1)->IsSizeable());
  163. return 1;
  164. }
  165. static int Frame_IsSmallCaption (lua_State *L) {
  166. lua_pushboolean(L, luaL_checkframe(L, 1)->IsSmallCaption());
  167. return 1;
  168. }
  169. static int Frame_KB_AddBoundKey (lua_State *L) {
  170. luaL_checkframe(L, 1)->KB_AddBoundKey(luaL_checkstring(L, 2), luaL_checkint(L, 3), luaL_checkint(L, 4));
  171. return 0;
  172. }
  173. static int Frame_KB_ChainToMap (lua_State *L) {
  174. luaL_checkframe(L, 1)->KB_ChainToMap();
  175. return 0;
  176. }
  177. static int Frame_MoveToCenterOfScreen (lua_State *L) {
  178. luaL_checkframe(L, 1)->MoveToCenterOfScreen();
  179. return 0;
  180. }
  181. static int Frame_OnCommand (lua_State *L) {
  182. luaL_checkframe(L, 1)->OnCommand(luaL_checkstring(L, 2));
  183. return 0;
  184. }
  185. static int Frame_PlaceUnderCursor (lua_State *L) {
  186. luaL_checkframe(L, 1)->PlaceUnderCursor();
  187. return 0;
  188. }
  189. static int Frame_SetChainKeysToParent (lua_State *L) {
  190. luaL_checkframe(L, 1)->SetChainKeysToParent(luaL_checkboolean(L, 2));
  191. return 0;
  192. }
  193. static int Frame_SetClipToParent (lua_State *L) {
  194. luaL_checkframe(L, 1)->SetClipToParent(luaL_checkboolean(L, 2));
  195. return 0;
  196. }
  197. static int Frame_SetCloseButtonVisible (lua_State *L) {
  198. luaL_checkframe(L, 1)->SetCloseButtonVisible(luaL_checkboolean(L, 2));
  199. return 0;
  200. }
  201. static int Frame_SetDeleteSelfOnClose (lua_State *L) {
  202. luaL_checkframe(L, 1)->SetDeleteSelfOnClose(luaL_checkboolean(L, 2));
  203. return 0;
  204. }
  205. static int Frame_SetImages (lua_State *L) {
  206. luaL_checkframe(L, 1)->SetImages(luaL_checkstring(L, 2), luaL_optstring(L, 3, 0));
  207. return 0;
  208. }
  209. static int Frame_SetMaximizeButtonVisible (lua_State *L) {
  210. luaL_checkframe(L, 1)->SetMaximizeButtonVisible(luaL_checkboolean(L, 2));
  211. return 0;
  212. }
  213. static int Frame_SetMenuButtonResponsive (lua_State *L) {
  214. luaL_checkframe(L, 1)->SetMenuButtonResponsive(luaL_checkboolean(L, 2));
  215. return 0;
  216. }
  217. static int Frame_SetMenuButtonVisible (lua_State *L) {
  218. luaL_checkframe(L, 1)->SetMenuButtonVisible(luaL_checkboolean(L, 2));
  219. return 0;
  220. }
  221. static int Frame_SetMinimizeButtonVisible (lua_State *L) {
  222. luaL_checkframe(L, 1)->SetMinimizeButtonVisible(luaL_checkboolean(L, 2));
  223. return 0;
  224. }
  225. static int Frame_SetMinimizeToSysTrayButtonVisible (lua_State *L) {
  226. luaL_checkframe(L, 1)->SetMinimizeToSysTrayButtonVisible(luaL_checkboolean(L, 2));
  227. return 0;
  228. }
  229. static int Frame_SetMoveable (lua_State *L) {
  230. luaL_checkframe(L, 1)->SetMoveable(luaL_checkboolean(L, 2));
  231. return 0;
  232. }
  233. static int Frame_SetSizeable (lua_State *L) {
  234. luaL_checkframe(L, 1)->SetSizeable(luaL_checkboolean(L, 2));
  235. return 0;
  236. }
  237. static int Frame_SetSmallCaption (lua_State *L) {
  238. luaL_checkframe(L, 1)->SetSmallCaption(luaL_checkboolean(L, 2));
  239. return 0;
  240. }
  241. static int Frame_SetTitle (lua_State *L) {
  242. luaL_checkframe(L, 1)->SetTitle(luaL_checkstring(L, 2), luaL_checkboolean(L, 3));
  243. return 0;
  244. }
  245. static int Frame_SetTitleBarVisible (lua_State *L) {
  246. luaL_checkframe(L, 1)->SetTitleBarVisible(luaL_checkboolean(L, 2));
  247. return 0;
  248. }
  249. static int Frame___index (lua_State *L) {
  250. Frame *pFrame = lua_toframe(L, 1);
  251. if (pFrame == NULL) { /* avoid extra test when d is not 0 */
  252. lua_Debug ar1;
  253. lua_getstack(L, 1, &ar1);
  254. lua_getinfo(L, "fl", &ar1);
  255. lua_Debug ar2;
  256. lua_getinfo(L, ">S", &ar2);
  257. lua_pushfstring(L, "%s:%d: attempt to index an INVALID_PANEL", ar2.short_src, ar1.currentline);
  258. return lua_error(L);
  259. }
  260. LFrame *plFrame = dynamic_cast<LFrame *>(pFrame);
  261. if (plFrame && plFrame->m_nTableReference != LUA_NOREF) {
  262. lua_getref(L, plFrame->m_nTableReference);
  263. lua_pushvalue(L, 2);
  264. lua_gettable(L, -2);
  265. if (lua_isnil(L, -1)) {
  266. lua_pop(L, 1);
  267. lua_getmetatable(L, 1);
  268. lua_pushvalue(L, 2);
  269. lua_gettable(L, -2);
  270. if (lua_isnil(L, -1)) {
  271. lua_pop(L, 1);
  272. luaL_getmetatable(L, "Panel");
  273. lua_pushvalue(L, 2);
  274. lua_gettable(L, -2);
  275. }
  276. }
  277. } else {
  278. lua_getmetatable(L, 1);
  279. lua_pushvalue(L, 2);
  280. lua_gettable(L, -2);
  281. if (lua_isnil(L, -1)) {
  282. lua_pop(L, 1);
  283. luaL_getmetatable(L, "Panel");
  284. lua_pushvalue(L, 2);
  285. lua_gettable(L, -2);
  286. }
  287. }
  288. return 1;
  289. }
  290. static int Frame___newindex (lua_State *L) {
  291. Frame *pFrame = lua_toframe(L, 1);
  292. if (pFrame == NULL) { /* avoid extra test when d is not 0 */
  293. lua_Debug ar1;
  294. lua_getstack(L, 1, &ar1);
  295. lua_getinfo(L, "fl", &ar1);
  296. lua_Debug ar2;
  297. lua_getinfo(L, ">S", &ar2);
  298. lua_pushfstring(L, "%s:%d: attempt to index an INVALID_PANEL", ar2.short_src, ar1.currentline);
  299. return lua_error(L);
  300. }
  301. LFrame *plFrame = dynamic_cast<LFrame *>(pFrame);
  302. if (plFrame) {
  303. if (plFrame->m_nTableReference == LUA_NOREF) {
  304. lua_newtable(L);
  305. plFrame->m_nTableReference = luaL_ref(L, LUA_REGISTRYINDEX);
  306. }
  307. lua_getref(L, plFrame->m_nTableReference);
  308. lua_pushvalue(L, 3);
  309. lua_setfield(L, -2, luaL_checkstring(L, 2));
  310. return 0;
  311. } else {
  312. lua_Debug ar1;
  313. lua_getstack(L, 1, &ar1);
  314. lua_getinfo(L, "fl", &ar1);
  315. lua_Debug ar2;
  316. lua_getinfo(L, ">S", &ar2);
  317. lua_pushfstring(L, "%s:%d: attempt to index a non-scripted panel", ar2.short_src, ar1.currentline);
  318. return lua_error(L);
  319. }
  320. }
  321. static int Frame___gc (lua_State *L) {
  322. LFrame *plFrame = dynamic_cast<LFrame *>(lua_toframe(L, 1));
  323. if (plFrame) {
  324. --plFrame->m_nRefCount;
  325. if (plFrame->m_nRefCount <= 0) {
  326. delete plFrame;
  327. }
  328. }
  329. return 0;
  330. }
  331. static int Frame___eq (lua_State *L) {
  332. lua_pushboolean(L, lua_toframe(L, 1) == lua_toframe(L, 2));
  333. return 1;
  334. }
  335. static int Frame___tostring (lua_State *L) {
  336. Frame *pFrame = lua_toframe(L, 1);
  337. if (pFrame == NULL)
  338. lua_pushstring(L, "INVALID_PANEL");
  339. else {
  340. const char *pName = pFrame->GetName();
  341. if (Q_strcmp(pName, "") == 0)
  342. pName = "(no name)";
  343. lua_pushfstring(L, "Frame: \"%s\"", pName);
  344. }
  345. return 1;
  346. }
  347. static const luaL_Reg Framemeta[] = {
  348. {"Activate", Frame_Activate},
  349. {"ActivateMinimized", Frame_ActivateMinimized},
  350. {"CanChainKeysToParent", Frame_CanChainKeysToParent},
  351. {"CanStartDragging", Frame_CanStartDragging},
  352. {"ChainToAnimationMap", Frame_ChainToAnimationMap},
  353. {"ChainToMap", Frame_ChainToMap},
  354. {"Close", Frame_Close},
  355. {"CloseModal", Frame_CloseModal},
  356. {"DeletePanel", Frame_DeletePanel},
  357. {"DisableFadeEffect", Frame_DisableFadeEffect},
  358. {"DoModal", Frame_DoModal},
  359. {"FlashWindow", Frame_FlashWindow},
  360. {"FlashWindowStop", Frame_FlashWindowStop},
  361. {"GetBottomRightSize", Frame_GetBottomRightSize},
  362. {"GetCaptionHeight", Frame_GetCaptionHeight},
  363. {"GetClipToParent", Frame_GetClipToParent},
  364. {"GetCornerSize", Frame_GetCornerSize},
  365. {"GetDraggerSize", Frame_GetDraggerSize},
  366. {"GetPanelBaseClassName", Frame_GetPanelBaseClassName},
  367. {"GetPanelClassName", Frame_GetPanelClassName},
  368. {"GetRefTable", Frame_GetRefTable},
  369. {"IsMinimized", Frame_IsMinimized},
  370. {"IsMoveable", Frame_IsMoveable},
  371. {"IsSizeable", Frame_IsSizeable},
  372. {"IsSmallCaption", Frame_IsSmallCaption},
  373. {"KB_AddBoundKey", Frame_KB_AddBoundKey},
  374. {"KB_ChainToMap", Frame_KB_ChainToMap},
  375. {"MoveToCenterOfScreen", Frame_MoveToCenterOfScreen},
  376. {"OnCommand", Frame_OnCommand},
  377. {"PlaceUnderCursor", Frame_PlaceUnderCursor},
  378. {"SetChainKeysToParent", Frame_SetChainKeysToParent},
  379. {"SetClipToParent", Frame_SetClipToParent},
  380. {"SetCloseButtonVisible", Frame_SetCloseButtonVisible},
  381. {"SetDeleteSelfOnClose", Frame_SetDeleteSelfOnClose},
  382. {"SetImages", Frame_SetImages},
  383. {"SetMaximizeButtonVisible", Frame_SetMaximizeButtonVisible},
  384. {"SetMenuButtonResponsive", Frame_SetMenuButtonResponsive},
  385. {"SetMenuButtonVisible", Frame_SetMenuButtonVisible},
  386. {"SetMinimizeButtonVisible", Frame_SetMinimizeButtonVisible},
  387. {"SetMinimizeToSysTrayButtonVisible", Frame_SetMinimizeToSysTrayButtonVisible},
  388. {"SetMoveable", Frame_SetMoveable},
  389. {"SetSizeable", Frame_SetSizeable},
  390. {"SetSmallCaption", Frame_SetSmallCaption},
  391. {"SetTitle", Frame_SetTitle},
  392. {"SetTitleBarVisible", Frame_SetTitleBarVisible},
  393. {"__index", Frame___index},
  394. {"__newindex", Frame___newindex},
  395. {"__gc", Frame___gc},
  396. {"__eq", Frame___eq},
  397. {"__tostring", Frame___tostring},
  398. {NULL, NULL}
  399. };
  400. static int luasrc_Frame (lua_State *L) {
  401. Frame *pFrame = new LFrame(luaL_optpanel(L, 1, VGui_GetClientLuaRootPanel()), luaL_optstring(L, 2, NULL), luaL_optboolean(L, 3, true));
  402. lua_pushframe(L, pFrame);
  403. return 1;
  404. }
  405. static const luaL_Reg Frame_funcs[] = {
  406. {"Frame", luasrc_Frame},
  407. {NULL, NULL}
  408. };
  409. /*
  410. ** Open Frame object
  411. */
  412. LUALIB_API int luaopen_vgui_Frame(lua_State *L) {
  413. luaL_newmetatable(L, "Frame");
  414. luaL_register(L, NULL, Framemeta);
  415. lua_pushstring(L, "panel");
  416. lua_setfield(L, -2, "__type"); /* metatable.__type = "panel" */
  417. luaL_register(L, "vgui", Frame_funcs);
  418. lua_pop(L, 2);
  419. return 1;
  420. }