From f8f84002c601b3e2a093b3a08ca0c4e80a9eaa02 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Mon, 13 Apr 2020 01:49:23 +0200 Subject: [PATCH] Positioning related methods --- lvgl/src/objx.rs | 171 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 155 insertions(+), 16 deletions(-) diff --git a/lvgl/src/objx.rs b/lvgl/src/objx.rs index 6736506..6f836ee 100644 --- a/lvgl/src/objx.rs +++ b/lvgl/src/objx.rs @@ -14,6 +14,79 @@ impl Object { pub(crate) fn new(raw: ptr::NonNull) -> Self { Self { raw } } + + pub fn set_pos(&mut self, x: i16, y: i16) { + unsafe { + lvgl_sys::lv_obj_set_pos( + self.raw().as_mut(), + x as lvgl_sys::lv_coord_t, + y as lvgl_sys::lv_coord_t, + ); + } + } + + pub fn set_size(&mut self, w: i16, h: i16) { + unsafe { + lvgl_sys::lv_obj_set_size( + self.raw().as_mut(), + w as lvgl_sys::lv_coord_t, + h as lvgl_sys::lv_coord_t, + ); + } + } + + pub fn set_width(&mut self, w: u32) { + unsafe { + lvgl_sys::lv_obj_set_width( + self.raw().as_mut(), + w as lvgl_sys::lv_coord_t, + ); + } + } + + pub fn set_height(&mut self, h: u32) { + unsafe { + lvgl_sys::lv_obj_set_height( + self.raw().as_mut(), + h as lvgl_sys::lv_coord_t, + ); + } + } + + pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) { + let align= match align { + ObjectAlign::Center => lvgl_sys::LV_ALIGN_CENTER, + ObjectAlign::InTopLeft => lvgl_sys::LV_ALIGN_IN_TOP_LEFT, + ObjectAlign::InTopMid => lvgl_sys::LV_ALIGN_IN_TOP_MID, + ObjectAlign::InTopRight => lvgl_sys::LV_ALIGN_IN_TOP_RIGHT, + ObjectAlign::InBottomLeft => lvgl_sys::LV_ALIGN_IN_BOTTOM_LEFT, + ObjectAlign::InBottomMid => lvgl_sys::LV_ALIGN_IN_BOTTOM_MID, + ObjectAlign::InBottomRight => lvgl_sys::LV_ALIGN_IN_BOTTOM_RIGHT, + ObjectAlign::InLeftMid => lvgl_sys::LV_ALIGN_IN_LEFT_MID, + ObjectAlign::InRightMid => lvgl_sys::LV_ALIGN_IN_RIGHT_MID, + ObjectAlign::OutTopLeft => lvgl_sys::LV_ALIGN_OUT_TOP_LEFT, + ObjectAlign::OutTopMid => lvgl_sys::LV_ALIGN_OUT_TOP_MID, + ObjectAlign::OutTopRight => lvgl_sys::LV_ALIGN_OUT_TOP_RIGHT, + ObjectAlign::OutBottomLeft => lvgl_sys::LV_ALIGN_OUT_BOTTOM_LEFT, + ObjectAlign::OutBottomMid => lvgl_sys::LV_ALIGN_OUT_BOTTOM_MID, + ObjectAlign::OutBottomRight => lvgl_sys::LV_ALIGN_OUT_BOTTOM_RIGHT, + ObjectAlign::OutLeftTop => lvgl_sys::LV_ALIGN_OUT_LEFT_TOP, + ObjectAlign::OutLeftMid => lvgl_sys::LV_ALIGN_OUT_LEFT_MID, + ObjectAlign::OutLeftBottom => lvgl_sys::LV_ALIGN_OUT_LEFT_BOTTOM, + ObjectAlign::OutRightTop => lvgl_sys::LV_ALIGN_OUT_RIGHT_TOP, + ObjectAlign::OutRightMid => lvgl_sys::LV_ALIGN_OUT_RIGHT_MID, + ObjectAlign::OutRightBottom => lvgl_sys::LV_ALIGN_OUT_RIGHT_BOTTOM, + } as lvgl_sys::lv_align_t; + unsafe { + lvgl_sys::lv_obj_align( + self.raw().as_mut(), + base.raw().as_mut(), + align, + x_mod as lvgl_sys::lv_coord_t, + y_mod as lvgl_sys::lv_coord_t + ); + } + } } impl Container for Object { @@ -22,6 +95,30 @@ impl Container for Object { } } +pub enum ObjectAlign { + Center, + InTopLeft, + InTopMid, + InTopRight, + InBottomLeft, + InBottomMid, + InBottomRight, + InLeftMid, + InRightMid, + OutTopLeft, + OutTopMid, + OutTopRight, + OutBottomLeft, + OutBottomMid, + OutBottomRight, + OutLeftTop, + OutLeftMid, + OutLeftBottom, + OutRightTop, + OutRightMid, + OutRightBottom, +} + pub struct Button { core: Object, } @@ -36,24 +133,24 @@ impl Button { Self { core } } - pub fn set_pos(&mut self, x: u16, y: u16) { - unsafe { - lvgl_sys::lv_obj_set_pos( - self.raw().as_mut(), - x as lvgl_sys::lv_coord_t, - y as lvgl_sys::lv_coord_t, - ); - } + pub fn set_pos(&mut self, x: i16, y: i16) { + self.core.set_pos(x, y) } - pub fn set_size(&mut self, w: u16, h: u16) { - unsafe { - lvgl_sys::lv_obj_set_size( - self.raw().as_mut(), - w as lvgl_sys::lv_coord_t, - h as lvgl_sys::lv_coord_t, - ); - } + pub fn set_size(&mut self, w: i16, h: i16) { + self.core.set_size(w, h) + } + + pub fn set_width(&mut self, w: u32) { + self.core.set_width(w) + } + + pub fn set_height(&mut self, h: u32) { + self.core.set_height(h) + } + + pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) { + self.core.set_object_align(base, align, x_mod, y_mod); } } @@ -63,6 +160,13 @@ impl Container for Button { } } +pub enum LabelAlign { + Left, + Center, + Right, + Auto, +} + pub struct Label { core: Object, } @@ -85,6 +189,41 @@ impl Label { ); } } + + pub fn set_align(&mut self, align: LabelAlign) { + let align = match align { + LabelAlign::Left => lvgl_sys::LV_LABEL_ALIGN_LEFT, + LabelAlign::Center => lvgl_sys::LV_LABEL_ALIGN_CENTER, + LabelAlign::Right => lvgl_sys::LV_LABEL_ALIGN_RIGHT, + LabelAlign::Auto => lvgl_sys::LV_LABEL_ALIGN_AUTO, + } as lvgl_sys::lv_label_align_t; + unsafe { + lvgl_sys::lv_label_set_align( + self.core.raw().as_mut(), + align + ); + } + } + + pub fn set_pos(&mut self, x: i16, y: i16) { + self.core.set_pos(x, y) + } + + pub fn set_size(&mut self, w: i16, h: i16) { + self.core.set_size(w, h) + } + + pub fn set_width(&mut self, w: u32) { + self.core.set_width(w) + } + + pub fn set_height(&mut self, h: u32) { + self.core.set_height(h) + } + + pub fn set_object_align(&mut self, base: &mut dyn Container, align: ObjectAlign, x_mod: i32, y_mod: i32) { + self.core.set_object_align(base, align, x_mod, y_mod); + } } impl Container for Label {