1- use crate :: window:: Window ;
1+ use crate :: window:: { TitlePosition , Window } ;
22use anyhow:: anyhow;
33use caw_window_utils:: persistent:: PersistentData ;
44use midly:: num:: u7;
5- use sdl2:: { mouse:: MouseButton , pixels:: Color , rect:: Rect } ;
5+ use sdl2:: {
6+ gfx:: rotozoom:: RotozoomSurface , mouse:: MouseButton , pixels:: Color ,
7+ rect:: Rect ,
8+ } ;
69use serde:: { Deserialize , Serialize } ;
710use std:: time:: Instant ;
811
@@ -19,13 +22,23 @@ impl PersistentData for State {
1922 const NAME : & ' static str = "xy_state" ;
2023}
2124
25+ #[ derive( Debug ) ]
26+ pub struct AxisLabels {
27+ pub x : Option < String > ,
28+ pub y : Option < String > ,
29+ }
30+
2231pub struct Xy {
2332 window : Window ,
2433 state : State ,
34+ axis_labels : AxisLabels ,
2535}
2636
2737impl Xy {
28- pub fn new ( title : Option < & str > ) -> anyhow:: Result < Self > {
38+ pub fn new (
39+ title : Option < & str > ,
40+ axis_labels : AxisLabels ,
41+ ) -> anyhow:: Result < Self > {
2942 let window = Window :: new ( title, WIDTH_PX , HEIGHT_PX ) ?;
3043 let state = if let Some ( state) = title. and_then ( |t| State :: load_ ( t) ) {
3144 state
@@ -35,7 +48,12 @@ impl Xy {
3548 y_px : HEIGHT_PX / 2 ,
3649 }
3750 } ;
38- Ok ( Self { window, state } )
51+ println ! ( "{:?}" , axis_labels) ;
52+ Ok ( Self {
53+ window,
54+ state,
55+ axis_labels,
56+ } )
3957 }
4058
4159 fn handle_events ( & mut self ) {
@@ -92,11 +110,94 @@ impl Xy {
92110 Ok ( ( ) )
93111 }
94112
113+ fn render_axes (
114+ & mut self ,
115+ x : Option < & str > ,
116+ y : Option < & str > ,
117+ ortho_padding_px : i32 ,
118+ ) -> anyhow:: Result < ( ) > {
119+ let padding_px = 10 ;
120+ if let Some ( x) = x {
121+ let text_surface = self
122+ . window
123+ . font
124+ . render ( x)
125+ . blended ( Color :: WHITE )
126+ . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
127+ let text_texture =
128+ text_surface. as_texture ( & self . window . texture_creator ) ?;
129+ let ( canvas_width, canvas_height) = self
130+ . window
131+ . canvas
132+ . output_size ( )
133+ . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
134+ let text_texture_query = text_texture. query ( ) ;
135+ let x_position = canvas_width as i32
136+ - text_texture_query. width as i32
137+ - padding_px;
138+ let y_position = canvas_height as i32
139+ - text_texture_query. height as i32
140+ - ortho_padding_px;
141+ let text_rect = Rect :: new (
142+ x_position,
143+ y_position,
144+ text_texture_query. width ,
145+ text_texture_query. height ,
146+ ) ;
147+ self . window
148+ . canvas
149+ . copy ( & text_texture, None , Some ( text_rect) )
150+ . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
151+ }
152+ if let Some ( y) = y {
153+ let text_surface = self
154+ . window
155+ . font
156+ . render ( y)
157+ . blended ( Color :: WHITE )
158+ . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
159+ let text_surface =
160+ text_surface. rotate_90deg ( 1 ) . map_err ( |s| anyhow ! ( "{}" , s) ) ?;
161+ let text_texture =
162+ text_surface. as_texture ( & self . window . texture_creator ) ?;
163+ let text_texture_query = text_texture. query ( ) ;
164+ let x_position = ortho_padding_px;
165+ let y_position = padding_px;
166+ let text_rect = Rect :: new (
167+ x_position,
168+ y_position,
169+ text_texture_query. width ,
170+ text_texture_query. height ,
171+ ) ;
172+ self . window
173+ . canvas
174+ . copy ( & text_texture, None , Some ( text_rect) )
175+ . map_err ( |e| anyhow ! ( "{e}" ) ) ?;
176+ }
177+
178+ Ok ( ( ) )
179+ }
180+
181+ fn render_axes_labels ( & mut self ) -> anyhow:: Result < ( ) > {
182+ let x = self . axis_labels . x . as_ref ( ) . map ( |x| format ! ( "{}" , x) ) ;
183+ let y = self . axis_labels . y . as_ref ( ) . map ( |y| format ! ( "{}" , y) ) ;
184+ self . render_axes ( x. as_deref ( ) , y. as_deref ( ) , 20 )
185+ }
186+
187+ fn render_axes_values ( & mut self ) -> anyhow:: Result < ( ) > {
188+ let ( x, y) = self . value_01 ( ) ;
189+ let x = format ! ( "{}" , x) ;
190+ let y = format ! ( "{}" , y) ;
191+ self . render_axes ( Some ( x. as_str ( ) ) , Some ( y. as_str ( ) ) , 0 )
192+ }
193+
95194 fn render ( & mut self ) -> anyhow:: Result < ( ) > {
96195 self . window . canvas . set_draw_color ( Color :: BLACK ) ;
97196 self . window . canvas . clear ( ) ;
98197 self . render_xy ( ) ?;
99- self . window . render_title ( ) ?;
198+ self . window . render_title ( TitlePosition :: Center ) ?;
199+ self . render_axes_labels ( ) ?;
200+ self . render_axes_values ( ) ?;
100201 self . window . canvas . present ( ) ;
101202 Ok ( ( ) )
102203 }
0 commit comments