Skip to content

Commit

Permalink
Fix #46
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Oct 18, 2024
1 parent 33bfeb9 commit 43e4416
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
32 changes: 30 additions & 2 deletions src/raygui/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ let value_box rct label vl ~min ~max edit =
let rt = _value_box (to_struct rct) label vl_ptr min max edit in
(Ctypes.(!@vl_ptr), rt)

let carray_of_string str =
(* Two null bytes to have room for a new character *)
let len = String.length str in
let open Ctypes in
let arr = CArray.make char (len + 2) in
String.iteri (CArray.set arr) str;
CArray.set arr len '\x00';
CArray.set arr (len + 1) '\x00';
arr

let text_box rct txt state =
let open Ctypes in
let str_arr = CArray.of_string txt in
Expand All @@ -49,7 +59,16 @@ let text_box rct txt state =
(CArray.length str_arr + 1)
state
in
(String.init (CArray.length str_arr) (CArray.unsafe_get str_arr), rt)
let str =
Ctypes.string_from_ptr (CArray.start str_arr)
~length:(CArray.length str_arr)
in
let str =
match String.index_opt str '\x00' with
| Some last -> String.sub str 0 last
| None -> str
in
(str, rt)

let text_box_multi rct txt state =
let open Ctypes in
Expand All @@ -59,7 +78,16 @@ let text_box_multi rct txt state =
(CArray.length str_arr + 1)
state
in
(String.init (CArray.length str_arr) (CArray.unsafe_get str_arr), rt)
let str =
Ctypes.string_from_ptr (CArray.start str_arr)
~length:(CArray.length str_arr)
in
let str =
match String.index_opt str '\x00' with
| Some last -> String.sub str 0 last
| None -> str
in
(str, rt)

let slider rct label txt value ~min ~max =
_slider (to_struct rct) label txt value min max
Expand Down
4 changes: 2 additions & 2 deletions src/raygui/raygui.mli
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ val value_box :
(** [value_box bounds text value ~min ~max edit_mode] Value Box control, updates input text with numbers, returns value, edit_mode *)

val text_box : Raylib.Rectangle.t -> string -> bool -> string * bool
(** [text_box bounds text text_size edit_mode] Text Box control, returns text, edit_mode *)
(** [text_box bounds text edit_mode] Text Box control, returns text, edit_mode *)

val text_box_multi : Raylib.Rectangle.t -> string -> bool -> string * bool
(** [text_box_multi bounds text text_size edit_mode] Text Box control with multiple lines,returns text, edit_mode *)
(** [text_box_multi bounds text edit_mode] Text Box control with multiple lines,returns text, edit_mode *)

val slider :
Raylib.Rectangle.t ->
Expand Down

0 comments on commit 43e4416

Please sign in to comment.