改造一些
This commit is contained in:
@@ -1,96 +1,27 @@
|
||||
#include "Numeric_Axis.h"
|
||||
|
||||
#include "Axis_Format.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace renderive::detail {
|
||||
namespace {
|
||||
|
||||
bool valid_range(Range range) {
|
||||
return std::isfinite(range.origin) && std::isfinite(range.target) && range.size() > 0.0;
|
||||
}
|
||||
|
||||
Numeric_Axis_State numeric_state_from(const Axis_Properties& properties) {
|
||||
return {
|
||||
properties.coordinates,
|
||||
std::clamp(properties.precision, 0, 12),
|
||||
properties.wheel,
|
||||
properties.drag
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Axis_Control::Axis_Control(Plot_Core& plot, const Axis_Properties& properties)
|
||||
: Abs_Axis(plot, properties), numeric_(numeric_state_from(properties)) {}
|
||||
|
||||
Range Axis_Control::coord_range() const {
|
||||
return numeric_.get<&Numeric_Axis_State::coordinates>();
|
||||
}
|
||||
|
||||
int Axis_Control::label_precision() const {
|
||||
return numeric_.get<&Numeric_Axis_State::precision>();
|
||||
}
|
||||
|
||||
void Axis_Control::set_label_precision(int value) {
|
||||
numeric_.set<&Numeric_Axis_State::precision>(std::clamp(value, 0, 12));
|
||||
changed();
|
||||
}
|
||||
|
||||
double Axis_Control::coord_start() const { return coord_range().origin; }
|
||||
|
||||
void Axis_Control::set_coord_start(double value) {
|
||||
auto range = coord_range();
|
||||
set_coord_range({value, value + range.length()});
|
||||
}
|
||||
|
||||
double Axis_Control::coord_length() const { return coord_range().length(); }
|
||||
|
||||
void Axis_Control::set_coord_length(double value) {
|
||||
auto range = coord_range();
|
||||
set_coord_range({range.origin, range.origin + value});
|
||||
}
|
||||
|
||||
void Axis_Control::set_coord_range(Range range) {
|
||||
if (!valid_range(range))
|
||||
return;
|
||||
numeric_.set<&Numeric_Axis_State::coordinates>(range);
|
||||
changed();
|
||||
}
|
||||
|
||||
void Axis_Control::set_use_wheel(bool value) {
|
||||
numeric_.set<&Numeric_Axis_State::wheel>(value);
|
||||
changed();
|
||||
}
|
||||
|
||||
void Axis_Control::set_use_drag(bool value) {
|
||||
numeric_.set<&Numeric_Axis_State::drag>(value);
|
||||
changed();
|
||||
}
|
||||
|
||||
bool Axis_Control::use_wheel() const {
|
||||
return numeric_.get<&Numeric_Axis_State::wheel>();
|
||||
}
|
||||
|
||||
bool Axis_Control::use_drag() const {
|
||||
return numeric_.get<&Numeric_Axis_State::drag>();
|
||||
}
|
||||
: Axis_State_Strategy(plot, properties) {}
|
||||
|
||||
void Axis_Control::handle_event(const Event& event) {
|
||||
if (event.type == Event_Type::Wheel && use_wheel()) {
|
||||
if (event.type == Event_Type::Wheel && get<&Axis_Properties::wheel>()) {
|
||||
const auto& wheel = static_cast<const Wheel_Event&>(event);
|
||||
Range range = coord_range();
|
||||
const double anchor_pixel = orientation() == Orientation::Horizontal ? wheel.position.x : wheel.position.y;
|
||||
const double anchor = pixel_to_coord(anchor_pixel);
|
||||
const Range range = get<&Axis_Properties::coordinates>();
|
||||
const Orientation orientation = get<&Axis_Base_Properties::orientation>();
|
||||
const double anchor_pixel = orientation == Orientation::Horizontal
|
||||
? wheel.position.x
|
||||
: wheel.position.y;
|
||||
const double anchor = transform().pixel_to_coord(anchor_pixel);
|
||||
const double factor = wheel.angle_delta_y >= 0.0 ? 0.9 : 1.1;
|
||||
set_coord_range({anchor + (range.origin - anchor) * factor,
|
||||
anchor + (range.target - anchor) * factor});
|
||||
const Range next{anchor + (range.origin - anchor) * factor,
|
||||
anchor + (range.target - anchor) * factor};
|
||||
set<&Axis_Properties::coordinates>(next);
|
||||
event.accept();
|
||||
return;
|
||||
}
|
||||
if (!use_drag())
|
||||
if (!get<&Axis_Properties::drag>())
|
||||
return;
|
||||
if (event.type == Event_Type::Pointer_Press) {
|
||||
const auto& pointer = static_cast<const Pointer_Event&>(event);
|
||||
@@ -114,12 +45,16 @@ void Axis_Control::handle_event(const Event& event) {
|
||||
});
|
||||
if (!dragging)
|
||||
return;
|
||||
const double delta = orientation() == Orientation::Horizontal
|
||||
const Orientation orientation = get<&Axis_Base_Properties::orientation>();
|
||||
const double delta = orientation == Orientation::Horizontal
|
||||
? pointer.position.x - previous.x
|
||||
: pointer.position.y - previous.y;
|
||||
Range range = coord_range();
|
||||
const double shift = pixel_length() == 0 ? 0.0 : -delta * range.length() / pixel_length();
|
||||
set_coord_range({range.origin + shift, range.target + shift});
|
||||
const Range range = get<&Axis_Properties::coordinates>();
|
||||
const std::size_t pixel_length = get<&Axis_Base_Properties::pixel_length>();
|
||||
const double shift = pixel_length == 0
|
||||
? 0.0
|
||||
: -delta * range.length() / static_cast<double>(pixel_length);
|
||||
set<&Axis_Properties::coordinates>(Range{range.origin + shift, range.target + shift});
|
||||
event.accept();
|
||||
} else if (event.type == Event_Type::Pointer_Release) {
|
||||
bool accepted{};
|
||||
@@ -132,26 +67,17 @@ void Axis_Control::handle_event(const Event& event) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string Axis_Control::tick_label(double tick) const {
|
||||
return localized_axis_number(tick, label_precision(), locale());
|
||||
}
|
||||
|
||||
void Axis_Control::publish() {
|
||||
Abs_Axis::publish();
|
||||
numeric_.publish();
|
||||
Axis_State_Strategy::publish();
|
||||
interaction_.publish();
|
||||
}
|
||||
|
||||
std::uint64_t Axis_Control::state_revision() const {
|
||||
return Abs_Axis::state_revision() + numeric_.state_revision() + interaction_.state_revision();
|
||||
return Axis_State_Strategy::state_revision() + interaction_.state_revision();
|
||||
}
|
||||
|
||||
Numeric_Axis_State Axis_Control::numeric_state() const {
|
||||
return numeric_.read([](const Numeric_Axis_State& value) { return value; });
|
||||
}
|
||||
|
||||
Numeric_Axis_State Axis_Control::render_numeric_state() const {
|
||||
return numeric_.render_use_state();
|
||||
Range Axis_Control::coordinate_range(const Axis_Properties& state) const noexcept {
|
||||
return state.coordinates.get();
|
||||
}
|
||||
|
||||
} // namespace renderive::detail
|
||||
|
||||
Reference in New Issue
Block a user