Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 149 additions & 1 deletion StoryCADLib/Services/API/StoryCADAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,9 @@ Applies a beat sheet template to a Problem element's structure.
This is step 3 in the beat sheets workflow.
problemGuid MUST be the GUID of a Problem element.
beatSheetName MUST be a name from GetBeatSheetNames.
This replaces any existing structure on the Problem.
This replaces the beats of the Problem.
The elements that were on the old beats still belong to the Problem, on no beat.
Call GetElementsOnNoBeat to list them.
After applying:
- Call GetProblemStructure to see the beats
- Call AssignElementToBeat to link Scenes/Problems to beats
Expand Down Expand Up @@ -1966,6 +1968,150 @@ public OperationResult<bool> MoveBeat(Guid problemGuid, int fromIndex, int toInd
return OperationResult<bool>.Success(true);
}

/// <summary>
/// Gives a Scene or a Problem to a Problem, on no beat
/// </summary>
[KernelFunction]
[Description("""
Gives a Scene or Problem element to a Problem, on no beat.
problemGuid MUST be the GUID of a Problem element. It needs no beat sheet.
elementGuid MUST be the GUID of a Scene or Problem element.
Use this when the element belongs to the Problem and its beat is not decided.
A Scene can be in more than one Problem. A Problem belongs to one Problem:
adding it moves it from where it was.
Use AssignElementToBeat to put the element on a beat later.
Use GetElementsOnNoBeat to list the elements that are on no beat.
""")]
public OperationResult<bool> AddElementToProblem(Guid problemGuid, Guid elementGuid)
{
try
{
if (CurrentModel == null)
return OperationResult<bool>.Failure("No StoryModel available");

var element = CurrentModel.StoryElements.FirstOrDefault(e => e.Uuid == problemGuid);
if (element == null || element.ElementType != StoryItemType.Problem)
return OperationResult<bool>.Failure($"Problem with GUID '{problemGuid}' not found");

outlineService.AddStructureElement(CurrentModel, (ProblemModel)element, elementGuid);
return OperationResult<bool>.Success(true);
}
catch (Exception ex)
{
return OperationResult<bool>.Failure($"Error in AddElementToProblem: {ex.Message}");
}
}

/// <summary>
/// Takes a Scene or a Problem out of a Problem, and off each beat of that Problem
/// </summary>
[KernelFunction]
[Description("""
Removes a Scene or Problem element from a Problem.
problemGuid MUST be the GUID of a Problem element.
elementGuid MUST be the GUID of an element that belongs to that Problem.
The element comes off every beat of the Problem and no longer belongs to it.
The element itself is not deleted.
To take an element off one beat and keep it in the Problem, use ClearBeatAssignment.
""")]
public OperationResult<bool> RemoveElementFromProblem(Guid problemGuid, Guid elementGuid)
{
try
{
if (CurrentModel == null)
return OperationResult<bool>.Failure("No StoryModel available");

var element = CurrentModel.StoryElements.FirstOrDefault(e => e.Uuid == problemGuid);
if (element == null || element.ElementType != StoryItemType.Problem)
return OperationResult<bool>.Failure($"Problem with GUID '{problemGuid}' not found");

outlineService.RemoveStructureElement(CurrentModel, (ProblemModel)element, elementGuid);
return OperationResult<bool>.Success(true);
}
catch (Exception ex)
{
return OperationResult<bool>.Failure($"Error in RemoveElementFromProblem: {ex.Message}");
}
}

