Skip to content

Commit 03c26a8

Browse files
authored
Merge pull request jMonkeyEngine#2349 from JNightRider/fix_2345
[ Fix ] Minor refactoring to DisplayInfo.class jMonkeyEngine#2345
2 parents 7aad158 + bf0a5eb commit 03c26a8

File tree

4 files changed

+250
-54
lines changed

4 files changed

+250
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,255 @@
11
/*
2-
* Copyright (c) 2009-2023 jMonkeyEngine All rights reserved.
2+
* Copyright (c) 2009-2025 jMonkeyEngine
3+
* All rights reserved.
34
*
4-
* Redistribution and use in source and binary forms, with or without modification, are permitted
5-
* provided that the following conditions are met:
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
68
*
7-
* * Redistributions of source code must retain the above copyright notice, this list of conditions
8-
* and the following disclaimer.
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
911
*
10-
* * Redistributions in binary form must reproduce the above copyright notice, this list of
11-
* conditions and the following disclaimer in the documentation and/or other materials provided with
12-
* the distribution.
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
1315
*
14-
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors may be used to endorse or
15-
* promote products derived from this software without specific prior written permission.
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
1619
*
17-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18-
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19-
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20-
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22-
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23-
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
24-
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2531
*/
2632
package com.jme3.system;
2733

