ScreenManager.java

com.mojang.blaze3d.platform.ScreenManager

信息

  • 全限定名:com.mojang.blaze3d.platform.ScreenManager
  • 类型:public class
  • 包:com.mojang.blaze3d.platform
  • 源码路径:src/main/java/com/mojang/blaze3d/platform/ScreenManager.java
  • 起始行号:L16
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L17
    • 说明:

      TODO

  • monitors

    • 类型: Long2ObjectMap<Monitor>
    • 修饰符: private final
    • 源码定位: L18
    • 说明:

      TODO

  • monitorCreator

    • 类型: MonitorCreator
    • 修饰符: private final
    • 源码定位: L19
    • 说明:

      TODO

内部类/嵌套类型

构造器

public ScreenManager(MonitorCreator monitorCreator) @ L21

  • 构造器名:ScreenManager
  • 源码定位:L21
  • 修饰符:public

参数:

  • monitorCreator: MonitorCreator

说明:

TODO

方法

下面的方法块按源码顺序生成。

private void onMonitorChange(long monitor, int event) @ L33

  • 方法名:onMonitorChange
  • 源码定位:L33
  • 返回类型:void
  • 修饰符:private

参数:

  • monitor: long
  • event: int

说明:

TODO

public Monitor getMonitor(long monitor) @ L44

  • 方法名:getMonitor
  • 源码定位:L44
  • 返回类型:Monitor
  • 修饰符:public

参数:

  • monitor: long

说明:

TODO

public Monitor findBestMonitor(Window window) @ L48

  • 方法名:findBestMonitor
  • 源码定位:L48
  • 返回类型:Monitor
  • 修饰符:public

参数:

  • window: Window

说明:

TODO

public static int clamp(int value, int min, int max) @ L88

  • 方法名:clamp
  • 源码定位:L88
  • 返回类型:int
  • 修饰符:public static

参数:

  • value: int
  • min: int
  • max: int

说明:

TODO

public void shutdown() @ L96

  • 方法名:shutdown
  • 源码定位:L96
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class ScreenManager {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final Long2ObjectMap<Monitor> monitors = new Long2ObjectOpenHashMap<>();
    private final MonitorCreator monitorCreator;
 
    public ScreenManager(MonitorCreator monitorCreator) {
        this.monitorCreator = monitorCreator;
        GLFW.glfwSetMonitorCallback(this::onMonitorChange);
        PointerBuffer buffer = GLFW.glfwGetMonitors();
        if (buffer != null) {
            for (int i = 0; i < buffer.limit(); i++) {
                long monitor = buffer.get(i);
                this.monitors.put(monitor, monitorCreator.createMonitor(monitor));
            }
        }
    }
 
    private void onMonitorChange(long monitor, int event) {
        RenderSystem.assertOnRenderThread();
        if (event == 262145) {
            this.monitors.put(monitor, this.monitorCreator.createMonitor(monitor));
            LOGGER.debug("Monitor {} connected. Current monitors: {}", monitor, this.monitors);
        } else if (event == 262146) {
            this.monitors.remove(monitor);
            LOGGER.debug("Monitor {} disconnected. Current monitors: {}", monitor, this.monitors);
        }
    }
 
    public @Nullable Monitor getMonitor(long monitor) {
        return this.monitors.get(monitor);
    }
 
    public @Nullable Monitor findBestMonitor(Window window) {
        long windowMonitor = GLFW.glfwGetWindowMonitor(window.handle());
        if (windowMonitor != 0L) {
            return this.getMonitor(windowMonitor);
        } else {
            int winMinX = window.getX();
            int winMaxX = winMinX + window.getScreenWidth();
            int winMinY = window.getY();
            int winMaxY = winMinY + window.getScreenHeight();
            int maxArea = -1;
            Monitor result = null;
            long primaryMonitor = GLFW.glfwGetPrimaryMonitor();
            LOGGER.debug("Selecting monitor - primary: {}, current monitors: {}", primaryMonitor, this.monitors);
 
            for (Monitor monitor : this.monitors.values()) {
                int monMinX = monitor.getX();
                int monMaxX = monMinX + monitor.getCurrentMode().getWidth();
                int monMinY = monitor.getY();
                int monMaxY = monMinY + monitor.getCurrentMode().getHeight();
                int minX = clamp(winMinX, monMinX, monMaxX);
                int maxX = clamp(winMaxX, monMinX, monMaxX);
                int minY = clamp(winMinY, monMinY, monMaxY);
                int maxY = clamp(winMaxY, monMinY, monMaxY);
                int sx = Math.max(0, maxX - minX);
                int sy = Math.max(0, maxY - minY);
                int area = sx * sy;
                if (area > maxArea) {
                    result = monitor;
                    maxArea = area;
                } else if (area == maxArea && primaryMonitor == monitor.getMonitor()) {
                    LOGGER.debug("Primary monitor {} is preferred to monitor {}", monitor, result);
                    result = monitor;
                }
            }
 
            LOGGER.debug("Selected monitor: {}", result);
            return result;
        }
    }
 
    public static int clamp(int value, int min, int max) {
        if (value < min) {
            return min;
        } else {
            return value > max ? max : value;
        }
    }
 
    public void shutdown() {
        RenderSystem.assertOnRenderThread();
        GLFWMonitorCallback callback = GLFW.glfwSetMonitorCallback(null);
        if (callback != null) {
            callback.free();
        }
    }
}

引用的其他类

  • Monitor

    • 引用位置: 字段/返回值
  • MonitorCreator

    • 引用位置: 参数/字段
  • Window

    • 引用位置: 参数
  • RenderSystem

    • 引用位置: 方法调用
    • 关联成员: RenderSystem.assertOnRenderThread()