TVCustomizeTrackColorsDlg.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : A dialog for customizing track colors
  9. #include "EditorDefs.h"
  10. #include "TVCustomizeTrackColorsDlg.h"
  11. // Qt
  12. #include <QLabel>
  13. #include <QMessageBox>
  14. #include <QSettings>
  15. // CryCommon
  16. #include <CryCommon/Maestro/Types/AnimParamType.h>
  17. // Editor
  18. #include "TrackViewDialog.h"
  19. #include "QtUI/ColorButton.h"
  20. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  21. #include <TrackView/ui_TVCustomizeTrackColorsDialog.h>
  22. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  23. #define TRACKCOLOR_ENTRY_PREFIX ("TrackColor")
  24. #define TRACKCOLOR_FOR_OTHERS_ENTRY ("TrackColorForOthers")
  25. #define TRACKCOLOR_FOR_DISABLED_ENTRY ("TrackColorForDisabled")
  26. #define TRACKCOLOR_FOR_MUTED_ENTRY ("TrackColorForMuted")
  27. struct STrackEntry
  28. {
  29. CAnimParamType paramType;
  30. QString name;
  31. QColor defaultColor;
  32. };
  33. namespace
  34. {
  35. const STrackEntry g_trackEntries[] = {
  36. // Color for tracks
  37. { AnimParamType::Position, "Pos", QColor(90, 150, 90) },
  38. { AnimParamType::Rotation, "Rot", QColor(90, 150, 90) },
  39. { AnimParamType::Scale, "Scale", QColor(90, 150, 90) },
  40. { AnimParamType::Event, "Event", QColor(220, 220, 220) },
  41. { AnimParamType::Visibility, "Visibility", QColor(220, 220, 220) },
  42. { AnimParamType::Camera, "Camera", QColor(220, 220, 220) },
  43. { AnimParamType::Sound, "Sound", QColor(220, 220, 220) },
  44. { AnimParamType::Animation, "Animation", QColor(220, 220, 220) },
  45. { AnimParamType::Sequence, "Sequence", QColor(220, 220, 220) },
  46. { AnimParamType::Console, "Console", QColor(220, 220, 220) },
  47. { AnimParamType::LookAt, "LookAt", QColor(220, 220, 220) },
  48. { AnimParamType::TrackEvent, "TrackEvent", QColor(220, 220, 220) },
  49. { AnimParamType::ShakeMultiplier, "ShakeMult", QColor(90, 150, 90) },
  50. { AnimParamType::TransformNoise, "Noise", QColor(90, 150, 90) },
  51. { AnimParamType::TimeWarp, "Timewarp", QColor(220, 220, 220) },
  52. { AnimParamType::FixedTimeStep, "FixedTimeStep", QColor(220, 220, 220) },
  53. { AnimParamType::DepthOfField, "DepthOfField", QColor(90, 150, 90) },
  54. { AnimParamType::CommentText, "CommentText", QColor(220, 220, 220) },
  55. { AnimParamType::ScreenFader, "ScreenFader", QColor(220, 220, 220) },
  56. { AnimParamType::LightDiffuse, "LightDiffuseColor", QColor(90, 150, 90) },
  57. { AnimParamType::LightRadius, "LightRadius", QColor(220, 220, 220) },
  58. { AnimParamType::LightDiffuseMult, "LightDiffuseMult", QColor(220, 220, 220) },
  59. { AnimParamType::LightHDRDynamic, "LightHDRDynamic", QColor(220, 220, 220) },
  60. { AnimParamType::LightSpecularMult, "LightSpecularMult", QColor(220, 220, 220) },
  61. { AnimParamType::LightSpecPercentage, "LightSpecularPercent", QColor(220, 220, 220) },
  62. { AnimParamType::FocusDistance, "FocusDistance", QColor(220, 220, 220) },
  63. { AnimParamType::FocusRange, "FocusRange", QColor(220, 220, 220) },
  64. { AnimParamType::BlurAmount, "BlurAmount", QColor(220, 220, 220) },
  65. { AnimParamType::PositionX, "PosX", QColor(220, 220, 220) },
  66. { AnimParamType::PositionY, "PosY", QColor(220, 220, 220) },
  67. { AnimParamType::PositionZ, "PosZ", QColor(220, 220, 220) },
  68. { AnimParamType::RotationX, "RotX", QColor(220, 220, 220) },
  69. { AnimParamType::RotationY, "RotY", QColor(220, 220, 220) },
  70. { AnimParamType::RotationZ, "RotZ", QColor(220, 220, 220) },
  71. { AnimParamType::ScaleX, "ScaleX", QColor(220, 220, 220) },
  72. { AnimParamType::ScaleY, "ScaleY", QColor(220, 220, 220) },
  73. { AnimParamType::ScaleZ, "ScaleZ", QColor(220, 220, 220) },
  74. { AnimParamType::ShakeAmpAMult, "ShakeMultAmpA", QColor(220, 220, 220) },
  75. { AnimParamType::ShakeAmpBMult, "ShakeMultAmpB", QColor(220, 220, 220) },
  76. { AnimParamType::ShakeFreqAMult, "ShakeMultFreqA", QColor(220, 220, 220) },
  77. { AnimParamType::ShakeFreqBMult, "ShakeMultFreqB", QColor(220, 220, 220) },
  78. { AnimParamType::ColorR, "ColorR", QColor(220, 220, 220) },
  79. { AnimParamType::ColorG, "ColorG", QColor(220, 220, 220) },
  80. { AnimParamType::ColorB, "ColorB", QColor(220, 220, 220) },
  81. { AnimParamType::MaterialOpacity, "MaterialOpacity", QColor(220, 220, 220) },
  82. { AnimParamType::MaterialSmoothness, "MaterialGlossiness", QColor(220, 220, 220) },
  83. { AnimParamType::MaterialEmissive, "MaterialEmission", QColor(220, 220, 220) },
  84. { AnimParamType::MaterialEmissiveIntensity, "MaterialEmissionIntensity", QColor(220, 220, 220) },
  85. { AnimParamType::NearZ, "NearZ", QColor(220, 220, 220) },
  86. { AnimParamType::User, "", QColor(0, 0, 0) }, // An empty string means a separator row.
  87. // Misc colors for special states of a track
  88. { AnimParamType::User, "Others", QColor(220, 220, 220) },
  89. { AnimParamType::User, "Disabled/Inactive", QColor(255, 224, 224) },
  90. { AnimParamType::User, "Muted", QColor(255, 224, 224) },
  91. };
  92. const int kMaxRows = 20;
  93. const int kColumnWidth = 300;
  94. const int kRowHeight = 24;
  95. const int kOthersEntryIndex = arraysize(g_trackEntries) - 3;
  96. const int kDisabledEntryIndex = arraysize(g_trackEntries) - 2;
  97. const int kMutedEntryIndex = arraysize(g_trackEntries) - 1;
  98. }
  99. std::map<CAnimParamType, QColor> CTVCustomizeTrackColorsDlg::s_trackColors;
  100. QColor CTVCustomizeTrackColorsDlg::s_colorForDisabled;
  101. QColor CTVCustomizeTrackColorsDlg::s_colorForMuted;
  102. QColor CTVCustomizeTrackColorsDlg::s_colorForOthers;
  103. CTVCustomizeTrackColorsDlg::CTVCustomizeTrackColorsDlg(QWidget* pParent)
  104. : QDialog(pParent)
  105. , m_aLabels(arraysize(g_trackEntries))
  106. , m_colorButtons(arraysize(g_trackEntries))
  107. , m_ui(new Ui::TVCustomizeTrackColorsDialog)
  108. {
  109. OnInitDialog();
  110. }
  111. CTVCustomizeTrackColorsDlg::~CTVCustomizeTrackColorsDlg()
  112. {
  113. }
  114. void CTVCustomizeTrackColorsDlg::OnInitDialog()
  115. {
  116. m_ui->setupUi(this);
  117. connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &CTVCustomizeTrackColorsDlg::accept);
  118. connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &CTVCustomizeTrackColorsDlg::reject);
  119. connect(m_ui->buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &CTVCustomizeTrackColorsDlg::OnApply);
  120. connect(m_ui->buttonResetAll, &QPushButton::clicked, this, &CTVCustomizeTrackColorsDlg::OnResetAll);
  121. connect(m_ui->buttonExport, &QPushButton::clicked, this, &CTVCustomizeTrackColorsDlg::OnExport);
  122. connect(m_ui->buttonImport, &QPushButton::clicked, this, &CTVCustomizeTrackColorsDlg::OnImport);
  123. QRect labelRect(QPoint(30, 30), QPoint(150, 50));
  124. QRect buttonRect(QPoint(180, 30), QPoint(280, 50));
  125. // Create a label and a color button for each track.
  126. int col = 0, i = 0;
  127. std::for_each(g_trackEntries, g_trackEntries + arraysize(g_trackEntries), [&](const STrackEntry& entry)
  128. {
  129. const QString labelText = entry.name;
  130. if(!labelText.isEmpty())
  131. {
  132. m_aLabels[i] = new QLabel(m_ui->frame);
  133. m_aLabels[i]->setGeometry(labelRect);
  134. m_aLabels[i]->setText(labelText);
  135. m_colorButtons[i] = new ColorButton(m_ui->frame);
  136. m_colorButtons[i]->setGeometry(buttonRect);
  137. if(entry.paramType.GetType() == AnimParamType::User)
  138. {
  139. assert(kOthersEntryIndex <= i);
  140. if (i == kOthersEntryIndex)
  141. {
  142. m_colorButtons[i]->SetColor(s_colorForOthers);
  143. }
  144. else if(i == kDisabledEntryIndex)
  145. {
  146. m_colorButtons[i]->SetColor(s_colorForDisabled);
  147. }
  148. else if(i == kMutedEntryIndex)
  149. {
  150. m_colorButtons[i]->SetColor(s_colorForMuted);
  151. }
  152. }
  153. else
  154. {
  155. m_colorButtons[i]->SetColor(s_trackColors[entry.paramType]);
  156. }
  157. }
  158. if(i % kMaxRows == kMaxRows - 1)
  159. {
  160. ++col;
  161. labelRect.moveTopLeft(QPoint(30+kColumnWidth*col, 30));
  162. buttonRect.moveTopLeft(QPoint(180+kColumnWidth*col, 30));
  163. }
  164. else
  165. {
  166. labelRect.translate(0, kRowHeight);
  167. buttonRect.translate(0, kRowHeight);
  168. }
  169. ++i;
  170. });
  171. // Resize this dialog to fit the contents.
  172. const QSize size(60 + kColumnWidth * (col + 1), 100 + kMaxRows * kRowHeight);
  173. m_ui->frame->setFixedSize(size);
  174. setFixedSize(sizeHint());
  175. }
  176. void CTVCustomizeTrackColorsDlg::accept()
  177. {
  178. OnApply();
  179. QDialog::accept();
  180. }
  181. void CTVCustomizeTrackColorsDlg::OnApply()
  182. {
  183. int i = 0;
  184. std::for_each(g_trackEntries, g_trackEntries + arraysize(g_trackEntries), [&](const STrackEntry& entry)
  185. {
  186. if(entry.paramType.GetType() != AnimParamType::User)
  187. {
  188. s_trackColors[entry.paramType] = m_colorButtons[i]->Color();
  189. }
  190. ++i;
  191. });
  192. s_colorForOthers = m_colorButtons[kOthersEntryIndex]->Color();
  193. s_colorForDisabled = m_colorButtons[kDisabledEntryIndex]->Color();
  194. s_colorForMuted = m_colorButtons[kMutedEntryIndex]->Color();
  195. CTrackViewDialog::GetCurrentInstance()->InvalidateDopeSheet();
  196. }
  197. void CTVCustomizeTrackColorsDlg::OnResetAll()
  198. {
  199. int i = 0;
  200. std::for_each(g_trackEntries, g_trackEntries + arraysize(g_trackEntries), [&](const STrackEntry& entry)
  201. {
  202. const QString labelText = entry.name;
  203. if(!labelText.isEmpty())
  204. {
  205. m_colorButtons[i]->SetColor(entry.defaultColor);
  206. }
  207. ++i;
  208. });
  209. }
  210. void CTVCustomizeTrackColorsDlg::SaveColors(const char* sectionName)
  211. {
  212. QSettings settings;
  213. for (auto g : QString(sectionName).split('\\'))
  214. {
  215. settings.beginGroup(g);
  216. }
  217. std::for_each(begin(s_trackColors), end(s_trackColors),
  218. [&](const std::pair<CAnimParamType, QColor>& pair)
  219. {
  220. const QString trackColorEntry = QString::fromLatin1("%1%2").arg(TRACKCOLOR_ENTRY_PREFIX).arg(static_cast<int>(pair.first.GetType()));
  221. settings.setValue(trackColorEntry, pair.second.rgb());
  222. });
  223. settings.setValue(TRACKCOLOR_FOR_OTHERS_ENTRY, s_colorForOthers.rgb());
  224. settings.setValue(TRACKCOLOR_FOR_DISABLED_ENTRY, s_colorForDisabled.rgb());
  225. settings.setValue(TRACKCOLOR_FOR_MUTED_ENTRY, s_colorForMuted.rgb());
  226. }
  227. void CTVCustomizeTrackColorsDlg::LoadColors(const char* sectionName)
  228. {
  229. QSettings settings;
  230. for (auto g : QString(sectionName).split('\\'))
  231. {
  232. settings.beginGroup(g);
  233. }
  234. std::for_each(g_trackEntries, g_trackEntries + arraysize(g_trackEntries), [&](const STrackEntry& entry)
  235. {
  236. if (entry.paramType.GetType() != AnimParamType::User)
  237. {
  238. s_trackColors[entry.paramType] = QColor::fromRgb(settings.value(QStringLiteral("%2%3").arg(TRACKCOLOR_ENTRY_PREFIX).arg(static_cast<int>(entry.paramType.GetType())), entry.defaultColor.rgb()).toInt());
  239. }
  240. });
  241. s_colorForOthers = QColor::fromRgb(settings.value(TRACKCOLOR_FOR_OTHERS_ENTRY, g_trackEntries[kOthersEntryIndex].defaultColor.rgb()).toInt());
  242. s_colorForDisabled = QColor::fromRgb(settings.value(TRACKCOLOR_FOR_DISABLED_ENTRY, g_trackEntries[kDisabledEntryIndex].defaultColor.rgb()).toInt());
  243. s_colorForMuted = QColor::fromRgb(settings.value(TRACKCOLOR_FOR_MUTED_ENTRY, g_trackEntries[kMutedEntryIndex].defaultColor.rgb()).toInt());
  244. }
  245. void CTVCustomizeTrackColorsDlg::OnExport()
  246. {
  247. QString savePath;
  248. if (CFileUtil::SelectSaveFile("Custom Track Colors Files (*.ctc)", "ctc",
  249. Path::GetUserSandboxFolder(), savePath))
  250. {
  251. Export(savePath);
  252. }
  253. }
  254. void CTVCustomizeTrackColorsDlg::OnImport()
  255. {
  256. QString loadPath;
  257. if (CFileUtil::SelectFile("Custom Track Colors Files (*.ctc)",
  258. Path::GetUserSandboxFolder(), loadPath))
  259. {
  260. if (Import(loadPath))
  261. {
  262. // since the user is explicitly pressing 'Import', we assume he or she wants to apply this import
  263. // to see the result immediately, based on a customer feedback sample of one
  264. OnApply();
  265. }
  266. else
  267. {
  268. QMessageBox::critical(this, tr("Cannot import"), tr("The file format is invalid!"));
  269. }
  270. }
  271. }
  272. void CTVCustomizeTrackColorsDlg::Export(const QString& fullPath) const
  273. {
  274. XmlNodeRef customTrackColorsNode = XmlHelpers::CreateXmlNode("customtrackcolors");
  275. int i = 0;
  276. std::for_each(g_trackEntries, g_trackEntries + arraysize(g_trackEntries), [&](const STrackEntry& entry)
  277. {
  278. if(entry.paramType.GetType() != AnimParamType::User)
  279. {
  280. XmlNodeRef entryNode = customTrackColorsNode->newChild("entry");
  281. // Serialization is const safe
  282. CAnimParamType &paramType = const_cast<CAnimParamType&>( entry.paramType );
  283. paramType.Serialize( entryNode, false );
  284. entryNode->setAttr("color", m_colorButtons[i]->Color().rgb());
  285. }
  286. ++i;
  287. });
  288. XmlNodeRef othersNode = customTrackColorsNode->newChild("others");
  289. othersNode->setAttr("color", m_colorButtons[kOthersEntryIndex]->Color().rgb());
  290. XmlNodeRef disabledNode = customTrackColorsNode->newChild("disabled");
  291. disabledNode->setAttr("color", m_colorButtons[kDisabledEntryIndex]->Color().rgb());
  292. XmlNodeRef mutedNode = customTrackColorsNode->newChild("muted");
  293. mutedNode->setAttr("color", m_colorButtons[kMutedEntryIndex]->Color().rgb());
  294. XmlHelpers::SaveXmlNode(GetIEditor()->GetFileUtil(), customTrackColorsNode, fullPath.toStdString().c_str());
  295. }
  296. bool CTVCustomizeTrackColorsDlg::Import(const QString& fullPath)
  297. {
  298. XmlNodeRef customTrackColorsNode = XmlHelpers::LoadXmlFromFile(fullPath.toStdString().c_str());
  299. if (customTrackColorsNode == nullptr)
  300. {
  301. return false;
  302. }
  303. QColor color;
  304. for (int i = 0; i < customTrackColorsNode->getChildCount(); ++i)
  305. {
  306. XmlNodeRef childNode = customTrackColorsNode->getChild(i);
  307. if (QString(childNode->getTag()) != "entry")
  308. {
  309. continue;
  310. }
  311. CAnimParamType paramType;
  312. paramType.Serialize(childNode, true);
  313. // Get the entry index for this param type.
  314. const STrackEntry* pEntry = std::find_if(g_trackEntries, g_trackEntries + arraysize(g_trackEntries),
  315. [=](const STrackEntry& entry)
  316. {
  317. return entry.paramType == paramType;
  318. });
  319. int entryIndex = static_cast<int>(pEntry - g_trackEntries);
  320. if (entryIndex >= arraysize(g_trackEntries)) // If not found, skip this.
  321. {
  322. continue;
  323. }
  324. GetQColorFromXmlNode(color, childNode);
  325. m_colorButtons[entryIndex]->SetColor(color);
  326. }
  327. XmlNodeRef othersNode = customTrackColorsNode->findChild("others");
  328. if (othersNode)
  329. {
  330. GetQColorFromXmlNode(color, othersNode);
  331. m_colorButtons[kOthersEntryIndex]->SetColor(color);
  332. }
  333. XmlNodeRef disabledNode = customTrackColorsNode->findChild("disabled");
  334. if (disabledNode)
  335. {
  336. GetQColorFromXmlNode(color, disabledNode);
  337. m_colorButtons[kDisabledEntryIndex]->SetColor(color);
  338. }
  339. XmlNodeRef mutedNode = customTrackColorsNode->findChild("muted");
  340. if (mutedNode)
  341. {
  342. GetQColorFromXmlNode(color, mutedNode);
  343. m_colorButtons[kMutedEntryIndex]->SetColor(color);
  344. }
  345. return true;
  346. }
  347. #include <TrackView/moc_TVCustomizeTrackColorsDlg.cpp>