Expose draw colours as Srgb members with get_<name>_color/set_<name>_color (defaults are the original literals, rendering unchanged). 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.
152 lines
6.8 KiB
C++
152 lines
6.8 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
|
|
#include <cmath>
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
// InstSlideSwitch - Slide switch: knob left is off, right is on. Click toggles; drag the knob across.
|
|
// Attribution: XFrame.
|
|
class InstSlideSwitch {
|
|
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;
|
|
bool on_ = false;
|
|
bool dragging_ = false;
|
|
|
|
// Getter/setter pass (Group D): draw colours/state exposed as properties.
|
|
// Defaults preserve the original rendering.
|
|
Srgb white_color_ = {255, 255, 255};
|
|
Srgb panel_color_ = {58, 58, 62};
|
|
Srgb bezel_color_ = {38, 38, 42};
|
|
Srgb rocker_color_ = {190, 190, 195};
|
|
Srgb dark_color_ = {40, 40, 40};
|
|
Srgb amber_color_ = {255, 176, 0};
|
|
Srgb green_color_ = {0, 180, 0};
|
|
public:
|
|
std::function<void()> on_click;
|
|
InstSlideSwitch(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_ = "InstSlideSwitch"; text_ = "Slide";
|
|
}
|
|
~InstSlideSwitch() {}
|
|
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 toggle() { on_ = !on_; if (on_click) on_click(); }
|
|
void set_on(bool v) { if (on_ != v) { on_ = v; if (on_click) on_click(); } }
|
|
bool is_on() { return on_; }
|
|
xframe::InstSwitchState get_state() { return on_ ? xframe::InstSwitchState::On : xframe::InstSwitchState::Off; }
|
|
|
|
// 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_panel_color() const { return panel_color_; }
|
|
void set_panel_color(Srgb c) { panel_color_ = c; }
|
|
Srgb get_bezel_color() const { return bezel_color_; }
|
|
void set_bezel_color(Srgb c) { bezel_color_ = c; }
|
|
Srgb get_rocker_color() const { return rocker_color_; }
|
|
void set_rocker_color(Srgb c) { rocker_color_ = c; }
|
|
Srgb get_dark_color() const { return dark_color_; }
|
|
void set_dark_color(Srgb c) { dark_color_ = c; }
|
|
Srgb get_amber_color() const { return amber_color_; }
|
|
void set_amber_color(Srgb c) { amber_color_ = c; }
|
|
Srgb get_green_color() const { return green_color_; }
|
|
void set_green_color(Srgb c) { green_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 panel = styles_->get_color_rgb(panel_color_.r, panel_color_.g, panel_color_.b);
|
|
unsigned long bezel = styles_->get_color_rgb(bezel_color_.r, bezel_color_.g, bezel_color_.b);
|
|
unsigned long rocker = styles_->get_color_rgb(rocker_color_.r, rocker_color_.g, rocker_color_.b);
|
|
unsigned long dark = styles_->get_color_rgb(dark_color_.r, dark_color_.g, dark_color_.b);
|
|
unsigned long amber = styles_->get_color_rgb(amber_color_.r, amber_color_.g, amber_color_.b);
|
|
unsigned long green = styles_->get_color_rgb(green_color_.r, green_color_.g, green_color_.b);
|
|
XSetForeground(display_, gc_, white);
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
int cx = (x1_ + x2_) / 2;
|
|
int cy = (y1_ + y2_) / 2;
|
|
int tw = 260, th = 64;
|
|
int tx = cx - tw / 2, ty = cy - th / 2;
|
|
XSetForeground(display_, gc_, bezel);
|
|
XFillRectangle(display_, window_, gc_, tx - 12, ty - 12, tw + 24, th + 24);
|
|
XSetForeground(display_, gc_, panel);
|
|
XFillRectangle(display_, window_, gc_, tx, ty, tw, th);
|
|
int kw = 90;
|
|
int kx = on_ ? tx + tw - kw - 6 : tx + 6;
|
|
XSetForeground(display_, gc_, on_ ? green : rocker);
|
|
XFillRectangle(display_, window_, gc_, kx, ty + 6, kw, th - 12);
|
|
XSetForeground(display_, gc_, dark);
|
|
XDrawRectangle(display_, window_, gc_, kx, ty + 6, kw, th - 12);
|
|
XSetForeground(display_, gc_, amber);
|
|
XDrawLine(display_, window_, gc_, kx + kw / 2 - 14, ty + th / 2, kx + kw / 2 + 14, ty + th / 2);
|
|
xf::draw_text_centered(display_, window_, gc_, st, tx, ty - 34, tx + 90, ty - 10, "OFF");
|
|
xf::draw_text_centered(display_, window_, gc_, st, tx + tw - 90, ty - 34, tx + tw, ty - 10, "ON");
|
|
xf::draw_text_centered(display_, window_, gc_, st, x1_, y2_ - 28, x2_, y2_ - 6, text_);
|
|
XSetForeground(display_, gc_, st.lineColor_);
|
|
XDrawRectangle(display_, window_, gc_, x1_, y1_, width(), height());
|
|
if (focused_) {
|
|
XSetForeground(display_, gc_, amber);
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 2, y1_ + 2, width() - 5, height() - 5);
|
|
}
|
|
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; dragging_ = true; toggle(); 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; dragging_ = false; return true; }
|
|
return false;
|
|
}
|
|
bool on_motion(int x, int y) {
|
|
hover_ = contains(x, y);
|
|
if (dragging_) {
|
|
int cx = (x1_ + x2_) / 2;
|
|
bool want = x > cx;
|
|
if (want != on_) { on_ = want; if (on_click) on_click(); }
|
|
return true;
|
|
}
|
|
return hover_;
|
|
}
|
|
bool on_key(KeySym key, const char* str) {
|
|
(void)str;
|
|
(void)key;
|
|
if (!focused_ || !enabled_) return false;
|
|
if (key == XK_space || key == XK_Return || key == XK_KP_Enter) { toggle(); return true; }
|
|
if (key == XK_Right) { if (!on_) toggle(); return true; }
|
|
if (key == XK_Left) { if (on_) toggle(); return true; }
|
|
return false;
|
|
}
|
|
|
|
};
|