Skip to content

Commit c665f0b

Browse files
haroonqcopybara-github
authored andcommitted
Disable spec editor when no spec is available.
PiperOrigin-RevId: 942493527 Change-Id: I90e1a0049b9cc9a82a5ede3a4a74a41425d5bee3
1 parent 38f0ff3 commit c665f0b

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/experimental/platform/ux/spec_editor.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace mujoco::platform {
2424

2525
SpecEditor::SpecEditor(int history_size) : capacity_(history_size) {}
2626

27+
void SpecEditor::Reset() {
28+
ref_spec_.reset();
29+
active_spec_.reset();
30+
}
31+
2732
void SpecEditor::Reset(const mjSpec& spec) {
2833
active_element_key_ = kInvalidElementKey;
2934
active_element_ = nullptr;
@@ -55,6 +60,10 @@ void SpecEditor::Reset(const mjSpec& spec) {
5560
}
5661

5762
std::unique_ptr<ModelHolder> SpecEditor::Compile() {
63+
if (!active_spec_) {
64+
return nullptr;
65+
}
66+
5867
auto holder = ModelHolder::FromSpec(mj_copySpec(active_spec_.get()));
5968
if (holder->ok()) {
6069
ref_spec_ = Copy(active_spec_.get());
@@ -66,6 +75,10 @@ std::unique_ptr<ModelHolder> SpecEditor::Compile() {
6675
mjSpec* SpecEditor::GetActiveSpec() const { return active_spec_.get(); }
6776

6877
mjsElement* SpecEditor::AddElement(mjtObj type) {
78+
if (!active_spec_) {
79+
return nullptr;
80+
}
81+
6982
// TODO: check that type is only for spec elements.
7083
mjsElement* element = AddElementToSpec(active_spec_.get(), type);
7184
if (element) {
@@ -82,6 +95,10 @@ mjsElement* SpecEditor::AddElement(mjtObj type) {
8295
}
8396

8497
mjsElement* SpecEditor::AddBodyElement(mjsBody* body, mjtObj type) {
98+
if (!active_spec_) {
99+
return nullptr;
100+
}
101+
85102
// TODO: check that type is only for body elements.
86103
mjsElement* element = AddElementToSpec(active_spec_.get(), type, body);
87104
if (element) {
@@ -98,6 +115,10 @@ mjsElement* SpecEditor::AddBodyElement(mjsBody* body, mjtObj type) {
98115
}
99116

100117
void SpecEditor::DeleteActiveElement() {
118+
if (!active_spec_) {
119+
return;
120+
}
121+
101122
if (active_element_) {
102123
const mjtObj type = active_element_->elemtype;
103124
const int index = active_map_.Remove(active_element_key_);
@@ -116,6 +137,10 @@ void SpecEditor::DeleteActiveElement() {
116137
}
117138

118139
void SpecEditor::SetActiveElement(mjsElement* element) {
140+
if (!active_spec_) {
141+
return;
142+
}
143+
119144
if (element == nullptr) {
120145
ref_element_ = nullptr;
121146
active_element_ = nullptr;
@@ -152,6 +177,10 @@ void SpecEditor::SetActiveElement(mjsElement* element) {
152177
}
153178

154179
void SpecEditor::UpdateReferenceElement() {
180+
if (!active_spec_) {
181+
return;
182+
}
183+
155184
if (active_element_ == nullptr) {
156185
ref_element_ = nullptr;
157186
} else {
@@ -175,6 +204,10 @@ mjsElement* SpecEditor::GetActiveElement() const { return active_element_; }
175204
mjsElement* SpecEditor::GetRefElement() const { return ref_element_; }
176205

177206
void SpecEditor::CommitChanges(mjsElement* element) {
207+
if (!active_spec_) {
208+
return;
209+
}
210+
178211
if (element == nullptr) {
179212
mju_warning("Element is null.");
180213
return;
@@ -233,6 +266,10 @@ void SpecEditor::Redo() {
233266
bool SpecEditor::CanRedo() const { return cursor_ < history_.size() - 1; }
234267

235268
void SpecEditor::AppendHistory(HistoryEntry entry) {
269+
if (!active_spec_) {
270+
return;
271+
}
272+
236273
++cursor_;
237274
while (history_.size() > cursor_) {
238275
history_.pop_back();

src/experimental/platform/ux/spec_editor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class SpecEditor {
3838
public:
3939
explicit SpecEditor(int history_size = 256);
4040

41+
// Resets the editor with an empty spec. No editing will be allowed.
42+
void Reset();
43+
4144
// Resets the editor to manage the given spec, clearing all cached data.
4245
// Assumes the given spec is correctly formed (i.e. compilable).
4346
void Reset(const mjSpec& spec);

src/experimental/studio/app.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ void App::LoadModelFromFile(const std::string& filepath) {
161161
model_holder_ = platform::ModelHolder::FromFile(resolved_file);
162162
if (model_holder_->ok()) {
163163
OnModelLoaded(filepath, kModelFromFile);
164-
spec_editor_.Reset(*spec());
164+
if (spec()) {
165+
spec_editor_.Reset(*spec());
166+
} else {
167+
spec_editor_.Reset();
168+
}
165169
UpdateFilePaths(resolved_file);
166170
if (model() && model()->names) {
167171
// Assumes the first string in the model is the name of the model itself.
@@ -188,7 +192,11 @@ void App::LoadModelFromBuffer(std::span<const std::byte> buffer,
188192
} else {
189193
SetLoadError(std::string(model_holder_->error()));
190194
}
191-
spec_editor_.Reset(*spec());
195+
if (spec()) {
196+
spec_editor_.Reset(*spec());
197+
} else {
198+
spec_editor_.Reset();
199+
}
192200
}
193201

194202
void App::OnModelLoaded(std::string filename, ModelKind model_kind) {

0 commit comments

Comments
 (0)