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
// Pushrod Widget Library
// Text Widget
//
// 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.

use crate::render::callbacks::CallbackRegistry;
use crate::render::layout_cache::LayoutContainer;
use crate::render::widget::*;
use crate::render::widget_cache::WidgetContainer;
use crate::render::widget_config::*;
use crate::render::{Points, Size};

use sdl2::render::{Canvas, TextureQuery};
use sdl2::ttf::FontStyle;
use sdl2::video::Window;

use sdl2::rect::Rect;
use std::any::Any;
use std::collections::HashMap;
use std::path::Path;

/// This enum is used by the `TextWidget`, which controls the justification of the text being
/// rendered within the bounds of the `Widget`.
pub enum TextJustify {
    /// Left-justified text.
    Left,

    /// Center-justified text: `(total width - text width) / 2`
    Center,

    /// Right-justified text: `(total width - text width)`
    Right,
}

/// This is the storage object for the `TextWidget`.  It stores the config, properties, callback registry,
/// the font name, style, size, justification, and text message.
pub struct TextWidget {
    config: WidgetConfig,
    system_properties: HashMap<i32, String>,
    callback_registry: CallbackRegistry,
    font_name: String,
    font_style: FontStyle,
    font_size: i32,
    justification: TextJustify,
    msg: String,
}

/// Creates a new `TextWidget`, which draws a unit of text on the screen, given the specified font,
/// size, justification, and layout coordinates.
impl TextWidget {
    /// Creates a new `TextWidget` object.  Requires the name of the font (the path to the font file),
    /// the style of font (`sdl2::ttf::FontStyle`), the size in pixels of the font, the `TextJustify`
    /// layout of the font, the message to display, and the x, y, w, h coordinates of the text.
    pub fn new(
        font_name: String,
        font_style: FontStyle,
        font_size: i32,
        justification: TextJustify,
        msg: String,
        points: Points,
        size: Size,
    ) -> Self {
        Self {
            config: WidgetConfig::new(points.clone(), size.clone()),
            system_properties: HashMap::new(),
            callback_registry: CallbackRegistry::new(),
            font_name,
            font_style,
            font_size,
            justification,
            msg: msg.clone(),
        }
    }

    /// Changes the text displayed in the body of the `Widget`.
    pub fn set_text(&mut self, msg: String) {
        self.msg = msg.clone();
        self.get_config().set_invalidated(true);
    }

    /// Retrieves the text currently being displayed in the `TextWidget`.
    pub fn get_text(&self) -> String {
        self.msg.clone()
    }
}

/// This is the `Widget` implementation of the `TextWidget`.  Text is rendered onto a 3D texture, then
/// copied to the canvas after rendering.  It uses blended mode texture mapping, which may be slow (as
/// described by the SDL2 documentation), so this might change later to use 8 bit color mapping.
impl Widget for TextWidget {
    fn draw(&mut self, c: &mut Canvas<Window>) {
        let base_color = self.get_color(CONFIG_COLOR_BASE);
        let text_max_width =
            self.get_size(CONFIG_SIZE)[0] - ((self.get_numeric(CONFIG_BORDER_WIDTH) * 2) as u32);

        let ttf_context = sdl2::ttf::init().map_err(|e| e.to_string()).unwrap();
        let texture_creator = c.texture_creator();
        let mut font = ttf_context
            .load_font(Path::new(&self.font_name), self.font_size as u16)
            .unwrap();
        let font_color = self.get_color(CONFIG_COLOR_TEXT);

        font.set_style(self.font_style);

        let surface = font
            .render(&self.msg)
            .blended_wrapped(font_color, text_max_width)
            .map_err(|e| e.to_string())
            .unwrap();
        let texture = texture_creator
            .create_texture_from_surface(&surface)
            .map_err(|e| e.to_string())
            .unwrap();

        let TextureQuery { width, height, .. } = texture.query();

        let texture_y = self.get_config().to_y(0);
        let widget_w = self.get_size(CONFIG_SIZE)[0] as i32;
        let texture_x = match self.justification {
            TextJustify::Left => self.get_config().to_x(0),

            TextJustify::Right => self.get_config().to_x(widget_w - width as i32),

            TextJustify::Center => self.get_config().to_x((widget_w - width as i32) / 2),
        };

        c.set_draw_color(base_color);
        c.fill_rect(self.get_drawing_area()).unwrap();

        c.copy(
            &texture,
            None,
            Rect::new(texture_x, texture_y, width, height),
        )
        .unwrap();
    }

    /// Monitors for changes in the text, color changes, or font sizes.
    fn on_config_changed(&mut self, _k: u8, _v: Config) {
        match _k {
            CONFIG_COLOR_TEXT => self.get_config().set_invalidated(true),
            CONFIG_COLOR_BASE => self.get_config().set_invalidated(true),
            CONFIG_FONT_SIZE => {
                if let Config::Numeric(size) = _v {
                    self.font_size = size;
                    self.get_config().set_invalidated(true);
                }
            }
            CONFIG_TEXT => {
                if let Config::Text(text) = _v {
                    self.msg = text.clone();
                    self.get_config().set_invalidated(true);
                }
            }

            _ => (),
        };
    }

    default_widget_functions!();
    default_widget_properties!();
    default_widget_callbacks!();
}