Skip to content

Commit

Permalink
make the track view follow the editor cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
superctr committed Dec 10, 2020
1 parent 793c103 commit 049e476
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 46 deletions.
127 changes: 81 additions & 46 deletions src/track_view_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Track_View_Window::Track_View_Window(std::shared_ptr<Song_Manager> song_mgr)
, x_scroll(0.0)
, y_scroll(0.0)
, y_follow(true)
, y_editor(0)
, y_player(0)
, y_user(0.0)
, song_manager(song_mgr)
, dragging(false)
Expand All @@ -71,6 +73,7 @@ Track_View_Window::Track_View_Window(std::shared_ptr<Song_Manager> song_mgr)

void Track_View_Window::display()
{
// Draw window
std::string window_id;
window_id = "Track View##" + std::to_string(id);

Expand All @@ -79,17 +82,14 @@ void Track_View_Window::display()

ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x * 0.2);

ImGui::InputDouble("Time", &y_user, 1.0f, 1.0f, "%.2f");
if(y_player)
ImGui::InputScalar("Time", ImGuiDataType_U32, &y_player, NULL, NULL, "%d.00", ImGuiInputTextFlags_ReadOnly);
else
ImGui::InputDouble("Time", &y_user, 1.0f, 1.0f, "%.2f");
ImGui::SameLine();
ImGui::InputDouble("Scale", &y_scale_log, 0.01f, 0.1f, "%.2f");
ImGui::SameLine();
ImGui::Checkbox("Follow", &y_follow);
if(y_player)
{
ImGui::SameLine();
ImGui::Text("%d", y_player);
}
y_scale = std::pow(y_scale_log, 2);

ImGui::PopItemWidth();

Expand All @@ -98,48 +98,12 @@ void Track_View_Window::display()
if (canvas_size.x < 50.0f) canvas_size.x = 50.0f;
if (canvas_size.y < 50.0f) canvas_size.y = 50.0f;

update_position();
handle_input();

draw_list = ImGui::GetWindowDrawList();
cursor_list.clear();

// handle controls
ImGuiIO& io = ImGui::GetIO();
ImGui::InvisibleButton("canvas", canvas_size);
if(dragging)
{
if(!ImGui::IsMouseDown(0))
dragging = false;
else
y_scroll = io.MouseDelta.y;
}
if(ImGui::IsItemHovered())
{
if(!dragging && ImGui::IsMouseClicked(0))
dragging = true;
y_scroll += io.MouseWheel * 5;
}

// handle scrolling
if(std::abs(y_scroll) >= inertia_threshold)
{
y_user -= y_scroll / y_scale;
y_scroll *= inertia_acceleration;
if(y_user < y_min)
y_user = y_min;
}

// Get player position
auto player = song_manager->get_player();
if(player != nullptr && !player->get_finished())
y_player = player->get_driver()->get_player_ticks();
else
y_player = 0;

// Set scroll position
if(y_follow && y_player)
y_pos = y_player - (canvas_size.y / 2.0) / y_scale;
else
y_pos = y_user - track_header_height / y_scale;

// draw background
draw_list->AddRectFilled(
canvas_pos,
Expand Down Expand Up @@ -174,6 +138,77 @@ void Track_View_Window::display()
ImGui::End();
}

//! Update position in follow mode
void Track_View_Window::update_position()
{
y_scale = std::pow(y_scale_log, 2);

// Get player position
auto player = song_manager->get_player();
if(player != nullptr && !player->get_finished())
y_player = player->get_driver()->get_player_ticks();
else
y_player = 0;

if(y_follow)
{
if(y_player)
{
// Set scroll position to player position in follow mode
y_pos = y_player - (canvas_size.y / 2.0) / y_scale;
y_scroll = 0;
dragging = false;
}
else if ( song_manager->get_editor_position().line >= 0
&& y_editor != song_manager->get_song_pos_at_cursor()
&& !song_manager->get_compile_in_progress())
{
// Set scroll position to editor position in follow mode
y_editor = song_manager->get_song_pos_at_cursor();
y_user = y_editor - (canvas_size.y / 2.0) / y_scale;
if(y_user < 0)
y_user = 0;
y_scroll = 0;
dragging = false;
}
}
}

//! Handle user input
void Track_View_Window::handle_input()
{
// handle controls
ImGuiIO& io = ImGui::GetIO();
ImGui::InvisibleButton("canvas", canvas_size);

if(!(y_follow && y_player))
{
if(dragging)
{
if(!ImGui::IsMouseDown(0))
dragging = false;
else
y_scroll = io.MouseDelta.y;
}
if(ImGui::IsItemHovered())
{
if(!dragging && ImGui::IsMouseClicked(0))
dragging = true;
y_scroll += io.MouseWheel * 5;
}

// handle scrolling
if(std::abs(y_scroll) >= inertia_threshold)
{
y_user -= y_scroll / y_scale;
y_scroll *= inertia_acceleration;
if(y_user < y_min)
y_user = y_min;
}
y_pos = y_user - track_header_height / y_scale;
}
}


//! Draw measure and beat ruler
void Track_View_Window::draw_ruler()
Expand Down
5 changes: 5 additions & 0 deletions src/track_view_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Track_View_Window : public Window

std::string get_note_name(uint16_t note) const;

void update_position();
void handle_input();

double x_pos;
double y_pos;

Expand All @@ -59,6 +62,8 @@ class Track_View_Window : public Window
double y_scroll;

bool y_follow; // If set, follow song position.

uint32_t y_editor; // last editor Y position
uint32_t y_player; // Player Y position
double y_user; // User Y position

Expand Down

0 comments on commit 049e476

Please sign in to comment.