Files
lightgui/widgets/components/InstHorizontalTankLevel/InstHorizontalTankLevel.h
Muse 560d0aac2f Inst*: proportions fix (audit Dec 2026) — worst offenders
Seg14/16 + LedDigits fill widget (label band removed, ghost
segments dimmed, diagonals thinned); DigitalReadout digits scale
to display; Nameplate engraving fit to plate width; NeedleVu
red-zone numerals + VU clear of scale; dial numerals 7-8% of
diameter with titles inside below top numerals; KeySwitch NO KEY
light-on-dark; HorizontalTank capsule ends inside widget.
Shared scaled-font helpers in InstData.h (cap-height convention).
2026-12-05 21:31:56 +00:00

208 lines
9.5 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <sstream>
#include <string>
// InstHorizontalTankLevel - Horizontal bullet tank, fill by cross-section width, percent readout, hi/lo alarm markers.
// Attribution: XFrame.
class InstHorizontalTankLevel {
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;
bool pressed_ = false;
bool hovered_ = false;
double level_;
xframe::InstAlarmBands bands_;
std::function<void()> on_click_;
// Getter/setter pass (Group D): draw colours/state exposed as properties.
// Defaults preserve the original rendering.
Srgb white_color_ = {255, 255, 255};
Srgb ink_color_ = {60, 60, 60};
Srgb pale_color_ = {235, 242, 248};
Srgb fill_color_ = {40, 120, 200};
Srgb grid_color_ = {210, 210, 210};
Srgb amber_color_ = {220, 150, 0};
Srgb red_color_ = {200, 30, 30};
Srgb hover_color_ = {255, 170, 0};
public:
InstHorizontalTankLevel(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles,
int x, int y, int width, int height, double level = xframe::inst_demo_level(43)) {
display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles;
x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height;
name_ = "InstHorizontalTankLevel"; text_ = "";
level_ = xframe::inst_clamp_level(level);
}
~InstHorizontalTankLevel() {}
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() { SStyle s = styles_->resolve(style_name_); XFontStruct* bf = xframe::inst_bold_font(display_); if (bf) s.font_ = bf; return s; }
void set_level(double pct) { level_ = xframe::inst_clamp_level(pct); }
double get_level() { return level_; }
void set_alarm_bands(const xframe::InstAlarmBands& b) { bands_ = b; }
void set_alarm_hi_hi(double v) { bands_.hi_hi = v; }
void set_alarm_hi(double v) { bands_.hi = v; }
void set_alarm_lo(double v) { bands_.lo = v; }
void set_alarm_lo_lo(double v) { bands_.lo_lo = v; }
const xframe::InstAlarmBands& alarm_bands() { return bands_; }
void set_on_click(std::function<void()> f) { on_click_ = f; }
static std::string fmt(double v) {
std::ostringstream oss; oss.precision(2); oss << std::fixed << v;
std::string s = oss.str();
while (!s.empty() && s.back() == '0') s.pop_back();
if (!s.empty() && s.back() == '.') s.pop_back();
return s;
}
double level_at_point(int x, int y) {
(void)y;
int tx = x1_ + 40;
int tw = width() - 180;
double f = (double)(x - tx) / (double)tw;
return xframe::inst_clamp_level(f * 100.0);
}
// Exposed draw colours/state (Group D getter/setter pass).
Srgb get_white_color() const { return white_color_; }
void set_white_color(Srgb c) { white_color_ = c; }
Srgb get_ink_color() const { return ink_color_; }
void set_ink_color(Srgb c) { ink_color_ = c; }
Srgb get_pale_color() const { return pale_color_; }
void set_pale_color(Srgb c) { pale_color_ = c; }
Srgb get_fill_color() const { return fill_color_; }
void set_fill_color(Srgb c) { fill_color_ = c; }
Srgb get_grid_color() const { return grid_color_; }
void set_grid_color(Srgb c) { grid_color_ = c; }
Srgb get_amber_color() const { return amber_color_; }
void set_amber_color(Srgb c) { amber_color_ = c; }
Srgb get_red_color() const { return red_color_; }
void set_red_color(Srgb c) { red_color_ = c; }
Srgb get_hover_color() const { return hover_color_; }
void set_hover_color(Srgb c) { hover_color_ = c; }
void draw() {
if (!visible_) return;
SStyle st = style();
// Audit: scale 16 px, word labels 14 px bold.
SStyle stNum = st; stNum.font_ = xframe::inst_cap_font(display_, 16);
int numAsc = stNum.font_ ? stNum.font_->ascent : 13;
SStyle stLab = st; stLab.font_ = xframe::inst_cap_font_prop(display_, 14);
unsigned long white = styles_->get_color_rgb(white_color_.r, white_color_.g, white_color_.b);
unsigned long ink = styles_->get_color_rgb(ink_color_.r, ink_color_.g, ink_color_.b);
unsigned long pale = styles_->get_color_rgb(pale_color_.r, pale_color_.g, pale_color_.b);
unsigned long fillc = styles_->get_color_rgb(fill_color_.r, fill_color_.g, fill_color_.b);
unsigned long grid = styles_->get_color_rgb(grid_color_.r, grid_color_.g, grid_color_.b);
unsigned long amber = styles_->get_color_rgb(amber_color_.r, amber_color_.g, amber_color_.b);
unsigned long red = styles_->get_color_rgb(red_color_.r, red_color_.g, red_color_.b);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
// Audit: capsule ends are true half-discs INSIDE the widget;
// the old left cap drew an arc centred off-window (tx-th/2 < x1_)
// and the pale fill never covered the caps.
int th = 150;
int tx = x1_ + th / 2 + 12;
int ty = y1_ + 90;
int tw = width() - th - 24;
XSetForeground(display_, gc_, pale);
XFillRectangle(display_, window_, gc_, tx, ty, tw, th);
XFillArc(display_, window_, gc_, tx - th / 2, ty, th, th, 90 * 64, 180 * 64);
XFillArc(display_, window_, gc_, tx + tw - th / 2, ty, th, th, 270 * 64, 180 * 64);
int fw = (int)(tw * level_ / 100.0);
if (fw > 0) {
XSetForeground(display_, gc_, fillc);
XFillRectangle(display_, window_, gc_, tx, ty, fw, th);
}
XSetForeground(display_, gc_, ink);
XDrawLine(display_, window_, gc_, tx, ty, tx + tw, ty);
XDrawLine(display_, window_, gc_, tx, ty + th, tx + tw, ty + th);
XDrawArc(display_, window_, gc_, tx - th / 2, ty, th, th, 90 * 64, 180 * 64);
XDrawArc(display_, window_, gc_, tx + tw - th / 2, ty, th, th, 270 * 64, 180 * 64);
for (int v = 0; v <= 100; v += 25) {
int gx = tx + (int)(tw * v / 100.0);
XSetForeground(display_, gc_, grid);
XDrawLine(display_, window_, gc_, gx, ty + th + 4, gx, ty + th + 14);
std::string lab = std::to_string(v);
int lw = xf::text_width(stNum, lab);
xf::draw_text_at(display_, window_, gc_, stNum, gx - lw / 2, ty + th + 32, lab);
}
auto mark = [&](double pct, unsigned long c, const char* lab) {
int gx = tx + (int)(tw * pct / 100.0);
XSetForeground(display_, gc_, c);
XDrawLine(display_, window_, gc_, gx, ty, gx, ty + th);
xf::draw_text_at(display_, window_, gc_, stLab, gx + 4, ty - 10, lab);
};
mark(bands_.hi_hi, red, "HI-HI");
mark(bands_.hi, amber, "HI");
mark(bands_.lo, amber, "LO");
mark(bands_.lo_lo, red, "LO-LO");
std::string pct = fmt(level_) + "%";
// % centred below the scale, clear of the right cap curve (G7).
SStyle stVal = st; stVal.font_ = xframe::inst_cap_font(display_, 18);
int pvw = xf::text_width(stVal, pct);
xf::draw_text_at(display_, window_, gc_, stVal, x1_ + (width() - pvw) / 2, ty + th + 58, pct);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 16, text_);
XSetForeground(display_, gc_, hovered_ ? styles_->get_color_rgb(hover_color_.r, hover_color_.g, hover_color_.b) : st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height());
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button) {
if (!contains(x, y) || !enabled_) return false;
focused_ = true;
if (button == 4) { set_level(level_ + 5.0); return true; }
if (button == 5) { set_level(level_ - 5.0); return true; }
if (button == 1) {
pressed_ = true;
set_level(level_at_point(x, y));
if (on_click_) on_click_();
return true;
}
return false;
}
bool on_button_release(int x, int y, unsigned int button) {
(void)x; (void)y;
if (button == 1 && pressed_) { pressed_ = false; return true; }
return false;
}
bool on_motion(int x, int y) {
bool h = contains(x, y);
if (h == hovered_) return false;
hovered_ = h;
return true;
}
bool on_key(KeySym key, const char* str) {
(void)str;
(void)key;
if (!focused_ || !enabled_) return false;
switch (key) {
case XK_Up: case XK_Right: set_level(level_ + 5.0); return true;
case XK_Down: case XK_Left: set_level(level_ - 5.0); return true;
case XK_Home: set_level(0.0); return true;
case XK_End: set_level(100.0); return true;
case XK_r: case XK_R: set_level(xframe::inst_demo_level(42)); return true;
}
return false;
}
};