@@ -25,7 +25,7 @@ pub struct TextureSlicer {
25
25
}
26
26
27
27
/// Defines how a texture slice scales when resized
28
- #[ derive( Debug , Clone , Default , Reflect ) ]
28
+ #[ derive( Debug , Copy , Clone , Default , Reflect ) ]
29
29
pub enum SliceScaleMode {
30
30
/// The slice will be stretched to fit the area
31
31
#[ default]
@@ -55,8 +55,7 @@ pub(crate) struct TextureSlice {
55
55
impl TextureSlicer {
56
56
/// Computes the 4 corner slices
57
57
fn corner_slices ( & self , base_rect : Rect , render_size : Vec2 ) -> [ TextureSlice ; 4 ] {
58
- let image_size = base_rect. size ( ) ;
59
- let coef = render_size / image_size;
58
+ let coef = render_size / base_rect. size ( ) ;
60
59
let min_coef = coef. x . min ( coef. y ) . min ( self . max_corner_scale . max ( 0.001 ) ) ;
61
60
[
62
61
// Top Left Corner
@@ -74,8 +73,8 @@ impl TextureSlicer {
74
73
// Top Right Corner
75
74
TextureSlice {
76
75
texture_rect : Rect {
77
- min : base_rect . min + Vec2 :: new ( image_size . x - self . border . right , 0.0 ) ,
78
- max : base_rect . min + Vec2 :: new ( image_size . x , self . border . top ) ,
76
+ min : Vec2 :: new ( base_rect . max . x - self . border . right , base_rect . min . y ) ,
77
+ max : Vec2 :: new ( base_rect . max . x , self . border . top ) ,
79
78
} ,
80
79
draw_size : Vec2 :: new ( self . border . right , self . border . top ) * min_coef,
81
80
offset : Vec2 :: new (
@@ -86,8 +85,8 @@ impl TextureSlicer {
86
85
// Bottom Left
87
86
TextureSlice {
88
87
texture_rect : Rect {
89
- min : base_rect . min + Vec2 :: new ( 0.0 , image_size . y - self . border . bottom ) ,
90
- max : base_rect. min + Vec2 :: new ( self . border . left , image_size . y ) ,
88
+ min : Vec2 :: new ( base_rect . min . x , base_rect . max . y - self . border . bottom ) ,
89
+ max : Vec2 :: new ( base_rect. min . x + self . border . left , base_rect . max . y ) ,
91
90
} ,
92
91
draw_size : Vec2 :: new ( self . border . left , self . border . bottom ) * min_coef,
93
92
offset : Vec2 :: new (
@@ -98,12 +97,11 @@ impl TextureSlicer {
98
97
// Bottom Right Corner
99
98
TextureSlice {
100
99
texture_rect : Rect {
101
- min : base_rect. min
102
- + Vec2 :: new (
103
- image_size. x - self . border . right ,
104
- image_size. y - self . border . bottom ,
105
- ) ,
106
- max : base_rect. min + Vec2 :: new ( image_size. x , image_size. y ) ,
100
+ min : Vec2 :: new (
101
+ base_rect. max . x - self . border . right ,
102
+ base_rect. max . y - self . border . bottom ,
103
+ ) ,
104
+ max : base_rect. max ,
107
105
} ,
108
106
draw_size : Vec2 :: new ( self . border . right , self . border . bottom ) * min_coef,
109
107
offset : Vec2 :: new (
@@ -121,12 +119,14 @@ impl TextureSlicer {
121
119
base_rect : Rect ,
122
120
render_size : Vec2 ,
123
121
) -> [ TextureSlice ; 2 ] {
124
- let image_size = base_rect. size ( ) ;
125
122
// left
126
123
let left_side = TextureSlice {
127
124
texture_rect : Rect {
128
125
min : base_rect. min + Vec2 :: new ( 0.0 , self . border . top ) ,
129
- max : base_rect. min + Vec2 :: new ( self . border . left , image_size. y - self . border . bottom ) ,
126
+ max : Vec2 :: new (
127
+ base_rect. min . x + self . border . left ,
128
+ base_rect. max . y - self . border . bottom ,
129
+ ) ,
130
130
} ,
131
131
draw_size : Vec2 :: new (
132
132
bl_corner. draw_size . x ,
@@ -138,9 +138,11 @@ impl TextureSlicer {
138
138
// right
139
139
let right_side = TextureSlice {
140
140
texture_rect : Rect {
141
- min : base_rect. min
142
- + Vec2 :: new ( image_size. x - self . border . right , self . border . bottom ) ,
143
- max : base_rect. min + Vec2 :: new ( image_size. x , image_size. y - self . border . top ) ,
141
+ min : Vec2 :: new (
142
+ base_rect. max . x - self . border . right ,
143
+ base_rect. min . y + self . border . bottom ,
144
+ ) ,
145
+ max : Vec2 :: new ( base_rect. max . x , base_rect. max . y - self . border . top ) ,
144
146
} ,
145
147
draw_size : Vec2 :: new (
146
148
br_corner. draw_size . x ,
@@ -158,12 +160,14 @@ impl TextureSlicer {
158
160
base_rect : Rect ,
159
161
render_size : Vec2 ,
160
162
) -> [ TextureSlice ; 2 ] {
161
- let image_size = base_rect. size ( ) ;
162
163
// Bottom
163
164
let bot_side = TextureSlice {
164
165
texture_rect : Rect {
165
- min : base_rect. min + Vec2 :: new ( self . border . left , image_size. y - self . border . bottom ) ,
166
- max : base_rect. min + Vec2 :: new ( image_size. x - self . border . right , image_size. y ) ,
166
+ min : Vec2 :: new (
167
+ base_rect. min . x + self . border . left ,
168
+ base_rect. max . y - self . border . bottom ,
169
+ ) ,
170
+ max : Vec2 :: new ( base_rect. max . x - self . border . right , base_rect. max . y ) ,
167
171
} ,
168
172
draw_size : Vec2 :: new (
169
173
render_size. x - ( bl_corner. draw_size . x + br_corner. draw_size . x ) ,
@@ -176,7 +180,10 @@ impl TextureSlicer {
176
180
let top_side = TextureSlice {
177
181
texture_rect : Rect {
178
182
min : base_rect. min + Vec2 :: new ( self . border . left , 0.0 ) ,
179
- max : base_rect. min + Vec2 :: new ( image_size. x - self . border . right , self . border . top ) ,
183
+ max : Vec2 :: new (
184
+ base_rect. max . x - self . border . right ,
185
+ base_rect. min . y + self . border . top ,
186
+ ) ,
180
187
} ,
181
188
draw_size : Vec2 :: new (
182
189
render_size. x - ( tl_corner. draw_size . x + tr_corner. draw_size . x ) ,
@@ -187,6 +194,13 @@ impl TextureSlicer {
187
194
[ bot_side, top_side]
188
195
}
189
196
197
+ /// Slices the given `rect` into at least 9 sections. If the center and/or side parts are set to tile,
198
+ /// a bigger number of sections will be computed.
199
+ ///
200
+ /// # Arguments
201
+ ///
202
+ /// * `rect` - The section of the texture to slice in 9 parts
203
+ /// * `render_size` - The optional draw size of the texture. If not set the `rect` size will be used.
190
204
pub ( crate ) fn compute_slices (
191
205
& self ,
192
206
rect : Rect ,
@@ -203,8 +217,7 @@ impl TextureSlicer {
203
217
let center = TextureSlice {
204
218
texture_rect : Rect {
205
219
min : rect. min + Vec2 :: new ( self . border . left , self . border . bottom ) ,
206
- max : rect. min
207
- + Vec2 :: new ( rect. max . x - self . border . right , rect. max . y - self . border . top ) ,
220
+ max : Vec2 :: new ( rect. max . x - self . border . right , rect. max . y - self . border . top ) ,
208
221
} ,
209
222
draw_size : Vec2 :: new (
210
223
render_size. x - ( corners[ 2 ] . draw_size . x + corners[ 3 ] . draw_size . x ) ,
0 commit comments