/// <summary>
/// Gets the elements that belong to a Problem and are on none of its beats
/// </summary>
[KernelFunction]
[Description("""
Lists the Scene and Problem elements that belong to a Problem and are on none of its beats.
problemGuid MUST be the GUID of a Problem element.
Returns the GUID, Name and Type (Scene or Problem) of each element.
Elements in the trash are not listed.
Use AssignElementToBeat to put one of them on a beat.
Use GetProblemStructure to see the elements that are on beats.
""")]
public OperationResult<IEnumerable<(Guid Guid, string Name, string Type)>> GetElementsOnNoBeat(Guid problemGuid)
{
try
{
if (CurrentModel == null)
return OperationResult<IEnumerable<(Guid, string, string)>>.Failure("No StoryModel available");

var element = CurrentModel.StoryElements.FirstOrDefault(e => e.Uuid == problemGuid);
if (element == null || element.ElementType != StoryItemType.Problem)
return OperationResult<IEnumerable<(Guid, string, string)>>.Failure($"Problem with GUID '{problemGuid}' not found");

var elements = new List<(Guid, string, string)>();
foreach (var guid in outlineService.GetElementsOnNoBeat((ProblemModel)element))
{
var member = CurrentModel.StoryElements.FirstOrDefault(e => e.Uuid == guid);
if (member != null && !outlineService.IsInTrash(member))
elements.Add((member.Uuid, member.Name, member.ElementType.ToString()));
}

return OperationResult<IEnumerable<(Guid, string, string)>>.Success(elements);
}
catch (Exception ex)
{
return OperationResult<IEnumerable<(Guid, string, string)>>.Failure($"Error in GetElementsOnNoBeat: {ex.Message}");
}
}

/// <summary>
/// Moves the element of one beat to another beat of the same Problem
/// </summary>
[KernelFunction]
[Description("""
Moves the element on one beat to another beat of the same Problem.
problemGuid MUST be the GUID of a Problem element with beats.
fromBeatIndex is the 0-based index of the beat that has the element.
toBeatIndex is the 0-based index of the beat that gets the element.
The beats stay where they are; only the element moves.
If the target beat already has an element, that element stays in the Problem, on no beat.
To move a beat itself, use MoveBeat.
""")]
public OperationResult<bool> MoveBeatElement(Guid problemGuid, int fromBeatIndex, int toBeatIndex)
{
try
{
if (CurrentModel == null)
return OperationResult<bool>.Failure("No StoryModel available");

var element = CurrentModel.StoryElements.FirstOrDefault(e => e.Uuid == problemGuid);
if (element == null || element.ElementType != StoryItemType.Problem)
return OperationResult<bool>.Failure($"Problem with GUID '{problemGuid}' not found");

var problem = (ProblemModel)element;
if (fromBeatIndex < 0 || fromBeatIndex >= problem.StructureBeats.Count)
return OperationResult<bool>.Failure($"Beat index {fromBeatIndex} is out of range");
if (toBeatIndex < 0 || toBeatIndex >= problem.StructureBeats.Count)
return OperationResult<bool>.Failure($"Beat index {toBeatIndex} is out of range");

outlineService.MoveBeatElement(CurrentModel, problem, fromBeatIndex, toBeatIndex);
return OperationResult<bool>.Success(true);
}
catch (Exception ex)
{
return OperationResult<bool>.Failure($"Error in MoveBeatElement: {ex.Message}");
}
}

