Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Fix ] Minor refactoring to DisplayInfo.class #2345 #2349

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 221 additions & 31 deletions jme3-core/src/main/java/com/jme3/system/DisplayInfo.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,255 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine All rights reserved.
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided with
* the distribution.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.system;

import java.util.Objects;

/**
* This class holds information about the display that was returned by glfwGetMonitors() calls in
* the context class
*
* @author Kevin Bales
* @author wil
*/
public class DisplayInfo {
public final class DisplayInfo {

/** display - display id that was return from Lwjgl3. */
private long display;

/** width - width that was return from Lwjgl3. */
private int width;

/** height - height that was return from Lwjgl3. */
private int height;

/** rate - refresh rate that was return from Lwjgl3. */
private int rate;

/** primary - indicates if the display is the primary monitor. */
private boolean primary;

/**
* displayID - display id that was return from Lwjgl3.
* name - display name that was return from Lwjgl3.
*/
public long displayID = 0;
private String name;

/**
* width - width that was return from Lwjgl3.
* Create a new display mode object with the default values
*/
public int width = 1080;
DisplayInfo() {
this(0L /*NULL*/, 1080, 1920, 60, false, "Generic Monitor");
}

/**
* Create a new display mode object with the supplied parameters.
* @param display the monitor pointer (native), the virtual memory
* address used by lwjgl.
* @param width the width of the display, provided by lwjgl.
* @param height the height of the display, provided by lwjgl.
* @param rate the refresh rate of the display, in hertz.
* @param primary a logical value that determines whether this display is
* primary or not; {@code true | false}
* @param name display name
*/
DisplayInfo(long display, int width, int height, int rate, boolean primary, String name) {
this.display = display;
this.width = width;
this.height = height;
this.rate = rate;
this.primary = primary;
this.name = name;
}

// === ----------------------------------------------------------------- ===
// === SETTERS ===
// === ----------------------------------------------------------------- ===

/**
* height - height that was return from Lwjgl3.
* Sets the monitor pointer (native), the virtual memory address used by lwjgl.
*
* @param display the monitor pointer (native), the virtual memory
* address used by lwjgl
*/
public int height = 1920;
void setDisplay(long display) {
this.display = display;
}

/**
* rate - refresh rate that was return from Lwjgl3.
* Sets the width of the display.
* @param width the width of the display
*/
public int rate = 60;
void setWidth(int width) {
this.width = width;
}

/**
* primary - indicates if the display is the primary monitor.
* Sets the height of the display.
* @param height the height of the display
*/
public boolean primary = false;
void setHeight(int height) {
this.height = height;
}

/**
* name - display name that was return from Lwjgl3.
* Sets the refresh rate of the display, in hertz.
* @param rate the refresh rate of the display, in hertz
*/
void setRate(int rate) {
this.rate = rate;
}

/**
* Set this display as primary or not.
* @param primary {@code true} if the display is primary, {@code false} otherwise.
*/
void setPrimary(boolean primary) {
this.primary = primary;
}

/**
* Set the screen (display) name
* @param name display name
*/
void setName(String name) {
this.name = name;
}

// === ----------------------------------------------------------------- ===
// === GETTERS ===
// === ----------------------------------------------------------------- ===

/**
* Returns the monitor pointer (native), the virtual memory address used by lwjgl.
* @return the monitor pointer (native), the virtual memory address used by lwjgl
*/
public long getDisplay() {
return display;
}

/**
* Returns the width of the display.
* @return the width of the display.
*/
public int getWidth() {
return width;
}

/**
* Returns the height of the display.
* @return the height of the display
*/
public int getHeight() {
return height;
}

/**
* Returns the refresh rate of the display, in hertz.
* @return the refresh rate of the display, in hertz
*/
public int getRate() {
return rate;
}

/**
* Determines if this display belongs to the main monitor.
* @return {@code true} if the display is primary, {@code false} otherwise.
*/
public boolean isPrimary() {
return primary;
}

/**
* Returns the display name.
* @return display name
*/
public String getName() {
return name;
}

/**
* {@inheritDoc }
*/
@Override
public int hashCode() {
int hash = 3;
hash = 97 * hash + (int) (this.display ^ (this.display >>> 32));
hash = 97 * hash + this.width;
hash = 97 * hash + this.height;
hash = 97 * hash + this.rate;
hash = 97 * hash + (this.primary ? 1 : 0);
hash = 97 * hash + Objects.hashCode(this.name);
return hash;
}

/**
* {@inheritDoc }
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final DisplayInfo other = (DisplayInfo) obj;
if (this.display != other.display) {
return false;
}
if (this.width != other.width) {
return false;
}
if (this.height != other.height) {
return false;
}
if (this.rate != other.rate) {
return false;
}
if (this.primary != other.primary) {
return false;
}
return Objects.equals(this.name, other.name);
}

/**
* {@inheritDoc }
*/
public String name = "Generic Monitor";
@Override
public String toString() {
return getDisplay() == 0L ? "NULL" : ("(" + getName() + "|"
+ getDisplay() + ")" + getWidth() + "x" + getHeight() + "@"
+ (getRate() > 0 ? getRate() + "Hz" : "[Unknown refresh rate]"));
}
}
23 changes: 15 additions & 8 deletions jme3-core/src/main/java/com/jme3/system/Displays.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@
*/
public class Displays {

private ArrayList<DisplayInfo> displays = new ArrayList<DisplayInfo>();
/** List of monitors. */
private ArrayList<DisplayInfo> displays = new ArrayList<>();

/**
* Add a new monitor to the list
*
* @param displaysID the (native) pointer of the new monitor
* @return the position of the monitor (represents the index)
*/
public int addNewMonitor(long displaysID) {
DisplayInfo info = new DisplayInfo();
info.displayID = displaysID;
info.setDisplay(displaysID);
displays.add(info);
return displays.size() - 1;
}

/**
* This function returns the size of the display ArrayList
*
* @return the
* @return the number of monitors available.
*/
public int size() {
return displays.size();
Expand Down Expand Up @@ -78,10 +85,10 @@ public void setInfo(int displayPos, String name, int width, int height, int rate
if (displayPos < displays.size()) {
DisplayInfo info = displays.get(displayPos);
if (info != null) {
info.width = width;
info.height = height;
info.rate = rate;
info.name = name;
info.setWidth(width);
info.setHeight(height);
info.setRate(rate);
info.setName(name);
}
}
}
Expand All @@ -94,7 +101,7 @@ public void setInfo(int displayPos, String name, int width, int height, int rate
public void setPrimaryDisplay(int displayPos) {
if (displayPos < displays.size()) {
DisplayInfo info = displays.get(displayPos);
if (info != null) info.primary = true;
if (info != null) info.setPrimary(true);
}
}
}
24 changes: 12 additions & 12 deletions jme3-examples/src/main/java/jme3test/app/TestMonitorApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public void simpleInitApp() {
String label =
"Selected Monitor " +
"Name: " +
monitors.get(settings.getDisplay()).name +
monitors.get(settings.getDisplay()).getName() +
" " +
monitorSelected +
" Res: " +
monitors.get(settings.getDisplay()).width +
monitors.get(settings.getDisplay()).getWidth() +
"," +
monitors.get(settings.getDisplay()).height +
monitors.get(settings.getDisplay()).getHeight() +
" refresh: " +
monitors.get(settings.getDisplay()).rate;
monitors.get(settings.getDisplay()).getRate();
selectedMonitorTxt.setText(label);
selectedMonitorTxt.setLocalTranslation(0, settings.getHeight() - 80, 0);
guiNode.attachChild(selectedMonitorTxt);
Expand All @@ -160,13 +160,13 @@ public void simpleInitApp() {
"Mon : " +
i +
" " +
monitor.name +
monitor.getName() +
" " +
monitor.width +
monitor.getWidth() +
"," +
monitor.height +
monitor.getHeight() +
" refresh: " +
monitor.rate;
monitor.getRate();
txt = new BitmapText(loadGuiFont());
txt.setText(labelValue);
txt.setLocalTranslation(0, settings.getHeight() - 160 - (40 * i), 0);
Expand Down Expand Up @@ -222,13 +222,13 @@ public void saveSettings() {
"Selected Monitor " +
monitorSelected +
" " +
monitors.get(monitorSelected).name +
monitors.get(monitorSelected).getName() +
" Res: " +
monitors.get(monitorSelected).width +
monitors.get(monitorSelected).getWidth() +
"," +
monitors.get(monitorSelected).height +
monitors.get(monitorSelected).getHeight() +
"refresh: " +
monitors.get(monitorSelected).rate;
monitors.get(monitorSelected).getRate();
selectedMonitorTxt.setText(label);
if (!settings.isFullscreen()) fullScreenTxt.setText(
"(f) Window Screen"
Expand Down
Loading
Loading