Skip to content

Commit

Permalink
Implemented closing buttons on tab control, fixed some div-2-rounding…
Browse files Browse the repository at this point in the history
… bugs
  • Loading branch information
T4r4sB committed Jun 15, 2022
1 parent e331225 commit 856a634
Show file tree
Hide file tree
Showing 15 changed files with 861 additions and 470 deletions.
639 changes: 439 additions & 200 deletions application/src/gui/gui_components.rs

Large diffs are not rendered by default.

60 changes: 38 additions & 22 deletions application/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl std::fmt::Debug for HotkeyCallback {
pub struct GuiControlBase {
pub(crate) size_constraints: SizeConstraints,
pub(crate) current_size_constraints: SizeConstraints,
pub(crate) minimal_size: Position,
pub(crate) self_ref: Option<Weak<RefCell<dyn GuiControl>>>,
pub visible: bool,
pub(crate) need_redraw: bool,
Expand All @@ -82,6 +83,7 @@ impl GuiControlBase {
let result = Self {
size_constraints,
current_size_constraints: size_constraints,
minimal_size: (size_constraints.0.absolute, size_constraints.1.absolute),
self_ref: None,
visible: true,
need_redraw: false,
Expand All @@ -100,14 +102,16 @@ impl GuiControlBase {
result
}

pub fn erase_background(buffer: &mut ImageViewMut<u32>, theme: &GuiColorTheme) {
buffer.fill(|p| *p = theme.background);
pub fn set_size_constaints(&mut self, constraints: SizeConstraints) {
self.size_constraints = constraints;
self.current_size_constraints = constraints;
self.minimal_size = (constraints.0.absolute, constraints.1.absolute);
}
}

pub enum GuiMessage<'i, 'j> {
Draw(&'i mut ImageViewMut<'j, u32>, &'i GuiColorTheme, bool),
UpdateSizeConstraints(&'i mut SizeConstraints),
UpdateSizeConstraints,
FindDestination(&'i mut Rc<RefCell<dyn GuiControl>>, Position),
RectUpdated,
FocusLose(JobSystem),
Expand Down Expand Up @@ -139,39 +143,33 @@ pub(crate) fn avg_color(color1: u32, color2: u32) -> u32 {
pub struct GuiColorTheme {
pub background: u32,
pub font: u32,
pub hotkey: u32,
pub splitter: u32,
pub highlight: u32,
pub pressed: u32,
pub selected: u32,
pub inactive: u32,
pub edit_highlight: u32,
pub edit_focused: u32,
}

pub static DARK_THEME: GuiColorTheme = GuiColorTheme {
background: 0x000000,
font: 0xCCCCCC,
hotkey: 0xAACCAA,
splitter: 0xAACCAA,
highlight: 0xAAAAAA,
highlight: 0x9999CC,
pressed: 0xFFFFFF,
selected: 0x66CC66,
inactive: 0x444444,
edit_highlight: 0x666666,
edit_focused: 0x888888,
inactive: 0x555555,
edit_focused: 0x999999,
};

pub static LIGHT_THEME: GuiColorTheme = GuiColorTheme {
background: 0xFFFFFF,
font: 0x000000,
hotkey: 0x664422,
splitter: 0x664422,
highlight: 0x666666,
highlight: 0x779966,
pressed: 0x222222,
selected: 0xCC8844,
inactive: 0xAAAAAA,
edit_highlight: 0xCCCCCC,
edit_focused: 0xEEEEEE,
};

Expand Down Expand Up @@ -231,6 +229,13 @@ macro_rules! set_property {
};
}

#[derive(Debug, Copy, Clone)]
pub enum EmptySpaceState {
Empty,
Inactive,
Splitter,
}

impl GuiSystem {
pub fn new(job_system: JobSystem) -> Self {
Self {
Expand Down Expand Up @@ -269,12 +274,6 @@ impl GuiSystem {
result
}

pub fn get_size_constraints(control: &mut dyn GuiControl) -> SizeConstraints {
let mut size_constraints = control.get_base_mut().size_constraints;
control.on_message(GuiMessage::UpdateSizeConstraints(&mut size_constraints));
size_constraints
}

fn get_focus(&self) -> Option<Rc<RefCell<dyn GuiControl>>> {
self.focus.as_ref().and_then(Weak::upgrade).clone()
}
Expand Down Expand Up @@ -330,15 +329,32 @@ impl GuiSystem {
self.color_theme = color_theme;
}

pub fn get_color(state: EmptySpaceState, color_theme: &GuiColorTheme) -> u32 {
match state {
EmptySpaceState::Empty => color_theme.background,
EmptySpaceState::Inactive => color_theme.inactive,
EmptySpaceState::Splitter => color_theme.splitter,
}
}

pub fn erase_background(
buffer: &mut ImageViewMut<u32>,
empty_space_state: EmptySpaceState,
theme: &GuiColorTheme,
) {
let color = GuiSystem::get_color(empty_space_state, theme);
buffer.fill(|p| *p = color);
}

pub fn on_resize(&mut self) {
self.updated = false;
}

pub fn get_minimal_size(&self) -> Position {
pub fn get_minimal_size_of_system(&self) -> Position {
if let Some(root) = &self.root {
let mut root = root.borrow_mut();
let size_constraints = Self::get_size_constraints(root.deref_mut());
(size_constraints.0.absolute, size_constraints.1.absolute)
root.on_message(GuiMessage::UpdateSizeConstraints);
root.get_base_mut().minimal_size
} else {
(0, 0)
}
Expand Down
4 changes: 3 additions & 1 deletion application/src/job_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ impl JobSystem {
self.jobs.borrow_mut().push(callback);
}

pub fn run_all(&self) {
pub fn run_all(&self) -> bool {
let result = !self.jobs.borrow().is_empty();
for callback in self.jobs.borrow().iter() {
callback();
}
self.jobs.borrow_mut().clear();
result
}
}
9 changes: 9 additions & 0 deletions application/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ impl Hotkey {
}
}

pub fn ctrl_shift(key: Key) -> Self {
Self {
key,
ctrl: true,
alt: false,
shift: true,
}
}

pub fn no_modifiers(&self) -> bool {
!(self.ctrl || self.alt || self.shift)
}
Expand Down
8 changes: 4 additions & 4 deletions gui_test/src/bottom_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn create_bottom_panel(

let _hr = bottom_panel
.borrow_mut()
.add_child(ColorBox::new(SizeConstraints(
.add_child(EmptySpace::new_splitter(SizeConstraints(
SizeConstraint::fixed(1),
SizeConstraint::flexible(0),
)));
Expand All @@ -55,7 +55,7 @@ pub fn create_bottom_panel(

let _es = bottom_panel
.borrow_mut()
.add_child(EmptySpace::new(SizeConstraints(
.add_child(EmptySpace::new_empty(SizeConstraints(
SizeConstraint::fixed(font_symbol_size.0 as i32 / 2),
SizeConstraint::flexible(0),
)));
Expand All @@ -70,7 +70,7 @@ pub fn create_bottom_panel(

let _es = bottom_panel
.borrow_mut()
.add_child(EmptySpace::new(SizeConstraints(
.add_child(EmptySpace::new_empty(SizeConstraints(
SizeConstraint::fixed(font_symbol_size.0 as i32 / 2),
SizeConstraint::flexible(0),
)));
Expand All @@ -85,7 +85,7 @@ pub fn create_bottom_panel(
);
let _es = bottom_panel
.borrow_mut()
.add_child(EmptySpace::new(SizeConstraints(
.add_child(EmptySpace::new_empty(SizeConstraints(
SizeConstraint::fixed(font_symbol_size.0 as i32 / 2),
SizeConstraint::flexible(0),
)));
Expand Down
4 changes: 3 additions & 1 deletion gui_test/src/draw_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub fn create_draw_menu(parent: &mut TabControl, font: &Font) -> Rc<RefCell<Cont
let menu_caption = "Рисовать";
let draw_menu = parent.add_tab(
menu_caption.to_string(),
font.get_size(menu_caption).0 as i32 + font_height,
GuiSystem::default_size(&menu_caption, None, &font)
.0
.absolute,
Container::new(
SizeConstraints(
SizeConstraint::flexible(0),
Expand Down
137 changes: 137 additions & 0 deletions gui_test/src/editor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::cell::RefCell;
use std::rc::Rc;

use application::gui::*;

use crate::config::*;
use curves::curves::*;
use curves::points::*;
use curves::render::*;

pub struct CadColorTheme {
line_color: u32,
line_aa_color: u32,
}

static CAD_DARK_THEME: CadColorTheme = CadColorTheme {
line_color: 0x88AA88,
line_aa_color: 0xCCFFCC,
};

static CAD_BEIGE_THEME: CadColorTheme = CadColorTheme {
line_color: 0x442200,
line_aa_color: 0x442200,
};

static CAD_LIGHT_THEME: CadColorTheme = CadColorTheme {
line_color: 0x000000,
line_aa_color: 0x000000,
};

static BEIGE_THEME: GuiColorTheme = GuiColorTheme {
background: 0xDDCCAA,
font: 0x000000,
splitter: 0x224466,
highlight: 0x88AA66,
pressed: 0x222222,
selected: 0x4499CC,
inactive: 0xAA9988,
edit_focused: 0xEEEEEE,
};

pub fn get_cad_color_theme(config: &Config) -> &'static CadColorTheme {
match config.color_theme {
ColorTheme::Dark => &CAD_DARK_THEME,
ColorTheme::Beige => &CAD_BEIGE_THEME,
ColorTheme::Light => &CAD_LIGHT_THEME,
}
}

pub fn get_gui_color_theme(config: &Config) -> &'static GuiColorTheme {
match config.color_theme {
ColorTheme::Dark => &DARK_THEME,
ColorTheme::Beige => &BEIGE_THEME,
ColorTheme::Light => &LIGHT_THEME,
}
}

pub struct Editor {
pub last_file_id: usize,
pub config: Rc<RefCell<Config>>,
}

impl Editor {
pub fn new(config: Config) -> Self {
Self {
last_file_id: 0,
config: Rc::new(RefCell::new(config)),
}
}

pub fn get_next_id(&mut self) -> usize {
self.last_file_id += 1;
self.last_file_id
}
}

pub struct CadView {
base: GuiControlBase,
editor: Rc<RefCell<Editor>>,
}

impl std::fmt::Debug for CadView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.base.fmt(f)
}
}

impl CadView {
pub fn new(size_constraints: SizeConstraints, editor: Rc<RefCell<Editor>>) -> Self {
Self {
base: GuiControlBase::new(size_constraints),
editor,
}
}
}

impl GuiControl for CadView {
fn get_base_mut(&mut self) -> &mut GuiControlBase {
&mut self.base
}

fn on_message(&mut self, m: GuiMessage) -> bool {
match m {
GuiMessage::Draw(buf, theme, force) => {
if self.base.can_draw(force) {
GuiSystem::erase_background(buf, EmptySpaceState::Empty, theme);
let curve = Curve::<f32>::circle(Point::new(100.0, 100.0), 50.0);
let e = Entity::Curve(curve);
let mut buffer = vec![(0, 0); buf.get_size().1 * 4];
let cad_color_theme =
get_cad_color_theme(&self.editor.borrow().config.borrow());

match self.editor.borrow().config.borrow().curves_aa_mode {
CurvesAAMode::NoAntiAliasing => {
draw_curve(buf, &e, cad_color_theme.line_color, 1.0, &mut buffer, 1)
}
CurvesAAMode::AntiAliasingX2 => {
draw_curve(buf, &e, cad_color_theme.line_aa_color, 1.0, &mut buffer, 2)
}
CurvesAAMode::AntiAliasingX4 => {
draw_curve(buf, &e, cad_color_theme.line_aa_color, 1.0, &mut buffer, 4)
}
};
}

return true;
}
GuiMessage::MouseDown(_) => {
return true;
}
GuiMessage::MouseUp(_, _) => {
return true;
}
_ => return false,
}
}
}
Loading

0 comments on commit 856a634

Please sign in to comment.