texture_progress.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*************************************************************************/
  2. /* texture_progress.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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_progress.h"
  30. void TextureProgress::set_under_texture(const Ref<Texture>& p_texture) {
  31. under=p_texture;
  32. update();
  33. minimum_size_changed();
  34. }
  35. Ref<Texture> TextureProgress::get_under_texture() const{
  36. return under;
  37. }
  38. void TextureProgress::set_over_texture(const Ref<Texture>& p_texture) {
  39. over=p_texture;
  40. update();
  41. minimum_size_changed();
  42. }
  43. Ref<Texture> TextureProgress::get_over_texture() const{
  44. return over;
  45. }
  46. Size2 TextureProgress::get_minimum_size() const {
  47. if (under.is_valid())
  48. return under->get_size();
  49. else if (over.is_valid())
  50. return over->get_size();
  51. else if (progress.is_valid())
  52. return progress->get_size();
  53. return Size2(1,1);
  54. }
  55. void TextureProgress::set_progress_texture(const Ref<Texture>& p_texture) {
  56. progress=p_texture;
  57. update();
  58. minimum_size_changed();
  59. }
  60. Ref<Texture> TextureProgress::get_progress_texture() const{
  61. return progress;
  62. }
  63. Point2 TextureProgress::unit_val_to_uv(float val) {
  64. if (progress.is_null())
  65. return Point2();
  66. if (val<0)
  67. val+=1;
  68. if (val>1)
  69. val-=1;
  70. Point2 p=get_relative_center();
  71. if (val<0.125)
  72. return Point2(p.x+(1-p.x)*val*8,0);
  73. if (val<0.25)
  74. return Point2(1,p.y*(val-0.125)*8);
  75. if (val<0.375)
  76. return Point2(1,p.y+(1-p.y)*(val-0.25)*8);
  77. if (val<0.5)
  78. return Point2(1-(1-p.x)*(val-0.375)*8,1);
  79. if (val<0.625)
  80. return Point2(p.x*(1-(val-0.5)*8),1);
  81. if (val<0.75)
  82. return Point2(0,1-((1-p.y)*(val-0.625)*8));
  83. if (val<0.875)
  84. return Point2(0,p.y-p.y*(val-0.75)*8);
  85. else
  86. return Point2(p.x*(val-0.875)*8,0);
  87. }
  88. Point2 TextureProgress::get_relative_center()
  89. {
  90. if (progress.is_null())
  91. return Point2();
  92. Point2 p = progress->get_size()/2;
  93. p+=rad_center_off;
  94. p.x/=progress->get_width();
  95. p.y/=progress->get_height();
  96. p.x=CLAMP(p.x,0,1);
  97. p.y=CLAMP(p.y,0,1);
  98. return p;
  99. }
  100. void TextureProgress::_notification(int p_what){
  101. const float corners[12]={-0.125,-0.375,-0.625,-0.875,0.125,0.375,0.625,0.875,1.125,1.375,1.625,1.875};
  102. switch(p_what) {
  103. case NOTIFICATION_DRAW: {
  104. if (under.is_valid())
  105. draw_texture(under,Point2());
  106. if (progress.is_valid()) {
  107. Size2 s = progress->get_size();
  108. switch (mode) {
  109. case FILL_LEFT_TO_RIGHT: {
  110. Rect2 region=Rect2(Point2(),Size2(s.x*get_unit_value(),s.y));
  111. draw_texture_rect_region(progress,region,region);
  112. } break;
  113. case FILL_RIGHT_TO_LEFT: {
  114. Rect2 region=Rect2(Point2(s.x-s.x*get_unit_value(),0),Size2(s.x*get_unit_value(),s.y));
  115. draw_texture_rect_region(progress,region,region);
  116. } break;
  117. case FILL_TOP_TO_BOTTOM: {
  118. Rect2 region=Rect2(Point2(),Size2(s.x,s.y*get_unit_value()));
  119. draw_texture_rect_region(progress,region,region);
  120. } break;
  121. case FILL_BOTTOM_TO_TOP: {
  122. Rect2 region=Rect2(Point2(0,s.y-s.y*get_unit_value()),Size2(s.x,s.y*get_unit_value()));
  123. draw_texture_rect_region(progress,region,region);
  124. } break;
  125. case FILL_CLOCKWISE:
  126. case FILL_COUNTER_CLOCKWISE: {
  127. float val=get_unit_value()*rad_max_degrees/360;
  128. if (val==1) {
  129. Rect2 region=Rect2(Point2(),s);
  130. draw_texture_rect_region(progress,region,region);
  131. } else if (val!=0) {
  132. Array pts;
  133. float direction=mode==FILL_CLOCKWISE?1:-1;
  134. float start=rad_init_angle/360;
  135. float end=start+direction*val;
  136. pts.append(start);
  137. pts.append(end);
  138. float from=MIN(start,end);
  139. float to=MAX(start,end);
  140. for (int i=0;i<12;i++)
  141. if (corners[i]>from&&corners[i]<to)
  142. pts.append(corners[i]);
  143. pts.sort();
  144. Vector<Point2> uvs;
  145. Vector<Point2> points;
  146. uvs.push_back(get_relative_center());
  147. points.push_back(Point2(s.x*get_relative_center().x,s.y*get_relative_center().y));
  148. for (int i=0;i<pts.size();i++) {
  149. Point2 uv=unit_val_to_uv(pts[i]);
  150. if (uvs.find(uv)>=0)
  151. continue;
  152. uvs.push_back(uv);
  153. points.push_back(Point2(uv.x*s.x,uv.y*s.y));
  154. }
  155. draw_polygon(points,Vector<Color>(),uvs,progress);
  156. }
  157. if (get_tree()->is_editor_hint()) {
  158. Point2 p=progress->get_size();
  159. p.x*=get_relative_center().x;
  160. p.y*=get_relative_center().y;
  161. p=p.floor();
  162. draw_line(p-Point2(8,0),p+Point2(8,0),Color(0.9,0.5,0.5),2);
  163. draw_line(p-Point2(0,8),p+Point2(0,8),Color(0.9,0.5,0.5),2);
  164. }
  165. } break;
  166. default:
  167. draw_texture_rect_region(progress,Rect2(Point2(),Size2(s.x*get_unit_value(),s.y)),Rect2(Point2(),Size2(s.x*get_unit_value(),s.y)));
  168. }
  169. }
  170. if (over.is_valid())
  171. draw_texture(over,Point2());
  172. } break;
  173. }
  174. }
  175. void TextureProgress::set_fill_mode(int p_fill)
  176. {
  177. ERR_FAIL_INDEX(p_fill,6);
  178. mode=(FillMode)p_fill;
  179. update();
  180. }
  181. int TextureProgress::get_fill_mode()
  182. {
  183. return mode;
  184. }
  185. void TextureProgress::set_radial_initial_angle(float p_angle)
  186. {
  187. while(p_angle>360)
  188. p_angle-=360;
  189. while (p_angle<0)
  190. p_angle+=360;
  191. rad_init_angle=p_angle;
  192. update();
  193. }
  194. float TextureProgress::get_radial_initial_angle()
  195. {
  196. return rad_init_angle;
  197. }
  198. void TextureProgress::set_fill_degrees(float p_angle)
  199. {
  200. rad_max_degrees=CLAMP(p_angle,0,360);
  201. update();
  202. }
  203. float TextureProgress::get_fill_degrees()
  204. {
  205. return rad_max_degrees;
  206. }
  207. void TextureProgress::set_radial_center_offset(const Point2 &p_off)
  208. {
  209. rad_center_off=p_off;
  210. update();
  211. }
  212. Point2 TextureProgress::get_radial_center_offset()
  213. {
  214. return rad_center_off;
  215. }
  216. void TextureProgress::_bind_methods() {
  217. ObjectTypeDB::bind_method(_MD("set_under_texture","tex"),&TextureProgress::set_under_texture);
  218. ObjectTypeDB::bind_method(_MD("get_under_texture"),&TextureProgress::get_under_texture);
  219. ObjectTypeDB::bind_method(_MD("set_progress_texture","tex"),&TextureProgress::set_progress_texture);
  220. ObjectTypeDB::bind_method(_MD("get_progress_texture"),&TextureProgress::get_progress_texture);
  221. ObjectTypeDB::bind_method(_MD("set_over_texture","tex"),&TextureProgress::set_over_texture);
  222. ObjectTypeDB::bind_method(_MD("get_over_texture"),&TextureProgress::get_over_texture);
  223. ObjectTypeDB::bind_method(_MD("set_fill_mode","mode"),&TextureProgress::set_fill_mode);
  224. ObjectTypeDB::bind_method(_MD("get_fill_mode"), &TextureProgress::get_fill_mode);
  225. ObjectTypeDB::bind_method(_MD("set_radial_initial_angle","mode"),&TextureProgress::set_radial_initial_angle);
  226. ObjectTypeDB::bind_method(_MD("get_radial_initial_angle"), &TextureProgress::get_radial_initial_angle);
  227. ObjectTypeDB::bind_method(_MD("set_radial_center_offset","mode"),&TextureProgress::set_radial_center_offset);
  228. ObjectTypeDB::bind_method(_MD("get_radial_center_offset"), &TextureProgress::get_radial_center_offset);
  229. ObjectTypeDB::bind_method(_MD("set_fill_degrees","mode"),&TextureProgress::set_fill_degrees);
  230. ObjectTypeDB::bind_method(_MD("get_fill_degrees"), &TextureProgress::get_fill_degrees);
  231. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture/under",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_under_texture"),_SCS("get_under_texture"));
  232. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture/over",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_over_texture"),_SCS("get_over_texture"));
  233. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"texture/progress",PROPERTY_HINT_RESOURCE_TYPE,"Texture"),_SCS("set_progress_texture"),_SCS("get_progress_texture"));
  234. ADD_PROPERTYNZ( PropertyInfo(Variant::INT,"mode",PROPERTY_HINT_ENUM,"Left to Right,Right to Left,Top to Bottom,Bottom to Top,Clockwise,Counter Clockwise"),_SCS("set_fill_mode"),_SCS("get_fill_mode"));
  235. ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"radial_fill/initial_angle",PROPERTY_HINT_RANGE,"0.0,360.0,0.1,slider"),_SCS("set_radial_initial_angle"),_SCS("get_radial_initial_angle"));
  236. ADD_PROPERTYNZ( PropertyInfo(Variant::REAL,"radial_fill/fill_degrees",PROPERTY_HINT_RANGE,"0.0,360.0,0.1,slider"),_SCS("set_fill_degrees"),_SCS("get_fill_degrees"));
  237. ADD_PROPERTY( PropertyInfo(Variant::VECTOR2,"radial_fill/center_offset"),_SCS("set_radial_center_offset"),_SCS("get_radial_center_offset"));
  238. BIND_CONSTANT( FILL_LEFT_TO_RIGHT );
  239. BIND_CONSTANT( FILL_RIGHT_TO_LEFT );
  240. BIND_CONSTANT( FILL_TOP_TO_BOTTOM );
  241. BIND_CONSTANT( FILL_BOTTOM_TO_TOP );
  242. BIND_CONSTANT( FILL_CLOCKWISE );
  243. BIND_CONSTANT( FILL_COUNTER_CLOCKWISE );
  244. }
  245. TextureProgress::TextureProgress()
  246. {
  247. mode=FILL_LEFT_TO_RIGHT;
  248. rad_center_off=Point2();
  249. rad_max_degrees=360;
  250. }