Files
lightgui/widgets/components/XTachometer/XTachometer.h
T
Muse 7da1a1ef93 Follow-up: tach title placement + vertical-bar peak shrink-to-fit
- XTachometer: the "RPM" title baseline now hangs below the top
  numeral's bottom edge instead of a fixed cy - r*0.55, so it
  cannot collide with the neighbouring numerals on small dials
  (found in the Python demo at r=73, where 3200/RPM/4800 jammed).
- XVerticalBar: the "peak N" label shrinks 14px -> 11px when the
  bar is too narrow for it to fit inside the frame.
2026-12-05 21:32:51 +00:00

282 lines
9.8 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
// region XTachometer
//
// A round tachometer: needle over a 240-degree sweep, a redline zone at
// the top of the scale, a digital readout and a peak-hold tick.
// Raw X11, same as the rest of the XFrame set.
//
class XTachometer
{
private:
Display* display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager* styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_;
std::string style_name_;
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
double value_ = 0.0;
double max_ = 8000.0;
double redline_ = 6500.0;
double peak_ = 0.0;
bool peak_hold_ = true;
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
Srgb dial_color_{240, 240, 240};
Srgb redline_color_{210, 40, 30};
Srgb needle_color_{30, 30, 30};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt0(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.0f", v);
return buf;
}
// Sweep: 240 degrees clockwise from 150 deg (7:30 position) to 30 deg.
static double angle_deg(double frac)
{
return 150.0 - frac * 240.0;
}
public:
XTachometer(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
{
display_ = display;
screen_ = screen;
window_ = window;
gc_ = gc;
styles_ = styles;
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
name_ = "XTachometer";
text_ = "RPM";
value_ = 4200.0;
peak_ = 6100.0;
}
~XTachometer() {}
// region Standard plumbing
void set_bounds(int x, int y, int width, int height)
{
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
}
int width() { return x2_ - x1_; }
int height() { return y2_ - y1_; }
bool contains(int x, int y)
{
return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_;
}
void set_visible(bool v) { visible_ = v; }
bool get_visible() { return visible_; }
void set_enabled(bool e) { enabled_ = e; }
bool get_enabled() { return enabled_; }
void set_style_name(std::string s) { style_name_ = s; }
void set_text(std::string t) { text_ = t; }
std::string get_text() { return text_; }
SStyle style() { return styles_->resolve(style_name_); }
// endregion
// region Gauge API
void set_value(double v)
{
value_ = std::max(0.0, std::min(max_, v));
if (peak_hold_) peak_ = std::max(peak_, value_);
}
double get_value() { return value_; }
void set_max(double m)
{
max_ = m > 0.0 ? m : 1.0;
value_ = std::min(value_, max_);
peak_ = std::min(peak_, max_);
redline_ = std::min(redline_, max_);
}
double get_max() { return max_; }
void set_redline(double r) { redline_ = std::max(0.0, std::min(max_, r)); }
double get_redline() { return redline_; }
double peak() { return peak_; }
void reset_peak() { peak_ = value_; }
// endregion
void draw()
{
if (!visible_) return;
SStyle st = style();
int w = width();
int h = height();
XSetForeground(display_, gc_, col(white_color_));
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
const int title_h = 20;
int cx = x1_ + w / 2;
int cy = y1_ + title_h + (h - title_h) / 2 + 8;
int r = std::max(20, std::min(w, h - title_h - 44) / 2 - 6);
double frac = max_ > 0.0 ? value_ / max_ : 0.0;
double red_frac = max_ > 0.0 ? redline_ / max_ : 1.0;
double peak_frac = max_ > 0.0 ? peak_ / max_ : 0.0;
// Scaled fonts (Dec 5, 2026 tach fix): dial numerals ~7-8% of
// the dial diameter, bold, just inside the major tick ring;
// "RPM" title slightly smaller, bold; readout legible, bold.
int numeral_px = xframe::clamp_font_px(0.075 * 2 * r, 13, 28);
int title_px = xframe::clamp_font_px(numeral_px - 4, 13, 24);
int readout_px = xframe::clamp_font_px(0.05 * 2 * r, 13, 20);
XFontStruct* numeral_font = xframe::scaled_font(display_, numeral_px, true);
if (!numeral_font) numeral_font = st.font_;
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
if (!title_font) title_font = st.font_;
XFontStruct* readout_font = xframe::scaled_font(display_, readout_px, true);
if (!readout_font) readout_font = st.font_;
// Dial face.
XSetForeground(display_, gc_, col(dial_color_));
XFillArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
XSetForeground(display_, gc_, col(axis_color_));
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
// Redline zone: thick red arc from the redline to the end of scale.
{
int a0 = (int)(angle_deg(red_frac) * 64);
int sweep = (int)((1.0 - red_frac) * 240.0 * 64);
XSetForeground(display_, gc_, col(redline_color_));
xf::set_line_width(display_, gc_, 8);
// X angles run counter-clockwise; our sweep is clockwise.
XDrawArc(display_, window_, gc_, cx - r + 4, cy - r + 4, 2 * (r - 4), 2 * (r - 4),
a0 - sweep, sweep);
xf::set_line_width(display_, gc_, 1);
}
// Tick marks every 1/8 of the sweep, labels every 1/4.
for (int i = 0; i <= 8; ++i)
{
double f = (double)i / 8.0;
double a = angle_deg(f) * M_PI / 180.0;
int x0 = cx + (int)((r - 10) * std::cos(a));
int y0 = cy - (int)((r - 10) * std::sin(a));
int x1 = cx + (int)((r - 2) * std::cos(a));
int y1 = cy - (int)((r - 2) * std::sin(a));
XSetForeground(display_, gc_, col(axis_color_));
XDrawLine(display_, window_, gc_, x0, y0, x1, y1);
if (i % 2 == 0)
{
std::string lab = fmt0(max_ * f / 1000.0);
int lx = cx + (int)((r - 26) * std::cos(a));
int ly = cy - (int)((r - 26) * std::sin(a));
int lw = xframe::font_text_width(numeral_font, lab);
// Numerals at/after the redline render in red.
Srgb lab_color = (f >= red_frac) ? redline_color_ : Srgb{20, 20, 20};
xframe::draw_text_font(display_, window_, gc_, numeral_font,
lx - lw / 2,
ly + (numeral_font ? numeral_font->ascent : 11) / 2,
lab, col(lab_color));
}
}
// "RPM" title: bold, centred inside the dial below the top
// numerals (not a tiny top-left corner label). Its baseline
// hangs below the top numeral's bottom edge, so it cannot
// collide with the neighbouring numerals on small dials.
if (!text_.empty())
{
int tw = xframe::font_text_width(title_font, text_);
int title_base = cy - (r - 22) + (int)(numeral_px * 0.75)
+ title_font->ascent + 4;
xframe::draw_text_font(display_, window_, gc_, title_font,
cx - tw / 2, title_base, text_,
col(Srgb{20, 20, 20}));
}
// Current value + peak readout: enlarged, inside the dial below
// the hub (not cramped at the bottom edge).
{
std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")";
int rw = xframe::font_text_width(readout_font, readout);
xframe::draw_text_font(display_, window_, gc_, readout_font,
cx - rw / 2, cy + (int)(r * 0.45), readout,
col(Srgb{20, 20, 20}));
}
// Peak-hold tick.
{
double a = angle_deg(peak_frac) * M_PI / 180.0;
int x0 = cx + (int)((r - 16) * std::cos(a));
int y0 = cy - (int)((r - 16) * std::sin(a));
int x1 = cx + (int)((r - 4) * std::cos(a));
int y1 = cy - (int)((r - 4) * std::sin(a));
XSetForeground(display_, gc_, col(redline_color_));
xf::set_line_width(display_, gc_, 3);
XDrawLine(display_, window_, gc_, x0, y0, x1, y1);
xf::set_line_width(display_, gc_, 1);
}
// Needle.
{
double a = angle_deg(frac) * M_PI / 180.0;
int nx = cx + (int)((r - 14) * std::cos(a));
int ny = cy - (int)((r - 14) * std::sin(a));
XSetForeground(display_, gc_, col(needle_color_));
xf::set_line_width(display_, gc_, 3);
XDrawLine(display_, window_, gc_, cx, cy, nx, ny);
xf::set_line_width(display_, gc_, 1);
XFillArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64);
}
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
if (button == 1 && contains(x, y))
{
reset_peak();
return true;
}
return false;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_motion(int x, int y)
{
(void)x;
(void)y;
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)key;
(void)str;
return false;
}
};