]> git.basschouten.com Git - openhab-addons.git/blob
65f3fcbf0f6a143ce1b8a76e672fc2f8a7aa3e5f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal.layout.shape;
14
15 import java.awt.Color;
16 import java.awt.Graphics2D;
17 import java.awt.Polygon;
18 import java.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.nanoleaf.internal.layout.DrawingSettings;
22 import org.openhab.binding.nanoleaf.internal.layout.ImagePoint2D;
23 import org.openhab.binding.nanoleaf.internal.layout.PanelState;
24 import org.openhab.binding.nanoleaf.internal.layout.Point2D;
25 import org.openhab.binding.nanoleaf.internal.layout.ShapeType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.util.ColorUtil;
28
29 /**
30  * Draws shapes, which are panels with a single LED.
31  *
32  * @author Jørgen Austvik - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class Shape extends Panel {
36
37     private final Point2D position;
38     private final int orientation;
39     private final int panelId;
40
41     public Shape(ShapeType shapeType, int panelId, Point2D position, int orientation) {
42         super(shapeType);
43         this.position = position;
44         this.orientation = orientation;
45         this.panelId = panelId;
46     }
47
48     public Point2D getPosition() {
49         return position;
50     }
51
52     public int getOrientation() {
53         return orientation;
54     };
55
56     protected int getPanelId() {
57         return panelId;
58     }
59
60     @Override
61     public abstract List<Point2D> generateOutline();
62
63     /**
64      * @param graphics The picture to draw on
65      * @param outline Outline of the shape to draw inside
66      * @return The position where the label of the shape should be placed
67      */
68     protected abstract ImagePoint2D labelPosition(Graphics2D graphics, List<ImagePoint2D> outline);
69
70     @Override
71     public void draw(Graphics2D graphics, DrawingSettings settings, PanelState state) {
72         List<ImagePoint2D> outline = settings.generateImagePoints(generateOutline());
73
74         Polygon p = new Polygon();
75         for (int i = 0; i < outline.size(); i++) {
76             ImagePoint2D pos = outline.get(i);
77             p.addPoint(pos.getX(), pos.getY());
78         }
79
80         HSBType color = state.getHSBForPanel(getPanelId());
81         graphics.setColor(new Color(ColorUtil.hsbTosRgb(color)));
82         if (settings.shouldFillWithColor()) {
83             graphics.fillPolygon(p);
84         }
85
86         if (settings.shouldDrawOutline()) {
87             graphics.setColor(settings.getOutlineColor());
88             graphics.drawPolygon(p);
89         }
90
91         if (settings.shouldDrawLabels()) {
92             graphics.setColor(settings.getLabelColor());
93             ImagePoint2D textPos = labelPosition(graphics, outline);
94             graphics.drawString(Integer.toString(getPanelId()), textPos.getX(), textPos.getY());
95         }
96     }
97 }