Skip to content

Commit 7314f86

Browse files
committed
UX improvements
1 parent 28c59a5 commit 7314f86

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

src/image_viewer.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,59 @@ void ImageViewer::init()
2121
filename_, screen.w, screen.h) };
2222
ok_ = (image_ != nullptr);
2323
if (!ok_) return;
24+
25+
const auto &fonts = CResourceManager::instance().getFonts();
26+
bool show_title = image_->h <= screen.actual_h - Y_LIST * screen.ppu_y;
27+
28+
// Create background image
29+
background_ = SDLSurfaceUniquePtr { SDL_utils::createImage(screen.actual_w,
30+
screen.actual_h, SDL_MapRGB(screen.surface->format, COLOR_BG_1)) };
31+
if (show_title) {
32+
SDL_Rect rect = SDL_utils::Rect(
33+
0, 0, screen.actual_w, HEADER_H * screen.ppu_y);
34+
SDL_FillRect(background_.get(), &rect,
35+
SDL_MapRGB(background_->format, COLOR_BORDER));
36+
}
37+
// Print title
38+
if (show_title) {
39+
SDLSurfaceUniquePtr tmp { SDL_utils::renderText(
40+
fonts, filename_, Globals::g_colorTextTitle, { COLOR_TITLE_BG }) };
41+
if (tmp->w > background_->w - 2 * VIEWER_MARGIN) {
42+
SDL_Rect rect;
43+
rect.x = tmp->w - (background_->w - 2 * VIEWER_MARGIN);
44+
rect.y = 0;
45+
rect.w = background_->w - 2 * VIEWER_MARGIN;
46+
rect.h = tmp->h;
47+
SDL_utils::applyPpuScaledSurface(VIEWER_MARGIN * screen.ppu_x,
48+
HEADER_PADDING_TOP * screen.ppu_y, tmp.get(), background_.get(),
49+
&rect);
50+
} else {
51+
SDL_utils::applyPpuScaledSurface(VIEWER_MARGIN * screen.ppu_x,
52+
HEADER_PADDING_TOP * screen.ppu_y, tmp.get(),
53+
background_.get());
54+
}
55+
}
56+
57+
// Transparency grid background.
58+
constexpr int kTransparentBgRectSize = 10;
59+
const Uint32 colors[2] = {
60+
SDL_MapRGB(background_->format, COLOR_BG_1),
61+
SDL_MapRGB(background_->format, COLOR_BG_2),
62+
};
63+
int j = 0;
64+
const int rect_w = static_cast<int>(kTransparentBgRectSize * screen.ppu_x);
65+
const int rect_h = static_cast<int>(kTransparentBgRectSize * screen.ppu_y);
66+
for (int j = 0, y = show_title ? Y_LIST * screen.ppu_y : 0; y < screen.actual_h; y += rect_h, ++j) {
67+
for (int i = 0, x = 0; x < screen.actual_w; x += rect_w, ++i) {
68+
SDL_Rect rect = SDL_utils::makeRect(x, y, rect_w, rect_h);
69+
SDL_FillRect(background_.get(), &rect, colors[(i + j) % 2]);
70+
}
71+
}
72+
73+
SDL_Rect frame_left_rect = SDL_utils::makeRect(screen.actual_w - 1, 0, 1, screen.actual_h);
74+
SDL_Rect frame_bottom_rect = SDL_utils::makeRect(0, screen.actual_h - 1, screen.actual_w, 1);
75+
SDL_FillRect(background_.get(), &frame_left_rect, 0);
76+
SDL_FillRect(background_.get(), &frame_bottom_rect, 0);
2477
}
2578

2679
void ImageViewer::onResize()
@@ -31,8 +84,14 @@ void ImageViewer::onResize()
3184

3285
void ImageViewer::render(const bool focused) const
3386
{
34-
SDL_FillRect(screen.surface, NULL, 0);
35-
SDL_utils::applyPpuScaledSurface(0, 0, image_.get(), screen.surface);
87+
SDL_utils::applyPpuScaledSurface(0, 0, background_.get(), screen.surface);
88+
int pos_y = image_->h <= screen.actual_h - Y_LIST * screen.ppu_y ?
89+
(Y_LIST * screen.ppu_y
90+
+ (screen.actual_h - Y_LIST * screen.ppu_y - image_->h) / 2)
91+
: (screen.actual_h - image_->h) / 2;
92+
SDL_utils::applyPpuScaledSurface((screen.actual_w - image_->w) / 2,
93+
pos_y,
94+
image_.get(), screen.surface);
3695
}
3796

3897
// Key press management

src/image_viewer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ImageViewer : public CWindow {
2525

2626
std::string filename_;
2727
SDLSurfaceUniquePtr image_;
28+
SDLSurfaceUniquePtr background_;
2829
bool ok_;
2930
};
3031

src/text_edit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void TextEdit::prepareSurfaces()
4949
void TextEdit::prepareColors()
5050
{
5151
const auto *pixel_format = background_->format;
52+
cursor_color_ = mapRGB(pixel_format, sdl_cursor_color_);
5253
border_color_ = mapRGB(pixel_format, sdl_border_color_);
5354
focus_border_color_ = mapRGB(pixel_format, sdl_focus_border_color_);
5455
bg_color_ = mapRGB(pixel_format, sdl_bg_color_);
@@ -125,7 +126,7 @@ void TextEdit::blitForeground(SDL_Surface &out, int x, int y) const
125126
cursor_rect.y = y + padding_y_ + border_width_y_;
126127
cursor_rect.w = 1;
127128
cursor_rect.h = foreground_rect_.h - 2 * padding_y_;
128-
SDL_FillRect(&out, &cursor_rect, border_color_);
129+
SDL_FillRect(&out, &cursor_rect, cursor_color_);
129130
}
130131

131132
void TextEdit::blitFocus(SDL_Surface &out, int x, int y) const

src/text_edit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class TextEdit {
6262

6363
int border_width_x_, border_width_y_, padding_x_, padding_y_;
6464

65+
SDL_Color sdl_cursor_color_ = SDL_Color { COLOR_TEXT_NORMAL };
66+
std::uint32_t cursor_color_;
67+
6568
SDL_Color sdl_border_color_ = SDL_Color { COLOR_BORDER };
6669
std::uint32_t border_color_;
6770

0 commit comments

Comments
 (0)