guiParticleGraphInspector.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. mMinX = 0;
  41. mMinXLabel = StringTable->insert("0");
  42. mMaxX = 1;
  43. mMaxXLabel = StringTable->insert("1");
  44. mMinY = 0;
  45. mMinYLabel = StringTable->insert("0");
  46. mMaxY = 10;
  47. mMaxYLabel = StringTable->insert("10");
  48. mLabelX = StringTable->insert("Time", true);
  49. mLabelY = StringTable->insert("Value", true);
  50. mSelectedIndex = -1;
  51. mDirty = true;
  52. mPointList = Vector<GraphPoint>();
  53. setField("profile", "GuiDefaultProfile");
  54. }
  55. void GuiParticleGraphInspector::initPersistFields()
  56. {
  57. Parent::initPersistFields();
  58. }
  59. void GuiParticleGraphInspector::inspectObject(ParticleAsset* object)
  60. {
  61. mTargetAsset = object;
  62. mDirty = true;
  63. }
  64. void GuiParticleGraphInspector::setDisplayField(const char* fieldName)
  65. {
  66. if (mTargetField != StringTable->insert(fieldName, true))
  67. {
  68. mSelectedIndex = -1;
  69. }
  70. mTargetField = StringTable->insert(fieldName, true);
  71. mDirty = true;
  72. }
  73. void GuiParticleGraphInspector::setDisplayField(const char* fieldName, U16 index)
  74. {
  75. mEmitterIndex = index;
  76. setDisplayField(fieldName);
  77. }
  78. void GuiParticleGraphInspector::setDisplayArea(StringTableEntry minX, StringTableEntry minY, StringTableEntry maxX, StringTableEntry maxY)
  79. {
  80. mMinXLabel = minX;
  81. mMinX = dAtof(minX);
  82. mMinYLabel = minY;
  83. mMinY = dAtof(minY);
  84. mMaxXLabel = maxX;
  85. mMaxX = dAtof(maxX);
  86. mMaxYLabel = maxY;
  87. mMaxY = dAtof(maxY);
  88. mDirty = true;
  89. }
  90. void GuiParticleGraphInspector::setDisplayLabels(const char* labelX, const char* labelY)
  91. {
  92. mLabelX = StringTable->insert(labelX, true);
  93. mLabelY = StringTable->insert(labelY, true);
  94. }
  95. ParticleAssetField* GuiParticleGraphInspector::getTargetField()
  96. {
  97. ParticleAssetFieldCollection &collection = mTargetAsset->getParticleFields();
  98. ParticleAssetField* field = collection.findField(mTargetField);
  99. if (field == NULL)
  100. {
  101. if (mEmitterIndex >= mTargetAsset->getEmitterCount())
  102. {
  103. mEmitterIndex = mTargetAsset->getEmitterCount() - 1;
  104. }
  105. ParticleAssetEmitter* emitter = mTargetAsset->getEmitter(mEmitterIndex);
  106. ParticleAssetFieldCollection &emitterCollection = emitter->getParticleFields();
  107. field = emitterCollection.findField(mTargetField);
  108. }
  109. AssertFatal(field != NULL, "GuiParticleGraphInspector::getTargetField() - Unable to find the requested field.");
  110. return field;
  111. }
  112. void GuiParticleGraphInspector::resize(const Point2I &newPosition, const Point2I &newExtent)
  113. {
  114. GuiControl::resize(newPosition, newExtent);
  115. mDirty = true;
  116. }
  117. void GuiParticleGraphInspector::setControlProfile(GuiControlProfile *prof)
  118. {
  119. GuiControl::setControlProfile(prof);
  120. mDirty = true;
  121. }
  122. void GuiParticleGraphInspector::onTouchUp(const GuiEvent &event)
  123. {
  124. if(mTargetAsset)
  125. mTargetAsset->refreshAsset();
  126. }
  127. void GuiParticleGraphInspector::onTouchDown(const GuiEvent &event)
  128. {
  129. if(!mTargetAsset)
  130. return;
  131. mSelectedIndex = findHitGraphPoint(event.mousePoint);
  132. if (mSelectedIndex != -1 && event.mouseClickCount == 2)
  133. {
  134. //remove the point
  135. ParticleAssetField* field = getTargetField();
  136. field->removeDataKey(mSelectedIndex);
  137. mDirty = true;
  138. }
  139. else if (mSelectedIndex == -1 && mGridRect.pointInRect(event.mousePoint))
  140. {
  141. //Time to create a new point!
  142. ParticleAssetField* field = getTargetField();
  143. F32 time = getGraphTime(event.mousePoint.x);
  144. F32 value = getGraphValue(event.mousePoint.y);
  145. mSelectedIndex = field->addDataKey(time, value);
  146. mDirty = true;
  147. }
  148. }
  149. void GuiParticleGraphInspector::onTouchDragged(const GuiEvent &event)
  150. {
  151. if (!mTargetAsset)
  152. return;
  153. 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));
  154. if (mSelectedIndex == 0)
  155. {
  156. //Time to move the first point!
  157. ParticleAssetField* field = getTargetField();
  158. F32 value = getGraphValue(point.y);
  159. field->setDataKeyValue(mSelectedIndex, value);
  160. mDirty = true;
  161. }
  162. else if (mSelectedIndex > 0)
  163. {
  164. //Time to move a point!
  165. ParticleAssetField* field = getTargetField();
  166. F32 time = getGraphTime(point.x);
  167. F32 value = getGraphValue(point.y);
  168. if (time == field->getDataKeyTime(mSelectedIndex) || field->doesKeyExist(time))
  169. {
  170. //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.
  171. field->setDataKeyValue(mSelectedIndex, value);
  172. }
  173. else
  174. {
  175. //Time travel! Destroy the old point. Recreate in a new time.
  176. field->removeDataKey(mSelectedIndex);
  177. mSelectedIndex = field->addDataKey(time, value);
  178. }
  179. mDirty = true;
  180. }
  181. }
  182. U32 GuiParticleGraphInspector::findHitGraphPoint(const Point2I &point)
  183. {
  184. for (U32 i = 0; i < mPointList.size(); i++)
  185. {
  186. F32 x = mPointList[i].mPoint.x - point.x;
  187. F32 y = mPointList[i].mPoint.y - point.y;
  188. F32 dist = mSqrt((x * x) + (y * y));
  189. if (dist <= mRadius)
  190. {
  191. return i;
  192. }
  193. }
  194. return -1;
  195. }
  196. F32 GuiParticleGraphInspector::getGraphValue(const F32 y)
  197. {
  198. S32 len = mGridRect.extent.y;
  199. F32 ratio = (F32)((y - mGridRect.point.y) / len);
  200. ratio = mRound(ratio * 100) / 100; //Snaps to a grid of 100 possible values.
  201. return mMinY + ((mMaxY - mMinY) * (1 - ratio));
  202. }
  203. F32 GuiParticleGraphInspector::getGraphTime(const F32 x)
  204. {
  205. S32 len = mGridRect.extent.x;
  206. F32 ratio = (F32)((x - mGridRect.point.x) / len);
  207. ratio = mRound(ratio * 100) / 100; //Snaps to a grid of 100 possible values.
  208. return mMinX + ((mMaxX - mMinX) * ratio);
  209. }
  210. void GuiParticleGraphInspector::onRender(Point2I offset, const RectI &updateRect)
  211. {
  212. RectI ctrlRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  213. if (!ctrlRect.isValidRect())
  214. {
  215. return;
  216. }
  217. renderUniversalRect(ctrlRect, mProfile, NormalState);
  218. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, NormalState, mProfile);
  219. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, NormalState, mProfile);
  220. //Make room for the graph labels
  221. GFont *font = mProfile->mFont;
  222. U32 fontHeight = font->getHeight();
  223. contentRect.extent.y -= fontHeight;
  224. U8 xReduction = getMax(getMax(fontHeight, font->getStrWidth(mMaxYLabel)), font->getStrWidth(mMinYLabel));
  225. contentRect.extent.x -= xReduction;
  226. contentRect.point.x += xReduction;
  227. //reduce the contentRect to be divisible by 10
  228. U32 modX = contentRect.len_x() % 10;
  229. U32 modY = contentRect.len_y() % 10;
  230. contentRect.extent.set(contentRect.len_x() - modX, contentRect.len_y() - modY);
  231. contentRect.point.set(contentRect.point.x + mFloor(modX / 2), contentRect.point.y + mFloor(modY / 2));
  232. //Draw the labels
  233. ColorI gridColor = mProfile->getFillColor(HighlightState);
  234. renderLabels(contentRect, gridColor);
  235. if (contentRect.isValidRect())
  236. {
  237. renderGrid(contentRect, gridColor);
  238. if (mCalculationOffset != offset)
  239. {
  240. mDirty = true;
  241. }
  242. renderPoints(contentRect, mProfile->getFillColor(SelectedState));
  243. mCalculationOffset = offset;
  244. }
  245. }
  246. void GuiParticleGraphInspector::renderLabels(const RectI &contentRect, const ColorI &labelColor)
  247. {
  248. GFont *font = mProfile->mFont;
  249. U32 fontHeight = font->getHeight();
  250. U32 textWidth;
  251. Point2I textPoint;
  252. //Set the color used for the grid. This will also be used for the text.
  253. dglSetBitmapModulation(labelColor);
  254. //x label
  255. textWidth = font->getStrWidth(mLabelX);
  256. textPoint = Point2I(contentRect.point.x + (contentRect.extent.x / 2) - (textWidth / 2), contentRect.point.y + contentRect.extent.y + 2);
  257. dglDrawText(font, textPoint, mLabelX, NULL, 0, 0);
  258. //x min label
  259. textWidth = font->getStrWidth(mMinXLabel);
  260. textPoint = Point2I(contentRect.point.x + 1, contentRect.point.y + contentRect.extent.y + 2);
  261. dglDrawText(font, textPoint, mMinXLabel, NULL, 0, 0);
  262. //x max label
  263. textWidth = font->getStrWidth(mMaxXLabel);
  264. textPoint = Point2I((contentRect.point.x + contentRect.extent.x - 1) - textWidth, contentRect.point.y + contentRect.extent.y + 2);
  265. dglDrawText(font, textPoint, mMaxXLabel, NULL, 0, 0);
  266. //y label
  267. textWidth = font->getStrWidth(mLabelY);
  268. textPoint = Point2I(contentRect.point.x - (fontHeight + 2), contentRect.point.y + (contentRect.extent.y / 2) + (textWidth / 2));
  269. dglDrawText(font, textPoint, mLabelY, NULL, 0, 90);
  270. //y min label
  271. textWidth = font->getStrWidth(mMinYLabel);
  272. textPoint = Point2I(contentRect.point.x - (textWidth + 2), (contentRect.point.y + contentRect.extent.y - 2) - (fontHeight / 2));
  273. dglDrawText(font, textPoint, mMinYLabel, NULL, 0, 0);
  274. //y max label
  275. textWidth = font->getStrWidth(mMaxYLabel);
  276. textPoint = Point2I(contentRect.point.x - (textWidth + 2), (contentRect.point.y + 4) - (fontHeight / 2));
  277. dglDrawText(font, textPoint, mMaxYLabel, NULL, 0, 0);
  278. }
  279. void GuiParticleGraphInspector::renderGrid(const RectI &contentRect, const ColorI &gridColor)
  280. {
  281. S32 x, y;
  282. x = contentRect.len_x() / 10;
  283. y = contentRect.len_y() / 10;
  284. //horizontal lines
  285. for (U8 i = 0; i < 11; i++)
  286. {
  287. if(i != 5)
  288. {
  289. dglDrawLine(Point2I(contentRect.point.x, contentRect.point.y + (y * i)), Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i)), gridColor);
  290. }
  291. else
  292. {
  293. Point2I p1 = Point2I(contentRect.point.x, contentRect.point.y + (y * i) - 1);
  294. Point2I p2 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i) - 1);
  295. Point2I p3 = Point2I(contentRect.point.x + contentRect.extent.x, contentRect.point.y + (y * i) + 1);
  296. Point2I p4 = Point2I(contentRect.point.x, contentRect.point.y + (y * i) + 1);
  297. dglDrawQuadFill(p1, p2, p3, p4, gridColor);
  298. }
  299. }
  300. //vertical lines
  301. for (U8 i = 0; i < 11; i++)
  302. {
  303. if (i != 5)
  304. {
  305. dglDrawLine(Point2I(contentRect.point.x + (x * i), contentRect.point.y), Point2I(contentRect.point.x + (x * i), contentRect.point.y + contentRect.extent.y), gridColor);
  306. }
  307. else
  308. {
  309. Point2I p1 = Point2I(contentRect.point.x + (x * i) - 1, contentRect.point.y);
  310. Point2I p2 = Point2I(contentRect.point.x + (x * i) + 1, contentRect.point.y);
  311. Point2I p3 = Point2I(contentRect.point.x + (x * i) + 1, contentRect.point.y + contentRect.extent.y);
  312. Point2I p4 = Point2I(contentRect.point.x + (x * i) - 1, contentRect.point.y + contentRect.extent.y);
  313. dglDrawQuadFill(p1, p2, p3, p4, gridColor);
  314. }
  315. }
  316. }
  317. void GuiParticleGraphInspector::calculatePoints(const RectI &contentRect)
  318. {
  319. mGridRect = RectI(contentRect);
  320. mPointList.clear();
  321. ParticleAssetField* field = getTargetField();
  322. F32 time = 0;
  323. U32 count = field->getDataKeyCount();
  324. for (U32 i = 0; i < count; i++)
  325. {
  326. ParticleAssetField::DataKey key = field->getDataKey(i);
  327. //force the first key to always be at time zero
  328. if (i == 0 && key.mTime != 0)
  329. {
  330. field->addDataKey(0, key.mValue);
  331. field->removeDataKey(1);
  332. key = field->getDataKey(0);
  333. count = field->getDataKeyCount();
  334. }
  335. //Remove the point if it has a bad time
  336. if (i > 0 && key.mTime <= time)
  337. {
  338. field->removeDataKey(i);
  339. count--;
  340. continue;
  341. }
  342. time = key.mTime;
  343. }
  344. F32 width = mMaxX - mMinX;
  345. F32 height = mMaxY - mMinY;
  346. Point2I p;
  347. for (U32 i = 0; i < count; i++)
  348. {
  349. ParticleAssetField::DataKey key = field->getDataKey(i);
  350. F32 ratioX = (key.mTime - mMinX) / width;
  351. F32 ratioY = (key.mValue - mMinY) / height;
  352. p = Point2I(contentRect.point.x + (contentRect.extent.x * ratioX), contentRect.point.y + (contentRect.extent.y * (1 - ratioY)));
  353. mPointList.push_back(GraphPoint(p, key.mTime, key.mValue, i));
  354. }
  355. mDirty = false;
  356. }
  357. void GuiParticleGraphInspector::renderPoints(const RectI &contentRect, const ColorI &lineColor)
  358. {
  359. if (mTargetAsset)
  360. {
  361. if (mDirty)
  362. {
  363. calculatePoints(contentRect);
  364. }
  365. //get the cursor position
  366. Point2I cursorPt = Point2I(0, 0);
  367. GuiCanvas *root = getRoot();
  368. if (root)
  369. {
  370. cursorPt = root->getCursorPos();
  371. }
  372. //Render the lines first
  373. Point2I p1, p2;
  374. U32 count = mPointList.size();
  375. for(U32 i = 1; i < count; i++)
  376. {
  377. p1 = mPointList[i-1].mPoint;
  378. p2 = mPointList[i].mPoint;
  379. renderLine(contentRect, p1, p2, lineColor);
  380. renderDot(contentRect, p1, cursorPt, mSelectedIndex == (i - 1));
  381. }
  382. p1 = mPointList[count - 1].mPoint;
  383. p2 = Point2I(contentRect.point.x + contentRect.extent.x, p1.y);
  384. if (p1.x < p2.x)
  385. {
  386. renderLine(contentRect, p1, p2, lineColor);
  387. }
  388. renderDot(contentRect, p1, cursorPt, mSelectedIndex == (count - 1));
  389. }
  390. }
  391. void GuiParticleGraphInspector::renderDot(const RectI &contentRect, const Point2I &point, const Point2I &cursorPt, bool isSelected)
  392. {
  393. 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)
  394. {
  395. F32 x = cursorPt.x - point.x;
  396. F32 y = cursorPt.y - point.y;
  397. F32 dist = mSqrt((x * x) + (y * y));
  398. ColorI color;
  399. if (isSelected)
  400. {
  401. color = mProfile->getFontColor(SelectedState);
  402. }
  403. else if (dist <= mRadius)
  404. {
  405. color = mProfile->getFontColor(HighlightState);
  406. }
  407. else
  408. {
  409. color = mProfile->getFontColor(NormalState);
  410. }
  411. dglDrawCircleFill(point, mRadius, ColorI(0, 0, 0, 100));
  412. dglDrawCircleFill(point, mRadius - 2, color);
  413. }
  414. }
  415. void GuiParticleGraphInspector::renderLine(const RectI &contentRect, const Point2I &point1, const Point2I &point2, const ColorI &lineColor)
  416. {
  417. RectClipper clipper = RectClipper(contentRect);
  418. Point2I p1;
  419. Point2I p2;
  420. if(clipper.clipLine(point1, point2, p1, p2))
  421. {
  422. dglDrawLine(p1, p2, ColorI(lineColor));
  423. }
  424. }