Skip to content

Commit 9607ca1

Browse files
Merge pull request #5649 from Baaaaaz/feature/5632-apply-flat-labels-to-initiative-panel
Apply flat labels to initiative panel
2 parents 33eda8f + eaa1848 commit 9607ca1

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/main/java/net/rptools/maptool/client/swing/label/FlatImageLabel.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,51 @@ public Dimension getDimensions(Graphics2D graphics2D, String string) {
164164

165165
/**
166166
* Renders a string with customizable properties such as font, padding, colors, and justification
167-
* using the specified graphics context.
167+
* using the specified graphics context. Height and width are computed based on the provided
168+
* string. Also see {@link #render(Graphics2D, int, int, int, int, String)}.
168169
*
169170
* @param graphics2D the graphics context used for rendering
170171
* @param x the x-coordinate of the top-left corner of the rendered string
171172
* @param y the y-coordinate of the top-left corner of the rendered string
172173
* @param string the string to be rendered
173174
* @return a Rectangle representing the dimensions and position of the rendered string with
174-
* padding
175+
* padding.
175176
*/
176177
public Rectangle render(Graphics2D graphics2D, int x, int y, String string) {
178+
177179
var g2d = (Graphics2D) graphics2D.create();
178180
g2d.setFont(font);
179-
var fm = g2d.getFontMetrics();
180-
int strWidth = SwingUtilities.computeStringWidth(fm, string);
181-
int strHeight = fm.getAscent() - fm.getDescent() - fm.getLeading();
182181

183182
var dim = getDimensions(g2d, string);
184183
int width = (int) dim.getWidth();
185184
int height = (int) dim.getHeight();
186185

186+
g2d.dispose();
187+
188+
return render(graphics2D, x, y, width, height, string);
189+
}
190+
191+
/**
192+
* Renders a string with customizable properties such as font, padding, colors, and justification
193+
* using the specified graphics context.
194+
*
195+
* @param graphics2D the graphics context used for rendering
196+
* @param x the x-coordinate of the top-left corner of the rendered string
197+
* @param y the y-coordinate of the top-left corner of the rendered string
198+
* @param width the width of the label
199+
* @param height the height of the label
200+
* @param string the string to be rendered
201+
* @return a Rectangle representing the dimensions and position of the rendered string with
202+
* padding
203+
*/
204+
public Rectangle render(
205+
Graphics2D graphics2D, int x, int y, int width, int height, String string) {
206+
var g2d = (Graphics2D) graphics2D.create();
207+
g2d.setFont(font);
208+
var fm = g2d.getFontMetrics();
209+
int strWidth = SwingUtilities.computeStringWidth(fm, string);
210+
int strHeight = fm.getAscent() - fm.getDescent() - fm.getLeading();
211+
187212
var bounds = new Rectangle(x, y, width, height);
188213

189214
int stringY = y + height / 2 + strHeight / 2;
@@ -206,6 +231,7 @@ public Rectangle render(Graphics2D graphics2D, int x, int y, String string) {
206231
g2d.draw(labelRect);
207232
}
208233

234+
g2d.dispose();
209235
return bounds;
210236
}
211237
}

src/main/java/net/rptools/maptool/client/ui/tokenpanel/InitiativeListCellRenderer.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
import net.rptools.lib.image.ImageUtil;
3131
import net.rptools.maptool.client.AppPreferences;
3232
import net.rptools.maptool.client.MapTool;
33-
import net.rptools.maptool.client.swing.ImageLabel;
3433
import net.rptools.maptool.client.swing.SwingUtil;
34+
import net.rptools.maptool.client.swing.label.FlatImageLabel;
35+
import net.rptools.maptool.client.swing.label.FlatImageLabelFactory;
3536
import net.rptools.maptool.client.ui.theme.Borders;
3637
import net.rptools.maptool.client.ui.theme.Icons;
3738
import net.rptools.maptool.client.ui.theme.RessourceManager;
3839
import net.rptools.maptool.client.ui.token.AbstractTokenOverlay;
3940
import net.rptools.maptool.client.ui.token.BarTokenOverlay;
4041
import net.rptools.maptool.model.InitiativeList.TokenInitiative;
4142
import net.rptools.maptool.model.Token;
42-
import net.rptools.maptool.util.GraphicsUtil;
4343
import net.rptools.maptool.util.ImageManager;
4444

4545
/**
@@ -64,7 +64,7 @@ public class InitiativeListCellRenderer extends JPanel
6464
private final InitiativePanel panel;
6565

6666
/** Used to draw the background of the item. */
67-
private ImageLabel backgroundImageLabel;
67+
private FlatImageLabel backgroundFlatImageLabel;
6868

6969
/**
7070
* The text height for the background image label. Only the text is painted inside, the token
@@ -158,11 +158,18 @@ public Component getListCellRendererComponent(
158158
setBorder(UNSELECTED_BORDER);
159159
return this;
160160
} // endif
161-
backgroundImageLabel =
162-
token.isVisible()
163-
? token.getType() == Token.Type.NPC ? GraphicsUtil.BLUE_LABEL : GraphicsUtil.GREY_LABEL
164-
: GraphicsUtil.DARK_GREY_LABEL;
165-
name.setForeground(Color.BLACK);
161+
162+
var labelRenderFactory = new FlatImageLabelFactory();
163+
backgroundFlatImageLabel = labelRenderFactory.getMapImageLabel(token);
164+
165+
// We still use the UI text so use the map label color preferences
166+
if (!token.isVisible()) {
167+
name.setForeground(AppPreferences.nonVisibleTokenMapLabelForeground.get());
168+
} else if (token.getType() == Token.Type.NPC) {
169+
name.setForeground(AppPreferences.npcMapLabelForeground.get());
170+
} else {
171+
name.setForeground(AppPreferences.pcMapLabelForeground.get());
172+
}
166173
name.setFont(name.getFont().deriveFont(token.isVisible() ? Font.PLAIN : Font.ITALIC));
167174

168175
// Show the indicator?
@@ -233,7 +240,8 @@ protected void paintComponent(Graphics g) {
233240
boolean initStateSecondLine = panel.isInitStateSecondLine() && panel.isShowInitState();
234241
Dimension s = name.getSize();
235242
int th = (textHeight + 2) * (initStateSecondLine ? 2 : 1);
236-
backgroundImageLabel.renderLabel((Graphics2D) g, 0, (s.height - th) / 2, s.width, th);
243+
// render an image label with set dimensions and without text
244+
backgroundFlatImageLabel.render((Graphics2D) g, 0, (s.height - th) / 2, s.width, th, "");
237245
super.paintComponent(g);
238246
}
239247

@@ -247,7 +255,7 @@ public Dimension getPreferredSize() {
247255
int th = textHeight * (initStateSecondLine ? 2 : 1);
248256
Insets insets = getInsets();
249257
if (getIcon() != null) th = Math.max(th, getIcon().getIconHeight());
250-
s.height = th + insets.top + insets.bottom - 4;
258+
s.height = th + insets.top + insets.bottom;
251259
return s;
252260
}
253261
}

0 commit comments

Comments
 (0)