Text.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. // keep these methods so that the hidden Memc/_Memc can be initialized/deleted
  6. TextCode::~TextCode() {}
  7. TextCode:: TextCode() {}
  8. /******************************************************************************/
  9. void Text::zero()
  10. {
  11. auto_line=AUTO_LINE_NONE;
  12. _code=null; _codes=0;
  13. }
  14. Text::Text() {zero();}
  15. Text& Text::del()
  16. {
  17. Free(_code); _codes=0;
  18. _text .del ();
  19. text_style.clear();
  20. skin .clear();
  21. super::del(); zero(); return T;
  22. }
  23. Text& Text::create(C Str &text, C TextStylePtr &text_style)
  24. {
  25. del();
  26. _type =GO_TEXT;
  27. _visible =true;
  28. T._text =text;
  29. T. text_style=text_style;
  30. return T;
  31. }
  32. Text& Text::create(C Text &src)
  33. {
  34. if(this!=&src)
  35. {
  36. if(!src.is())del();else
  37. {
  38. copyParams(src);
  39. _type =GO_TEXT;
  40. auto_line =src. auto_line ;
  41. text_style=src. text_style;
  42. skin =src. skin ;
  43. _text =src._text ;
  44. CopyN(Alloc(Free(T._code), T._codes=src._codes), src._code, src._codes);
  45. REP(_codes)_code[i].pos=Ptr(T._text()+UInt((CChar*)src._code[i].pos-src._text())); // adjust pos
  46. }
  47. }
  48. return T;
  49. }
  50. /******************************************************************************/
  51. Text& Text::clear()
  52. {
  53. Free(_code); _codes=0; _text.del();
  54. return T;
  55. }
  56. Text& Text::set(C Str &text)
  57. {
  58. if(is()) // only if created, this prevents modifying values after destructor was already called
  59. {
  60. Free(_code); _codes=0; T._text=text;
  61. }
  62. return T;
  63. }
  64. Text& Text::code(C Str &code)
  65. {
  66. if(is()) // only if created, this prevents modifying values after destructor was already called
  67. {
  68. Memt<TextCodeData> codes; SetTextCode(code, _text, codes);
  69. Free(_code); Alloc(_code, _codes=codes.elms()); codes.copyTo(_code);
  70. }
  71. return T;
  72. }
  73. Str Text::code()C {return GetTextCode(T(), _code, _codes);}
  74. /******************************************************************************/
  75. TextStyle* Text::getTextStyle()C
  76. {
  77. if(text_style)return text_style();
  78. if(GuiSkin *skin=getSkin())return skin->text.text_style();
  79. return null;
  80. }
  81. Vec2 Text::textSize()C
  82. {
  83. if(TextStyle *text_style=getTextStyle())
  84. {
  85. #if DEFAULT_FONT_FROM_CUSTOM_SKIN
  86. TextStyleParams ts=*text_style; if(!ts.font())if(GuiSkin *skin=getSkin())ts.font(skin->font()); // adjust font in case it's empty and the custom skin has a different font than the 'Gui.skin'
  87. #else
  88. C TextStyle &ts=*text_style;
  89. #endif
  90. Flt width; Int lines=ts.textLines(_text, rect().w(), auto_line, &width);
  91. return Vec2(width, lines*ts.lineHeight());
  92. }
  93. return 0;
  94. }
  95. /******************************************************************************/
  96. void Text::draw(C GuiPC &gpc)
  97. {
  98. if(visible() && gpc.visible && T().is())
  99. if(TextStyle *text_style=getTextStyle())
  100. {
  101. Rect rect=T.rect()+gpc.offset;
  102. D.clip(gpc.clip);
  103. #if DEFAULT_FONT_FROM_CUSTOM_SKIN
  104. GuiSkin *skin; if(!text_style->font() && (skin=getSkin())) // adjust font in case it's empty and the custom skin has a different font than the Gui.skin
  105. {
  106. TextStyleParams ts=*text_style; ts.font(skin->font()); ts.drawCode(rect, _text, auto_line, _code, _codes);
  107. }else
  108. #endif
  109. text_style->drawCode(rect, _text, auto_line, _code, _codes);
  110. }
  111. }
  112. /******************************************************************************/
  113. Bool Text::save(File &f, CChar *path)C
  114. {
  115. if(super::save(f, path))
  116. {
  117. f.putMulti(Byte(6), auto_line)<<_text; // version
  118. f.putAsset(text_style.id());
  119. f.putAsset(skin .id());
  120. f.cmpUIntV(_codes); FREP(_codes){TextCodeData &c=_code[i]; f.putMulti(c.shadow_mode, c.color_mode, c.nocode_mode, c.shadow, c.color, UInt((CChar*)c.pos-_text()));}
  121. return f.ok();
  122. }
  123. return false;
  124. }
  125. Bool Text::load(File &f, CChar *path)
  126. {
  127. del(); if(super::load(f, path))switch(f.decUIntV()) // version
  128. {
  129. case 6:
  130. {
  131. _type=GO_TEXT;
  132. f>>auto_line>>_text;
  133. text_style.require(f.getAssetID(), path);
  134. skin .require(f.getAssetID(), path);
  135. _codes=f.decUIntV(); Alloc(_code, _codes); FREP(_codes){TextCodeData &c=_code[i]; UInt pos; f.getMulti(c.shadow_mode, c.color_mode, c.nocode_mode, c.shadow, c.color, pos); c.pos=Ptr(_text()+pos);}
  136. if(f.ok())return true;
  137. }break;
  138. case 5:
  139. {
  140. _type=GO_TEXT;
  141. f>>auto_line; f._getStr2(_text);
  142. text_style.require(f._getAsset(), path);
  143. skin .require(f._getAsset(), path);
  144. _codes=f.decUIntV(); Alloc(_code, _codes); FREP(_codes){TextCodeData &c=_code[i]; UInt pos; f.getMulti(c.shadow_mode, c.color_mode, c.nocode_mode, c.shadow, c.color, pos); c.pos=Ptr(_text()+pos);}
  145. if(f.ok())return true;
  146. }break;
  147. case 4:
  148. {
  149. _type=GO_TEXT;
  150. f>>auto_line; f._getStr(_text); text_style.require(f._getStr(), path); f._getStr();
  151. f>>_codes; Alloc(_code, _codes); FREP(_codes){TextCodeData &c=_code[i]; c.shadow_mode=TextCodeData::MODE(f.getByte()); c.color_mode=TextCodeData::MODE(f.getByte()); c.nocode_mode=TextCodeData::MODE(f.getByte()); f>>c.shadow>>c.color; c.pos=Ptr(_text()+f.getUInt());}
  152. if(f.ok())return true;
  153. }break;
  154. case 3:
  155. {
  156. _type=GO_TEXT;
  157. f>>auto_line; f._getStr(_text); text_style.require(f._getStr(), path); f._getStr();
  158. f>>_codes; Alloc(_code, _codes); FREP(_codes){TextCodeData &c=_code[i]; c.shadow_mode=TextCodeData::MODE(f.getByte()); f.skip(3); c.color_mode=TextCodeData::MODE(f.getByte()); f.skip(3); c.nocode_mode=TextCodeData::DEFAULT; f>>c.shadow>>c.color; c.pos=Ptr(_text()+f.getUInt());}
  159. if(f.ok())return true;
  160. }break;
  161. case 2:
  162. {
  163. _type=GO_TEXT;
  164. f>>auto_line; f._getStr(_text); text_style.require(f._getStr(), path); f._getStr();
  165. if(f.ok())return true;
  166. }break;
  167. case 1:
  168. {
  169. _type=GO_TEXT;
  170. _text=f._getStr16(); text_style.require(f._getStr16(), path); f._getStr16();
  171. if(f.ok())return true;
  172. }break;
  173. case 0:
  174. {
  175. _type=GO_TEXT;
  176. _text=f._getStr8(); text_style.require(f._getStr8(), path); f._getStr8();
  177. if(f.ok())return true;
  178. }break;
  179. }
  180. del(); return false;
  181. }
  182. /******************************************************************************/
  183. }
  184. /******************************************************************************/