guiCanvas_ScriptBinding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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->setCursor(curs);
  136. }
  137. /*!
  138. */
  139. ConsoleMethodWithDocs( GuiCanvas, renderFront, ConsoleVoid, 3, 3, (bool enable))
  140. {
  141. Canvas->setRenderFront(dAtob(argv[2]));
  142. }
  143. /*!
  144. */
  145. ConsoleMethodWithDocs( GuiCanvas, showCursor, ConsoleVoid, 2, 2, ())
  146. {
  147. Canvas->showCursor(true);
  148. }
  149. /*!
  150. */
  151. ConsoleMethodWithDocs( GuiCanvas, hideCursor, ConsoleVoid, 2, 2, ())
  152. {
  153. Canvas->showCursor(false);
  154. }
  155. /*!
  156. */
  157. ConsoleMethodWithDocs( GuiCanvas, isCursorOn, ConsoleBool, 2, 2, ())
  158. {
  159. return Canvas->isCursorON();
  160. }
  161. /*!
  162. */
  163. ConsoleMethodWithDocs( GuiCanvas, setDoubleClickDelay, ConsoleVoid, 3, 3, ())
  164. {
  165. Canvas->setDoubleClickTime(dAtoi(argv[2]));
  166. }
  167. /*!
  168. */
  169. ConsoleMethodWithDocs( GuiCanvas, setDoubleClickMoveBuffer, ConsoleVoid, 4, 4, ())
  170. {
  171. Canvas->setDoubleClickWidth(dAtoi(argv[2]));
  172. Canvas->setDoubleClickHeight(dAtoi(argv[3]));
  173. }
  174. /*! Use the repaint method to force the canvas to redraw all elements.
  175. @return No return value
  176. */
  177. ConsoleMethodWithDocs( GuiCanvas, repaint, ConsoleVoid, 2, 2, ())
  178. {
  179. Canvas->paint();
  180. }
  181. /*! Use the reset method to reset the current canvas update region.
  182. @return No return value
  183. */
  184. ConsoleMethodWithDocs( GuiCanvas, reset, ConsoleVoid, 2, 2, ())
  185. {
  186. Canvas->resetUpdateRegions();
  187. }
  188. /*! Use the getCursorPos method to retrieve the current position of the mouse pointer.
  189. @return Returns a vector containing the ???x y??? coordinates of the cursor in the canvas
  190. */
  191. ConsoleMethodWithDocs( GuiCanvas, getCursorPos, ConsoleString, 2, 2, ())
  192. {
  193. Point2I pos = Canvas->getCursorPos();
  194. char * ret = Con::getReturnBuffer(32);
  195. dSprintf(ret, 32, "%d %d", pos.x, pos.y);
  196. return(ret);
  197. }
  198. /*! Use the setCursorPos method to set the position of the cursor in the cavas.
  199. @param position An \x y\ position vector specifying the new location of the cursor.
  200. @return No return value
  201. */
  202. ConsoleMethodWithDocs( GuiCanvas, setCursorPos, ConsoleVoid, 3, 4, ( ))
  203. {
  204. Point2I pos(0,0);
  205. if(argc == 4)
  206. pos.set(dAtoi(argv[2]), dAtoi(argv[3]));
  207. else
  208. dSscanf(argv[2], "%d %d", &pos.x, &pos.y);
  209. Canvas->setCursorPos(pos);
  210. }
  211. /*! Gets the gui control under the mouse.
  212. */
  213. ConsoleMethodWithDocs( GuiCanvas, getMouseControl, ConsoleInt, 2, 2, ())
  214. {
  215. GuiControl* control = object->getMouseControl();
  216. if (control)
  217. return control->getId();
  218. return NULL;
  219. }
  220. //-----------------------------------------------------------------------------
  221. /*! Sets the background color for the canvas.
  222. @param red The red value.
  223. @param green The green value.
  224. @param blue The blue value.
  225. @param alpha The alpha value.
  226. @return No return Value.
  227. */
  228. ConsoleMethodWithDocs(GuiCanvas, setBackgroundColor, ConsoleVoid, 3, 6, (float red, float green, float blue, [float alpha = 1.0]))
  229. {
  230. // The colors.
  231. F32 red;
  232. F32 green;
  233. F32 blue;
  234. F32 alpha = 1.0f;
  235. // Space separated.
  236. if (argc == 3)
  237. {
  238. // Grab the element count.
  239. const U32 elementCount = Utility::mGetStringElementCount(argv[2]);
  240. // Has a single argument been specified?
  241. if ( elementCount == 1 )
  242. {
  243. object->setDataField( StringTable->insert("BackgroundColor"), NULL, argv[2] );
  244. return;
  245. }
  246. // ("R G B [A]")
  247. if ((elementCount == 3) || (elementCount == 4))
  248. {
  249. // Extract the color.
  250. red = dAtof(Utility::mGetStringElement(argv[2], 0));
  251. green = dAtof(Utility::mGetStringElement(argv[2], 1));
  252. blue = dAtof(Utility::mGetStringElement(argv[2], 2));
  253. // Grab the alpha if it's there.
  254. if (elementCount > 3)
  255. alpha = dAtof(Utility::mGetStringElement(argv[2], 3));
  256. }
  257. // Invalid.
  258. else
  259. {
  260. Con::warnf("GuiCanvas::setBackgroundColor() - Invalid Number of parameters!");
  261. return;
  262. }
  263. }
  264. // (R, G, B)
  265. else if (argc >= 5)
  266. {
  267. red = dAtof(argv[2]);
  268. green = dAtof(argv[3]);
  269. blue = dAtof(argv[4]);
  270. // Grab the alpha if it's there.
  271. if (argc > 5)
  272. alpha = dAtof(argv[5]);
  273. }
  274. // Invalid.
  275. else
  276. {
  277. Con::warnf("GuiCanvas::setBackgroundColor() - Invalid Number of parameters!");
  278. return;
  279. }
  280. // Set background color.
  281. object->setBackgroundColor(ColorF(red, green, blue, alpha) );
  282. }
  283. //-----------------------------------------------------------------------------
  284. /*! Gets the background color for the canvas.
  285. @return (float red / float green / float blue / float alpha) The background color for the canvas.
  286. */
  287. ConsoleMethodWithDocs(GuiCanvas, getBackgroundColor, ConsoleString, 2, 2, (...))
  288. {
  289. // Get the background color.
  290. const ColorF& color = object->getBackgroundColor();
  291. // Fetch color name.
  292. StringTableEntry colorName = StockColor::name( color );
  293. // Return the color name if it's valid.
  294. if ( colorName != StringTable->EmptyString )
  295. return colorName;
  296. // Create Returnable Buffer.
  297. char* pBuffer = Con::getReturnBuffer(64);
  298. // Format Buffer.
  299. dSprintf(pBuffer, 64, "%g %g %g %g", color.red, color.green, color.blue, color.alpha );
  300. // Return buffer.
  301. return pBuffer;
  302. }
  303. //-----------------------------------------------------------------------------
  304. /*! Sets whether to use the canvas background color or not.
  305. @param useBackgroundColor Whether to use the canvas background color or not.
  306. @return No return value.
  307. */
  308. ConsoleMethodWithDocs(GuiCanvas, setUseBackgroundColor, ConsoleVoid, 3, 3, (...))
  309. {
  310. // Fetch flag.
  311. const bool useBackgroundColor = dAtob(argv[2]);
  312. // Set the flag.
  313. object->setUseBackgroundColor( useBackgroundColor );
  314. }
  315. //-----------------------------------------------------------------------------
  316. /*! Gets whether the canvas background color is in use or not.
  317. @return Whether the canvas background color is in use or not.
  318. */
  319. ConsoleMethodWithDocs(GuiCanvas, getUseBackgroundColor, ConsoleBool, 2, 2, (...))
  320. {
  321. // Get the flag.
  322. return object->getUseBackgroundColor();
  323. }
  324. ConsoleMethodGroupEndWithDocs(GuiCanvas)
  325. /*! Use the createCanvas function to initialize the canvas.
  326. @return Returns true on success, false on failure.
  327. @sa createEffectCanvas
  328. */
  329. ConsoleFunctionWithDocs( createCanvas, ConsoleBool, 2, 2, ( WindowTitle ))
  330. {
  331. AssertISV(!Canvas, "CreateCanvas: canvas has already been instantiated");
  332. Platform::initWindow(Point2I(MIN_RESOLUTION_X, MIN_RESOLUTION_Y), argv[1]);
  333. if (!Video::getResolutionList())
  334. return false;
  335. // create the canvas, and add it to the manager
  336. Canvas = new GuiCanvas();
  337. Canvas->registerObject("Canvas"); // automatically adds to GuiGroup
  338. return true;
  339. }
  340. /*! Sets the title to the provided string
  341. @param windowTitle The desired title
  342. @return No Return Value
  343. */
  344. ConsoleFunctionWithDocs( setCanvasTitle, ConsoleVoid, 2, 2, (string windowTitle))
  345. {
  346. Platform::setWindowTitle( argv[1] );
  347. }
  348. /*!
  349. Take a screenshot.
  350. @param format One of JPEG or PNG.
  351. */
  352. ConsoleFunctionWithDocs(screenShot, ConsoleVoid, 3, 3, (string file, string format))
  353. {
  354. #ifndef TORQUE_OS_IOS
  355. // PUAP -Mat no screenshots on iPhone can do it from Xcode
  356. FileStream fStream;
  357. if(!fStream.open(argv[1], FileStream::Write))
  358. {
  359. Con::printf("Failed to open file '%s'.", argv[1]);
  360. return;
  361. }
  362. glReadBuffer(GL_FRONT);
  363. Point2I extent = Canvas->getExtent();
  364. U8 * pixels = new U8[extent.x * extent.y * 4];
  365. glReadPixels(0, 0, extent.x, extent.y, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  366. GBitmap * bitmap = new GBitmap;
  367. bitmap->allocateBitmap(U32(extent.x), U32(extent.y));
  368. // flip the rows
  369. for(U32 y = 0; y < (U32)extent.y; y++)
  370. dMemcpy(bitmap->getAddress(0, extent.y - y - 1), pixels + y * extent.x * 3, U32(extent.x * 3));
  371. if ( dStrcmp( argv[2], "JPEG" ) == 0 )
  372. bitmap->writeJPEG(fStream);
  373. else if( dStrcmp( argv[2], "PNG" ) == 0)
  374. bitmap->writePNG(fStream);
  375. else
  376. bitmap->writePNG(fStream);
  377. fStream.close();
  378. delete [] pixels;
  379. delete bitmap;
  380. #endif
  381. }