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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::{cell::RefCell, collections::HashMap, convert::TryFrom, fmt};

use log::info;
use termion::clear;

use crate::model::{AreaId, Game, Suit};

use super::{
    blank::BlankWidget, card::CARD_SIZE, geometry, help::HelpWidget, stack::StackWidget,
    win::WinWidget, DisplayState, Widget,
};

lazy_static! {
    static ref STOCK_COORDS: geometry::Point2D<u16> = geometry::point2(2, 0);
    static ref TALON_COORDS: geometry::Point2D<u16> = geometry::point2(13, 0);
    static ref FOUNDATION_COORDS: geometry::Point2D<u16> = geometry::point2(35, 0);
    static ref TABLEAUX_COORDS: geometry::Point2D<u16> = geometry::point2(2, 5);
    static ref COLUMN_OFFSET: geometry::Vector2D<u16> = geometry::vec2(3, 0);
}

#[derive(Debug, Default)]
pub struct GameWidgetStateValue {
    bounds_cache: HashMap<AreaId, geometry::Rect<u16>>,
    prev_display_state: Option<DisplayState>,
    prev_bounds: Option<geometry::Rect<u16>>,
}

#[derive(Debug, Default)]
pub struct GameWidgetState {
    cell: RefCell<GameWidgetStateValue>,
}

#[derive(Debug)]
pub struct GameWidget<'a> {
    pub area_ids: &'a [AreaId],
    pub bounds: geometry::Rect<u16>,
    pub game: &'a Game,
    pub display_state: DisplayState,
    pub widget_state: &'a GameWidgetState,
}

impl<'a> Widget for GameWidget<'a> {
    fn bounds(&self) -> geometry::Rect<u16> {
        self.bounds
    }
}

impl<'a> fmt::Display for GameWidget<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let full_refresh_required = self.is_full_refresh_required();

        if full_refresh_required {
            // We're going to clear the whole terminal, so no need to remember where the widgets were.
            self.widget_state.cell.borrow_mut().bounds_cache.clear();

            let clear = clear::All;
            write!(fmt, "{}", clear)?;
        }

        match self.display_state {
            DisplayState::Playing => {
                let game_area_ids = self.game.area_ids();

                let area_ids = if full_refresh_required {
                    &game_area_ids
                } else {
                    self.area_ids
                };

                for area_id in area_ids {
                    self.write_area(*area_id, fmt)?;
                }
            }
            DisplayState::HelpMessageOpen => {
                self.write_help(fmt)?;
            }
            DisplayState::WinMessageOpen => {
                self.write_win(fmt)?;
            }
            _ => {}
        }

        let mut state = self.widget_state.cell.borrow_mut();
        state.prev_bounds = Some(self.bounds);
        state.prev_display_state = Some(self.display_state);

        Ok(())
    }
}

impl<'a> GameWidget<'a> {
    fn is_full_refresh_required(&self) -> bool {
        let state = self.widget_state.cell.borrow();

        let bounds_changed = state
            .prev_bounds
            .map(|prev_bounds| prev_bounds != self.bounds)
            .unwrap_or(true);
        let display_state_changed = state
            .prev_display_state
            .map(|prev_display_state| {
                prev_display_state != self.display_state && self.state_requires_refresh()
            })
            .unwrap_or(true);

        bounds_changed || display_state_changed
    }

    fn state_requires_refresh(&self) -> bool {
        match self.display_state {
            DisplayState::WinMessageOpen => false,
            _ => true,
        }
    }

    fn write_area(&self, area_id: AreaId, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let bounds = bounds_for_area(area_id, self.bounds);

        info!("Printing {:?} at {:?}", area_id, bounds.origin);

        // Let's just go ahead and borrow mutably right away. We'll need it at the end of the
        // method when we update the bounds, anyway. Better to know up-front if there's any
        // weird aliasing going on.
        let bounds_cache = &mut self.widget_state.cell.borrow_mut().bounds_cache;

        if let Some(&bounds) = bounds_cache.get(&area_id) {
            let blank_widget = BlankWidget { bounds };
            write!(fmt, "{}", blank_widget)?;
        }

        if let Some(stack) = self.game.stack(area_id) {
            let stack_widget = StackWidget {
                bounds,
                stack: &stack,
            };

            let new_bounds = stack_widget.bounds();
            write!(fmt, "{}", stack_widget)?;

            // Ignore return value, because we don't need the old value.
            bounds_cache.insert(area_id, new_bounds);
        } else {
            bounds_cache.remove(&area_id);
        }

        Ok(())
    }

    fn write_help(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let widget = HelpWidget {
            bounds: self.bounds,
        };

        write!(fmt, "{}", widget)?;

        Ok(())
    }

    fn write_win(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        let widget = WinWidget {
            bounds: self.bounds,
        };

        write!(fmt, "{}", widget)?;

        Ok(())
    }
}

fn bounds_for_area(area_id: AreaId, widget_bounds: geometry::Rect<u16>) -> geometry::Rect<u16> {
    let top_left = coords_for_area(area_id);

    match area_id {
        AreaId::Stock => {
            let right = coords_for_area(AreaId::Talon).x - 1;
            let bottom = coords_for_area(AreaId::Tableaux(0)).y - 1;
            let bottom_right = geometry::point2(right, bottom);

            geometry::Box2D::new(top_left, bottom_right).to_rect()
        }
        AreaId::Talon => {
            let first_suit = Suit::try_from(0).unwrap();
            let right = coords_for_area(AreaId::Foundation(first_suit)).x - 1;
            let bottom = coords_for_area(AreaId::Tableaux(0)).y - 1;
            let bottom_right = geometry::point2(right, bottom);

            geometry::Box2D::new(top_left, bottom_right).to_rect()
        }
        AreaId::Foundation(suit) => {
            let next_suit = Suit::try_from(u8::from(suit) + 1).ok();
            let right = if let Some(next_suit) = next_suit {
                coords_for_area(AreaId::Foundation(next_suit)).x - 1
            } else {
                widget_bounds.max_x()
            };
            let bottom = coords_for_area(AreaId::Tableaux(0)).y - 1;
            let bottom_right = geometry::point2(right, bottom);

            geometry::Box2D::new(top_left, bottom_right).to_rect()
        }
        AreaId::Tableaux(index) => {
            let right = coords_for_area(AreaId::Tableaux(index + 1)).x - 1;
            let bottom = widget_bounds.max_y();
            let bottom_right = geometry::point2(right, bottom);

            geometry::Box2D::new(top_left, bottom_right).to_rect()
        }
    }
}

fn coords_for_area(area_id: AreaId) -> geometry::Point2D<u16> {
    let card_offset = geometry::vec2(CARD_SIZE.width, 0);

    match area_id {
        AreaId::Stock => *STOCK_COORDS,
        AreaId::Talon => *TALON_COORDS,
        AreaId::Foundation(suit) => {
            *FOUNDATION_COORDS + (card_offset + *COLUMN_OFFSET) * u16::from(u8::from(suit))
        }
        AreaId::Tableaux(index) => {
            *TABLEAUX_COORDS + (card_offset + *COLUMN_OFFSET) * u16::from(index)
        }
    }
}