Skip to content

Commit

Permalink
SampleSelectionDialog: preview playback
Browse files Browse the repository at this point in the history
  • Loading branch information
momentarylapse committed Dec 16, 2024
1 parent 0c767dc commit f21c018
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
49 changes: 35 additions & 14 deletions src/view/dialog/SampleSelectionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
*/

#include "SampleSelectionDialog.h"

#include <os/msg.h>

#include "../../storage/Storage.h"
#include "../../data/Song.h"
#include "../../data/Sample.h"
#include "../../Session.h"
#include "../../lib/base/iter.h"
#include "../../lib/hui/language.h"
#include "../../lib/hui/hui.h"
#include "../../lib/hui/Menu.h"
#include "../helper/SamplePreviewPlayer.h"

namespace tsunami {

Expand All @@ -30,14 +36,18 @@ SampleSelectionDialog::SampleSelectionDialog(Session *_session, hui::Panel *pare

list_id = "sample_selection_list";

menu_samples = hui::create_resource_menu("popup-menu-sample-selection", this);

fill_list();

event("import", [this] { on_import(); });
event("ok", [this] { on_ok(); });
event("cancel", [this] { on_cancel(); });
event("hui:close", [this] { on_cancel(); });
event_x(list_id, "hui:select", [this] { on_select(); });
event_x(list_id, "hui:right-button-down", [this] { on_right_click(); });
event(list_id, [this] { on_list(); });
event("sample-preview", [this] { on_preview(); });
}

SampleSelectionDialog::~SampleSelectionDialog() {
Expand All @@ -51,7 +61,7 @@ void SampleSelectionDialog::fill_list() {

set_string(list_id, _("\\- none -\\"));
set_int(list_id, 0);
foreachi(Sample *s, weak(song->samples), i) {
for (const auto& [i, s]: enumerate(weak(song->samples))) {
icon_names.add(get_sample_preview(s, session->view));
set_string(list_id, icon_names[i] + "\\" + song->get_time_str_long(s->buf->length) + "\\" + s->name);
if (s == selected)
Expand All @@ -60,24 +70,28 @@ void SampleSelectionDialog::fill_list() {
}

void SampleSelectionDialog::on_select() {
selected = get_selected();
enable("ok", selected);
}

void SampleSelectionDialog::on_right_click() {
int n = hui::get_event()->row;
menu_samples->enable("sample-preview", n >= 1);
menu_samples->open_popup(this);
}


Sample* SampleSelectionDialog::get_selected() {
int n = get_int("");
selected = nullptr;
if (n >= 1)
selected = song->samples[n - 1].get();
enable("ok", n >= 0);
return song->samples[n - 1].get();
return nullptr;
}

void SampleSelectionDialog::on_list() {
int n = get_int("");
if (n == 0) {
selected = nullptr;
_promise(selected);
request_destroy();
} else if (n >= 1) {
selected = song->samples[n - 1].get();
_promise(selected);
request_destroy();
}
selected = get_selected();
_promise(selected);
request_destroy();
}

void SampleSelectionDialog::on_import() {
Expand All @@ -89,6 +103,13 @@ void SampleSelectionDialog::on_import() {
});
}

void SampleSelectionDialog::on_preview() {
int n = hui::get_event()->row;
if (n >= 1)
SamplePreviewPlayer::play(this, session, song->samples[n - 1].get());
}


void SampleSelectionDialog::on_ok() {
_promise(selected);
request_destroy();
Expand Down
5 changes: 5 additions & 0 deletions src/view/dialog/SampleSelectionDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class SampleSelectionDialog : public hui::Dialog {

void fill_list();
void on_select();
void on_right_click();
void on_preview();
void on_list();
void on_import();
void on_ok();
void on_cancel();
Sample* get_selected();

Sample *selected;
Sample *_old;
Expand All @@ -35,6 +38,8 @@ class SampleSelectionDialog : public hui::Dialog {
Session *session;
string list_id;

owned<hui::Menu> menu_samples;

base::promise<Sample*> _promise;

static base::future<Sample*> select(Session *session, hui::Panel *parent, Sample *old);
Expand Down
35 changes: 33 additions & 2 deletions static/hui_resources.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
t7
# Number Of Resources
89
90
# Resource
Menu
menu_fake
Expand Down Expand Up @@ -6540,6 +6540,31 @@ create-from-selection
0
# Resource
Menu
popup-menu-sample-selection

0
0
3
Item
header
disabled
0
0
0
Separator


1
0
0
Item
sample-preview

2
0
0
# Resource
Menu
popup-menu-session-manager

0
Expand Down Expand Up @@ -9335,7 +9360,7 @@ default,image=hui:ok
# Language
English
# Number Of IDs
811
813
# Text
/bars-add
Add bars...
Expand Down Expand Up @@ -11062,6 +11087,12 @@ Preview
popup-menu-sample-manager/sample-scale
Scale...

popup-menu-sample-selection/header
Sample

popup-menu-sample-selection/sample-preview
Preview

popup-menu-sample/header
Sample

Expand Down

0 comments on commit f21c018

Please sign in to comment.