Audacity 3.2.0
CustomUpdaterValue.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 CustomUpdaterValue.cpp
6
7 Dominic Mazzoni
8 Michael Papadopoulos split from Ruler.cpp
9
10**********************************************************************/
11
12
13#include "CustomUpdaterValue.h"
14
16{
17 static CustomUpdaterValue instance;
18 return instance;
19}
20
21bool CustomUpdaterValue::TickCustom(wxDC& dc, int labelIdx, wxFont font,
22 // in/out:
23 TickOutputs outputs,
24 const RulerStruct& context) const
25{
26 const double mMin = context.mMin;
27 const double mMax = context.mMax;
28
29 const int mLeft = context.mLeft;
30 const int mTop = context.mTop;
31 const int mRight = context.mRight;
32 const int mBottom = context.mBottom;
33 const int mLength = context.mLength;
34 const int mOrientation = context.mOrientation;
35
36 const RulerStruct::Fonts& mFonts = *context.mpFonts;
37 constexpr int spacing = 2;
38 const bool mFlip = context.mFlip;
39 const TranslatableString mUnits = context.mUnits;
40 const bool mLabelEdges = context.mLabelEdges;
41
42 auto TickAtValue =
43 [this, &dc, &mFonts, mOrientation, mLabelEdges,
44 mMin, mMax, mLength, mRight, mBottom, &context]
45 (double value, double *pos) -> bool {
46 // Make a tick only if the value is strictly between the bounds
47 double min = std::min(mMin, mMax);
48 double max = std::max(mMin, mMax);
49 if ((value <= min && !mLabelEdges) || value < min)
50 return false;
51 if ((value >= max && !mLabelEdges) || value > max)
52 return false;
53
54 int mid = (int)(mLength * ((mMin - value) / (mMin - mMax)) + 0.5);
55
56 const int iMaxPos = (mOrientation == wxHORIZONTAL) ? mRight : mBottom - 5;
57 if (mid >= 0 && mid < iMaxPos) {
58 *pos = mid;
59 return true;
60 }
61 return false;
62 };
63
64 // FIXME: We don't draw a tick if of end of our label arrays
65 // But we shouldn't have an array of labels.
66 if (labelIdx >= outputs.labels.size())
67 return false;
68
69 // Get the correct position based on value.
70 // Don't draw if the value is out of bounds.
71 double pos;
72 if (!TickAtValue(outputs.labels[labelIdx].value, &pos))
73 return false;
74
75 Label lab;
76 lab.value = 0.0;
77 lab.pos = pos;
78 // Custom is flexible with text format
79 // We can assume they use the right format, but still append the right units.
80 lab.text = outputs.labels[labelIdx].text;
81 lab.units = mUnits;
82
83 auto result = MakeTick(
84 lab,
85 dc, font,
86 outputs.bits,
87 mLeft, mTop, spacing, mFonts.lead,
88 mFlip,
89 mOrientation);
90
91 if (!result.second.text)
92 // Always a non-empty optional
93 result.second.text = { TranslatableString{} };
94
95 auto& rect = result.first;
96 outputs.box.Union(rect);
97 outputs.labels[labelIdx] = (result.second);
98 return !rect.IsEmpty();
99}
100
int min(int a, int b)
static const CustomUpdaterValue & Instance()
~CustomUpdaterValue() override
bool TickCustom(wxDC &dc, int labelIdx, wxFont font, TickOutputs outputs, const RulerStruct &context) const override
static std::pair< wxRect, Label > MakeTick(RulerUpdater::Label lab, wxDC &dc, wxFont font, std::vector< bool > &bits, int left, int top, int spacing, int lead, bool flip, int orientation)
Holds a msgid for the translation catalog; may also bind format arguments.
std::unique_ptr< Fonts > mpFonts
Definition: RulerUpdater.h:52
double mMax
Definition: RulerUpdater.h:34
TranslatableString mUnits
Definition: RulerUpdater.h:53
int mOrientation
Definition: RulerUpdater.h:37
bool mLabelEdges
Definition: RulerUpdater.h:39
double mMin
Definition: RulerUpdater.h:32
An array of these created by the Updater is used to determine what and where text annotations to the ...
Definition: RulerUpdater.h:60
std::optional< TranslatableString > text
Definition: RulerUpdater.h:67
TranslatableString units
Definition: RulerUpdater.h:69