efimage.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. //
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class JoystickImageImpl : public JoystickImage {
  8. private:
  9. TArray<TRef<ModifiableNumber> , 2> m_ppnumber;
  10. TArray<TRef<ModifiableBoolean>, 3> m_ppboolButton;
  11. bool m_bJoystickEnabled;
  12. bool m_bButtonsEnabled;
  13. bool m_bJustEnabled;
  14. float m_sensitivity;
  15. public:
  16. JoystickImageImpl(float sensitivity) :
  17. m_sensitivity(sensitivity),
  18. m_bJoystickEnabled(false),
  19. m_bButtonsEnabled(false),
  20. m_bJustEnabled(false)
  21. {
  22. m_ppnumber[0] = new ModifiableNumber(0);
  23. m_ppnumber[1] = new ModifiableNumber(0);
  24. for (int index = 0; index < m_ppboolButton.GetCount(); index++) {
  25. m_ppboolButton[index] = new ModifiableBoolean(false);
  26. }
  27. }
  28. //////////////////////////////////////////////////////////////////////////////
  29. //
  30. // JoystickImage members
  31. //
  32. //////////////////////////////////////////////////////////////////////////////
  33. void SetEnabled(bool bJoystickEnabled, bool bButtonsEnabled)
  34. {
  35. if (m_bJoystickEnabled != bJoystickEnabled) {
  36. m_bJoystickEnabled = bJoystickEnabled;
  37. if (m_bJoystickEnabled) {
  38. m_bJustEnabled = true;
  39. } else {
  40. m_ppnumber[0]->SetValue(0);
  41. m_ppnumber[1]->SetValue(0);
  42. }
  43. }
  44. if (m_bButtonsEnabled != bButtonsEnabled) {
  45. m_bButtonsEnabled = bButtonsEnabled;
  46. if (!m_bButtonsEnabled) {
  47. for (int index = 0; index < m_ppboolButton.GetCount(); index++) {
  48. m_ppboolButton[index]->SetValue(false);
  49. }
  50. }
  51. }
  52. }
  53. void SetSensitivity(float sensitivity)
  54. {
  55. m_sensitivity = sensitivity;
  56. }
  57. bool GetJoystickEnabled()
  58. {
  59. return m_bJoystickEnabled;
  60. }
  61. bool GetButtonsEnabled()
  62. {
  63. return m_bButtonsEnabled;
  64. }
  65. //////////////////////////////////////////////////////////////////////////////
  66. //
  67. // JoystickInputStream members
  68. //
  69. //////////////////////////////////////////////////////////////////////////////
  70. bool HasForceFeedback()
  71. {
  72. return false;
  73. }
  74. void CreateEffects()
  75. {
  76. }
  77. void PlayFFEffect(short effectID, LONG lDirection)
  78. {
  79. }
  80. ZString GetShortDescription(int index)
  81. {
  82. return "Mouse";
  83. }
  84. ZString GetDescription()
  85. {
  86. return "Mouse";
  87. }
  88. ZString GetValueDescription(int id)
  89. {
  90. switch(id) {
  91. case 0: return "Left";
  92. case 1: return "Right";
  93. case 2: return "Middle";
  94. }
  95. return ZString();
  96. }
  97. //////////////////////////////////////////////////////////////////////////////
  98. //
  99. // InputStream members
  100. //
  101. //////////////////////////////////////////////////////////////////////////////
  102. int GetValueCount()
  103. {
  104. return 2;
  105. }
  106. int GetButtonCount()
  107. {
  108. return 3;
  109. }
  110. Boolean* IsDown(int id)
  111. {
  112. return m_ppboolButton[id];
  113. }
  114. Number* GetValue(int id)
  115. {
  116. return m_ppnumber[id];
  117. }
  118. ButtonEvent::Source* GetEventSource()
  119. {
  120. ZUnimplemented();
  121. return NULL;
  122. }
  123. //////////////////////////////////////////////////////////////////////////////
  124. //
  125. // IMouseInput members
  126. //
  127. //////////////////////////////////////////////////////////////////////////////
  128. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  129. {
  130. return (m_bJoystickEnabled || m_bButtonsEnabled) ? MouseResultHit() : MouseResult();
  131. }
  132. void MouseMove(IInputProvider* pprovider, const Point& pointMouse, bool bCaptured, bool bInside)
  133. {
  134. if (m_bJoystickEnabled) {
  135. if (m_bJustEnabled) {
  136. m_bJustEnabled = false;
  137. } else {
  138. float dx = (pointMouse.X() - 320) * m_sensitivity;
  139. float dy = (pointMouse.Y() - 240) * m_sensitivity;
  140. Point
  141. point(
  142. m_ppnumber[0]->GetValue() - dx,
  143. m_ppnumber[1]->GetValue() - dy
  144. );
  145. float length = point.Length();
  146. if (length > 1) {
  147. point = point / length;
  148. }
  149. m_ppnumber[0]->SetValue(point.X());
  150. m_ppnumber[1]->SetValue(point.Y());
  151. }
  152. pprovider->SetCursorPos(Point(320, 240));
  153. }
  154. }
  155. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  156. {
  157. if (m_bButtonsEnabled) {
  158. if (button <= 3) {
  159. m_ppboolButton[button]->SetValue(bDown);
  160. }
  161. }
  162. return MouseResult();
  163. }
  164. };
  165. TRef<JoystickImage> CreateJoystickImage(float sensitivity)
  166. {
  167. return new JoystickImageImpl(sensitivity);
  168. }
  169. //////////////////////////////////////////////////////////////////////////////
  170. //
  171. // BlendImage
  172. //
  173. //////////////////////////////////////////////////////////////////////////////
  174. class BlendColorImage : public WrapImage {
  175. private:
  176. ColorValue* GetColor() { return ColorValue::Cast(GetChild(1)); }
  177. public:
  178. BlendColorImage(Image* pimage, ColorValue* pcolor) :
  179. WrapImage(pimage, pcolor)
  180. {
  181. }
  182. void Render(Context* pcontext)
  183. {
  184. WrapImage::Render(pcontext);
  185. pcontext->SetBlendMode(BlendModeSourceAlpha);
  186. pcontext->FillRect(GetBounds().GetRect(), GetColor()->GetValue());
  187. }
  188. };
  189. TRef<Image> CreateBlendColorImage(Image* pimage, ColorValue* pcolor)
  190. {
  191. return new BlendColorImage(pimage, pcolor);
  192. }
  193. //////////////////////////////////////////////////////////////////////////////
  194. //
  195. // String Grid Image
  196. //
  197. //////////////////////////////////////////////////////////////////////////////
  198. class StringGridImageImpl : public StringGridImage {
  199. private:
  200. //////////////////////////////////////////////////////////////////////////////
  201. //
  202. // Data members
  203. //
  204. //////////////////////////////////////////////////////////////////////////////
  205. TRef<IEngineFont> m_pfont;
  206. TVector<TVector<ZString> > m_vvstr;
  207. TVector<Color> m_vcolor;
  208. TVector<float> m_vColumn;
  209. float m_ysizeRow;
  210. public:
  211. //////////////////////////////////////////////////////////////////////////////
  212. //
  213. // Constructor
  214. //
  215. //////////////////////////////////////////////////////////////////////////////
  216. StringGridImageImpl(int columns, int rows, IEngineFont* pfont) :
  217. m_pfont(pfont),
  218. m_vvstr(rows),
  219. m_vcolor(rows),
  220. m_vColumn(columns)
  221. {
  222. for (int index = 0; index < rows; index++) {
  223. m_vvstr.Get(index).SetCount(columns);
  224. }
  225. m_ysizeRow = (float)(m_pfont->GetTextExtent("W").Y());
  226. }
  227. //////////////////////////////////////////////////////////////////////////////
  228. //
  229. // StringGrid methods
  230. //
  231. //////////////////////////////////////////////////////////////////////////////
  232. void SetString(int row, int column, const ZString& str)
  233. {
  234. if (m_vvstr.Get(row).Get(column) != str) {
  235. m_vvstr.Get(row).Set(column, str);
  236. Changed();
  237. }
  238. }
  239. void SetColor(int row, const Color& color)
  240. {
  241. if (m_vcolor.Get(row) != color) {
  242. m_vcolor.Set(row, color);
  243. Changed();
  244. }
  245. }
  246. void SetSize(int rows, int columns)
  247. {
  248. m_vvstr.SetCount(rows);
  249. m_vcolor.SetCount(rows);
  250. m_vColumn.SetCount(columns);
  251. for (int index = 0; index < rows; index++) {
  252. m_vvstr.Get(index).SetCount(columns);
  253. }
  254. Changed();
  255. }
  256. //////////////////////////////////////////////////////////////////////////////
  257. //
  258. // Image methods
  259. //
  260. //////////////////////////////////////////////////////////////////////////////
  261. void CalcBounds()
  262. {
  263. int rows = m_vvstr.GetCount();
  264. int columns = m_vvstr[0].GetCount();
  265. float xsize = 0;
  266. for (int column = 0; column < columns; column++) {
  267. m_vColumn.Set(column, xsize);
  268. float xsizeColumn = 0;
  269. for (int row = 0; row < rows; row++) {
  270. WinPoint size = m_pfont->GetTextExtent(m_vvstr[row][column]);
  271. if ((float)size.X() > xsizeColumn) {
  272. xsizeColumn = (float)size.X();
  273. }
  274. }
  275. xsize += xsizeColumn + 4;
  276. }
  277. m_bounds.SetRect(
  278. Rect(
  279. 0,
  280. (float)(-(rows * m_ysizeRow)),
  281. (float)xsize,
  282. 0
  283. )
  284. );
  285. }
  286. void Render(Context* pcontext)
  287. {
  288. int rows = m_vvstr.GetCount();
  289. int columns = m_vvstr[0].GetCount();
  290. for (int row = 0; row < rows; row++) {
  291. float y = -(m_ysizeRow * (float)(row + 1));
  292. for (int column = 0; column < columns; column++) {
  293. pcontext->DrawString(
  294. m_pfont,
  295. m_vcolor[row],
  296. Point(
  297. m_vColumn[column],
  298. y
  299. ),
  300. m_vvstr[row][column]
  301. );
  302. }
  303. }
  304. }
  305. };
  306. TRef<StringGridImage> CreateStringGridImage(int columns, int rows, IEngineFont* pfont)
  307. {
  308. return new StringGridImageImpl(columns, rows, pfont);
  309. }
  310. //////////////////////////////////////////////////////////////////////////////
  311. //
  312. // Muzzle Flare
  313. //
  314. //////////////////////////////////////////////////////////////////////////////
  315. class MuzzleFlareImageImpl : public MuzzleFlareImage {
  316. class FlareData {
  317. public:
  318. float m_time;
  319. Point m_point;
  320. bool m_bRendered;
  321. };
  322. TList<FlareData, DefaultNoEquals> m_list;
  323. TRef<Image> m_pimage;
  324. bool m_bVisible;
  325. Number* GetTime() { return Number::Cast(GetChild(0)); }
  326. public:
  327. MuzzleFlareImageImpl(Modeler* pmodeler, Number* ptime) :
  328. MuzzleFlareImage(ptime),
  329. m_bVisible(true)
  330. {
  331. m_pimage = pmodeler->LoadImage(AWF_EFFECT_MUZZLE_FLARE, true);
  332. }
  333. void AddFlare(const Point& point, float duration)
  334. {
  335. m_list.PushFront();
  336. m_list.GetFront().m_point = point;
  337. m_list.GetFront().m_time = GetTime()->GetValue() + duration;
  338. m_list.GetFront().m_bRendered = false;
  339. }
  340. void SetVisible(bool bVisible)
  341. {
  342. m_bVisible = bVisible;
  343. }
  344. void Evaluate()
  345. {
  346. float time = GetTime()->GetValue();
  347. TList<FlareData, DefaultNoEquals>::Iterator iter(m_list);
  348. while (!iter.End()) {
  349. if (iter.Value().m_bRendered && time > iter.Value().m_time) {
  350. iter.Remove();
  351. } else {
  352. iter.Next();
  353. }
  354. }
  355. MuzzleFlareImage::Evaluate();
  356. }
  357. void Render(Context* pcontext)
  358. {
  359. /*
  360. if (m_bVisible) {
  361. pcontext->SetBlendMode(BlendModeAdd);
  362. pcontext->SetShadeMode(ShadeModeGouraud);
  363. TList<FlareData, DefaultNoEquals>::Iterator iter(m_list);
  364. while (!iter.End()) {
  365. iter.Value().m_bRendered = true;
  366. pcontext->PushState();
  367. pcontext->PreTranslate(pcontext->TransformNDCToImage(iter.Value().m_point));
  368. pcontext->PreScale(10);
  369. pcontext->DrawImage(m_pimage->GetSurface(), true);
  370. pcontext->PopState();
  371. iter.Next();
  372. }
  373. }
  374. */
  375. }
  376. };
  377. TRef<MuzzleFlareImage> CreateMuzzleFlareImage(Modeler* pmodeler, Number* ptime)
  378. {
  379. return new MuzzleFlareImageImpl(pmodeler, ptime);
  380. }
  381. //////////////////////////////////////////////////////////////////////////////
  382. //
  383. // PosterImage
  384. //
  385. //////////////////////////////////////////////////////////////////////////////
  386. class PosterImageImpl : public PosterImage {
  387. class PosterData {
  388. public:
  389. Vector m_vec;
  390. TRef<Image> m_pimage;
  391. float m_scale;
  392. Matrix m_mat;
  393. };
  394. TList<PosterData, DefaultNoEquals> m_list;
  395. Viewport* GetViewport() { return Viewport::Cast(GetChild(0)); }
  396. Camera* GetCamera() { return GetViewport()->GetCamera(); }
  397. RectValue* GetViewRect() { return GetViewport()->GetViewRect(); }
  398. public:
  399. PosterImageImpl(Viewport* pviewport) :
  400. PosterImage(pviewport)
  401. {
  402. }
  403. void AddPoster(Image* pimage, const Vector& vec, float scale)
  404. {
  405. m_list.PushFront();
  406. m_list.GetFront().m_pimage = pimage;
  407. m_list.GetFront().m_vec = vec;
  408. m_list.GetFront().m_scale = scale;
  409. m_list.GetFront().m_mat.SetScale(scale);
  410. m_list.GetFront().m_mat.LookAtFrom(Vector(0, 0, 0), vec * 400.0f, Vector(0, 1, 0));
  411. }
  412. void Render(Context* pcontext)
  413. {
  414. Rect rect = GetViewRect()->GetValue();
  415. pcontext->Translate(rect.Center());
  416. pcontext->Scale2(0.5f * rect.XSize());
  417. pcontext->Begin3DLayer(GetCamera(), false);
  418. #ifndef FixPermedia
  419. pcontext->SetShadeMode(ShadeModeCopy);
  420. #endif
  421. TList<PosterData, DefaultNoEquals>::Iterator iter(m_list);
  422. while (!iter.End()) {
  423. PosterData& data = iter.Value();
  424. Point point;
  425. if (pcontext->TransformDirectionToImage(data.m_vec, point)) {
  426. pcontext->SetTexture(data.m_pimage->GetSurface());
  427. pcontext->PushState();
  428. pcontext->Multiply(data.m_mat);
  429. #ifdef FixPermedia
  430. Geo::GetWhiteEmissiveSquare()->Render(pcontext);
  431. #else
  432. Geo::GetSquare()->Render(pcontext);
  433. #endif
  434. pcontext->PopState();
  435. }
  436. iter.Next();
  437. }
  438. pcontext->End3DLayer();
  439. }
  440. };
  441. TRef<PosterImage> CreatePosterImage(Viewport* pviewport)
  442. {
  443. return new PosterImageImpl(pviewport);
  444. }
  445. //////////////////////////////////////////////////////////////////////////////
  446. //
  447. // LensFlareImage
  448. //
  449. //////////////////////////////////////////////////////////////////////////////
  450. class LensFlareData {
  451. public:
  452. float m_pos;
  453. float m_scale;
  454. Color m_color;
  455. int m_index;
  456. LensFlareData(int index, float pos, float scale, const Color& color) :
  457. m_pos(pos),
  458. m_scale(scale),
  459. m_color(color),
  460. m_index(index)
  461. {
  462. }
  463. LensFlareData(const LensFlareData& data) :
  464. m_pos(data.m_pos),
  465. m_scale(data.m_scale),
  466. m_color(data.m_color),
  467. m_index(data.m_index)
  468. {
  469. }
  470. };
  471. LensFlareData g_lensFlareData[] =
  472. {
  473. LensFlareData(2, -1.00, 1.50, Color::White()),
  474. LensFlareData(0, -0.75, 1.00, Color::Blue() ),
  475. LensFlareData(0, -0.50, 0.50, Color::Blue() ),
  476. LensFlareData(0, -0.25, 0.25, Color::Blue() ),
  477. LensFlareData(0, 0.25, 0.25, Color::Blue() ),
  478. LensFlareData(0, 0.50, 0.50, Color::Blue() ),
  479. LensFlareData(0, 0.75, 1.00, Color::Blue() ),
  480. LensFlareData(1, 1.00, 1.50, Color::White() )
  481. };
  482. class LensFlareImageImpl : public LensFlareImage {
  483. TRef<Surface> m_psurfaces[3];
  484. Vector m_vec;
  485. Viewport* GetViewport() { return Viewport::Cast(GetChild(0)); }
  486. Camera* GetCamera() { return GetViewport()->GetCamera(); }
  487. RectValue* GetViewRect() { return GetViewport()->GetViewRect(); }
  488. public:
  489. LensFlareImageImpl(Modeler* pmodeler, Viewport* pviewport) :
  490. LensFlareImage(pviewport),
  491. m_vec(0, 0, 1)
  492. {
  493. m_psurfaces[0] =
  494. pmodeler
  495. ->LoadImage(AWF_EFFECT_LENS_FLARE_MID_RING, true)
  496. ->GetSurface();
  497. m_psurfaces[1] =
  498. pmodeler
  499. ->LoadImage(AWF_EFFECT_LENS_FLARE_STAR_AURA, true)
  500. ->GetSurface();
  501. m_psurfaces[2] =
  502. pmodeler
  503. ->LoadImage(AWF_EFFECT_LENS_FLARE_END_RING, true)
  504. ->GetSurface();
  505. }
  506. void SetLightDirection(const Vector& vec)
  507. {
  508. m_vec = vec;
  509. }
  510. void Render(Context* pcontext)
  511. {
  512. m_vec = Vector(0, 0, 1);
  513. const Rect& rect = GetViewRect()->GetValue();
  514. Point pointCenter = rect.Center();
  515. float scale = rect.XSize() / 800.0f;
  516. Point pointLight;
  517. if (GetCamera()->TransformDirectionToImage(m_vec, pointLight)) {
  518. pointLight = rect.TransformNDCToImage(pointLight);
  519. if (rect.Inside(pointLight)) {
  520. pointLight = pointLight - pointCenter;
  521. pcontext->SetBlendMode(BlendModeAdd);
  522. pcontext->SetShadeMode(ShadeModeFlat);
  523. pcontext->Translate(pointCenter);
  524. int count = ArrayCount(g_lensFlareData);
  525. for(int index = 0; index < count; index++) {
  526. pcontext->PushState();
  527. pcontext->Translate(pointLight * g_lensFlareData[index].m_pos);
  528. pcontext->Scale2(scale * g_lensFlareData[index].m_scale);
  529. float angle = 0;
  530. int indexSurface = g_lensFlareData[index].m_index;
  531. if (indexSurface == 1) {
  532. pcontext->Rotate((pointLight.X() + pointLight.Y()) / 200.0f);
  533. }
  534. pcontext->DrawImage3D(m_psurfaces[indexSurface], g_lensFlareData[index].m_color, true);
  535. pcontext->PopState();
  536. }
  537. }
  538. }
  539. }
  540. };
  541. TRef<LensFlareImage> CreateLensFlareImage(Modeler* pmodeler, Viewport* pviewport)
  542. {
  543. return new LensFlareImageImpl(pmodeler, pviewport);
  544. }