/// <summary>
/// Moves a story element from its current parent to a new parent node.
/// </summary>
Expand Down Expand Up @@ -2135,6 +2281,8 @@ Loads a beat sheet from a .stbeat file into a Problem.
problemGuid MUST be the GUID of a Problem element.
filePath is the full path to the .stbeat file to load.
This replaces any existing beats on the Problem.
The elements that were on the old beats still belong to the Problem, on no beat.
Call GetElementsOnNoBeat to list them.
Use SaveBeatSheet to create .stbeat files from existing Problems.
""")]
public OperationResult<bool> LoadBeatSheet(Guid problemGuid, string filePath)
Expand Down
20 changes: 20 additions & 0 deletions StoryCADLib/Services/Collaborator/Contracts/IStoryCADAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,26 @@ OperationResult<bool> AddRelationship(
/// </summary>
OperationResult<bool> MoveBeat(Guid problemGuid, int fromIndex, int toIndex);

/// <summary>
/// Gives a Scene or a Problem to a Problem, on no beat
/// </summary>
OperationResult<bool> AddElementToProblem(Guid problemGuid, Guid elementGuid);

/// <summary>
/// Takes a Scene or a Problem out of a Problem, and off each beat of that Problem
/// </summary>
OperationResult<bool> RemoveElementFromProblem(Guid problemGuid, Guid elementGuid);

/// <summary>
/// Gets the elements that belong to a Problem and are on none of its beats
/// </summary>
OperationResult<IEnumerable<(Guid Guid, string Name, string Type)>> GetElementsOnNoBeat(Guid problemGuid);

/// <summary>
/// Moves the element of one beat to another beat of the same Problem
/// </summary>
OperationResult<bool> MoveBeatElement(Guid problemGuid, int fromBeatIndex, int toBeatIndex);

/// <summary>
/// Saves a Problem's beat sheet structure to a .stbeat file
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions StoryCADLib/bindings/python/storycad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,31 @@ def move_beat(self, problem_guid: uuid.UUID, from_index: int, to_index: int) ->
"move_beat",
)

def add_element_to_problem(self, problem_guid: uuid.UUID, element_guid: uuid.UUID) -> None:
_unwrap(
self._api.AddElementToProblem(_to_net_guid(problem_guid), _to_net_guid(element_guid)),
"add_element_to_problem",
)

def remove_element_from_problem(self, problem_guid: uuid.UUID, element_guid: uuid.UUID) -> None:
_unwrap(
self._api.RemoveElementFromProblem(_to_net_guid(problem_guid), _to_net_guid(element_guid)),
"remove_element_from_problem",
)

def get_elements_on_no_beat(self, problem_guid: uuid.UUID) -> list[ElementSummary]:
raw = _unwrap(self._api.GetElementsOnNoBeat(_to_net_guid(problem_guid)), "get_elements_on_no_beat")
return [
ElementSummary(uuid=_from_net_guid(e.Item1), name=str(e.Item2), element_type=str(e.Item3))
for e in raw
]

def move_beat_element(self, problem_guid: uuid.UUID, from_beat_index: int, to_beat_index: int) -> None:
_unwrap(
self._api.MoveBeatElement(_to_net_guid(problem_guid), from_beat_index, to_beat_index),
"move_beat_element",
)

def save_beat_sheet(self, problem_guid: uuid.UUID, file_path: str) -> None:
_unwrap(
self._api.SaveBeatSheet(_to_net_guid(problem_guid), file_path),
Expand Down
48 changes: 48 additions & 0 deletions StoryCADLib/bindings/python/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,54 @@ def test_assign_and_clear_beat_assignment(outline):
assert sc.get_problem_structure(problem).beats[0].linked_element is None


def test_add_element_to_problem(outline):
sc, overview = outline
problem = sc.add_element(sc.item_type.Problem, overview.uuid, "P")
scene = sc.add_element(sc.item_type.Scene, overview.uuid, "S")

sc.add_element_to_problem(problem, scene)
assert [e.uuid for e in sc.get_elements_on_no_beat(problem)] == [scene]


def test_remove_element_from_problem(outline):
sc, overview = outline
problem = sc.add_element(sc.item_type.Problem, overview.uuid, "P")
scene = sc.add_element(sc.item_type.Scene, overview.uuid, "S")
sc.add_element_to_problem(problem, scene)

sc.remove_element_from_problem(problem, scene)
assert sc.get_elements_on_no_beat(problem) == []


def test_get_elements_on_no_beat(outline):
sc, overview = outline
problem = sc.add_element(sc.item_type.Problem, overview.uuid, "P")
sc.apply_beat_sheet_to_problem(problem, sc.get_beat_sheet_names()[0])
on_beat = sc.add_element(sc.item_type.Scene, overview.uuid, "On a beat")
on_no_beat = sc.add_element(sc.item_type.Scene, overview.uuid, "On no beat")
sc.assign_element_to_beat(problem, 0, on_beat)
sc.add_element_to_problem(problem, on_no_beat)

elements = sc.get_elements_on_no_beat(problem)
assert len(elements) == 1
assert elements[0].uuid == on_no_beat
assert elements[0].name == "On no beat"
assert elements[0].element_type == "Scene"


def test_move_beat_element(outline):
sc, overview = outline
problem = sc.add_element(sc.item_type.Problem, overview.uuid, "P")
sc.apply_beat_sheet_to_problem(problem, sc.get_beat_sheet_names()[0])
scene = sc.add_element(sc.item_type.Scene, overview.uuid, "S")
sc.assign_element_to_beat(problem, 0, scene)

sc.move_beat_element(problem, 0, 1)
beats = sc.get_problem_structure(problem).beats
assert beats[0].linked_element is None
assert beats[1].linked_element == scene


def test_save_and_load_beat_sheet(outline, tmp_path):
sc, overview = outline
problem = sc.add_element(sc.item_type.Problem, overview.uuid, "P")
Expand Down
Loading
Loading