Vital
Loading...
Searching...
No Matches
tab_selector.cpp
Go to the documentation of this file.
1
3
4#include "tab_selector.h"
5
6#include "fonts.h"
7#include "skin.h"
8
11TabSelector::TabSelector(String name) : Slider(name), font_height_percent_(kDefaultFontHeightPercent), active_(true) {
12 image_component_.setComponent(this);
13 image_component_.setScissor(true);
14
15 setTextBoxStyle(Slider::NoTextBox, true, 0, 0);
16 setColour(Slider::backgroundColourId, Colour(0xff303030));
17 setColour(Slider::textBoxOutlineColourId, Colour(0x00000000));
18 setRange(0, 1, 1);
19}
20
23void TabSelector::paint(Graphics& g) {
24 static constexpr float kLightHeightPercent = 0.08f;
25 int selected = getValue();
26 int num_types = getMaximum() - getMinimum() + 1;
27 int from_highlight = getTabX(selected);
28 int to_highlight = getTabX(selected + 1);
29 int light_height = std::max<int>(getHeight() * kLightHeightPercent, 1);
30
31 Colour highlight_color = findColour(Skin::kWidgetPrimary1, true);
32 if (!active_)
33 highlight_color = highlight_color.withSaturation(0.0f);
34
35 g.setColour(findColour(Skin::kLightenScreen, true));
36 g.fillRect(0, 0, getWidth(), light_height);
37
38 g.setColour(highlight_color);
39 g.fillRect(from_highlight, 0, to_highlight - from_highlight, light_height);
40
41 g.setFont(Fonts::instance()->proportional_light().withPointHeight(getHeight() * font_height_percent_));
42 for (int i = 0; i < num_types && i < names_.size(); ++i) {
43 std::string name = names_[i];
44 int from_x = getTabX(i);
45 int to_x = getTabX(i + 1);
46 if (i == selected)
47 g.setColour(highlight_color);
48 else
49 g.setColour(findColour(Skin::kTextComponentText, true));
50
51 g.drawText(name, from_x, 0, to_x - from_x, getHeight(), Justification::centred);
52 }
53}
54
57void TabSelector::mouseEvent(const juce::MouseEvent &e) {
58 float x = e.getPosition().getX();
59 int index = x * (getMaximum() + 1) / getWidth();
60 setValue(index);
61}
62
65void TabSelector::mouseDown(const juce::MouseEvent &e) {
66 mouseEvent(e);
67}
68
71void TabSelector::mouseDrag(const juce::MouseEvent &e) {
72 mouseEvent(e);
73}
74
77void TabSelector::mouseUp(const juce::MouseEvent &e) {
78 mouseEvent(e);
79}
80
85int TabSelector::getTabX(int position) {
86 int num_types = getMaximum() - getMinimum() + 1;
87 return std::round(float((getWidth() + 1) * position) / num_types);
88}
89
static Fonts * instance()
Gets the singleton instance of the Fonts class.
Definition fonts.h:52
void setComponent(Component *component)
Sets the component to be drawn into the OpenGL image. If not set, uses this component.
Definition open_gl_image_component.h:83
void setScissor(bool scissor)
Enables or disables scissor testing when drawing the image.
Definition open_gl_image_component.h:89
@ kWidgetPrimary1
Definition skin.h:165
@ kLightenScreen
Definition skin.h:141
@ kTextComponentText
Definition skin.h:148
TabSelector(String name)
Definition tab_selector.cpp:11
void paint(Graphics &g) override
Definition tab_selector.cpp:23
void mouseEvent(const MouseEvent &e)
Definition tab_selector.cpp:57
void mouseDrag(const MouseEvent &e) override
Definition tab_selector.cpp:71
void mouseDown(const MouseEvent &e) override
Definition tab_selector.cpp:65
void mouseUp(const MouseEvent &e) override
Definition tab_selector.cpp:77
Declares the TabSelector class, a slider-based UI component for selecting tabs.