Progress.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "Progress.h"
  2. #include "..\utils\InterfaceUtils.h"
  3. Progress:: Progress()
  4. {
  5. buffer = null;
  6. m_drawPriority = 0;
  7. texture1 = null;
  8. texture2 = null;
  9. Circular_id = null;
  10. BarTexture = null;
  11. }
  12. Progress::~Progress()
  13. {
  14. RELEASE(buffer)
  15. RELEASE(texture1)
  16. RELEASE(texture2)
  17. BarTexture = NULL;
  18. }
  19. void Progress::Restart()
  20. {
  21. ReCreate();
  22. }
  23. bool Progress::Create(MOPReader &reader)
  24. {
  25. if( !BarTexture )
  26. BarTexture = Render().GetTechniqueGlobalVariable("CircularTexture",_FL_);
  27. if( !Circular_id )
  28. Render().GetShaderId("Circular",Circular_id);
  29. if( !buffer )
  30. CreateBuffer();
  31. InitParams(reader);
  32. return true;
  33. }
  34. bool Progress::EditMode_Update(MOPReader &reader)
  35. {
  36. InitParams(reader);
  37. return true;
  38. }
  39. void Progress::Activate(bool isActive)
  40. {
  41. //
  42. }
  43. void Progress::Show(bool isShow)
  44. {
  45. MissionObject::Show(isShow);
  46. if( isShow )
  47. SetUpdate(&Progress::Draw,ML_GUI1 - 1 + m_drawPriority);
  48. else
  49. DelUpdate(&Progress::Draw);
  50. if( isShow )
  51. LogicDebug("Show");
  52. else
  53. LogicDebug("Hide");
  54. }
  55. void _cdecl Progress::Draw(float dltTime, long level)
  56. {
  57. if( InterfaceUtils::IsHide())
  58. return;
  59. float m_x = this->m_x*m_aspect;
  60. float t = m_pos/m_max;
  61. float p = t*m_w;
  62. Vertex *v = (Vertex *)buffer->Lock(0,0,LOCK_DISCARD);
  63. v->p = Vector(
  64. (m_x + 0.0f)*2.0f - 1.0f,
  65. 1.0f - (m_y + 0.0f)*2.0f,0.0f);
  66. v->tu = 0.0f;
  67. v->tv = 0.0f;
  68. v->al = 1.0f;
  69. v++;
  70. v->p = Vector(
  71. (m_x + 0.0f)*2.0f - 1.0f,
  72. 1.0f - (m_y + m_h)*2.0f,0.0f);
  73. v->tu = 0.0f;
  74. v->tv = 1.0f;
  75. v->al = 1.0f;
  76. v++;
  77. v->p = Vector(
  78. (m_x + p)*2.0f - 1.0f,
  79. 1.0f - (m_y + 0.0f)*2.0f,0.0f);
  80. v->tu = t;
  81. v->tv = 0.0f;
  82. v->al = 1.0f;
  83. v++;
  84. v->p = Vector(
  85. (m_x + p)*2.0f - 1.0f,
  86. 1.0f - (m_y + m_h)*2.0f,0.0f);
  87. v->tu = t;
  88. v->tv = 1.0f;
  89. v->al = 1.0f;
  90. v++;
  91. v->p = Vector(
  92. (m_x + m_w)*2.0f - 1.0f,
  93. 1.0f - (m_y + 0.0f)*2.0f,0.0f);
  94. v->tu = 1.0f;
  95. v->tv = 0.0f;
  96. v->al = 1.0f;
  97. v++;
  98. v->p = Vector(
  99. (m_x + m_w)*2.0f - 1.0f,
  100. 1.0f - (m_y + m_h)*2.0f,0.0f);
  101. v->tu = 1.0f;
  102. v->tv = 1.0f;
  103. v->al = 1.0f;
  104. v++;
  105. buffer->Unlock();
  106. Render().SetStreamSource(0,buffer);
  107. if( texture1 )
  108. {
  109. if( BarTexture )
  110. BarTexture->SetTexture(texture1);
  111. Render().DrawPrimitive(Circular_id,
  112. PT_TRIANGLESTRIP,0,1*2);
  113. }
  114. if( texture2 )
  115. {
  116. if( BarTexture )
  117. BarTexture->SetTexture(texture2);
  118. Render().DrawPrimitive(Circular_id,
  119. PT_TRIANGLESTRIP,2,1*2);
  120. }
  121. }
  122. void Progress::Command(const char *id, dword numParams, const char **params)
  123. {
  124. if( string::IsEmpty(id))
  125. return;
  126. if( string::IsEqual(id,"set_max"))
  127. {
  128. if( numParams < 2 )
  129. {
  130. LogicDebugError("Command <set_max> error. Not enought parameters.");
  131. return;
  132. }
  133. m_max = (float)atof(params[0]);
  134. LogicDebug("Command <set_max>. Max pos = %.",m_max);
  135. }
  136. else
  137. if( string::IsEqual(id,"set_pos"))
  138. {
  139. if( numParams < 2 )
  140. {
  141. LogicDebugError("Command <set_pos> error. Not enought parameters.");
  142. return;
  143. }
  144. m_pos = (float)atof(params[0]);
  145. if( m_pos > m_max )
  146. m_pos = m_max;
  147. LogicDebug("Command <set_pos>. Cur pos = %.",m_pos);
  148. }
  149. else
  150. if( string::IsEqual(id,"restart"))
  151. {
  152. Restart();
  153. LogicDebug("Command <restart>. Bar was restarted.",m_pos);
  154. }
  155. else
  156. {
  157. LogicDebugError("Unknown command \"%s\".",id);
  158. }
  159. }
  160. void Progress::SetPos(float val)
  161. {
  162. m_pos = val;
  163. if( m_pos > m_max )
  164. m_pos = m_max;
  165. }
  166. void Progress::CreateBuffer()
  167. {
  168. // if( buffer )
  169. // buffer->Release();
  170. buffer = Render().CreateVertexBuffer(
  171. sizeof(Vertex)*(3)*2,
  172. sizeof(Vertex),
  173. _FL_,USAGE_WRITEONLY|USAGE_DYNAMIC,POOL_DEFAULT);
  174. Assert(buffer)
  175. }
  176. void Progress::InitAspect()
  177. {
  178. // все элементы создаются из расчета этого аспекта
  179. const float def_aspect = 16.0f/9.0f;
  180. float cx;
  181. float cy;
  182. if( EditMode_IsOn())
  183. {
  184. // cx = (float)Render().GetViewport().Width;
  185. // cy = (float)Render().GetViewport().Height;
  186. cx = (float)Render().GetFullScreenViewPort_2D().Width;
  187. cy = (float)Render().GetFullScreenViewPort_2D().Height;
  188. }
  189. else
  190. {
  191. cx = (float)Render().GetFullScreenViewPort_2D().Width;
  192. cy = (float)Render().GetFullScreenViewPort_2D().Height;
  193. }
  194. // аспект разрешения экрана
  195. float scr_aspect = cx/cy;
  196. // api->Trace("");
  197. // api->Trace(" WINDOW: asp = %f res = %.0fx%.0f",scr_aspect,cx,cy);
  198. // аспект пикселя
  199. float dot_aspect = InterfaceUtils::AspectRatio(Render());
  200. // api->Trace(" DOT: asp = %f",dot_aspect);
  201. // api->Trace("");
  202. // реальный аспект
  203. float cur_aspect = scr_aspect/dot_aspect;
  204. // m_aspect = def_aspect/cur_aspect;
  205. // m_aspect_native = cur_aspect;
  206. m_aspect = def_aspect/cur_aspect;
  207. }
  208. void Progress::InitParams(MOPReader &reader)
  209. {
  210. // m_aspect = InterfaceUtils::AspectRatio(Render());
  211. InitAspect();
  212. const char *t = reader.String().c_str();
  213. if( !texture1 || !string::IsEqual(texture1->GetName(),t))
  214. {
  215. if( texture1 )
  216. texture1->Release();
  217. // texture1 = Render().CreateTexture(_FL_,t);
  218. texture1 = Render().CreateTextureFullQuality(_FL_,t);
  219. }
  220. t = reader.String().c_str();
  221. if( !texture2 || !string::IsEqual(texture2->GetName(),t))
  222. {
  223. if( texture2 )
  224. texture2->Release();
  225. // texture2 = Render().CreateTexture(_FL_,t);
  226. texture2 = Render().CreateTextureFullQuality(_FL_,t);
  227. }
  228. m_x = reader.Float()*0.01f;
  229. m_y = reader.Float()*0.01f;
  230. m_w = reader.Float()*0.01f;
  231. m_h = reader.Float()*0.01f;
  232. m_max = reader.Float();
  233. m_pos = reader.Float();
  234. if( m_pos > m_max )
  235. m_pos = m_max;
  236. m_drawPriority = reader.Long();
  237. Show(reader.Bool());
  238. }
  239. MOP_BEGINLISTCG(Progress, "Progress", '1.00', 100, "", "Interface")
  240. MOP_STRING("texture1", "")
  241. MOP_STRING("texture2", "")
  242. MOP_FLOAT("x", 0.0f)
  243. MOP_FLOAT("y", 0.0f)
  244. MOP_FLOAT("w", 1.0f)
  245. MOP_FLOAT("h", 1.0f)
  246. MOP_FLOAT("max", 1.0f)
  247. MOP_FLOAT("pos", 0.0f)
  248. MOP_LONG("Draw priority", 0)
  249. MOP_BOOL("Show", true)
  250. MOP_ENDLIST(Progress)