sprite.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*************************************************************************/
  2. /* sprite.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "sprite.h"
  31. #include "core/core_string_names.h"
  32. #include "os/os.h"
  33. #include "scene/main/viewport.h"
  34. #include "scene/scene_string_names.h"
  35. void Sprite::edit_set_pivot(const Point2 &p_pivot) {
  36. set_offset(p_pivot);
  37. }
  38. Point2 Sprite::edit_get_pivot() const {
  39. return get_offset();
  40. }
  41. bool Sprite::edit_has_pivot() const {
  42. return true;
  43. }
  44. void Sprite::_notification(int p_what) {
  45. switch (p_what) {
  46. case NOTIFICATION_DRAW: {
  47. if (texture.is_null())
  48. return;
  49. RID ci = get_canvas_item();
  50. /*
  51. texture->draw(ci,Point2());
  52. break;
  53. */
  54. Size2 s;
  55. Rect2 src_rect;
  56. if (region) {
  57. s = region_rect.size;
  58. src_rect = region_rect;
  59. } else {
  60. s = Size2(texture->get_size());
  61. s = s / Size2(hframes, vframes);
  62. src_rect.size = s;
  63. src_rect.pos.x += float(frame % hframes) * s.x;
  64. src_rect.pos.y += float(frame / hframes) * s.y;
  65. }
  66. Point2 ofs = offset;
  67. if (centered)
  68. ofs -= s / 2;
  69. if (OS::get_singleton()->get_use_pixel_snap()) {
  70. ofs = ofs.floor();
  71. }
  72. Rect2 dst_rect(ofs, s);
  73. if (hflip)
  74. dst_rect.size.x = -dst_rect.size.x;
  75. if (vflip)
  76. dst_rect.size.y = -dst_rect.size.y;
  77. texture->draw_rect_region(ci, dst_rect, src_rect, modulate);
  78. } break;
  79. }
  80. }
  81. void Sprite::set_texture(const Ref<Texture> &p_texture) {
  82. if (p_texture == texture)
  83. return;
  84. #ifdef DEBUG_ENABLED
  85. if (texture.is_valid()) {
  86. texture->disconnect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->update);
  87. }
  88. #endif
  89. texture = p_texture;
  90. #ifdef DEBUG_ENABLED
  91. if (texture.is_valid()) {
  92. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  93. texture->connect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->update);
  94. }
  95. #endif
  96. update();
  97. emit_signal("texture_changed");
  98. item_rect_changed();
  99. }
  100. Ref<Texture> Sprite::get_texture() const {
  101. return texture;
  102. }
  103. void Sprite::set_centered(bool p_center) {
  104. centered = p_center;
  105. update();
  106. item_rect_changed();
  107. }
  108. bool Sprite::is_centered() const {
  109. return centered;
  110. }
  111. void Sprite::set_offset(const Point2 &p_offset) {
  112. offset = p_offset;
  113. update();
  114. item_rect_changed();
  115. _change_notify("offset");
  116. }
  117. Point2 Sprite::get_offset() const {
  118. return offset;
  119. }
  120. void Sprite::set_flip_h(bool p_flip) {
  121. hflip = p_flip;
  122. update();
  123. }
  124. bool Sprite::is_flipped_h() const {
  125. return hflip;
  126. }
  127. void Sprite::set_flip_v(bool p_flip) {
  128. vflip = p_flip;
  129. update();
  130. }
  131. bool Sprite::is_flipped_v() const {
  132. return vflip;
  133. }
  134. void Sprite::set_region(bool p_region) {
  135. if (p_region == region)
  136. return;
  137. region = p_region;
  138. update();
  139. }
  140. bool Sprite::is_region() const {
  141. return region;
  142. }
  143. void Sprite::set_region_rect(const Rect2 &p_region_rect) {
  144. if (region_rect == p_region_rect)
  145. return;
  146. region_rect = p_region_rect;
  147. if (region)
  148. item_rect_changed();
  149. _change_notify("region_rect");
  150. }
  151. Rect2 Sprite::get_region_rect() const {
  152. return region_rect;
  153. }
  154. void Sprite::set_frame(int p_frame) {
  155. ERR_FAIL_INDEX(p_frame, vframes * hframes);
  156. if (frame != p_frame)
  157. item_rect_changed();
  158. frame = p_frame;
  159. _change_notify("frame");
  160. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  161. }
  162. int Sprite::get_frame() const {
  163. return frame;
  164. }
  165. void Sprite::set_vframes(int p_amount) {
  166. ERR_FAIL_COND(p_amount < 1);
  167. vframes = p_amount;
  168. update();
  169. item_rect_changed();
  170. _change_notify();
  171. }
  172. int Sprite::get_vframes() const {
  173. return vframes;
  174. }
  175. void Sprite::set_hframes(int p_amount) {
  176. ERR_FAIL_COND(p_amount < 1);
  177. hframes = p_amount;
  178. update();
  179. item_rect_changed();
  180. _change_notify();
  181. }
  182. int Sprite::get_hframes() const {
  183. return hframes;
  184. }
  185. void Sprite::set_modulate(const Color &p_color) {
  186. modulate = p_color;
  187. update();
  188. }
  189. Color Sprite::get_modulate() const {
  190. return modulate;
  191. }
  192. Rect2 Sprite::get_item_rect() const {
  193. if (texture.is_null())
  194. return Rect2(0, 0, 1, 1);
  195. //if (texture.is_null())
  196. // return CanvasItem::get_item_rect();
  197. Size2i s;
  198. if (region) {
  199. s = region_rect.size;
  200. } else {
  201. s = texture->get_size();
  202. s = s / Point2(hframes, vframes);
  203. }
  204. Point2 ofs = offset;
  205. if (centered)
  206. ofs -= s / 2;
  207. if (s == Size2(0, 0))
  208. s = Size2(1, 1);
  209. return Rect2(ofs, s);
  210. }
  211. void Sprite::_validate_property(PropertyInfo &property) const {
  212. if (property.name == "frame") {
  213. property.hint = PROPERTY_HINT_SPRITE_FRAME;
  214. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  215. }
  216. }
  217. void Sprite::_bind_methods() {
  218. ObjectTypeDB::bind_method(_MD("set_texture", "texture:Texture"), &Sprite::set_texture);
  219. ObjectTypeDB::bind_method(_MD("get_texture:Texture"), &Sprite::get_texture);
  220. ObjectTypeDB::bind_method(_MD("set_centered", "centered"), &Sprite::set_centered);
  221. ObjectTypeDB::bind_method(_MD("is_centered"), &Sprite::is_centered);
  222. ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &Sprite::set_offset);
  223. ObjectTypeDB::bind_method(_MD("get_offset"), &Sprite::get_offset);
  224. ObjectTypeDB::bind_method(_MD("set_flip_h", "flip_h"), &Sprite::set_flip_h);
  225. ObjectTypeDB::bind_method(_MD("is_flipped_h"), &Sprite::is_flipped_h);
  226. ObjectTypeDB::bind_method(_MD("set_flip_v", "flip_v"), &Sprite::set_flip_v);
  227. ObjectTypeDB::bind_method(_MD("is_flipped_v"), &Sprite::is_flipped_v);
  228. ObjectTypeDB::bind_method(_MD("set_region", "enabled"), &Sprite::set_region);
  229. ObjectTypeDB::bind_method(_MD("is_region"), &Sprite::is_region);
  230. ObjectTypeDB::bind_method(_MD("set_region_rect", "rect"), &Sprite::set_region_rect);
  231. ObjectTypeDB::bind_method(_MD("get_region_rect"), &Sprite::get_region_rect);
  232. ObjectTypeDB::bind_method(_MD("set_frame", "frame"), &Sprite::set_frame);
  233. ObjectTypeDB::bind_method(_MD("get_frame"), &Sprite::get_frame);
  234. ObjectTypeDB::bind_method(_MD("set_vframes", "vframes"), &Sprite::set_vframes);
  235. ObjectTypeDB::bind_method(_MD("get_vframes"), &Sprite::get_vframes);
  236. ObjectTypeDB::bind_method(_MD("set_hframes", "hframes"), &Sprite::set_hframes);
  237. ObjectTypeDB::bind_method(_MD("get_hframes"), &Sprite::get_hframes);
  238. ObjectTypeDB::bind_method(_MD("set_modulate", "modulate"), &Sprite::set_modulate);
  239. ObjectTypeDB::bind_method(_MD("get_modulate"), &Sprite::get_modulate);
  240. ADD_SIGNAL(MethodInfo("frame_changed"));
  241. ADD_SIGNAL(MethodInfo("texture_changed"));
  242. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"), _SCS("get_texture"));
  243. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "centered"), _SCS("set_centered"), _SCS("is_centered"));
  244. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "offset"), _SCS("set_offset"), _SCS("get_offset"));
  245. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "flip_h"), _SCS("set_flip_h"), _SCS("is_flipped_h"));
  246. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "flip_v"), _SCS("set_flip_v"), _SCS("is_flipped_v"));
  247. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), _SCS("set_vframes"), _SCS("get_vframes"));
  248. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), _SCS("set_hframes"), _SCS("get_hframes"));
  249. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), _SCS("set_frame"), _SCS("get_frame"));
  250. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "modulate"), _SCS("set_modulate"), _SCS("get_modulate"));
  251. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "region"), _SCS("set_region"), _SCS("is_region"));
  252. ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), _SCS("set_region_rect"), _SCS("get_region_rect"));
  253. }
  254. Sprite::Sprite() {
  255. centered = true;
  256. hflip = false;
  257. vflip = false;
  258. region = false;
  259. frame = 0;
  260. vframes = 1;
  261. hframes = 1;
  262. modulate = Color(1, 1, 1, 1);
  263. }
  264. //////////////////////////// VPSPRITE
  265. ///
  266. ///
  267. ///
  268. void ViewportSprite::edit_set_pivot(const Point2 &p_pivot) {
  269. set_offset(p_pivot);
  270. }
  271. Point2 ViewportSprite::edit_get_pivot() const {
  272. return get_offset();
  273. }
  274. bool ViewportSprite::edit_has_pivot() const {
  275. return true;
  276. }
  277. void ViewportSprite::_notification(int p_what) {
  278. switch (p_what) {
  279. case NOTIFICATION_ENTER_TREE: {
  280. if (!viewport_path.is_empty()) {
  281. Node *n = get_node(viewport_path);
  282. ERR_FAIL_COND(!n);
  283. Viewport *vp = n->cast_to<Viewport>();
  284. ERR_FAIL_COND(!vp);
  285. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  286. texture = rtt;
  287. texture->connect("changed", this, "update");
  288. item_rect_changed();
  289. }
  290. } break;
  291. case NOTIFICATION_EXIT_TREE: {
  292. if (texture.is_valid()) {
  293. texture->disconnect("changed", this, "update");
  294. texture = Ref<Texture>();
  295. }
  296. } break;
  297. case NOTIFICATION_DRAW: {
  298. if (texture.is_null())
  299. return;
  300. RID ci = get_canvas_item();
  301. /*
  302. texture->draw(ci,Point2());
  303. break;
  304. */
  305. Size2i s;
  306. Rect2i src_rect;
  307. s = texture->get_size();
  308. src_rect.size = s;
  309. Point2 ofs = offset;
  310. if (centered)
  311. ofs -= s / 2;
  312. if (OS::get_singleton()->get_use_pixel_snap()) {
  313. ofs = ofs.floor();
  314. }
  315. Rect2 dst_rect(ofs, s);
  316. texture->draw_rect_region(ci, dst_rect, src_rect, modulate);
  317. } break;
  318. }
  319. }
  320. void ViewportSprite::set_viewport_path(const NodePath &p_viewport) {
  321. viewport_path = p_viewport;
  322. update();
  323. if (!is_inside_tree())
  324. return;
  325. if (texture.is_valid()) {
  326. texture->disconnect("changed", this, "update");
  327. texture = Ref<Texture>();
  328. }
  329. if (viewport_path.is_empty())
  330. return;
  331. Node *n = get_node(viewport_path);
  332. ERR_FAIL_COND(!n);
  333. Viewport *vp = n->cast_to<Viewport>();
  334. ERR_FAIL_COND(!vp);
  335. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  336. texture = rtt;
  337. if (texture.is_valid()) {
  338. texture->connect("changed", this, "update");
  339. }
  340. item_rect_changed();
  341. }
  342. NodePath ViewportSprite::get_viewport_path() const {
  343. return viewport_path;
  344. }
  345. void ViewportSprite::set_centered(bool p_center) {
  346. centered = p_center;
  347. update();
  348. item_rect_changed();
  349. }
  350. bool ViewportSprite::is_centered() const {
  351. return centered;
  352. }
  353. void ViewportSprite::set_offset(const Point2 &p_offset) {
  354. offset = p_offset;
  355. update();
  356. item_rect_changed();
  357. }
  358. Point2 ViewportSprite::get_offset() const {
  359. return offset;
  360. }
  361. void ViewportSprite::set_modulate(const Color &p_color) {
  362. modulate = p_color;
  363. update();
  364. }
  365. Color ViewportSprite::get_modulate() const {
  366. return modulate;
  367. }
  368. Rect2 ViewportSprite::get_item_rect() const {
  369. if (texture.is_null())
  370. return Rect2(0, 0, 1, 1);
  371. //if (texture.is_null())
  372. // return CanvasItem::get_item_rect();
  373. Size2i s;
  374. s = texture->get_size();
  375. Point2 ofs = offset;
  376. if (centered)
  377. ofs -= s / 2;
  378. if (s == Size2(0, 0))
  379. s = Size2(1, 1);
  380. return Rect2(ofs, s);
  381. }
  382. String ViewportSprite::get_configuration_warning() const {
  383. if (!has_node(viewport_path) || !get_node(viewport_path) || !get_node(viewport_path)->cast_to<Viewport>()) {
  384. return TTR("Path property must point to a valid Viewport node to work. Such Viewport must be set to 'render target' mode.");
  385. } else {
  386. Node *n = get_node(viewport_path);
  387. if (n) {
  388. Viewport *vp = n->cast_to<Viewport>();
  389. if (!vp->is_set_as_render_target()) {
  390. return TTR("The Viewport set in the path property must be set as 'render target' in order for this sprite to work.");
  391. }
  392. }
  393. }
  394. return String();
  395. }
  396. void ViewportSprite::_bind_methods() {
  397. ObjectTypeDB::bind_method(_MD("set_viewport_path", "path"), &ViewportSprite::set_viewport_path);
  398. ObjectTypeDB::bind_method(_MD("get_viewport_path"), &ViewportSprite::get_viewport_path);
  399. ObjectTypeDB::bind_method(_MD("set_centered", "centered"), &ViewportSprite::set_centered);
  400. ObjectTypeDB::bind_method(_MD("is_centered"), &ViewportSprite::is_centered);
  401. ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &ViewportSprite::set_offset);
  402. ObjectTypeDB::bind_method(_MD("get_offset"), &ViewportSprite::get_offset);
  403. ObjectTypeDB::bind_method(_MD("set_modulate", "modulate"), &ViewportSprite::set_modulate);
  404. ObjectTypeDB::bind_method(_MD("get_modulate"), &ViewportSprite::get_modulate);
  405. ADD_PROPERTYNZ(PropertyInfo(Variant::NODE_PATH, "viewport"), _SCS("set_viewport_path"), _SCS("get_viewport_path"));
  406. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "centered"), _SCS("set_centered"), _SCS("is_centered"));
  407. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "offset"), _SCS("set_offset"), _SCS("get_offset"));
  408. ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "modulate"), _SCS("set_modulate"), _SCS("get_modulate"));
  409. }
  410. ViewportSprite::ViewportSprite() {
  411. centered = true;
  412. modulate = Color(1, 1, 1, 1);
  413. }