Refactor NotifyValue: optimize value setting and modification tracking with move semantics and improved method names
This commit is contained in:
@@ -139,8 +139,8 @@ absl::Status Tile16Editor::UpdateBlockset() {
|
|||||||
EndChild();
|
EndChild();
|
||||||
|
|
||||||
if (!blockset_canvas_.points().empty()) {
|
if (!blockset_canvas_.points().empty()) {
|
||||||
notify_tile16.mutable_get() = blockset_canvas_.GetTileIdFromMousePos();
|
notify_tile16.edit() = blockset_canvas_.GetTileIdFromMousePos();
|
||||||
notify_tile16.apply_changes();
|
notify_tile16.commit();
|
||||||
|
|
||||||
if (notify_tile16.modified()) {
|
if (notify_tile16.modified()) {
|
||||||
current_tile16_ = notify_tile16.get();
|
current_tile16_ = notify_tile16.get();
|
||||||
@@ -246,8 +246,8 @@ absl::Status Tile16Editor::DrawTileEditControls() {
|
|||||||
Text("Tile16 ID: %d", current_tile16_);
|
Text("Tile16 ID: %d", current_tile16_);
|
||||||
Text("Tile8 ID: %d", current_tile8_);
|
Text("Tile8 ID: %d", current_tile8_);
|
||||||
Text("Options:");
|
Text("Options:");
|
||||||
gui::InputHexByte("Palette", ¬ify_palette.mutable_get());
|
gui::InputHexByte("Palette", ¬ify_palette.edit());
|
||||||
notify_palette.apply_changes();
|
notify_palette.commit();
|
||||||
if (notify_palette.modified()) {
|
if (notify_palette.modified()) {
|
||||||
auto palette = palettesets_[current_palette_].main_;
|
auto palette = palettesets_[current_palette_].main_;
|
||||||
auto value = notify_palette.get();
|
auto value = notify_palette.get();
|
||||||
|
|||||||
@@ -17,38 +17,53 @@ class NotifyValue {
|
|||||||
: value_(value), modified_(false), temp_value_() {}
|
: value_(value), modified_(false), temp_value_() {}
|
||||||
|
|
||||||
void set(const T &value) {
|
void set(const T &value) {
|
||||||
|
if (value != value_) {
|
||||||
value_ = value;
|
value_ = value;
|
||||||
modified_ = true;
|
modified_ = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set(T &&value) {
|
||||||
|
if (value != value_) {
|
||||||
|
value_ = std::move(value);
|
||||||
|
modified_ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const T &get() {
|
const T &get() {
|
||||||
modified_ = false;
|
modified_ = false;
|
||||||
return value_;
|
return value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
T &mutable_get() {
|
T &edit() {
|
||||||
modified_ = false;
|
modified_ = false;
|
||||||
temp_value_ = value_;
|
temp_value_ = value_;
|
||||||
return temp_value_;
|
return temp_value_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_changes() {
|
void commit() {
|
||||||
if (temp_value_ != value_) {
|
if (temp_value_ != value_) {
|
||||||
value_ = temp_value_;
|
value_ = temp_value_;
|
||||||
modified_ = true;
|
modified_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator=(const T &value) { set(value); }
|
bool consume_modified() {
|
||||||
operator T() { return get(); }
|
bool modified = modified_;
|
||||||
|
modified_ = false;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator T() { return get(); }
|
||||||
|
void operator=(const T &value) { set(value); }
|
||||||
bool modified() const { return modified_; }
|
bool modified() const { return modified_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T value_;
|
T value_;
|
||||||
bool modified_;
|
|
||||||
T temp_value_;
|
T temp_value_;
|
||||||
|
bool modified_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace util
|
} // namespace util
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user