Refactor NotifyValue: optimize value setting and modification tracking with move semantics and improved method names

This commit is contained in:
scawful
2025-02-28 01:13:16 -05:00
parent a28ad9c516
commit 7d77c51a27
2 changed files with 26 additions and 11 deletions

View File

@@ -139,8 +139,8 @@ absl::Status Tile16Editor::UpdateBlockset() {
EndChild();
if (!blockset_canvas_.points().empty()) {
notify_tile16.mutable_get() = blockset_canvas_.GetTileIdFromMousePos();
notify_tile16.apply_changes();
notify_tile16.edit() = blockset_canvas_.GetTileIdFromMousePos();
notify_tile16.commit();
if (notify_tile16.modified()) {
current_tile16_ = notify_tile16.get();
@@ -246,8 +246,8 @@ absl::Status Tile16Editor::DrawTileEditControls() {
Text("Tile16 ID: %d", current_tile16_);
Text("Tile8 ID: %d", current_tile8_);
Text("Options:");
gui::InputHexByte("Palette", &notify_palette.mutable_get());
notify_palette.apply_changes();
gui::InputHexByte("Palette", &notify_palette.edit());
notify_palette.commit();
if (notify_palette.modified()) {
auto palette = palettesets_[current_palette_].main_;
auto value = notify_palette.get();

View File

@@ -17,8 +17,17 @@ class NotifyValue {
: value_(value), modified_(false), temp_value_() {}
void set(const T &value) {
value_ = value;
modified_ = true;
if (value != value_) {
value_ = value;
modified_ = true;
}
}
void set(T &&value) {
if (value != value_) {
value_ = std::move(value);
modified_ = true;
}
}
const T &get() {
@@ -26,29 +35,35 @@ class NotifyValue {
return value_;
}
T &mutable_get() {
T &edit() {
modified_ = false;
temp_value_ = value_;
return temp_value_;
}
void apply_changes() {
void commit() {
if (temp_value_ != value_) {
value_ = temp_value_;
modified_ = true;
}
}
void operator=(const T &value) { set(value); }
operator T() { return get(); }
bool consume_modified() {
bool modified = modified_;
modified_ = false;
return modified;
}
operator T() { return get(); }
void operator=(const T &value) { set(value); }
bool modified() const { return modified_; }
private:
T value_;
bool modified_;
T temp_value_;
bool modified_;
};
} // namespace util
} // namespace yaze