mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 16:35:08 +00:00
FontEditor: Fix focus and implement keyboard navigation in the glyph map
This commit is contained in:
parent
6fcf4e48f8
commit
3d32c3352b
2 changed files with 70 additions and 20 deletions
|
@ -87,33 +87,79 @@ void GlyphMapWidget::paint_event(GUI::PaintEvent& event)
|
||||||
painter.set_font(font());
|
painter.set_font(font());
|
||||||
painter.fill_rect(frame_inner_rect(), palette().base());
|
painter.fill_rect(frame_inner_rect(), palette().base());
|
||||||
|
|
||||||
u8 glyph = 0;
|
for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
|
||||||
|
Gfx::Rect outer_rect = get_outer_rect(glyph);
|
||||||
for (int row = 0; row < rows(); ++row) {
|
Gfx::Rect inner_rect(
|
||||||
for (int column = 0; column < columns(); ++column, ++glyph) {
|
outer_rect.x() + m_horizontal_spacing / 2,
|
||||||
Gfx::Rect outer_rect = get_outer_rect(glyph);
|
outer_rect.y() + m_vertical_spacing / 2,
|
||||||
Gfx::Rect inner_rect(
|
font().max_glyph_width(),
|
||||||
outer_rect.x() + m_horizontal_spacing / 2,
|
font().glyph_height());
|
||||||
outer_rect.y() + m_vertical_spacing / 2,
|
if (glyph == m_selected_glyph) {
|
||||||
font().max_glyph_width(),
|
painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
|
||||||
font().glyph_height());
|
painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
|
||||||
if (glyph == m_selected_glyph) {
|
} else {
|
||||||
painter.fill_rect(outer_rect, palette().selection());
|
painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
|
||||||
painter.draw_glyph(inner_rect.location(), glyph, palette().selection_text());
|
|
||||||
} else {
|
|
||||||
painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event)
|
void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event)
|
||||||
{
|
{
|
||||||
|
GUI::Frame::mousedown_event(event);
|
||||||
|
|
||||||
// FIXME: This is a silly loop.
|
// FIXME: This is a silly loop.
|
||||||
for (unsigned glyph = 0; glyph < 256; ++glyph) {
|
for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
|
||||||
if (get_outer_rect(glyph).contains(event.position())) {
|
if (get_outer_rect(glyph).contains(event.position())) {
|
||||||
set_selected_glyph(glyph);
|
set_selected_glyph(glyph);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GlyphMapWidget::keydown_event(GUI::KeyEvent& event)
|
||||||
|
{
|
||||||
|
GUI::Frame::keydown_event(event);
|
||||||
|
|
||||||
|
if (event.key() == KeyCode::Key_Up) {
|
||||||
|
if (selected_glyph() >= m_columns) {
|
||||||
|
set_selected_glyph(selected_glyph() - m_columns);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.key() == KeyCode::Key_Down) {
|
||||||
|
if (selected_glyph() < m_glyph_count - m_columns) {
|
||||||
|
set_selected_glyph(selected_glyph() + m_columns);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.key() == KeyCode::Key_Left) {
|
||||||
|
if (selected_glyph() > 0) {
|
||||||
|
set_selected_glyph(selected_glyph() - 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.key() == KeyCode::Key_Right) {
|
||||||
|
if (selected_glyph() < m_glyph_count - 1) {
|
||||||
|
set_selected_glyph(selected_glyph() + 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (event.ctrl() && event.key() == KeyCode::Key_Home) {
|
||||||
|
set_selected_glyph(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.ctrl() && event.key() == KeyCode::Key_End) {
|
||||||
|
set_selected_glyph(m_glyph_count - 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
|
||||||
|
set_selected_glyph(selected_glyph() / m_columns * m_columns);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!event.ctrl() && event.key() == KeyCode::Key_End) {
|
||||||
|
int new_selection = selected_glyph() / m_columns * m_columns + (m_columns - 1);
|
||||||
|
new_selection = clamp(new_selection, 0, m_glyph_count - 1);
|
||||||
|
set_selected_glyph(new_selection);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
|
#include <AK/StdLibExtras.h>
|
||||||
#include <LibGUI/Frame.h>
|
#include <LibGUI/Frame.h>
|
||||||
|
|
||||||
class GlyphMapWidget final : public GUI::Frame {
|
class GlyphMapWidget final : public GUI::Frame {
|
||||||
|
@ -37,8 +38,8 @@ public:
|
||||||
u8 selected_glyph() const { return m_selected_glyph; }
|
u8 selected_glyph() const { return m_selected_glyph; }
|
||||||
void set_selected_glyph(u8);
|
void set_selected_glyph(u8);
|
||||||
|
|
||||||
int rows() const { return m_rows; }
|
int rows() const { return ceil_div(m_glyph_count, m_columns); }
|
||||||
int columns() const { return 256 / m_rows; }
|
int columns() const { return m_columns; }
|
||||||
|
|
||||||
int preferred_width() const;
|
int preferred_width() const;
|
||||||
int preferred_height() const;
|
int preferred_height() const;
|
||||||
|
@ -52,13 +53,16 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit GlyphMapWidget(Gfx::Font&);
|
explicit GlyphMapWidget(Gfx::Font&);
|
||||||
|
virtual bool accepts_focus() const override { return true; }
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||||
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
|
|
||||||
Gfx::Rect get_outer_rect(u8 glyph) const;
|
Gfx::Rect get_outer_rect(u8 glyph) const;
|
||||||
|
|
||||||
RefPtr<Gfx::Font> m_font;
|
RefPtr<Gfx::Font> m_font;
|
||||||
int m_rows { 8 };
|
int m_glyph_count { 256 };
|
||||||
|
int m_columns { 32 };
|
||||||
int m_horizontal_spacing { 2 };
|
int m_horizontal_spacing { 2 };
|
||||||
int m_vertical_spacing { 2 };
|
int m_vertical_spacing { 2 };
|
||||||
u8 m_selected_glyph { 0 };
|
u8 m_selected_glyph { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue