From f387b097cd5b2bb6e67cc1bfc24541b1e5206d4b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Dec 2020 20:19:45 +0100 Subject: [PATCH] Add ui.drag_angle_tau --- CHANGELOG.md | 1 + egui/src/ui.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a975d8c..f4db48ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * `ui.allocate_ui(size, |ui| ...)`: Easily create a child-`Ui` of a given size. * `SelectableLabel` (`ui.selectable_label` and `ui.selectable_value`): A text-button that can be selected. * `ui.small_button`: A smaller button that looks good embedded in text. +* `ui.drag_angle_tau`: For those who want to specify angles as fractions of τ (a full turn). * Add `Resize::id_source` and `ScrollArea::id_source` to let the user avoid Id clashes. ### Changed 🔧 diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 9b2f573a..95c564c7 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -667,6 +667,27 @@ impl Ui { response } + /// Modify an angle. The given angle should be in radians, + /// but is shown to the user in fractions of one Tau (i.e. fractions of one turn). + /// The angle is NOT wrapped, so the user may select, for instance 2𝞃 (720°) + pub fn drag_angle_tau(&mut self, radians: &mut f32) -> Response { + #![allow(clippy::float_cmp)] + + use std::f32::consts::TAU; + + let mut taus = *radians / TAU; + let response = self + .add(DragValue::f32(&mut taus).speed(0.01).suffix("τ")) + .on_hover_text("1τ = one turn, 0.5τ = half a turn, etc. 0.25τ = 90°"); + + // only touch `*radians` if we actually changed the value + if taus != *radians / TAU { + *radians = taus * TAU; + } + + response + } + /// Show an image here with the given size pub fn image(&mut self, texture_id: TextureId, desired_size: Vec2) -> Response { self.add(Image::new(texture_id, desired_size))