menus now support inter-dependancies + added alignedRight to integer drawing
This commit is contained in:
parent
262b48f93f
commit
68fca51497
4
csts.ml
4
csts.ml
|
@ -28,6 +28,10 @@ let sleep_d = 0.01 ;;
|
|||
let difficulty = ref 1 ;;
|
||||
let calc_length = ref 3 ;;
|
||||
let time_to_ans = ref 10 ;;
|
||||
let n_inf = ref 1 ;;
|
||||
let n_sup = ref 10 ;;
|
||||
|
||||
let time_to_ans_f = ref 10.0 ;;
|
||||
|
||||
let score = ref 0
|
||||
and combo = ref 0
|
||||
|
|
BIN
drawing.cmi
BIN
drawing.cmi
Binary file not shown.
BIN
drawing.cmx
BIN
drawing.cmx
Binary file not shown.
42
drawing.ml
42
drawing.ml
|
@ -76,6 +76,48 @@ let draw_integer_alignedleft ?retval:(rv=fodder) x0 y n0 len =
|
|||
done ;
|
||||
rv := (x0 + (size+1)*(len*11/7)) ;;
|
||||
|
||||
let draw_integer_alignedright ?retval:(rv=fodder) x0 y n0 len =
|
||||
(* 7-seg display 2 *)
|
||||
set_line_width (max 1 (len/4));
|
||||
let n = ref n0 in
|
||||
let size = Math.ln_b 10 n0 in
|
||||
|
||||
let cur_x = ref (x0 - len*11/7) in
|
||||
|
||||
if !n < 0 then begin
|
||||
n := !n * (-1);
|
||||
draw_poly_line [|(x0, y); (x0-(size+1)*len, y)|];
|
||||
cur_x := !cur_x + (len*11/7)
|
||||
end;
|
||||
|
||||
for i = 0 to size do
|
||||
let x = !cur_x in
|
||||
if Array.mem (!n mod 10) [|0; 4; 5; 6; 7; 8; 9|] then
|
||||
draw_poly_line [|(x + !(Csts.camx), y+len + !(Csts.camy)); (x + !(Csts.camx), y + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 7; 8; 9|] then
|
||||
draw_poly_line [|(x + !(Csts.camx), y+len + !(Csts.camy)); (x+len + !(Csts.camx), y+len + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|0; 1; 2; 3; 4; 7; 8; 9|] then
|
||||
draw_poly_line [|(x+len + !(Csts.camx), y+len + !(Csts.camy)); (x+len + !(Csts.camx), y + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|2; 3; 4; 5; 6; 8; 9|] then
|
||||
draw_poly_line [|(x + !(Csts.camx), y + !(Csts.camy)); (x+len + !(Csts.camx), y + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|0; 1; 3; 4; 5; 6; 7; 8; 9|] then
|
||||
draw_poly_line [|(x+len + !(Csts.camx), y-len + !(Csts.camy)); (x+len + !(Csts.camx), y + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|0; 2; 3; 5; 6; 8; 9|] then
|
||||
draw_poly_line [|(x + !(Csts.camx), y-len + !(Csts.camy)); (x+len + !(Csts.camx), y-len + !(Csts.camy))|];
|
||||
|
||||
if Array.mem (!n mod 10) [|0; 2; 6; 8|] then
|
||||
draw_poly_line [|(x + !(Csts.camx), y-len + !(Csts.camy)); (x + !(Csts.camx), y + !(Csts.camy))|];
|
||||
|
||||
n := !n/10;
|
||||
cur_x := !cur_x - (len*11/7);
|
||||
done ;
|
||||
rv := (x0 + (len*11/7)) ;;
|
||||
|
||||
let decode_letter x y len arr =
|
||||
set_line_width 3;
|
||||
if arr.(0) = 1 then
|
||||
|
|
BIN
dynamic.cmx
BIN
dynamic.cmx
Binary file not shown.
15
maeth.ml
15
maeth.ml
|
@ -118,3 +118,18 @@ let print_calc (f : operation) (x0 : int) (y0 : int) (size : int) =
|
|||
if require_bracket g then draw_bracket_close !x y0 size ~retval:x ;
|
||||
in
|
||||
aux f ;;
|
||||
|
||||
let maeth_main () =
|
||||
let start = Unix.gettimeofday () in
|
||||
let found = ref false in
|
||||
while not !found && (Unix.gettimeofday() -. start <= !Csts.time_to_ans_f) do
|
||||
()
|
||||
done ;
|
||||
if !found then begin
|
||||
incr Csts.combo ;
|
||||
true
|
||||
end
|
||||
else begin
|
||||
Csts.combo := 0 ;
|
||||
false
|
||||
end ;;
|
1
main.ml
1
main.ml
|
@ -16,6 +16,7 @@ let mainloop () =
|
|||
()
|
||||
with
|
||||
| Menus.MenuStart ->
|
||||
Csts.time_to_ans_f := float_of_int (!Csts.time_to_ans) ;
|
||||
clear_graph () ;
|
||||
Maeth.print_calc (Maeth.generate_random_calc 8 1 20) (10) (Csts.__height__/2) 20 ;
|
||||
while true do
|
||||
|
|
134
menus.ml
134
menus.ml
|
@ -10,6 +10,8 @@ type 'a value_changer = {
|
|||
min : 'a ;
|
||||
max : 'a ;
|
||||
pad : 'a ;
|
||||
min_update : ('a ref) list ; (* after this value changes, these values will be set to min(their_value, this_value-1) *)
|
||||
max_update : ('a ref) list ; (* after this value changes, these values will be set to max(their_value, this_value+1) *)
|
||||
} ;;
|
||||
|
||||
type action = Nothing | Tweak of int value_changer | Warp of int
|
||||
|
@ -120,11 +122,13 @@ let build_interface (title : string) (red : int) (green : int) (blue : int) (but
|
|||
Dynamic.dyn_add arr_interfaces res ;
|
||||
incr current_interface_index ;;
|
||||
|
||||
let build_tweak (ptr : int ref) (m : int) (mx : int) (st : int) = {
|
||||
let build_tweak (ptr : int ref) (m : int) (mx : int) (st : int) (mi : (int ref) list) (sp : (int ref) list) = {
|
||||
ptr = ptr ;
|
||||
min = m;
|
||||
max = mx ;
|
||||
pad = st ;
|
||||
min_update = mi ;
|
||||
max_update = sp ;
|
||||
} ;;
|
||||
|
||||
(* --------------------------------------------------------------------------------------------------------------------------------------------- *)
|
||||
|
@ -162,7 +166,11 @@ let print_button (b : button) =
|
|||
fill_rect b.pos.x b.pos.y b.pos.w b.pos.h ;
|
||||
draw_closest b.red b.green b.blue ;
|
||||
set_line_width 2 ;
|
||||
Drawing.draw_string (b.pos.x+3) (b.pos.y + b.pos.h/2) b.text (min (3*b.pos.h/4-2) ((7*(b.pos.w*3/4-4)/(max 1 (String.length b.text)))/10)) ;;
|
||||
Drawing.draw_string (b.pos.x+3) (b.pos.y + b.pos.h/2) b.text (min (3*b.pos.h/4-2) ((7*((b.pos.w*3)/4-50)/(max 1 (String.length b.text)))/10)) ;
|
||||
match b.actn with
|
||||
| Tweak v ->
|
||||
Drawing.draw_integer_alignedright (b.pos.x + b.pos.w) (b.pos.y + b.pos.h/2) !(v.ptr) 15
|
||||
| _ -> () ;;
|
||||
|
||||
let print_interface (it : interface) =
|
||||
List.iter print_button it.bts ;
|
||||
|
@ -176,7 +184,7 @@ let print_current_interface () =
|
|||
Drawing.draw_string 20 (Csts.__height__ - 40) interf.title 30 ;;
|
||||
|
||||
(* --------------------------------------------------------------------------------------------------------------------------------------------- *)
|
||||
(* actions *)
|
||||
(* actions 1/2 *)
|
||||
|
||||
let get1char () =
|
||||
if key_pressed () then
|
||||
|
@ -184,13 +192,116 @@ let get1char () =
|
|||
else
|
||||
'@' ;;
|
||||
|
||||
let change_other (reference : int) (mins : (int ref) list) (maxs : (int ref) list) =
|
||||
let rec aux = function
|
||||
| ([], []) ->
|
||||
()
|
||||
| (hmin::tmin, []) ->
|
||||
hmin := min (!hmin) (reference -1) ;
|
||||
aux (tmin, [])
|
||||
| ([], hmax::tmax) ->
|
||||
hmax := max (!hmax) (reference +1) ;
|
||||
aux ([], tmax)
|
||||
| (hmin::tmin, hmax::tmax) ->
|
||||
hmin := min (!hmin) (reference -1) ;
|
||||
hmax := max (!hmax) (reference +1) ;
|
||||
aux (tmin, tmax)
|
||||
in aux (mins, maxs) ;;
|
||||
|
||||
let offset_pm = 40 ;;
|
||||
let size_pm = (Csts.__width__ - 2*offset_pm) /11 ;;
|
||||
|
||||
let draw_rect_pm (x : int) (y : int) (w: int) (h : int) (red : int) (green : int) (blue : int) =
|
||||
let (mx, my) = mouse_pos () in
|
||||
if is_within {x = x; y = y; w = w; h = h} mx my then begin
|
||||
set_color (rgb 0 255 0) ;
|
||||
fill_rect x y w h ;
|
||||
set_color (rgb red green blue) ;
|
||||
fill_rect (x+6) (y+6) (w-12) (h-12)
|
||||
end
|
||||
else begin
|
||||
set_color (rgb red green blue) ;
|
||||
fill_rect x y w h
|
||||
end ;;
|
||||
|
||||
let draw_incrdecr () =
|
||||
draw_rect_pm (offset_pm) (offset_pm) size_pm size_pm 255 0 0;
|
||||
draw_rect_pm (Csts.__width__/11 + offset_pm) (offset_pm) size_pm size_pm 255 0 0;
|
||||
draw_rect_pm (2*Csts.__width__/11 + offset_pm) (offset_pm) size_pm size_pm 255 0 0;
|
||||
draw_rect_pm (3*Csts.__width__/11 + offset_pm) (offset_pm) size_pm size_pm 255 0 0;
|
||||
draw_rect_pm (4*Csts.__width__/11 + offset_pm) (offset_pm) size_pm size_pm 255 0 0;
|
||||
draw_rect_pm (10*Csts.__width__/11 - offset_pm) (offset_pm) size_pm size_pm 0 0 255;
|
||||
draw_rect_pm (9*Csts.__width__/11 - offset_pm) (offset_pm) size_pm size_pm 0 0 255;
|
||||
draw_rect_pm (8*Csts.__width__/11 - offset_pm) (offset_pm) size_pm size_pm 0 0 255;
|
||||
draw_rect_pm (7*Csts.__width__/11 - offset_pm) (offset_pm) size_pm size_pm 0 0 255;
|
||||
draw_rect_pm (6*Csts.__width__/11 - offset_pm) (offset_pm) size_pm size_pm 0 0 255;;
|
||||
|
||||
let draw_digits_incrdecr () =
|
||||
set_color white ;
|
||||
Drawing.draw_string_centered (offset_pm + size_pm/2) (offset_pm + size_pm/2) "min" (size_pm/6) ;
|
||||
Drawing.draw_integer (offset_pm + Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 1 (size_pm/3) ;
|
||||
Drawing.draw_integer (offset_pm + 2*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 10 (size_pm/3) ;
|
||||
Drawing.draw_integer (offset_pm + 3*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 100 (size_pm/3) ;
|
||||
Drawing.draw_integer (offset_pm + 4*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 500 (size_pm/3) ;
|
||||
|
||||
Drawing.draw_string_centered (-offset_pm + 10*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) "max" (size_pm/6) ;
|
||||
Drawing.draw_integer (-offset_pm + 9*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 1 (size_pm/3) ;
|
||||
Drawing.draw_integer (-offset_pm + 8*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 10 (size_pm/3) ;
|
||||
Drawing.draw_integer (-offset_pm + 7*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 100 (size_pm/3) ;
|
||||
Drawing.draw_integer (-offset_pm + 6*Csts.__width__/11 + size_pm/2) (offset_pm + size_pm/2) 500 (size_pm/3) ;;
|
||||
|
||||
(* --------------------------------------------------------------------------------------------------------------------------------------------- *)
|
||||
(* actions 2/2 *)
|
||||
|
||||
let stall_pm = ref false ;;
|
||||
let use_incrdecr (val_ptr : int value_changer) =
|
||||
if button_down () then begin
|
||||
if !stall_pm then
|
||||
()
|
||||
else begin
|
||||
stall_pm := true ;
|
||||
let (mox, moy) = mouse_pos () in
|
||||
if moy >= offset_pm && moy <= offset_pm + size_pm then begin
|
||||
match mox with
|
||||
| x when x >= offset_pm && x <= offset_pm + size_pm ->
|
||||
val_ptr.ptr := val_ptr.min
|
||||
| x when x >= offset_pm + Csts.__width__/11 && x <= offset_pm + Csts.__width__/11 + size_pm ->
|
||||
val_ptr.ptr := max val_ptr.min (!(val_ptr.ptr) - 1)
|
||||
| x when x >= offset_pm + 2*Csts.__width__/11 && x <= offset_pm + 2*Csts.__width__/11 + size_pm ->
|
||||
val_ptr.ptr := max val_ptr.min (!(val_ptr.ptr) - 10)
|
||||
| x when x >= offset_pm + 3*Csts.__width__/11 && x <= offset_pm + 3*Csts.__width__/11 + size_pm ->
|
||||
val_ptr.ptr := max val_ptr.min (!(val_ptr.ptr) - 100)
|
||||
| x when x >= offset_pm + 4*Csts.__width__/11 && x <= offset_pm + 4*Csts.__width__/11 + size_pm ->
|
||||
val_ptr.ptr := max val_ptr.min (!(val_ptr.ptr) - 500)
|
||||
|
||||
| x when x >= 6*Csts.__width__/11 - offset_pm && x <= 6*Csts.__width__/11 - offset_pm + size_pm ->
|
||||
val_ptr.ptr := min val_ptr.max (!(val_ptr.ptr) + 500)
|
||||
| x when x >= 7*Csts.__width__/11 - offset_pm && x <= 7*Csts.__width__/11 - offset_pm + size_pm ->
|
||||
val_ptr.ptr := min val_ptr.max (!(val_ptr.ptr) + 100)
|
||||
| x when x >= 8*Csts.__width__/11 - offset_pm && x <= 8*Csts.__width__/11 - offset_pm + size_pm ->
|
||||
val_ptr.ptr := min val_ptr.max (!(val_ptr.ptr) + 10)
|
||||
| x when x >= 9*Csts.__width__/11 - offset_pm && x <= 9*Csts.__width__/11 - offset_pm + size_pm ->
|
||||
val_ptr.ptr := min val_ptr.max (!(val_ptr.ptr) + 1)
|
||||
| x when x >= 10*Csts.__width__/11 - offset_pm && x <= 10*Csts.__width__/11 - offset_pm + size_pm ->
|
||||
val_ptr.ptr := val_ptr.max
|
||||
| _ -> ()
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
stall_pm := false ;;
|
||||
|
||||
let move_interface (b : button) = match b.actn with
|
||||
| Nothing -> ()
|
||||
| Tweak val_ptr -> begin
|
||||
let halted = ref false in
|
||||
set_color black ;
|
||||
Drawing.draw_string_centered (Csts.__width__/2) (Csts.__height__ - 50) "press space to confirm" 15 ;
|
||||
set_line_width 3 ;
|
||||
while not !halted do
|
||||
auto_synchronize false ;
|
||||
draw_incrdecr () ;
|
||||
draw_digits_incrdecr () ;
|
||||
set_color black ;
|
||||
fill_rect (3*Csts.__width__/10) (3*Csts.__height__/10) (2*Csts.__width__/5) (2*Csts.__height__/5) ;
|
||||
set_color white ;
|
||||
|
@ -201,13 +312,15 @@ let move_interface (b : button) = match b.actn with
|
|||
Drawing.draw_string (7*Csts.__width__/10-5-3*20*10/7) (11*Csts.__height__/20) "Max" 20 ;
|
||||
Drawing.draw_integer_alignedleft (7*Csts.__width__/10-5-3*20*10/7) (Csts.__height__/2) val_ptr.max 18 ;
|
||||
auto_synchronize true ;
|
||||
use_incrdecr val_ptr ;
|
||||
Unix.sleepf Csts.sleep_d ;
|
||||
match get1char () with
|
||||
| '+' -> val_ptr.ptr := min val_ptr.max (!(val_ptr.ptr) + val_ptr.pad)
|
||||
| '-' -> val_ptr.ptr := max val_ptr.min (!(val_ptr.ptr) - val_ptr.pad)
|
||||
| '\n' | ' ' -> halted := true
|
||||
| _ -> ()
|
||||
done
|
||||
done ;
|
||||
change_other !(val_ptr.ptr) val_ptr.min_update val_ptr.max_update
|
||||
end
|
||||
| Warp ne ->
|
||||
if ne = -1 then
|
||||
|
@ -255,10 +368,13 @@ build_empty_interface "Options " 32 32 32 ;;
|
|||
build_button "Start" {x = Csts.__width__/2 - 150; y = 270; w = 300; h = 100} 32 255 32 (Warp (-2)) ;;
|
||||
build_button "Options" {x = Csts.__width__/2 - 150; y = 160; w = 300; h = 100} 255 32 255 (Warp 1) ;;
|
||||
build_button "Exit" {x = Csts.__width__/2 - 150; y = 50; w = 300; h = 100} 1 1 1 (Warp (-1)) ;;
|
||||
build_button "Length" {x = Csts.__width__/2 - 150; y = 380; w = 300; h = 100} 255 32 32 (Tweak (build_tweak Csts.calc_length 2 20 1)) ;;
|
||||
build_button "Time" {x = Csts.__width__/2 - 150; y = 270; w = 300; h = 100} 32 255 32 (Tweak (build_tweak Csts.time_to_ans 3 30 1)) ;;
|
||||
build_button "Difficulty" {x = Csts.__width__/2 - 150; y = 160; w = 300; h = 100} 32 32 255 (Tweak (build_tweak Csts.difficulty 1 10 1)) ;;
|
||||
build_button "Back" {x = Csts.__width__/2 - 150; y = 50; w = 300; h = 100} 32 32 32 (Warp 0) ;;
|
||||
|
||||
build_button "Minimum" {x = Csts.__width__/2 - 200; y = 600; w = 400; h = 100} 255 128 32 (Tweak (build_tweak Csts.n_inf 1 1000 1 [] [Csts.n_sup])) ;;
|
||||
build_button "Maximum" {x = Csts.__width__/2 - 200; y = 490; w = 400; h = 100} 128 255 32 (Tweak (build_tweak Csts.n_sup 1 1000 1 [Csts.n_inf] [])) ;;
|
||||
build_button "Length" {x = Csts.__width__/2 - 200; y = 380; w = 400; h = 100} 255 32 32 (Tweak (build_tweak Csts.calc_length 2 20 1 [] [])) ;;
|
||||
build_button "Time" {x = Csts.__width__/2 - 200; y = 270; w = 400; h = 100} 32 255 32 (Tweak (build_tweak Csts.time_to_ans 3 30 1 [] [])) ;;
|
||||
build_button "Difficulty" {x = Csts.__width__/2 - 200; y = 160; w = 400; h = 100} 32 32 255 (Tweak (build_tweak Csts.difficulty 1 10 1 [] [])) ;;
|
||||
build_button "Back" {x = Csts.__width__/2 - 200; y = 50; w = 400; h = 100} 32 32 32 (Warp 0) ;;
|
||||
|
||||
(*Printf.printf "(B : %d ; I : %d)\n" arr_buttons.len arr_interfaces.len ;;*)
|
||||
add_button_to_interface 0 0 ;
|
||||
|
@ -268,3 +384,5 @@ add_button_to_interface 1 3 ;
|
|||
add_button_to_interface 1 4 ;
|
||||
add_button_to_interface 1 5 ;
|
||||
add_button_to_interface 1 6 ;
|
||||
add_button_to_interface 1 7 ;
|
||||
add_button_to_interface 1 8 ;
|
Loading…
Reference in New Issue