]> git.basschouten.com Git - openhab-addons.git/blob
df7c6545ebc4a85e8a0b5886ea4d278762ac614c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
28 /**
29  * Draws shapes, which are panels with a single LED.
30  *
31  * @author Jørgen Austvik - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class Shape extends Panel {
35
36     private final Point2D position;
37     private final int orientation;
38     private final int panelId;
39
40     public Shape(ShapeType shapeType, int panelId, Point2D position, int orientation) {
41         super(shapeType);
42         this.position = position;
43         this.orientation = orientation;
44         this.panelId = panelId;
45     }
46
47     public Point2D getPosition() {
48         return position;
49     }
50
51     public int getOrientation() {
52         return orientation;
53     };
54
55     protected int getPanelId() {
56         return panelId;
57     }
58
59     @Override
60     public abstract List<Point2D> generateOutline();
61
62     /**
63      * @param graphics The picture to draw on
64      * @param outline Outline of the shape to draw inside
65      * @return The position where the label of the shape should be placed
66      */
67     protected abstract ImagePoint2D labelPosition(Graphics2D graphics, List<ImagePoint2D> outline);
68
69     @Override
70     public void draw(Graphics2D graphics, DrawingSettings settings, PanelState state) {
71         List<ImagePoint2D> outline = settings.generateImagePoints(generateOutline());
72
73         Polygon p = new Polygon();
74         for (int i = 0; i < outline.size(); i++) {
75             ImagePoint2D pos = outline.get(i);
76             p.addPoint(pos.getX(), pos.getY());
77         }
78
79         HSBType color = state.getHSBForPanel(getPanelId());
80         graphics.setColor(new Color(color.getRGB()));
81         if (settings.shouldFillWithColor()) {
82             graphics.fillPolygon(p);
83         }
84
85         if (settings.shouldDrawOutline()) {
86             graphics.setColor(settings.getOutlineColor());
87             graphics.drawPolygon(p);
88         }
89
90         if (settings.shouldDrawLabels()) {
91             graphics.setColor(settings.getLabelColor());
92             ImagePoint2D textPos = labelPosition(graphics, outline);
93             graphics.drawString(Integer.toString(getPanelId()), textPos.getX(), textPos.getY());
94         }
95     }
96 }