1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use tuix::*;
const STYLE: &str = r#"
arc {
width: 50px;
height: 50px;
background-color: red;
radius: 5px;
}
"#;
#[derive(Default, Lens)]
pub struct AppState {
backgorund_color: Color,
}
impl Model for AppState {
fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
if let Some(custom_event) = event.message.downcast() {
match custom_event {
CustomEvent::ChangeColor(color) => {
self.backgorund_color = *color;
entity.emit(state, BindEvent::Update);
event.consume();
}
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
enum CustomEvent {
ChangeColor(Color),
}
#[derive(Default)]
struct Container {
knob: Entity,
}
impl Widget for Container {
type Ret = Entity;
type Data = Color;
fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {
/*
// Generic float range
let map = GenericMap::new(-1.0, 1.0, Gradient::Linear, DisplayDecimals::Two, None);
let normalized_default = map.value_to_normalized(-0.5);
self.knob = Knob::new(map, normalized_default)
.build(state, entity, |builder| {
builder
.set_space(Stretch(1.0))
});
*/
// Decibel range
// let map = DecibelMap::new(-90.0, 3.0, ValueScaling::Power(0.15), DisplayDecimals::One, true);
// let normalized_default = map.db_to_normalized(0.0);
let map = DecibelMap::new(-12.0, 12.0, ValueScaling::Linear, DisplayDecimals::One, true);
let normalized_default = map.db_to_normalized(0.0);
self.knob = Knob::new(map, normalized_default)
.on_changing(|data, state, knob|{
let col = (data.normalized_value * 255.0) as u8;
knob.emit(state, CustomEvent::ChangeColor(Color::rgb(col, col, col)));
})
.bind(AppState::backgorund_color, |color| color.r() as f32 / 255.0)
.build(state, entity, |builder| {
builder
.set_space(Stretch(1.0))
});
let map = DecibelMap::new(-90.0, 3.0, ValueScaling::Power(0.15), DisplayDecimals::One, true);
let normalized_default = map.db_to_normalized(0.0);
self.knob = Knob::new(map, normalized_default)
.on_changing(|data, state, knob|{
let col = (data.normalized_value * 255.0) as u8;
knob.emit(state, CustomEvent::ChangeColor(Color::rgb(col, col, col)));
})
.bind(AppState::backgorund_color, |color| color.r() as f32 / 255.0)
.build(state, entity, |builder| {
builder
.set_space(Stretch(1.0))
});
/*
// Frequency range
let map = FrequencyMap::new(20.0, 20_000.0, Gradient::Frequency, FrequencyDisplayMode::default(), true);
let normalized_default = map.hz_to_normalized(1_000.0);
self.knob = Knob::new(map, normalized_default)
.build(state, entity, |builder| {
builder
.set_space(Stretch(1.0))
});
*/
/*
// Integer range
let map = IntMap::new(
0,
6,
Some(&|int: i32| -> String {
String::from(match int {
0 => "A",
1 => "B",
2 => "C",
3 => "D",
4 => "E",
5 => "F",
_ => "G",
})
}));
let normalized_default = map.int_to_normalized(2);
self.knob = Knob::new(map, normalized_default)
.build(state, entity, |builder| {
builder
.set_space(Stretch(1.0))
});
*/
entity.set_background_color(state, Color::rgb(79,79,79))
}
fn on_update(&mut self, state: &mut State, entity: Entity, data: &Self::Data) {
entity.set_background_color(state, *data);
}
// fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
// if let Some(custom_event) = event.message.downcast() {
// match custom_event {
// CustomEvent::ChangeColor(color) => {
// entity.set_background_color(state, *color);
// }
// }
// }
// }
}
fn main() {
let app = Application::new(
WindowDescription::new()
.with_title("Knob")
.with_inner_size(300, 300),
|state, window| {
window.set_background_color(state, Color::rgb(79,79,79));
//state.add_theme(STYLE);
state.add_stylesheet("examples/themes/knob_theme.css").expect("Failed to load theme");
let store = Store::new(AppState::default()).build(state, window, |builder| builder);
Container::default()
.bind(AppState::backgorund_color, |value| *value)
.build(state, store, |builder| builder);
},
);
app.run();
}