Files
lightgui/widgets/components/InstManometer/InstManometer.h
Muse a84a8e4e41 Inst*: proportions fix (audit Dec 2026) — level gauges + meters
Scale labels 16 px, alarm names 14 px with clearance (right-aligned
where they faced the vessel, dot/label separated); % readouts 18 px
below the vessel; SightGlass tube widened; BarMeter bar lowered so
title and top label clear the border.
2026-12-05 21:32:07 +00:00

166 lines
7.1 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
// InstManometer - U-tube manometer, liquid levels move apart with differential pressure in mm.
// Attribution: XFrame.
class InstManometer {
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_ = -50;
double max_ = 50;
double value_ = 12.0;
// Getter/setter pass (Group D): draw colours/state exposed as properties.
// Defaults preserve the original rendering.
Srgb white_color_ = {255, 255, 255};
Srgb face_color_ = {245, 245, 240};
Srgb ink_color_ = {40, 40, 40};
Srgb blue_color_ = {30, 120, 200};
public:
InstManometer(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles,
int x, int y, int width, int height, double value = 12.0) {
display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles;
x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height;
name_ = "InstManometer"; text_ = "Manometer";
value_ = value;
}
~InstManometer() {}
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; }
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_white_color() const { return white_color_; }
void set_white_color(Srgb c) { white_color_ = c; }
Srgb get_face_color() const { return face_color_; }
void set_face_color(Srgb c) { face_color_ = c; }
Srgb get_ink_color() const { return ink_color_; }
void set_ink_color(Srgb c) { ink_color_ = c; }
Srgb get_blue_color() const { return blue_color_; }
void set_blue_color(Srgb c) { blue_color_ = c; }
void draw() {
if (!visible_) return;
SStyle st = style();
// Audit: scale 16 px, title 18 px bold.
SStyle stNum = st; stNum.font_ = xframe::inst_cap_font(display_, 16);
int numAsc = stNum.font_ ? stNum.font_->ascent : 13;
SStyle stTitle = st; stTitle.font_ = xframe::inst_cap_font_prop(display_, 18);
unsigned long white = styles_->get_color_rgb(white_color_.r, white_color_.g, white_color_.b);
unsigned long face = styles_->get_color_rgb(face_color_.r, face_color_.g, face_color_.b);
unsigned long ink = styles_->get_color_rgb(ink_color_.r, ink_color_.g, ink_color_.b);
unsigned long blue = styles_->get_color_rgb(blue_color_.r, blue_color_.g, blue_color_.b);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
int cx = (x1_ + x2_) / 2;
int top = y1_ + 34, bot = y2_ - 34;
int colw = 36, gap = 30;
int lx = cx - gap / 2 - colw, rx = cx + gap / 2;
XSetForeground(display_, gc_, face);
XFillRectangle(display_, window_, gc_, lx, top, colw, bot - top);
XFillRectangle(display_, window_, gc_, rx, top, colw, bot - top);
XFillRectangle(display_, window_, gc_, lx, bot - 18, (rx + colw) - lx, 18);
int mid = (top + bot) / 2;
int dh = (int)((value_ / 50.0) * ((bot - top) / 4));
int ltop = mid + dh, rtop = mid - dh;
XSetForeground(display_, gc_, blue);
XFillRectangle(display_, window_, gc_, lx, ltop, colw, bot - ltop);
XFillRectangle(display_, window_, gc_, rx, rtop, colw, bot - rtop);
XFillRectangle(display_, window_, gc_, lx, bot - 18, (rx + colw) - lx, 18);
XSetForeground(display_, gc_, ink);
XDrawRectangle(display_, window_, gc_, lx, top, colw, bot - top);
XDrawRectangle(display_, window_, gc_, rx, top, colw, bot - top);
XDrawLine(display_, window_, gc_, lx, bot, rx + colw, bot);
for (int i = 0; i <= 10; ++i) {
int gy = top + (int)((i / 10.0) * (bot - top));
XDrawLine(display_, window_, gc_, lx - 8, gy, lx, gy);
if (i % 2 == 0) {
std::string lab = fmt(50.0 - (i / 10.0) * 100.0);
int tw = xf::text_width(stNum, lab);
xf::draw_text_at(display_, window_, gc_, stNum, lx - 12 - tw, gy + numAsc / 2, lab);
}
}
std::string val = text_ + " " + fmt(value_) + " mm";
int vw = xf::text_width(stTitle, val);
xf::draw_text_at(display_, window_, gc_, stTitle, x1_ + (width() - vw) / 2, y1_ + 26, val);
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;
int cx = (x1_ + x2_) / 2;
set_value(std::max(-50.0, std::min(50.0, (double)(x - cx) / std::max(1, width() / 2) * 50.0)));
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;
}
};