image.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Empty Image
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class EmptyImage : public Image {
  8. public:
  9. EmptyImage() : Image()
  10. {
  11. }
  12. //
  13. // Value members
  14. //
  15. ZString GetString(int indent)
  16. {
  17. return "emptyImage";
  18. }
  19. ZString GetFunctionName()
  20. {
  21. return "emptyImage";
  22. }
  23. void Write(IMDLBinaryFile* pfile)
  24. {
  25. pfile->WriteReference("emptyImage");
  26. }
  27. };
  28. TRef<Image> g_pemptyImage = new EmptyImage();
  29. Image* Image::GetEmpty()
  30. {
  31. return g_pemptyImage;
  32. }
  33. //////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Image
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. void Image::SetEmpty()
  39. {
  40. ChangeTo(GetEmpty());
  41. }
  42. const Bounds2& Image::GetBounds()
  43. {
  44. return m_bounds;
  45. }
  46. void Image::CalcBounds()
  47. {
  48. m_bounds = Bounds2::GetEmpty();
  49. }
  50. void Image::Evaluate()
  51. {
  52. CalcBounds();
  53. }
  54. void Image::Render(Context* pcontext)
  55. {
  56. TRef<Surface> psurface = GetSurface();
  57. if (psurface) {
  58. pcontext->DrawImage(psurface);
  59. }
  60. }
  61. TRef<Surface> Image::GetSurface()
  62. {
  63. return NULL;
  64. }
  65. MouseResult Image::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  66. {
  67. if (m_bounds.GetRect().Inside(point)) {
  68. return MouseResultHit();
  69. }
  70. return MouseResult();
  71. }
  72. //////////////////////////////////////////////////////////////////////////////
  73. //
  74. // Constant Image
  75. //
  76. //////////////////////////////////////////////////////////////////////////////
  77. ConstantImage::ConstantImage(Surface* psurface, const ZString& str) :
  78. m_psurface(psurface),
  79. m_str(str)
  80. {
  81. }
  82. const Bounds2& ConstantImage::GetBounds()
  83. {
  84. CalcBounds();
  85. return m_bounds;
  86. }
  87. void ConstantImage::CalcBounds()
  88. {
  89. m_bounds.SetRect(
  90. Rect(
  91. Point(0, 0),
  92. Point::Cast(m_psurface->GetSize())
  93. )
  94. );
  95. }
  96. TRef<Surface> ConstantImage::GetSurface()
  97. {
  98. return m_psurface;
  99. }
  100. void ConstantImage::Render(Context* pcontext)
  101. {
  102. pcontext->DrawImage(m_psurface);
  103. }
  104. ZString ConstantImage::GetString(int indent)
  105. {
  106. return
  107. "ImportImage(\""
  108. + m_str + "\", "
  109. + GetString(m_psurface->HasColorKey()) + ")";
  110. }
  111. ZString ConstantImage::GetFunctionName()
  112. {
  113. return "ImportImage";
  114. }
  115. void ConstantImage::Write(IMDLBinaryFile* pmdlFile)
  116. {
  117. pmdlFile->WriteReference("ImportImage");
  118. TRef<ZFile> pfile = pmdlFile->WriteBinary();
  119. m_psurface->Write(pfile);
  120. }
  121. MouseResult ConstantImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  122. {
  123. if (m_bounds.GetRect().Inside(point)) {
  124. if (m_psurface->HasColorKey()) {
  125. bool bHit = m_psurface->GetColor(WinPoint((int)point.X(), (int)point.Y())) != m_psurface->GetColorKey();
  126. m_psurface->ReleasePointer();
  127. if (bHit) {
  128. return MouseResultHit();
  129. }
  130. } else {
  131. return MouseResultHit();
  132. }
  133. }
  134. return MouseResult();
  135. }
  136. //////////////////////////////////////////////////////////////////////////////
  137. //
  138. //
  139. //
  140. //////////////////////////////////////////////////////////////////////////////
  141. class ConstantImage3D : public Image {
  142. private:
  143. TRef<Surface> m_psurface;
  144. ColorValue* GetColor() { return ColorValue::Cast(GetChild(0)); }
  145. public:
  146. ConstantImage3D(Surface* psurface, ColorValue* pcolor) :
  147. Image(pcolor),
  148. m_psurface(psurface)
  149. {
  150. }
  151. const Bounds2& GetBounds()
  152. {
  153. CalcBounds();
  154. return m_bounds;
  155. }
  156. void CalcBounds()
  157. {
  158. m_bounds.SetRect(
  159. Rect(
  160. Point(0, 0),
  161. Point::Cast(m_psurface->GetSize())
  162. )
  163. );
  164. }
  165. TRef<Surface> GetSurface()
  166. {
  167. return m_psurface;
  168. }
  169. void Render(Context* pcontext)
  170. {
  171. pcontext->DrawImage3D(m_psurface, GetColor()->GetValue());
  172. }
  173. ZString GetFunctionName()
  174. {
  175. return "ImportImage3D";
  176. }
  177. };
  178. TRef<Image> CreateConstantImage3D(Surface* psurface, ColorValue* pcolor)
  179. {
  180. return new ConstantImage3D(psurface, pcolor);
  181. }
  182. //////////////////////////////////////////////////////////////////////////////
  183. //
  184. // String Image
  185. //
  186. //////////////////////////////////////////////////////////////////////////////
  187. class StringImage : public Image {
  188. private:
  189. Justification m_justification;
  190. int m_width;
  191. int m_indent;
  192. TRef<IEngineFont> m_pfont;
  193. //////////////////////////////////////////////////////////////////////////////
  194. //
  195. // value accessors
  196. //
  197. //////////////////////////////////////////////////////////////////////////////
  198. StringValue* GetString() { return StringValue::Cast(GetChild(0)); }
  199. ColorValue* GetColor() { return ColorValue::Cast(GetChild(1)); }
  200. public:
  201. StringImage(
  202. Justification justification,
  203. IEngineFont* pfont,
  204. ColorValue* pcolor,
  205. int width,
  206. StringValue* pstring,
  207. int indent
  208. ) :
  209. Image(pstring, pcolor),
  210. m_justification(justification),
  211. m_pfont(pfont),
  212. m_width(width),
  213. m_indent(indent)
  214. {
  215. }
  216. //////////////////////////////////////////////////////////////////////////////
  217. //
  218. // StringImageImpl methods
  219. //
  220. //////////////////////////////////////////////////////////////////////////////
  221. ZString BreakLine(ZString& str, int width)
  222. {
  223. ZString strSave = str;
  224. //
  225. // Search forward for any new lines
  226. //
  227. int indexNewLine = str.Find("\n");
  228. if (indexNewLine == -1) {
  229. indexNewLine = str.GetLength();
  230. }
  231. //
  232. // Figure out how many characters fit on the line
  233. //
  234. int nchar = m_pfont->GetMaxTextLength(str, width, true);
  235. //
  236. // Chop off at the newline
  237. //
  238. if (nchar >= indexNewLine) {
  239. nchar = indexNewLine;
  240. } else {
  241. //
  242. // Search backwards for the first space
  243. //
  244. if (nchar != str.GetLength()) {
  245. for (int index = nchar - 1; index >= 0; index--) {
  246. if (str[index] == ' ') {
  247. while (index > 0) {
  248. if (str[index] != ' ') {
  249. nchar = index + 1;
  250. break;
  251. }
  252. index--;
  253. }
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. if (nchar < str.GetLength()) {
  260. str = strSave.RightOf(nchar + 1);
  261. return strSave.Left(nchar);
  262. } else {
  263. //
  264. // remove any leading spaces from the next line
  265. //
  266. str = strSave.RightOf(nchar);
  267. int length = str.GetLength();
  268. int index = 0;
  269. while (index < length && str[index] == ' ') {
  270. index++;
  271. }
  272. if (index != 0) {
  273. str = str.RightOf(index);
  274. }
  275. return strSave.Left(nchar);
  276. }
  277. }
  278. //////////////////////////////////////////////////////////////////////////////
  279. //
  280. // Image methods
  281. //
  282. //////////////////////////////////////////////////////////////////////////////
  283. void CalcBounds()
  284. {
  285. ZString str = GetString()->GetValue();
  286. int xsize = 0;
  287. int ysize = 0;
  288. int indent = 0;
  289. while (!str.IsEmpty()) {
  290. ZString strLine = BreakLine(str, m_width - indent);
  291. WinPoint size = m_pfont->GetTextExtent(strLine);
  292. xsize = max(xsize, size.X());
  293. ysize += size.Y();
  294. indent = m_indent;
  295. }
  296. m_bounds.SetRect(Rect(0, (float)(-ysize), (float)m_width, 0));
  297. }
  298. void Render(Context* pcontext)
  299. {
  300. ZString str = GetString()->GetValue();
  301. const Rect& rect = m_bounds.GetRect();
  302. int y = 0;
  303. int indent = 0;
  304. const Color& color = GetColor()->GetValue();
  305. while (!str.IsEmpty()) {
  306. ZString strLine = BreakLine(str, m_width - indent);
  307. WinPoint size = m_pfont->GetTextExtent(strLine);
  308. int x;
  309. if (m_justification == JustifyLeft()) {
  310. x = 0;
  311. } else if (m_justification == JustifyRight()) {
  312. x = m_width - size.X();
  313. } else if (m_justification == JustifyCenter()) {
  314. x = (m_width - size.X()) / 2;
  315. } else {
  316. ZError("Invalid Justification");
  317. }
  318. y -= size.Y();
  319. pcontext->DrawString(
  320. m_pfont,
  321. color,
  322. Point(
  323. (float)x + indent,
  324. (float)y
  325. ),
  326. strLine
  327. );
  328. indent = m_indent;
  329. }
  330. }
  331. //////////////////////////////////////////////////////////////////////////////
  332. //
  333. // Value members
  334. //
  335. //////////////////////////////////////////////////////////////////////////////
  336. ZString GetFunctionName() { return "StringImage"; }
  337. };
  338. TRef<Image> CreateStringImage(
  339. Justification justification,
  340. IEngineFont* pfont,
  341. ColorValue* pcolor,
  342. int width,
  343. StringValue* pstring,
  344. int indent
  345. ) {
  346. return new StringImage(justification, pfont, pcolor, width, pstring, indent);
  347. }
  348. //////////////////////////////////////////////////////////////////////////////
  349. //
  350. // GaugeImage
  351. //
  352. //////////////////////////////////////////////////////////////////////////////
  353. class GaugeImage : public Image {
  354. private:
  355. Justification m_justification;
  356. Rect m_rect;
  357. bool m_b3D;
  358. public:
  359. GaugeImage(
  360. Justification justification,
  361. Image* pimage,
  362. const Rect& rect,
  363. bool b3D,
  364. Number* pnumber
  365. ) :
  366. Image(pimage, pnumber),
  367. m_rect(rect),
  368. m_b3D(b3D),
  369. m_justification(justification)
  370. {
  371. }
  372. GaugeImage(
  373. Justification justification,
  374. Image* pimage,
  375. bool b3D,
  376. Number* pnumber
  377. ) :
  378. Image(pimage, pnumber),
  379. m_rect(pimage->GetBounds().GetRect()),
  380. m_b3D(b3D),
  381. m_justification(justification)
  382. {
  383. }
  384. //////////////////////////////////////////////////////////////////////////////
  385. //
  386. // GaugeImage methods
  387. //
  388. //////////////////////////////////////////////////////////////////////////////
  389. Image* GetImage() { return Image::Cast(GetChild(0)); }
  390. Number* GetNumber() { return Number::Cast(GetChild(1)); }
  391. //////////////////////////////////////////////////////////////////////////////
  392. //
  393. // Image methods
  394. //
  395. //////////////////////////////////////////////////////////////////////////////
  396. void CalcBounds()
  397. {
  398. m_bounds = GetImage()->GetBounds();
  399. }
  400. void Render(Context* pcontext)
  401. {
  402. TRef<Surface> psurface = GetImage()->GetSurface();
  403. float number = GetNumber()->GetValue();
  404. number = bound(number, 0.0f, 1.0f);
  405. Rect rect = m_rect;
  406. Point offset(0, 0);
  407. float xoffset = (float)(int)(m_rect.XSize() * number);
  408. float yoffset = (float)(int)(m_rect.YSize() * number);
  409. float xnegOffset = m_rect.XSize() - xoffset;
  410. float ynegOffset = m_rect.YSize() - yoffset;
  411. if (m_b3D) {
  412. if (m_justification == JustifyLeft()) {
  413. rect.SetXMax(m_rect.XMin() + xoffset);
  414. } else if (m_justification == JustifyRight()) {
  415. rect.SetXMin(m_rect.XMin() + xnegOffset);
  416. offset.SetX(xnegOffset);
  417. } else if (m_justification == JustifyTop()) {
  418. rect.SetYMin(m_rect.YMin() + ynegOffset);
  419. offset.SetY(ynegOffset);
  420. } else if (m_justification == JustifyBottom()) {
  421. rect.SetYMax(m_rect.YMin() + yoffset);
  422. } else {
  423. ZError("Bad Justification for GaugePane");
  424. }
  425. pcontext->DrawImage3D(psurface, rect, Color::White(), false, offset);
  426. } else {
  427. if (m_justification == JustifyLeft()) {
  428. rect.SetXMax(m_rect.XMin() + xoffset);
  429. } else if (m_justification == JustifyRight()) {
  430. rect.SetXMin(m_rect.XMin() + xnegOffset);
  431. offset.SetX(xnegOffset);
  432. } else if (m_justification == JustifyTop()) {
  433. rect.SetYMax(m_rect.YMin() + yoffset);
  434. offset.SetY(ynegOffset);
  435. } else if (m_justification == JustifyBottom()) {
  436. rect.SetYMin(m_rect.YMin() + ynegOffset);
  437. } else {
  438. ZError("Bad Justification for GaugePane");
  439. }
  440. pcontext->DrawImage(psurface, rect, false, offset);
  441. }
  442. }
  443. //////////////////////////////////////////////////////////////////////////////
  444. //
  445. // Value members
  446. //
  447. //////////////////////////////////////////////////////////////////////////////
  448. ZString GetFunctionName() { return "GaugeImage"; }
  449. };
  450. TRef<Image> CreateGaugeImage(Justification justification, Image* pimage, bool b3D, Number* pnumber)
  451. {
  452. return new GaugeImage(justification, pimage, b3D, pnumber);
  453. }
  454. TRef<Image> CreateGaugeImage(
  455. Justification justification,
  456. Image* pimage,
  457. const Rect& rect,
  458. bool b3D,
  459. Number* pnumber
  460. ) {
  461. return new GaugeImage(justification, pimage, rect, b3D, pnumber);
  462. }
  463. //////////////////////////////////////////////////////////////////////////////
  464. //
  465. // Animated Image
  466. //
  467. //////////////////////////////////////////////////////////////////////////////
  468. AnimatedImage::AnimatedImage(Number* ptime, AnimatedImage* pimage) :
  469. Image(ptime),
  470. m_psurfaces(pimage->m_psurfaces),
  471. m_index(0)
  472. {
  473. }
  474. AnimatedImage::AnimatedImage(Number* ptime, Surface* psurfaceSource, int nRows, int nCols) :
  475. Image(ptime),
  476. m_index(0)
  477. {
  478. if (nRows == 0) {
  479. nRows = 1;
  480. WinPoint size = psurfaceSource->GetSize();
  481. nCols = size.X() / size.Y();
  482. }
  483. int h = psurfaceSource->GetSize().X() / nCols;
  484. int w = psurfaceSource->GetSize().Y() / nRows;
  485. m_psurfaces.SetCount(nRows * nCols);
  486. if (m_psurfaces.GetCount() == 1) {
  487. psurfaceSource->SetColorKey(Color(0, 0, 0));
  488. m_psurfaces.Set(0, psurfaceSource);
  489. } else {
  490. for (int row = 0; row < nRows; row++) {
  491. for (int col = 0; col < nCols; col++) {
  492. int y = row * h;
  493. int x = col * w;
  494. int size = (w <= h) ? w : h;
  495. int index = row * nCols + col;
  496. TRef<Surface> psurfaceTextureSource =
  497. psurfaceSource->CreateCompatibleSurface(WinPoint(size, size), SurfaceType2D());
  498. ZAssert(size == w && size == h);
  499. psurfaceTextureSource->FillSurfaceWithColorKey();
  500. psurfaceTextureSource->BitBlt(
  501. WinPoint(0, 0),
  502. psurfaceSource,
  503. WinRect(x, y, x + size, y + size)
  504. );
  505. psurfaceTextureSource->SetColorKey(Color(0, 0, 0));
  506. m_psurfaces.Set(index, psurfaceTextureSource);
  507. }
  508. }
  509. }
  510. }
  511. void AnimatedImage::Evaluate()
  512. {
  513. float time = GetTime()->GetValue();
  514. int count = m_psurfaces.GetCount();
  515. m_index = (int)(time * count) % count;
  516. Image::Evaluate();
  517. }
  518. TRef<Surface> AnimatedImage::GetSurface()
  519. {
  520. return m_psurfaces[m_index];
  521. }
  522. MouseResult AnimatedImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  523. {
  524. // , AnimatedImage should be a vector of images instead of a vector of psurfaces
  525. Surface* psurface = GetSurface();
  526. bool bHit =
  527. (!psurface->HasColorKey())
  528. || psurface->GetColor(WinPoint((int)point.X(), (int)point.Y())) != psurface->GetColorKey();
  529. psurface->ReleasePointer();
  530. if (bHit) {
  531. return MouseResultHit();
  532. }
  533. return MouseResult();
  534. }
  535. //////////////////////////////////////////////////////////////////////////////
  536. //
  537. // Wrap Image
  538. //
  539. //////////////////////////////////////////////////////////////////////////////
  540. void WrapImage::SetImage(Image* pvalue)
  541. {
  542. if (pvalue != GetImage()) {
  543. if (m_bHasCapture) {
  544. RemoveCapture();
  545. m_bHasCapture = false;
  546. }
  547. SetChild(0, pvalue);
  548. }
  549. }
  550. void WrapImage::CalcBounds()
  551. {
  552. m_bounds = GetImage()->GetBounds();
  553. }
  554. TRef<Surface> WrapImage::GetSurface()
  555. {
  556. return GetImage()->GetSurface();
  557. }
  558. void WrapImage::Render(Context* pcontext)
  559. {
  560. ZEnter("WrapImage::Render()");
  561. GetImage()->Render(pcontext);
  562. ZExit("WrapImage::Render()");
  563. }
  564. void WrapImage::RemoveCapture()
  565. {
  566. GetImage()->RemoveCapture();
  567. m_bHasCapture = false;
  568. }
  569. MouseResult WrapImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  570. {
  571. if (m_bHasCapture) {
  572. ZAssert(bCaptured);
  573. } else {
  574. if (bCaptured) {
  575. return MouseResultRelease();
  576. }
  577. }
  578. return GetImage()->HitTest(pprovider, point, bCaptured);
  579. }
  580. void WrapImage::MouseMove(IInputProvider* pprovider, const Point& point, bool bCaptured, bool bInside)
  581. {
  582. GetImage()->MouseMove(pprovider, point, bCaptured, bInside);
  583. }
  584. void WrapImage::MouseEnter(IInputProvider* pprovider, const Point& point)
  585. {
  586. GetImage()->MouseEnter(pprovider, point);
  587. }
  588. void WrapImage::MouseLeave(IInputProvider* pprovider)
  589. {
  590. GetImage()->MouseLeave(pprovider);
  591. }
  592. MouseResult WrapImage::Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  593. {
  594. TRef<WrapImage> pthis = this;
  595. TRef<Image> pimage = GetImage();
  596. MouseResult result = pimage->Button(pprovider, point, button, bCaptured, bInside, bDown);
  597. if (result.Test(MouseResultRelease())) {
  598. m_bHasCapture = false;
  599. } else if (result.Test(MouseResultCapture())) {
  600. m_bHasCapture = true;
  601. }
  602. return result;
  603. }
  604. //////////////////////////////////////////////////////////////////////////////
  605. //
  606. // UndetectableImage
  607. //
  608. //////////////////////////////////////////////////////////////////////////////
  609. class UndetectableImageImpl : public WrapImage {
  610. public:
  611. UndetectableImageImpl(Image* pimage) :
  612. WrapImage(pimage)
  613. {
  614. }
  615. //////////////////////////////////////////////////////////////////////////////
  616. //
  617. // IMouseInput methods
  618. //
  619. //////////////////////////////////////////////////////////////////////////////
  620. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  621. {
  622. return MouseResult();
  623. }
  624. //////////////////////////////////////////////////////////////////////////////
  625. //
  626. // Value members
  627. //
  628. //////////////////////////////////////////////////////////////////////////////
  629. ZString GetFunctionName() { return "UndetectableImage"; }
  630. };
  631. TRef<Image> CreateUndetectableImage(Image* pimage)
  632. {
  633. return new UndetectableImageImpl(pimage);
  634. }
  635. //////////////////////////////////////////////////////////////////////////////
  636. //
  637. // Trans Image
  638. //
  639. //////////////////////////////////////////////////////////////////////////////
  640. TransformImage::TransformImage(Image* pimage, Transform2* ptrans) :
  641. WrapImage(pimage, ptrans)
  642. {
  643. }
  644. void TransformImage::CalcBounds()
  645. {
  646. m_bounds = GetImage()->GetBounds().Transform(GetTransform2()->GetValue());
  647. }
  648. void TransformImage::Render(Context* pcontext)
  649. {
  650. pcontext->Multiply(GetTransform2()->GetValue());
  651. WrapImage::Render(pcontext);
  652. }
  653. Point TransformImage::TransformPoint(const Point& point)
  654. {
  655. Matrix2 mat = GetTransform2()->GetValue();
  656. mat.Inverse();
  657. return mat.Transform(point);
  658. }
  659. MouseResult TransformImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  660. {
  661. return GetImage()->HitTest(pprovider, TransformPoint(point), bCaptured);
  662. }
  663. void TransformImage::MouseMove(IInputProvider* pprovider, const Point& point, bool bCaptured, bool bInside)
  664. {
  665. GetImage()->MouseMove(pprovider, TransformPoint(point), bCaptured, bInside);
  666. }
  667. void TransformImage::MouseEnter(IInputProvider* pprovider, const Point& point)
  668. {
  669. GetImage()->MouseEnter(pprovider, TransformPoint(point));
  670. }
  671. MouseResult TransformImage::Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  672. {
  673. return GetImage()->Button(pprovider, TransformPoint(point), button, bCaptured, bInside, bDown);
  674. }
  675. //////////////////////////////////////////////////////////////////////////////
  676. //
  677. // Trans Image
  678. //
  679. //////////////////////////////////////////////////////////////////////////////
  680. TranslateImage::TranslateImage(Image* pimage, const Point& point) :
  681. WrapImage(pimage),
  682. m_point(point)
  683. {
  684. }
  685. void TranslateImage::CalcBounds()
  686. {
  687. Rect rect = GetImage()->GetBounds().GetRect();
  688. rect.Offset(m_point);
  689. m_bounds.SetRect(rect);
  690. }
  691. void TranslateImage::Render(Context* pcontext)
  692. {
  693. pcontext->Translate(m_point);
  694. WrapImage::Render(pcontext);
  695. }
  696. Point TranslateImage::TransformPoint(const Point& point)
  697. {
  698. return point - m_point;
  699. }
  700. MouseResult TranslateImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  701. {
  702. return GetImage()->HitTest(pprovider, TransformPoint(point), bCaptured);
  703. }
  704. void TranslateImage::MouseMove(IInputProvider* pprovider, const Point& point, bool bCaptured, bool bInside)
  705. {
  706. GetImage()->MouseMove(pprovider, TransformPoint(point), bCaptured, bInside);
  707. }
  708. void TranslateImage::MouseEnter(IInputProvider* pprovider, const Point& point)
  709. {
  710. GetImage()->MouseEnter(pprovider, TransformPoint(point));
  711. }
  712. MouseResult TranslateImage::Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  713. {
  714. return GetImage()->Button(pprovider, TransformPoint(point), button, bCaptured, bInside, bDown);
  715. }
  716. //////////////////////////////////////////////////////////////////////////////
  717. //
  718. // Group Image
  719. //
  720. //////////////////////////////////////////////////////////////////////////////
  721. GroupImage::GroupImage() :
  722. Image(new ValueList(this))
  723. {
  724. }
  725. GroupImage::GroupImage(Image* pimage1) :
  726. Image(new ValueList(this))
  727. {
  728. AddImage(pimage1);
  729. }
  730. GroupImage::GroupImage(Image* pimage1, Image* pimage2) :
  731. Image(new ValueList(this))
  732. {
  733. AddImage(pimage1);
  734. AddImage(pimage2);
  735. }
  736. GroupImage::GroupImage(Image* pimage1, Image* pimage2, Image* pimage3) :
  737. Image(new ValueList(this))
  738. {
  739. AddImage(pimage1);
  740. AddImage(pimage2);
  741. AddImage(pimage3);
  742. }
  743. GroupImage::GroupImage(Image* pimage1, Image* pimage2, Image* pimage3, Image* pimage4) :
  744. Image(new ValueList(this))
  745. {
  746. AddImage(pimage1);
  747. AddImage(pimage2);
  748. AddImage(pimage3);
  749. AddImage(pimage4);
  750. }
  751. bool GroupImage::RemoveValue(Value* pvalue)
  752. {
  753. return pvalue == Image::GetEmpty();
  754. }
  755. void GroupImage::AddImage(Image* pimage)
  756. {
  757. GetValueList()->PushFront(pimage);
  758. }
  759. void GroupImage::AddImageToTop(Image* pimage)
  760. {
  761. GetValueList()->PushEnd(pimage);
  762. }
  763. void GroupImage::RemoveImage(Image* pimage)
  764. {
  765. if (m_pimageCapture == pimage) {
  766. RemoveCapture();
  767. EngineWindow::DoHitTest();
  768. }
  769. if (m_pimageHit == pimage) {
  770. m_pimageHit = NULL;
  771. EngineWindow::DoHitTest();
  772. }
  773. GetValueList()->Remove(pimage);
  774. }
  775. void GroupImage::CalcBounds()
  776. {
  777. m_bounds = Bounds2::GetEmpty();
  778. Image* pimage = Image::Cast(GetValueList()->GetFirst());
  779. while (pimage) {
  780. m_bounds = Union(m_bounds, pimage->GetBounds());
  781. pimage = Image::Cast(GetValueList()->GetNext());
  782. }
  783. }
  784. void GroupImage::Render(Context* pcontext)
  785. {
  786. ZEnter("GroupImage::Render()");
  787. Image* pimage = Image::Cast(GetValueList()->GetFirst());
  788. while (pimage) {
  789. if (GetValueList()->IsLast()) {
  790. pimage->Render(pcontext);
  791. } else {
  792. pcontext->PushState();
  793. pimage->Render(pcontext);
  794. pcontext->PopState();
  795. }
  796. pimage = Image::Cast(GetValueList()->GetNext());
  797. }
  798. ZExit("GroupImage::Render()");
  799. }
  800. void GroupImage::RemoveCapture()
  801. {
  802. if (m_pimageCapture) {
  803. m_pimageCapture->RemoveCapture();
  804. m_pimageCapture = NULL;
  805. }
  806. }
  807. MouseResult GroupImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  808. {
  809. //
  810. // Make sure we aren't deallocated in the middle of this function
  811. //
  812. TRef<GroupImage> pthis = this;
  813. //
  814. // figure out which image was hit
  815. //
  816. Image* pimageHit = NULL;
  817. if (m_pimageCapture) {
  818. ZAssert(bCaptured);
  819. //
  820. // Is the mouse over the captured image?
  821. //
  822. MouseResult mouseResult = m_pimageCapture->HitTest(pprovider, point, true);
  823. if (mouseResult.Test(MouseResultHit())) {
  824. pimageHit = m_pimageCapture;
  825. }
  826. //
  827. // Release Capture?
  828. //
  829. if (mouseResult.Test(MouseResultRelease())) {
  830. RemoveCapture();
  831. return mouseResult;
  832. }
  833. //
  834. // Mouse move
  835. //
  836. m_pimageCapture->MouseMove(pprovider, point, true, pimageHit != NULL);
  837. } else {
  838. //
  839. // Our parent thinks we are captured. Tell them otherwise.
  840. //
  841. if (bCaptured) {
  842. return MouseResultRelease();
  843. }
  844. //
  845. // Which image are we over?
  846. //
  847. pimageHit = Image::Cast(GetValueList()->GetLast());
  848. while (pimageHit) {
  849. MouseResult mouseResult = pimageHit->HitTest(pprovider, point, false);
  850. if (mouseResult.Test(MouseResultHit())) {
  851. break;
  852. }
  853. pimageHit = Image::Cast(GetValueList()->GetPrevious());
  854. }
  855. //
  856. // Call MouseMove, MouseEnter, or MouseLeave
  857. //
  858. if (m_pimageHit != pimageHit) {
  859. if (m_pimageHit) {
  860. m_pimageHit->MouseLeave(pprovider);
  861. }
  862. m_pimageHit = pimageHit;
  863. if (m_pimageHit) {
  864. m_pimageHit->MouseEnter(pprovider, point);
  865. }
  866. } else if (m_pimageHit) {
  867. m_pimageHit->MouseMove(pprovider, point, false, true);
  868. }
  869. }
  870. if (pimageHit == NULL) {
  871. return MouseResult();
  872. } else {
  873. return MouseResultHit();
  874. }
  875. }
  876. void GroupImage::MouseLeave(IInputProvider* pprovider)
  877. {
  878. if (m_pimageHit) {
  879. m_pimageHit->MouseLeave(pprovider);
  880. m_pimageHit = NULL;
  881. }
  882. }
  883. MouseResult GroupImage::Button(
  884. IInputProvider* pprovider,
  885. const Point& point,
  886. int button,
  887. bool bCaptured,
  888. bool bInside,
  889. bool bDown
  890. ) {
  891. MouseResult mouseResult;
  892. if (m_pimageCapture) {
  893. mouseResult =
  894. m_pimageCapture->Button(
  895. pprovider,
  896. point,
  897. button,
  898. true,
  899. m_pimageHit != NULL,
  900. bDown
  901. );
  902. if (mouseResult.Test(MouseResultRelease())) {
  903. m_pimageCapture = NULL;
  904. }
  905. } else if (m_pimageHit) {
  906. mouseResult = m_pimageHit->Button(pprovider, point, button, false, true, bDown);
  907. if (mouseResult.Test(MouseResultCapture())) {
  908. m_pimageCapture = m_pimageHit;
  909. }
  910. }
  911. return mouseResult;
  912. }
  913. //////////////////////////////////////////////////////////////////////////////
  914. //
  915. // Geo Image
  916. //
  917. //////////////////////////////////////////////////////////////////////////////
  918. GeoImage::GeoImage(Geo* pgeo, Viewport* pviewport, bool bZBuffer) :
  919. Image(pgeo, pviewport),
  920. m_bZBuffer(bZBuffer),
  921. m_vecLight(0, 0, 1),
  922. m_bBidirectional(false),
  923. m_color(Color::White()),
  924. m_colorAlt(Color::White()),
  925. m_ambientLevel(0.0f)
  926. {
  927. }
  928. void GeoImage::SetLightDirection(const Vector& vector)
  929. {
  930. m_vecLight = vector.Normalize();
  931. }
  932. void GeoImage::SetLight(const Color& color)
  933. {
  934. m_bBidirectional = false;
  935. m_color = color;
  936. }
  937. void GeoImage::SetLight(const Color& color, const Color& colorAlt)
  938. {
  939. m_bBidirectional = true;
  940. m_color = color;
  941. m_colorAlt = colorAlt;
  942. }
  943. void GeoImage::SetAmbientLevel(float level)
  944. {
  945. m_ambientLevel = level;
  946. }
  947. void GeoImage::CalcBounds()
  948. {
  949. m_bounds.SetRect(Rect::Cast(GetViewRect()->GetValue()));
  950. }
  951. void GeoImage::Render(Context* pcontext)
  952. {
  953. ZEnter("GeoImage::Render()");
  954. const Rect& rect = GetViewRect()->GetValue();
  955. pcontext->Translate(rect.Center());
  956. pcontext->Scale2(0.5f * rect.XSize());
  957. pcontext->Begin3DLayer(GetCamera(), m_bZBuffer);
  958. pcontext->SetAmbientLevel(m_ambientLevel);
  959. if (m_bBidirectional) {
  960. pcontext->BidirectionalLight(m_vecLight, m_color, m_colorAlt);
  961. } else {
  962. pcontext->DirectionalLight(m_vecLight, m_color);
  963. }
  964. GetGeo()->Render(pcontext);
  965. pcontext->End3DLayer();
  966. ZExit("GeoImage::Render()");
  967. }
  968. void GeoImage::RemoveCapture()
  969. {
  970. GetGeo()->RemoveCapture();
  971. }
  972. MouseResult GeoImage::HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  973. {
  974. return MouseResult();
  975. /*
  976. HitData3D hitData;
  977. hitData.m_distance = FLT_MAX;
  978. hitData.m_vecHit = Vector::GetZero();
  979. hitData.m_pgeo = NULL;
  980. // !!! what are the correct Vectors
  981. Geo* pgeo = GetGeo();
  982. pgeo->HitTest(Vector::GetZero(), Vector(0, 0, -1), hitData, bCaptured);
  983. Geo* pgeoHit = hitData.m_pgeo;
  984. */
  985. }
  986. MouseResult GeoImage::Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  987. {
  988. // , implement
  989. return MouseResult();
  990. }
  991. //////////////////////////////////////////////////////////////////////////////
  992. //
  993. // Switch Image
  994. //
  995. //////////////////////////////////////////////////////////////////////////////
  996. class SwitchImageImpl : public SwitchImage {
  997. protected:
  998. //////////////////////////////////////////////////////////////////////////////
  999. //
  1000. // Types
  1001. //
  1002. //////////////////////////////////////////////////////////////////////////////
  1003. class Data {
  1004. public:
  1005. float m_number;
  1006. TRef<Image> m_pimage;
  1007. };
  1008. typedef TList<Data, DefaultNoEquals> DataList;
  1009. //////////////////////////////////////////////////////////////////////////////
  1010. //
  1011. // Members
  1012. //
  1013. //////////////////////////////////////////////////////////////////////////////
  1014. DataList m_list;
  1015. TRef<Image> m_pimage;
  1016. public:
  1017. //////////////////////////////////////////////////////////////////////////////
  1018. //
  1019. // Methods
  1020. //
  1021. //////////////////////////////////////////////////////////////////////////////
  1022. SwitchImageImpl(Image* pimage, Number* pnumber) :
  1023. SwitchImage(pimage, pnumber),
  1024. m_pimage(pimage)
  1025. {
  1026. }
  1027. Number* GetNumber() { return Number::Cast(GetChild(1)); }
  1028. //////////////////////////////////////////////////////////////////////////////
  1029. //
  1030. // SwitchImage Methods
  1031. //
  1032. //////////////////////////////////////////////////////////////////////////////
  1033. void AddImage(float number, Image* pimage)
  1034. {
  1035. m_list.PushFront();
  1036. m_list.GetFront().m_number = number;
  1037. m_list.GetFront().m_pimage = pimage;
  1038. }
  1039. //////////////////////////////////////////////////////////////////////////////
  1040. //
  1041. // Value Methods
  1042. //
  1043. //////////////////////////////////////////////////////////////////////////////
  1044. void Evaluate()
  1045. {
  1046. float number = GetNumber()->GetValue();
  1047. DataList::Iterator iter(m_list);
  1048. while (!iter.End()) {
  1049. const Data& data = iter.Value();
  1050. if (data.m_number == number) {
  1051. SetImage(data.m_pimage);
  1052. WrapImage::Evaluate();
  1053. return;
  1054. }
  1055. iter.Next();
  1056. }
  1057. SetImage(m_pimage);
  1058. SwitchImage::Evaluate();
  1059. }
  1060. };
  1061. TRef<SwitchImage> CreateSwitchImage(Number* pnumber, Image* pimageDefault)
  1062. {
  1063. return new SwitchImageImpl(pimageDefault, pnumber);
  1064. }
  1065. //////////////////////////////////////////////////////////////////////////////
  1066. //
  1067. // GeoImage2D
  1068. //
  1069. //////////////////////////////////////////////////////////////////////////////
  1070. class GeoImage2D : public Image {
  1071. public:
  1072. GeoImage2D(Geo* pgeo) :
  1073. Image(pgeo)
  1074. {
  1075. }
  1076. Geo* GetGeo() { return Geo::Cast(GetChild(0)); }
  1077. //
  1078. // Image methods
  1079. //
  1080. void Render(Context* pcontext)
  1081. {
  1082. GetGeo()->Render(pcontext);
  1083. }
  1084. //
  1085. // Value members
  1086. //
  1087. ZString GetFunctionName() { return "GeoImage2D"; }
  1088. //
  1089. // IMouseInput methods
  1090. //
  1091. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  1092. {
  1093. return MouseResult();
  1094. }
  1095. };
  1096. TRef<Image> CreateGeoImage2D(Geo* pgeo)
  1097. {
  1098. return new GeoImage2D(pgeo);
  1099. }
  1100. //////////////////////////////////////////////////////////////////////////////
  1101. //
  1102. // ClipImage
  1103. //
  1104. //////////////////////////////////////////////////////////////////////////////
  1105. class ClipImage : public WrapImage {
  1106. public:
  1107. ClipImage(Image* pimage, RectValue* prect) :
  1108. WrapImage(pimage, prect)
  1109. {
  1110. }
  1111. RectValue* GetRect() { return RectValue::Cast(GetChild(1)); }
  1112. //
  1113. // Image methods
  1114. //
  1115. void CalcBounds()
  1116. {
  1117. m_bounds = Intersect(GetImage()->GetBounds(), GetRect()->GetValue());
  1118. }
  1119. void Render(Context* pcontext)
  1120. {
  1121. pcontext->Clip(GetRect()->GetValue());
  1122. WrapImage::Render(pcontext);
  1123. }
  1124. //
  1125. // Value members
  1126. //
  1127. ZString GetFunctionName() { return "ClipImage"; }
  1128. };
  1129. TRef<Image> CreateClipImage(Image* pimage, RectValue* prect)
  1130. {
  1131. return new ClipImage(pimage, prect);
  1132. }
  1133. //////////////////////////////////////////////////////////////////////////////
  1134. //
  1135. // PickImage
  1136. //
  1137. //////////////////////////////////////////////////////////////////////////////
  1138. class PickImageImpl : public PickImage {
  1139. private:
  1140. TRef<EventSourceImpl> m_peventSource;
  1141. public:
  1142. PickImageImpl(Image* pimage) :
  1143. PickImage(pimage),
  1144. m_peventSource(new EventSourceImpl())
  1145. {
  1146. }
  1147. //
  1148. // PickImage
  1149. //
  1150. IEventSource* GetEventSource()
  1151. {
  1152. return m_peventSource;
  1153. }
  1154. //
  1155. // IMouseInput
  1156. //
  1157. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  1158. {
  1159. if (button == 0 && bDown) {
  1160. m_peventSource->Trigger();
  1161. }
  1162. return MouseResult();
  1163. }
  1164. //
  1165. // Value members
  1166. //
  1167. ZString GetFunctionName() { return "PickImage"; }
  1168. };
  1169. TRef<PickImage> CreatePickImage(Image* pimage)
  1170. {
  1171. return new PickImageImpl(pimage);
  1172. }
  1173. //////////////////////////////////////////////////////////////////////////////
  1174. //
  1175. // BlendImage
  1176. //
  1177. //////////////////////////////////////////////////////////////////////////////
  1178. class BlendImage : public WrapImage {
  1179. private:
  1180. BlendMode m_blendMode;
  1181. public:
  1182. BlendImage(Image* pimage, BlendMode blendMode) :
  1183. WrapImage(pimage),
  1184. m_blendMode(blendMode)
  1185. {
  1186. }
  1187. //
  1188. // Image methods
  1189. //
  1190. void Render(Context* pcontext)
  1191. {
  1192. pcontext->SetBlendMode(m_blendMode);
  1193. WrapImage::Render(pcontext);
  1194. }
  1195. //
  1196. // Value members
  1197. //
  1198. ZString GetFunctionName() { return "BlendImage"; }
  1199. };
  1200. TRef<Image> CreateBlendImage(Image* pimage, BlendMode blendMode)
  1201. {
  1202. return new BlendImage(pimage, blendMode);
  1203. }
  1204. //////////////////////////////////////////////////////////////////////////////
  1205. //
  1206. //
  1207. //
  1208. //////////////////////////////////////////////////////////////////////////////
  1209. class ColorImage : public Image {
  1210. public:
  1211. ColorImage(ColorValue* pcolor) :
  1212. Image(pcolor)
  1213. {
  1214. m_bounds = Rect(-100000, -100000, 100000, 100000);
  1215. }
  1216. ColorValue* GetColor() { return ColorValue::Cast(GetChild(0)); }
  1217. //
  1218. // Image methods
  1219. //
  1220. void CalcBounds()
  1221. {
  1222. }
  1223. void Render(Context* pcontext)
  1224. {
  1225. pcontext->FillInfinite(GetColor()->GetValue());
  1226. }
  1227. //
  1228. // Value members
  1229. //
  1230. ZString GetFunctionName() { return "ColorImage"; }
  1231. };
  1232. TRef<Image> CreateColorImage(ColorValue* pcolor)
  1233. {
  1234. return new ColorImage(pcolor);
  1235. }
  1236. //////////////////////////////////////////////////////////////////////////////
  1237. //
  1238. //
  1239. //
  1240. //////////////////////////////////////////////////////////////////////////////
  1241. class ExtentImage : public Image {
  1242. public:
  1243. ExtentImage(RectValue* prect, ColorValue* pcolor) :
  1244. Image(prect, pcolor)
  1245. {
  1246. }
  1247. RectValue* GetRect() { return RectValue::Cast(GetChild(0)); }
  1248. ColorValue* GetColor() { return ColorValue::Cast(GetChild(1)); }
  1249. //
  1250. // Image methods
  1251. //
  1252. void CalcBounds()
  1253. {
  1254. m_bounds = GetRect()->GetValue();
  1255. }
  1256. void Render(Context* pcontext)
  1257. {
  1258. pcontext->FillRect(GetRect()->GetValue(), GetColor()->GetValue());
  1259. }
  1260. //
  1261. // Value members
  1262. //
  1263. ZString GetFunctionName() { return "ExtentImage"; }
  1264. };
  1265. TRef<Image> CreateExtentImage(RectValue* prect, ColorValue* pcolor)
  1266. {
  1267. return new ExtentImage(prect, pcolor);
  1268. }
  1269. //////////////////////////////////////////////////////////////////////////////
  1270. //
  1271. //
  1272. //
  1273. //////////////////////////////////////////////////////////////////////////////
  1274. class TransparentImage : public Image {
  1275. public:
  1276. TransparentImage()
  1277. {
  1278. m_bounds = Rect(-100000, -100000, 100000, 100000);
  1279. }
  1280. //
  1281. // Image methods
  1282. //
  1283. void CalcBounds()
  1284. {
  1285. }
  1286. void Render(Context* pcontext)
  1287. {
  1288. }
  1289. //
  1290. // Value members
  1291. //
  1292. ZString GetFunctionName() { return "TransparentImage"; }
  1293. };
  1294. TRef<Image> CreateTransparentImage()
  1295. {
  1296. return new TransparentImage();
  1297. }
  1298. //////////////////////////////////////////////////////////////////////////////
  1299. //
  1300. //
  1301. //
  1302. //////////////////////////////////////////////////////////////////////////////
  1303. class VisibleImage : public WrapImage {
  1304. public:
  1305. VisibleImage(Image* pimage, Boolean* bShow) :
  1306. WrapImage(pimage, bShow)
  1307. {
  1308. }
  1309. Boolean* GetVisible() { return Boolean::Cast(GetChild(1)); }
  1310. //////////////////////////////////////////////////////////////////////////////
  1311. //
  1312. // Image methods
  1313. //
  1314. //////////////////////////////////////////////////////////////////////////////
  1315. void Render(Context* pcontext)
  1316. {
  1317. if (GetVisible()->GetValue())
  1318. {
  1319. WrapImage::Render(pcontext);
  1320. }
  1321. }
  1322. //////////////////////////////////////////////////////////////////////////////
  1323. //
  1324. // Value methods
  1325. //
  1326. //////////////////////////////////////////////////////////////////////////////
  1327. ZString GetFunctionName() { return "VisibleImage"; }
  1328. //////////////////////////////////////////////////////////////////////////////
  1329. //
  1330. // IMouseInput methods
  1331. //
  1332. //////////////////////////////////////////////////////////////////////////////
  1333. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  1334. {
  1335. MouseResult mouseResult = WrapImage::HitTest(pprovider, point, bCaptured);
  1336. if (!GetVisible()->GetValue()) {
  1337. mouseResult.Clear(MouseResultHit());
  1338. }
  1339. return mouseResult;
  1340. }
  1341. };
  1342. TRef<Image> CreateVisibleImage(Image* pimage, Boolean* bShow)
  1343. {
  1344. return new VisibleImage(pimage, bShow);
  1345. }
  1346. //////////////////////////////////////////////////////////////////////////////
  1347. //
  1348. //
  1349. //
  1350. //////////////////////////////////////////////////////////////////////////////
  1351. class JustifyImage : public WrapImage {
  1352. Justification m_justification;
  1353. Point m_point;
  1354. public:
  1355. JustifyImage(Image* pimage, Justification justification) :
  1356. WrapImage(pimage),
  1357. m_justification(justification)
  1358. {
  1359. }
  1360. //////////////////////////////////////////////////////////////////////////////
  1361. //
  1362. // Implementation methods
  1363. //
  1364. //////////////////////////////////////////////////////////////////////////////
  1365. Point TransformPoint(const Point& point)
  1366. {
  1367. return point - m_point;
  1368. }
  1369. //////////////////////////////////////////////////////////////////////////////
  1370. //
  1371. // Image methods
  1372. //
  1373. //////////////////////////////////////////////////////////////////////////////
  1374. void CalcBounds()
  1375. {
  1376. Rect rect = GetImage()->GetBounds().GetRect();
  1377. if (m_justification.Test(JustifyLeft())) {
  1378. m_point.SetX(-rect.XMin());
  1379. } else if (m_justification.Test(JustifyRight())) {
  1380. m_point.SetX(-rect.XMax());
  1381. } else if (m_justification.Test(JustifyXCenter())) {
  1382. m_point.SetX(-rect.Center().X());
  1383. } else {
  1384. m_point.SetX(0);
  1385. }
  1386. if (m_justification.Test(JustifyBottom())) {
  1387. m_point.SetY(-rect.YMin());
  1388. } else if (m_justification.Test(JustifyTop())) {
  1389. m_point.SetY(-rect.YMax());
  1390. } else if (m_justification.Test(JustifyYCenter())) {
  1391. m_point.SetY(-rect.Center().Y());
  1392. } else {
  1393. m_point.SetY(0);
  1394. }
  1395. rect.Offset(m_point);
  1396. m_bounds.SetRect(rect);
  1397. }
  1398. void Render(Context* pcontext)
  1399. {
  1400. pcontext->Translate(m_point);
  1401. WrapImage::Render(pcontext);
  1402. }
  1403. //////////////////////////////////////////////////////////////////////////////
  1404. //
  1405. // IMouseInput methods
  1406. //
  1407. //////////////////////////////////////////////////////////////////////////////
  1408. MouseResult HitTest(IInputProvider* pprovider, const Point& point, bool bCaptured)
  1409. {
  1410. return GetImage()->HitTest(pprovider, TransformPoint(point), bCaptured);
  1411. }
  1412. void MouseMove(IInputProvider* pprovider, const Point& point, bool bCaptured, bool bInside)
  1413. {
  1414. GetImage()->MouseMove(pprovider, TransformPoint(point), bCaptured, bInside);
  1415. }
  1416. void MouseEnter(IInputProvider* pprovider, const Point& point)
  1417. {
  1418. GetImage()->MouseEnter(pprovider, TransformPoint(point));
  1419. }
  1420. MouseResult Button(IInputProvider* pprovider, const Point& point, int button, bool bCaptured, bool bInside, bool bDown)
  1421. {
  1422. return GetImage()->Button(pprovider, TransformPoint(point), button, bCaptured, bInside, bDown);
  1423. }
  1424. //////////////////////////////////////////////////////////////////////////////
  1425. //
  1426. // Value methods
  1427. //
  1428. //////////////////////////////////////////////////////////////////////////////
  1429. ZString GetFunctionName() { return "JustifyImage"; }
  1430. };
  1431. TRef<Image> CreateJustifyImage(Image* pimage, Justification justification)
  1432. {
  1433. return new JustifyImage(pimage, justification);
  1434. }
  1435. //////////////////////////////////////////////////////////////////////////////
  1436. //
  1437. //
  1438. //
  1439. //////////////////////////////////////////////////////////////////////////////
  1440. class MDLFileImageImpl : public MDLFileImage {
  1441. protected:
  1442. TRef<Modeler> m_pmodeler;
  1443. ZString m_str;
  1444. public:
  1445. MDLFileImageImpl(Modeler* pmodeler, const ZString& str) :
  1446. m_pmodeler(pmodeler),
  1447. m_str(str)
  1448. {
  1449. Load(str);
  1450. }
  1451. //////////////////////////////////////////////////////////////////////////////
  1452. //
  1453. // MDLFileImage methods
  1454. //
  1455. //////////////////////////////////////////////////////////////////////////////
  1456. void Load(const ZString& str)
  1457. {
  1458. TRef<INameSpace> pns = m_pmodeler->GetNameSpace(str, false);
  1459. if (pns) {
  1460. TRef<Image> pimage = pns->FindImage("image");
  1461. if (pimage) {
  1462. SetImage(pimage);
  1463. }
  1464. m_pmodeler->UnloadNameSpace(pns);
  1465. }
  1466. }
  1467. void Reload()
  1468. {
  1469. Load(m_str);
  1470. }
  1471. //////////////////////////////////////////////////////////////////////////////
  1472. //
  1473. // Value members
  1474. //
  1475. //////////////////////////////////////////////////////////////////////////////
  1476. ZString GetFunctionName() { return "MDLFileImage"; }
  1477. };
  1478. TRef<MDLFileImage> CreateMDLFileImage(Modeler* pmodeler, const ZString& str)
  1479. {
  1480. return new MDLFileImageImpl(pmodeler, str);
  1481. }