-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNineSliceImageComponent.cpp
More file actions
145 lines (119 loc) · 5.19 KB
/
NineSliceImageComponent.cpp
File metadata and controls
145 lines (119 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2025 JDSherbert. All rights reserved.
#include "NineSliceImageComponent.h"
// ======================================================================= //
Sherbert::SliceLayout::SliceLayout
(
const juce::Image& image,
juce::Rectangle<int> destinationBounds,
const Margin& margin
)
{
const int imageWidth = image.getWidth();
const int imageHeight = image.getHeight();
const int destinationWidth = destinationBounds.getWidth();
const int destinationHeight = destinationBounds.getHeight();
const int rightEdgeX = destinationWidth - margin.right;
const int bottomEdgeY = destinationHeight - margin.bottom;
const int sourceCenterWidth = imageWidth - margin.left - margin.right;
const int sourceCenterHeight = imageHeight - margin.top - margin.bottom;
const int destCenterWidth = destinationWidth - margin.left - margin.right;
const int destCenterHeight = destinationHeight - margin.top - margin.bottom;
// Source rects
const juce::Rectangle<int> sourceRects[3][3] =
{
{
{ 0, 0, margin.left, margin.top },
{ margin.left, 0, sourceCenterWidth, margin.top },
{ margin.left + sourceCenterWidth, 0, margin.right,margin.top }
},
{
{ 0, margin.top, margin.left, sourceCenterHeight },
{ margin.left, margin.top, sourceCenterWidth, sourceCenterHeight },
{ margin.left + sourceCenterWidth, margin.top, margin.right,sourceCenterHeight }
},
{
{ 0, margin.top + sourceCenterHeight, margin.left, margin.bottom },
{ margin.left, margin.top + sourceCenterHeight, sourceCenterWidth, margin.bottom },
{ margin.left + sourceCenterWidth, margin.top + sourceCenterHeight, margin.right,margin.bottom }
}
};
// Destination rects
const juce::Rectangle<int> destinationRects[3][3] =
{
{
{ 0, 0, margin.left, margin.top },
{ margin.left, 0, destCenterWidth, margin.top },
{ rightEdgeX, 0, margin.right, margin.top }
},
{
{ 0, margin.top, margin.left, destCenterHeight },
{ margin.left, margin.top, destCenterWidth, destCenterHeight },
{ rightEdgeX, margin.top, margin.right,destCenterHeight }
},
{
{ 0, bottomEdgeY, margin.left, margin.bottom },
{ margin.left, bottomEdgeY, destCenterWidth, margin.bottom },
{ rightEdgeX, bottomEdgeY, margin.right, margin.bottom }
}
};
// Flatten into array
int index = 0;
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 3; ++col)
{
slices[index++] = { destinationRects[row][col], sourceRects[row][col] };
}
}
}
// ======================================================================= //
void Sherbert::NineSliceImageComponent::setImage(const juce::Image& newImage, const Margin& newMargins)
{
jassert(newMargins.left >= 0);
jassert(newMargins.top >= 0);
jassert(newMargins.right >= 0);
jassert(newMargins.bottom >= 0);
jassert(newMargins.left + newMargins.right <= newImage.getWidth());
jassert(newMargins.top + newMargins.bottom <= newImage.getHeight());
image = newImage;
margins = newMargins;
repaint();
}
// ======================================================================= //
void Sherbert::NineSliceImageComponent::paint(juce::Graphics& g)
{
if (!image.isValid())
return;
g.setImageResamplingQuality(resamplingQuality);
const SliceLayout layout(image, getLocalBounds(), margins);
for (const auto& slice : layout.slices)
{
if (slice.source.isEmpty() || slice.destination.isEmpty()) continue;
g.drawImage
(
image,
slice.destination.getX(), slice.destination.getY(),
slice.destination.getWidth(), slice.destination.getHeight(),
slice.source.getX(), slice.source.getY(),
slice.source.getWidth(), slice.source.getHeight()
);
}
// DEBUG OVERLAY
if (showDebugSlices)
{
const auto bounds = getLocalBounds();
const int boundsWidth = bounds.getWidth();
const int boundsHeight = bounds.getHeight();
g.setColour(juce::Colours::magenta.withAlpha(0.6f));
// Vertical slice boundaries
g.drawVerticalLine(margins.left, 0.0f, (float)boundsHeight);
g.drawVerticalLine(boundsWidth - margins.right, 0.0f, (float)boundsHeight);
// Horizontal slice boundaries
g.drawHorizontalLine(margins.top, 0.0f, (float)boundsWidth);
g.drawHorizontalLine(boundsHeight - margins.bottom, 0.0f, (float)boundsWidth);
// Outline component
g.setColour(juce::Colours::cyan.withAlpha(0.6f));
g.drawRect(bounds, 1);
}
}
// ======================================================================= //