diff --git a/abletk-common/src/brush.rs b/abletk-common/src/brush.rs new file mode 100755 index 0000000..c6810fb --- /dev/null +++ b/abletk-common/src/brush.rs @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2022 Umut İnan Erdoğan + * + * 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 for RawBrush { + fn from(brush: Brush) -> Self { + match brush { + Brush::Solid(color) => RawBrush::Solid(color.into()), + } + } +} diff --git a/abletk-common/src/lib.rs b/abletk-common/src/lib.rs index 4f39868..d922eda 100755 --- a/abletk-common/src/lib.rs +++ b/abletk-common/src/lib.rs @@ -6,9 +6,11 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +pub mod brush; pub mod color; pub mod rect; +use brush::Brush; use raw_window_handle::RawWindowHandle; use color::Color; use rect::Rect; @@ -55,6 +57,10 @@ impl Renderer { pub fn resized(&mut self, width: u32, height: u32) { 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) { self.renderer.size() diff --git a/abletk-direct2d/src/brush.rs b/abletk-direct2d/src/brush.rs new file mode 100755 index 0000000..9aca792 --- /dev/null +++ b/abletk-direct2d/src/brush.rs @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2022 Umut İnan Erdoğan + * + * 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() + ) + }, + } + } +} diff --git a/abletk-direct2d/src/lib.rs b/abletk-direct2d/src/lib.rs index 5b20b83..46ea8ec 100755 --- a/abletk-direct2d/src/lib.rs +++ b/abletk-direct2d/src/lib.rs @@ -6,39 +6,23 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +pub mod brush; + use std::mem; +use brush::{Brush, DEFAULT_BRUSH_COLOR}; use raw_window_handle::Win32Handle; use windows::{ - core::Interface, Foundation::Numerics::*, Win32::Foundation::*, + core::Interface, Win32::Foundation::*, Win32::Graphics::Direct2D::Common::*, Win32::Graphics::Direct2D::*, 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 { handle: Win32Handle, factory: ID2D1Factory1, dw_factory: IDWriteFactory, target: Option, - brush: Option, + brush: Option, stroke_style: ID2D1StrokeStyle, system_fonts: IDWriteFontCollection, text_format: IDWriteTextFormat @@ -123,7 +107,11 @@ 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) { let size = unsafe { self.target.as_ref().unwrap().GetSize() @@ -159,15 +147,10 @@ impl Renderer { &render_properties, &hwnd_render_properties) .unwrap() - }; - - let brush = unsafe { - target.CreateSolidColorBrush(&DEFAULT_BRUSH_COLOR, &DEFAULT_BRUSH_PROPERTIES) - .unwrap() - }; + }; + self.brush = Some(Brush::Solid(DEFAULT_BRUSH_COLOR).to_brush(&target)); self.target = Some(target); - self.brush = Some(brush); } } } diff --git a/src/window.rs b/src/window.rs index 6ceb075..d15cf0b 100755 --- a/src/window.rs +++ b/src/window.rs @@ -206,7 +206,7 @@ impl WindowBuilder { self.events, // todo: make this the application name 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.maximized, self.resizable,