From 429073a29c84062a2680f2f3faa0caf19478e465 Mon Sep 17 00:00:00 2001 From: Malik Olivier Boussejra Date: Wed, 16 May 2018 23:26:38 +0900 Subject: [PATCH 1/3] window_draw_list: Add support for drawing text --- src/window_draw_list.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/window_draw_list.rs b/src/window_draw_list.rs index abc6a53..3a34f80 100644 --- a/src/window_draw_list.rs +++ b/src/window_draw_list.rs @@ -209,6 +209,19 @@ impl<'ui> WindowDrawList<'ui> { Circle::new(self, center, radius, color) } + /// Draw a text whose upper-left corner is at point `pos`. + pub fn add_text(&self, pos: P, col: C, text: &str) + where + P: Into, + C: Into, + { + unsafe { + let start = text.as_ptr() as *const i8; + let end = (start as usize + text.len()) as *const i8; + sys::ImDrawList_AddText(self.draw_list, pos.into(), col.into().into(), start, end) + } + } + /// Returns a Bezier curve stretching from `pos0` to `pos1`, whose /// curvature is defined by `cp0` and `cp1`. pub fn add_bezier_curve( From 485c8a230e3b11ed3109ca49ca2db7f2bbb6800b Mon Sep 17 00:00:00 2001 From: Malik Olivier Boussejra Date: Wed, 30 May 2018 14:20:28 +0900 Subject: [PATCH 2/3] window_draw_list: Use generic to add_text This change allows to use `add_text` with any type that implements `AsRef`. This includes `String`, `&str`, `ImString` and `&ImStr`. --- src/window_draw_list.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/window_draw_list.rs b/src/window_draw_list.rs index 3a34f80..4790b5f 100644 --- a/src/window_draw_list.rs +++ b/src/window_draw_list.rs @@ -210,11 +210,13 @@ impl<'ui> WindowDrawList<'ui> { } /// Draw a text whose upper-left corner is at point `pos`. - pub fn add_text(&self, pos: P, col: C, text: &str) + pub fn add_text(&self, pos: P, col: C, text: T) where P: Into, C: Into, + T: AsRef, { + let text = text.as_ref(); unsafe { let start = text.as_ptr() as *const i8; let end = (start as usize + text.len()) as *const i8; From 9b0804f6be58e02df2f9a1c0617a79dbf3d0c9ac Mon Sep 17 00:00:00 2001 From: Malik Olivier Boussejra Date: Wed, 30 May 2018 14:25:42 +0900 Subject: [PATCH 3/3] window_draw_list: Use c_char instead of i8 Target expects a c_char, which happens to be an i8 on x86, but it may be something else on other platforms (e.g. u8 on ARM). --- src/window_draw_list.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/window_draw_list.rs b/src/window_draw_list.rs index 4790b5f..b9f6f18 100644 --- a/src/window_draw_list.rs +++ b/src/window_draw_list.rs @@ -216,10 +216,12 @@ impl<'ui> WindowDrawList<'ui> { C: Into, T: AsRef, { + use std::os::raw::c_char; + let text = text.as_ref(); unsafe { - let start = text.as_ptr() as *const i8; - let end = (start as usize + text.len()) as *const i8; + let start = text.as_ptr() as *const c_char; + let end = (start as usize + text.len()) as *const c_char; sys::ImDrawList_AddText(self.draw_list, pos.into(), col.into().into(), start, end) } }