Extract layer operation planning
This commit is contained in:
@@ -225,6 +225,19 @@ struct PlanLayerRenameArgs {
|
||||
std::string new_name;
|
||||
};
|
||||
|
||||
struct PlanLayerOperationArgs {
|
||||
std::string kind = "select";
|
||||
int layer_count = 1;
|
||||
int index = 0;
|
||||
int from_index = 0;
|
||||
int to_index = 0;
|
||||
int source_index = 0;
|
||||
std::string name = "Layer";
|
||||
float opacity = 1.0F;
|
||||
bool flag = false;
|
||||
int blend_mode = 0;
|
||||
};
|
||||
|
||||
struct SimulateAppSessionArgs {
|
||||
bool has_canvas = true;
|
||||
bool new_document = false;
|
||||
@@ -470,6 +483,34 @@ const char* document_layer_rename_action_name(pp::app::DocumentLayerRenameAction
|
||||
return "no-op-same-name";
|
||||
}
|
||||
|
||||
const char* document_layer_operation_name(pp::app::DocumentLayerOperation operation) noexcept
|
||||
{
|
||||
switch (operation) {
|
||||
case pp::app::DocumentLayerOperation::add:
|
||||
return "add";
|
||||
case pp::app::DocumentLayerOperation::duplicate:
|
||||
return "duplicate";
|
||||
case pp::app::DocumentLayerOperation::select:
|
||||
return "select";
|
||||
case pp::app::DocumentLayerOperation::reorder:
|
||||
return "reorder";
|
||||
case pp::app::DocumentLayerOperation::remove:
|
||||
return "remove";
|
||||
case pp::app::DocumentLayerOperation::set_opacity:
|
||||
return "set-opacity";
|
||||
case pp::app::DocumentLayerOperation::set_visibility:
|
||||
return "set-visibility";
|
||||
case pp::app::DocumentLayerOperation::set_alpha_lock:
|
||||
return "set-alpha-lock";
|
||||
case pp::app::DocumentLayerOperation::set_blend_mode:
|
||||
return "set-blend-mode";
|
||||
case pp::app::DocumentLayerOperation::set_highlight:
|
||||
return "set-highlight";
|
||||
}
|
||||
|
||||
return "select";
|
||||
}
|
||||
|
||||
const char* document_file_write_decision_name(pp::app::DocumentFileWriteDecision decision) noexcept
|
||||
{
|
||||
switch (decision) {
|
||||
@@ -708,6 +749,7 @@ void print_help()
|
||||
<< " plan-app-status [--doc-name NAME] [--unsaved] [--resolution N] [--resolution-index N] [--zoom N] [--history-bytes N] [--recording-running] [--encoder-available] [--encoded-frames N]\n"
|
||||
<< " plan-document-resize [--current-resolution N] [--selected-resolution-index N]\n"
|
||||
<< " plan-layer-rename --old-name NAME --new-name NAME\n"
|
||||
<< " plan-layer-operation --kind add|duplicate|select|reorder|remove|opacity|visibility|alpha-lock|blend-mode|highlight [--layer-count N] [--index N] [--from-index N] [--to-index N] [--source-index N] [--name NAME] [--opacity N] [--blend-mode N] [--enabled]\n"
|
||||
<< " plan-share-file [--path FILE]\n"
|
||||
<< " plan-picked-path [--path FILE]\n"
|
||||
<< " plan-display-file [--path FILE]\n"
|
||||
@@ -2351,6 +2393,148 @@ int plan_layer_rename(int argc, char** argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
pp::foundation::Status parse_plan_layer_operation_args(
|
||||
int argc,
|
||||
char** argv,
|
||||
PlanLayerOperationArgs& args)
|
||||
{
|
||||
for (int i = 2; i < argc; ++i) {
|
||||
const std::string_view key(argv[i]);
|
||||
if (key == "--kind" || key == "--name") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
if (key == "--kind") {
|
||||
args.kind = argv[++i];
|
||||
} else {
|
||||
args.name = argv[++i];
|
||||
}
|
||||
} else if (key == "--layer-count" || key == "--index" || key == "--from-index"
|
||||
|| key == "--to-index" || key == "--source-index" || key == "--blend-mode") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
const auto value = pp::foundation::parse_u32(argv[++i]);
|
||||
if (!value) {
|
||||
return value.status();
|
||||
}
|
||||
if (key == "--layer-count") {
|
||||
args.layer_count = static_cast<int>(value.value());
|
||||
} else if (key == "--index") {
|
||||
args.index = static_cast<int>(value.value());
|
||||
} else if (key == "--from-index") {
|
||||
args.from_index = static_cast<int>(value.value());
|
||||
} else if (key == "--to-index") {
|
||||
args.to_index = static_cast<int>(value.value());
|
||||
} else if (key == "--source-index") {
|
||||
args.source_index = static_cast<int>(value.value());
|
||||
} else {
|
||||
args.blend_mode = static_cast<int>(value.value());
|
||||
}
|
||||
} else if (key == "--opacity") {
|
||||
if (i + 1 >= argc) {
|
||||
return pp::foundation::Status::invalid_argument("missing value for option");
|
||||
}
|
||||
const auto value = parse_float_arg(argv[++i]);
|
||||
if (!value) {
|
||||
return value.status();
|
||||
}
|
||||
args.opacity = value.value();
|
||||
} else if (key == "--enabled") {
|
||||
args.flag = true;
|
||||
} else if (key == "--disabled") {
|
||||
args.flag = false;
|
||||
} else {
|
||||
return pp::foundation::Status::invalid_argument("unknown option");
|
||||
}
|
||||
}
|
||||
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<pp::app::DocumentLayerOperationPlan> make_layer_operation_plan(
|
||||
const PlanLayerOperationArgs& args)
|
||||
{
|
||||
if (args.kind == "add") {
|
||||
return pp::app::plan_document_layer_add(args.layer_count, args.index, args.name);
|
||||
}
|
||||
if (args.kind == "duplicate") {
|
||||
return pp::app::plan_document_layer_duplicate(args.layer_count, args.source_index);
|
||||
}
|
||||
if (args.kind == "select") {
|
||||
return pp::app::plan_document_layer_select(args.layer_count, args.index);
|
||||
}
|
||||
if (args.kind == "reorder") {
|
||||
return pp::app::plan_document_layer_reorder(args.layer_count, args.from_index, args.to_index);
|
||||
}
|
||||
if (args.kind == "remove") {
|
||||
return pp::app::plan_document_layer_remove(args.layer_count, args.index);
|
||||
}
|
||||
if (args.kind == "opacity") {
|
||||
return pp::app::plan_document_layer_opacity(args.layer_count, args.index, args.opacity);
|
||||
}
|
||||
if (args.kind == "visibility") {
|
||||
return pp::app::plan_document_layer_visibility(args.layer_count, args.index, args.flag);
|
||||
}
|
||||
if (args.kind == "alpha-lock") {
|
||||
return pp::app::plan_document_layer_alpha_lock(args.layer_count, args.index, args.flag);
|
||||
}
|
||||
if (args.kind == "blend-mode") {
|
||||
return pp::app::plan_document_layer_blend_mode(args.layer_count, args.index, args.blend_mode);
|
||||
}
|
||||
if (args.kind == "highlight") {
|
||||
return pp::app::plan_document_layer_highlight(args.layer_count, args.index, args.flag);
|
||||
}
|
||||
|
||||
return pp::foundation::Result<pp::app::DocumentLayerOperationPlan>::failure(
|
||||
pp::foundation::Status::invalid_argument("unknown layer operation kind"));
|
||||
}
|
||||
|
||||
int plan_layer_operation(int argc, char** argv)
|
||||
{
|
||||
PlanLayerOperationArgs args;
|
||||
const auto status = parse_plan_layer_operation_args(argc, argv, args);
|
||||
if (!status.ok()) {
|
||||
print_error("plan-layer-operation", status.message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
const auto plan = make_layer_operation_plan(args);
|
||||
if (!plan) {
|
||||
print_error("plan-layer-operation", plan.status().message);
|
||||
return 2;
|
||||
}
|
||||
|
||||
const auto& value = plan.value();
|
||||
std::cout << "{\"ok\":true,\"command\":\"plan-layer-operation\""
|
||||
<< ",\"state\":{\"kind\":\"" << json_escape(args.kind)
|
||||
<< "\",\"layerCount\":" << args.layer_count
|
||||
<< ",\"index\":" << args.index
|
||||
<< ",\"fromIndex\":" << args.from_index
|
||||
<< ",\"toIndex\":" << args.to_index
|
||||
<< ",\"sourceIndex\":" << args.source_index
|
||||
<< ",\"name\":\"" << json_escape(args.name)
|
||||
<< "\",\"opacity\":" << args.opacity
|
||||
<< ",\"flag\":" << json_bool(args.flag)
|
||||
<< ",\"blendMode\":" << args.blend_mode
|
||||
<< "},\"plan\":{\"operation\":\"" << document_layer_operation_name(value.operation)
|
||||
<< "\",\"index\":" << value.index
|
||||
<< ",\"fromIndex\":" << value.from_index
|
||||
<< ",\"toIndex\":" << value.to_index
|
||||
<< ",\"insertIndex\":" << value.insert_index
|
||||
<< ",\"sourceIndex\":" << value.source_index
|
||||
<< ",\"name\":\"" << json_escape(value.name)
|
||||
<< "\",\"opacity\":" << value.opacity
|
||||
<< ",\"flag\":" << json_bool(value.flag)
|
||||
<< ",\"blendMode\":" << value.blend_mode
|
||||
<< ",\"mutatesDocument\":" << json_bool(value.mutates_document)
|
||||
<< ",\"marksUnsaved\":" << json_bool(value.marks_unsaved)
|
||||
<< ",\"reloadsAnimationLayers\":" << json_bool(value.reloads_animation_layers)
|
||||
<< ",\"updatesTitle\":" << json_bool(value.updates_title)
|
||||
<< "}}\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
pp::foundation::Status parse_plan_share_file_args(
|
||||
int argc,
|
||||
char** argv,
|
||||
@@ -4759,6 +4943,10 @@ int main(int argc, char** argv)
|
||||
return plan_layer_rename(argc, argv);
|
||||
}
|
||||
|
||||
if (command == "plan-layer-operation") {
|
||||
return plan_layer_operation(argc, argv);
|
||||
}
|
||||
|
||||
if (command == "plan-share-file") {
|
||||
return plan_share_file(argc, argv);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user