guiCanvas_ScriptBinding.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleMethodGroupBeginWithDocs(GuiCanvas, GuiControl)
  23. /*! Use the getContent method to get the ID of the control which is being used as the current canvas content.
  24. @return Returns the ID of the current canvas content (a control), or 0 meaning the canvas is empty
  25. */
  26. ConsoleMethodWithDocs( GuiCanvas, getContent, ConsoleInt, 2, 2, ())
  27. {
  28. GuiControl *ctrl = object->getContentControl();
  29. if(ctrl)
  30. return ctrl->getId();
  31. return -1;
  32. }
  33. /*! Use the setContent method to set the control identified by handle as the current canvas content.
  34. @param handle The numeric ID or name of the control to be made the canvas contents.
  35. @return No return value
  36. */
  37. ConsoleMethodWithDocs( GuiCanvas, setContent, ConsoleVoid, 3, 3, ( handle ))
  38. {
  39. GuiControl *gui = NULL;
  40. if(argv[2][0])
  41. {
  42. if (!Sim::findObject(argv[2], gui))
  43. {
  44. Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
  45. return;
  46. }
  47. }
  48. //set the new content control
  49. Canvas->setContentControl(gui);
  50. }
  51. /*! Use the pushDialog method to open a dialog on a specific canvas layer, or in the same layer the last openned dialog. Newly placed dialogs placed in a layer with another dialog(s) will overlap the prior dialog(s).
  52. @param handle The numeric ID or name of the dialog to be opened.
  53. @param layer A integer value in the range [ 0 , inf ) specifying the canvas layer to place the dialog in.
  54. @return No return value
  55. */
  56. ConsoleMethodWithDocs( GuiCanvas, pushDialog, ConsoleVoid, 3, 4, ( handle [ , layer ] ))
  57. {
  58. GuiControl *gui;
  59. if (! Sim::findObject(argv[2], gui))
  60. {
  61. Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
  62. return;
  63. }
  64. //find the layer
  65. S32 layer = 0;
  66. if (argc == 4)
  67. layer = dAtoi(argv[3]);
  68. //set the new content control
  69. Canvas->pushDialogControl(gui, layer);
  70. }
  71. /*! Use the popDialog method to remove a currently showing dialog. If no handle is provided, the top most dialog is popped.
  72. @param handle The ID or a previously pushed dialog.
  73. @return No return value.
  74. @sa pushDialog, popLayer
  75. */
  76. ConsoleMethodWithDocs( GuiCanvas, popDialog, ConsoleVoid, 2, 3, ( handle ))
  77. {
  78. // Must initialize this to NULL to avoid crash on the if (gui) statement further down [KNM | 07/28/11 | ITGB-120]
  79. //GuiControl * gui;
  80. GuiControl * gui = NULL;
  81. if (argc == 3)
  82. {
  83. if (!Sim::findObject(argv[2], gui))
  84. {
  85. Con::printf("%s(): Invalid control: %s", argv[0], argv[2]);
  86. return;
  87. }
  88. }
  89. if (gui)
  90. Canvas->popDialogControl(gui);
  91. else
  92. Canvas->popDialogControl();
  93. }
  94. /*! Use the popLayer method to remove (close) all dialogs in the specified canvas ???layer???.
  95. @param layer A integer value in the range [ 0 , inf ) specifying the canvas layer to clear.
  96. @return No return value.
  97. @sa pushDialog, popDialog
  98. */
  99. ConsoleMethodWithDocs( GuiCanvas, popLayer, ConsoleVoid, 2, 3, ( layer ))
  100. {
  101. S32 layer = 0;
  102. if (argc == 3)
  103. layer = dAtoi(argv[2]);
  104. Canvas->popDialogControl(layer);
  105. }
  106. /*! Use the cursorOn method to enable the cursor.
  107. @return No return value
  108. */
  109. ConsoleMethodWithDocs(GuiCanvas, cursorOn, ConsoleVoid, 2, 2, ())
  110. {
  111. Canvas->setCursorON(true);
  112. }
  113. /*! Use the cursorOff method to disable the cursor.
  114. @return No return value
  115. */
  116. ConsoleMethodWithDocs(GuiCanvas, cursorOff, ConsoleVoid, 2, 2, ())
  117. {
  118. Canvas->setCursorON(false);
  119. }
  120. /*! Use the setCursor method to select the current cursor.
  121. @param cursorHandle The ID of a previously defined GuiCursor object.
  122. @return No return value
  123. */
  124. ConsoleMethodWithDocs( GuiCanvas, setCursor, ConsoleVoid, 3, 3, ( cursorHandle ))
  125. {
  126. GuiCursor *curs = NULL;
  127. if(argv[2][0])
  128. {
  129. if(!Sim::findObject(argv[2], curs))
  130. {
  131. Con::printf("%s is not a valid cursor.", argv[2]);
  132. return;
  133. }
  134. }
  135. Canvas->useNativeCursor(false);
  136. Canvas->setCursor(curs);
  137. Canvas->showCursor(true);
  138. }
  139. /*! Returns the cursor to the system default.
  140. @return No return value
  141. */
  142. ConsoleMethodWithDocs(GuiCanvas, resetCursor, void, 2, 2, ())
  143. {
  144. Canvas->useNativeCursor(true);
  145. Canvas->showCursor(false);
  146. Input::refreshCursor();
  147. }
  148. /*!
  149. */
  150. ConsoleMethodWithDocs( GuiCanvas, renderFront, ConsoleVoid, 3, 3, (bool enable))
  151. {
  152. Canvas->setRenderFront(dAtob(argv[2]));
  153. }
  154. /*!
  155. */
  156. ConsoleMethodWithDocs( GuiCanvas, showCursor, ConsoleVoid, 2, 2, ())
  157. {
  158. Canvas->showCursor(true);
  159. }
  160. /*!
  161. */
  162. ConsoleMethodWithDocs( GuiCanvas, hideCursor, ConsoleVoid, 2, 2, ())
  163. {
  164. Canvas->showCursor(false);
  165. }
  166. /*!
  167. */
  168. ConsoleMethodWithDocs( GuiCanvas, isCursorOn, ConsoleBool, 2, 2, ())
  169. {
  170. return Canvas->isCursorON();
  171. }
  172. /*!
  173. */
  174. ConsoleMethodWithDocs( GuiCanvas, setDoubleClickDelay, ConsoleVoid, 3, 3, ())
  175. {
  176. Canvas->setDoubleClickTime(dAtoi(argv[2]));
  177. }
  178. /*!
  179. */
  180. ConsoleMethodWithDocs( GuiCanvas, setDoubleClickMoveBuffer, ConsoleVoid, 4, 4, ())
  181. {
  182. Canvas->setDoubleClickWidth(dAtoi(argv[2]));
  183. Canvas->setDoubleClickHeight(dAtoi(argv[3]));
  184. }
  185. /*! Use the repaint method to force the canvas to redraw all elements.
  186. @return No return value
  187. */
  188. ConsoleMethodWithDocs( GuiCanvas, repaint, ConsoleVoid, 2, 2, ())
  189. {
  190. Canvas->paint();
  191. }
  192. /*! Use the reset method to reset the current canvas update region.
  193. @return No return value
  194. */
  195. ConsoleMethodWithDocs( GuiCanvas, reset, ConsoleVoid, 2, 2, ())
  196. {
  197. Canvas->resetUpdateRegions();
  198. }
  199. /*! Use the getCursorPos method to retrieve the current position of the mouse pointer.
  200. @return Returns a vector containing the ???x y??? coordinates of the cursor in the canvas
  201. */
  202. ConsoleMethodWithDocs( GuiCanvas, getCursorPos, ConsoleString, 2, 2, ())
  203. {
  204. Point2I pos = Canvas->getCursorPos();
  205. char * ret = Con::getReturnBuffer(32);
  206. dSprintf(ret, 32, "%d %d", pos.x, pos.y);
  207. return(ret);
  208. }
  209. /*! Use the setCursorPos method to set the position of the cursor in the cavas.
  210. @param position An \x y\ position vector specifying the new location of the cursor.
  211. @return No return value
  212. */
  213. ConsoleMethodWithDocs( GuiCanvas, setCursorPos, ConsoleVoid, 3, 4, ( ))
  214. {
  215. Point2I pos(0,0);
  216. if(argc == 4)
  217. pos.set(dAtoi(argv[2]), dAtoi(argv[3]));
  218. else
  219. dSscanf(argv[2], "%d %d", &pos.x, &pos.y);
  220. Canvas->setCursorPos(pos);
  221. }
  222. /*! Gets the gui control under the mouse.
  223. */
  224. ConsoleMethodWithDocs( GuiCanvas, getMouseControl, ConsoleInt, 2, 2, ())
  225. {
  226. GuiControl* control = object->getMouseControl();
  227. if (control)
  228. return control->getId();
  229. return 0;
  230. }
  231. //-----------------------------------------------------------------------------
  232. /*! Sets the background color for the canvas.
  233. @param red The red value.
  234. @param green The green value.
  235. @param blue The blue value.
  236. @param alpha The alpha value.
  237. @return No return Value.
  238. */
  239. ConsoleMethodWithDocs(GuiCanvas, setBackgroundColor, ConsoleVoid, 3, 6, (float red, float green, float blue, [float alpha = 1.0]))
  240. {
  241. // The colors.
  242. F32 red;
  243. F32 green;
  244. F32 blue;
  245. F32 alpha = 1.0f;
  246. // Space separated.
  247. if (argc == 3)
  248. {
  249. // Grab the element count.
  250. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  251. // Has a single argument been specified?
  252. if ( elementCount == 1 )
  253. {
  254. object->setDataField( StringTable->insert("BackgroundColor"), NULL, argv[2] );
  255. return;
  256. }
  257. // ("R G B [A]")
  258. if ((elementCount == 3) || (elementCount == 4))
  259. {
  260. // Extract the color.
  261. red = dAtof(Utility::mGetStringElement(argv[2], 0));
  262. green = dAtof(Utility::mGetStringElement(argv[2], 1));
  263. blue = dAtof(Utility::mGetStringElement(argv[2], 2));
  264. // Grab the alpha if it's there.
  265. if (elementCount > 3)
  266. alpha = dAtof(Utility::mGetStringElement(argv[2], 3));
  267. }
  268. // Invalid.
  269. else
  270. {
  271. Con::warnf("GuiCanvas::setBackgroundColor() - Invalid Number of parameters!");
  272. return;
  273. }
  274. }
  275. // (R, G, B)
  276. else if (argc >= 5)
  277. {
  278. red = dAtof(argv[2]);
  279. green = dAtof(argv[3]);
  280. blue = dAtof(argv[4]);
  281. // Grab the alpha if it's there.
  282. if (argc > 5)
  283. alpha = dAtof(argv[5]);
  284. }
  285. // Invalid.
  286. else
  287. {
  288. Con::warnf("GuiCanvas::setBackgroundColor() - Invalid Number of parameters!");
  289. return;
  290. }
  291. // Set background color.
  292. object->setBackgroundColor(ColorF(red, green, blue, alpha) );
  293. }
  294. //-----------------------------------------------------------------------------
  295. /*! Gets the background color for the canvas.
  296. @return (float red / float green / float blue / float alpha) The background color for the canvas.
  297. */
  298. ConsoleMethodWithDocs(GuiCanvas, getBackgroundColor, ConsoleString, 2, 2, (...))
  299. {
  300. // Get the background color.
  301. const ColorF& color = object->getBackgroundColor();
  302. // Fetch color name.
  303. StringTableEntry colorName = StockColor::name( color );
  304. // Return the color name if it's valid.
  305. if ( colorName != StringTable->EmptyString )
  306. return colorName;
  307. // Create Returnable Buffer.
  308. char* pBuffer = Con::getReturnBuffer(64);
  309. // Format Buffer.
  310. dSprintf(pBuffer, 64, "%g %g %g %g", color.red, color.green, color.blue, color.alpha );
  311. // Return buffer.
  312. return pBuffer;
  313. }
  314. //-----------------------------------------------------------------------------
  315. /*! Sets whether to use the canvas background color or not.
  316. @param useBackgroundColor Whether to use the canvas background color or not.
  317. @return No return value.
  318. */
  319. ConsoleMethodWithDocs(GuiCanvas, setUseBackgroundColor, ConsoleVoid, 3, 3, (...))
  320. {
  321. // Fetch flag.
  322. const bool useBackgroundColor = dAtob(argv[2]);
  323. // Set the flag.
  324. object->setUseBackgroundColor( useBackgroundColor );
  325. }
  326. //-----------------------------------------------------------------------------
  327. /*! Gets whether the canvas background color is in use or not.
  328. @return Whether the canvas background color is in use or not.
  329. */
  330. ConsoleMethodWithDocs(GuiCanvas, getUseBackgroundColor, ConsoleBool, 2, 2, (...))
  331. {
  332. // Get the flag.
  333. return object->getUseBackgroundColor();
  334. }
  335. ConsoleMethodGroupEndWithDocs(GuiCanvas)
  336. /*! Use the createCanvas function to initialize the canvas.
  337. @return Returns true on success, false on failure.
  338. @sa createEffectCanvas
  339. */
  340. ConsoleFunctionWithDocs( createCanvas, ConsoleBool, 2, 2, ( WindowTitle ))
  341. {
  342. AssertISV(!Canvas, "CreateCanvas: canvas has already been instantiated");
  343. Platform::initWindow(Point2I(MIN_RESOLUTION_X, MIN_RESOLUTION_Y), argv[1]);
  344. if (!Video::getResolutionList())
  345. return false;
  346. // create the canvas, and add it to the manager
  347. Canvas = new GuiCanvas();
  348. Canvas->registerObject("Canvas"); // automatically adds to GuiGroup
  349. return true;
  350. }
  351. /*! Sets the title to the provided string
  352. @param windowTitle The desired title
  353. @return No Return Value
  354. */
  355. ConsoleFunctionWithDocs( setCanvasTitle, ConsoleVoid, 2, 2, (string windowTitle))
  356. {
  357. Platform::setWindowTitle( argv[1] );
  358. }
  359. /*!
  360. Take a screenshot.
  361. @param format One of JPEG or PNG.
  362. */
  363. ConsoleFunctionWithDocs(screenShot, ConsoleVoid, 3, 3, (string file, string format))
  364. {
  365. #if !defined(TORQUE_OS_IOS) && !defined(TORQUE_OS_ANDROID)
  366. // PUAP -Mat no screenshots on iPhone can do it from Xcode
  367. FileStream fStream;
  368. if(!fStream.open(argv[1], FileStream::Write))
  369. {
  370. Con::printf("Failed to open file '%s'.", argv[1]);
  371. return;
  372. }
  373. glReadBuffer(GL_FRONT);
  374. Point2I extent = Canvas->getExtent();
  375. U8 * pixels = new U8[extent.x * extent.y * 4];
  376. glReadPixels(0, 0, extent.x, extent.y, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  377. GBitmap * bitmap = new GBitmap;
  378. bitmap->allocateBitmap(U32(extent.x), U32(extent.y));
  379. // flip the rows
  380. for(U32 y = 0; y < (U32)extent.y; y++)
  381. dMemcpy(bitmap->getAddress(0, extent.y - y - 1), pixels + y * extent.x * 3, U32(extent.x * 3));
  382. if ( dStrcmp( argv[2], "JPEG" ) == 0 )
  383. bitmap->writeJPEG(fStream);
  384. else if( dStrcmp( argv[2], "PNG" ) == 0)
  385. bitmap->writePNG(fStream);
  386. else
  387. bitmap->writePNG(fStream);
  388. fStream.close();
  389. delete [] pixels;
  390. delete bitmap;
  391. #endif
  392. }