Files
lightgui/widgets/components/InstPanelPlate/InstPanelPlate.h
cnmcdee f7a27ad95a Getter/setter pass Group D (Instrumentation) batch 4/7: PanelLampBlue, PanelLampGreen, PanelLampRed, PanelLampWhite, PanelPlate, PressureGauge, PushButton, PushButtonLatching, RgbStatusLed, RockerSwitch
Expose draw colours as Srgb members with get_<name>_color/set_<name>_color
(defaults are the original literals, rendering unchanged); lens colours for
the lamp_colour() helpers; RGB channel + swatch-outline colours for
RgbStatusLed; needle line width for PressureGauge; set_on for PushButton
(on_ previously had no setter). Dead colour locals that draw() never used
are removed. Setters mutate state only, matching every existing setter in
the codebase; the host redraws after a state change.
2026-11-29 21:53:00 +00:00

143 lines
5.9 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <sstream>
#include <string>
#include <vector>
// InstPanelPlate - mounting plate with corner screws and an amber title strip.
// Attribution: XFrame.
class InstPanelPlate {
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;
// Getter/setter pass (Group D): draw colours/state exposed as properties.
// Defaults preserve the original rendering.
Srgb screw_color_ = {150, 150, 150};
Srgb screw_ring_color_ = {40, 40, 40};
Srgb white_color_ = {255, 255, 255};
Srgb ink_color_ = {40, 40, 40};
Srgb amber_color_ = {255, 176, 0};
Srgb panel_color_ = {210, 210, 205};
public:
InstPanelPlate(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_ = "InstPanelPlate"; text_ = "CONTROL PANEL";
}
~InstPanelPlate() {}
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 draw_screw(int cx, int cy) {
unsigned long g = styles_->get_color_rgb(screw_color_.r, screw_color_.g, screw_color_.b);
unsigned long ik = styles_->get_color_rgb(screw_ring_color_.r, screw_ring_color_.g, screw_ring_color_.b);
XSetForeground(display_, gc_, g);
XFillArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64);
XSetForeground(display_, gc_, ik);
XDrawArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64);
XDrawLine(display_, window_, gc_, cx - 4, cy, cx + 4, cy);
}
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_screw_color() const { return screw_color_; }
void set_screw_color(Srgb c) { screw_color_ = c; }
Srgb get_screw_ring_color() const { return screw_ring_color_; }
void set_screw_ring_color(Srgb c) { screw_ring_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_panel_color() const { return panel_color_; }
void set_panel_color(Srgb c) { panel_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 amber = styles_->get_color_rgb(amber_color_.r, amber_color_.g, amber_color_.b);
unsigned long panel = styles_->get_color_rgb(panel_color_.r, panel_color_.g, panel_color_.b);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
XSetForeground(display_, gc_, panel);
XFillRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 4, height() - 4);
XSetForeground(display_, gc_, ink);
XDrawRectangle(display_, window_, gc_, x1_ + 8, y1_ + 8, width() - 16, height() - 16);
int sx = x1_ + 10, sy = y1_ + 10, sw = width() - 20, sh = 30;
XSetForeground(display_, gc_, amber);
XFillRectangle(display_, window_, gc_, sx, sy, sw, sh);
XSetForeground(display_, gc_, ink);
XDrawRectangle(display_, window_, gc_, sx, sy, sw, sh);
int tw = xf::text_width(st, text_);
xf::draw_text_at(display_, window_, gc_, st, sx + (sw - tw) / 2, sy + 20, text_);
draw_screw(x1_ + 20, y1_ + 20);
draw_screw(x2_ - 20, y1_ + 20);
draw_screw(x1_ + 20, y2_ - 20);
draw_screw(x2_ - 20, y2_ - 20);
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;
return false;
}
};