Skip to content

Commit 29670a7

Browse files
committed
Added a simple settings window and a preset chooser dialog.
1 parent b59488a commit 29670a7

5 files changed

+269
-29
lines changed

src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ add_executable(projectMSDL WIN32
1515
AudioCapture.h
1616
FPSLimiter.cpp
1717
FPSLimiter.h
18+
GuiFileChooserWindow.cpp
19+
GuiFileChooserWindow.h
1820
PresetSelectionGui.cpp
1921
PresetSelectionGui.h
2022
ProjectMSDLApplication.cpp

src/GuiFileChooserWindow.cpp

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#include "GuiFileChooserWindow.h"
2+
3+
#include "imgui.h"
4+
5+
#include <Poco/SortedDirectoryIterator.h>
6+
#include <Poco/String.h>
7+
8+
void GuiFileChooserWindow::Show()
9+
{
10+
_visible = true;
11+
}
12+
13+
bool GuiFileChooserWindow::Draw()
14+
{
15+
if (!_visible)
16+
{
17+
return false;
18+
}
19+
20+
bool fileSelected{false};
21+
22+
if (!_currentDir.isDirectory() || !Poco::File(_currentDir).exists())
23+
{
24+
_currentDir = Poco::Path::home();
25+
}
26+
27+
if (ImGui::Begin("Load Preset", &_visible), ImGuiWindowFlags_NoCollapse)
28+
{
29+
DrawNavButtons();
30+
31+
ImGui::Separator();
32+
33+
ImGui::Text("%s", _currentDir.toString().c_str());
34+
35+
if (ImGui::BeginListBox("##filelist", ImVec2(-1,-1)))
36+
{
37+
Poco::SortedDirectoryIterator directoryIterator(_currentDir);
38+
Poco::SortedDirectoryIterator directoryEnd;
39+
40+
int index = 0;
41+
while (directoryIterator != directoryEnd)
42+
{
43+
bool isSelected = _selectedFileIndex == index;
44+
bool isDirectory = false;
45+
bool isHidden = false;
46+
try
47+
{
48+
// This will throw for broken symlinks or if the file/dir isn't accessible
49+
isHidden = directoryIterator->isHidden();
50+
isDirectory = directoryIterator->isDirectory();
51+
}
52+
catch (...)
53+
{
54+
}
55+
56+
if (!_showhidden && isHidden)
57+
{
58+
++directoryIterator;
59+
continue;
60+
}
61+
62+
auto filename = directoryIterator.name();
63+
64+
if (isDirectory)
65+
{
66+
filename.append("/");
67+
}
68+
else
69+
{
70+
if (Poco::icompare(directoryIterator.path().getExtension(), "milk") != 0)
71+
{
72+
++directoryIterator;
73+
continue;
74+
}
75+
}
76+
77+
if (ImGui::Selectable(filename.c_str(), isSelected, ImGuiSelectableFlags_AllowDoubleClick))
78+
{
79+
_selectedFileIndex = index;
80+
81+
if (ImGui::IsMouseDoubleClicked(0))
82+
{
83+
if (isDirectory)
84+
{
85+
_currentDir = directoryIterator.path();
86+
_currentDir.makeDirectory();
87+
poco_debug_f1(_logger, "Changing dir to: %s", _currentDir.toString());
88+
}
89+
else
90+
{
91+
_selectedFile = directoryIterator.path();
92+
poco_debug_f1(_logger, "User selected file: %s", _selectedFile.path());
93+
fileSelected = true;
94+
_visible = false;
95+
}
96+
}
97+
}
98+
99+
++directoryIterator;
100+
index++;
101+
}
102+
103+
ImGui::EndListBox();
104+
}
105+
}
106+
else
107+
{
108+
_visible = false;
109+
}
110+
111+
ImGui::End();
112+
113+
return fileSelected;
114+
}
115+
void GuiFileChooserWindow::DrawNavButtons()
116+
{
117+
ImGui::Checkbox("Show hidden files", &_showhidden);
118+
119+
// Root path buttons first
120+
std::vector<std::string> roots;
121+
Poco::Path::listRoots(roots);
122+
123+
if (ImGui::Button("Up"))
124+
{
125+
_currentDir = _currentDir.parent();
126+
_currentDir.makeDirectory();
127+
poco_debug_f1(_logger, "Going one dir up: %s", _currentDir.toString());
128+
}
129+
130+
ImGui::SameLine();
131+
132+
if (ImGui::Button("Home"))
133+
{
134+
_currentDir = Poco::Path::home();
135+
poco_debug_f1(_logger, "Going to user's home dir: %s", _currentDir.toString());
136+
}
137+
138+
for (const auto& root: roots)
139+
{
140+
ImGui::SameLine();
141+
142+
if (ImGui::Button(root.c_str()))
143+
{
144+
_currentDir = root;
145+
146+
poco_debug_f1(_logger, "Changing root/drive to: %s", _currentDir.toString());
147+
}
148+
}
149+
}
150+
151+
const Poco::File& GuiFileChooserWindow::SelectedFile() const
152+
{
153+
return _selectedFile;
154+
}

src/GuiFileChooserWindow.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <Poco/File.h>
4+
#include <Poco/Path.h>
5+
#include <Poco/Logger.h>
6+
7+
class GuiFileChooserWindow
8+
{
9+
public:
10+
11+
void Show();
12+
13+
/**
14+
* @brief Draws the dialog and returns if the user chose a file.
15+
* @return True if the user chose a file, false if not.
16+
*/
17+
bool Draw();
18+
19+
const Poco::File& SelectedFile() const;
20+
21+
protected:
22+
void DrawNavButtons();
23+
24+
bool _visible{ false }; //!< File chooser window visible.
25+
bool _showhidden{ false }; //!< If true, hidden files/dirs are shown.
26+
Poco::Path _currentDir{ Poco::Path::current() }; //!< Current working dir.
27+
Poco::File _selectedFile; //!< Currently selected file.
28+
int _selectedFileIndex{ 0 }; //!< Currently selected item in the file list.
29+
30+
Poco::Logger& _logger{ Poco::Logger::get("GuiFileChooserWindow") };
31+
};

src/PresetSelectionGui.cpp

+69-28
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void PresetSelectionGui::initialize(Poco::Util::Application& app)
2222
IMGUI_CHECKVERSION();
2323
ImGui::CreateContext();
2424
ImGuiIO& io = ImGui::GetIO();
25-
(void)io;
25+
(void) io;
2626

2727
ImGui::StyleColorsDark();
2828

@@ -91,46 +91,43 @@ void PresetSelectionGui::UpdateFontSize()
9191
void PresetSelectionGui::ProcessInput(const SDL_Event& event)
9292
{
9393
ImGui_ImplSDL2_ProcessEvent(&event);
94+
unsigned int position;
95+
projectm_get_selected_preset_index(_projectMWrapper->ProjectM(), &position);
96+
_playlistPosition = static_cast<int>(position);
97+
_playlistSize = projectm_get_playlist_size(_projectMWrapper->ProjectM());
9498
}
9599

96100
void PresetSelectionGui::Draw()
97101
{
98-
ImGui_ImplOpenGL3_NewFrame();
99102
ImGui_ImplSDL2_NewFrame();
103+
ImGui_ImplOpenGL3_NewFrame();
100104
ImGui::NewFrame();
101105

106+
if (_settingsVisible)
102107
{
103-
static float f = 0.0f;
104-
static int counter = 0;
108+
DrawSettingsWindow();
105109

106-
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
110+
}
107111

108-
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
112+
try {
109113

110-
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
114+
if (_fileChooser.Draw())
115+
{
116+
// Preset selected, load & switch.
117+
std::string presetName = Poco::Path(_fileChooser.SelectedFile().path()).getFileName();
111118

112-
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
113-
counter++;
114-
ImGui::SameLine();
115-
ImGui::Text("counter = %d", counter);
119+
int ratingList[2]{};
120+
projectm_add_preset_url(_projectMWrapper->ProjectM(),
121+
_fileChooser.SelectedFile().path().c_str(),
122+
presetName.c_str(),
123+
&ratingList[0], 2);
124+
projectm_select_preset(_projectMWrapper->ProjectM(), projectm_get_playlist_size(_projectMWrapper->ProjectM()) - 1, true);
125+
}
116126

117-
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
118-
119-
static char text[1024 * 16] =
120-
"/*\n"
121-
" The Pentium F00F bug, shorthand for F0 0F C7 C8,\n"
122-
" the hexadecimal encoding of one offending instruction,\n"
123-
" more formally, the invalid operand with locked CMPXCHG8B\n"
124-
" instruction bug, is a design flaw in the majority of\n"
125-
" Intel Pentium, Pentium MMX, and Pentium OverDrive\n"
126-
" processors (all in the P5 microarchitecture).\n"
127-
"*/\n\n"
128-
"label:\n"
129-
"\tlock cmpxchg8b eax\n";
130-
131-
ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_None);
132-
133-
ImGui::End();
127+
}
128+
catch(Poco::Exception& ex)
129+
{
130+
poco_error_f1(_logger, "Exception in file chooser: %s", ex.message());
134131
}
135132

136133
ImGui::Render();
@@ -148,3 +145,47 @@ bool PresetSelectionGui::WantsMouseInput()
148145
auto& io = ImGui::GetIO();
149146
return io.WantCaptureMouse;
150147
}
148+
149+
void PresetSelectionGui::DrawSettingsWindow()
150+
{
151+
if (ImGui::Begin("projectM Settings", &_settingsVisible))
152+
{
153+
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Hint:");
154+
ImGui::SameLine();
155+
ImGui::Text("Press <TAB> after clicking on a slider to enter a custom value.");
156+
157+
if (ImGui::SliderFloat("Preset Display Duration", &_displayDuration, 1.0f, 60.0f))
158+
{
159+
projectm_set_preset_duration(_projectMWrapper->ProjectM(), _displayDuration);
160+
}
161+
162+
if (ImGui::SliderInt("Playlist Position", &_playlistPosition, 0, _playlistSize - 1))
163+
{
164+
projectm_select_preset(_projectMWrapper->ProjectM(), _playlistPosition, true);
165+
}
166+
167+
168+
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
169+
170+
if (ImGui::Button("Toggle Fullscreen"))
171+
{
172+
_sdlRenderingWindow->ToggleFullscreen();
173+
}
174+
175+
ImGui::SameLine();
176+
177+
if (ImGui::Button("Random Preset"))
178+
{
179+
projectm_select_random_preset(_projectMWrapper->ProjectM(), true);
180+
}
181+
182+
ImGui::SameLine();
183+
184+
if (ImGui::Button("Load Preset..."))
185+
{
186+
_fileChooser.Show();
187+
}
188+
189+
}
190+
ImGui::End();
191+
}

