Explorar el Código

Give buttons a raised bevel and text fields an inset edge

Johannes Zellner hace 1 semana
padre
commit
52a09e7bc6
Se han modificado 2 ficheros con 66 adiciones y 4 borrados
  1. 1 1
      gallery.c
  2. 65 3
      ngui.c

+ 1 - 1
gallery.c

@@ -766,7 +766,7 @@ static void draw_buttons(struct ngui *g, struct ngui_rect card)
 	ngui_text_ellipsis(g, rect_make(x, card.y + 72, card.w - 2 * CARD_PAD,
 					16),
 			   x, card.y + 72,
-			   "Buttons fire on release inside; hover paints an accent frame.",
+			   "Buttons fire on release inside; hover lights the raised edge.",
 			   g->theme.text_dim);
 	ngui_text_ellipsis(g, rect_make(x, card.y + 94, card.w - 2 * CARD_PAD,
 					16),

+ 65 - 3
ngui.c

@@ -494,6 +494,61 @@ void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
 	}
 }
 
+/* Brighten (step > 0) or darken (step < 0) an RGB565 color for the
+ * one-pixel bevel.  Green has six bits, red and blue five, so the green
+ * component moves twice as far to keep the shade visually even. */
+static unsigned short shade565(unsigned short c, int step)
+{
+	int r = (int)((c >> 11) & 0x1F) + step;
+	int g = (int)((c >> 5) & 0x3F) + step * 2;
+	int b = (int)(c & 0x1F) + step;
+
+	if (r < 0)
+		r = 0;
+	if (r > 0x1F)
+		r = 0x1F;
+	if (g < 0)
+		g = 0;
+	if (g > 0x3F)
+		g = 0x3F;
+	if (b < 0)
+		b = 0;
+	if (b > 0x1F)
+		b = 0x1F;
+	return (unsigned short)((r << 11) | (g << 5) | b);
+}
+
+/* A one-pixel two-tone edge: `tl` along the top and left, `br` along the
+ * bottom and right.  A raised widget lights its top/left and shades its
+ * bottom/right; an inset one (a text field) swaps the tones. */
+static void bevel_rect(struct ngui *ui, struct ngui_rect r,
+		       unsigned short tl, unsigned short br)
+{
+	if (r.w <= 0 || r.h <= 0)
+		return;
+	ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), tl);
+	ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), br);
+	if (r.h > 2) {
+		ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), tl);
+		ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
+					     r.h - 2), br);
+	}
+}
+
+/* Raised button edge: lit on the top/left, shaded on the bottom/right.  A
+ * pressed button inverts the bevel, so it reads as pushed in.  The hovered
+ * button keeps its accent on the lit edge. */
+static void button_bevel(struct ngui *ui, struct ngui_rect r,
+			 unsigned short bg, int hot, int down)
+{
+	if (down)
+		bevel_rect(ui, r, shade565(bg, -4), shade565(bg, 4));
+	else if (hot)
+		bevel_rect(ui, r, ui->theme.accent, shade565(bg, -4));
+	else
+		bevel_rect(ui, r, shade565(bg, 4), shade565(bg, -4));
+}
+
 static int ngui_isqrt(int v)
 {
 	int r = 0, bit = 1 << 30;
@@ -1240,7 +1295,7 @@ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	else
 		bg = ui->theme.button;
 	ngui_fill_rect(ui, r, bg);
-	ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
+	button_bevel(ui, r, bg, hot, ui->active == id);
 
 	if (label && label[0]) {
 		tw = (int)ngui_text_width_ui(ui, label);
@@ -1356,7 +1411,7 @@ int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	else
 		bg = ui->theme.button;
 	ngui_fill_rect(ui, r, bg);
-	ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
+	button_bevel(ui, r, bg, hot, ui->active == id);
 	ngui_icon(ui, r, icon, ui->theme.text);
 	if (hot)
 		ui->hot = id;
@@ -1777,7 +1832,14 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	}
 
 	ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
-	ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
+	/* Inset edge: the shadow sits on the top/left, the light on the
+	 * bottom/right.  A focused field keeps the shape in accent blue. */
+	if (focused)
+		bevel_rect(ui, r, ui->theme.accent,
+			   shade565(ui->theme.accent, 4));
+	else
+		bevel_rect(ui, r, shade565(ui->theme.panel, -4),
+			   shade565(ui->theme.panel, 4));
 
 	if (!in->disabled)
 		input_scroll_to_cursor(ui, in, avail);