Files
lightgui/widgets/components/InstLedRed/InstLedRed.h
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

151 lines
6.8 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <functional>
#include <string>
#include <utility>
#include <vector>
// // InstLedRed - round bezel panel lamp, RED: on/off + brightness 0-100. Click or Space toggles; the lens brightens with brightness.
// Attribution: XFrame.
class InstLedRed {
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 dark_color_ = {40, 40, 40};
Srgb lead_color_ = {120, 120, 120};
Srgb hover_color_ = {0, 120, 200};
Srgb lens_color_ = {220, 30, 30};
public:
std::function<void()> on_click;
InstLedRed(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_ = "InstLedRed"; text_ = "RED";
}
~InstLedRed() {}
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_dark_color() const { return dark_color_; }
void set_dark_color(Srgb c) { dark_color_ = c; }
Srgb get_lead_color() const { return lead_color_; }
void set_lead_color(Srgb c) { lead_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 28 px bold, inset above the leads' end.
SStyle stLab = st; stLab.font_ = xframe::inst_cap_font_prop(display_, 28);
unsigned long white = styles_->get_color_rgb(white_color_.r, white_color_.g, white_color_.b);
unsigned long dark = styles_->get_color_rgb(dark_color_.r, dark_color_.g, dark_color_.b);
unsigned long lead = styles_->get_color_rgb(lead_color_.r, lead_color_.g, lead_color_.b);
XSetForeground(display_, gc_, white);
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
int cx = (x1_ + x2_) / 2;
int cy = (y1_ + y2_) / 2 - 16;
int r = std::max(8, std::min(width(), height()) / 4);
XSetForeground(display_, gc_, dark);
XFillRectangle(display_, window_, gc_, cx - r - 8, cy - r - 8, 2 * r + 16, 2 * r + 16);
int lead_top = cy + r + 8;
int lead_h = std::max(1, y2_ - 44 - lead_top); // leads end above the label
XSetForeground(display_, gc_, lead);
XFillRectangle(display_, window_, gc_, cx - 10, lead_top, 4, lead_h);
XFillRectangle(display_, window_, gc_, cx + 6, lead_top, 4, lead_h);
XSetForeground(display_, gc_, lamp_colour(lens_color_.r, lens_color_.g, lens_color_.b));
XFillArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
if (lamp_.on && lamp_.brightness >= 50) {
XSetForeground(display_, gc_, white);
int hr = std::max(2, r / 4);
XFillArc(display_, window_, gc_, cx - r / 2 - hr, cy - r / 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 - r - 4, cy - r - 4, 2 * (r + 4), 2 * (r + 4), 0, 360 * 64);
}
int tw = xf::text_width(stLab, text_);
xf::draw_text_at(display_, window_, gc_, stLab, cx - tw / 2, y2_ - 8, 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;
}
};