sprite.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*************************************************************************/
  2. /* sprite.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. bool filter_clip = false;
  57. if (region) {
  58. s = region_rect.size;
  59. src_rect = region_rect;
  60. filter_clip = region_filter_clip;
  61. } else {
  62. s = Size2(texture->get_size());
  63. s = s / Size2(hframes, vframes);
  64. src_rect.size = s;
  65. src_rect.position.x += float(frame % hframes) * s.x;
  66. src_rect.position.y += float(frame / hframes) * s.y;
  67. }
  68. Point2 ofs = offset;
  69. if (centered)
  70. ofs -= s / 2;
  71. if (Engine::get_singleton()->get_use_pixel_snap()) {
  72. ofs = ofs.floor();
  73. }
  74. Rect2 dst_rect(ofs, s);
  75. if (hflip)
  76. dst_rect.size.x = -dst_rect.size.x;
  77. if (vflip)
  78. dst_rect.size.y = -dst_rect.size.y;
  79. texture->draw_rect_region(ci, dst_rect, src_rect, Color(1, 1, 1), false, normal_map, filter_clip);
  80. } break;
  81. }
  82. }
  83. void Sprite::set_texture(const Ref<Texture> &p_texture) {
  84. if (p_texture == texture)
  85. return;
  86. texture = p_texture;
  87. update();
  88. emit_signal("texture_changed");
  89. item_rect_changed();
  90. _change_notify("texture");
  91. }
  92. void Sprite::set_normal_map(const Ref<Texture> &p_texture) {
  93. normal_map = p_texture;
  94. update();
  95. }
  96. Ref<Texture> Sprite::get_normal_map() const {
  97. return normal_map;
  98. }
  99. Ref<Texture> Sprite::get_texture() const {
  100. return texture;
  101. }
  102. void Sprite::set_centered(bool p_center) {
  103. centered = p_center;
  104. update();
  105. item_rect_changed();
  106. }
  107. bool Sprite::is_centered() const {
  108. return centered;
  109. }
  110. void Sprite::set_offset(const Point2 &p_offset) {
  111. offset = p_offset;
  112. update();
  113. item_rect_changed();
  114. _change_notify("offset");
  115. }
  116. Point2 Sprite::get_offset() const {
  117. return offset;
  118. }
  119. void Sprite::set_flip_h(bool p_flip) {
  120. hflip = p_flip;
  121. update();
  122. }
  123. bool Sprite::is_flipped_h() const {
  124. return hflip;
  125. }
  126. void Sprite::set_flip_v(bool p_flip) {
  127. vflip = p_flip;
  128. update();
  129. }
  130. bool Sprite::is_flipped_v() const {
  131. return vflip;
  132. }
  133. void Sprite::set_region(bool p_region) {
  134. if (p_region == region)
  135. return;
  136. region = p_region;
  137. update();
  138. }
  139. bool Sprite::is_region() const {
  140. return region;
  141. }
  142. void Sprite::set_region_rect(const Rect2 &p_region_rect) {
  143. if (region_rect == p_region_rect)
  144. return;
  145. region_rect = p_region_rect;
  146. if (region)
  147. item_rect_changed();
  148. _change_notify("region_rect");
  149. }
  150. Rect2 Sprite::get_region_rect() const {
  151. return region_rect;
  152. }
  153. void Sprite::set_region_filter_clip(bool p_enable) {
  154. region_filter_clip = p_enable;
  155. update();
  156. }
  157. bool Sprite::is_region_filter_clip_enabled() const {
  158. return region_filter_clip;
  159. }
  160. void Sprite::set_frame(int p_frame) {
  161. ERR_FAIL_INDEX(p_frame, vframes * hframes);
  162. if (frame != p_frame)
  163. item_rect_changed();
  164. frame = p_frame;
  165. _change_notify("frame");
  166. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  167. }
  168. int Sprite::get_frame() const {
  169. return frame;
  170. }
  171. void Sprite::set_vframes(int p_amount) {
  172. ERR_FAIL_COND(p_amount < 1);
  173. vframes = p_amount;
  174. update();
  175. item_rect_changed();
  176. _change_notify();
  177. }
  178. int Sprite::get_vframes() const {
  179. return vframes;
  180. }
  181. void Sprite::set_hframes(int p_amount) {
  182. ERR_FAIL_COND(p_amount < 1);
  183. hframes = p_amount;
  184. update();
  185. item_rect_changed();
  186. _change_notify();
  187. }
  188. int Sprite::get_hframes() const {
  189. return hframes;
  190. }
  191. Rect2 Sprite::get_item_rect() const {
  192. if (texture.is_null())
  193. return Rect2(0, 0, 1, 1);
  194. /*
  195. if (texture.is_null())
  196. return CanvasItem::get_item_rect();
  197. */
  198. Size2i s;
  199. if (region) {
  200. s = region_rect.size;
  201. } else {
  202. s = texture->get_size();
  203. s = s / Point2(hframes, vframes);
  204. }
  205. Point2 ofs = offset;
  206. if (centered)
  207. ofs -= s / 2;
  208. if (s == Size2(0, 0))
  209. s = Size2(1, 1);
  210. return Rect2(ofs, s);
  211. }
  212. void Sprite::_validate_property(PropertyInfo &property) const {
  213. if (property.name == "frame") {
  214. property.hint = PROPERTY_HINT_SPRITE_FRAME;
  215. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  216. }
  217. }
  218. void Sprite::_bind_methods() {
  219. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite::set_texture);
  220. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite::get_texture);
  221. ClassDB::bind_method(D_METHOD("set_normal_map", "normal_map"), &Sprite::set_normal_map);
  222. ClassDB::bind_method(D_METHOD("get_normal_map"), &Sprite::get_normal_map);
  223. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &Sprite::set_centered);
  224. ClassDB::bind_method(D_METHOD("is_centered"), &Sprite::is_centered);
  225. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Sprite::set_offset);
  226. ClassDB::bind_method(D_METHOD("get_offset"), &Sprite::get_offset);
  227. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &Sprite::set_flip_h);
  228. ClassDB::bind_method(D_METHOD("is_flipped_h"), &Sprite::is_flipped_h);
  229. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &Sprite::set_flip_v);
  230. ClassDB::bind_method(D_METHOD("is_flipped_v"), &Sprite::is_flipped_v);
  231. ClassDB::bind_method(D_METHOD("set_region", "enabled"), &Sprite::set_region);
  232. ClassDB::bind_method(D_METHOD("is_region"), &Sprite::is_region);
  233. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite::set_region_rect);
  234. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite::get_region_rect);
  235. ClassDB::bind_method(D_METHOD("set_region_filter_clip", "enabled"), &Sprite::set_region_filter_clip);
  236. ClassDB::bind_method(D_METHOD("is_region_filter_clip_enabled"), &Sprite::is_region_filter_clip_enabled);
  237. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite::set_frame);
  238. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite::get_frame);
  239. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite::set_vframes);
  240. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite::get_vframes);
  241. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite::set_hframes);
  242. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite::get_hframes);
  243. ADD_SIGNAL(MethodInfo("frame_changed"));
  244. ADD_SIGNAL(MethodInfo("texture_changed"));
  245. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  246. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "normal_map", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_map", "get_normal_map");
  247. ADD_GROUP("Offset", "");
  248. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  249. ADD_PROPERTYNZ(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  250. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  251. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  252. ADD_GROUP("Animation", "");
  253. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  254. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  255. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
  256. ADD_GROUP("Region", "region_");
  257. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region");
  258. ADD_PROPERTYNZ(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  259. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "region_filter_clip"), "set_region_filter_clip", "is_region_filter_clip_enabled");
  260. }
  261. Sprite::Sprite() {
  262. centered = true;
  263. hflip = false;
  264. vflip = false;
  265. region = false;
  266. region_filter_clip = false;
  267. frame = 0;
  268. vframes = 1;
  269. hframes = 1;
  270. }
  271. //////////////////////////// VPSPRITE
  272. ///
  273. ///
  274. ///
  275. #if 0
  276. void ViewportSprite::edit_set_pivot(const Point2& p_pivot) {
  277. set_offset(p_pivot);
  278. }
  279. Point2 ViewportSprite::edit_get_pivot() const {
  280. return get_offset();
  281. }
  282. bool ViewportSprite::edit_has_pivot() const {
  283. return true;
  284. }
  285. void ViewportSprite::_notification(int p_what) {
  286. switch(p_what) {
  287. case NOTIFICATION_ENTER_TREE: {
  288. if (!viewport_path.is_empty()) {
  289. Node *n = get_node(viewport_path);
  290. ERR_FAIL_COND(!n);
  291. Viewport *vp=Object::cast_to<Viewport>(n);
  292. ERR_FAIL_COND(!vp);
  293. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  294. texture=rtt;
  295. texture->connect("changed",this,"update");
  296. item_rect_changed();
  297. }
  298. } break;
  299. case NOTIFICATION_EXIT_TREE: {
  300. if (texture.is_valid()) {
  301. texture->disconnect("changed",this,"update");
  302. texture=Ref<Texture>();
  303. }
  304. } break;
  305. case NOTIFICATION_DRAW: {
  306. if (texture.is_null())
  307. return;
  308. RID ci = get_canvas_item();
  309. /*
  310. texture->draw(ci,Point2());
  311. break;
  312. */
  313. Size2i s;
  314. Rect2i src_rect;
  315. s = texture->get_size();
  316. src_rect.size=s;
  317. Point2 ofs=offset;
  318. if (centered)
  319. ofs-=s/2;
  320. if (OS::get_singleton()->get_use_pixel_snap()) {
  321. ofs=ofs.floor();
  322. }
  323. Rect2 dst_rect(ofs,s);
  324. texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  325. } break;
  326. }
  327. }
  328. void ViewportSprite::set_viewport_path(const NodePath& p_viewport) {
  329. viewport_path=p_viewport;
  330. update();
  331. if (!is_inside_tree())
  332. return;
  333. if (texture.is_valid()) {
  334. texture->disconnect("changed",this,"update");
  335. texture=Ref<Texture>();
  336. }
  337. if (viewport_path.is_empty())
  338. return;
  339. Node *n = get_node(viewport_path);
  340. ERR_FAIL_COND(!n);
  341. Viewport *vp=Object::cast_to<Viewport>(n);
  342. ERR_FAIL_COND(!vp);
  343. Ref<RenderTargetTexture> rtt = vp->get_render_target_texture();
  344. texture=rtt;
  345. if (texture.is_valid()) {
  346. texture->connect("changed",this,"update");
  347. }
  348. item_rect_changed();
  349. }
  350. NodePath ViewportSprite::get_viewport_path() const {
  351. return viewport_path;
  352. }
  353. void ViewportSprite::set_centered(bool p_center) {
  354. centered=p_center;
  355. update();
  356. item_rect_changed();
  357. }
  358. bool ViewportSprite::is_centered() const {
  359. return centered;
  360. }
  361. void ViewportSprite::set_offset(const Point2& p_offset) {
  362. offset=p_offset;
  363. update();
  364. item_rect_changed();
  365. }
  366. Point2 ViewportSprite::get_offset() const {
  367. return offset;
  368. }
  369. void ViewportSprite::set_modulate(const Color& p_color) {
  370. modulate=p_color;
  371. update();
  372. }
  373. Color ViewportSprite::get_modulate() const{
  374. return modulate;
  375. }
  376. Rect2 ViewportSprite::get_item_rect() const {
  377. if (texture.is_null())
  378. return Rect2(0,0,1,1);
  379. /*
  380. if (texture.is_null())
  381. return CanvasItem::get_item_rect();
  382. */
  383. Size2i s;
  384. s = texture->get_size();
  385. Point2 ofs=offset;
  386. if (centered)
  387. ofs-=s/2;
  388. if (s==Size2(0,0))
  389. s=Size2(1,1);
  390. return Rect2(ofs,s);
  391. }
  392. String ViewportSprite::get_configuration_warning() const {
  393. if (!has_node(viewport_path) || !Object::cast_to<Viewport>(get_node(viewport_path))) {
  394. return TTR("Path property must point to a valid Viewport node to work. Such Viewport must be set to 'render target' mode.");
  395. } else {
  396. Node *n = get_node(viewport_path);
  397. if (n) {
  398. Viewport *vp = Object::cast_to<Viewport>(n);
  399. if (!vp->is_set_as_render_target()) {
  400. return TTR("The Viewport set in the path property must be set as 'render target' in order for this sprite to work.");
  401. }
  402. }
  403. }
  404. return String();
  405. }
  406. void ViewportSprite::_bind_methods() {
  407. ClassDB::bind_method(D_METHOD("set_viewport_path","path"),&ViewportSprite::set_viewport_path);
  408. ClassDB::bind_method(D_METHOD("get_viewport_path"),&ViewportSprite::get_viewport_path);
  409. ClassDB::bind_method(D_METHOD("set_centered","centered"),&ViewportSprite::set_centered);
  410. ClassDB::bind_method(D_METHOD("is_centered"),&ViewportSprite::is_centered);
  411. ClassDB::bind_method(D_METHOD("set_offset","offset"),&ViewportSprite::set_offset);
  412. ClassDB::bind_method(D_METHOD("get_offset"),&ViewportSprite::get_offset);
  413. ClassDB::bind_method(D_METHOD("set_modulate","modulate"),&ViewportSprite::set_modulate);
  414. ClassDB::bind_method(D_METHOD("get_modulate"),&ViewportSprite::get_modulate);
  415. ADD_PROPERTYNZ( PropertyInfo( Variant::NODE_PATH, "viewport"), "set_viewport_path","get_viewport_path");
  416. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "centered"), "set_centered","is_centered");
  417. ADD_PROPERTYNZ( PropertyInfo( Variant::VECTOR2, "offset"), "set_offset","get_offset");
  418. ADD_PROPERTYNO( PropertyInfo( Variant::COLOR, "modulate"), "set_modulate","get_modulate");
  419. }
  420. ViewportSprite::ViewportSprite() {
  421. centered=true;
  422. modulate=Color(1,1,1,1);
  423. }
  424. #endif