Skip to content

Commit

Permalink
Add bxt_tas_studio_select_{next,prev}
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Jul 27, 2023
1 parent 026e5e0 commit 9d9602b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/modules/tas_studio/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,48 @@ impl Editor {
Ok(())
}

/// Selects the given frame bulk.
pub fn select_bulk(&mut self, bulk_idx: usize) -> ManualOpResult<()> {
if self.is_any_adjustment_active() {
return Err(ManualOpError::CannotDoDuringAdjustment);
}

if self.in_camera_editor {
return Err(ManualOpError::CannotDoInCameraEditor);
}

let bulk_count = self.script().frame_bulks().count();

if bulk_idx >= bulk_count {
return Err(ManualOpError::UserError(
"there's no frame bulk with this index".to_owned(),
));
}

self.selected_bulk_idx = Some(bulk_idx);
Ok(())
}

/// Selects the next frame bulk.
pub fn select_next(&mut self) -> ManualOpResult<()> {
let bulk_idx = if let Some(bulk_idx) = self.selected_bulk_idx {
(bulk_idx + 1).min(self.script().frame_bulks().count().saturating_sub(1))
} else {
0
};
self.select_bulk(bulk_idx)
}

/// Selects the previous frame bulk.
pub fn select_prev(&mut self) -> ManualOpResult<()> {
let bulk_idx = if let Some(bulk_idx) = self.selected_bulk_idx {
bulk_idx.saturating_sub(1)
} else {
self.script().frame_bulks().count().saturating_sub(1)
};
self.select_bulk(bulk_idx)
}

/// Deletes the selected line, if any.
pub fn delete_selected(&mut self) -> ManualOpResult<()> {
// Don't delete during active adjustments because they store the frame bulk index.
Expand Down
52 changes: 52 additions & 0 deletions src/modules/tas_studio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Module for TasStudio {
&BXT_TAS_STUDIO_SET_COMMANDS,
&BXT_TAS_STUDIO_UNSET_PITCH,
&BXT_TAS_STUDIO_UNSET_YAW,
&BXT_TAS_STUDIO_SELECT_NEXT,
&BXT_TAS_STUDIO_SELECT_PREV,
&BXT_TAS_STUDIO_SPLIT,
&BXT_TAS_STUDIO_DELETE,
&BXT_TAS_STUDIO_DELETE_LAST,
Expand Down Expand Up @@ -632,6 +634,56 @@ fn set_commands(marker: MainThreadMarker, commands: String) {
}
}

static BXT_TAS_STUDIO_SELECT_NEXT: Command = Command::new(
b"bxt_tas_studio_select_next\0",
handler!(
"bxt_tas_studio_select_next
Selects the next frame bulk.",
select_next as fn(_)
),
);

fn select_next(marker: MainThreadMarker) {
let mut state = STATE.borrow_mut(marker);
let State::Editing { editor, .. } = &mut *state else {
return;
};

if let Err(err) = editor.select_next() {
con_print(marker, &format!("Error selecting frame bulk: {err}\n"));
if err.is_internal() {
error!("error selecting frame bulk: {err:?}\n");
*state = State::Idle;
}
}
}

static BXT_TAS_STUDIO_SELECT_PREV: Command = Command::new(
b"bxt_tas_studio_select_prev\0",
handler!(
"bxt_tas_studio_select_prev
Selects the previous frame bulk.",
select_prev as fn(_)
),
);

fn select_prev(marker: MainThreadMarker) {
let mut state = STATE.borrow_mut(marker);
let State::Editing { editor, .. } = &mut *state else {
return;
};

if let Err(err) = editor.select_prev() {
con_print(marker, &format!("Error selecting frame bulk: {err}\n"));
if err.is_internal() {
error!("error selecting frame bulk: {err:?}\n");
*state = State::Idle;
}
}
}

static PLUS_BXT_TAS_STUDIO_INSERT_CAMERA_LINE: Command = Command::new(
b"+bxt_tas_studio_insert_camera_line\0",
handler!(
Expand Down

0 comments on commit 9d9602b

Please sign in to comment.