2
0

TaskViewer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "TaskViewer.h"
  2. //Вершины для рендера картинок
  3. Vector4 TaskViewer::vertices[4];
  4. TaskViewer::TaskViewer() : lines(_FL_)
  5. {
  6. back = null;
  7. mask = null;
  8. font = null;
  9. texVar = null;
  10. w = h = 0.0f;
  11. vertices[0] = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
  12. vertices[1] = Vector4(1.0f, 0.0f, 0.0f, 1.0f);
  13. vertices[2] = Vector4(0.0f, 1.0f, 0.0f, 1.0f);
  14. vertices[3] = Vector4(1.0f, 1.0f, 0.0f, 1.0f);
  15. showPosition = 0.0f;
  16. }
  17. TaskViewer::~TaskViewer()
  18. {
  19. if(texVar)
  20. {
  21. texVar->SetTexture(null);
  22. texVar = null;
  23. }
  24. RELEASE(back);
  25. RELEASE(mask);
  26. RELEASE(font);
  27. }
  28. //Инициализировать объект
  29. bool TaskViewer::Create(MOPReader & reader)
  30. {
  31. Render().GetShaderId("Interface_TaskviewerPictureMask", Interface_TaskviewerPictureMask_id);
  32. Render().GetShaderId("Interface_TaskviewerPicture", Interface_TaskviewerPicture_id);
  33. if(!texVar)
  34. {
  35. texVar = Render().GetTechniqueGlobalVariable("TaskViewer_Texture", _FL_);
  36. }
  37. RELEASE(back);
  38. RELEASE(mask);
  39. RELEASE(font);
  40. const char * s = reader.String().c_str();
  41. if(s && s[0])
  42. {
  43. // back = Render().CreateTexture(_FL_, s);
  44. back = Render().CreateTextureFullQuality(_FL_, s);
  45. }
  46. s = reader.String().c_str();
  47. if(s && s[0])
  48. {
  49. // mask = Render().CreateTexture(_FL_, s);
  50. mask = Render().CreateTextureFullQuality(_FL_, s);
  51. }
  52. string str = reader.LocString();
  53. dword textColor = reader.Colors().GetDword();
  54. font = Render().CreateFont(reader.String().c_str());
  55. float size = reader.Float();
  56. if(font)
  57. {
  58. float scale = Render().GetScreenInfo2D().dwWidth/1024.0f;
  59. font->SetHeight(size*scale);
  60. font->SetColor(textColor);
  61. font->SetTechnique("Interface_TaskviewerFont");
  62. }
  63. w = reader.Float();
  64. h = reader.Float();
  65. SetText(str);
  66. showTime = currentTime = reader.Float();
  67. showPosition = 0.0f;
  68. drawPriority = reader.Long();
  69. Show(reader.Bool());
  70. Activate(reader.Bool());
  71. return true;
  72. }
  73. //Инициализировать объект
  74. bool TaskViewer::EditMode_Create(MOPReader & reader)
  75. {
  76. Create(reader);
  77. return true;
  78. }
  79. //Обновить параметры
  80. bool TaskViewer::EditMode_Update(MOPReader & reader)
  81. {
  82. Create(reader);
  83. return true;
  84. }
  85. //Обработчик команд для объекта
  86. void TaskViewer::Command(const char * id, dword numParams, const char ** params)
  87. {
  88. if( numParams < 1 )
  89. return;
  90. if( string::IsEmpty(id))
  91. return;
  92. if( string::IsEqual(id,"Task"))
  93. {
  94. SetText(params[0]);
  95. }
  96. }
  97. //Показать/скрыть объект
  98. void TaskViewer::Show(bool isShow)
  99. {
  100. if(EditMode_IsOn())
  101. {
  102. if(EditMode_IsSelect())
  103. {
  104. isShow = true;
  105. }
  106. }
  107. MissionObject::Show(isShow);
  108. if(isShow)
  109. {
  110. LogicDebug("Show");
  111. Activate(false);
  112. Activate(IsActive());
  113. }else{
  114. LogicDebug("Hide");
  115. DelUpdate(&TaskViewer::Draw);
  116. }
  117. }
  118. //Активировать
  119. void TaskViewer::Activate(bool isActive)
  120. {
  121. if(EditMode_IsOn())
  122. {
  123. if(EditMode_IsSelect())
  124. {
  125. isActive = true;
  126. }
  127. }
  128. bool isChange = isActive != IsActive();
  129. MissionObject::Activate(isActive);
  130. if(!IsShow())
  131. {
  132. LogicDebug("Save active state, but not applyed, becose element not visible");
  133. return;
  134. }
  135. if(isActive)
  136. {
  137. LogicDebug("Activate");
  138. if(isChange)
  139. {
  140. showPosition = 0.0f;
  141. }
  142. currentTime = showTime;
  143. if(back || mask)
  144. {
  145. SetUpdate(&TaskViewer::Draw, ML_GUI3 + drawPriority);
  146. }
  147. }else{
  148. LogicDebug("Deactivate");
  149. }
  150. }
  151. #ifndef MIS_STOP_EDIT_FUNCS
  152. //Выделить объект
  153. void TaskViewer::EditMode_Select(bool isSelect)
  154. {
  155. MissionObject::EditMode_Select(isSelect);
  156. Show(IsShow());
  157. Activate(IsActive());
  158. }
  159. #endif
  160. //Нарисовать модельку
  161. void _cdecl TaskViewer::Draw(float dltTime, long level)
  162. {
  163. if(InterfaceUtils::IsHide()) return;
  164. if(EditMode_IsOn())
  165. {
  166. if(!EditMode_IsSelect()) return;
  167. }else{
  168. if(showTime > 0.0f)
  169. {
  170. currentTime -= dltTime;
  171. if(currentTime <= 0.0f)
  172. {
  173. Activate(false);
  174. }
  175. }
  176. }
  177. //Анализируем смещение
  178. const float moveSpeed = 2.0f;
  179. if(IsActive())
  180. {
  181. showPosition += dltTime*moveSpeed;
  182. if(showPosition > 1.0f)
  183. {
  184. showPosition = 1.0f;
  185. }
  186. }else{
  187. showPosition += dltTime*moveSpeed;
  188. if(showPosition >= 2.0f)
  189. {
  190. showPosition = 0.0f;
  191. if(!EditMode_IsOn())
  192. {
  193. DelUpdate(&TaskViewer::Draw);
  194. }
  195. return;
  196. }
  197. }
  198. if(EditMode_IsOn())
  199. {
  200. if(!EditMode_IsSelect()) return;
  201. showPosition = 1.0f;
  202. }
  203. //Сохраняем текущие параметры
  204. RENDERVIEWPORT savedVP = Render().GetViewport();
  205. Matrix savedView = Render().GetView();
  206. Matrix savedPrj = Render().GetProjection();
  207. //Новый вьюпорт
  208. // dword width = Render().GetScreenInfo().dwWidth;
  209. // dword height = Render().GetScreenInfo().dwHeight;
  210. const RENDERVIEWPORT &nat = Render().GetViewport();
  211. dword width = nat.Width;
  212. dword height = nat.Height;
  213. RENDERVIEWPORT vp;
  214. vp.Width = dword(w*width);
  215. vp.Height = dword(h*height*InterfaceUtils::AspectRatio(Render()));
  216. vp.X = nat.X + 0;
  217. vp.Y = nat.Y + height - vp.Height - 1 - InterfaceUtils::HideFieldSize(Render());
  218. if(vp.Width < 16) vp.Width = 16;
  219. if(vp.Width > width) vp.Width = width;
  220. if(vp.Height < 16) vp.Height = 16;
  221. if(vp.Y + vp.Height > height) vp.Height = height - vp.Y;
  222. vp.MinZ = 0.0f;
  223. vp.MaxZ = 1.0f;
  224. Render().SetViewport(vp);
  225. //Рисуем элементы
  226. RENDERRECT rect;
  227. rect.x1 = vp.X;
  228. rect.y1 = vp.Y;
  229. rect.x2 = vp.X + vp.Width - 1;
  230. rect.y2 = vp.Y + vp.Height - 1;
  231. Render().Clear(1, &rect, CLEAR_ZBUFFER, 0, 1.0f, 0);
  232. if(mask)
  233. {
  234. vertices[0].z = showPosition;
  235. vertices[1].z = showPosition;
  236. vertices[2].z = showPosition;
  237. vertices[3].z = showPosition;
  238. texVar->SetTexture(mask);
  239. Render().DrawPrimitiveUP(Interface_TaskviewerPictureMask_id, PT_TRIANGLESTRIP, 2, vertices, sizeof(vertices[0]));
  240. texVar->SetTexture(null);
  241. }
  242. if(back)
  243. {
  244. vertices[0].z = 1.0f;
  245. vertices[1].z = 1.0f;
  246. vertices[2].z = 1.0f;
  247. vertices[3].z = 1.0f;
  248. texVar->SetTexture(back);
  249. Render().DrawPrimitiveUP(Interface_TaskviewerPicture_id, PT_TRIANGLESTRIP, 2, vertices, sizeof(vertices[0]));
  250. texVar->SetTexture(null);
  251. }
  252. if(font && lines > 0)
  253. {
  254. float y = (vp.Height - font->GetHeight()*(lines-1))*0.5f;
  255. for(long i = 0; i < lines; i++)
  256. {
  257. float x = (vp.Width - lines[i].width)*0.5f;
  258. font->Print(x, y, lines[i].text);
  259. y += lines[i].height;
  260. }
  261. }
  262. //Востанавливаем параметры
  263. Render().SetViewport(savedVP);
  264. Render().SetView(savedView);
  265. Render().SetProjection(savedPrj);
  266. }
  267. //Установить текст
  268. void TaskViewer::SetText(const char * rawText)
  269. {
  270. lines.DelAll();
  271. totalTextHeight = 0.0f;
  272. string str;
  273. if(!rawText) rawText = "";
  274. if(rawText[0] == '#')
  275. {
  276. //Подменим идентификатор на реальную строку
  277. // потом
  278. str = rawText;
  279. }else{
  280. str = rawText;
  281. }
  282. InterfaceUtils::WordWrapString(str, font, w*Render().GetScreenInfo2D().dwWidth);
  283. //Разделяем строки
  284. dword index = lines.Add();
  285. for(dword i = 0; i < str.Size(); i++)
  286. {
  287. if(str[i] == '\\')
  288. {
  289. if(i + 1 < str.Size())
  290. {
  291. if(str[i + 1] == 'n')
  292. {
  293. if(lines[index].text.IsEmpty())
  294. {
  295. lines[index].text = " ";
  296. }
  297. index = lines.Add();
  298. i++;
  299. continue;
  300. }else
  301. if(str[i + 1] >= '0' && str[i + 1] <= '9')
  302. {
  303. long num = 0;
  304. for(i++; i < str.Size(); i++)
  305. {
  306. if(str[i] >= '0' && str[i] <= '9')
  307. {
  308. num = num*10 + str[i];
  309. }
  310. }
  311. if(num < 1) num = 1;
  312. if(num > 255) num = 255;
  313. lines[index].text += char(num);
  314. continue;
  315. }
  316. }
  317. }
  318. else
  319. if(str[i] == '\n')
  320. {
  321. if(lines[index].text.IsEmpty())
  322. {
  323. lines[index].text = " ";
  324. }
  325. index = lines.Add();
  326. continue;
  327. }
  328. lines[index].text += str[i];
  329. }
  330. if(font)
  331. {
  332. for(long i = 0; i < lines; i++)
  333. {
  334. lines[i].width = font->GetLength(lines[i].text);
  335. lines[i].height = font->GetHeight(lines[i].text);
  336. totalTextHeight += lines[i].height;
  337. }
  338. }
  339. }
  340. const char * TaskViewer::comment =
  341. "UI element for show task description.\n"
  342. "Commands:\n"
  343. " Task [new string]\n"
  344. " \n"
  345. "For next line use \"\\n\" \n"
  346. " ";
  347. MOP_BEGINLISTCG(TaskViewer, "Task viewer", '1.00', 100000000, TaskViewer::comment, "Interface")
  348. MOP_STRING("Background", "")
  349. MOP_STRING("Show mask", "")
  350. MOP_LOCSTRING("Text")
  351. MOP_COLOR("Text color", Color(0xffffffffL))
  352. MOP_STRING("Font name", "DemoFont")
  353. MOP_FLOATEX("Font size", 20.0f, 4.0f, 1000.0f)
  354. MOP_FLOATEX("Width", 0.87f, 0.0f, 1.0f)
  355. MOP_FLOATEX("Height", 0.125f, 0.0f, 1.0f)
  356. MOP_FLOATEXC("Active time", 3.3f, 0.0f, 100.0f, "For active all time set 0")
  357. MOP_LONG("Draw priority", 0)
  358. MOP_BOOLC("Show", true, "Show or hide instantly")
  359. MOP_BOOLC("Active", true, "Show or hide with animation")
  360. MOP_ENDLIST(TaskViewer)