File: SnapUtils.h

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (124 lines) | stat: -rw-r--r-- 3,585 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  SnapUtils.h

  Dmitry Vedenko

**********************************************************************/
#pragma once

#include "Callable.h"
#include "Prefs.h"
#include "Registry.h"

#include <functional>
#include <string_view>

enum class SnapMode
{
   SNAP_OFF,
   SNAP_NEAREST,
   SNAP_PRIOR
};

extern SNAPPING_API StringSetting SnapToSetting;
extern SNAPPING_API EnumSetting<SnapMode> SnapModeSetting;

SNAPPING_API SnapMode ReadSnapMode();
SNAPPING_API Identifier ReadSnapTo();

struct SnapRegistryGroup;
struct SnapRegistryItem;
struct SnapFunctionSuperGroup;

class AudacityProject;

struct SNAPPING_API SnapResult final
{
   //! Snapped time.
   /*!
      In case the snapping has failed for any reason, this will match the input.
      Snapping can fail if the function matching the identifier was not found
      or if the function decides the input is incorrect.
      */
   double time {};
   //! Set to true, if snapping has succeeded.
   bool snapped {};
};

struct SnapRegistryTraits : Registry::DefaultTraits{
   using LeafTypes = List<SnapRegistryItem>;
   using NodeTypes = List<SnapRegistryGroup, SnapFunctionSuperGroup>;
};
using SnapRegistryVisitor = Registry::VisitorFunctions<SnapRegistryTraits>;

struct SnapRegistryGroupData {
   const TranslatableString label;
   const bool inlined;
};

struct SNAPPING_API SnapRegistryGroup final
   : Composite::Extension<
      Registry::GroupItem<SnapRegistryTraits>,
      SnapRegistryGroupData,
      const Identifier &
   >
{
   using Extension::Extension;
   ~SnapRegistryGroup() override;
   bool Inlined() const { return inlined; }
   const TranslatableString &Label() const { return label; }
};

struct SNAPPING_API SnapRegistryItem : public Registry::SingleItem
{
   SnapRegistryItem(
      const Identifier& internalName, const TranslatableString& label);
   ~SnapRegistryItem() override;

   const TranslatableString label;

   virtual SnapResult Snap(const AudacityProject& project, double time, bool nearest) const = 0;
   virtual SnapResult SingleStep(const AudacityProject& project, double time, bool upwards) const = 0;
};

std::unique_ptr<SnapRegistryItem> TimeInvariantSnapFunction(
   const Identifier& functionId, const TranslatableString& label,
   double multiplier);

using MultiplierFunctor = std::function<double(const AudacityProject&)>;

std::unique_ptr<SnapRegistryItem> TimeInvariantSnapFunction(
   const Identifier& functionId, const TranslatableString& label,
   MultiplierFunctor functor);

constexpr auto SnapFunctionGroup = Callable::UniqueMaker<
   SnapRegistryGroup, const Identifier &, SnapRegistryGroupData>();

struct SNAPPING_API SnapFunctionsRegistry final {
   static Registry::GroupItem<SnapRegistryTraits>& Registry();

   static void Visit(const SnapRegistryVisitor& visitor);

   static const SnapRegistryItem* Find(const Identifier& id);

   static SnapResult Snap(const Identifier& id, const AudacityProject& project, double time, bool nearest);
   static SnapResult SingleStep(
      const Identifier& id, const AudacityProject& project, double time,
      bool upwards);
};

using SnapRegistryItemRegistrator =
   Registry::RegisteredItem<SnapFunctionsRegistry>;

struct SnapFunctionSuperGroup : Composite::Extension<
   Registry::GroupItem<SnapRegistryTraits>, void, const Identifier&
> {
   using Extension::Extension;
};

constexpr auto SnapFunctionItems =
   Callable::UniqueMaker<SnapFunctionSuperGroup>();