forked from jwty/tv-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_viewer.cpp
More file actions
226 lines (192 loc) · 6.78 KB
/
image_viewer.cpp
File metadata and controls
226 lines (192 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "image_viewer.h"
#include "config.h"
#include "def.h"
#include "palette.h"
#include "resourceManager.h"
#include "screen.h"
#include "sdlutils.h"
#include "text_viewer.h"
#include <SDL_image.h>
#include <algorithm>
#include <sys/stat.h>
ImageViewer::ImageViewer(CPanel *panel)
: panel_(panel), showTitle_(false)
{
init();
setPath(panel->getHighlightedItemFull());
}
void ImageViewer::init()
{
// Create the fullscreen background surface
background_ = SDLSurfaceUniquePtr{
SDL_utils::createImage(screen.actual_w,
screen.actual_h, toPixel(screen.surface->format, g_palette.bg_normal))
};
// Checkerboard background
constexpr int kTransparentBgRectSize = 10;
const Uint32 colors[2] = {
SDL_MapRGB(background_->format, 240, 240, 240),
SDL_MapRGB(background_->format, 155, 155, 155),
};
const int rect_w = static_cast<int>(kTransparentBgRectSize * screen.ppu_x);
const int rect_h = static_cast<int>(kTransparentBgRectSize * screen.ppu_y);
for (int j = 0, y = showTitle_ ? Y_LIST * screen.ppu_y : 0; y < screen.actual_h;
y += rect_h, ++j) {
for (int i = 0, x = 0; x < screen.actual_w; x += rect_w, ++i) {
SDL_Rect rect = SDL_utils::makeRect(x, y, rect_w, rect_h);
SDL_FillRect(background_.get(), &rect, colors[(i + j) % 2]);
}
}
}
void ImageViewer::setPath(const std::string &path)
{
filename_ = std::move(path);
image_ = nullptr;
// Load image scaled to fit the screen
image_ = SDLSurfaceUniquePtr{
SDL_utils::loadImageToFit(filename_, screen.w, screen.h)
};
ok_ = (image_ != nullptr);
}
void ImageViewer::onResize()
{
// Recreate background and reload the image when the window is resized
image_ = nullptr;
background_ = nullptr;
init();
setPath(filename_);
}
void ImageViewer::render(const bool focused) const
{
// Draw background
SDL_utils::applyPpuScaledSurface(0, 0, background_.get(), screen.surface);
// Draw centered image
SDL_utils::applyPpuScaledSurface(
(screen.actual_w - image_->w) / 2,
(screen.actual_h - image_->h) / 2,
image_.get(), screen.surface
);
// Draw title bar if enabled
if (showTitle_) {
const auto &fonts = CResourceManager::instance().getFonts();
// Draw background rectangle for title
SDL_Rect rect = SDL_utils::Rect(0, 0, screen.actual_w, HEADER_H * screen.ppu_y);
SDL_FillRect(screen.surface, &rect, toPixel(screen.surface->format, g_palette.panel));
// Render title text
SDLSurfaceUniquePtr tmp{
SDL_utils::renderText(fonts, filename_, g_palette.text_header, g_palette.panel)
};
SDL_utils::applyPpuScaledSurface(2 * screen.ppu_x, HEADER_PADDING_TOP * screen.ppu_y,
tmp.get(), screen.surface);
}
}
// --- Helper: check if a file can be loaded as an image ---
static bool isSupportedImageFile(const std::string& filename) {
SDL_Surface *test = IMG_Load(filename.c_str());
if (test) {
SDL_FreeSurface(test);
return true;
}
SDL_ClearError();
return false;
}
// --- Helper: check if a path is a directory ---
static bool isDirectory(const std::string& path) {
struct stat s;
if (stat(path.c_str(), &s) == 0) {
return (s.st_mode & S_IFDIR);
}
return false;
}
// --- Helper: restore panel cursor to a previous file path ---
// Used when no valid image is found, so the selection goes back
// to the file that was selected before navigation started.
static void restoreSelection(CPanel* panel, const std::string& old_path) {
// Try moving upwards until we find the old path
while (true) {
std::string current = panel->getHighlightedItemFull();
if (current == old_path) return; // found it
if (!panel->moveCursorUp(1)) break;
}
// If not found upwards, try moving downwards
while (true) {
std::string current = panel->getHighlightedItemFull();
if (current == old_path) return; // trouvé
if (!panel->moveCursorDown(1)) break;
}
// If not found at all, leave cursor where it is
}
bool ImageViewer::nextOrPreviousImage(int direction)
{
std::string old_path = panel_->getHighlightedItemFull();
while (true) {
if (direction == -1) {
if (!panel_->moveCursorUp(1)) break;
} else {
if (!panel_->moveCursorDown(1)) break;
}
std::string new_path = panel_->getHighlightedItemFull();
if (File_utils::getFileName(new_path) == "..") continue;
if (isDirectory(new_path)) continue;
if (!isSupportedImageFile(new_path)) continue;
constexpr std::size_t kMaxFileSize = 16777216; // = 16 MB
if (File_utils::getFileSize(new_path) > kMaxFileSize) continue;
setPath(std::move(new_path));
return ok();
}
// No valid image found → restore previous selection
restoreSelection(panel_, old_path);
return false;
}
// Key press management
bool ImageViewer::keyPress(
const SDL_Event &event, SDLC_Keycode key, ControllerButton button)
{
CWindow::keyPress(event, key, button);
const auto &c = config();
const auto sym = event.key.keysym.sym;
// Exit viewer
if (key == c.key_parent || button == c.gamepad_parent) {
m_retVal = -1;
return true;
}
// Previous image
if (key == c.key_up || button == c.gamepad_up ||
key == c.key_left || button == c.gamepad_left) return actionUp();
// Next image
if (key == c.key_down || button == c.gamepad_down ||
key == c.key_right || button == c.gamepad_right) return actionDown();
// Toggle title bar
if (key == c.key_system || button == c.gamepad_system) {
showTitle_ = !showTitle_;
return true;
}
return false;
}
bool ImageViewer::actionUp() { return nextOrPreviousImage(-1); }
bool ImageViewer::actionDown() { return nextOrPreviousImage(1); }
bool ImageViewer::actionLeft() { return nextOrPreviousImage(-1); }
bool ImageViewer::actionRight() { return nextOrPreviousImage(1); }
bool ImageViewer::keyHold()
{
const auto &c = config();
if (tick(c.key_up) || tick(c.key_left)) return actionUp();
if (tick(c.key_down) || tick(c.key_right)) return actionDown();
return false;
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
bool ImageViewer::gamepadHold(SDL_GameController *controller)
{
const auto &c = config();
if (tick(controller, c.gamepad_up) || tick(controller, c.gamepad_left)) return actionUp();
if (tick(controller, c.gamepad_down) || tick(controller, c.gamepad_right)) return actionDown();
return false;
}
#endif
bool ImageViewer::mouseWheel(int dx, int dy)
{
CWindow::mouseWheel(dx, dy);
if (dy < 0) return nextOrPreviousImage(-1);
if (dy > 0) return nextOrPreviousImage(1);
return false;
}