34+
import java.util.Objects;
35+
2836
/**
2937
* This class holds information about the display that was returned by glfwGetMonitors() calls in
3038
* the context class
3139
*
3240
* @author Kevin Bales
41+
* @author wil
3342
*/
34-
public class DisplayInfo {
43+
public final class DisplayInfo {
44+
45+
/** display - display id that was return from Lwjgl3. */
46+
private long display;
47+
48+
/** width - width that was return from Lwjgl3. */
49+
private int width;
50+
51+
/** height - height that was return from Lwjgl3. */
52+
private int height;
53+
54+
/** rate - refresh rate that was return from Lwjgl3. */
55+
private int rate;
56+
57+
/** primary - indicates if the display is the primary monitor. */
58+
private boolean primary;
3559

3660
/**
37-
* displayID - display id that was return from Lwjgl3.
61+
* name - display name that was return from Lwjgl3.
3862
*/
39-
public long displayID = 0;
63+
private String name;
4064

4165
/**
42-
* width - width that was return from Lwjgl3.
66+
* Create a new display mode object with the default values
4367
*/
44-
public int width = 1080;
68+
DisplayInfo() {
69+
this(0L /*NULL*/, 1080, 1920, 60, false, "Generic Monitor");
70+
}
71+
72+
/**
73+
* Create a new display mode object with the supplied parameters.
74+
* @param display the monitor pointer (native), the virtual memory
75+
* address used by lwjgl.
76+
* @param width the width of the display, provided by lwjgl.
77+
* @param height the height of the display, provided by lwjgl.
78+
* @param rate the refresh rate of the display, in hertz.
79+
* @param primary a logical value that determines whether this display is
80+
* primary or not; {@code true | false}
81+
* @param name display name
82+
*/
83+
DisplayInfo(long display, int width, int height, int rate, boolean primary, String name) {
84+
this.display = display;
85+
this.width = width;
86+
this.height = height;
87+
this.rate = rate;
88+
this.primary = primary;
89+
this.name = name;
90+
}
4591

92+
// === ----------------------------------------------------------------- ===
93+
// === SETTERS ===
94+
// === ----------------------------------------------------------------- ===
95+
4696
/**
47-
* height - height that was return from Lwjgl3.
97+
* Sets the monitor pointer (native), the virtual memory address used by lwjgl.
98+
*
99+
* @param display the monitor pointer (native), the virtual memory
100+
* address used by lwjgl
48101
*/
49-
public int height = 1920;
102+
void setDisplay(long display) {
103+
this.display = display;
104+
}
50105

51106
/**
52-
* rate - refresh rate that was return from Lwjgl3.
107+
* Sets the width of the display.
108+
* @param width the width of the display
53109
*/
54-
public int rate = 60;
110+
void setWidth(int width) {
111+
this.width = width;
112+
}
55113

56114
/**
57-
* primary - indicates if the display is the primary monitor.
115+
* Sets the height of the display.
116+
* @param height the height of the display
58117
*/
59-
public boolean primary = false;
118+
void setHeight(int height) {
119+
this.height = height;
120+
}
60121

61122
/**
62-
* name - display name that was return from Lwjgl3.
123+
* Sets the refresh rate of the display, in hertz.
124+
* @param rate the refresh rate of the display, in hertz
125+
*/
126+
void setRate(int rate) {
127+
this.rate = rate;
128+
}
129+
130+
/**
131+
* Set this display as primary or not.
132+
* @param primary {@code true} if the display is primary, {@code false} otherwise.
133+
*/
134+
void setPrimary(boolean primary) {
135+
this.primary = primary;
136+
}
137+
138+
/**
139+
* Set the screen (display) name
140+
* @param name display name
141+
*/
142+
void setName(String name) {
143+
this.name = name;
144+
}
145+
146+
// === ----------------------------------------------------------------- ===
147+
// === GETTERS ===
148+
// === ----------------------------------------------------------------- ===
149+
150+
/**
151+
* Returns the monitor pointer (native), the virtual memory address used by lwjgl.
152+
* @return the monitor pointer (native), the virtual memory address used by lwjgl
153+
*/
154+
public long getDisplay() {
155+
return display;
156+
}
157+
158+
/**
159+
* Returns the width of the display.
160+
* @return the width of the display.
161+
*/
162+
public int getWidth() {
163+
return width;
164+
}
165+
166+
/**
167+
* Returns the height of the display.
168+
* @return the height of the display
169+
*/
170+
public int getHeight() {
171+
return height;
172+
}
173+
174+
/**
175+
* Returns the refresh rate of the display, in hertz.
176+
* @return the refresh rate of the display, in hertz
177+
*/
178+
public int getRate() {
179+
return rate;
180+
}
181+
182+
/**
183+
* Determines if this display belongs to the main monitor.
184+
* @return {@code true} if the display is primary, {@code false} otherwise.
185+
*/
186+
public boolean isPrimary() {
187+
return primary;
188+
}
189+
190+
/**
191+
* Returns the display name.
192+
* @return display name
193+
*/
194+
public String getName() {
195+
return name;
196+
}
197+
198+
/**
199+
* {@inheritDoc }
200+
*/
201+
@Override
202+
public int hashCode() {
203+
int hash = 3;
204+
hash = 97 * hash + (int) (this.display ^ (this.display >>> 32));
205+
hash = 97 * hash + this.width;
206+
hash = 97 * hash + this.height;
207+
hash = 97 * hash + this.rate;
208+
hash = 97 * hash + (this.primary ? 1 : 0);
209+
hash = 97 * hash + Objects.hashCode(this.name);
210+
return hash;
211+
}
212+
213+
/**
214+
* {@inheritDoc }
215+
*/
216+
@Override
217+
public boolean equals(Object obj) {
218+
if (this == obj) {
219+
return true;
220+
}
221+
if (obj == null) {
222+
return false;
223+
}
224+
if (getClass() != obj.getClass()) {
225+
return false;
226+
}
227+
final DisplayInfo other = (DisplayInfo) obj;
228+
if (this.display != other.display) {
229+
return false;
230+
}
231+
if (this.width != other.width) {
232+
return false;
233+
}
234+
if (this.height != other.height) {
235+
return false;
236+
}
237+
if (this.rate != other.rate) {
238+
return false;
239+
}
240+
if (this.primary != other.primary) {
241+
return false;
242+
}
243+
return Objects.equals(this.name, other.name);
244+
}
245+
246+
/**
247+
* {@inheritDoc }
63248
*/
64-
public String name = "Generic Monitor";
249+
@Override
250+
public String toString() {
251+
return getDisplay() == 0L ? "NULL" : ("(" + getName() + "|"
252+
+ getDisplay() + ")" + getWidth() + "x" + getHeight() + "@"
253+
+ (getRate() > 0 ? getRate() + "Hz" : "[Unknown refresh rate]"));
254+
}
65255
}

jme3-core/src/main/java/com/jme3/system/Displays.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,26 @@
3535
*/
3636
public class Displays {
3737

38-
private ArrayList<DisplayInfo> displays = new ArrayList<DisplayInfo>();
38+
/** List of monitors. */
39+
private ArrayList<DisplayInfo> displays = new ArrayList<>();
3940

41+
/**
42+
* Add a new monitor to the list
43+
*
44+
* @param displaysID the (native) pointer of the new monitor
45+
* @return the position of the monitor (represents the index)
46+
*/
4047
public int addNewMonitor(long displaysID) {
4148
DisplayInfo info = new DisplayInfo();
42-
info.displayID = displaysID;
49+
info.setDisplay(displaysID);
4350
displays.add(info);
4451
return displays.size() - 1;
4552
}
4653

4754
/**
4855
* This function returns the size of the display ArrayList
4956
*
50-
* @return the
57+
* @return the number of monitors available.
5158
*/
5259
public int size() {
5360
return displays.size();
@@ -78,10 +85,10 @@ public void setInfo(int displayPos, String name, int width, int height, int rate
7885
if (displayPos < displays.size()) {
7986
DisplayInfo info = displays.get(displayPos);
8087
if (info != null) {
81-
info.width = width;
82-
info.height = height;
83-
info.rate = rate;
84-
info.name = name;
88+
info.setWidth(width);
89+
info.setHeight(height);
90+
info.setRate(rate);
91+
info.setName(name);
8592
}
8693
}
8794
}
@@ -94,7 +101,7 @@ public void setInfo(int displayPos, String name, int width, int height, int rate
94101
public void setPrimaryDisplay(int displayPos) {
95102
if (displayPos < displays.size()) {
96103
DisplayInfo info = displays.get(displayPos);
97-
if (info != null) info.primary = true;
104+
if (info != null) info.setPrimary(true);
98105
}
99106
}
100107
}

jme3-examples/src/main/java/jme3test/app/TestMonitorApp.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ public void simpleInitApp() {
140140
String label =
141141
"Selected Monitor " +
142142
"Name: " +
143-
monitors.get(settings.getDisplay()).name +
143+
monitors.get(settings.getDisplay()).getName() +
144144
" " +
145145
monitorSelected +
146146
" Res: " +
147-
monitors.get(settings.getDisplay()).width +
147+
monitors.get(settings.getDisplay()).getWidth() +
148148
"," +
149-
monitors.get(settings.getDisplay()).height +
149+
monitors.get(settings.getDisplay()).getHeight() +
150150
" refresh: " +
151-
monitors.get(settings.getDisplay()).rate;
151+
monitors.get(settings.getDisplay()).getRate();
152152
selectedMonitorTxt.setText(label);
153153
selectedMonitorTxt.setLocalTranslation(0, settings.getHeight() - 80, 0);
154154
guiNode.attachChild(selectedMonitorTxt);
@@ -160,13 +160,13 @@ public void simpleInitApp() {
160160
"Mon : " +
161161
i +
162162
" " +
163-
monitor.name +
163+
monitor.getName() +
164164
" " +
165-
monitor.width +
165+
monitor.getWidth() +
166166
"," +
167-
monitor.height +
167+
monitor.getHeight() +
168168
" refresh: " +
169-
monitor.rate;
169+
monitor.getRate();
170170
txt = new BitmapText(loadGuiFont());
171171
txt.setText(labelValue);
172172
txt.setLocalTranslation(0, settings.getHeight() - 160 - (40 * i), 0);
@@ -222,13 +222,13 @@ public void saveSettings() {
222222
"Selected Monitor " +
223223
monitorSelected +
224224
" " +
225-
monitors.get(monitorSelected).name +
225+
monitors.get(monitorSelected).getName() +
226226
" Res: " +
227-
monitors.get(monitorSelected).width +
227+
monitors.get(monitorSelected).getWidth() +
228228
"," +
229-
monitors.get(monitorSelected).height +
229+
monitors.get(monitorSelected).getHeight() +
230230
"refresh: " +
231-
monitors.get(monitorSelected).rate;
231+
monitors.get(monitorSelected).getRate();
232232
selectedMonitorTxt.setText(label);
233233
if (!settings.isFullscreen()) fullScreenTxt.setText(
234234
"(f) Window Screen"

0 commit comments

Comments
 (0)