make Grid.draw more flexible

- printer callback includes Vec2.t in the arguments now
- added optional rev_x and rev_y params
This commit is contained in:
ryan manseau 2024-01-03 09:18:57 -08:00
parent a7c039197b
commit 490696c63d
3 changed files with 12 additions and 8 deletions

View File

@ -9,7 +9,7 @@ type tile =
| Path_L
| Path_R
let char_of_tile = function
let char_of_tile _ = function
| Empty -> '-'
| Rock -> '#'
| Sand -> 'o'

View File

@ -82,12 +82,16 @@ let iter grid callback =
iter_region grid a b callback
;;
let draw grid printer =
let str = ref [] in
let draw grid ?(rev_y = false) ?(rev_x = false) printer =
let line = ref [] in
let lines = ref [] in
iter grid (fun point ->
let item = grid.%(point) in
let char = printer @@ Option.get_exn_or "iter is broken" item in
str := !str @ [ char ];
if point.x = grid.width - 1 then str := !str @ [ '\n' ]);
String.of_list !str
let char = printer point (Option.get_exn_or "iter is broken" item) in
line := if rev_x then char :: !line else !line @ [ char ];
if List.length !line = grid.width
then (
lines := !lines @ [ String.of_list !line ];
line := []));
String.concat "\n" (if rev_y then List.rev !lines else !lines)
;;

View File

@ -11,4 +11,4 @@ val find : 'a t -> ('a -> bool) -> (Vec2.t * 'a) option
val count : 'a t -> ('a -> bool) -> int
val iter_region : 'a t -> Vec2.t -> Vec2.t -> (Vec2.t -> unit) -> unit
val iter : 'a t -> (Vec2.t -> unit) -> unit
val draw : 'a t -> ('a -> char) -> string
val draw : 'a t -> ?rev_y:bool -> ?rev_x:bool -> (Vec2.t -> 'a -> char) -> string