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
use std::fmt::Debug;

use figures::units::{Px, UPx};
use figures::{Point, Size};
use intentional::Cast;
use kludgine::app::winit::event::{ElementState, MouseScrollDelta, TouchPhase};
use kludgine::app::winit::window::CursorIcon;
use kludgine::tilemap;
use kludgine::tilemap::TileMapFocus;

use crate::context::{EventContext, GraphicsContext, LayoutContext, Trackable};
use crate::tick::Tick;
use crate::value::{Dynamic, IntoValue, Value};
use crate::widget::{EventHandling, Widget, HANDLED, IGNORED};
use crate::window::{DeviceId, KeyEvent};
use crate::ConstraintLimit;

/// A layered tile-based 2d game surface.
#[derive(Debug)]
#[must_use]
pub struct TileMap<Layers> {
    layers: Value<Layers>,
    focus: Value<TileMapFocus>,
    zoom: f32,
    tick: Option<Tick>,
}

impl<Layers> TileMap<Layers> {
    fn construct(layers: Value<Layers>) -> Self {
        Self {
            layers,
            focus: Value::default(),
            zoom: 1.,
            tick: None,
        }
    }

    /// Returns a new tilemap that contains dynamic layers.
    pub fn dynamic(layers: Dynamic<Layers>) -> Self {
        Self::construct(Value::Dynamic(layers))
    }

    /// Returns a new tilemap that renders `layers`.
    pub fn new(layers: Layers) -> Self {
        Self::construct(Value::Constant(layers))
    }

    /// Sets the camera's focus and returns self.
    ///
    /// The tilemap will ensure that `focus` is centered.
    // TODO how do we allow the camera to "lag" for juice effects?
    pub fn focus_on(mut self, focus: impl IntoValue<TileMapFocus>) -> Self {
        self.focus = focus.into_value();
        self
    }

    /// Associates a [`Tick`] with this widget and returns self.
    pub fn tick(mut self, tick: Tick) -> Self {
        self.tick = Some(tick);
        self
    }
}

impl<Layers> Widget for TileMap<Layers>
where
    Layers: tilemap::Layers,
{
    fn redraw(&mut self, context: &mut GraphicsContext<'_, '_, '_, '_>) {
        let focus = self.focus.get();
        // TODO this needs to be updated to support being placed in side of a scroll view.
        let redraw_after = match &mut self.layers {
            Value::Constant(layers) => tilemap::draw(
                layers,
                focus,
                self.zoom,
                context.elapsed(),
                context.gfx.inner_graphics(),
            ),
            Value::Dynamic(layers) => {
                let mut layers = layers.lock();
                layers.prevent_notifications();
                tilemap::draw(
                    &mut *layers,
                    focus,
                    self.zoom,
                    context.elapsed(),
                    context.gfx.inner_graphics(),
                )
            }
        };

        context.draw_focus_ring();

        if let Some(tick) = &self.tick {
            // When we are driven by a tick, we ignore all other sources of
            // refreshes.
            tick.rendered(context);
        } else {
            if let Some(redraw_after) = redraw_after {
                context.redraw_in(redraw_after);
            }
            self.focus.redraw_when_changed(context);
            self.layers.redraw_when_changed(context);
        }
    }

    fn accept_focus(&mut self, _context: &mut EventContext<'_>) -> bool {
        true
    }

    fn hit_test(
        &mut self,
        _location: figures::Point<figures::units::Px>,
        _context: &mut EventContext<'_>,
    ) -> bool {
        true
    }

    fn layout(
        &mut self,
        available_space: Size<ConstraintLimit>,
        _context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        Size::new(available_space.width.max(), available_space.height.max())
    }

    fn mouse_wheel(
        &mut self,
        _device_id: DeviceId,
        delta: MouseScrollDelta,
        _phase: TouchPhase,
        context: &mut EventContext<'_>,
    ) -> EventHandling {
        let amount = match delta {
            MouseScrollDelta::LineDelta(_, lines) => lines,
            MouseScrollDelta::PixelDelta(px) => px.y.cast::<f32>() / 16.0,
        };

        self.zoom += self.zoom * 0.1 * amount;

        context.set_needs_redraw();
        HANDLED
    }

    fn hover(&mut self, local: Point<Px>, context: &mut EventContext<'_>) -> Option<CursorIcon> {
        if let Some(tick) = &self.tick {
            let size = context.last_layout().map(|rect| rect.size)?;

            let world =
                tilemap::translate_coordinates(local, context.kludgine.scale(), self.zoom, size);
            let offset = self
                .layers
                .map(|layers| self.focus.get().world_coordinate(layers));

            tick.set_cursor_position(Some(world + offset));
        }

        None
    }

    fn unhover(&mut self, _context: &mut EventContext<'_>) {
        if let Some(tick) = &self.tick {
            tick.set_cursor_position(None);
        }
    }

    fn keyboard_input(
        &mut self,
        _device_id: DeviceId,
        input: KeyEvent,
        _is_synthetic: bool,
        _context: &mut EventContext<'_>,
    ) -> EventHandling {
        if let Some(tick) = &self.tick {
            tick.key_input(&input)?;
        }

        IGNORED
    }

    fn mouse_down(
        &mut self,
        _location: Point<Px>,
        _device_id: DeviceId,
        button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) -> EventHandling {
        if let Some(tick) = &self.tick {
            tick.mouse_button(button, ElementState::Pressed);
            context.focus();
            HANDLED
        } else {
            IGNORED
        }
    }

    fn mouse_up(
        &mut self,
        _location: Option<Point<Px>>,
        _device_id: DeviceId,
        button: kludgine::app::winit::event::MouseButton,
        _context: &mut EventContext<'_>,
    ) {
        if let Some(tick) = &self.tick {
            tick.mouse_button(button, ElementState::Released);
        }
    }
}