Просмотр исходного кода

ngui: share one corner radius and fix its rounding

Cards, tiles, list rows and dialogs each picked their own radius (6 to
10 px).  The corner scanline inset also used a half-resolution distance,
so a corner was taller than it was wide.

Add NGUI_RADIUS for the shared radius and compute the inset from pixel
centres, then use the constant in disks and the control panel.
Johannes Zellner 2 дней назад
Родитель
Сommit
828cba05ea
4 измененных файлов с 33 добавлено и 15 удалено
  1. 5 2
      user/apps/disks/ucmd_disks.c
  2. 10 5
      user/apps/netmenu/ucmd_netmenu.c
  3. 15 8
      user/lib/ngui/ngui.c
  4. 3 0
      user/lib/ngui/ngui.h

+ 5 - 2
user/apps/disks/ucmd_disks.c

@@ -1015,9 +1015,12 @@ static struct ngui_rect dialog_rect(int w, int h)
 	r.x = (ui.w - r.w) / 2;
 	r.x = (ui.w - r.w) / 2;
 	r.y = (ui.h - r.h) / 2;
 	r.y = (ui.h - r.h) / 2;
 	ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1,
 	ngui_fill_round_rect(&ui, (struct ngui_rect){ r.x - 1, r.y - 1,
-				  r.w + 2, r.h + 2 }, 9, 9, 9, 9,
+				  r.w + 2, r.h + 2 },
+			     NGUI_RADIUS + 1, NGUI_RADIUS + 1,
+			     NGUI_RADIUS + 1, NGUI_RADIUS + 1,
 			     ui.theme.border);
 			     ui.theme.border);
-	ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
+	ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
 	return r;
 	return r;
 }
 }
 
 

+ 10 - 5
user/apps/netmenu/ucmd_netmenu.c

@@ -1054,8 +1054,10 @@ static void draw_status_card(struct ngui_rect r)
 	else
 	else
 		dot_color = ui.theme.success;
 		dot_color = ui.theme.success;
 
 
-	ngui_fill_round_rect(&ui, r, 8, 8, 8, 8, ui.theme.panel);
-	ngui_frame_round(&ui, r, 8, 8, 8, 8, 1, ui.theme.panel_alt);
+	ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
+	ngui_frame_round(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			 NGUI_RADIUS, NGUI_RADIUS, 1, ui.theme.panel_alt);
 	ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color);
 	ngui_fill_round_rect(&ui, dot, 5, 5, 5, 5, dot_color);
 
 
 	p = line;
 	p = line;
@@ -1208,7 +1210,8 @@ static void draw_summary(struct ngui_rect r)
 		dot = ui.theme.warning;
 		dot = ui.theme.warning;
 	}
 	}
 
 
-	ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel);
+	ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
 	ngui_fill_round_rect(&ui,
 	ngui_fill_round_rect(&ui,
 		(struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 },
 		(struct ngui_rect){ r.x + 14, r.y + 23, 10, 10 },
 		5, 5, 5, 5, dot);
 		5, 5, 5, 5, dot);
@@ -1245,7 +1248,8 @@ static void draw_brightness(struct ngui_rect r)
 	int v = brightness;
 	int v = brightness;
 	int tw;
 	int tw;
 
 
-	ngui_fill_round_rect(&ui, r, 10, 10, 10, 10, ui.theme.panel);
+	ngui_fill_round_rect(&ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
 	ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10,
 	ngui_text_px(&ui, r.x + 14, r.y + 6, "Brightness", 10,
 		     ui.theme.text, 14, 1, 0, 0);
 		     ui.theme.text, 14, 1, 0, 0);
 	p = put_dec(b, (unsigned int)(v < 0 ? 0 : v));
 	p = put_dec(b, (unsigned int)(v < 0 ? 0 : v));
@@ -1853,7 +1857,8 @@ static void draw_connect(void)
 	 * the popover footer. */
 	 * the popover footer. */
 	log = body;
 	log = body;
 	log.h -= 38;
 	log.h -= 38;
-	ngui_fill_round_rect(&ui, log, 6, 6, 6, 6, ui.theme.panel);
+	ngui_fill_round_rect(&ui, log, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, ui.theme.panel);
 	ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8,
 	ngui_push_clip(&ui, (struct ngui_rect){ log.x + 8, log.y + 8,
 						log.w - 16, log.h - 16 });
 						log.w - 16, log.h - 16 });
 
 

+ 15 - 8
user/lib/ngui/ngui.c

@@ -598,23 +598,28 @@ static int ngui_isqrt(int v)
 	return r;
 	return r;
 }
 }
 
 
-/* Horizontal inset of one scanline of a corner with the given radius. */
+/* Horizontal inset of one scanline of a corner with the given radius.
+ * Samples the pixel centre, so a corner is as wide as it is tall. */
 static int round_inset(int y, int h, int r)
 static int round_inset(int y, int h, int r)
 {
 {
-	int dy, d2;
+	int dy2, d2;
 
 
 	if (r <= 0)
 	if (r <= 0)
 		return 0;
 		return 0;
 	if (y < r)
 	if (y < r)
-		dy = r - y - 1;
+		dy2 = 2 * r - 2 * y - 1;
 	else if (y >= h - r)
 	else if (y >= h - r)
-		dy = y - (h - r);
+		dy2 = 2 * (y - (h - r)) + 1;
 	else
 	else
 		return 0;
 		return 0;
-	d2 = r * r - dy * dy;
+	/* d2 = (2r)^2 - dy2^2 = 4 * (r^2 - dy^2).  The inset is
+	 * r - floor(sqrt(d2) / 2 + 0.5), i.e. r - (isqrt(d2) + 1) / 2.
+	 * d2 is 3 or 7 mod 8, never a perfect square, so the rounded
+	 * square root is exact. */
+	d2 = 4 * r * r - dy2 * dy2;
 	if (d2 < 0)
 	if (d2 < 0)
 		d2 = 0;
 		d2 = 0;
-	return r - ngui_isqrt(d2);
+	return r - (ngui_isqrt(d2) + 1) / 2;
 }
 }
 
 
 void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
 void ngui_fill_round_rect(struct ngui *ui, struct ngui_rect r,
@@ -1546,7 +1551,8 @@ static int ngui_list_row_common(struct ngui *ui, unsigned int id,
 	else if (hot || ui->active == id)
 	else if (hot || ui->active == id)
 		bg = ui->theme.panel_alt;
 		bg = ui->theme.panel_alt;
 	if (bg)
 	if (bg)
-		ngui_fill_round_rect(ui, r, 6, 6, 6, 6, bg);
+		ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
+				     NGUI_RADIUS, NGUI_RADIUS, bg);
 	title_h = ngui_text_line_height(ui, ui->font_px, 0);
 	title_h = ngui_text_line_height(ui, ui->font_px, 0);
 	fg = ui->theme.text;
 	fg = ui->theme.text;
 	sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
 	sub_fg = row->selected ? ui->theme.text : ui->theme.text_dim;
@@ -1662,7 +1668,8 @@ int ngui_tile(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		bg = ui->theme.button_hot;
 		bg = ui->theme.button_hot;
 	else
 	else
 		bg = ui->theme.panel_alt;
 		bg = ui->theme.panel_alt;
-	ngui_fill_round_rect(ui, r, 10, 10, 10, 10, bg);
+	ngui_fill_round_rect(ui, r, NGUI_RADIUS, NGUI_RADIUS,
+			     NGUI_RADIUS, NGUI_RADIUS, bg);
 	fg = ui->theme.text;
 	fg = ui->theme.text;
 	sub_fg = on ? ui->theme.text : ui->theme.text_dim;
 	sub_fg = on ? ui->theme.text : ui->theme.text_dim;
 
 

+ 3 - 0
user/lib/ngui/ngui.h

@@ -13,6 +13,9 @@
 #define NGUI_INPUT_MAX 512
 #define NGUI_INPUT_MAX 512
 #define NGUI_MOTION_REDRAW_MS 33
 #define NGUI_MOTION_REDRAW_MS 33
 
 
+/* Corner radius shared by cards, tiles, list rows and dialogs. */
+#define NGUI_RADIUS 6
+
 #define NGUI_INPUT_CHANGED 1
 #define NGUI_INPUT_CHANGED 1
 #define NGUI_INPUT_SUBMIT  2
 #define NGUI_INPUT_SUBMIT  2
 #define NGUI_INPUT_CANCEL  4
 #define NGUI_INPUT_CANCEL  4