src/PresetSelectionGui.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "GuiFileChooserWindow.h"
4+
35
#include <SDL2/SDL.h>
46

57
#include <Poco/Logger.h>
@@ -31,14 +33,24 @@ class PresetSelectionGui : public Poco::Util::Subsystem
3133
bool WantsMouseInput();
3234

3335
protected:
36+
void DrawSettingsWindow();
37+
38+
ProjectMWrapper* _projectMWrapper{ nullptr };
39+
SDLRenderingWindow* _sdlRenderingWindow{ nullptr };
3440

3541
SDL_Window* _renderingWindow{ nullptr }; //!< Pointer to the SDL window used for rendering.
3642
SDL_GLContext _glContext{ nullptr }; //!< Pointer to the OpenGL context associated with the window.
3743
ImFont* _font{ nullptr }; //!< Currently loaded UI font.
3844

3945
float _dpi{ 0.0f }; //!< Last DPI value.
4046

41-
Poco::Logger& _logger{ Poco::Logger::get("PresetSelectionGui") }; //!< The class logger.
47+
GuiFileChooserWindow _fileChooser; //!< File chooser dialog.
4248

49+
bool _settingsVisible{ true }; //!< Flag for settings window visibility.
50+
float _displayDuration{ 0.0f }; //!< Preset display time
51+
int _playlistPosition{ 0 }; //!< Playlist position
52+
int _playlistSize{ 0 }; //!< Playlist size
53+
54+
Poco::Logger& _logger{ Poco::Logger::get("PresetSelectionGui") }; //!< The class logger.
4355

4456
};

0 commit comments

Comments
 (0)