2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal.layout.shape;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Graphics2D;
18 import java.awt.Polygon;
19 import java.awt.Stroke;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.nanoleaf.internal.layout.DrawingSettings;
26 import org.openhab.binding.nanoleaf.internal.layout.ImagePoint2D;
27 import org.openhab.binding.nanoleaf.internal.layout.PanelState;
28 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
29 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
30 import org.openhab.binding.nanoleaf.internal.model.PositionDatum;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.util.ColorUtil;
37 * @author Jørgen Austvik - Initial contribution
40 public class SingleLine extends Panel {
42 private static final int CORNER_DIAMETER = 4;
43 private static final int LINE_WIDTH = 6;
45 private final List<PositionDatum> corners;
47 public SingleLine(ShapeType shapeType, List<PositionDatum> corners) {
50 this.corners = Collections.unmodifiableList(new ArrayList<>(corners));
54 public List<Point2D> generateOutline() {
55 List<Point2D> result = new ArrayList<>(corners.size());
56 for (PositionDatum corner : corners) {
57 result.add(new Point2D(corner.getPosX(), corner.getPosY()));
64 public void draw(Graphics2D graphics, DrawingSettings settings, PanelState state) {
65 List<ImagePoint2D> outline = settings.generateImagePoints(generateOutline());
66 Polygon p = new Polygon();
67 for (int i = 0; i < outline.size(); i++) {
68 ImagePoint2D pos = outline.get(i);
69 p.addPoint(pos.getX(), pos.getY());
72 if (settings.shouldFillWithColor()) {
73 Color corner1Color = getColor(corners.get(0).getPanelId(), state);
74 Color corner2Color = getColor(corners.get(0).getPanelId(), state);
76 ImagePoint2D center = findCenter(outline);
78 Stroke oldStroke = graphics.getStroke();
79 Stroke lineStroke = new BasicStroke(LINE_WIDTH, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
80 graphics.setStroke(lineStroke);
81 graphics.setColor(corner1Color);
82 graphics.drawLine(outline.get(0).getX(), outline.get(0).getY(), center.getX(), center.getY());
83 graphics.setColor(corner2Color);
84 graphics.drawLine(outline.get(1).getX(), outline.get(1).getY(), center.getX(), center.getY());
85 graphics.setStroke(oldStroke);
88 if (settings.shouldDrawOutline()) {
89 graphics.setColor(settings.getOutlineColor());
90 graphics.drawPolygon(p);
93 if (settings.shouldDrawCorners()) {
94 for (PositionDatum corner : corners) {
95 ImagePoint2D position = settings.generateImagePoint(new Point2D(corner.getPosX(), corner.getPosY()));
96 graphics.setColor(getColor(corner.getPanelId(), state));
97 graphics.fillOval(position.getX() - CORNER_DIAMETER / 2, position.getY() - CORNER_DIAMETER / 2,
98 CORNER_DIAMETER, CORNER_DIAMETER);
100 if (settings.shouldDrawOutline()) {
101 graphics.setColor(settings.getOutlineColor());
102 graphics.drawOval(position.getX() - CORNER_DIAMETER / 2, position.getY() - CORNER_DIAMETER / 2,
103 CORNER_DIAMETER, CORNER_DIAMETER);
108 if (settings.shouldDrawLabels()) {
109 graphics.setColor(settings.getLabelColor());
111 for (PositionDatum corner : corners) {
112 ImagePoint2D position = settings.generateImagePoint(new Point2D(corner.getPosX(), corner.getPosY()));
113 graphics.drawString(Integer.toString(corner.getPanelId()), position.getX(), position.getY());
118 private ImagePoint2D findCenter(List<ImagePoint2D> outline) {
119 Point2D[] bounds = findBounds(outline);
120 int midX = bounds[0].getX() + (bounds[1].getX() - bounds[0].getX()) / 2;
121 int midY = bounds[0].getY() + (bounds[1].getY() - bounds[0].getY()) / 2;
122 return new ImagePoint2D(midX, midY);
125 private static Color getColor(int panelId, PanelState state) {
126 HSBType color = state.getHSBForPanel(panelId);
127 return new Color(ColorUtil.hsbTosRgb(color));