Skip to content

Commit

Permalink
feat: Added keybinds for setting time and carousel movement
Browse files Browse the repository at this point in the history
  • Loading branch information
gigareggie committed Jan 6, 2025
1 parent 8ba8a72 commit cf4c6c1
Showing 1 changed file with 59 additions and 32 deletions.
91 changes: 59 additions & 32 deletions pyttml/screens/sync.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from components.carousel import Carousel, ScrollDirection, VerticalScroller
from components.playerbox import PlayerBox
from components.sidebar import Sidebar
from ttml import Lyrics, process_lyrics
from screens.edit import EditScreen

from ttml import Lyrics

from textual import events
from textual.app import ComposeResult
Expand All @@ -15,13 +13,32 @@
class SyncScreen(Screen):
lyrics_saved_state: Lyrics = None
active_line_index: int = 0
action_mapping:dict = {}

BINDINGS = [
("a", "move_carousel_backward()", "Moves carousel backward"),
("d", "move_carousel_forward()", "Moves carousel forward"),
("f", "set_start_time()", "Sets the start time of active word as current timestamp"),
("g", "set_end_move()", "Sets the end time of the active word and start time of the next word as current timestamp"),
("h", "set_end_time()", "Sets the end time of the active word as current timestamp"),
]

def __init__(self):
self.action_mapping = {
"next_word": self.action_move_carousel_forward,
"prev_word": self.action_move_carousel_backward,
"set_start_time": self.action_set_start_time,
"set_end_time": self.action_set_end_time,
"set_end_move": self.action_set_end_move
}
super().__init__()

def compose(self) -> ComposeResult:
yield Static("No Lyrics Loaded", id="lyrics_label")
yield Sidebar(id="sidebar")
yield (Horizontal(
Button("←", id="prev_word_button"),
Button("→", id="next_word_button"),
Button("←", id="prev_word"),
Button("→", id="next_word"),
Button("F", id="set_start_time",
tooltip="Set timestamp as the startime of the word"),
Button("G", id="set_end_move",
Expand All @@ -40,36 +57,46 @@ def update_scroller(self) -> None:
vertical_scroller.scroll(ScrollDirection.backward)
elif vertical_scroller.active_line_index < carousel_item_line_index:
vertical_scroller.scroll(ScrollDirection.forward)

def action_move_carousel_forward(self):
self.query_one(Carousel).move(ScrollDirection.forward)
self.update_scroller()

def action_move_carousel_backward(self):
self.query_one(Carousel).move(ScrollDirection.backward)
self.update_scroller()

def action_set_start_time(self):
carousel: Carousel = self.query_one(Carousel)

def on_button_pressed(self, event: Button.Pressed):
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
active_item_index = carousel._nodes.index(carousel.active_item)
self.app.CURR_LYRICS.element_map[active_word_index][0].start_time = timestamp
carousel._nodes[active_item_index].update()

def action_set_end_time(self):
carousel: Carousel = self.query_one(Carousel)

if event.button.id == "next_word_button":
carousel.move(ScrollDirection.forward)
self.update_scroller()

elif event.button.id == "prev_word_button":
carousel.move(ScrollDirection.backward)
self.update_scroller()

elif event.button.id in ["set_start_time", "set_end_time", "set_end_move"]:
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
active_item_index = carousel._nodes.index(carousel.active_item)

if event.button.id == "set_start_time":
self.app.CURR_LYRICS.element_map[active_word_index][0].start_time = timestamp

elif event.button.id in ["set_end_time", "set_end_move"]:
self.app.CURR_LYRICS.element_map[active_word_index][0].end_time = timestamp

if event.button.id == "set_end_move":
carousel.move(ScrollDirection.forward)
self.update_scroller()
self.app.CURR_LYRICS.element_map[active_word_index + 1][0].start_time = timestamp + 0.02
carousel._nodes[active_item_index + 1].update()

carousel._nodes[active_item_index].update()
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
active_item_index = carousel._nodes.index(carousel.active_item)
self.app.CURR_LYRICS.element_map[active_word_index][0].end_time = timestamp
carousel._nodes[active_item_index].update()

def action_set_end_move(self):
carousel: Carousel = self.query_one(Carousel)
self.action_set_end_time()
carousel.move(ScrollDirection.forward)
self.update_scroller()
self.action_set_start_time()

def on_button_pressed(self, event: Button.Pressed):

try:
self.action_mapping[event.button.id]()
except KeyError:
pass

def on_screen_resume(self, event: events.ScreenResume):
label: Static = self.query_one("#lyrics_label", Static)
Expand Down

0 comments on commit cf4c6c1

Please sign in to comment.