Files
cnmcdee 7ff8b38128 Phase 4: per-component setters the Inspector needs + prop-table generator
8 tank widgets gain set_alarm_hi_hi/hi/lo/lo_lo (member bands_);
StockSectorComparison gains set_labels_text (pipe-separated labels);
InstControlPanel gains set_grid_rows/set_grid_cols. tools/gen_phase4_props.py
parses the C headers and emits the per-widget Inspector tables (C++ and
Rust) plus the generator capability table from one source of truth.
2026-11-30 14:03:06 +00:00

211 lines
9.6 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <sstream>
#include <string>
// InstOpenChannelLevel - Open reservoir / channel with waterline and depth scale, percent depth readout.
// Attribution: XFrame.
class InstOpenChannelLevel {
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 surface_color_ = {20, 80, 160};
Srgb hover_color_ = {255, 170, 0};
public:
InstOpenChannelLevel(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(48)) {
display_ = display; screen_ = screen; window_ = window; gc_ = gc; styles_ = styles;
x1_ = x; y1_ = y; x2_ = x + width; y2_ = y + height;
name_ = "InstOpenChannelLevel"; text_ = "InstOpenChannelLevel";
level_ = xframe::inst_clamp_level(level);
}
~InstOpenChannelLevel() {}
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)x;
int ty = y1_ + 60;
int th = height() - 140;
double f = 1.0 - (double)(y - ty) / (double)th;
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_surface_color() const { return surface_color_; }
void set_surface_color(Srgb c) { surface_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();
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());
int tx = x1_ + 60;
int ty = y1_ + 60;
int tw = width() - 200;
int th = height() - 140;
XPoint basin[4];
basin[0].x = (short)tx; basin[0].y = (short)ty;
basin[1].x = (short)(tx + tw); basin[1].y = (short)ty;
basin[2].x = (short)(tx + tw - 40); basin[2].y = (short)(ty + th);
basin[3].x = (short)(tx + 40); basin[3].y = (short)(ty + th);
XSetForeground(display_, gc_, pale);
XFillPolygon(display_, window_, gc_, basin, 4, Convex, CoordModeOrigin);
double f = level_ / 100.0;
int sy = ty + (int)(th * (1.0 - f));
int inset = (int)(40.0 * (double)(sy - ty) / (double)th);
XPoint water[4];
water[0].x = (short)(tx + inset); water[0].y = (short)sy;
water[1].x = (short)(tx + tw - inset); water[1].y = (short)sy;
water[2].x = (short)(tx + tw - 40); water[2].y = (short)(ty + th);
water[3].x = (short)(tx + 40); water[3].y = (short)(ty + th);
if (level_ > 0.0) {
XSetForeground(display_, gc_, fillc);
XFillPolygon(display_, window_, gc_, water, 4, Convex, CoordModeOrigin);
}
XSetForeground(display_, gc_, ink);
XDrawLines(display_, window_, gc_, basin, 4, CoordModeOrigin);
XDrawLine(display_, window_, gc_, tx + 40, ty + th, tx + tw - 40, ty + th);
XSetForeground(display_, gc_, styles_->get_color_rgb(surface_color_.r, surface_color_.g, surface_color_.b));
XDrawLine(display_, window_, gc_, tx + inset, sy, tx + tw - inset, sy);
for (int v = 0; v <= 100; v += 25) {
int gy = ty + th - (int)(th * v / 100.0);
XSetForeground(display_, gc_, grid);
XDrawLine(display_, window_, gc_, tx - 14, gy, tx - 4, gy);
std::string lab = std::to_string(v);
int lw = xf::text_width(st, lab);
xf::draw_text_at(display_, window_, gc_, st, tx - 18 - lw, gy + 4, lab);
}
auto mark = [&](double pct, unsigned long c, const char* lab) {
int gy = ty + th - (int)(th * pct / 100.0);
XSetForeground(display_, gc_, c);
XDrawLine(display_, window_, gc_, tx + tw + 4, gy, tx + tw + 14, gy);
xf::draw_text_at(display_, window_, gc_, st, tx + tw + 18, gy + 4, 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_) + "% depth";
xf::draw_text_at(display_, window_, gc_, st, tx + tw + 18, ty + 4, 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;
}
};