Skip to content

Commit

Permalink
[Misc] Refactor & remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
native-m committed Oct 27, 2024
1 parent c648679 commit fa7832e
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 134 deletions.
2 changes: 1 addition & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ void app_render_control_bar() {
bool new_project = false;
bool open_project = false;
bool save_project = false;
static float tempo = 150.0;

ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_TitleBg));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 4.0f));
Expand Down Expand Up @@ -359,6 +358,7 @@ void app_render_control_bar() {
controls::song_position();
set_current_font(FontType::Nornal);
ImGui::SameLine(0.0f, 4.0f);
float tempo = (float)g_engine.get_bpm();
if (ImGui::DragFloat("##TEMPO_DRAG", &tempo, 1.0f, 0.0f, 0.0f, "%.2f BPM", ImGuiSliderFlags_Vertical)) {
g_engine.set_bpm((double)tempo);
}
Expand Down
6 changes: 3 additions & 3 deletions src/engine/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ ProjectFileResult read_project_file(const std::filesystem::path& path, Engine& e
if (file.read_array(sample_path_str) < 4)
return ProjectFileResult::ErrCorruptedFile;
std::filesystem::path sample_path(sample_path_str);
// Check if this file exists. If not, do plan B or C.
// Plan B: Scan the file in project relative path.
// Plan C: Scan the file in user's directory path.
// Check if this file exists. If not, do B or C.
// B: Scan the file in project relative path.
// C: Scan the file in user's directory path.
if (!std::filesystem::is_regular_file(sample_path)) {
std::filesystem::path filename = sample_path.filename();
bool found = false;
Expand Down
10 changes: 5 additions & 5 deletions src/ui/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void song_position() {

template <typename T>
static bool slider2_ranged(const SliderProperties& properties, const char* str_id, const ImVec2& size,
const ImColor& color, T* value, const NonLinearRange& db_range, T default_value = 0.0f,
const ImColor& color, T* value, const NonLinearRange& range, T default_value = 0.0f,
const char* format = "%.2f") {
ImGuiWindow* window = GImGui->CurrentWindow;
ImVec2 cursor_pos = window->DC.CursorPos;
Expand All @@ -111,7 +111,7 @@ static bool slider2_ranged(const SliderProperties& properties, const char* str_i
grab_size.y = diameter;
}

T normalized_value = (T)db_range.plain_to_normalized((float)*value);
T normalized_value = (T)range.plain_to_normalized((float)*value);
ImGuiContext& g = *ImGui::GetCurrentContext();
float scroll_height = size.y - grab_size.y;
float inv_scroll_height = 1.0f / scroll_height;
Expand All @@ -124,15 +124,15 @@ static bool slider2_ranged(const SliderProperties& properties, const char* str_i

float inv_normalized_default_value = 0.0f;
if (held || properties.with_default_value_tick)
inv_normalized_default_value = 1.0f - (float)db_range.plain_to_normalized(default_value);
inv_normalized_default_value = 1.0f - (float)range.plain_to_normalized(default_value);

if (held) {
float current_grab_pos = math::round(mouse_pos.y - cursor_pos.y - g.SliderGrabClickOffset);
float default_value_grab_pos = math::round(inv_normalized_default_value * scroll_height);
float val = !math::near_equal(current_grab_pos, default_value_grab_pos) ? current_grab_pos * inv_scroll_height
: inv_normalized_default_value;
normalized_value = std::clamp(1.0f - val, 0.0f, 1.0f);
*value = (T)db_range.normalized_to_plain(normalized_value);
*value = (T)range.normalized_to_plain(normalized_value);
}

float half_grab_size_y = grab_size.y * 0.5f;
Expand Down Expand Up @@ -208,7 +208,7 @@ bool param_drag_db(const char* str_id, float* value, float speed, float min_db,
return ImGui::DragFloat(str_id, value, 0.1f, min_db, max_db, str_value, flags);
}

bool param_drag_pan(const char* str_id, float* value, float speed, ImGuiSliderFlags flags) {
bool param_drag_panning(const char* str_id, float* value, float speed, ImGuiSliderFlags flags) {
float pan = *value * 100.0f;
char pan_value[16] {};
if (pan < 0) {
Expand Down
93 changes: 1 addition & 92 deletions src/ui/controls.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,102 +124,11 @@ static bool resizable_horizontal_separator(T id, float* size, float default_size
return is_separator_active;
}

template <typename T>
static bool slider2(const SliderProperties& properties, const char* str_id, const ImVec2& size,
const ImColor& color, T* value, T min, T max, T default_value = 1.0f,
const char* format = "%.2f") {
ImGuiWindow* window = GImGui->CurrentWindow;
ImVec2 cursor_pos = window->DC.CursorPos;
// ImVec2 padded_size(size.x - properties.extra_padding.x, size.y - properties.extra_padding.y);
// cursor_pos.x += properties.extra_padding.x;
// cursor_pos.y += properties.extra_padding.y;

ImRect bb(ImVec2(cursor_pos.x, cursor_pos.y), ImVec2(cursor_pos.x, cursor_pos.y) + size);
ImGuiID id = ImGui::GetID(str_id);

ImGui::ItemSize(bb);
if (!ImGui::ItemAdd(bb, id))
return false;

bool hovered, held;
bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None);
bool dragging = held && ImGui::IsMouseDragging(ImGuiMouseButton_Left, 0.0f);
ImGuiContext& g = *ImGui::GetCurrentContext();
const ImVec2& mouse_pos = g.IO.MousePos;
ImU32 frame_col = ImGui::GetColorU32(ImGui::GetStyleColorVec4(ImGuiCol_Border));
ImU32 grab_col = ImGui::GetColorU32(color.Value);
T range = max - min;
T normalized_value = (*value - min) / range;
float frame_width = std::max(properties.frame_width, 3.0f);
ImVec2 grab_size;

if (properties.grab_shape == SliderGrabShape::Rectangle) {
grab_size = properties.grab_size;
grab_size.x = math::min(properties.grab_size.x, size.x);
} else {
float diameter = math::min(properties.grab_size.x, properties.grab_size.y);
grab_size.x = diameter;
grab_size.y = diameter;
}

float scroll_height = size.y - grab_size.y;
float inv_scroll_height = 1.0f / scroll_height;
// Log::debug("{}", normalized_value);

if (ImGui::IsItemActivated())
g.SliderGrabClickOffset =
mouse_pos.y - ((1.0f - (float)normalized_value) * scroll_height + cursor_pos.y);

if (held) {
float val = (mouse_pos.y - cursor_pos.y - g.SliderGrabClickOffset) * inv_scroll_height;
normalized_value = std::clamp(1.0f - val, 0.0f, 1.0f);
*value = normalized_value * range + min;
// ImGui::SetNextWindowPos(ImVec2())
ImGui::BeginTooltip();
ImGui::Text(format, *value);
ImGui::EndTooltip();
}

float grab_pos = (1.0f - (float)normalized_value) * scroll_height;
ImDrawList* draw_list = ImGui::GetWindowDrawList();
float center_x = cursor_pos.x + size.x * 0.5f;
ImVec2 frame_rect_min(center_x - frame_width * 0.5f, cursor_pos.y + grab_size.y * 0.5f);
ImVec2 frame_rect_max(frame_rect_min.x + frame_width, frame_rect_min.y + scroll_height);

// draw_list->AddRect(cursor_pos, bb.Max, frame_col);
draw_list->AddRectFilled(frame_rect_min, frame_rect_max, frame_col);

if (properties.grab_shape == SliderGrabShape::Rectangle) {
ImVec2 grab_rect_min(center_x - grab_size.x * 0.5f, cursor_pos.y + math::round(grab_pos));
ImVec2 grab_rect_max(grab_rect_min.x + grab_size.x, grab_rect_min.y + grab_size.y);
draw_list->AddRectFilled(grab_rect_min, grab_rect_max, grab_col, properties.grab_roundness);
draw_list->AddLine(
ImVec2(grab_rect_min.x + 2.0f, grab_rect_min.y + grab_size.y * 0.5f),
ImVec2(grab_rect_min.x + grab_size.x - 2.0f, grab_rect_min.y + grab_size.y * 0.5f),
0xFFFFFFFF, 3.0f);
} else {
float radius1 = grab_size.x * 0.5f;
float radius2 = grab_size.x * 0.25f;
float pos_y = math::round(grab_pos) + radius1;
draw_list->AddCircleFilled(ImVec2(center_x, cursor_pos.y + pos_y), radius1, grab_col);
draw_list->AddCircleFilled(ImVec2(center_x, cursor_pos.y + pos_y), radius2, 0xFFFFFFFF);
}

if (dragging) {
ImVec2 delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left, 0.0f);
ImGui::ResetMouseDragDelta();
if (delta.y != 0.0f)
return true;
}

return false;
}

void song_position();
bool param_drag_db(const char* str_id, float* value, float speed = 0.1f, float min_db = -72.0f,
float max_db = 6.0f, const char* format = "%.2fdB",
ImGuiSliderFlags flags = ImGuiSliderFlags_Vertical);
bool param_drag_pan(const char* str_id, float* value, float speed = 1.0f,
bool param_drag_panning(const char* str_id, float* value, float speed = 1.0f,
ImGuiSliderFlags flags = ImGuiSliderFlags_Vertical);
bool param_slider_db(const SliderProperties& properties, const char* str_id, const ImVec2& size,
const ImColor& color, float* value, const NonLinearRange& db_range,
Expand Down
35 changes: 6 additions & 29 deletions src/ui/timeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ void GuiTimeline::render_track_controls() {
if (ImGui::Button("M")) {
track->set_mute(!mute);
}

ImGui::SameLine(0.0f, 2.0f);
if (ImGui::Button("S")) {
g_engine.solo_track(i);
}

ImGui::SameLine(0.0f, 2.0f);
ImVec2 pos = ImGui::GetCursorPos();
ImGui::SetNextItemWidth(free_region.x - pos.x);
Expand All @@ -431,7 +431,7 @@ void GuiTimeline::render_track_controls() {
if (height > item_height * 2.5f) {
if (height > item_height * 3.5f) {
float pan = track->ui_parameter_state.pan;
if (controls::param_drag_pan("Pan", &pan)) {
if (controls::param_drag_panning("Pan", &pan)) {
track->set_pan(pan);
}
}
Expand Down Expand Up @@ -623,7 +623,7 @@ void GuiTimeline::render_track_lanes() {
}

// The timeline is actually just a very large button that cover almost
// entire screen.
// entire window.
ImGui::InvisibleButton("##timeline", ImVec2(timeline_width, std::max(timeline_area.y, area_size.y + vscroll)));

double view_scale = calc_view_scale();
Expand Down Expand Up @@ -765,6 +765,7 @@ void GuiTimeline::render_track_lanes() {
.min_time = target_sel_range.min,
.max_time = target_sel_range.max,
});
recalculate_timeline_length();
redraw = true;
}
}
Expand Down Expand Up @@ -944,7 +945,7 @@ void GuiTimeline::render_track_lanes() {
if (hovering_current_track && edit_action == TimelineEditAction::None && !holding_ctrl) {
static constexpr float handle_offset = 4.0f;
ImRect clip_rect(min_bb, max_bb);
// Sizing handle hitboxes
// Hitboxes for sizing handle
ImRect left_handle(min_pos_x_in_pixel, track_pos_y, min_pos_x_in_pixel + handle_offset, max_bb.y);
ImRect right_handle(max_pos_x_in_pixel - handle_offset, track_pos_y, max_pos_x_in_pixel, max_bb.y);

Expand Down Expand Up @@ -1324,30 +1325,6 @@ void GuiTimeline::recalculate_timeline_length() {
}
}

void GuiTimeline::delete_selected_range() {
double beat_duration = g_engine.beat_duration.load(std::memory_order_relaxed);
uint32_t first_track = target_sel_range.first_track;
uint32_t last_track = target_sel_range.last_track;
g_engine.edit_lock();

if (last_track < first_track) {
std::swap(first_track, last_track);
}

for (uint32_t i = first_track; i <= last_track; i++) {
Track* track = g_engine.tracks[i];
auto query_result = track->query_clip_by_range(target_sel_range.min, target_sel_range.max);
if (!query_result) {
continue;
}
Log::debug("first: {} {}, last: {} {}", query_result->first, query_result->first_offset, query_result->last,
query_result->last_offset);
g_engine.reserve_track_region(track, query_result->first, query_result->last, target_sel_range.min,
target_sel_range.max, false, nullptr);
}
g_engine.edit_unlock();
}

GuiTimeline g_timeline;

} // namespace wb
2 changes: 0 additions & 2 deletions src/ui/timeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ struct GuiTimeline : public TimelineBase {
void finish_edit_action();
void recalculate_timeline_length();

void delete_selected_range();

inline void redraw_screen() { force_redraw = true; }
};

Expand Down
2 changes: 0 additions & 2 deletions src/ui/timeline_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ void TimelineBase::render_horizontal_scrollbar() {
if (resizing_lhs_scroll_grab) {
auto drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left, 1.0f);
min_hscroll = math::clamp(last_hscroll + drag_delta.x / scroll_btn_max_length, 0.0, max_hscroll - min_space);
Log::debug("{}", min_hscroll, last_hscroll);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
} else if (resizing_rhs_scroll_grab) {
auto drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Left, 1.0f);
max_hscroll = math::max(last_hscroll + drag_delta.x / scroll_btn_max_length, min_hscroll + min_space);
Log::debug("{} {}", max_hscroll, last_hscroll);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
} else if (grabbing_scroll) {
ImVec2 drag_delta = ImGui::GetMouseDragDelta();
Expand Down

0 comments on commit fa7832e

Please sign in to comment.