Files
Muse 2b3d82a726 Inst*: proportions fix (audit Dec 2026) — lamps + LEDs
LED labels 28 px with leads ending above them; panel-lamp discs
shrunk so 17 px labels clear them; RGB swatches 24 px with white
letters; Beacon dome clear of bar, FLASH/BEACON 16 px inset;
StackLight tier letters white/light-grey 24 px, base raised so the
STACK title clears it.
2026-12-05 21:32:07 +00:00

153 lines
7.0 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <string>
#include <utility>
#include <vector>
// // InstPanelLampRed - round bezel panel lamp, RED: on/off + brightness 0-100. Click or Space toggles; the lens brightens with brightness.
// Attribution: XFrame.
class InstPanelLampRed {
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 hovered_ = false;
bool pressed_ = false;
xframe::InstLamp lamp_;
// Getter/setter pass (Group D): draw colours/state exposed as properties.
// Defaults preserve the original rendering.
Srgb white_color_ = {255, 255, 255};
Srgb bezel_color_ = {70, 70, 70};
Srgb ring_color_ = {160, 160, 160};
Srgb dark_color_ = {40, 40, 40};
Srgb hover_color_ = {0, 120, 200};
Srgb lens_color_ = {220, 30, 30};
public:
std::function<void()> on_click;
InstPanelLampRed(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_ = "InstPanelLampRed"; text_ = "RED";
}
~InstPanelLampRed() {}
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; }
bool is_pressed() const { return pressed_; }
bool is_hovered() const { return hovered_; }
void set_on(bool on) { lamp_.on = on; }
bool is_on() const { return lamp_.on; }
void toggle() { lamp_.on = !lamp_.on; }
void set_brightness(int b) { lamp_.brightness = (int)xframe::inst_clamp_level((double)b); }
int get_brightness() const { return lamp_.brightness; }
xframe::InstLamp lamp_state() const { return lamp_; }
void set_lamp(const xframe::InstLamp& s) { lamp_.on = s.on; lamp_.brightness = (int)xframe::inst_clamp_level((double)s.brightness); }
unsigned long lamp_colour(int r, int g, int b) {
double f = lamp_.on ? (0.08 + 0.92 * lamp_.brightness / 100.0) : 0.10;
return styles_->get_color_rgb((unsigned char)(r * f), (unsigned char)(g * f), (unsigned char)(b * f));
}
// 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_bezel_color() const { return bezel_color_; }
void set_bezel_color(Srgb c) { bezel_color_ = c; }
Srgb get_ring_color() const { return ring_color_; }
void set_ring_color(Srgb c) { ring_color_ = c; }
Srgb get_dark_color() const { return dark_color_; }
void set_dark_color(Srgb c) { dark_color_ = c; }
Srgb get_hover_color() const { return hover_color_; }
void set_hover_color(Srgb c) { hover_color_ = c; }
Srgb get_lens_color() const { return lens_color_; }
void set_lens_color(Srgb c) { lens_color_ = c; }
void draw() {
if (!visible_) return;
SStyle st = style();
// Audit: label 16-18 px bold, 10 px below the disc.
SStyle stLab = st; stLab.font_ = xframe::inst_cap_font_prop(display_, 17);
unsigned long white = styles_->get_color_rgb(white_color_.r, white_color_.g, white_color_.b);
unsigned long bezel = styles_->get_color_rgb(bezel_color_.r, bezel_color_.g, bezel_color_.b);
unsigned long ring = styles_->get_color_rgb(ring_color_.r, ring_color_.g, ring_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 cx = (x1_ + x2_) / 2;
int cy = (y1_ + y2_) / 2 - 8;
int R = std::max(6, std::min(width(), height()) / 2 - 30); // audit: disc shrunk, label clear below it
int lr = std::max(4, R - 8);
XSetForeground(display_, gc_, pressed_ ? dark : bezel);
XFillArc(display_, window_, gc_, cx - R, cy - R, 2 * R, 2 * R, 0, 360 * 64);
XSetForeground(display_, gc_, ring);
XFillArc(display_, window_, gc_, cx - R + 3, cy - R + 3, 2 * (R - 3), 2 * (R - 3), 0, 360 * 64);
XSetForeground(display_, gc_, lamp_colour(lens_color_.r, lens_color_.g, lens_color_.b));
XFillArc(display_, window_, gc_, cx - lr, cy - lr, 2 * lr, 2 * lr, 0, 360 * 64);
if (lamp_.on && lamp_.brightness >= 50) {
XSetForeground(display_, gc_, white);
int hr = std::max(2, lr / 4);
XFillArc(display_, window_, gc_, cx - lr / 2 - hr, cy - lr / 2 - hr, 2 * hr, 2 * hr, 0, 360 * 64);
}
if (lamp_.on && lamp_.brightness >= 80) {
XSetForeground(display_, gc_, lamp_colour(lens_color_.r, lens_color_.g, lens_color_.b));
XDrawArc(display_, window_, gc_, cx - lr - 4, cy - lr - 4, 2 * (lr + 4), 2 * (lr + 4), 0, 360 * 64);
}
int tw = xf::text_width(stLab, text_);
xf::draw_text_at(display_, window_, gc_, stLab, cx - tw / 2, y2_ - 10, text_);
if (hovered_ || focused_) {
XSetForeground(display_, gc_, styles_->get_color_rgb(hover_color_.r, hover_color_.g, hover_color_.b));
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, width() - 2, height() - 2);
}
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;
focused_ = true;
if (button == 1) { pressed_ = true; toggle(); 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) {
hovered_ = contains(x, y);
return hovered_;
}
bool on_key(KeySym key, const char* str) {
(void)str;
if (!focused_ || !enabled_) return false;
if (key == XK_space) { toggle(); if (on_click) on_click(); return true; }
return false;
}
};