Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
druid-widgets
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Paul Miller
druid-widgets
Commits
33b785d0
Commit
33b785d0
authored
5 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
dial envelope
parent
15bd4622
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/dial.rs
+99
-0
99 additions, 0 deletions
examples/dial.rs
src/dial.rs
+7
-12
7 additions, 12 deletions
src/dial.rs
with
106 additions
and
12 deletions
examples/dial.rs
0 → 100644
+
99
−
0
View file @
33b785d0
// Copyright 2019 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! An example of the `dial` widget.
use
druid
::
widget
::{
Flex
,
Label
,
WidgetExt
};
use
druid
::{
AppLauncher
,
Color
,
Data
,
Lens
,
PlatformError
,
Size
,
Widget
,
WindowDesc
};
use
druid_widgets
::
Dial
;
#[derive(Clone,
Data,
Lens)]
struct
DialLabel
{
value
:
f64
,
}
impl
DialLabel
{
fn
new
(
text
:
&
str
,
value
:
f64
)
->
impl
Widget
<
DialLabel
>
{
let
text
=
text
.to_string
();
let
label
=
Label
::
new
(
move
|
data
:
&
DialLabel
,
_env
:
&
_
|
format!
(
"{} {:.3}"
,
text
,
data
.value
));
let
solid
=
Color
::
rgb8
(
0x3a
,
0x3a
,
0x3a
);
let
mut
col
=
Flex
::
column
();
col
.add_child
(
Dial
::
new
(
value
,
Size
::
new
(
100.0
,
100.0
))
.lens
(
DialLabel
::
value
)
.background
(
solid
.clone
())
.padding
(
5.0
),
0.0
,
);
col
.add_child
(
label
.padding
(
5.0
)
.background
(
solid
.clone
()),
0.0
);
col
}
}
#[derive(Clone,
Data,
Lens)]
struct
Envelope
{
attack
:
DialLabel
,
decay
:
DialLabel
,
sustain
:
DialLabel
,
release
:
DialLabel
,
}
impl
Envelope
{
fn
new
()
->
impl
Widget
<
Envelope
>
{
let
solid
=
Color
::
rgb8
(
0x3a
,
0x3a
,
0x3a
);
Flex
::
row
()
.with_child
(
DialLabel
::
new
(
"Attack"
,
0.0
)
.lens
(
Envelope
::
attack
)
.background
(
solid
.clone
())
.padding
(
5.0
),
0.0
,
)
.with_child
(
DialLabel
::
new
(
"Decay"
,
0.0
)
.lens
(
Envelope
::
decay
)
.background
(
solid
.clone
())
.padding
(
5.0
),
0.0
,
)
.with_child
(
DialLabel
::
new
(
"Sustain"
,
0.0
)
.lens
(
Envelope
::
sustain
)
.background
(
solid
.clone
())
.padding
(
5.0
),
0.0
,
)
.with_child
(
DialLabel
::
new
(
"Release"
,
0.0
)
.lens
(
Envelope
::
release
)
.background
(
solid
.clone
())
.padding
(
5.0
),
0.0
,
)
}
}
fn
main
()
->
Result
<
(),
PlatformError
>
{
AppLauncher
::
with_window
(
WindowDesc
::
new
(
Envelope
::
new
))
.use_simple_logger
()
.launch
(
Envelope
{
attack
:
DialLabel
{
value
:
0.1
},
decay
:
DialLabel
{
value
:
0.2
},
sustain
:
DialLabel
{
value
:
0.3
},
release
:
DialLabel
{
value
:
0.4
},
})
?
;
Ok
(())
}
This diff is collapsed.
Click to expand it.
src/dial.rs
+
7
−
12
View file @
33b785d0
use
std
::
f64
::
consts
::
PI
;
use
std
::
f64
::
consts
::
PI
;
use
druid
::
kurbo
::{
BezPath
,
Line
};
use
druid
::
kurbo
::
BezPath
;
use
druid
::
widget
::{
Align
,
Flex
,
Label
,
WidgetExt
};
use
druid
::{
use
druid
::{
AppLauncher
,
BoxConstraints
,
Color
,
Data
,
Env
,
Event
,
EventCtx
,
LayoutCtx
,
LinearGradient
,
BoxConstraints
,
Color
,
Env
,
Event
,
EventCtx
,
LayoutCtx
,
PaintCtx
,
Point
,
RenderContext
,
Size
,
PaintCtx
,
PlatformError
,
Point
,
RenderContext
,
Size
,
UnitPoint
,
UpdateCtx
,
Vec2
,
WheelEvent
,
UpdateCtx
,
Vec2
,
Widget
,
Widget
,
WindowDesc
,
};
};
use
log
::
info
;
use
std
::
convert
::
From
;
#[derive(Debug,
Clone,
Default)]
#[derive(Debug,
Clone,
Default)]
pub
struct
Dial
{
pub
struct
Dial
{
// properties for the Dial
// properties for the Dial
...
@@ -17,8 +13,8 @@ pub struct Dial {
...
@@ -17,8 +13,8 @@ pub struct Dial {
}
}
impl
Dial
{
impl
Dial
{
pub
fn
new
(
size
:
Size
)
->
impl
Widget
<
f64
>
{
pub
fn
new
(
value
:
f64
,
size
:
Size
)
->
impl
Widget
<
f64
>
{
Self
{
value
:
0.0
,
size
}
Self
{
value
,
size
}
}
}
}
}
...
@@ -26,7 +22,7 @@ impl Widget<f64> for Dial {
...
@@ -26,7 +22,7 @@ impl Widget<f64> for Dial {
fn
event
(
&
mut
self
,
ctx
:
&
mut
EventCtx
,
event
:
&
Event
,
data
:
&
mut
f64
,
_env
:
&
Env
)
{
fn
event
(
&
mut
self
,
ctx
:
&
mut
EventCtx
,
event
:
&
Event
,
data
:
&
mut
f64
,
_env
:
&
Env
)
{
match
event
{
match
event
{
Event
::
Wheel
(
w
)
=>
{
Event
::
Wheel
(
w
)
=>
{
let
mut
delta
=
w
.delta.y
/
(
120
*
10
)
as
f64
;
// 1% for my machine
let
mut
delta
=
w
.delta.y
/
(
120
*
10
)
as
f64
;
// 1
0
% for my machine
if
w
.mods.shift
{
if
w
.mods.shift
{
delta
=
delta
/
10.0
;
delta
=
delta
/
10.0
;
};
};
...
@@ -59,13 +55,12 @@ impl Widget<f64> for Dial {
...
@@ -59,13 +55,12 @@ impl Widget<f64> for Dial {
}
}
fn
paint
(
&
mut
self
,
paint_ctx
:
&
mut
PaintCtx
,
data
:
&
f64
,
_env
:
&
Env
)
{
fn
paint
(
&
mut
self
,
paint_ctx
:
&
mut
PaintCtx
,
data
:
&
f64
,
_env
:
&
Env
)
{
info!
(
"dial paint"
);
// for some reason origin (0,0) seems not to point to the border not the interior
let
center
=
Point
::
new
(
self
.size.width
/
2.0
,
self
.size.height
/
2.0
);
let
center
=
Point
::
new
(
self
.size.width
/
2.0
,
self
.size.height
/
2.0
);
let
diameter
=
0.8
*
self
.size.width
/
2.0
;
let
diameter
=
0.8
*
self
.size.width
/
2.0
;
let
mut
path
=
BezPath
::
new
();
let
mut
path
=
BezPath
::
new
();
let
ambit0
=
center
+
diameter
*
Vec2
::
from_angle
(
0.0
);
let
ambit0
=
center
+
diameter
*
Vec2
::
from_angle
(
0.0
);
path
.move_to
(
ambit0
);
path
.move_to
(
ambit0
);
// Circle drawn in 36 segments
for
v
in
(
0
..
360
)
.step_by
(
10
)
{
for
v
in
(
0
..
360
)
.step_by
(
10
)
{
let
ambit
=
center
+
diameter
*
Vec2
::
from_angle
(
v
as
f64
/
360.0
*
2.0
*
PI
);
let
ambit
=
center
+
diameter
*
Vec2
::
from_angle
(
v
as
f64
/
360.0
*
2.0
*
PI
);
path
.line_to
(
ambit
);
path
.line_to
(
ambit
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment