diff options
author | Mustafa Quraish <mustafaq9@gmail.com> | 2021-09-05 20:36:10 -0400 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-09-06 14:01:24 +0430 |
commit | c1ede97543a6374c07af8dbafb3191b9457ef3b1 (patch) | |
tree | bd2a7c435c98cc02eafe30c6e0caff61ec66db57 /Userland/Applications/FontEditor | |
parent | 63523d383634f9f08b69d6ca977d51180d1c9944 (diff) | |
download | serenity-c1ede97543a6374c07af8dbafb3191b9457ef3b1.zip |
FontEditor: Don't loop over all glyphs to find selected one
Previously we would loop over all glyphs in the GlyphMap, compute
their rects, and then test to see if the mouse click position was
inside that rect. This is silly since each element in the glyph
map for a particular font is the same size, and we can just do
some coordinate manipulation to get the index directly.
Diffstat (limited to 'Userland/Applications/FontEditor')
-rw-r--r-- | Userland/Applications/FontEditor/GlyphMapWidget.cpp | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Userland/Applications/FontEditor/GlyphMapWidget.cpp b/Userland/Applications/FontEditor/GlyphMapWidget.cpp index a30b34b30a..d430d1558a 100644 --- a/Userland/Applications/FontEditor/GlyphMapWidget.cpp +++ b/Userland/Applications/FontEditor/GlyphMapWidget.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -111,12 +112,13 @@ void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event) { GUI::Frame::mousedown_event(event); - // FIXME: This is a silly loop. - for (int glyph = 0; glyph < m_glyph_count; ++glyph) { - if (get_outer_rect(glyph).contains(event.position())) { - set_selected_glyph(glyph); - break; - } + Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() }; + auto map_position = event.position() - map_offset; + auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing)); + auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing)); + auto glyph = row * columns() + col; + if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count) { + set_selected_glyph(glyph); } } |