Files
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

187 lines
8.4 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
// InstDigitalReadout - 7-segment style numeric display, 0 to 999.9.
// Attribution: XFrame.
class InstDigitalReadout {
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 hover_ = false;
bool pressed_ = false;
double min_ = 0;
double max_ = 999.9;
double value_ = 123.4;
// Getter/setter pass (Group D): draw colours/state exposed as properties.
// Defaults preserve the original rendering.
Srgb segment_on_color_ = {255, 176, 0};
Srgb segment_off_color_ = {56, 48, 20}; // audit: unlit segments slightly more visible, uniform dim
Srgb white_color_ = {255, 255, 255};
Srgb ink_color_ = {40, 40, 40};
Srgb amber_color_ = {255, 176, 0};
Srgb dark_color_ = {30, 30, 30};
public:
InstDigitalReadout(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles,
int x, int y, int width, int height, double value = 123.4) {
display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles;
x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height;
name_ = "InstDigitalReadout"; text_ = "Readout";
value_ = value;
}
~InstDigitalReadout() {}
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; }
std::function<void()> on_click;
void set_value(double v) { value_ = std::max(min_, std::min(max_, v)); }
double get_value() const { return value_; }
void set_range(double lo, double hi) { if (hi > lo) { min_ = lo; max_ = hi; set_value(value_); } }
double min_value() const { return min_; }
double max_value() const { return max_; }
double fraction() const { return (max_ > min_) ? (value_ - min_) / (max_ - min_) : 0.0; }
void draw_segment(int x, int y, int w, int h, bool on, unsigned long col, unsigned long off) {
XSetForeground(display_, gc_, on ? col : off);
XFillRectangle(display_, window_, gc_, x, y, w, h);
}
void draw_digit(int x, int y, int w, int h, int d) {
static const bool seg[10][7] = {{1,1,1,1,1,1,0},{0,1,1,0,0,0,0},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,0,0,0,0},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
unsigned long on = styles_->get_color_rgb(segment_on_color_.r, segment_on_color_.g, segment_on_color_.b);
unsigned long off = styles_->get_color_rgb(segment_off_color_.r, segment_off_color_.g, segment_off_color_.b);
int t = std::max(6, h / 10); // segment thickness scales with digit height
draw_segment(x + t, y, w - 2 * t, t, seg[d][0], on, off);
draw_segment(x + w - t, y + t, t, h / 2 - 2 * t, seg[d][1], on, off);
draw_segment(x + w - t, y + h / 2 + t, t, h / 2 - 2 * t, seg[d][2], on, off);
draw_segment(x + t, y + h - t, w - 2 * t, t, seg[d][3], on, off);
draw_segment(x, y + h / 2 + t, t, h / 2 - 2 * t, seg[d][4], on, off);
draw_segment(x, y + t, t, h / 2 - 2 * t, seg[d][5], on, off);
draw_segment(x + t, y + h / 2 - t / 2, w - 2 * t, t, seg[d][6], on, off);
}
static std::string fmt(double v, int prec = 0) {
std::ostringstream oss; oss.precision(prec); oss << std::fixed << v;
return oss.str();
}
// Exposed draw colours/state (Group D getter/setter pass).
Srgb get_segment_on_color() const { return segment_on_color_; }
void set_segment_on_color(Srgb c) { segment_on_color_ = c; }
Srgb get_segment_off_color() const { return segment_off_color_; }
void set_segment_off_color(Srgb c) { segment_off_color_ = c; }
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_amber_color() const { return amber_color_; }
void set_amber_color(Srgb c) { amber_color_ = c; }
Srgb get_dark_color() const { return dark_color_; }
void set_dark_color(Srgb c) { dark_color_ = c; }
void draw() {
if (!visible_) return;
SStyle st = style();
// Audit G4: title 16 px; digits scale with the display.
SStyle stTitle = st; stTitle.font_ = xframe::inst_cap_font_prop(display_, 16);
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 amber = styles_->get_color_rgb(amber_color_.r, amber_color_.g, amber_color_.b);
unsigned long dark = styles_->get_color_rgb(dark_color_.r, dark_color_.g, dark_color_.b);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
int tw0 = xf::text_width(stTitle, text_);
xf::draw_text_at(display_, window_, gc_, stTitle, x1_ + (width() - tw0) / 2, y1_ + 24, text_);
int dx = x1_ + 12, dy = y1_ + 34, dw = width() - 24, dh = height() - 46;
XSetForeground(display_, gc_, dark);
XFillRectangle(display_, window_, gc_, dx, dy, dw, dh);
XSetForeground(display_, gc_, ink);
XDrawRectangle(display_, window_, gc_, dx, dy, dw, dh);
std::string s = fmt(value_, 1);
// Audit G4: digits were fixed 45x85 in a 696x438 window (19% of
// its height); now ~42% of the display height, centred.
int digh = (int)(dh * 0.42);
int digw = (int)(digh * 0.55);
int gap = 20;
int dps = std::max(8, digh / 12);
int totalw = 0;
for (char c : s) totalw += (c == '.') ? (dps + 6) : (c == '-' ? 27 : digw + gap);
totalw -= gap;
int tx = dx + (dw - totalw) / 2;
int ty = dy + (dh - digh) / 2;
for (char c : s) {
if (c == '.') {
XSetForeground(display_, gc_, amber);
XFillRectangle(display_, window_, gc_, tx, ty + digh - dps, dps, dps);
tx += dps + 6;
} else if (c == '-') {
XSetForeground(display_, gc_, amber);
XFillRectangle(display_, window_, gc_, tx, ty + digh / 2 - 4, 21, 8);
tx += 27;
} else if (c >= '0' && c <= '9') {
draw_digit(tx, ty, digw, digh, c - '0');
tx += digw + gap;
}
}
XSetForeground(display_, gc_, 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;
(void)button;
focused_ = true; pressed_ = true;
if (on_click) on_click();
return true;
}
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) {
hover_ = contains(x, y);
return hover_;
}
bool on_key(KeySym key, const char* str) {
(void)str;
(void)key;
if (!focused_ || !enabled_) return false;
double step = (max_ - min_) * 0.05;
switch (key) {
case XK_Left: case XK_Down: set_value(value_ - step); return true;
case XK_Right: case XK_Up: set_value(value_ + step); return true;
case XK_Home: set_value((min_ + max_) / 2.0); return true;
}
return false;
}
};