Skip to content

Commit

Permalink
Add save dynamics options between sessions (fix aseprite#3933)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gasparoken committed Jul 28, 2023
1 parent ab2d7f7 commit 408fd37
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
14 changes: 14 additions & 0 deletions data/pref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,20 @@
<option id="refer_to" type="FillReferTo" default="FillReferTo::ACTIVE_LAYER" />
<option id="pixel_connectivity" type="PixelConnectivity" default="PixelConnectivity::FOUR_CONNECTED" />
</section>
<section id="dynamics">
<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>
</tool>

<document>
Expand Down
3 changes: 2 additions & 1 deletion src/app/pref/preferences.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
Expand All @@ -15,6 +15,7 @@
#include "app/pref/option.h"
#include "app/sprite_sheet_data_format.h"
#include "app/sprite_sheet_type.h"
#include "app/tools/dynamics.h"
#include "app/tools/freehand_algorithm.h"
#include "app/tools/ink_type.h"
#include "app/tools/rotation_algorithm.h"
Expand Down
3 changes: 3 additions & 0 deletions src/app/script/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "app/script/docobj.h"
#include "app/script/engine.h"
#include "app/script/luacpp.h"
#include "app/tools/dynamics.h"
#include "doc/frame.h"
#include "doc/layer.h"
#include "doc/remap.h"
Expand Down Expand Up @@ -333,6 +334,8 @@ FOR_ENUM(app::gen::SymmetryMode)
FOR_ENUM(app::gen::TimelinePosition)
FOR_ENUM(app::gen::ToGrayAlgorithm)
FOR_ENUM(app::gen::WindowColorProfile)
FOR_ENUM(app::tools::ColorFromTo)
FOR_ENUM(app::tools::DynamicSensor)
FOR_ENUM(app::tools::FreehandAlgorithm)
FOR_ENUM(app::tools::RotationAlgorithm)
FOR_ENUM(doc::AniDir)
Expand Down
33 changes: 23 additions & 10 deletions src/app/ui/context_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,29 @@ class ContextBar::DynamicsField : public ButtonSet
: ButtonSet(1)
, m_ctxBar(ctxBar) {
addItem(SkinTheme::get(this)->parts.dynamics(), "dynamics_field");

m_popup.reset(new DynamicsPopup(this));
m_popup->loadDynamicPref(&Preferences::instance().tool(App::instance()->activeTool()));
m_popup->setOptionsGridVisibility(m_optionsGridVisibility);
m_dynamics = m_popup->getDynamics();
m_popup->Close.connect(
[this](CloseEvent&){
deselectItems();
m_dynamics = m_popup->getDynamics();
auto& dynaPref = Preferences::instance().tool(App::instance()->activeTool()).dynamics;
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 switchPopup() {
Expand All @@ -1160,16 +1183,6 @@ class ContextBar::DynamicsField : public ButtonSet
return;
}

if (!m_popup) {
m_popup.reset(new DynamicsPopup(this));
m_popup->setOptionsGridVisibility(m_optionsGridVisibility);
m_popup->Close.connect(
[this](CloseEvent&){
deselectItems();
m_dynamics = m_popup->getDynamics();
});
}

const gfx::Rect bounds = this->bounds();
m_popup->remapWindow();
fit_bounds(display(), m_popup.get(),
Expand Down
45 changes: 37 additions & 8 deletions src/app/ui/dynamics_popup.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2020-2022 Igara Studio S.A.
// Copyright (C) 2020-2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
Expand Down Expand Up @@ -58,6 +58,8 @@ class DynamicsPopup::ThresholdSlider : public Widget {

float minThreshold() const { return m_minThreshold; }
float maxThreshold() const { return m_maxThreshold; }
void minThreshold(float min) { m_minThreshold = min; }
void maxThreshold(float max) { m_maxThreshold = max; }
void setSensorValue(float v) {
m_sensorValue = v;
invalidate();
Expand Down Expand Up @@ -259,8 +261,6 @@ DynamicsPopup::DynamicsPopup(Delegate* delegate)
m_dynamics->pressurePlaceholder()->addChild(m_pressureThreshold = new ThresholdSlider);
m_dynamics->velocityPlaceholder()->addChild(m_velocityThreshold = new ThresholdSlider);
addChild(m_dynamics);

onValuesChange(nullptr);
}

void DynamicsPopup::setOptionsGridVisibility(bool state)
Expand All @@ -270,6 +270,38 @@ void DynamicsPopup::setOptionsGridVisibility(bool state)
expandWindow(sizeHint());
}

int DynamicsPopup::ditheringIndex() const {
if (m_ditheringSel)
return m_ditheringSel->getSelectedItemIndex();
return 0;
}

void DynamicsPopup::loadDynamicPref(ToolPreferences* toolPref) {
if (toolPref) {
auto& dynaPref = toolPref->dynamics;
int stabilizerFactor = dynaPref.stabilizerFactor();
m_dynamics->stabilizerFactor()->setValue(stabilizerFactor);
m_dynamics->stabilizer()->setSelected(stabilizerFactor > 0 ? true : false);
m_dynamics->minSize()->setValue(dynaPref.minSize());
m_dynamics->minAngle()->setValue(dynaPref.minAngle());
m_pressureThreshold->minThreshold(dynaPref.minPressureThreshold());
m_pressureThreshold->maxThreshold(dynaPref.maxPressureThreshold());
m_velocityThreshold->minThreshold(dynaPref.minVelocityThreshold());
m_velocityThreshold->maxThreshold(dynaPref.maxVelocityThreshold());
m_fromTo = dynaPref.colorFromTo();

setCheck(SIZE_WITH_PRESSURE, dynaPref.size() == tools::DynamicSensor::Pressure);
setCheck(SIZE_WITH_VELOCITY, dynaPref.size() == tools::DynamicSensor::Velocity);
setCheck(ANGLE_WITH_PRESSURE, dynaPref.angle() == tools::DynamicSensor::Pressure);
setCheck(ANGLE_WITH_VELOCITY, dynaPref.angle() == tools::DynamicSensor::Velocity);
setCheck(GRADIENT_WITH_PRESSURE, dynaPref.gradient() == tools::DynamicSensor::Pressure);
setCheck(GRADIENT_WITH_VELOCITY, dynaPref.gradient() == tools::DynamicSensor::Velocity);

if (m_ditheringSel)
m_ditheringSel->setSelectedItemIndex(dynaPref.matrixIndex());
}
}

tools::DynamicsOptions DynamicsPopup::getDynamics() const
{
tools::DynamicsOptions opts;
Expand Down Expand Up @@ -367,9 +399,7 @@ void DynamicsPopup::onValuesChange(ButtonSet::Item* item)
const bool any = (needsSize || needsAngle || needsGradient);
doc::BrushRef brush = m_delegate->getActiveBrush();

if (needsSize && !m_dynamics->minSize()->isVisible()) {
m_dynamics->minSize()->setValue(1);

if (needsSize) {
int maxSize = brush->size();
if (maxSize == 1) {
// If brush size == 1, we put it to 4 so the user has some size
Expand All @@ -383,8 +413,7 @@ void DynamicsPopup::onValuesChange(ButtonSet::Item* item)
m_dynamics->minSize()->setVisible(needsSize);
m_dynamics->maxSize()->setVisible(needsSize);

if (needsAngle && !m_dynamics->minAngle()->isVisible()) {
m_dynamics->minAngle()->setValue(brush->angle());
if (needsAngle) {
m_dynamics->maxAngle()->setValue(brush->angle());
}
m_dynamics->angleLabel()->setVisible(needsAngle);
Expand Down
5 changes: 4 additions & 1 deletion src/app/ui/dynamics_popup.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2020-2021 Igara Studio S.A.
// Copyright (C) 2020-2023 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
Expand All @@ -8,6 +8,7 @@
#define APP_UI_DYNAMICS_POPUP_H_INCLUDED
#pragma once

#include "app/pref/preferences.h"
#include "app/tools/dynamics.h"
#include "app/tools/velocity.h"
#include "app/ui/button_set.h"
Expand Down Expand Up @@ -36,6 +37,8 @@ namespace app {

tools::DynamicsOptions getDynamics() const;
void setOptionsGridVisibility(bool state);
void loadDynamicPref(ToolPreferences* toolPref);
int ditheringIndex() const;

private:
class ThresholdSlider;
Expand Down

0 comments on commit 408fd37

Please sign in to comment.