label.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*************************************************************************/
  2. /* label.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 "label.h"
  30. #include "print_string.h"
  31. #include "globals.h"
  32. #include "translation.h"
  33. void Label::set_autowrap(bool p_autowrap) {
  34. autowrap=p_autowrap;
  35. word_cache_dirty=true;
  36. minimum_size_changed();
  37. }
  38. bool Label::has_autowrap() const {
  39. return autowrap;
  40. }
  41. void Label::set_uppercase(bool p_uppercase) {
  42. uppercase=p_uppercase;
  43. word_cache_dirty=true;
  44. minimum_size_changed();
  45. }
  46. bool Label::is_uppercase() const {
  47. return uppercase;
  48. }
  49. int Label::get_line_height() const {
  50. return get_font("font")->get_height();
  51. }
  52. void Label::_notification(int p_what) {
  53. if (p_what==NOTIFICATION_DRAW) {
  54. if (clip && !autowrap)
  55. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
  56. if (word_cache_dirty)
  57. regenerate_word_cache();
  58. RID ci = get_canvas_item();
  59. Size2 string_size;
  60. Size2 size=get_size();
  61. Ref<Font> font = get_font("font");
  62. Color font_color = get_color("font_color");
  63. Color font_color_shadow = get_color("font_color_shadow");
  64. bool use_outlinde = get_constant("shadow_as_outline");
  65. Point2 shadow_ofs(get_constant("shadow_offset_x"),get_constant("shadow_offset_y"));
  66. int font_h = font->get_height();
  67. int line_from=(int)get_val(); // + p_exposed.pos.y / font_h;
  68. int lines_visible = size.y/font_h;
  69. int line_to=(int)get_val() + lines_visible; //p_exposed.pos.y+p_exposed.size.height / font_h;
  70. int space_w=font->get_char_size(' ').width;
  71. int lines_total = get_max();
  72. int chars_total=0;
  73. int vbegin=0,vsep=0;
  74. if (lines_total && lines_total < lines_visible) {
  75. switch(valign) {
  76. case VALIGN_TOP: {
  77. //nothing
  78. } break;
  79. case VALIGN_CENTER: {
  80. vbegin=(lines_visible-lines_total) * font_h / 2;
  81. vsep=0;
  82. } break;
  83. case VALIGN_BOTTOM: {
  84. vbegin=(lines_visible-lines_total) * font_h;
  85. vsep=0;
  86. } break;
  87. case VALIGN_FILL: {
  88. vbegin=0;
  89. if (lines_total>1) {
  90. vsep=(lines_visible-lines_total) * font_h / (lines_total-1);
  91. } else {
  92. vsep=0;
  93. }
  94. } break;
  95. }
  96. }
  97. WordCache *wc = word_cache;
  98. if (!wc)
  99. return;
  100. int line=0;
  101. while(wc) {
  102. /* handle lines not meant to be drawn quickly */
  103. if (line>line_to)
  104. break;
  105. if (line<line_from) {
  106. while (wc && wc->char_pos>=0)
  107. wc=wc->next;
  108. if (wc)
  109. wc=wc->next;
  110. line++;
  111. continue;
  112. }
  113. /* handle lines normally */
  114. if (wc->char_pos<0) {
  115. //empty line
  116. wc=wc->next;
  117. line++;
  118. continue;
  119. }
  120. WordCache *from=wc;
  121. WordCache *to=wc;
  122. int taken=0;
  123. int spaces=0;
  124. while(to && to->char_pos>=0) {
  125. taken+=to->pixel_width;
  126. if (to!=from) {
  127. spaces++;
  128. }
  129. to=to->next;
  130. }
  131. bool can_fill = to && to->char_pos==WordCache::CHAR_WRAPLINE;
  132. float x_ofs=0;
  133. switch (align) {
  134. case ALIGN_FILL:
  135. case ALIGN_LEFT: {
  136. x_ofs=0;
  137. } break;
  138. case ALIGN_CENTER: {
  139. x_ofs=int(size.width-(taken+spaces*space_w))/2;;
  140. } break;
  141. case ALIGN_RIGHT: {
  142. x_ofs=int(size.width-(taken+spaces*space_w));
  143. } break;
  144. }
  145. int y_ofs=(line-(int)get_val())*font_h + font->get_ascent();
  146. y_ofs+=vbegin + line*vsep;
  147. while(from!=to) {
  148. // draw a word
  149. int pos = from->char_pos;
  150. if (from->char_pos<0) {
  151. ERR_PRINT("BUG");
  152. return;
  153. }
  154. if (from!=wc) {
  155. /* spacing */
  156. x_ofs+=space_w;
  157. if (can_fill && align==ALIGN_FILL && spaces) {
  158. x_ofs+=int((size.width-(taken+space_w*spaces))/spaces);
  159. }
  160. }
  161. if (font_color_shadow.a>0) {
  162. int chars_total_shadow = chars_total; //save chars drawn
  163. float x_ofs_shadow=x_ofs;
  164. for (int i=0;i<from->word_len;i++) {
  165. if (visible_chars < 0 || chars_total_shadow<visible_chars) {
  166. CharType c = text[i+pos];
  167. CharType n = text[i+pos+1];
  168. if (uppercase) {
  169. c=String::char_uppercase(c);
  170. n=String::char_uppercase(c);
  171. }
  172. float move=font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+shadow_ofs, c, n,font_color_shadow );
  173. if (use_outlinde) {
  174. font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(-shadow_ofs.x,shadow_ofs.y), c, n,font_color_shadow );
  175. font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(shadow_ofs.x,-shadow_ofs.y), c, n,font_color_shadow );
  176. font->draw_char(ci, Point2( x_ofs_shadow, y_ofs )+Vector2(-shadow_ofs.x,-shadow_ofs.y), c, n,font_color_shadow );
  177. }
  178. x_ofs_shadow+=move;
  179. chars_total_shadow++;
  180. }
  181. }
  182. }
  183. for (int i=0;i<from->word_len;i++) {
  184. if (visible_chars < 0 || chars_total<visible_chars) {
  185. CharType c = text[i+pos];
  186. CharType n = text[i+pos+1];
  187. if (uppercase) {
  188. c=String::char_uppercase(c);
  189. n=String::char_uppercase(c);
  190. }
  191. x_ofs+=font->draw_char(ci,Point2( x_ofs, y_ofs ), c, n, font_color );
  192. chars_total++;
  193. }
  194. }
  195. from=from->next;
  196. }
  197. wc=to?to->next:0;
  198. line++;
  199. }
  200. }
  201. if (p_what==NOTIFICATION_THEME_CHANGED) {
  202. word_cache_dirty=true;
  203. update();
  204. }
  205. if (p_what==NOTIFICATION_RESIZED) {
  206. word_cache_dirty=true;
  207. }
  208. }
  209. Size2 Label::get_minimum_size() const {
  210. if (autowrap)
  211. return Size2(1,1);
  212. else {
  213. // don't want to mutable everything
  214. if(word_cache_dirty)
  215. const_cast<Label*>(this)->regenerate_word_cache();
  216. Size2 ms=minsize;
  217. if (clip)
  218. ms.width=1;
  219. return ms;
  220. }
  221. }
  222. int Label::get_longest_line_width() const {
  223. Ref<Font> font = get_font("font");
  224. int max_line_width=0;
  225. int line_width=0;
  226. for (int i=0;i<text.size()+1;i++) {
  227. CharType current=i<text.length()?text[i]:' '; //always a space at the end, so the algo works
  228. if (uppercase)
  229. current=String::char_uppercase(current);
  230. if (current<32) {
  231. if (current=='\n') {
  232. if (line_width>max_line_width)
  233. max_line_width=line_width;
  234. line_width=0;
  235. }
  236. } else {
  237. int char_width=font->get_char_size(current).width;
  238. line_width+=char_width;
  239. }
  240. }
  241. if (line_width>max_line_width)
  242. max_line_width=line_width;
  243. return max_line_width;
  244. }
  245. int Label::get_line_count() const {
  246. if (!is_inside_scene())
  247. return 1;
  248. if (word_cache_dirty)
  249. const_cast<Label*>(this)->regenerate_word_cache();
  250. return line_count;
  251. }
  252. void Label::regenerate_word_cache() {
  253. while (word_cache) {
  254. WordCache *current=word_cache;
  255. word_cache=current->next;
  256. memdelete( current );
  257. }
  258. int width=autowrap?get_size().width:get_longest_line_width();
  259. Ref<Font> font = get_font("font");
  260. int current_word_size=0;
  261. int word_pos=0;
  262. int line_width=0;
  263. int last_width=0;
  264. line_count=1;
  265. total_char_cache=0;
  266. WordCache *last=NULL;
  267. for (int i=0;i<text.size()+1;i++) {
  268. CharType current=i<text.length()?text[i]:' '; //always a space at the end, so the algo works
  269. if (uppercase)
  270. current=String::char_uppercase(current);
  271. bool insert_newline=false;
  272. if (current<33) {
  273. if (current_word_size>0) {
  274. WordCache *wc = memnew( WordCache );
  275. if (word_cache) {
  276. last->next=wc;
  277. } else {
  278. word_cache=wc;
  279. }
  280. last=wc;
  281. wc->pixel_width=current_word_size;
  282. wc->char_pos=word_pos;
  283. wc->word_len=i-word_pos;
  284. current_word_size=0;
  285. }
  286. if (current=='\n') {
  287. insert_newline=true;
  288. } else {
  289. total_char_cache++;
  290. }
  291. if (i<text.length() && text[i] == ' ') {
  292. total_char_cache--; // do not count spaces
  293. }
  294. } else {
  295. if (current_word_size==0) {
  296. if (line_width>0) // add a space before the new word if a word existed before
  297. line_width+=font->get_char_size(' ').width;
  298. word_pos=i;
  299. }
  300. int char_width=font->get_char_size(current).width;
  301. current_word_size+=char_width;
  302. line_width+=char_width;
  303. total_char_cache++;
  304. }
  305. if ((autowrap && line_width>=width && last_width<width) || insert_newline) {
  306. WordCache *wc = memnew( WordCache );
  307. if (word_cache) {
  308. last->next=wc;
  309. } else {
  310. word_cache=wc;
  311. }
  312. last=wc;
  313. wc->pixel_width=0;
  314. wc->char_pos=insert_newline?WordCache::CHAR_NEWLINE:WordCache::CHAR_WRAPLINE;
  315. line_width=current_word_size;
  316. line_count++;
  317. }
  318. last_width=line_width;
  319. }
  320. total_char_cache -= line_count + 1; // do not count new lines (including the first one)
  321. if (!autowrap) {
  322. minsize.width=width;
  323. minsize.height=font->get_height()*line_count;
  324. set_page( line_count );
  325. } else {
  326. set_page( get_size().height / font->get_height() );
  327. }
  328. set_max(line_count);
  329. word_cache_dirty=false;
  330. }
  331. void Label::set_align(Align p_align) {
  332. ERR_FAIL_INDEX(p_align,4);
  333. align=p_align;
  334. update();
  335. }
  336. Label::Align Label::get_align() const{
  337. return align;
  338. }
  339. void Label::set_valign(VAlign p_align) {
  340. ERR_FAIL_INDEX(p_align,4);
  341. valign=p_align;
  342. update();
  343. }
  344. Label::VAlign Label::get_valign() const{
  345. return valign;
  346. }
  347. void Label::set_text(const String& p_string) {
  348. String str = XL_MESSAGE(p_string);
  349. if (text==str)
  350. return;
  351. text=str;
  352. word_cache_dirty=true;
  353. update();
  354. if (!autowrap)
  355. minimum_size_changed();
  356. }
  357. void Label::set_clip_text(bool p_clip) {
  358. if (clip==p_clip)
  359. return;
  360. clip=p_clip;
  361. update();
  362. minimum_size_changed();
  363. }
  364. bool Label::is_clipping_text() const {
  365. return clip;
  366. }
  367. String Label::get_text() const {
  368. return text;
  369. }
  370. void Label::set_visible_characters(int p_amount) {
  371. visible_chars=p_amount;
  372. update();
  373. }
  374. void Label::set_percent_visible(float p_percent) {
  375. if (p_percent<0)
  376. set_visible_characters(-1);
  377. else
  378. set_visible_characters(get_total_character_count()*p_percent);
  379. percent_visible=p_percent;
  380. }
  381. float Label::get_percent_visible() const{
  382. return percent_visible;
  383. }
  384. int Label::get_total_character_count() const {
  385. if (word_cache_dirty)
  386. const_cast<Label*>(this)->regenerate_word_cache();
  387. return total_char_cache;
  388. }
  389. void Label::_bind_methods() {
  390. ObjectTypeDB::bind_method(_MD("set_align","align"),&Label::set_align);
  391. ObjectTypeDB::bind_method(_MD("get_align"),&Label::get_align);
  392. ObjectTypeDB::bind_method(_MD("set_valign","valign"),&Label::set_valign);
  393. ObjectTypeDB::bind_method(_MD("get_valign"),&Label::get_valign);
  394. ObjectTypeDB::bind_method(_MD("set_text","text"),&Label::set_text);
  395. ObjectTypeDB::bind_method(_MD("get_text"),&Label::get_text);
  396. ObjectTypeDB::bind_method(_MD("set_autowrap","enable"),&Label::set_autowrap);
  397. ObjectTypeDB::bind_method(_MD("has_autowrap"),&Label::has_autowrap);
  398. ObjectTypeDB::bind_method(_MD("set_uppercase","enable"),&Label::set_uppercase);
  399. ObjectTypeDB::bind_method(_MD("is_uppercase"),&Label::is_uppercase);
  400. ObjectTypeDB::bind_method(_MD("get_line_height"),&Label::get_line_height);
  401. ObjectTypeDB::bind_method(_MD("get_line_count"),&Label::get_line_count);
  402. ObjectTypeDB::bind_method(_MD("get_total_character_count"),&Label::get_total_character_count);
  403. ObjectTypeDB::bind_method(_MD("set_visible_characters"),&Label::set_visible_characters);
  404. ObjectTypeDB::bind_method(_MD("set_percent_visible","percent_visible"),&Label::set_percent_visible);
  405. ObjectTypeDB::bind_method(_MD("get_percent_visible"),&Label::get_percent_visible);
  406. BIND_CONSTANT( ALIGN_LEFT );
  407. BIND_CONSTANT( ALIGN_CENTER );
  408. BIND_CONSTANT( ALIGN_RIGHT );
  409. BIND_CONSTANT( ALIGN_FILL );
  410. BIND_CONSTANT( VALIGN_TOP );
  411. BIND_CONSTANT( VALIGN_CENTER );
  412. BIND_CONSTANT( VALIGN_BOTTOM );
  413. BIND_CONSTANT( VALIGN_FILL );
  414. ADD_PROPERTY( PropertyInfo( Variant::STRING, "text",PROPERTY_HINT_MULTILINE_TEXT,"",PROPERTY_USAGE_DEFAULT_INTL), _SCS("set_text"),_SCS("get_text") );
  415. ADD_PROPERTY( PropertyInfo( Variant::INT, "align", PROPERTY_HINT_ENUM,"Left,Center,Right,Fill" ),_SCS("set_align"),_SCS("get_align") );
  416. ADD_PROPERTY( PropertyInfo( Variant::INT, "valign", PROPERTY_HINT_ENUM,"Top,Center,Bottom,Fill" ),_SCS("set_valign"),_SCS("get_valign") );
  417. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "autowrap"),_SCS("set_autowrap"),_SCS("has_autowrap") );
  418. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "uppercase"),_SCS("set_uppercase"),_SCS("is_uppercase") );
  419. ADD_PROPERTY( PropertyInfo( Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE,"0,1,0.001"),_SCS("set_percent_visible"),_SCS("get_percent_visible") );
  420. }
  421. Label::Label(const String &p_text) {
  422. align=ALIGN_LEFT;
  423. valign=VALIGN_TOP;
  424. text="";
  425. word_cache=NULL;
  426. word_cache_dirty=true;
  427. autowrap=false;
  428. line_count=0;
  429. set_v_size_flags(0);
  430. clip=false;
  431. set_ignore_mouse(true);
  432. total_char_cache=0;
  433. visible_chars=-1;
  434. percent_visible=-1;
  435. set_text(p_text);
  436. uppercase=false;
  437. }
  438. Label::~Label() {
  439. while (word_cache) {
  440. WordCache *current=word_cache;
  441. word_cache=current->next;
  442. memdelete( current );
  443. }
  444. }