Debian Codegraph @

src/gui/newSong.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "gui.h"
21 #include "misc/cpp/imgui_stdlib.h"
22 #include <fmt/printf.h>
23 #include <algorithm>
24
25 String sysDefID;
26
27 void FurnaceGUI::drawSysDefs(std::vector<FurnaceGUISysDef>& category, bool& accepted, std::vector<int>& sysDefStack, bool& alreadyHover) {
28 int index=0;
29 String sysDefIDLeader="##NS";
30 for (int i: sysDefStack) {
31 sysDefIDLeader+=fmt::sprintf("/%d",i);
32 }
33 for (FurnaceGUISysDef& i: category) {
34 bool treeNode=false;
35 bool isHovered=false;
36 ImGui::TableNextRow();
37 ImGui::TableNextColumn();
38 if (!i.subDefs.empty()) {
39 if (i.orig.empty()) {
40 sysDefID=fmt::sprintf("%s%s/%dS",_(i.name.c_str()),sysDefIDLeader,index);
41 } else {
42 sysDefID=fmt::sprintf("%s/%dS",sysDefIDLeader,index);
43 }
44 treeNode=ImGui::TreeNodeEx(sysDefID.c_str(),i.orig.empty()?ImGuiTreeNodeFlags_SpanAvailWidth:0);
45 ImGui::SameLine();
46 }
47 if (!i.orig.empty()) {
48 sysDefID=fmt::sprintf("%s%s/%d",_(i.name.c_str()),sysDefIDLeader,index);
49 if (ImGui::Selectable(sysDefID.c_str(),false,ImGuiSelectableFlags_DontClosePopups)) {
50 nextDesc=i.definition;
51 nextDescName=i.name;
52 accepted=true;
53 }
54 if (ImGui::IsItemHovered()) isHovered=true;
55 } else if (i.subDefs.empty()) {
56 ImGui::TextUnformatted(i.name.c_str());
57 if (ImGui::IsItemHovered()) isHovered=true;
58 }
59 if (treeNode) {
60 sysDefStack.push_back(index);
61 drawSysDefs(i.subDefs,accepted,sysDefStack,alreadyHover);
62 sysDefStack.erase(sysDefStack.end()-1);
63 ImGui::TreePop();
64 }
65 if (isHovered && !alreadyHover) {
66 alreadyHover=true;
67 if (ImGui::BeginTooltip()) {
68 ImGui::Dummy(ImVec2(400.0f*dpiScale,0.0f));
69 std::map<DivSystem,int> chipCounts;
70 std::vector<DivSystem> chips;
71 for (FurnaceGUISysDefChip chip: i.orig) {
72 if (chipCounts.find(chip.sys)==chipCounts.end()) {
73 chipCounts[chip.sys]=1;
74 chips.push_back(chip.sys);
75 } else {
76 chipCounts[chip.sys]+=1;
77 }
78 }
79 for (size_t chipIndex=0; chipIndex<chips.size(); chipIndex++) {
80 DivSystem chip=chips[chipIndex];
81 const DivSysDef* sysDef=e->getSystemDef(chip);
82 ImGui::PushTextWrapPos(MIN(scrW*dpiScale,400.0f*dpiScale));
83 ImGui::Text("%s (x%d): ",sysDef->name,chipCounts[chip]);
84 ImGui::Text("%s",sysDef->description);
85 ImGui::Separator();
86 drawSystemChannelInfoText(sysDef);
87 drawSystemChannelInfo(sysDef);
88 ImGui::PopTextWrapPos();
89 if (chipIndex+1<chips.size()) {
90 ImGui::Separator();
91 }
92 }
93
94 ImGui::EndTooltip();
95 }
96 }
97 index++;
98 }
99 }
100
101 void findInSubs(std::vector<FurnaceGUISysDef>& where, std::vector<FurnaceGUISysDef>& newSongSearchResults, String lowerCase) {
102 for (FurnaceGUISysDef& j: where) {
103 if (!j.orig.empty()) {
104 String lowerCase1=j.name;
105 for (char& i: lowerCase1) {
106 if (i>='A' && i<='Z') i+='a'-'A';
107 }
108 auto lastItem=std::remove_if(lowerCase1.begin(),lowerCase1.end(),[](char c) {
109 return (c==' ' || c=='_' || c=='-');
110 });
111 lowerCase1.erase(lastItem,lowerCase1.end());
112 if (lowerCase1.find(lowerCase)!=String::npos) {
113 newSongSearchResults.push_back(j);
114 newSongSearchResults[newSongSearchResults.size()-1].subDefs.clear();
115 }
116 }
117 findInSubs(j.subDefs,newSongSearchResults,lowerCase);
118 }
119 }
120
121 void FurnaceGUI::drawNewSong() {
122 bool accepted=false;
123 std::vector<int> sysDefStack;
124
125 ImGui::PushFont(bigFont);
126 ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x-ImGui::CalcTextSize(_("Choose a System!")).x)*0.5);
127 ImGui::Text(_("Choose a System!"));
128 ImGui::PopFont();
129
130 ImVec2 avail=ImGui::GetContentRegionAvail();
131 avail.y-=ImGui::GetFrameHeightWithSpacing();
132
133 if (ImGui::BeginChild("sysPickerC",avail,0,ImGuiWindowFlags_NoScrollWithMouse|ImGuiWindowFlags_NoScrollbar)) {
134 if (newSongFirstFrame)
135 ImGui::SetKeyboardFocusHere();
136 ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x);
137 if (ImGui::InputTextWithHint("##SysSearch",_("Search..."),&newSongQuery)) {
138 String lowerCase=newSongQuery;
139 for (char& i: lowerCase) {
140 if (i>='A' && i<='Z') i+='a'-'A';
141 }
142 auto lastItem=std::remove_if(lowerCase.begin(),lowerCase.end(),[](char c) {
143 return (c==' ' || c=='_' || c=='-');
144 });
145 lowerCase.erase(lastItem,lowerCase.end());
146 newSongSearchResults.clear();
147 for (FurnaceGUISysCategory& i: sysCategories) {
148 for (FurnaceGUISysDef& j: i.systems) {
149 if (!j.orig.empty()) {
150 String lowerCase1=j.name;
151 for (char& i: lowerCase1) {
152 if (i>='A' && i<='Z') i+='a'-'A';
153 }
154 auto lastItem=std::remove_if(lowerCase1.begin(),lowerCase1.end(),[](char c) {
155 return (c==' ' || c=='_' || c=='-');
156 });
157 lowerCase1.erase(lastItem,lowerCase1.end());
158 if (lowerCase1.find(lowerCase)!=String::npos) {
159 newSongSearchResults.push_back(j);
160 newSongSearchResults[newSongSearchResults.size()-1].subDefs.clear();
161 }
162 }
163 findInSubs(j.subDefs,newSongSearchResults,lowerCase);
164 }
165 }
166 std::sort(newSongSearchResults.begin(),newSongSearchResults.end(),[](const FurnaceGUISysDef& a, const FurnaceGUISysDef& b) {
167 return strcmp(a.name.c_str(),b.name.c_str())<0;
168 });
169 auto lastItem1=std::unique(newSongSearchResults.begin(),newSongSearchResults.end(),[](const FurnaceGUISysDef& a, const FurnaceGUISysDef& b) {
170 return a.name==b.name;
171 });
172 newSongSearchResults.erase(lastItem1,newSongSearchResults.end());
173 }
174 if (ImGui::BeginTable("sysPicker",newSongQuery.empty()?2:1,ImGuiTableFlags_BordersInnerV)) {
175 if (newSongQuery.empty()) {
176 ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthFixed,0.0f);
177 }
178 ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch,0.0f);
179
180 if (newSongQuery.empty()) {
181 ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
182 ImGui::TableNextColumn();
183 ImGui::Text(_("Categories"));
184 ImGui::TableNextColumn();
185 ImGui::Text(_("Systems"));
186 }
187
188 ImGui::TableNextRow();
189
190 // CATEGORIES
191 if (newSongQuery.empty()) {
192 ImGui::TableNextColumn();
193 int index=0;
194 for (FurnaceGUISysCategory& i: sysCategories) {
195 if (ImGui::Selectable(i.name,newSongCategory==index,ImGuiSelectableFlags_DontClosePopups)) {
196 newSongCategory=index;
197 }
198 if (ImGui::IsItemHovered()) {
199 ImGui::SetTooltip("%s",i.description);
200 }
201 if (strcmp(i.name,_("User"))==0) ImGui::Separator();
202 index++;
203 }
204 }
205
206 // SYSTEMS
207 ImGui::TableNextColumn();
208 if (ImGui::BeginTable("Systems",1,ImGuiTableFlags_BordersInnerV|ImGuiTableFlags_ScrollY)) {
209 std::vector<FurnaceGUISysDef>& category=(newSongQuery.empty())?(sysCategories[newSongCategory].systems):(newSongSearchResults);
210 if (category.empty()) {
211 ImGui::TableNextRow();
212 ImGui::TableNextColumn();
213 if (newSongQuery.empty()) {
214 ImGui::Text(_("no systems here yet!"));
215 } else {
216 ImGui::Text(_("no results"));
217 }
218 } else {
219 bool alreadyHover=false;
220 sysDefStack.push_back(newSongQuery.empty()?newSongCategory:-1);
221 drawSysDefs(category,accepted,sysDefStack,alreadyHover);
222 sysDefStack.erase(sysDefStack.end()-1);
223 }
224 ImGui::EndTable();
225 }
226
227 ImGui::EndTable();
228 }
229 }
230 ImGui::EndChild();
231
232 if (ImGui::Button(_("I'm feeling lucky"))) {
233 if (sysCategories.size()==0) {
234 showError(_("no categories available! what in the world."));
235 ImGui::CloseCurrentPopup();
236 } else {
237 int tries=0;
238 for (tries=0; tries<50; tries++) {
239 FurnaceGUISysCategory* newSystemCat=&sysCategories[rand()%sysCategories.size()];
240 if (newSystemCat->systems.empty()) {
241 continue;
242 } else {
243 unsigned int selection=rand()%newSystemCat->systems.size();
244
245 if (newSystemCat->systems[selection].orig.empty() && newSystemCat->systems[selection].subDefs.empty()) continue;
246 if (!newSystemCat->systems[selection].subDefs.empty()) {
247 if (rand()%2) {
248 unsigned int subSel=rand()%newSystemCat->systems[selection].subDefs.size();
249 nextDesc=newSystemCat->systems[selection].subDefs[subSel].definition;
250 nextDescName=newSystemCat->systems[selection].subDefs[subSel].name;
251 accepted=true;
252 } else {
253 if (newSystemCat->systems[selection].orig.empty()) continue;
254 nextDesc=newSystemCat->systems[selection].definition;
255 nextDescName=newSystemCat->systems[selection].name;
256 accepted=true;
257 }
258 } else {
259 nextDesc=newSystemCat->systems[selection].definition;
260 nextDescName=newSystemCat->systems[selection].name;
261 accepted=true;
262 }
263 }
264
265 if (accepted) break;
266 }
267
268 if (tries>=50) {
269 showError(_("it appears you're extremely lucky today!"));
270 ImGui::CloseCurrentPopup();
271 }
272 }
273 }
274
275 ImGui::SameLine();
276
277 if (ImGui::Button(_("Cancel")) || ImGui::IsKeyPressed(ImGuiKey_Escape)) {
278 ImGui::CloseCurrentPopup();
279 }
280
281 if (accepted) {
282 e->createNew(nextDesc.c_str(),nextDescName,false);
283 undoHist.clear();
284 redoHist.clear();
285 curFileName="";
286 modified=false;
287 curNibble=false;
288 orderNibble=false;
289 orderCursor=-1;
290 samplePos=0;
291 updateSampleTex=true;
292 selStart=SelectionPoint();
293 selEnd=SelectionPoint();
294 cursor=SelectionPoint();
295 updateWindowTitle();
296 updateROMExportAvail();
297 ImGui::CloseCurrentPopup();
298 }
299
300 newSongFirstFrame=false;
301 }