implement brush changing
This commit is contained in:
parent
36b9c8808d
commit
37b2e3caa8
25
abletk-common/src/brush.rs
Executable file
25
abletk-common/src/brush.rs
Executable file
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
use abletk_direct2d::brush::Brush as RawBrush;
|
||||||
|
|
||||||
|
use crate::color::Color;
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub enum Brush {
|
||||||
|
Solid(Color),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Brush> for RawBrush {
|
||||||
|
fn from(brush: Brush) -> Self {
|
||||||
|
match brush {
|
||||||
|
Brush::Solid(color) => RawBrush::Solid(color.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,9 +6,11 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
pub mod brush;
|
||||||
pub mod color;
|
pub mod color;
|
||||||
pub mod rect;
|
pub mod rect;
|
||||||
|
|
||||||
|
use brush::Brush;
|
||||||
use raw_window_handle::RawWindowHandle;
|
use raw_window_handle::RawWindowHandle;
|
||||||
use color::Color;
|
use color::Color;
|
||||||
use rect::Rect;
|
use rect::Rect;
|
||||||
|
@ -56,6 +58,10 @@ impl Renderer {
|
||||||
self.renderer.resized(width, height)
|
self.renderer.resized(width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_brush(&mut self, brush: Brush) {
|
||||||
|
self.renderer.set_brush(brush.into())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> (f32, f32) {
|
pub fn size(&self) -> (f32, f32) {
|
||||||
self.renderer.size()
|
self.renderer.size()
|
||||||
}
|
}
|
||||||
|
|
54
abletk-direct2d/src/brush.rs
Executable file
54
abletk-direct2d/src/brush.rs
Executable file
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022 Umut İnan Erdoğan <umutinanerdogan@pm.me>
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::mem;
|
||||||
|
|
||||||
|
use windows::{
|
||||||
|
Foundation::Numerics::*,
|
||||||
|
Win32::Graphics::Direct2D::Common::*, Win32::Graphics::Direct2D::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// The default brush color is black.
|
||||||
|
pub(crate) const DEFAULT_BRUSH_COLOR: D2D1_COLOR_F = D2D1_COLOR_F {
|
||||||
|
r: 0.0,
|
||||||
|
g: 0.0,
|
||||||
|
b: 0.0,
|
||||||
|
a: 1.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(crate) const DEFAULT_BRUSH_PROPERTIES: D2D1_BRUSH_PROPERTIES = D2D1_BRUSH_PROPERTIES {
|
||||||
|
opacity: 1.0,
|
||||||
|
// Matrix3x2::identity() is not a const fn but it could be
|
||||||
|
//
|
||||||
|
// I (TheOddGarlic) sent windows-rs a PR and it got merged but now we wait
|
||||||
|
// for it to be included in the next release
|
||||||
|
transform: Matrix3x2 {
|
||||||
|
M11: 1.0, M12: 0.0, M21: 0.0, M22: 1.0, M31: 0.0, M32: 0.0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub enum Brush {
|
||||||
|
Solid(D2D1_COLOR_F),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Brush {
|
||||||
|
pub(crate) fn to_brush(&self, target: &ID2D1HwndRenderTarget) -> ID2D1Brush {
|
||||||
|
match self {
|
||||||
|
Brush::Solid(color) => unsafe {
|
||||||
|
mem::transmute(
|
||||||
|
target.CreateSolidColorBrush(
|
||||||
|
color,
|
||||||
|
&DEFAULT_BRUSH_PROPERTIES,
|
||||||
|
).unwrap()
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,39 +6,23 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
pub mod brush;
|
||||||
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use brush::{Brush, DEFAULT_BRUSH_COLOR};
|
||||||
use raw_window_handle::Win32Handle;
|
use raw_window_handle::Win32Handle;
|
||||||
use windows::{
|
use windows::{
|
||||||
core::Interface, Foundation::Numerics::*, Win32::Foundation::*,
|
core::Interface, Win32::Foundation::*,
|
||||||
Win32::Graphics::Direct2D::Common::*, Win32::Graphics::Direct2D::*,
|
Win32::Graphics::Direct2D::Common::*, Win32::Graphics::Direct2D::*,
|
||||||
Win32::Graphics::DirectWrite::*, Win32::UI::WindowsAndMessaging::*,
|
Win32::Graphics::DirectWrite::*, Win32::UI::WindowsAndMessaging::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The default brush color is black.
|
|
||||||
const DEFAULT_BRUSH_COLOR: D2D1_COLOR_F = D2D1_COLOR_F {
|
|
||||||
r: 0.0,
|
|
||||||
g: 0.0,
|
|
||||||
b: 0.0,
|
|
||||||
a: 1.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEFAULT_BRUSH_PROPERTIES: D2D1_BRUSH_PROPERTIES = D2D1_BRUSH_PROPERTIES {
|
|
||||||
opacity: 1.0,
|
|
||||||
// Matrix3x2::identity() is not a const fn but it could be
|
|
||||||
//
|
|
||||||
// I (TheOddGarlic) sent windows-rs a PR and it got merged but now we wait
|
|
||||||
// for it to be included in the next release
|
|
||||||
transform: Matrix3x2 {
|
|
||||||
M11: 1.0, M12: 0.0, M21: 0.0, M22: 1.0, M31: 0.0, M32: 0.0
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
handle: Win32Handle,
|
handle: Win32Handle,
|
||||||
factory: ID2D1Factory1,
|
factory: ID2D1Factory1,
|
||||||
dw_factory: IDWriteFactory,
|
dw_factory: IDWriteFactory,
|
||||||
target: Option<ID2D1HwndRenderTarget>,
|
target: Option<ID2D1HwndRenderTarget>,
|
||||||
brush: Option<ID2D1SolidColorBrush>,
|
brush: Option<ID2D1Brush>,
|
||||||
stroke_style: ID2D1StrokeStyle,
|
stroke_style: ID2D1StrokeStyle,
|
||||||
system_fonts: IDWriteFontCollection,
|
system_fonts: IDWriteFontCollection,
|
||||||
text_format: IDWriteTextFormat
|
text_format: IDWriteTextFormat
|
||||||
|
@ -124,6 +108,10 @@ impl Renderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_brush(&mut self, brush: Brush) {
|
||||||
|
self.brush = Some(brush.to_brush(self.target.as_ref().unwrap()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> (f32, f32) {
|
pub fn size(&self) -> (f32, f32) {
|
||||||
let size = unsafe {
|
let size = unsafe {
|
||||||
self.target.as_ref().unwrap().GetSize()
|
self.target.as_ref().unwrap().GetSize()
|
||||||
|
@ -161,13 +149,8 @@ impl Renderer {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let brush = unsafe {
|
self.brush = Some(Brush::Solid(DEFAULT_BRUSH_COLOR).to_brush(&target));
|
||||||
target.CreateSolidColorBrush(&DEFAULT_BRUSH_COLOR, &DEFAULT_BRUSH_PROPERTIES)
|
|
||||||
.unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
self.target = Some(target);
|
self.target = Some(target);
|
||||||
self.brush = Some(brush);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -206,7 +206,7 @@ impl WindowBuilder {
|
||||||
self.events,
|
self.events,
|
||||||
// todo: make this the application name
|
// todo: make this the application name
|
||||||
self.always_on_top,
|
self.always_on_top,
|
||||||
self.background.unwrap_or(Color::RGB(255.0, 255.0, 255.0)),
|
self.background.unwrap_or(Color::RGB(1.0, 1.0, 1.0)),
|
||||||
self.decorations,
|
self.decorations,
|
||||||
self.maximized,
|
self.maximized,
|
||||||
self.resizable,
|
self.resizable,
|
||||||
|
|
Loading…
Reference in a new issue