| | @@ -437,9 +437,61 @@ pub fn svg_icon(painter: &egui::Painter, rect: egui::Rect, asset: &str, color: C |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | /// 가장 긴 이름(압축해제, FONT_SMALL에서 약 48px) 양옆에 12px씩 남는 폭. |
| 440 | | -삭제: pub const TOOL_SIZE: egui::Vec2 = egui::vec2(72.0, 68.0); |
| 440 | +추가: pub const TOOL_SIZE: egui::Vec2 = egui::vec2(72.0, 60.0); |
| 441 | 441 | const TOOL_ICON: f32 = 30.0; |
| 442 | 442 | const TOOL_LABEL: f32 = FONT_SMALL; |
| 443 | +추가: /// 아이콘 그림 아래와 이름 글자 위 사이의 보이는 간격. |
| 444 | +추가: const TOOL_GAP: f32 = 6.0; |
| 445 | +추가: |
| 446 | +추가: /// SVG 폴리라인 점들의 세로 범위(24 단위 좌표). |
| 447 | +추가: fn svg_y_range(asset: &str) -> Option<(f32, f32)> { |
| 448 | +추가: asset |
| 449 | +추가: .split("points=\"") |
| 450 | +추가: .skip(1) |
| 451 | +추가: .flat_map(|element| { |
| 452 | +추가: element |
| 453 | +추가: .split('"') |
| 454 | +추가: .next() |
| 455 | +추가: .unwrap_or_default() |
| 456 | +추가: .split_whitespace() |
| 457 | +추가: .filter_map(|pair| pair.split_once(',')?.1.parse::<f32>().ok()) |
| 458 | +추가: .collect::<Vec<_>>() |
| 459 | +추가: }) |
| 460 | +추가: .fold(None, |range: Option<(f32, f32)>, y| { |
| 461 | +추가: Some(range.map_or((y, y), |(lo, hi)| (lo.min(y), hi.max(y)))) |
| 462 | +추가: }) |
| 463 | +추가: } |
| 464 | +추가: |
| 465 | +추가: /// 상단 메뉴 아이콘 세 개를 합친 그림 범위. 모든 버튼이 같은 기준을 써서 이름 높이가 맞음. |
| 466 | +추가: fn tool_icon_ink() -> (f32, f32) { |
| 467 | +추가: let scale = TOOL_ICON / 24.0; |
| 468 | +추가: let half_stroke = TOOL_ICON / 18.0 / 2.0; |
| 469 | +추가: let (lo, hi) = [OPEN, CREATE, EXTRACT] |
| 470 | +추가: .iter() |
| 471 | +추가: .filter_map(|asset| svg_y_range(asset)) |
| 472 | +추가: .fold((24.0f32, 0.0f32), |(lo, hi), (a, b)| (lo.min(a), hi.max(b))); |
| 473 | +추가: (lo * scale - half_stroke, hi * scale + half_stroke) |
| 474 | +추가: } |
| 475 | +추가: |
| 476 | +추가: /// 글자 줄 상자가 아니라 실제로 그려지는 글자 모양의 위아래 범위. |
| 477 | +추가: fn galley_ink(galley: &egui::Galley) -> (f32, f32) { |
| 478 | +추가: galley |
| 479 | +추가: .rows |
| 480 | +추가: .iter() |
| 481 | +추가: .flat_map(|row| { |
| 482 | +추가: row.glyphs |
| 483 | +추가: .iter() |
| 484 | +추가: .filter(|g| g.uv_rect.size.y > 0.0) |
| 485 | +추가: .map(move |g| { |
| 486 | +추가: let top = row.pos.y + g.pos.y + g.uv_rect.offset.y; |
| 487 | +추가: (top, top + g.uv_rect.size.y) |
| 488 | +추가: }) |
| 489 | +추가: }) |
| 490 | +추가: .fold(None, |range: Option<(f32, f32)>, (top, bottom)| { |
| 491 | +추가: Some(range.map_or((top, bottom), |(a, b)| (a.min(top), b.max(bottom)))) |
| 492 | +추가: }) |
| 493 | +추가: .unwrap_or((0.0, galley.size().y)) |
| 494 | +추가: } |
| 443 | 495 | |
| 444 | 496 | pub fn tool( |
| 445 | 497 | ui: &mut egui::Ui, |
| | @@ -470,23 +522,24 @@ pub fn tool( |
| 470 | 522 | }; |
| 471 | 523 | ui.painter().rect_filled(rect, 8, fill); |
| 472 | 524 | } |
| 473 | | -삭제: // 아이콘과 글자를 붙여 가운데에 두고, 아래쪽은 여백으로 남김. |
| 474 | | -삭제: let label_height = TOOL_LABEL * 1.3; |
| 475 | | -삭제: let gap = 4.0; |
| 476 | | -삭제: let top = rect.top() + (rect.height() - TOOL_ICON - gap - label_height) / 2.0; |
| 525 | +추가: // 아이콘 그림 위쪽부터 글자 모양 아래쪽까지를 하나로 보고 버튼 가운데에 둠. |
| 526 | +추가: // 그래서 마우스를 올려 배경이 보일 때 위아래 여백이 같음. |
| 527 | +추가: let galley = |
| 528 | +추가: ui.painter() |
| 529 | +추가: .layout_no_wrap(label.to_owned(), FontId::proportional(TOOL_LABEL), color); |
| 530 | +추가: let (icon_top, icon_bottom) = tool_icon_ink(); |
| 531 | +추가: let (text_top, text_bottom) = galley_ink(&galley); |
| 532 | +추가: let block = (icon_bottom - icon_top) + TOOL_GAP + (text_bottom - text_top); |
| 533 | +추가: let start = rect.top() + (rect.height() - block) / 2.0; |
| 477 | 534 | let icon_rect = egui::Rect::from_min_size( |
| 478 | | -삭제: egui::pos2(rect.center().x - TOOL_ICON / 2.0, top), |
| 535 | +추가: egui::pos2(rect.center().x - TOOL_ICON / 2.0, start - icon_top), |
| 479 | 536 | egui::vec2(TOOL_ICON, TOOL_ICON), |
| 480 | 537 | ); |
| 481 | 538 | svg_icon(ui.painter(), icon_rect, asset, color); |
| 482 | | -삭제: ui.painter().text( |
| 483 | | -삭제: egui::pos2( |
| 484 | | -삭제: rect.center().x, |
| 485 | | -삭제: icon_rect.bottom() + gap + label_height / 2.0, |
| 486 | | -삭제: ), |
| 487 | | -삭제: egui::Align2::CENTER_CENTER, |
| 488 | | -삭제: label, |
| 489 | | -삭제: FontId::proportional(TOOL_LABEL), |
| 539 | +추가: let text_y = start + (icon_bottom - icon_top) + TOOL_GAP - text_top; |
| 540 | +추가: ui.painter().galley( |
| 541 | +추가: egui::pos2(rect.center().x - galley.size().x / 2.0, text_y).round(), |
| 542 | +추가: galley, |
| 490 | 543 | color, |
| 491 | 544 | ); |
| 492 | 545 | focus(&response); |