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
174
175
176
177
178
179
180
181
//! This module contains all render objects used in OrbTk. Render objects are used to define how to draw parts of a widget.

use std::{any::Any, cell::RefCell, collections::BTreeMap, rc::Rc};

use crate::{css_engine::*, prelude::*, shell::WindowShell, utils::*};

pub use self::clear::*;
pub use self::default::*;
pub use self::font_icon::*;
pub use self::image::*;
pub use self::pipeline::*;
pub use self::rectangle::*;
pub use self::text::*;

mod clear;
mod default;
mod font_icon;
mod image;
mod pipeline;
mod rectangle;
mod text;

pub trait RenderObject: Any {
    fn render(
        &self,
        shell: &mut WindowShell<WindowAdapter>,
        entity: Entity,
        ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
        render_objects: &RefCell<BTreeMap<Entity, Box<dyn RenderObject>>>,
        layouts: &Rc<RefCell<BTreeMap<Entity, Box<dyn Layout>>>>,
        handlers: &Rc<RefCell<EventHandlerMap>>,
        states: &Rc<RefCell<BTreeMap<Entity, Box<dyn State>>>>,
        theme: &ThemeValue,
        offsets: &mut BTreeMap<Entity, (f64, f64)>,
        debug: bool,
    ) {
        let mut global_position = Point::default();

        if let Some(parent) = ecm.entity_store().parent[&entity] {
            if let Some(offset) = offsets.get(&parent) {
                global_position = Point::new(offset.0, offset.1);
            }
        }

        if let Ok(visibility) = ecm
            .component_store()
            .get::<Visibility>("visibility", entity)
        {
            if *visibility != Visibility::Visible {
                return;
            }
        } else {
            return;
        }

        shell.render_context_2_d().begin_path();
        shell.render_context_2_d().set_alpha(
            *ecm.component_store()
                .get::<f32>("opacity", entity)
                .unwrap_or(&1.0),
        );

        // Could be unwrap because every widget has the clip property
        let clip = *ecm.component_store().get::<bool>("clip", entity).unwrap();
        if clip {
            if let Ok(bounds) = ecm.component_store().get::<Rectangle>("bounds", entity) {
                shell.render_context_2_d().save();
                shell.render_context_2_d().rect(
                    global_position.x + bounds.x(),
                    global_position.y + bounds.y(),
                    bounds.width(),
                    bounds.height(),
                );
                shell.render_context_2_d().clip();
            }
        }

        self.render_self(
            &mut Context::new(
                (entity, ecm),
                shell,
                &theme,
                render_objects,
                &mut layouts.borrow_mut(),
                &mut handlers.borrow_mut(),
                &states,
                &mut BTreeMap::new(),
            ),
            &global_position,
        );

        let mut global_pos = (0.0, 0.0);

        if let Ok(bounds) = ecm.component_store().get::<Rectangle>("bounds", entity) {
            global_pos = (
                global_position.x + bounds.x(),
                global_position.y + bounds.y(),
            );
            offsets.insert(entity, global_pos);
        }

        if let Ok(g_pos) = ecm
            .component_store_mut()
            .get_mut::<Point>("position", entity)
        {
            g_pos.x = global_pos.0;
            g_pos.y = global_pos.1;
        }

        self.render_children(
            shell,
            entity,
            ecm,
            render_objects,
            layouts,
            handlers,
            states,
            theme,
            offsets,
            debug,
        );

        shell.render_context_2_d().close_path();

        if clip {
            shell.render_context_2_d().restore();
        }

        // render debug border for each widget
        if debug {
            if let Ok(bounds) = ecm.component_store().get::<Rectangle>("bounds", entity) {
                let selector = Selector::from("debug-border");
                let brush = theme.brush("border-color", &selector).unwrap();
                shell.render_context_2_d().begin_path();
                shell.render_context_2_d().set_stroke_style(brush);
                shell.render_context_2_d().stroke_rect(
                    global_position.x + bounds.x(),
                    global_position.y + bounds.y(),
                    bounds.width(),
                    bounds.height(),
                );
                shell.render_context_2_d().close_path();
            }
        }
    }

    fn render_self(&self, _: &mut Context<'_>, _: &Point) {}

    fn render_children(
        &self,
        shell: &mut WindowShell<WindowAdapter>,
        entity: Entity,
        ecm: &mut EntityComponentManager<Tree, StringComponentStore>,
        render_objects: &RefCell<BTreeMap<Entity, Box<dyn RenderObject>>>,
        layouts: &Rc<RefCell<BTreeMap<Entity, Box<dyn Layout>>>>,
        handlers: &Rc<RefCell<EventHandlerMap>>,
        states: &Rc<RefCell<BTreeMap<Entity, Box<dyn State>>>>,
        theme: &ThemeValue,
        offsets: &mut BTreeMap<Entity, (f64, f64)>,
        debug: bool,
    ) {
        for index in 0..ecm.entity_store().children[&entity].len() {
            let child = ecm.entity_store().children[&entity][index];

            if let Some(render_object) = render_objects.borrow().get(&child) {
                render_object.render(
                    shell,
                    child,
                    ecm,
                    render_objects,
                    layouts,
                    handlers,
                    states,
                    theme,
                    offsets,
                    debug,
                );
            }
        }
    }
}