Skip to content

Commit

Permalink
Show next/previous image with D-Pad
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 1, 2022
1 parent 9706b2d commit 6cc7cf5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 39 deletions.
25 changes: 12 additions & 13 deletions commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ const bool CCommander::openCopyMenu(void) const
return true;
});


l_dialog.addOption(m_panelSource == &m_panelLeft ? "Symlink >" : "< Symlink");
handlers.push_back([&]() {
File_utils::symlinkFile(l_list, m_panelTarget->getCurrentPath());
Expand All @@ -342,7 +341,7 @@ const bool CCommander::openCopyMenu(void) const
File_utils::removeFile(l_list);
return true;
});
const int delete_option = handlers.size();
const int delete_option = handlers.size();

l_dialog.addOption("Disk used");
handlers.push_back([&]() {
Expand Down Expand Up @@ -468,25 +467,25 @@ OpenFileResult OpenFileDialog(const std::string &path,
return options[dlg.execute()];
}

void ViewFile(const std::string &path)
} // namespace

void CCommander::ViewFile(std::string &&path) const
{
// Check size
constexpr std::size_t kMaxFileSize = 16777216; // = 16 MB
const auto file_size = File_utils::getFileSize(path);
if (file_size > kMaxFileSize) {
ErrorDialog(path, "Error:", "File too large (>16 MiB)");
} else {
ImageViewer image_viewer(path);
if (image_viewer.ok()) {
image_viewer.execute();
return;
}

TextViewer(path).execute();
return;
}
ImageViewer image_viewer(m_panelSource);
if (image_viewer.ok()) {
image_viewer.execute();
return;
}
}

} // namespace
TextViewer(path).execute();
}

void CCommander::openExecuteMenu(void) const
{
Expand Down
2 changes: 2 additions & 0 deletions commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CCommander : public CWindow
bool itemMenu() const;
bool operationMenu() const;

void ViewFile(std::string &&path) const;

const bool openCopyMenu(void) const;
void openExecuteMenu(void) const;

Expand Down
89 changes: 67 additions & 22 deletions image_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,55 @@

#define VIEWER_MARGIN 1

ImageViewer::ImageViewer(std::string filename)
: filename_(std::move(filename))
ImageViewer::ImageViewer(CPanel *panel)
: panel_(panel)
{
init();
setPath(panel->getHighlightedItemFull());
}

void ImageViewer::init()
{
// Create background image
background_ = SDLSurfaceUniquePtr { SDL_utils::createImage(screen.actual_w,
screen.actual_h, SDL_MapRGB(screen.surface->format, COLOR_BG_1)) };

// Transparency grid background.
constexpr int kTransparentBgRectSize = 10;
const Uint32 colors[2] = {
SDL_MapRGB(background_->format, 240, 240, 240),
SDL_MapRGB(background_->format, 155, 155, 155),
};
int j = 0;
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 = Y_LIST * screen.ppu_y; 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(std::string &&path)
{
filename_ = std::move(path);
image_ = nullptr; // Release memory first
image_ = SDLSurfaceUniquePtr { SDL_utils::loadImageToFit(
filename_, screen.w, screen.h - Y_LIST) };
ok_ = (image_ != nullptr);
if (!ok_) return;

const auto &fonts = CResourceManager::instance().getFonts();

// Create background image
background_ = SDLSurfaceUniquePtr { SDL_utils::createImage(screen.actual_w,
screen.actual_h, SDL_MapRGB(screen.surface->format, COLOR_BG_1)) };
// Print title
{
SDL_Rect rect = SDL_utils::Rect(
0, 0, screen.actual_w, HEADER_H * screen.ppu_y);
SDL_FillRect(background_.get(), &rect,
SDL_MapRGB(background_->format, COLOR_BORDER));
}
// Print title
{
SDLSurfaceUniquePtr tmp { SDL_utils::renderText(
fonts, filename_, Globals::g_colorTextTitle, { COLOR_TITLE_BG }) };
Expand All @@ -52,29 +76,14 @@ void ImageViewer::init()
background_.get());
}
}

// Transparency grid background.
constexpr int kTransparentBgRectSize = 10;
const Uint32 colors[2] = {
SDL_MapRGB(background_->format, 240, 240, 240),
SDL_MapRGB(background_->format, 155, 155, 155),
};
int j = 0;
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 = Y_LIST * screen.ppu_y; 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::onResize()
{
image_ = nullptr;
background_ = nullptr;
init();
setPath(std::move(filename_));
}

void ImageViewer::render(const bool focused) const
Expand All @@ -86,6 +95,24 @@ void ImageViewer::render(const bool focused) const
image_.get(), screen.surface);
}

