pub struct ImageWidget { /* private fields */ }Expand description
This is the storage object for the ImageWidget. It stores the config, properties, callback registry,
the image name, and a scale flag.
Implementations§
Source§impl ImageWidget
Creates a new ImageWidget, which draws an image in a supported image format for SDL2 at a specific
location on the screen. Requires the name of the image (the full path to the file), the position
within the widget (defined as ImagePosition), the xywh bounds, and whether or not the image is
scaled within the bounds of the Widget.
impl ImageWidget
Creates a new ImageWidget, which draws an image in a supported image format for SDL2 at a specific
location on the screen. Requires the name of the image (the full path to the file), the position
within the widget (defined as ImagePosition), the xywh bounds, and whether or not the image is
scaled within the bounds of the Widget.
Sourcepub fn new(image_name: String, points: Points, size: Size, scaled: bool) -> Self
pub fn new(image_name: String, points: Points, size: Size, scaled: bool) -> Self
Creates a new instance of the ImageWidget object. Requires an image name (full path of the file),
image position (defined in ImagePosition), the xywh bounds of the Widget, and a scale flag.
If scaled is set to true, the image will be scaled within the Widget bounds, and the
ImagePosition will be ignored. Likewise, if set to false, the image will be displayed for
the size of the image, and will be placed in the bounds of the Widget based on the position
specified in the ImagePosition.
Examples found in repository?
11pub fn main() {
12 const WIDTH: u32 = 500;
13 const HEIGHT: u32 = 270;
14
15 let sdl_context = sdl2::init().unwrap();
16 let video_subsystem = sdl_context.video().unwrap();
17 let window = video_subsystem
18 .window("pushrod-render image demo", WIDTH, HEIGHT)
19 .position_centered()
20 .opengl()
21 .build()
22 .unwrap();
23 let mut engine = Engine::new(WIDTH, HEIGHT, 60);
24 let mut widget1 = ImageWidget::new(
25 String::from("assets/rust-48x48.jpg"),
26 make_points(20, 16),
27 make_size(60, 60),
28 false,
29 );
30
31 widget1.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
32 widget1.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::NW);
33
34 let mut widget2 = ImageWidget::new(
35 String::from("assets/rust-48x48.jpg"),
36 make_points(90, 16),
37 make_size(60, 60),
38 false,
39 );
40
41 widget2.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
42 widget2.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::N);
43
44 let mut widget3 = ImageWidget::new(
45 String::from("assets/rust-48x48.jpg"),
46 make_points(160, 16),
47 make_size(60, 60),
48 false,
49 );
50
51 widget3.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
52 widget3.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::NE);
53
54 let mut widget4 = ImageWidget::new(
55 String::from("assets/rust-48x48.jpg"),
56 make_points(20, 86),
57 make_size(60, 60),
58 false,
59 );
60
61 widget4.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
62 widget4.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::W);
63
64 let mut widget5 = ImageWidget::new(
65 String::from("assets/rust-48x48.jpg"),
66 make_points(90, 86),
67 make_size(60, 60),
68 false,
69 );
70
71 widget5.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
72 widget5.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::Center);
73
74 let mut widget6 = ImageWidget::new(
75 String::from("assets/rust-48x48.jpg"),
76 make_points(160, 86),
77 make_size(60, 60),
78 false,
79 );
80
81 widget6.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
82 widget6.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::E);
83
84 let mut widget7 = ImageWidget::new(
85 String::from("assets/rust-48x48.jpg"),
86 make_points(20, 156),
87 make_size(60, 60),
88 false,
89 );
90
91 widget7.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
92 widget7.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::SW);
93
94 let mut widget8 = ImageWidget::new(
95 String::from("assets/rust-48x48.jpg"),
96 make_points(90, 156),
97 make_size(60, 60),
98 false,
99 );
100
101 widget8.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
102 widget8.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::S);
103
104 let mut widget9 = ImageWidget::new(
105 String::from("assets/rust-48x48.jpg"),
106 make_points(160, 156),
107 make_size(60, 60),
108 false,
109 );
110
111 widget9.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
112 widget9.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::SE);
113
114 let mut widget10 = ImageWidget::new(
115 String::from("assets/rust-48x48.jpg"),
116 make_points(230, 16),
117 make_size(80, 80),
118 true,
119 );
120
121 widget10.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
122 widget10.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::NW);
123
124 let mut widget11 = ImageWidget::new(
125 String::from("assets/rust-48x48.jpg"),
126 make_points(260, 46),
127 make_size(120, 120),
128 true,
129 );
130
131 widget11.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
132 widget11.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::NW);
133
134 let mut widget12 = ImageWidget::new(
135 String::from("assets/rust-48x48.jpg"),
136 make_points(320, 86),
137 make_size(160, 160),
138 true,
139 );
140
141 widget12.set_color(CONFIG_COLOR_BASE, Color::RGB(0, 0, 0));
142 widget12.set_compass(CONFIG_IMAGE_POSITION, CompassPosition::NW);
143
144 engine.add_widget(Box::new(widget1), String::from("widget1"));
145 engine.add_widget(Box::new(widget2), String::from("widget2"));
146 engine.add_widget(Box::new(widget3), String::from("widget3"));
147 engine.add_widget(Box::new(widget4), String::from("widget4"));
148 engine.add_widget(Box::new(widget5), String::from("widget5"));
149 engine.add_widget(Box::new(widget6), String::from("widget6"));
150 engine.add_widget(Box::new(widget7), String::from("widget7"));
151 engine.add_widget(Box::new(widget8), String::from("widget8"));
152 engine.add_widget(Box::new(widget9), String::from("widget9"));
153 engine.add_widget(Box::new(widget10), String::from("widget10"));
154 engine.add_widget(Box::new(widget11), String::from("widget11"));
155 engine.add_widget(Box::new(widget12), String::from("widget12"));
156
157 engine.run(sdl_context, window);
158}Sourcepub fn get_texture_size(&self) -> Size
pub fn get_texture_size(&self) -> Size
Returns the size of the texture.
Trait Implementations§
Source§impl Widget for ImageWidget
This is the Widget implementation of the ImageWidget. Image is rendered onto a 3D texture, then
copied to the canvas after rendering.
impl Widget for ImageWidget
This is the Widget implementation of the ImageWidget. Image is rendered onto a 3D texture, then
copied to the canvas after rendering.
Source§fn on_config_changed(&mut self, _k: u8, _v: Config)
fn on_config_changed(&mut self, _k: u8, _v: Config)
Responds to a screen redraw only if the CONFIG_IMAGE_POSITION key was changed.
Source§fn as_any(&mut self) -> &mut dyn Any
fn as_any(&mut self) -> &mut dyn Any
This function is a macro-created getter function that returns the Widget as an Any
type. This allows the Widget trait to be downcast into a struct that implements
the Widget trait.
Source§fn get_config(&mut self) -> &mut WidgetConfig
fn get_config(&mut self) -> &mut WidgetConfig
This function is a macro-created getter function that returns the Widget’s configuration
object as a borrowed mutable reference. This code is auto-generated using the
default_widget_properties!() macro.
Source§fn get_system_properties(&mut self) -> &mut HashMap<i32, String>
fn get_system_properties(&mut self) -> &mut HashMap<i32, String>
This function is a macro-created getter function that returns the Widget’s system
properties as a borrowed mutable reference. This code is auto-generated using the
default_widget_properties!() macro.
Source§fn get_callbacks(&mut self) -> &mut CallbackRegistry
fn get_callbacks(&mut self) -> &mut CallbackRegistry
This function is a macro-created getter function that returns the Widget’s
CallbackRegistry object as a borrowed mutable reference. This code is auto-generated
using the default_widget_properties!() macro.
Source§fn tick_callback(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
)
fn tick_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )
This function is a macro-created tick callback override, created by the
default_widget_callbacks!() macro.
Source§fn mouse_entered_callback(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
)
fn mouse_entered_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )
This function is a macro-created mouse entered callback override, created by the
default_widget_callbacks!() macro.
Source§fn mouse_exited_callback(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
)
fn mouse_exited_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )
This function is a macro-created mouse exited callback override, created by the
default_widget_callbacks!() macro.
Source§fn mouse_moved_callback(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_points: Points,
)
fn mouse_moved_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )
This function is a macro-created mouse moved callback override, created by the
default_widget_callbacks!() macro.
Source§fn mouse_scrolled_callback(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_points: Points,
)
fn mouse_scrolled_callback( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )
This function is a macro-created mouse scrolled callback override, created by the
default_widget_callbacks!() macro.
This function is a macro-created mouse scrolled callback override, created by the
default_widget_callbacks!() macro.
Source§fn draw(
&mut self,
c: &mut Canvas<Window>,
t: &mut TextureCache,
) -> Option<&Texture>
fn draw( &mut self, c: &mut Canvas<Window>, t: &mut TextureCache, ) -> Option<&Texture>
mut in
your implementation (ie fn draw(&mut self, mut canvas: Canvas<Window>)). The _canvas
is the currently active drawing canvas at the time this function is called. This called
during the draw loop of the Engine. This returns a reference to the stored Texture object
within the Widget. It is then copied to the canvas, and displayed in the display loop.
In this function, you can just return a reference to the Texture if no invalidation state
was set, otherwise, the draw can be re-performed, and the Texture returned. If the drawing
function returns no texture, return a None, and it will not be rendered during the display
loop, but it will still be called. A TextureCache is provided in case your Widget needs
to cache an image or a font store. Read moreSource§fn mouse_entered(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
)
fn mouse_entered( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )
Widget, this function is triggered. This function
implementation is optional.Source§fn mouse_exited(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
)
fn mouse_exited( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], )
Widget, this function is triggered. This function
implementation is optional.Source§fn mouse_moved(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_points: Points,
)
fn mouse_moved( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )
Widget, this function is triggered. It
contains the X and Y coordinates relative to the bounds of the Widget. The
points start at 0x0. This function implementation is optional.Source§fn mouse_scrolled(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_points: Points,
)
fn mouse_scrolled( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _points: Points, )
Widget, this function is
triggered. Movement along the X axis indicate horizontal movement, where the Y axis
indicates vertical movement. Positive movement means to the right or down, respectively.
Negative movement means to the left or up, respectively. This function implementation
is optional.Widget, this
function is called. If a mouse button is clicked, and the mouse leaves the bounds of the
Widget, the mouse release event will still be triggered for the last Widget which
received the mouse down state. This prevents Widgets from becoming confused. This
behavior is tracked by the main loop, not by the Widget code. Therefore, when a mouse
button is released outside of the bounds of this Widget, you must adjust your state
accordingly, if you pay attention to the button_clicked function. This function
implementation is optional.Source§fn tick(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer])
fn tick(&mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer])
Source§fn other_event(
&mut self,
_widgets: &[WidgetContainer],
_layouts: &[LayoutContainer],
_event: Event,
)
fn other_event( &mut self, _widgets: &[WidgetContainer], _layouts: &[LayoutContainer], _event: Event, )
Event is sent to the application that is not handled by the Engine::run loop, this
method is called, sending the unhandled Event to the currently active Widget. This behavior
is subject to change as the Engine::run loop is modified to handle more Events.Source§fn set_numeric(&mut self, config: u8, value: i32)
fn set_numeric(&mut self, config: u8, value: i32)
Source§fn set_toggle(&mut self, config: u8, flag: bool)
fn set_toggle(&mut self, config: u8, flag: bool)
Source§fn set_compass(&mut self, config: u8, value: CompassPosition)
fn set_compass(&mut self, config: u8, value: CompassPosition)
Source§fn get_point(&mut self, k: u8) -> Points
fn get_point(&mut self, k: u8) -> Points
Points for a configuration key. Returns Points::default if not set.Source§fn get_size(&mut self, k: u8) -> Size
fn get_size(&mut self, k: u8) -> Size
Size for a configuration key. Returns a Size::default if not set.Source§fn get_color(&mut self, k: u8) -> Color
fn get_color(&mut self, k: u8) -> Color
Color for a configuration key. Returns white if not set.Source§fn get_numeric(&mut self, k: u8) -> i32
fn get_numeric(&mut self, k: u8) -> i32
Source§fn get_text(&mut self, k: u8) -> String
fn get_text(&mut self, k: u8) -> String
Source§fn get_toggle(&mut self, k: u8) -> bool
fn get_toggle(&mut self, k: u8) -> bool
false if not set.Source§fn get_compass(&mut self, k: u8) -> CompassPosition
fn get_compass(&mut self, k: u8) -> CompassPosition
CompassPosition toggle for a configuration key. Returns CompassPosition::W if not set.Source§fn set_origin(&mut self, _origin: Points)
fn set_origin(&mut self, _origin: Points)
Widget, adjusting the X and Y coordinates. Automatically sets the
invalidate flag to true when adjusted, but only if the new origin is not the same as
the previous origin.Source§fn set_size(&mut self, _size: Vec<u32>)
fn set_size(&mut self, _size: Vec<u32>)
Widget, adjusting the width and height. Automatically
sets the invalidate flag to true when adjusted, but only if the new size is not the
same as the previous size.Source§fn get_drawing_area(&mut self) -> Rect
fn get_drawing_area(&mut self) -> Rect
Rect object containing the drawing bounds of this Widget.Source§fn is_invalidated(&mut self) -> bool
fn is_invalidated(&mut self) -> bool
Widget is invalidated state.Source§fn set_invalidated(&mut self, flag: bool)
fn set_invalidated(&mut self, flag: bool)
Widget.