guiParticleGraphInspector.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiDefaultControlRender.h"
  26. #include "math/rectClipper.h"
  27. #include "gui/guiCanvas.h"
  28. #ifndef _PARTICLE_ASSET_H_
  29. #include "2d/assets/ParticleAsset.h"
  30. #endif
  31. #include "gui/editor/guiParticleGraphInspector.h"
  32. #include "gui/editor/guiParticleGraphInspector_ScriptBinding.h"
  33. IMPLEMENT_CONOBJECT(GuiParticleGraphInspector);
  34. GuiParticleGraphInspector::GuiParticleGraphInspector()
  35. {
  36. mBounds.extent.set(300, 200);
  37. mTargetAsset = NULL;
  38. mTargetField = StringTable->insert("QuantityScale");
  39. mEmitterIndex = 0;
  40. mVariationInspector = NULL;
  41. mMinX = 0;
  42. mMinXLabel = StringTable->insert("0");
  43. mMaxX = 1;
  44. mMaxXLabel = StringTable->insert("1");
  45. mMinY = 0;
  46. mMinYLabel = StringTable->insert("0");
  47. mMaxY = 10;
  48. mMaxYLabel = StringTable->insert("10");
  49. mLabelX = StringTable->insert("Time", true);
  50. mLabelY = StringTable->insert("Value", true);
  51. mSelectedIndex = -1;
  52. mDirty = true;
  53. mPointList = Vector<GraphPoint>();
  54. setField("profile", "GuiDefaultProfile");
  55. }
  56. void GuiParticleGraphInspector::initPersistFields()
  57. {
  58. Parent::initPersistFields();
  59. }
  60. void GuiParticleGraphInspector::inspectObject(ParticleAsset* object)
  61. {
  62. mTargetAsset = object;
  63. mDirty = true;
  64. }
  65. void GuiParticleGraphInspector::setDisplayField(const char* fieldName)
  66. {
  67. if (mTargetField != StringTable->insert(fieldName, true))
  68. {
  69. mSelectedIndex = -1;
  70. }
  71. mTargetField = StringTable->insert(fieldName, true);
  72. mDirty = true;
  73. }
  74. void GuiParticleGraphInspector::setDisplayField(const char* fieldName, U16 index)
  75. {
  76. mEmitterIndex = index;
  77. setDisplayField(fieldName);
  78. }
  79. void GuiParticleGraphInspector::setDisplayArea(StringTableEntry minX, StringTableEntry minY, StringTableEntry maxX, StringTableEntry maxY)
  80. {
  81. mMinXLabel = minX;
  82. mMinX = dAtof(minX);
  83. mMinYLabel = minY;
  84. mMinY = dAtof(minY);
  85. mMaxXLabel = maxX;
  86. mMaxX = dAtof(maxX);
  87. mMaxYLabel = maxY;
  88. mMaxY = dAtof(maxY);
  89. mDirty = true;
  90. }
  91. void GuiParticleGraphInspector::setDisplayLabels(const char* labelX, const char* labelY)
  92. {
  93. mLabelX = StringTable->insert(labelX, true);
  94. mLabelY = StringTable->insert(labelY, true);
  95. }
  96. ParticleAssetField* GuiParticleGraphInspector::getTargetField()
  97. {
  98. ParticleAssetFieldCollection& collection = mTargetAsset->getParticleFields();
  99. ParticleAssetField* field = collection.findField(mTargetField);
  100. if (field == NULL)
  101. {
  102. if (mEmitterIndex >= mTargetAsset->getEmitterCount())
  103. {
  104. mEmitterIndex = mTargetAsset->getEmitterCount() - 1;
  105. }
  106. ParticleAssetEmitter* emitter = mTargetAsset->getEmitter(mEmitterIndex);
  107. ParticleAssetFieldCollection& emitterCollection = emitter->getParticleFields();
  108. field = emitterCollection.findField(mTargetField);
  109. }
  110. AssertFatal(field != NULL, "GuiParticleGraphInspector::getTargetField() - Unable to find the requested field.");
  111. return field;
  112. }
  113. void GuiParticleGraphInspector::resize(const Point2I &newPosition, const Point2I &newExtent)
  114. {
  115. GuiControl::resize(newPosition, newExtent);
  116. mDirty = true;
  117. }
  118. void GuiParticleGraphInspector::setControlProfile(GuiControlProfile *prof)
  119. {
  120. GuiControl::setControlProfile(prof);
  121. mDirty = true;
  122. }
  123. void GuiParticleGraphInspector::onTouchUp(const GuiEvent &event)
  124. {
  125. if(mTargetAsset)
  126. mTargetAsset->refreshAsset();
  127. }
  128. void GuiParticleGraphInspector::onTouchDown(const GuiEvent &event)
  129. {
  130. if(!mTargetAsset)
  131. return;
  132. mSelectedIndex = findHitGraphPoint(event.mousePoint);
  133. if (mSelectedIndex != -1 && event.mouseClickCount == 2)
  134. {
  135. //remove the point
  136. ParticleAssetField* field = getTargetField();
  137. field->removeDataKey(mSelectedIndex);
  138. mDirty = true;
  139. }
  140. else if (mSelectedIndex == -1 && mGridRect.pointInRect(event.mousePoint))
  141. {
  142. //Time to create a new point!
  143. ParticleAssetField* field = getTargetField();
  144. F32 time = getGraphTime(event.mousePoint.x);
  145. F32 value = getGraphValue(event.mousePoint.y);
  146. mSelectedIndex = field->addDataKey(time, value);
  147. mDirty = true;
  148. }
  149. }
  150. void GuiParticleGraphInspector::onTouchDragged(const GuiEvent &event)
  151. {
  152. if (!mTargetAsset)
  153. return;
  154. Point2I point = Point2I(mClamp(event.mousePoint.x, mGridRect.point.x, mGridRect.point.x + mGridRect.extent.x), mClamp(event.mousePoint.y, mGridRect.point.y, mGridRect.point.y + mGridRect.extent.y));
  155. if (mSelectedIndex == 0)
  156. {
  157. //Time to move the first point!
  158. ParticleAssetField* field = getTargetField();
  159. F32 value = getGraphValue(point.y);
  160. field->setDataKeyValue(mSelectedIndex, value);
  161. mDirty = true;
  162. }
  163. else if (mSelectedIndex > 0)
  164. {
  165. //Time to move a point!
  166. ParticleAssetField* field = getTargetField();
  167. F32 time = getGraphTime(point.x);
  168. F32 value = getGraphValue(point.y);
  169. if (time == field->getDataKeyTime(mSelectedIndex) || field->doesKeyExist(time))
  170. {
  171. //If we're not moving through time or we tried to drag it into a time with a different point, then just change the value.
  172. field->setDataKeyValue(mSelectedIndex, value);
  173. }
  174. else
  175. {
  176. //Time travel! Destroy the old point. Recreate in a new time.
  177. field->removeDataKey(mSelectedIndex);
  178. mSelectedIndex = field->addDataKey(time, value);
  179. }
  180. mDirty = true;
  181. }
  182. }
  183. U32 GuiParticleGraphInspector::findHitGraphPoint(const Point2I &point)
  184. {
  185. for (U32 i = 0; i < mPointList.size(); i++)
  186. {
  187. F32 x = mPointList[i].mPoint.x - point.x;
  188. F32 y = mPointList[i].mPoint.y - point.y;
  189. F32 dist = mSqrt((x * x) + (y * y));
  190. if (dist <= mRadius)
  191. {
  192. return i;
  193. }
  194. }
  195. return -1;
  196. }
  197. F32 GuiParticleGraphInspector::getGraphValue(const F32 y)
  198. {
  199. S32 len = mGridRect.extent.y;
  200. F32 ratio = (F32)((y - mGridRect.point.y) / len);
  201. ratio = mRound(ratio * 100) / 100; //Snaps to a grid of 100 possible values.
  202. return mMinY + ((mMaxY - mMinY) * (1 - ratio));
  203. }
  204. F32 GuiParticleGraphInspector::getGraphTime(const F32 x)
  205. {
  206. S32 len = mGridRect.extent.x;
  207. F32 ratio = (F32)((x - mGridRect.point.x) / len);
  208. ratio = mRound(ratio * 100) / 100; //Snaps to a grid of 100 possible values.
  209. return mMinX + ((mMaxX - mMinX) * ratio);
  210. }
  211. void GuiParticleGraphInspector::onRender(Point2I offset, const RectI &updateRect)
  212. {
  213. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  214. if (!ctrlRect.isValidRect())
  215. {
  216. return;
  217. }
  218. renderUniversalRect(ctrlRect, mProfile, NormalState);
  219. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  220. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  221. //Make room for the graph labels
  222. GFont *font = mProfile->getFont(mFontSizeAdjust);
  223. U32 fontHeight = font->getHeight();
  224. contentRect.extent.y -= fontHeight;
  225. U8 xReduction = getMax(getMax(fontHeight, font->getStrWidth(mMaxYLabel)), font->getStrWidth(mMinYLabel));
  226. contentRect.extent.x -= xReduction;
  227. contentRect.point.x += xReduction;
  228. //reduce the contentRect to be divisible by 10
  229. U32 modX = contentRect.len_x() % 10;
  230. U32 modY = contentRect.len_y() % 10;
  231. contentRect.extent.set(contentRect.len_x() - modX, contentRect.len_y() - modY);
  232. contentRect.point.set(contentRect.point.x + mFloor(modX / 2), contentRect.point.y + mFloor(modY / 2));
  233. //Draw the labels
  234. ColorI gridColor = mProfile->getFillColor(HighlightState);
  235. renderLabels(contentRect, gridColor);
  236. if (contentRect.isValidRect())
  237. {
  238. renderGrid(contentRect, gridColor);
  239. if (mCalculationOffset != offset)
  240. {
  241. mDirty = true;
  242. }
  243. renderPoints(contentRect, mProfile->getFillColor(SelectedState));
  244. mCalculationOffset = offset;
  245. }
  246. }
  247. void GuiParticleGraphInspector::renderLabels(const RectI &contentRect, const ColorI &labelColor)
  248. {
  249. GFont *font = mProfile->getFont(mFontSizeAdjust);
  250. U32 fontHeight = font->getHeight();
  251. U32 textWidth;
  252. Point2I textPoint;
  253. //Set the color used for the grid. This will also be used for the text.
  254. dglSetBitmapModulation(labelColor);
  255. //x label
  256. textWidth = font->getStrWidth(mLabelX);
  257. textPoint = Point2I(contentRect.point.x + (contentRect.extent.x / 2) - (textWidth / 2), contentRect.point.y + contentRect.extent.y + 2);
  258. dglDrawText(font, textPoint, mLabelX, NULL, 0, 0);
  259. //x min label
  260. textWidth = font->getStrWidth(mMinXLabel);
  261. textPoint = Point2I(contentRect.point.x + 1, contentRect.point.y + contentRect.extent.y + 2);
  262. dglDrawText(font, textPoint, mMinXLabel, NULL, 0, 0);
  263. //x max label
  264. textWidth = font->getStrWidth(mMaxXLabel);
  265. textPoint = Point2I((contentRect.point.x + contentRect.extent.x - 1) - textWidth, contentRect.point.y + contentRect.extent.y + 2);
  266. dglDrawText(font, textPoint, mMaxXLabel, NULL, 0, 0);
  267. //y label
  268. textWidth = font->getStrWidth(mLabelY);
  269. textPoint = Point2I(contentRect.point.x - (fontHeight + 2), contentRect.point.y + (contentRect.extent.y / 2) + (textWidth / 2));
  270. dglDrawText(font, textPoint, mLabelY, NULL, 0, 90);
  271. //y min label
  272. textWidth = font->getStrWidth(mMinYLabel);
  273. textPoint = Point2I(contentRect.point.x - (textWidth + 2), (contentRect.point.y + contentRect.extent.y - 2) - (fontHeight / 2));
  274. dglDrawText(font, textPoint, mMinYLabel, NULL, 0, 0);
  275. //y max label
  276. textWidth = font->getStrWidth(mMaxYLabel);
  277. textPoint = Point2I(contentRect.point.x - (textWidth + 2), (contentRect.point.y + 4) - (fontHeight / 2));
  278. dglDrawText(font, textPoint, mMaxYLabel, NULL, 0, 0);
  279. }
  280. void GuiParticleGraphInspector::renderGrid(const RectI &contentRect, const ColorI &gridColor)
  281. {
  282. S32 x, y;
  283. x = contentRect.len_x() / 10;
  284. y = contentRect.len_y() / 10;
  285. //horizontal lines
  286. for (U8 i = 0; i < 11; i++)
  287. {
  288. if(i != 5)
  289. {
  290. dglDrawLine(Point2I(contentRect.point.x, contentRect.point.y + (y * i)), Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i)), gridColor);
  291. }
  292. else
  293. {
  294. Point2I p1 = Point2I(contentRect.point.x, contentRect.point.y + (y * i) - 1);
  295. Point2I p2 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i) - 1);
  296. Point2I p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i) + 1);
  297. Point2I p4 = Point2I(contentRect.point.x, contentRect.point.y + (y * i) + 1);
  298. dglDrawQuadFill(p1, p2, p3, p4, gridColor);
  299. }
  300. }
  301. //vertical lines
  302. for (U8 i = 0; i < 11; i++)
  303. {
  304. if (i != 5)
  305. {
  306. dglDrawLine(Point2I(contentRect.point.x + (x * i), contentRect.point.y), Point2I(contentRect.point.x + (x * i), contentRect.point.y + contentRect.extent.y), gridColor);
  307. }
  308. else
  309. {
  310. Point2I p1 = Point2I(contentRect.point.x + (x * i) - 1, contentRect.point.y);
  311. Point2I p2 = Point2I(contentRect.point.x + (x * i) + 1, contentRect.point.y);
  312. Point2I p3 = Point2I(contentRect.point.x + (x * i) + 1, contentRect.point.y + contentRect.extent.y);
  313. Point2I p4 = Point2I(contentRect.point.x + (x * i) - 1, contentRect.point.y + contentRect.extent.y);
  314. dglDrawQuadFill(p1, p2, p3, p4, gridColor);
  315. }
  316. }
  317. }
  318. void GuiParticleGraphInspector::calculatePoints(const RectI &contentRect)
  319. {
  320. mGridRect = RectI(contentRect);
  321. mPointList.clear();
  322. ParticleAssetField* field = getTargetField();
  323. F32 time = 0;
  324. U32 count = field->getDataKeyCount();
  325. for (U32 i = 0; i < count; i++)
  326. {
  327. ParticleAssetField::DataKey key = field->getDataKey(i);
  328. //force the first key to always be at time zero
  329. if (i == 0 && key.mTime != 0)
  330. {
  331. field->addDataKey(0, key.mValue);
  332. field->removeDataKey(1);
  333. key = field->getDataKey(0);
  334. count = field->getDataKeyCount();
  335. }
  336. //Remove the point if it has a bad time
  337. if (i > 0 && key.mTime <= time)
  338. {
  339. field->removeDataKey(i);
  340. count--;
  341. continue;
  342. }
  343. time = key.mTime;
  344. }
  345. Point2I p;
  346. for (U32 i = 0; i < count; i++)
  347. {
  348. ParticleAssetField::DataKey key = field->getDataKey(i);
  349. p = convertToRenderPoint(contentRect, key.mTime, key.mValue);
  350. mPointList.push_back(GraphPoint(p, key.mTime, key.mValue, i));
  351. }
  352. mDirty = false;
  353. }
  354. Point2I GuiParticleGraphInspector::convertToRenderPoint(const RectI& contentRect, F32 time, F32 value)
  355. {
  356. F32 width = mMaxX - mMinX;
  357. F32 height = mMaxY - mMinY;
  358. F32 ratioX = (time - mMinX) / width;
  359. F32 ratioY = (value - mMinY) / height;
  360. return Point2I(contentRect.point.x + (contentRect.extent.x * ratioX), contentRect.point.y + (contentRect.extent.y * (1 - ratioY)));
  361. }
  362. void GuiParticleGraphInspector::renderPoints(const RectI &contentRect, const ColorI &lineColor)
  363. {
  364. if (mTargetAsset)
  365. {
  366. if (mDirty)
  367. {
  368. calculatePoints(contentRect);
  369. }
  370. //get the cursor position
  371. Point2I cursorPt = Point2I(0, 0);
  372. GuiCanvas *root = getRoot();
  373. if (root)
  374. {
  375. cursorPt = root->getCursorPos();
  376. }
  377. //Render variation
  378. if (mVariationInspector != NULL)
  379. {
  380. ColorI variColor = ColorI(lineColor);
  381. variColor.alpha /= 2;
  382. renderVariation(contentRect, variColor);
  383. }
  384. //Render the lines
  385. Point2I p1, p2;
  386. U32 count = mPointList.size();
  387. for(U32 i = 1; i < count; i++)
  388. {
  389. p1 = mPointList[i-1].mPoint;
  390. p2 = mPointList[i].mPoint;
  391. renderLine(contentRect, p1, p2, lineColor);
  392. renderDot(contentRect, p1, cursorPt, mSelectedIndex == (i - 1));
  393. }
  394. p1 = mPointList[count - 1].mPoint;
  395. p2 = Point2I(contentRect.point.x + contentRect.extent.x, p1.y);
  396. if (p1.x < p2.x)
  397. {
  398. renderLine(contentRect, p1, p2, lineColor);
  399. }
  400. renderDot(contentRect, p1, cursorPt, mSelectedIndex == (count - 1));
  401. }
  402. }
  403. Vector<GuiParticleGraphInspector::GraphPoint>* GuiParticleGraphInspector::getRenderPoints()
  404. {
  405. if (!mAwake)
  406. {
  407. return NULL;
  408. }
  409. if (mDirty)
  410. {
  411. RectI rect = RectI(0, 0, 1, 1);
  412. calculatePoints(rect); mDirty = true;
  413. }
  414. return &mPointList;
  415. }
  416. void GuiParticleGraphInspector::renderVariation(const RectI& contentRect, const ColorI& color)
  417. {
  418. Vector<GraphPoint>* variPointList = mVariationInspector->getRenderPoints();
  419. if (!variPointList)
  420. {
  421. return;
  422. }
  423. S32 vPen = 0;
  424. S32 bPen = 0;
  425. Point2I up1, down1, up2, down2;
  426. while (vPen < variPointList->size() || bPen < mPointList.size())
  427. {
  428. GraphPoint& vari = variPointList->at(getMin(vPen, variPointList->size() - 1));
  429. GraphPoint& base = mPointList.at(getMin(bPen, mPointList.size() - 1));
  430. if (vPen == 0 && bPen == 0)
  431. {
  432. up1 = convertToRenderPoint(contentRect, 0, base.mValue + vari.mValue);
  433. down1 = convertToRenderPoint(contentRect, 0, base.mValue - vari.mValue);
  434. vPen = 1;
  435. bPen = 1;
  436. continue;
  437. }
  438. if (vPen >= variPointList->size() || bPen >= mPointList.size())
  439. {
  440. F32 time = vPen >= variPointList->size() ? base.mTime : vari.mTime;
  441. up2 = convertToRenderPoint(contentRect, time, base.mValue + vari.mValue);
  442. down2 = convertToRenderPoint(contentRect, time, base.mValue - vari.mValue);
  443. vPen++;
  444. bPen++;
  445. }
  446. else if (vari.mTime == base.mTime)
  447. {
  448. up2 = convertToRenderPoint(contentRect, base.mTime, base.mValue + vari.mValue);
  449. down2 = convertToRenderPoint(contentRect, base.mTime, base.mValue - vari.mValue);
  450. vPen++;
  451. bPen++;
  452. }
  453. else if (vari.mTime < base.mTime)
  454. {
  455. GraphPoint& oldBase = mPointList.at(bPen - 1);
  456. F32 timeDeltaB = base.mTime - oldBase.mTime;
  457. F32 timeDeltaV = vari.mTime - oldBase.mTime;
  458. F32 ratio = timeDeltaV / timeDeltaB;
  459. F32 baseValue = oldBase.mValue + ((base.mValue - oldBase.mValue) * ratio);
  460. up2 = convertToRenderPoint(contentRect, vari.mTime, baseValue + vari.mValue);
  461. down2 = convertToRenderPoint(contentRect, vari.mTime, baseValue - vari.mValue);
  462. vPen++;
  463. }
  464. else if (vari.mTime > base.mTime)
  465. {
  466. GraphPoint& oldVari = variPointList->at(vPen - 1);
  467. F32 timeDeltaB = base.mTime - oldVari.mTime;
  468. F32 timeDeltaV = vari.mTime - oldVari.mTime;
  469. F32 ratio = timeDeltaB / timeDeltaV;
  470. F32 variValue = oldVari.mValue + ((vari.mValue - oldVari.mValue) * ratio);
  471. up2 = convertToRenderPoint(contentRect, base.mTime, base.mValue + variValue);
  472. down2 = convertToRenderPoint(contentRect, base.mTime, base.mValue - variValue);
  473. bPen++;
  474. }
  475. renderQuad(contentRect, up1, up2, down1, down2, color);
  476. up1 = up2;
  477. down1 = down2;
  478. }
  479. up2 = Point2I(contentRect.point.x + contentRect.extent.x, up1.y);
  480. down2 = Point2I(contentRect.point.x + contentRect.extent.x, down1.y);
  481. if (up1.x < up2.x)
  482. {
  483. renderQuad(contentRect, up1, up2, down1, down2, color);
  484. }
  485. }
  486. void GuiParticleGraphInspector::renderDot(const RectI &contentRect, const Point2I &point, const Point2I &cursorPt, bool isSelected)
  487. {
  488. if(point.x >= contentRect.point.x && point.x <= contentRect.point.x + contentRect.extent.x && point.y >= contentRect.point.y && point.y <= contentRect.point.y + contentRect.extent.y)
  489. {
  490. F32 x = cursorPt.x - point.x;
  491. F32 y = cursorPt.y - point.y;
  492. F32 dist = mSqrt((x * x) + (y * y));
  493. ColorI color;
  494. if (isSelected)
  495. {
  496. color = mProfile->getFontColor(SelectedState);
  497. }
  498. else if (dist <= mRadius)
  499. {
  500. color = mProfile->getFontColor(HighlightState);
  501. }
  502. else
  503. {
  504. color = mProfile->getFontColor(NormalState);
  505. }
  506. dglDrawCircleFill(point, mRadius, ColorI(0, 0, 0, 100));
  507. dglDrawCircleFill(point, mRadius - 2, color);
  508. }
  509. }
  510. void GuiParticleGraphInspector::renderLine(const RectI &contentRect, const Point2I &point1, const Point2I &point2, const ColorI &lineColor)
  511. {
  512. RectClipper clipper = RectClipper(contentRect);
  513. Point2I p1;
  514. Point2I p2;
  515. if(clipper.clipLine(point1, point2, p1, p2))
  516. {
  517. dglDrawLine(p1, p2, ColorI(lineColor));
  518. }
  519. }
  520. //Points are leftTop, rightTop, leftBottom, rightBottom
  521. void GuiParticleGraphInspector::renderQuad(const RectI& contentRect, const Point2I& point1, const Point2I& point2, const Point2I& point3, const Point2I& point4, const ColorI& quadColor)
  522. {
  523. //if the heights of the left and right sides are both zero then we can exit now
  524. if ((point1.y - point3.y) == 0 && (point2.y - point4.y) == 0)
  525. {
  526. return;
  527. }
  528. RectI area = RectI(point1.x, getMin(point1.y, point2.y), point2.x - point1.x, point1.y < point2.y ? getMax(point3.y, point4.y) - point1.y : getMax(point3.y, point4.y) - point2.y);
  529. if (!contentRect.overlaps(area))
  530. {
  531. //Nothing to draw here...
  532. return;
  533. }
  534. if ((point1.y > point4.y || point2.y > point3.y) && area.extent.y > 1)
  535. {
  536. Point2I point5 = Point2I(mRound((point1.x + point2.x) / 2), mRound((point1.y + point2.y) / 2));
  537. Point2I point6 = Point2I(mRound((point3.x + point4.x) / 2), mRound((point3.y + point4.y) / 2));
  538. renderQuad(contentRect, point1, point5, point3, point6, quadColor);
  539. renderQuad(contentRect, point5, point2, point6, point4, quadColor);
  540. return;
  541. }
  542. RectClipper clipper = RectClipper(contentRect);
  543. Point2I topStart;
  544. Point2I topEnd;
  545. bool hasTop = clipper.clipLine(point1, point2, topStart, topEnd);
  546. Point2I bottomStart;
  547. Point2I bottomEnd;
  548. bool hasBottom = clipper.clipLine(point3, point4, bottomStart, bottomEnd);
  549. Point2I leftStart;
  550. Point2I leftEnd;
  551. bool hasLeft = clipper.clipLine(point1, point3, leftStart, leftEnd);
  552. Point2I rightStart;
  553. Point2I rightEnd;
  554. bool hasRight = clipper.clipLine(point2, point4, rightStart, rightEnd);
  555. //Replace left and right if they're missing
  556. if (!hasLeft)
  557. {
  558. leftStart.x = leftEnd.x = contentRect.point.x;
  559. leftStart.y = hasTop ? topStart.y : contentRect.point.y;
  560. leftEnd.y = hasBottom ? bottomStart.y : contentRect.point.y + contentRect.extent.y;
  561. }
  562. if (!hasRight)
  563. {
  564. rightStart.x = rightEnd.x = contentRect.point.x + contentRect.extent.x - 1;
  565. rightStart.y = hasTop ? topEnd.y : contentRect.point.y;
  566. rightEnd.y = hasBottom ? bottomEnd.y : contentRect.point.y + contentRect.extent.y;
  567. }
  568. S32 leftEdge = leftStart.x;
  569. S32 rightEdge = rightStart.x;
  570. //Middle Section
  571. S32 y = getMax(leftStart.y, rightStart.y);
  572. S32 h = getMin(leftEnd.y - y, rightEnd.y - y);
  573. RectI fillRect = RectI(leftStart.x, y, rightStart.x - leftStart.x, h);
  574. dglDrawRectFill(fillRect, quadColor);
  575. //Top Section
  576. if (hasTop && topStart.y != topEnd.y)
  577. {
  578. if (leftEdge != topStart.x && topStart.y < topEnd.y)
  579. {
  580. RectI rect = RectI(leftEdge, topStart.y, topStart.x - leftEdge, topEnd.y - topStart.y);
  581. dglDrawRectFill(rect, quadColor);
  582. Point2I p = Point2I(topStart.x, topEnd.y);
  583. dglDrawTriangleFill(topStart, p, topEnd, quadColor);
  584. }
  585. else if (rightEdge != topEnd.x && topStart.y > topEnd.y)
  586. {
  587. RectI rect = RectI(topEnd.x, topEnd.y, rightEdge - topEnd.x, topStart.y - topEnd.y);
  588. dglDrawRectFill(rect, quadColor);
  589. Point2I p = Point2I(topEnd.x, topStart.y);
  590. dglDrawTriangleFill(topStart, p, topEnd, quadColor);
  591. }
  592. else if (topStart.y > topEnd.y)
  593. {
  594. Point2I p = Point2I(topEnd.x, topStart.y);
  595. dglDrawTriangleFill(topStart, p, topEnd, quadColor);
  596. }
  597. else if (topStart.y < topEnd.y)
  598. {
  599. Point2I p = Point2I(topStart.x, topEnd.y);
  600. dglDrawTriangleFill(topStart, p, topEnd, quadColor);
  601. }
  602. }
  603. //Bottom Section
  604. if (hasBottom && bottomStart.y != bottomEnd.y)
  605. {
  606. if (leftEdge != bottomStart.x && bottomStart.y > bottomEnd.y)
  607. {
  608. RectI rect = RectI(leftEdge, bottomEnd.y, bottomStart.x - leftEdge, bottomStart.y - bottomEnd.y);
  609. dglDrawRectFill(rect, quadColor);
  610. Point2I p = Point2I(bottomStart.x, bottomEnd.y);
  611. dglDrawTriangleFill(bottomEnd, p, bottomStart, quadColor);
  612. }
  613. else if (rightEdge != bottomEnd.x && bottomStart.y < bottomEnd.y)
  614. {
  615. RectI rect = RectI(bottomEnd.x, bottomStart.y, rightEdge - bottomEnd.x, bottomEnd.y - bottomStart.y);
  616. dglDrawRectFill(rect, quadColor);
  617. Point2I p = Point2I(bottomEnd.x, bottomStart.y);
  618. dglDrawTriangleFill(bottomStart, bottomEnd, p, quadColor);
  619. }
  620. else if (bottomStart.y < bottomEnd.y)
  621. {
  622. Point2I p = Point2I(bottomEnd.x, bottomStart.y);
  623. dglDrawTriangleFill(bottomStart, bottomEnd, p, quadColor);
  624. }
  625. else if (bottomStart.y > bottomEnd.y)
  626. {
  627. Point2I p = Point2I(bottomStart.x, bottomEnd.y);
  628. dglDrawTriangleFill(bottomStart, bottomEnd, p, quadColor);
  629. }
  630. }
  631. }