bool button(const rect & r, const std::string & label) { fill_rect(r, { 1, 1, 1 }); fill_rect(r.shrink(2), r.contains(cursor) ? (mouse_down ? color{ 0.3f, 0.3f, 0.3f } : color{ 0.4f, 0.4f, 0.4f }) : color{ 0.5f, 0.5f, 0.5f }); glColor3f(1, 1, 1); draw_text(r.x0 + 4, r.y1 - 8, label.c_str()); return click && r.contains(cursor); }
bool slider(int id, const rect & r, double min, double max, double step, double & value, bool disable_dragger = false) { bool changed = false; const int w = r.x1 - r.x0, h = r.y1 - r.y0; double p = (w - h) * (value - min) / (max - min); if (mouse_down && clicked_id == id) { p = std::max(0.0, std::min<double>(cursor.x - clicked_offset.x - r.x0, w - h)); double new_value = min + p * (max - min) / (w - h); if (step) new_value = std::round((new_value - min) / step) * step + min; changed = new_value != value; value = new_value; p = (w - h) * (value - min) / (max - min); } const rect dragger = { int(r.x0 + p), int(r.y0), int(r.x0 + p + h), int(r.y1) }; if (click && dragger.contains(cursor) && !disable_dragger) { clicked_offset = { cursor.x - dragger.x0, cursor.y - dragger.y0 }; clicked_id = id; } fill_rect(r, { 0.5, 0.5, 0.5 }); if (!disable_dragger) fill_rect(dragger, { 1, 1, 1 }); return changed; }
void mouseup (point p) { if (box.contains (p)) { invert (); (*cb) (); } }
void vscroll(const rect & r, int client_height, int & offset) { if (r.contains(cursor)) offset -= scroll_vec.y * 20; offset = std::min(offset, client_height - (r.y1 - r.y0)); offset = std::max(offset, 0); if (client_height <= r.y1 - r.y0) return; auto bar = r; bar.x0 = bar.x1 - 10; auto dragger = bar; dragger.y0 = bar.y0 + offset * (r.y1 - r.y0) / client_height; dragger.y1 = bar.y0 + (offset + r.y1 - r.y0) * (r.y1 - r.y0) / client_height; fill_rect(bar, { 0.5, 0.5, 0.5 }); fill_rect(dragger, { 1, 1, 1 }); }
bool checkbox(const rect & r, bool & value) { bool changed = false; if (click && r.contains(cursor)) { value = !value; changed = true; } fill_rect(r, { 1, 1, 1 }); fill_rect(r.shrink(1), { 0.5, 0.5, 0.5 }); if (value) fill_rect(r.shrink(3), { 1, 1, 1 }); return changed; }
void mouseup (point p) { if (bounds.contains (p)) { select (); dirty (); } }