texture_frame.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*************************************************************************/
  2. /* texture_frame.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "texture_frame.h"
  30. #include "servers/visual_server.h"
  31. void TextureFrame::_notification(int p_what) {
  32. if (p_what==NOTIFICATION_DRAW) {
  33. if (texture.is_null())
  34. return;
  35. Size2 s=expand?get_size():texture->get_size();
  36. RID ci = get_canvas_item();
  37. draw_texture_rect(texture,Rect2(Point2(),s),false,modulate);
  38. /*
  39. Vector<Point2> points;
  40. points.resize(4);
  41. points[0]=Point2(0,0);
  42. points[1]=Point2(s.x,0);
  43. points[2]=Point2(s.x,s.y);
  44. points[3]=Point2(0,s.y);
  45. Vector<Point2> uvs;
  46. uvs.resize(4);
  47. uvs[0]=Point2(0,0);
  48. uvs[1]=Point2(1,0);
  49. uvs[2]=Point2(1,1);
  50. uvs[3]=Point2(0,1);
  51. VisualServer::get_singleton()->canvas_item_add_primitive(ci,points,Vector<Color>(),uvs,texture->get_rid());
  52. */
  53. }
  54. }
  55. Size2 TextureFrame::get_minimum_size() const {
  56. if (!expand && !texture.is_null())
  57. return texture->get_size();
  58. else
  59. return Size2();
  60. }
  61. void TextureFrame::_bind_methods() {
  62. ObjectTypeDB::bind_method(_MD("set_texture","texture"), & TextureFrame::set_texture );
  63. ObjectTypeDB::bind_method(_MD("get_texture"), & TextureFrame::get_texture );
  64. ObjectTypeDB::bind_method(_MD("set_modulate","modulate"), & TextureFrame::set_modulate );
  65. ObjectTypeDB::bind_method(_MD("get_modulate"), & TextureFrame::get_modulate );
  66. ObjectTypeDB::bind_method(_MD("set_expand","enable"), & TextureFrame::set_expand );
  67. ObjectTypeDB::bind_method(_MD("has_expand"), & TextureFrame::has_expand );
  68. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), _SCS("set_texture"),_SCS("get_texture") );
  69. ADD_PROPERTY( PropertyInfo( Variant::COLOR, "modulate"), _SCS("set_modulate"),_SCS("get_modulate") );
  70. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "expand" ), _SCS("set_expand"),_SCS("has_expand") );
  71. }
  72. void TextureFrame::set_texture(const Ref<Texture>& p_tex) {
  73. texture=p_tex;
  74. update();
  75. if (texture.is_valid())
  76. texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  77. minimum_size_changed();
  78. }
  79. Ref<Texture> TextureFrame::get_texture() const {
  80. return texture;
  81. }
  82. void TextureFrame::set_modulate(const Color& p_tex) {
  83. modulate=p_tex;
  84. update();
  85. }
  86. Color TextureFrame::get_modulate() const{
  87. return modulate;
  88. }
  89. void TextureFrame::set_expand(bool p_expand) {
  90. expand=p_expand;
  91. update();
  92. minimum_size_changed();
  93. }
  94. bool TextureFrame::has_expand() const {
  95. return expand;
  96. }
  97. TextureFrame::TextureFrame() {
  98. expand=false;
  99. modulate=Color(1,1,1,1);
  100. set_ignore_mouse(true);
  101. }
  102. TextureFrame::~TextureFrame()
  103. {
  104. }