Skip to content

Commit

Permalink
Add memorization of tool-shared dynamics options
Browse files Browse the repository at this point in the history
  • Loading branch information
Gasparoken committed Aug 23, 2023
1 parent 572cdf2 commit 7855924
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 85 deletions.
15 changes: 14 additions & 1 deletion data/pref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,22 @@
<option id="discard_brush" type="bool" default="false" />
</section>
<section id="shared">
<option id="share_dynamics" type="bool" default="false" />
<option id="share_ink" type="bool" default="false" />
<option id="ink" type="app::tools::InkType" default="app::tools::InkType::DEFAULT" />
<option id="share_dynamics" type="bool" default="false" />
<option id="stabilizer" type="bool" default="false" />
<option id="stabilizer_factor" type="int" default="0" />
<option id="size" type="app::tools::DynamicSensor" default="app::tools::DynamicSensor::Static" />
<option id="angle" type="app::tools::DynamicSensor" default="app::tools::DynamicSensor::Static" />
<option id="gradient" type="app::tools::DynamicSensor" default="app::tools::DynamicSensor::Static" />
<option id="min_size" type="int" default="1" />
<option id="min_angle" type="int" default="1" />
<option id="color_from_to" type="app::tools::ColorFromTo" default="app::tools::ColorFromTo::BgToFg" />
<option id="matrix_index" type="int" default="0" />
<option id="min_pressure_threshold" type="double" default="0.1" />
<option id="max_pressure_threshold" type="double" default="0.9" />
<option id="min_velocity_threshold" type="double" default="0.1" />
<option id="max_velocity_threshold" type="double" default="0.9" />
</section>
<section id="new_file">
<option id="width" type="int" default="32" />
Expand Down
123 changes: 79 additions & 44 deletions src/app/ui/context_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,22 +1164,15 @@ class ContextBar::DynamicsField : public ButtonSet

if (!m_popup.get())
m_popup.reset(new DynamicsPopup(this));
auto activeTool = App::instance()->activeTool();
m_popup->loadDynamicsPref();
m_dynamics = m_popup->getDynamics();
m_sameInAllTools = m_popup->sharedSettings();
m_popup->loadDynamicsPref(m_sameInAllTools);
m_dynamics = m_popup->getDynamics();
m_popup->Close.connect(
[this](CloseEvent&) {
deselectItems();
auto activeTool = App::instance()->activeTool();
m_dynamics = m_popup->getDynamics();
m_sameInAllTools = m_popup->sharedSettings();
if (m_sameInAllTools) {
for (Tool* tool : *App::instance()->toolBox())
saveDynamicsPref(tool);
}
else
saveDynamicsPref(activeTool);
saveDynamicsPref(App::instance()->activeTool());
});

m_popup->refreshVisibility();
Expand Down Expand Up @@ -1207,45 +1200,87 @@ class ContextBar::DynamicsField : public ButtonSet
}

void saveDynamicsPref(Tool* tool) {
auto& dynaPref = Preferences::instance().tool(tool).dynamics;
dynaPref.stabilizer(m_dynamics.stabilizer);
dynaPref.stabilizerFactor(m_dynamics.stabilizerFactor);
dynaPref.size(m_dynamics.size);
dynaPref.angle(m_dynamics.angle);
dynaPref.gradient(m_dynamics.gradient);
dynaPref.minSize.setValue(m_dynamics.minSize);
dynaPref.minAngle.setValue(m_dynamics.minAngle);
dynaPref.minPressureThreshold(m_dynamics.minPressureThreshold);
dynaPref.minVelocityThreshold(m_dynamics.minVelocityThreshold);
dynaPref.maxPressureThreshold(m_dynamics.maxPressureThreshold);
dynaPref.maxVelocityThreshold(m_dynamics.maxVelocityThreshold);
dynaPref.colorFromTo(m_dynamics.colorFromTo);
dynaPref.matrixIndex(m_popup->ditheringIndex());
m_sameInAllTools = m_popup->sharedSettings();
Preferences::instance().shared.shareDynamics(m_sameInAllTools);

if (m_sameInAllTools) {
auto& sharedPref = Preferences::instance().shared;
sharedPref.stabilizer(m_dynamics.stabilizer);
sharedPref.stabilizerFactor(m_dynamics.stabilizerFactor);
sharedPref.size(m_dynamics.size);
sharedPref.angle(m_dynamics.angle);
sharedPref.gradient(m_dynamics.gradient);
sharedPref.minSize.setValue(m_dynamics.minSize);
sharedPref.minAngle.setValue(m_dynamics.minAngle);
sharedPref.minPressureThreshold(m_dynamics.minPressureThreshold);
sharedPref.minVelocityThreshold(m_dynamics.minVelocityThreshold);
sharedPref.maxPressureThreshold(m_dynamics.maxPressureThreshold);
sharedPref.maxVelocityThreshold(m_dynamics.maxVelocityThreshold);
sharedPref.colorFromTo(m_dynamics.colorFromTo);
sharedPref.matrixIndex(m_popup->ditheringIndex());
}
else {
auto& dynaPref = Preferences::instance().tool(tool).dynamics;
dynaPref.stabilizer(m_dynamics.stabilizer);
dynaPref.stabilizerFactor(m_dynamics.stabilizerFactor);
dynaPref.size(m_dynamics.size);
dynaPref.angle(m_dynamics.angle);
dynaPref.gradient(m_dynamics.gradient);
dynaPref.minSize.setValue(m_dynamics.minSize);
dynaPref.minAngle.setValue(m_dynamics.minAngle);
dynaPref.minPressureThreshold(m_dynamics.minPressureThreshold);
dynaPref.minVelocityThreshold(m_dynamics.minVelocityThreshold);
dynaPref.maxPressureThreshold(m_dynamics.maxPressureThreshold);
dynaPref.maxVelocityThreshold(m_dynamics.maxVelocityThreshold);
dynaPref.colorFromTo(m_dynamics.colorFromTo);
dynaPref.matrixIndex(m_popup->ditheringIndex());
}
}

