Skip to content

Commit

Permalink
Add support for animation easings (#80)
Browse files Browse the repository at this point in the history
Fixes #79.
  • Loading branch information
soreau authored Jun 24, 2024
1 parent 5f667b7 commit 84b1893
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ Option::Option(xmlNode *cur_node, Plugin *plugin)
} else if (type == "dynamic-list")
{
this->type = OPTION_TYPE_DYNAMIC_LIST;
} else if (type == "animation")
{
this->type = OPTION_TYPE_ANIMATION;
this->data.min = 0;
this->data.max = DBL_MAX;
} else
{
printf("WARN: [%s] unknown option type: %s\n", plugin->name.c_str(),
Expand Down Expand Up @@ -133,6 +138,10 @@ Option::Option(xmlNode *cur_node, Plugin *plugin)
this->default_value = atoi((char*)node->children->content);
break;

case OPTION_TYPE_ANIMATION:
this->default_value = (char*)node->children->content;
break;

case OPTION_TYPE_BOOL:
if (std::string((char*)node->children->content) == "true")
{
Expand Down Expand Up @@ -182,7 +191,8 @@ Option::Option(xmlNode *cur_node, Plugin *plugin)
}

if ((this->type != OPTION_TYPE_INT) &&
(this->type != OPTION_TYPE_DOUBLE))
(this->type != OPTION_TYPE_DOUBLE) &&
(this->type != OPTION_TYPE_ANIMATION))
{
printf("WARN: [%s] min defined for option type !int && !double\n",
plugin->name.c_str());
Expand All @@ -197,7 +207,8 @@ Option::Option(xmlNode *cur_node, Plugin *plugin)
}

if ((this->type != OPTION_TYPE_INT) &&
(this->type != OPTION_TYPE_DOUBLE))
(this->type != OPTION_TYPE_DOUBLE) &&
(this->type != OPTION_TYPE_ANIMATION))
{
printf("WARN: [%s] max defined for option type !int && !double\n",
plugin->name.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum option_type
OPTION_TYPE_GROUP,
OPTION_TYPE_SUBGROUP,
OPTION_TYPE_DYNAMIC_LIST,
OPTION_TYPE_ANIMATION,
};

enum mod_type
Expand Down
114 changes: 104 additions & 10 deletions src/wcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <wayfire/config/compound-option.hpp>
#include <wayfire/config/types.hpp>
#include <wayfire/config/xml.hpp>
#include <wayfire/util/duration.hpp>
#include <wordexp.h>

#define OUTPUT_CONFIG_PROGRAM "wdisplays"
Expand Down Expand Up @@ -338,11 +339,27 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
switch (option->type)
{
case OPTION_TYPE_INT:
case OPTION_TYPE_ANIMATION:
{
auto value_optional = wf::option_type::from_string<int>(
wf_option->get_value_str());
int value = value_optional ? value_optional.value() : std::get<int>(
option->default_value);
int value;
int default_value;
std::string current_easing_type;
if (option->type == OPTION_TYPE_INT)
{
value = wf::option_type::from_string<int>(wf_option->get_value_str()).value_or(
std::get<int>(option->default_value));
default_value = std::get<int>(option->default_value);
} else // OPTION_TYPE_ANIMATION
{
auto set_value =
wf::option_type::from_string<wf::animation_description_t>(wf_option->get_value_str());
value = set_value.has_value() ? set_value->length_ms : std::get<int>(option->default_value);
default_value =
wf::option_type::from_string<wf::animation_description_t>(std::get<std::string>(option->
default_value))->length_ms;
current_easing_type = wf::option_type::from_string<wf::animation_description_t>(
wf_option->get_value_str())->easing_name;
}

if (option->int_labels.empty())
{
Expand All @@ -352,14 +369,91 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
spin_button->signal_value_changed().connect(
[=, widget = spin_button.get()]
{
option->set_save(widget->get_value_as_int());
});
reset_button.signal_clicked().connect(
[=, widget = spin_button.get()]
{
widget->set_value(std::get<int>(option->default_value));
if (option->type == OPTION_TYPE_INT)
{
option->set_save(widget->get_value_as_int());
} else
{
auto set_value =
wf::option_type::from_string<wf::animation_description_t>(
wf_option->get_value_str());
auto easing =
set_value.has_value() ? set_value->easing_name : std::get<std::string>(option->
default_value);
std::string animation_value = std::to_string(
widget->get_value_as_int()) + "ms " + easing;
option->set_save(animation_value);
}
});
auto spinner = spin_button.get();
pack_end(std::move(spin_button));
if (option->type == OPTION_TYPE_INT)
{
reset_button.signal_clicked().connect(
[=, widget = spin_button.get()]
{
widget->set_value(std::get<int>(option->default_value));
});
}

if (option->type == OPTION_TYPE_ANIMATION)
{
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
std::string easing_types[] = {"linear", "circle", "sigmoid", "easeOutElastic"};
for (const auto & name : easing_types)
{
combo_box->append(name);
}

for (size_t i = 0; i < sizeof(easing_types); i++)
{
if (easing_types[i] == current_easing_type)
{
combo_box->set_active(i);
break;
}
}

combo_box->signal_changed().connect([=, widget = combo_box.get()]
{
int current_value;
auto opt_string = wf_option->get_value_str();
auto set_value = wf::option_type::from_string<wf::animation_description_t>(
opt_string);
current_value =
set_value.has_value() ? set_value->length_ms : std::get<int>(
option->default_value);
std::string animation_value = std::to_string(
current_value) + "ms " + widget->get_active_text();
option->set_save(animation_value);
});
reset_button.signal_clicked().connect(
[&, widget = combo_box.get(), spinner, option, default_value]
{
std::cout << __func__ << std::endl;
if (current_easing_type == easing_types[3])
{
widget->set_active(3);
} else if (current_easing_type == easing_types[2])
{
widget->set_active(2);
} else if (current_easing_type == easing_types[1])
{
widget->set_active(1);
} else // if (current_easing_type == easing_types[0])
{
widget->set_active(0);
}

std::cout << default_value << std::endl;
spinner->set_value(default_value);
std::string animation_value = std::to_string(
default_value) + "ms " + widget->get_active_text();
std::cout << animation_value << std::endl;
option->set_save(animation_value);
});
pack_end(std::move(combo_box));
}
} else
{
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
Expand Down

0 comments on commit 84b1893

Please sign in to comment.