Skip to content

Commit

Permalink
A
Browse files Browse the repository at this point in the history
  • Loading branch information
JJL772 committed May 14, 2023
1 parent 20d9e9a commit 0feab01
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 87 deletions.
2 changes: 0 additions & 2 deletions src/common/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
#include <cstring>
#include <fstream>
#include <string>
#include <memory>
#include <charconv>

#include "strtools.hpp"

/******************************/
namespace util
{

Expand Down
106 changes: 98 additions & 8 deletions src/gui/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include "common/enums.hpp"

using namespace vtfview;
using namespace VTFLib;

Document::Document(QObject* parent)
: QObject(parent) {
history_.push_back({});
}

void Document::new_file() {
Expand Down Expand Up @@ -41,13 +43,20 @@ bool Document::save(const std::string& path) {
return true;

// Image needs converting
if (format_ != IMAGE_FORMAT_NONE) {
if (data().format_ != IMAGE_FORMAT_NONE) {
fmt::print(
"Converting image from {} to {} on save...\n", NAMEOF_ENUM(file_->GetFormat()),
NAMEOF_ENUM(format_));
if (!file_->ConvertInPlace(format_))
NAMEOF_ENUM(data().format_));
if (!file_->ConvertInPlace(data().format_))
return false;
}

bool needsMipRegen = data().mips_ != file_->GetMipmapCount();

// Image needs resize
if (data().width_ != file_->GetWidth() || data().height_ != file_->GetHeight()) {
needsMipRegen = true; // Always regenerate mips if we change size
}

if (!file_->Save(path.empty() ? path_.c_str() : path.c_str())) {
return false;
Expand Down Expand Up @@ -93,26 +102,107 @@ void Document::unload_file() {
file_ = nullptr;
path_ = "";
unmark_modified();
format_ = IMAGE_FORMAT_NONE;
data().format_ = IMAGE_FORMAT_NONE;
}

bool Document::load_file_internal(const void* data, size_t size) {
bool Document::load_file_internal(const void* imgdata, size_t size) {
auto* oldFile = file_;
file_ = new VTFLib::CVTFFile();
if (!file_->Load(data, size)) {
if (!file_->Load(imgdata, size)) {
delete file_;
file_ = oldFile;
return false;
}

clear_history();
data().height_ = file_->GetHeight();
data().width_ = file_->GetWidth();
data().mips_ = file_->GetMipmapCount();
data().flags_ = file_->GetFlags();
data().startFrame_ = file_->GetStartFrame();

delete oldFile;
return true;
}

void Document::set_format(VTFImageFormat format) {
if (format == file_->GetFormat()) {
format_ = IMAGE_FORMAT_NONE;
data().format_ = IMAGE_FORMAT_NONE;
return;
}
format_ = format;
data().format_ = format;
mark_modified();
}

void Document::set_height(int height) {
if (height != data().height_) {
data().height_ = height;
mark_modified();
}
}

void Document::set_width(int width) {
if (data().width_ != width) {
data().width_ = width;
mark_modified();
}
}

void Document::set_mips(int mipCount) {
if (mipCount != data().mips_) {
data().mips_ = mipCount;
mark_modified();
}
}

int Document::max_mips() const {
return CVTFFile::ComputeMipmapCount(data().width_, data().height_, 1);
}

void Document::set_flag(VTFImageFlag flag, bool on) {
if ((data().flags_ & flag) != (on ? flag : 0)) {
if (on)
data().flags_ |= flag;
else
data().flags_ &= ~flag;
mark_modified();
}
}

void Document::mark_history(const std::string& name) {
// Erase any additional entries in the history (i.e. leftover from undo-ing)
if (histIndex_ != history_.size()-1)
history_.erase(history_.begin() + histIndex_ + 1, history_.end());
history_.push_back({
.name = name,
.d = data()
});
histIndex_++;
}

bool Document::undo() {
if (histIndex_ == 0)
return false;
histIndex_--;
return true;
}

bool Document::redo() {
if (histIndex_ == history_.size()-1)
return false;
histIndex_++;
return true;
}

void Document::set_start_frame(int frame) {
if (frame != data().startFrame_) {
data().startFrame_ = frame;
mark_modified();
}
}

void Document::clear_history() {
history_.clear();
history_.push_back({});
histIndex_ = 0;
}
85 changes: 83 additions & 2 deletions src/gui/document.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include <stack>
#include <QObject>

#include "VTFLib.h"
Expand All @@ -10,7 +11,7 @@ namespace vtfview

/**
* Represents a VTF file document
* Handles saving, loading, etc
* Handles saving, loading and modification of image parameters
*/
class Document : public QObject {
Q_OBJECT;
Expand Down Expand Up @@ -49,6 +50,62 @@ namespace vtfview
}

void set_format(VTFImageFormat format);

void set_width(int width);
void set_height(int height);
void set_mips(int mipCount);

void set_flag(VTFImageFlag flag, bool on);

/**
* Get or set the starting frame
*/
int start_frame() const { return data().startFrame_; }

void set_start_frame(int frame);

/**
* Returns currently set flags
*/
uint32_t flags() const { return data().flags_; }

/**
* Returns the size this image will be resized to on save
* For actual current data size, query size from file()
*/
inline int resize_width() const { return data().width_; }
inline int resize_height() const { return data().height_; }

/**
* Returns how many mip levels we can have for this image, based on current
* width and height
*/
int max_mips() const;

/**
* Marks a point in history, allowing an undo/redo operation
* Any operations on the stack that exist above this will be erased
* @param name Name of the history point, for display in UI
*/
void mark_history(const std::string& name);

/**
* Undo the last operation
* @returns True if we undid something
*/
bool undo();

/**
* Redo any previously undone operations.
* Traverses upwards in the history stack
* @returns True if we redid something
*/
bool redo();

/**
* Clears all history and set it to default state
*/
void clear_history();

signals:
/**
Expand All @@ -71,7 +128,31 @@ namespace vtfview
VTFLib::CVTFFile* file_ = nullptr;
bool dirty_ = false;
std::string path_;
VTFImageFormat format_ = IMAGE_FORMAT_NONE;

struct data_t {
VTFImageFormat format_ = IMAGE_FORMAT_NONE; // Format to convert to on save, if none, don't convert
int width_ = 0;
int height_ = 0;
int mips_ = 0;
uint32_t flags_ = 0;
int startFrame_;
};

struct history_point_t {
std::string name;
data_t d;
};

int histIndex_ = 0;
std::vector<history_point_t> history_;

inline data_t& data() {
return history_[histIndex_].d;
}

inline const data_t& data() const {
return history_[histIndex_].d;
}
};

} // namespace vtfview
Loading

0 comments on commit 0feab01

Please sign in to comment.