Update volume identified by ObjectID

(NOT VolumePtr which could invalidate by deleting volume)
This commit is contained in:
Filip Sykala 2022-03-22 16:58:29 +01:00
parent d9249024ba
commit ffab47dac7
4 changed files with 47 additions and 30 deletions

View File

@ -69,6 +69,7 @@ GLGizmoEmboss::GLGizmoEmboss(GLCanvas3D &parent)
, m_is_initialized(false) // initialize on first opening gizmo
, m_rotate_gizmo(parent, GLGizmoRotate::Axis::Z) // grab id = 2 (Z axis)
, m_font_manager(m_imgui->get_glyph_ranges())
, m_update_job_cancel(std::make_shared<bool>(false))
{
m_rotate_gizmo.set_group_id(0);
// TODO: add suggestion to use https://fontawesome.com/
@ -732,12 +733,18 @@ bool GLGizmoEmboss::process()
// exist loaded font?
Emboss::FontFileWithCache font = m_font_manager.get_font().font_file_with_cache;
if (!font.has_value()) return false;
auto data = std::make_unique<EmbossDataUpdate>(font,
create_configuration(),
create_volume_name(), m_volume);
auto &worker = wxGetApp().plater()->get_ui_job_worker();
replace_job(worker, std::make_unique<EmbossUpdateJob>(std::move(data)));
// cancel must be befor create new data
*m_update_job_cancel = true; // set old job to cancel
m_update_job_cancel = std::make_shared<bool>(false); // create new shared ptr
// IMPROVE: Cancel only EmbossUpdateJob no others
worker.cancel();
auto data = std::make_unique<EmbossDataUpdate>(
font, create_configuration(), create_volume_name(),
m_volume->id(), m_update_job_cancel);
queue_job(worker, std::make_unique<EmbossUpdateJob>(std::move(data)));
// notification is removed befor object is changed by job
remove_notification_not_valid_font();

View File

@ -202,11 +202,11 @@ private:
void fill_stored_font_items();
void select_stored_font_item();
//FontList m_font_list;
//size_t m_font_selected;// index to m_font_list
std::string m_text;
// cancel for previous update of volume to cancel finalize part
std::shared_ptr<bool> m_update_job_cancel;
// actual volume
ModelVolume *m_volume;

View File

@ -66,7 +66,7 @@ void EmbossUpdateJob::process(Ctl &ctl)
void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &)
{
if (canceled) return;
if (canceled || *m_input->cancel) return;
// for sure that some object is created from shape
if (m_result.its.indices.empty()) return;
@ -80,24 +80,21 @@ void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &)
// Check emboss gizmo is still open
if (manager.get_current_type() != GLGizmosManager::Emboss) return;
Plater::TakeSnapshot snapshot(plater,
GUI::format(_L("Text: %1%"),
m_input->text_configuration.text),
UndoRedo::SnapshotType::GizmoAction);
std::string snap_name = GUI::format(_L("Text: %1%"), m_input->text_configuration.text);
Plater::TakeSnapshot snapshot(plater, snap_name, UndoRedo::SnapshotType::GizmoAction);
ModelVolume *volume = m_input->volume;
// find volume by object id - NOT WORK
// -> edit text change volume id so could apper not found volume
// ModelVolume *volume = nullptr;
// Model &model = plater->model();
// for (auto obj : model.objects)
// for (auto vol : obj->volumes)
// if (vol->id() == volume_id) {
// volume = vol;
// break;
// }
// if (volume == nullptr) return;
assert(volume != nullptr);
ModelVolume *volume = nullptr;
Model &model = plater->model();
for (auto obj : model.objects)
for (auto vol : obj->volumes)
if (vol->id() == m_input->volume_id) {
volume = vol;
break;
}
// could appear when user delete edited volume
if (volume == nullptr)
return;
// update volume
volume->set_mesh(std::move(m_result));

View File

@ -29,6 +29,14 @@ class EmbossUpdateJob : public Job
public:
EmbossUpdateJob(std::unique_ptr<EmbossDataUpdate> input);
void process(Ctl &ctl) override;
/// <summary>
/// Update volume - change object_id
/// </summary>
/// <param name="canceled">Was process canceled.
/// NOTE: Be carefull it doesn't care about
/// time between finished process and started finalize part.</param>
/// <param name="">unused</param>
void finalize(bool canceled, std::exception_ptr &) override;
};
@ -89,8 +97,11 @@ struct EmbossDataBase
struct EmbossDataUpdate : public EmbossDataBase
{
// unique identifier of volume to change
// I can't proove of alive pointer
ModelVolume *volume;
ObjectID volume_id;
// flag that job is canceled
// for time after process.
std::shared_ptr<bool> cancel;
// unique identifier of volume to change
// Change of volume change id, last change could disapear
@ -98,9 +109,11 @@ struct EmbossDataUpdate : public EmbossDataBase
EmbossDataUpdate(Emboss::FontFileWithCache font_file,
TextConfiguration text_configuration,
std::string volume_name,
ModelVolume *volume)
ObjectID volume_id,
std::shared_ptr<bool> cancel)
: EmbossDataBase(font_file, text_configuration, volume_name)
, volume(volume)
, volume_id(volume_id)
, cancel(std::move(cancel))
{}
};