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;
36 * @author Jørgen Austvik - Initial contribution
39 public class SingleLine extends Panel {
41 private static final int CORNER_DIAMETER = 4;
42 private static final int LINE_WIDTH = 6;
44 private final List<PositionDatum> corners;
46 public SingleLine(ShapeType shapeType, List<PositionDatum> corners) {
49 this.corners = Collections.unmodifiableList(new ArrayList<>(corners));
53 public List<Point2D> generateOutline() {
54 List<Point2D> result = new ArrayList<>(corners.size());
55 for (PositionDatum corner : corners) {
56 result.add(new Point2D(corner.getPosX(), corner.getPosY()));
63 public void draw(Graphics2D graphics, DrawingSettings settings, PanelState state) {
64 List<ImagePoint2D> outline = settings.generateImagePoints(generateOutline());
65 Polygon p = new Polygon();
66 for (int i = 0; i < outline.size(); i++) {
67 ImagePoint2D pos = outline.get(i);
68 p.addPoint(pos.getX(), pos.getY());
71 if (settings.shouldFillWithColor()) {
72 Color corner1Color = getColor(corners.get(0).getPanelId(), state);
73 Color corner2Color = getColor(corners.get(0).getPanelId(), state);
75 ImagePoint2D center = findCenter(outline);
77 Stroke oldStroke = graphics.getStroke();
78 Stroke lineStroke = new BasicStroke(LINE_WIDTH, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
79 graphics.setStroke(lineStroke);
80 graphics.setColor(corner1Color);
81 graphics.drawLine(outline.get(0).getX(), outline.get(0).getY(), center.getX(), center.getY());
82 graphics.setColor(corner2Color);
83 graphics.drawLine(outline.get(1).getX(), outline.get(1).getY(), center.getX(), center.getY());
84 graphics.setStroke(oldStroke);
87 if (settings.shouldDrawOutline()) {
88 graphics.setColor(settings.getOutlineColor());
89 graphics.drawPolygon(p);
92 if (settings.shouldDrawCorners()) {
93 for (PositionDatum corner : corners) {
94 ImagePoint2D position = settings.generateImagePoint(new Point2D(corner.getPosX(), corner.getPosY()));
95 graphics.setColor(getColor(corner.getPanelId(), state));
96 graphics.fillOval(position.getX() - CORNER_DIAMETER / 2, position.getY() - CORNER_DIAMETER / 2,
97 CORNER_DIAMETER, CORNER_DIAMETER);
99 if (settings.shouldDrawOutline()) {
100 graphics.setColor(settings.getOutlineColor());
101 graphics.drawOval(position.getX() - CORNER_DIAMETER / 2, position.getY() - CORNER_DIAMETER / 2,
102 CORNER_DIAMETER, CORNER_DIAMETER);
107 if (settings.shouldDrawLabels()) {
108 graphics.setColor(settings.getLabelColor());
110 for (PositionDatum corner : corners) {
111 ImagePoint2D position = settings.generateImagePoint(new Point2D(corner.getPosX(), corner.getPosY()));
112 graphics.drawString(Integer.toString(corner.getPanelId()), position.getX(), position.getY());
117 private ImagePoint2D findCenter(List<ImagePoint2D> outline) {
118 Point2D[] bounds = findBounds(outline);
119 int midX = bounds[0].getX() + (bounds[1].getX() - bounds[0].getX()) / 2;
120 int midY = bounds[0].getY() + (bounds[1].getY() - bounds[0].getY()) / 2;
121 return new ImagePoint2D(midX, midY);
124 private static Color getColor(int panelId, PanelState state) {
125 HSBType color = state.getHSBForPanel(panelId);
126 return new Color(color.getRGB());