bool ImageViewer::nextOrPreviousImage(int direction)
{
while (true) {
if (direction == -1) {
if (!panel_->moveCursorUp(1)) return false;
} else {
if (!panel_->moveCursorDown(1)) return false;
}
std::string new_path = panel_->getHighlightedItemFull();

constexpr std::size_t kMaxFileSize = 16777216; // = 16 MB
if (File_utils::getFileSize(new_path) > kMaxFileSize) continue;

setPath(std::move(new_path));
if (ok()) return true;
}
}

// Key press management
bool ImageViewer::keyPress(
const SDL_Event &event, SDLC_Keycode key, ControllerButton button)
Expand All @@ -98,5 +125,23 @@ bool ImageViewer::keyPress(
m_retVal = -1;
return true;
}

if (key == c.key_left || button == c.gamepad_left || key == c.key_up
|| button == c.gamepad_up) {
return nextOrPreviousImage(-1);
}
if (key == c.key_right || button == c.gamepad_right || key == c.key_down
|| button == c.gamepad_down) {
return nextOrPreviousImage(1);
}

return false;
}

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;
}
11 changes: 9 additions & 2 deletions image_viewer.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#ifndef IMAGE_VIEWER_H_
#define IMAGE_VIEWER_H_

#include <cstddef>
#include <string>

#include "panel.h"
#include "sdl_ptrs.h"
#include "window.h"

class ImageViewer : public CWindow {
public:
explicit ImageViewer(std::string filename);
explicit ImageViewer(CPanel *panel);
virtual ~ImageViewer() = default;

ImageViewer(const ImageViewer &) = delete;
Expand All @@ -17,17 +19,22 @@ class ImageViewer : public CWindow {
bool ok() const { return ok_; }

private:
void setPath(std::string &&path);
void init();
void render(const bool focused) const override;
bool keyPress(const SDL_Event &event, SDLC_Keycode key,
ControllerButton button) override;
bool mouseWheel(int dx, int dy) override;
void onResize() override;
bool isFullScreen() const override { return true; }

std::string filename_;
bool nextOrPreviousImage(int direction);

SDLSurfaceUniquePtr background_;
SDLSurfaceUniquePtr image_;
bool ok_;
CPanel *panel_;
std::string filename_;
};

#endif // IMAGE_VIEWER_H_
2 changes: 1 addition & 1 deletion panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ const std::string &CPanel::getHighlightedItem(void) const
return m_fileLister[m_highlightedLine].m_name;
}

const std::string CPanel::getHighlightedItemFull(void) const
std::string CPanel::getHighlightedItemFull(void) const
{
return m_currentPath + (m_currentPath == "/" ? "" : "/") + m_fileLister[m_highlightedLine].m_name;
}
Expand Down
2 changes: 1 addition & 1 deletion panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CPanel
const std::string &getHighlightedItem(void) const;

// Selected file with full path
const std::string getHighlightedItemFull(void) const;
std::string getHighlightedItemFull(void) const;

// Current path
const std::string &getCurrentPath(void) const;
Expand Down

0 comments on commit 6cc7cf5

Please sign in to comment.