yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
planning_editor.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <filesystem>
5#include <memory>
6#include <utility>
7
8#include "absl/strings/str_format.h"
9#include "absl/strings/str_join.h"
10#include "absl/strings/str_split.h"
11#include "absl/strings/strip.h"
12#include "absl/time/clock.h"
13#include "absl/time/time.h"
19#include "app/gui/core/icons.h"
21#include "core/project.h"
23#include "imgui/imgui.h"
24#include "imgui/misc/cpp/imgui_stdlib.h"
25
26namespace yaze::editor {
27namespace {
28
29constexpr const char* kCategory = "Planning";
30constexpr const char* kMilestoneStatuses[] = {"planned", "active", "done"};
31constexpr const char* kItemStatuses[] = {"implemented", "prototype", "decided",
32 "proposed", "open"};
33constexpr const char* kDecisionStatuses[] = {"open", "decided", "dropped"};
34constexpr const char* kStepKinds[] = {"story", "errand", "dungeon",
35 "shrine", "boss", "optional"};
36
38 public:
39 PlanningPanel(std::string id, std::string name, const char* icon,
40 int priority, std::function<void()> draw)
41 : id_(std::move(id)),
42 name_(std::move(name)),
43 icon_(icon),
44 priority_(priority),
45 draw_(std::move(draw)) {}
46
47 std::string GetId() const override { return id_; }
48 std::string GetDisplayName() const override { return name_; }
49 std::string GetIcon() const override { return icon_; }
50 std::string GetEditorCategory() const override { return kCategory; }
52 return WindowPresentationPolicy::CoreWorkspace();
53 }
54 int GetPriority() const override { return priority_; }
55 bool IsVisibleByDefault() const override { return true; }
56 float GetPreferredWidth() const override { return 640.0f; }
57 void Draw(bool* /*p_open*/) override {
58 if (draw_) {
59 draw_();
60 }
61 }
62
63 private:
64 std::string id_;
65 std::string name_;
66 std::string icon_;
68 std::function<void()> draw_;
69};
70
71template <size_t N>
72bool StatusCombo(const char* label, std::string* value,
73 const char* const (&options)[N]) {
74 bool changed = false;
75 if (ImGui::BeginCombo(label, value->empty() ? "-" : value->c_str())) {
76 for (const char* option : options) {
77 const bool selected = *value == option;
78 if (ImGui::Selectable(option, selected)) {
79 *value = option;
80 changed = true;
81 }
82 }
83 ImGui::EndCombo();
84 }
85 return changed;
86}
87
88ImVec4 StatusColor(const std::string& status) {
89 if (status == "implemented" || status == "done" || status == "decided") {
90 return gui::GetSuccessColor();
91 }
92 if (status == "prototype" || status == "active") {
93 return gui::GetInfoColor();
94 }
95 if (status == "open") {
96 return gui::GetWarningColor();
97 }
98 return gui::GetDisabledColor();
99}
100
101std::string Today() {
102 return absl::FormatTime("%Y-%m-%d", absl::Now(), absl::LocalTimeZone());
103}
104
105std::string DaysUntil(const std::string& date) {
106 absl::Time due;
107 std::string error;
108 if (date.empty() ||
109 !absl::ParseTime("%Y-%m-%d", date, absl::LocalTimeZone(), &due, &error)) {
110 return "";
111 }
112 const int64_t days = absl::ToInt64Hours(due - absl::Now()) / 24;
113 if (days < 0) {
114 return absl::StrFormat("%d days past", -days);
115 }
116 return absl::StrFormat("%d days left", days);
117}
118
119std::string JoinTargets(const std::vector<std::string>& targets) {
120 return absl::StrJoin(targets, ", ");
121}
122
123std::vector<std::string> SplitTargets(const std::string& text) {
124 std::vector<std::string> out;
125 for (absl::string_view part :
126 absl::StrSplit(text, ',', absl::SkipWhitespace())) {
127 out.emplace_back(absl::StripAsciiWhitespace(part));
128 }
129 return out;
130}
131
132std::string StepLabel(const core::PlanRouteStep& step) {
133 return step.code.empty() ? step.title : step.code;
134}
135
136std::string JsonString(const core::PlanJson& obj, const char* key,
137 const char* fallback) {
138 auto it = obj.find(key);
139 return it != obj.end() && it->is_string() ? it->get<std::string>()
140 : std::string(fallback);
141}
142
143constexpr const char* kRemoveConfirmPopupId = "Remove entry##confirm";
144
145} // namespace
146
147absl::Status ValidateStepIdEdit(const core::ProjectPlan& plan,
148 std::string_view current_id,
149 std::string_view proposed_id) {
150 const std::string proposed(proposed_id);
151 if (absl::StripAsciiWhitespace(proposed).empty()) {
152 return absl::InvalidArgumentError("Step Id cannot be empty");
153 }
154 if (proposed_id != current_id && plan.FindStep(proposed_id) >= 0) {
155 return absl::AlreadyExistsError(
156 absl::StrFormat("Another step already uses Id \"%s\"", proposed));
157 }
158 return absl::OkStatus();
159}
160
163 return;
164 }
165 auto* window_manager = dependencies_.window_manager;
166 window_manager->RegisterWindowContent(std::make_unique<PlanningPanel>(
167 "planning.overview", "Plan Overview", ICON_MD_DASHBOARD, 10,
168 [this]() { DrawOverview(); }));
169 window_manager->RegisterWindowContent(std::make_unique<PlanningPanel>(
170 "planning.route", "Route", ICON_MD_ROUTE, 20, [this]() { DrawRoute(); }));
171 panels_registered_ = true;
172}
173
175 return SyncWithProject();
176}
177
178absl::Status PlanningEditor::Save() {
179 if (!plan_ || !dirty_ || plan_path_.empty()) {
180 return absl::OkStatus();
181 }
182 if (!project() || project()->filepath != project_path_ ||
183 project()->PlanningFilePath() != plan_path_) {
184 return absl::FailedPreconditionError(
185 "Restore the plan's project before saving its edits");
186 }
187 std::string expected_source = loaded_source_;
188 std::string disk_source;
189 if (core::ProjectPlan::LoadFromFile(plan_path_, &disk_source).ok() &&
190 disk_source != loaded_source_) {
192 disk_source)) {
194 "The plan file changed outside agent status since it was loaded. "
195 "Your edits are kept; copy them, then Reload to see the file.";
196 return absl::AbortedError(save_error_);
197 }
198 expected_source = std::move(disk_source);
199 }
200 absl::Status status = plan_->SaveToFile(plan_path_, expected_source);
201 if (!status.ok()) {
202 save_error_ = std::string(status.message());
203 return status;
204 }
205 save_error_.clear();
206 loaded_source_ = plan_->Serialize();
207 dirty_ = false;
208 return absl::OkStatus();
209}
210
212 const std::string path = project() ? project()->PlanningFilePath() : "";
213 const std::string project_path = project() ? project()->filepath : "";
214 if (path == plan_path_ && project_path == project_path_) {
215 return absl::OkStatus();
216 }
217 if (dirty_) {
218 save_error_ = "Save or discard plan edits before replacing their project";
219 return absl::FailedPreconditionError(save_error_);
220 }
221 plan_path_ = path;
222 project_path_ = project_path;
223 loaded_source_.clear();
224 plan_.reset();
225 load_error_.clear();
226 dirty_ = false;
227 selected_step_ = -1;
228 selected_item_ = -1;
229 if (!plan_path_.empty()) {
230 return Reload();
231 }
232 return absl::OkStatus();
233}
234
235absl::Status PlanningEditor::Reload(bool discard_changes) {
236 if (dirty_ && !discard_changes) {
237 save_error_ = "Save or explicitly discard plan edits before reloading";
238 return absl::FailedPreconditionError(save_error_);
239 }
240 std::string source;
241 auto loaded = core::ProjectPlan::LoadFromFile(plan_path_, &source);
242 if (!loaded.ok()) {
243 load_error_ = std::string(loaded.status().message());
244 return loaded.status();
245 }
246 plan_ = std::move(*loaded);
247 loaded_source_ = std::move(source);
248 load_error_.clear();
249 save_error_.clear();
250 step_id_edit_for_.clear();
251 step_id_error_.clear();
252 dirty_ = false;
253 if (selected_step_ >= static_cast<int>(plan_->route_steps.size())) {
254 selected_step_ = -1;
255 }
256 if (selected_item_ >= static_cast<int>(plan_->route_items.size())) {
257 selected_item_ = -1;
258 }
259 return absl::OkStatus();
260}
261
263 dirty_ = false;
264 plan_.reset();
265 plan_path_.clear();
266 project_path_.clear();
267 loaded_source_.clear();
268 save_error_.clear();
269 step_id_edit_for_.clear();
270 step_id_error_.clear();
271 (void)SyncWithProject();
272}
273
275 if (!project() || project()->filepath.empty()) {
276 ImGui::TextWrapped(
277 "Open a .yaze project to plan it. The plan is a JSON file named by "
278 "the project's [planning] section.");
279 return;
280 }
281 if (!load_error_.empty()) {
283 if (ImGui::Button(ICON_MD_REFRESH " Retry")) {
284 (void)Reload();
285 }
286 ImGui::Separator();
287 }
288 ImGui::TextWrapped(
289 load_error_.empty()
290 ? "This project has no plan file. Create one, or set [planning] "
291 "file= in the .yaze file."
292 : "The linked plan could not be loaded. Retry after repairing it, or "
293 "create a plan at a new path. Existing files are never replaced.");
294 if (new_plan_path_.empty()) {
295 const std::filesystem::path project_path(project()->filepath);
296 new_plan_path_ = (project_path.parent_path() /
297 (project_path.stem().string() + ".plan.json"))
298 .string();
299 }
300 ImGui::SetNextItemWidth(-1);
301 ImGui::InputText("##new_plan_path", &new_plan_path_);
302 if (ImGui::Button(ICON_MD_ADD " Create plan and link it to the project")) {
303 absl::Status status =
305 if (!status.ok()) {
306 load_error_ = std::string(status.message());
307 } else {
308 (void)SyncWithProject();
309 }
310 }
311}
312
314 (void)SyncWithProject();
315 if (!plan_) {
316 DrawNoPlan();
317 return false;
318 }
319 ImGui::TextDisabled("%s", plan_path_.c_str());
320 ImGui::SameLine();
321 ImGui::BeginDisabled(!dirty_);
322 if (ImGui::SmallButton(ICON_MD_SAVE " Save plan")) {
323 absl::Status status = Save();
326 status.ok() ? "Plan saved" : std::string(status.message()),
327 status.ok() ? ToastType::kSuccess : ToastType::kError);
328 }
329 }
330 ImGui::EndDisabled();
331 ImGui::SameLine();
332 if (ImGui::SmallButton(ICON_MD_REFRESH " Reload")) {
333 if (dirty_) {
334 ImGui::OpenPopup("Discard plan edits?");
335 } else {
336 (void)Reload();
337 }
338 }
339 if (ImGui::BeginPopupModal("Discard plan edits?", nullptr,
340 ImGuiWindowFlags_AlwaysAutoResize)) {
341 ImGui::TextUnformatted("Reload discards the unsaved plan edits.");
342 if (ImGui::Button("Discard and reload")) {
343 (void)Reload(true);
344 ImGui::CloseCurrentPopup();
345 }
346 ImGui::SameLine();
347 if (ImGui::Button("Keep editing")) {
348 ImGui::CloseCurrentPopup();
349 }
350 ImGui::EndPopup();
351 }
352 if (dirty_) {
353 ImGui::SameLine();
355 }
356 if (!save_error_.empty()) {
358 }
359 return plan_.has_value();
360}
361
363 if (!DrawPlanHeader()) {
364 return;
365 }
366 if (!plan_->title.empty()) {
367 ImGui::TextUnformatted(plan_->title.c_str());
368 }
369 if (!plan_->about.empty()) {
370 ImGui::TextWrapped("%s", plan_->about.c_str());
371 }
372 ImGui::Separator();
373 DrawGoal();
379}
380
382 auto& goal = plan_->goal;
383 ImGui::SeparatorText(ICON_MD_FLAG " Goal");
384 ImGui::SetNextItemWidth(-1);
385 if (ImGui::InputTextWithHint("##goal_title", "What this push delivers",
386 &goal.title)) {
387 MarkDirty();
388 }
389 ImGui::SetNextItemWidth(120);
390 if (ImGui::InputTextWithHint("##goal_due", "YYYY-MM-DD", &goal.due)) {
391 MarkDirty();
392 }
393 const std::string left = DaysUntil(goal.due);
394 if (!left.empty()) {
395 ImGui::SameLine();
396 ImGui::TextDisabled("%s", left.c_str());
397 }
398 if (ImGui::InputTextMultiline("##goal_detail", &goal.detail,
399 ImVec2(-1, ImGui::GetTextLineHeight() * 3))) {
400 MarkDirty();
401 }
402}
403
405 ImGui::SeparatorText(ICON_MD_TIMELINE " Milestones");
406 const core::PlanMilestone* current = plan_->CurrentMilestone(Today());
407 if (current) {
408 gui::ColoredTextF(gui::GetInfoColor(), "Now: %s", current->title.c_str());
409 if (!current->detail.empty()) {
410 ImGui::TextWrapped("%s", current->detail.c_str());
411 }
412 }
413 if (!ImGui::TreeNode("##milestones", "All milestones (%zu)",
414 plan_->milestones.size())) {
415 return;
416 }
417 if (ImGui::BeginTable("##milestone_table", 4,
418 ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerH |
419 ImGuiTableFlags_SizingStretchProp)) {
420 ImGui::TableSetupColumn("Milestone", ImGuiTableColumnFlags_WidthStretch);
421 ImGui::TableSetupColumn("Start", ImGuiTableColumnFlags_WidthFixed, 90);
422 ImGui::TableSetupColumn("End", ImGuiTableColumnFlags_WidthFixed, 90);
423 ImGui::TableSetupColumn("Status", ImGuiTableColumnFlags_WidthFixed, 110);
424 ImGui::TableHeadersRow();
425 for (size_t i = 0; i < plan_->milestones.size(); ++i) {
426 auto& m = plan_->milestones[i];
427 ImGui::PushID(static_cast<int>(i));
428 ImGui::TableNextRow();
429 ImGui::TableNextColumn();
430 ImGui::SetNextItemWidth(-1);
431 if (ImGui::InputText("##title", &m.title)) {
432 MarkDirty();
433 }
434 ImGui::TableNextColumn();
435 ImGui::SetNextItemWidth(-1);
436 if (ImGui::InputText("##start", &m.start)) {
437 MarkDirty();
438 }
439 ImGui::TableNextColumn();
440 ImGui::SetNextItemWidth(-1);
441 if (ImGui::InputText("##end", &m.end)) {
442 MarkDirty();
443 }
444 ImGui::TableNextColumn();
445 ImGui::SetNextItemWidth(-1);
446 if (StatusCombo("##status", &m.status, kMilestoneStatuses)) {
447 MarkDirty();
448 }
449 ImGui::PopID();
450 }
451 ImGui::EndTable();
452 }
453 if (ImGui::SmallButton(ICON_MD_ADD " Milestone")) {
455 m.id = plan_->NewMilestoneId();
456 m.title = "New milestone";
457 m.status = "planned";
458 plan_->milestones.push_back(std::move(m));
459 MarkDirty();
460 }
461 ImGui::TreePop();
462}
463
465 ImGui::SeparatorText(ICON_MD_ROUTE " Route");
466 if (plan_->route_steps.empty()) {
467 ImGui::TextDisabled("No route steps yet. Add them in the Route window.");
468 return;
469 }
470 ImGui::BeginChild("##route_strip",
471 ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 1.6f), false,
472 ImGuiWindowFlags_HorizontalScrollbar);
473 for (size_t i = 0; i < plan_->route_steps.size(); ++i) {
474 const auto& step = plan_->route_steps[i];
475 if (i > 0) {
476 ImGui::SameLine(0, 2);
477 ImGui::TextDisabled(ICON_MD_CHEVRON_RIGHT);
478 ImGui::SameLine(0, 2);
479 }
480 ImGui::PushID(static_cast<int>(i));
481 const bool selected = selected_step_ == static_cast<int>(i);
482 if (selected) {
483 ImGui::PushStyleColor(ImGuiCol_Button,
484 ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive));
485 }
486 if (ImGui::Button(StepLabel(step).c_str())) {
487 selected_step_ = static_cast<int>(i);
488 selected_item_ = -1;
489 }
490 if (selected) {
491 ImGui::PopStyleColor();
492 }
493 if (ImGui::IsItemHovered()) {
494 ImGui::SetTooltip("%s\n%s", step.title.c_str(), step.note.c_str());
495 }
496 ImGui::PopID();
497 }
498 ImGui::EndChild();
499 if (selected_step_ >= 0 &&
500 selected_step_ < static_cast<int>(plan_->route_steps.size())) {
501 const auto& step = plan_->route_steps[selected_step_];
502 ImGui::Text("%s", step.title.c_str());
503 if (!step.note.empty()) {
504 ImGui::TextWrapped("%s", step.note.c_str());
505 }
506 DrawTargets(step.targets, "strip");
507 DrawAgentObservation("step:" + step.id);
508 for (const auto* item : plan_->ItemsAtStep(step.id)) {
509 ImGui::BulletText("%s", item->name.c_str());
510 ImGui::SameLine();
511 gui::ColoredText(item->status.c_str(), StatusColor(item->status));
512 }
513 }
514}
515
517 const auto open = plan_->OpenDecisions();
518 ImGui::SeparatorText(
519 absl::StrFormat(ICON_MD_CHECKLIST " Decisions (%zu open)", open.size())
520 .c_str());
521 for (size_t i = 0; i < plan_->decisions.size(); ++i) {
522 auto& d = plan_->decisions[i];
523 ImGui::PushID(static_cast<int>(i));
524 const bool is_open = d.status.empty() || d.status == "open";
525 std::string header = d.priority > 0
526 ? absl::StrFormat("%d. %s", d.priority, d.title)
527 : d.title;
528 if (!is_open) {
529 header += absl::StrFormat(" (%s)", d.status);
530 }
531 ImGui::PushStyleColor(ImGuiCol_Text,
532 is_open ? ImGui::GetStyleColorVec4(ImGuiCol_Text)
534 const bool expanded = ImGui::TreeNode("##decision", "%s", header.c_str());
535 ImGui::PopStyleColor();
536 if (expanded) {
537 if (!d.recommendation.empty()) {
538 ImGui::TextWrapped("Recommendation: %s", d.recommendation.c_str());
539 }
540 ImGui::SetNextItemWidth(120);
541 if (StatusCombo("Status", &d.status, kDecisionStatuses)) {
542 MarkDirty();
543 }
544 if (ImGui::InputTextMultiline(
545 "##answer", &d.answer,
546 ImVec2(-1, ImGui::GetTextLineHeight() * 3))) {
547 MarkDirty();
548 }
549 DrawTargets(d.targets, "decision");
550 for (const auto& ref : d.refs) {
551 ImGui::TextDisabled("%s", ref.c_str());
552 }
553 DrawAgentObservation("decision:" + d.id);
554 ImGui::TreePop();
555 }
556 ImGui::PopID();
557 }
558 if (ImGui::SmallButton(ICON_MD_ADD " Decision")) {
560 d.id = plan_->NewDecisionId();
561 d.title = "New decision";
562 d.status = "open";
563 plan_->decisions.push_back(std::move(d));
564 MarkDirty();
565 }
566}
567
569 if (plan_->references.empty()) {
570 return;
571 }
572 ImGui::SeparatorText(ICON_MD_EVENT_NOTE " References");
573 for (size_t i = 0; i < plan_->references.size(); ++i) {
574 const auto& r = plan_->references[i];
575 ImGui::PushID(static_cast<int>(i));
576 ImGui::BulletText("%s", r.label.c_str());
577 if (!r.author.empty() || !r.date.empty()) {
578 ImGui::SameLine();
579 ImGui::TextDisabled("%s %s", r.author.c_str(), r.date.c_str());
580 }
581 if (!r.path.empty()) {
582 ImGui::SameLine();
583 if (ImGui::SmallButton(ICON_MD_CONTENT_COPY)) {
584 ImGui::SetClipboardText(r.path.c_str());
585 }
586 if (ImGui::IsItemHovered()) {
587 ImGui::SetTooltip("Copy path: %s", r.path.c_str());
588 }
589 }
590 ImGui::PopID();
591 }
592}
593
595 if (!DrawPlanHeader()) {
596 return;
597 }
598 if (ImGui::BeginTable(
599 "##route_layout", 2,
600 ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersInnerV)) {
601 ImGui::TableSetupColumn("Steps", ImGuiTableColumnFlags_WidthStretch, 0.45f);
602 ImGui::TableSetupColumn("Detail", ImGuiTableColumnFlags_WidthStretch,
603 0.55f);
604 ImGui::TableNextRow();
605 ImGui::TableNextColumn();
606 DrawStepList();
607 ImGui::TableNextColumn();
609 ImGui::EndTable();
610 }
611 ImGui::Separator();
612 DrawItems();
613}
614
616 auto& steps = plan_->route_steps;
617 ImGui::BeginChild("##step_list", ImVec2(0, 320));
618 std::string last_group;
619 int move_from = -1;
620 int move_to = -1;
621 for (size_t i = 0; i < steps.size(); ++i) {
622 const auto& step = steps[i];
623 if (!step.group.empty() && step.group != last_group) {
624 ImGui::SeparatorText(step.group.c_str());
625 last_group = step.group;
626 }
627 ImGui::PushID(static_cast<int>(i));
628 if (ImGui::Selectable(absl::StrFormat("%zu. %s %s", i + 1, StepLabel(step),
629 step.code.empty() ? "" : step.title)
630 .c_str(),
631 selected_step_ == static_cast<int>(i))) {
632 selected_step_ = static_cast<int>(i);
633 }
634 if (ImGui::BeginDragDropSource()) {
635 const int index = static_cast<int>(i);
636 ImGui::SetDragDropPayload("PLAN_STEP", &index, sizeof(index));
637 ImGui::TextUnformatted(StepLabel(step).c_str());
638 ImGui::EndDragDropSource();
639 }
640 if (ImGui::BeginDragDropTarget()) {
641 if (const ImGuiPayload* payload =
642 ImGui::AcceptDragDropPayload("PLAN_STEP")) {
643 move_from = *static_cast<const int*>(payload->Data);
644 move_to = static_cast<int>(i);
645 }
646 ImGui::EndDragDropTarget();
647 }
648 ImGui::PopID();
649 }
650 ImGui::EndChild();
651 if (move_from >= 0 && move_to >= 0 && move_from != move_to) {
652 auto moved = std::move(steps[move_from]);
653 steps.erase(steps.begin() + move_from);
654 steps.insert(steps.begin() + move_to, std::move(moved));
655 selected_step_ = move_to;
656 MarkDirty();
657 }
658 if (ImGui::SmallButton(ICON_MD_ADD " Step")) {
660 step.id = plan_->NewStepId();
661 step.title = "New step";
662 step.kind = "story";
663 const size_t at = selected_step_ >= 0 ? selected_step_ + 1 : steps.size();
664 steps.insert(steps.begin() + at, std::move(step));
665 selected_step_ = static_cast<int>(at);
666 MarkDirty();
667 }
668 ImGui::SameLine();
669 ImGui::TextDisabled("Drag steps to reorder");
670}
671
672void PlanningEditor::CommitStepIdEdit(std::string step_id) {
673 if (!plan_.has_value() || plan_->FindStep(step_id) < 0) {
674 return;
675 }
676 const std::string proposed(absl::StripAsciiWhitespace(step_id_edit_));
677 absl::Status status = ValidateStepIdEdit(*plan_, step_id, proposed);
678 std::string current = step_id;
679 if (status.ok() && proposed != step_id) {
680 status = plan_->RenameStep(step_id, proposed);
681 if (status.ok()) {
682 current = proposed;
683 MarkDirty();
684 }
685 }
686 // A rejected Id leaves the old Id in place and shows the reason inline.
687 step_id_edit_ = current;
688 step_id_edit_for_ = current;
689 step_id_error_ = status.ok() ? "" : std::string(status.message());
690}
691
692void PlanningEditor::RequestRemoval(RemovalKind kind, const std::string& id,
693 std::string label) {
694 pending_removal_ = kind;
696 pending_removal_label_ = std::move(label);
697}
698
704
706 bool removed = false;
707 if (plan_.has_value() && pending_removal_ == RemovalKind::kStep) {
708 const int index = plan_->FindStep(pending_removal_id_);
709 if (index >= 0) {
710 auto& steps = plan_->route_steps;
711 steps.erase(steps.begin() + index);
713 std::min<int>(selected_step_, static_cast<int>(steps.size()) - 1);
714 removed = true;
715 }
716 } else if (plan_.has_value() && pending_removal_ == RemovalKind::kItem) {
717 auto& items = plan_->route_items;
718 const auto it = std::find_if(
719 items.begin(), items.end(),
720 [&](const auto& item) { return item.id == pending_removal_id_; });
721 if (it != items.end()) {
722 items.erase(it);
723 selected_item_ = -1;
724 removed = true;
725 }
726 }
727 if (removed) {
728 MarkDirty();
729 }
731 return removed;
732}
733
735 const std::string message = absl::StrFormat(
736 "Remove %s? Planning edits have no undo; reloading the saved plan is "
737 "the only way back.",
739 if (gui::ConfirmationDialog(kRemoveConfirmPopupId, "Remove from plan?",
740 message.c_str(), "Remove", "Cancel")) {
742 }
743}
744
746 auto& steps = plan_->route_steps;
747 if (selected_step_ < 0 || selected_step_ >= static_cast<int>(steps.size())) {
748 ImGui::TextDisabled("Select a step");
749 return;
750 }
751 auto& step = steps[selected_step_];
752 ImGui::PushID("step_detail");
753 if (step_id_edit_for_ != step.id) {
754 // A different step is selected. Commit an edit still pending on the old
755 // one (focus left the field by selecting another step), then reseed.
758 }
759 if (step_id_edit_for_ != step.id) {
760 step_id_edit_ = step.id;
761 step_id_edit_for_ = step.id;
762 step_id_error_.clear();
763 }
764 }
765 ImGui::InputText("Id", &step_id_edit_);
766 if (ImGui::IsItemActivated()) {
767 step_id_error_.clear();
768 }
769 if (ImGui::IsItemDeactivatedAfterEdit()) {
770 CommitStepIdEdit(step.id);
771 }
772 if (!step_id_error_.empty()) {
774 }
775 if (ImGui::InputText("Code", &step.code)) {
776 MarkDirty();
777 }
778 if (ImGui::InputText("Title", &step.title)) {
779 MarkDirty();
780 }
781 if (ImGui::InputText("Group", &step.group)) {
782 MarkDirty();
783 }
784 if (StatusCombo("Kind", &step.kind, kStepKinds)) {
785 MarkDirty();
786 }
787 std::string targets = JoinTargets(step.targets);
788 if (ImGui::InputTextWithHint("Targets", "room:0x0C, map:0x4B, area:D1",
789 &targets)) {
790 step.targets = SplitTargets(targets);
791 MarkDirty();
792 }
793 DrawTargets(step.targets, "detail");
794 if (ImGui::InputTextMultiline("##note", &step.note,
795 ImVec2(-1, ImGui::GetTextLineHeight() * 4))) {
796 MarkDirty();
797 }
798 DrawAgentObservation("step:" + step.id);
799 if (ImGui::SmallButton(ICON_MD_DELETE " Remove step")) {
801 RemovalKind::kStep, step.id,
802 absl::StrFormat("step \"%s\" (Id %s)", StepLabel(step), step.id));
803 ImGui::OpenPopup(kRemoveConfirmPopupId);
804 }
806 ImGui::PopID();
807}
808
810 auto& items = plan_->route_items;
811 const std::string step_id =
812 selected_step_ >= 0 &&
813 selected_step_ < static_cast<int>(plan_->route_steps.size())
814 ? plan_->route_steps[selected_step_].id
815 : "";
816 ImGui::Text("Items (%zu)", items.size());
817 ImGui::SameLine();
818 ImGui::Checkbox("Selected step only", &items_at_step_only_);
819 ImGui::SameLine();
820 if (ImGui::SmallButton(ICON_MD_ADD " Item")) {
822 item.id = plan_->NewItemId();
823 item.name = "New item";
824 item.step = step_id;
825 item.status = "proposed";
826 items.push_back(std::move(item));
827 selected_item_ = static_cast<int>(items.size()) - 1;
828 MarkDirty();
829 }
830 if (ImGui::BeginTable("##items", 5,
831 ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY |
832 ImGuiTableFlags_BordersInnerH,
833 ImVec2(0, 220))) {
834 ImGui::TableSetupScrollFreeze(0, 1);
835 ImGui::TableSetupColumn("Item", ImGuiTableColumnFlags_WidthStretch);
836 ImGui::TableSetupColumn("Step", ImGuiTableColumnFlags_WidthFixed, 80);
837 ImGui::TableSetupColumn("Category", ImGuiTableColumnFlags_WidthFixed, 80);
838 ImGui::TableSetupColumn("Status", ImGuiTableColumnFlags_WidthFixed, 90);
839 ImGui::TableSetupColumn("Where", ImGuiTableColumnFlags_WidthStretch);
840 ImGui::TableHeadersRow();
841 for (size_t i = 0; i < items.size(); ++i) {
842 const auto& item = items[i];
843 if (items_at_step_only_ && !step_id.empty() && item.step != step_id) {
844 continue;
845 }
846 ImGui::PushID(static_cast<int>(i));
847 ImGui::TableNextRow();
848 ImGui::TableNextColumn();
849 if (ImGui::Selectable(item.name.c_str(),
850 selected_item_ == static_cast<int>(i),
851 ImGuiSelectableFlags_SpanAllColumns)) {
852 selected_item_ = static_cast<int>(i);
853 }
854 ImGui::TableNextColumn();
855 const int step_index = plan_->FindStep(item.step);
856 ImGui::TextUnformatted(
857 step_index >= 0 ? StepLabel(plan_->route_steps[step_index]).c_str()
858 : item.step.c_str());
859 ImGui::TableNextColumn();
860 ImGui::TextUnformatted(item.category.c_str());
861 ImGui::TableNextColumn();
862 gui::ColoredText(item.status.c_str(), StatusColor(item.status));
863 ImGui::TableNextColumn();
864 ImGui::TextUnformatted(item.where.c_str());
865 ImGui::PopID();
866 }
867 ImGui::EndTable();
868 }
869
870 if (selected_item_ < 0 || selected_item_ >= static_cast<int>(items.size())) {
871 return;
872 }
873 auto& item = items[selected_item_];
874 ImGui::SeparatorText(item.name.c_str());
875 ImGui::PushID("item_detail");
876 if (ImGui::InputText("Name", &item.name)) {
877 MarkDirty();
878 }
879 if (ImGui::BeginCombo("Step", item.step.c_str())) {
880 for (const auto& step : plan_->route_steps) {
881 if (ImGui::Selectable(
882 absl::StrFormat("%s %s", StepLabel(step), step.title).c_str(),
883 item.step == step.id)) {
884 item.step = step.id;
885 MarkDirty();
886 }
887 }
888 ImGui::EndCombo();
889 }
890 if (ImGui::InputText("Category", &item.category)) {
891 MarkDirty();
892 }
893 if (StatusCombo("Status", &item.status, kItemStatuses)) {
894 MarkDirty();
895 }
896 if (ImGui::Checkbox("Required", &item.required)) {
897 MarkDirty();
898 }
899 if (ImGui::InputText("Where", &item.where)) {
900 MarkDirty();
901 }
902 if (ImGui::InputTextMultiline("##item_note", &item.note,
903 ImVec2(-1, ImGui::GetTextLineHeight() * 3))) {
904 MarkDirty();
905 }
906 DrawAgentObservation("item:" + item.id);
907 for (const auto& need : item.needs) {
908 const int step_index = plan_->FindStep(need.step);
909 ImGui::BulletText("Needed at %s: %s",
910 step_index >= 0
911 ? StepLabel(plan_->route_steps[step_index]).c_str()
912 : need.step.c_str(),
913 need.what.c_str());
914 }
915 if (ImGui::SmallButton(ICON_MD_DELETE " Remove item")) {
917 absl::StrFormat("item \"%s\" (Id %s)", item.name, item.id));
918 ImGui::OpenPopup(kRemoveConfirmPopupId);
919 }
921 ImGui::PopID();
922}
923
926 if (section == nullptr) {
927 return;
928 }
929 ImGui::SeparatorText(ICON_MD_SMART_TOY " Agent status");
930 const auto observations = section->find("observations");
931 const size_t count =
932 observations != section->end() && observations->is_object()
933 ? observations->size()
934 : 0;
935 ImGui::TextDisabled("%zu observations, updated %s by %s", count,
936 JsonString(*section, "updated_utc", "-").c_str(),
937 JsonString(*section, "agent", "-").c_str());
938 const auto orphans = core::OrphanedPlanAgentObservations(*plan_);
939 if (!orphans.empty()) {
941 "%zu observations name entries no longer in the plan: %s",
942 orphans.size(), absl::StrJoin(orphans, ", ").c_str());
943 }
944 if (count == 0 ||
945 !ImGui::TreeNode("##agent_observations", "Observations (%zu)", count)) {
946 return;
947 }
948 for (auto it = observations->begin(); it != observations->end(); ++it) {
949 ImGui::BulletText("%s", it.key().c_str());
950 const std::string state = JsonString(it.value(), "state", "");
951 if (!state.empty()) {
952 ImGui::SameLine();
953 gui::ColoredText(state.c_str(), gui::GetInfoColor());
954 }
955 }
956 ImGui::TreePop();
957}
958
959void PlanningEditor::DrawAgentObservation(const std::string& key) {
961 if (obs == nullptr) {
962 return;
963 }
964 ImGui::PushID(key.c_str());
965 ImGui::SeparatorText(ICON_MD_SMART_TOY " Agent status");
966 const std::string state = JsonString(*obs, "state", "");
967 if (!state.empty()) {
968 gui::ColoredText(state.c_str(), gui::GetInfoColor());
969 }
970 for (const char* field : {"summary", "next"}) {
971 const std::string text = JsonString(*obs, field, "");
972 if (!text.empty()) {
973 ImGui::TextWrapped("%s", text.c_str());
974 }
975 }
976 if (auto evidence = obs->find("evidence");
977 evidence != obs->end() && evidence->is_array()) {
978 for (const auto& entry : *evidence) {
979 if (entry.is_string()) {
980 ImGui::BulletText("%s", entry.get<std::string>().c_str());
981 }
982 }
983 }
984 if (auto prs = obs->find("prs"); prs != obs->end() && prs->is_array()) {
985 std::vector<std::string> labels;
986 for (const auto& pr : *prs) {
987 labels.push_back(pr.is_string() ? pr.get<std::string>()
988 : "#" + pr.dump());
989 }
990 ImGui::TextDisabled("PRs: %s", absl::StrJoin(labels, ", ").c_str());
991 }
992 ImGui::TextDisabled(
993 "%s %s - read-only; agents update this, your notes stay "
994 "yours",
995 JsonString(*obs, "agent", "").c_str(),
996 JsonString(*obs, "observed_utc", "").c_str());
997 ImGui::PopID();
998}
999
1000void PlanningEditor::DrawTargets(const std::vector<std::string>& targets,
1001 const char* id) {
1002 if (targets.empty()) {
1003 return;
1004 }
1005 ImGui::PushID(id);
1006 for (size_t i = 0; i < targets.size(); ++i) {
1007 if (i > 0) {
1008 ImGui::SameLine();
1009 }
1010 ImGui::PushID(static_cast<int>(i));
1011 const core::PlanTarget target = core::PlanTarget::Parse(targets[i]);
1012 if (target.NumericId() >= 0) {
1013 if (ImGui::SmallButton(targets[i].c_str())) {
1014 OpenTarget(targets[i]);
1015 }
1016 } else {
1017 ImGui::TextDisabled("%s", targets[i].c_str());
1018 }
1019 ImGui::PopID();
1020 }
1021 ImGui::PopID();
1022}
1023
1024void PlanningEditor::OpenTarget(const std::string& text) {
1025 const core::PlanTarget target = core::PlanTarget::Parse(text);
1026 const int id = target.NumericId();
1028 if (!bus || id < 0) {
1029 return;
1030 }
1031 if (target.kind == "room") {
1033 } else if (target.kind == "map") {
1035 }
1036}
1037
1038} // namespace yaze::editor
static absl::Status CreateAndLink(project::YazeProject *project, const std::string &path)
static absl::StatusOr< ProjectPlan > LoadFromFile(const std::string &path, std::string *source=nullptr)
int FindStep(std::string_view id) const
project::YazeProject * project() const
Definition editor.h:370
EditorDependencies dependencies_
Definition editor.h:382
void RequestRemoval(RemovalKind kind, const std::string &id, std::string label)
absl::Status Reload(bool discard_changes=false)
absl::Status Save() override
void CommitStepIdEdit(std::string step_id)
void DrawAgentObservation(const std::string &key)
void DrawTargets(const std::vector< std::string > &targets, const char *id)
std::optional< core::ProjectPlan > plan_
absl::Status Update() override
void OpenTarget(const std::string &target)
void Show(const std::string &message, ToastType type=ToastType::kInfo, float ttl_seconds=3.0f, std::string action_label={}, std::function< void()> on_action=nullptr)
Base interface for all logical window content components.
void RegisterWindowContent(std::unique_ptr< WindowContent > window)
Register a WindowContent instance for central drawing.
std::string GetId() const override
Unique identifier for this panel.
std::string GetIcon() const override
Material Design icon for this panel.
std::string GetDisplayName() const override
Human-readable name shown in menus and title bars.
WindowPresentationPolicy GetPresentationPolicy() const override
How this content is hosted and advertised by workspace chrome.
bool IsVisibleByDefault() const override
Whether this panel should be visible by default.
std::string GetEditorCategory() const override
Editor category this panel belongs to.
PlanningPanel(std::string id, std::string name, const char *icon, int priority, std::function< void()> draw)
int GetPriority() const override
Get display priority for menu ordering.
float GetPreferredWidth() const override
Get preferred width for this panel (optional)
#define ICON_MD_EVENT_NOTE
Definition icons.h:696
#define ICON_MD_CHECKLIST
Definition icons.h:402
#define ICON_MD_REFRESH
Definition icons.h:1572
#define ICON_MD_ROUTE
Definition icons.h:1627
#define ICON_MD_ADD
Definition icons.h:86
#define ICON_MD_FLAG
Definition icons.h:784
#define ICON_MD_DASHBOARD
Definition icons.h:517
#define ICON_MD_SAVE
Definition icons.h:1644
#define ICON_MD_DELETE
Definition icons.h:530
#define ICON_MD_CONTENT_COPY
Definition icons.h:465
#define ICON_MD_CHEVRON_RIGHT
Definition icons.h:406
#define ICON_MD_TIMELINE
Definition icons.h:1981
#define ICON_MD_SMART_TOY
Definition icons.h:1781
const PlanJson * FindPlanAgentStatus(const ProjectPlan &plan)
const PlanJson * FindPlanAgentObservation(const ProjectPlan &plan, std::string_view key)
bool AdoptPlanAgentOnlyChanges(ProjectPlan *draft, std::string_view loaded_source, std::string_view disk_source)
nlohmann::ordered_json PlanJson
std::vector< std::string > OrphanedPlanAgentObservations(const ProjectPlan &plan)
::yaze::EventBus * event_bus()
Get the current EventBus instance.
bool StatusCombo(const char *label, std::string *value, const char *const (&options)[N])
std::vector< std::string > SplitTargets(const std::string &text)
std::string StepLabel(const core::PlanRouteStep &step)
std::string JoinTargets(const std::vector< std::string > &targets)
std::string JsonString(const core::PlanJson &obj, const char *key, const char *fallback)
Editors are the view controllers for the application.
absl::Status ValidateStepIdEdit(const core::ProjectPlan &plan, std::string_view current_id, std::string_view proposed_id)
void ColoredText(const char *text, const ImVec4 &color)
ImVec4 GetSuccessColor()
Definition ui_helpers.cc:49
ImVec4 GetDisabledColor()
Definition ui_helpers.cc:74
void ColoredTextF(const ImVec4 &color, const char *fmt,...)
ImVec4 GetErrorColor()
Definition ui_helpers.cc:59
ImVec4 GetWarningColor()
Definition ui_helpers.cc:54
bool ConfirmationDialog(const char *id, const char *title, const char *message, const char *confirm_text, const char *cancel_text)
ImVec4 GetModifiedColor()
ImVec4 GetInfoColor()
Definition ui_helpers.cc:64
static PlanTarget Parse(std::string_view text)
WorkspaceWindowManager * window_manager
Definition editor.h:182
static JumpToMapRequestEvent Create(int map, size_t session=0)
static JumpToRoomRequestEvent Create(int room, size_t session=0)
std::string PlanningFilePath() const
Definition project.cc:2785