Files
lightgui/widgets/components/XTachometer/XTachometer.h
Muse 4488d527a5 XFrame monitor widgets: XUsageBar, XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge
Gradient usage bar (green->yellow->red, absolute scale), stacked
multi-segment bar with legend, vertical bar meter with scale +
peak-hold marker (click resets peak), round tachometer (needle,
redline zone, digital readout, peak hold), semicircular arc gauge
with coloured zones + big %, circular progress ring with centred %.
Header-only C++, demo mains, examples, Xvfb screenshots.
2026-12-05 16:49:02 +00:00

245 lines
7.7 KiB
C++

#pragma once
#include "core/xframe_core.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;
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
// 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));
xf::draw_text_at(display_, window_, gc_, st, lx - xf::text_width(st, lab) / 2, ly + 4, lab);
}
}
// 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);
}
// Digital readout under the dial.
std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")";
int rw = xf::text_width(st, readout);
xf::draw_text_at(display_, window_, gc_, st, cx - rw / 2, y2_ - 10, readout);
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;
}
};