void loadDynamicsPref() {
auto& dynaPref = Preferences::instance()
.tool(App::instance()->activeTool()).dynamics;
m_dynamics.stabilizer = dynaPref.stabilizer();
m_dynamics.stabilizerFactor = dynaPref.stabilizerFactor();
m_dynamics.size = dynaPref.size();
m_dynamics.angle = dynaPref.angle();
m_dynamics.gradient = dynaPref.gradient();
m_dynamics.minSize = dynaPref.minSize();
m_dynamics.minAngle = dynaPref.minAngle();
m_dynamics.minPressureThreshold = dynaPref.minPressureThreshold();
m_dynamics.minVelocityThreshold = dynaPref.minVelocityThreshold();
m_dynamics.maxPressureThreshold = dynaPref.maxPressureThreshold();
m_dynamics.maxVelocityThreshold = dynaPref.maxVelocityThreshold();
m_dynamics.colorFromTo = dynaPref.colorFromTo();

DitheringSelector matrixSel(DitheringSelector::SelectMatrix);
matrixSel.setSelectedItemIndex(dynaPref.matrixIndex());
render::DitheringMatrix matrix(matrixSel.ditheringMatrix());
m_dynamics.ditheringMatrix = matrix;

m_sameInAllTools = Preferences::instance().shared.shareDynamics();

if (m_sameInAllTools) {
auto& sharedPref = Preferences::instance().shared;
m_dynamics.stabilizer = sharedPref.stabilizer();
m_dynamics.stabilizerFactor = sharedPref.stabilizerFactor();
m_dynamics.size = sharedPref.size();
m_dynamics.angle = sharedPref.angle();
m_dynamics.gradient = sharedPref.gradient();
m_dynamics.minSize = sharedPref.minSize();
m_dynamics.minAngle = sharedPref.minAngle();
m_dynamics.minPressureThreshold = sharedPref.minPressureThreshold();
m_dynamics.minVelocityThreshold = sharedPref.minVelocityThreshold();
m_dynamics.maxPressureThreshold = sharedPref.maxPressureThreshold();
m_dynamics.maxVelocityThreshold = sharedPref.maxVelocityThreshold();
m_dynamics.colorFromTo = sharedPref.colorFromTo();

DitheringSelector matrixSel(DitheringSelector::SelectMatrix);
matrixSel.setSelectedItemIndex(sharedPref.matrixIndex());
render::DitheringMatrix matrix(matrixSel.ditheringMatrix());
m_dynamics.ditheringMatrix = matrix;
}
else {
auto& dynaPref = Preferences::instance()
.tool(App::instance()->activeTool()).dynamics;
m_dynamics.stabilizer = dynaPref.stabilizer();
m_dynamics.stabilizerFactor = dynaPref.stabilizerFactor();
m_dynamics.size = dynaPref.size();
m_dynamics.angle = dynaPref.angle();
m_dynamics.gradient = dynaPref.gradient();
m_dynamics.minSize = dynaPref.minSize();
m_dynamics.minAngle = dynaPref.minAngle();
m_dynamics.minPressureThreshold = dynaPref.minPressureThreshold();
m_dynamics.minVelocityThreshold = dynaPref.minVelocityThreshold();
m_dynamics.maxPressureThreshold = dynaPref.maxPressureThreshold();
m_dynamics.maxVelocityThreshold = dynaPref.maxVelocityThreshold();
m_dynamics.colorFromTo = dynaPref.colorFromTo();

DitheringSelector matrixSel(DitheringSelector::SelectMatrix);
matrixSel.setSelectedItemIndex(dynaPref.matrixIndex());
render::DitheringMatrix matrix(matrixSel.ditheringMatrix());
m_dynamics.ditheringMatrix = matrix;
}
}

private:
Expand Down
14 changes: 10 additions & 4 deletions src/app/ui/dithering_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ void DitheringSelector::onInitTheme(ui::InitThemeEvent& ev)
setSizeHint(getItem(0)->sizeHint());
}

void DitheringSelector::regenerate()
void DitheringSelector::setSelectedIndex(int itemIndex)
{
setSelectedItemIndex(itemIndex);
regenerate(itemIndex);
}

void DitheringSelector::regenerate(int selectedItemIndex)
{
deleteAllItems();

Expand Down Expand Up @@ -255,9 +261,9 @@ void DitheringSelector::regenerate()
}
break;
}

setSelectedItemIndex(0);
setSizeHint(getItem(0)->sizeHint());
selectedItemIndex = std::min(std::max(0, selectedItemIndex), getItemCount()-1);
setSelectedItemIndex(selectedItemIndex);
setSizeHint(getItem(selectedItemIndex)->sizeHint());
}

render::DitheringAlgorithm DitheringSelector::ditheringAlgorithm()
Expand Down
4 changes: 2 additions & 2 deletions src/app/ui/dithering_selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace app {

render::DitheringAlgorithm ditheringAlgorithm();
render::DitheringMatrix ditheringMatrix();
void setSelectedIndex(int itemIndex);

protected:
void onInitTheme(ui::InitThemeEvent& ev) override;

private:
void regenerate();

void regenerate(int selectedItemIndex = 0);
Type m_type;
obs::scoped_connection m_extChanges;
};
Expand Down
Loading

0 comments on commit 7855924

Please sign in to comment.