113 lines
4.2 KiB
Python
113 lines
4.2 KiB
Python
from os import environ
|
|
from sys import maxsize, platform
|
|
|
|
import pygame
|
|
from pygame import image, mouse
|
|
from pygame.locals import *
|
|
|
|
from .GameChild import *
|
|
|
|
class Display(GameChild):
|
|
|
|
def __init__(self, game):
|
|
GameChild.__init__(self, game)
|
|
print(f"available fullscreen modes: {pygame.display.list_modes()}")
|
|
self.delegate = self.get_delegate()
|
|
self.load_configuration()
|
|
self.align_window()
|
|
self.init_screen()
|
|
self.set_caption()
|
|
self.set_icon()
|
|
self.set_mouse_visibility()
|
|
self.subscribe(self.toggle_fullscreen)
|
|
|
|
def load_configuration(self):
|
|
config = self.get_configuration("display")
|
|
self.centered = config["centered"]
|
|
self.fullscreen_enabled = config["fullscreen"]
|
|
self.caption = config["caption"]
|
|
self.windowed_flag = config["windowed-flag"]
|
|
self.icon_path = self.get_resource("display", "icon-path")
|
|
self.mouse_visibility = self.get_configuration("mouse", "visible")
|
|
|
|
def align_window(self):
|
|
if self.centered:
|
|
environ["SDL_VIDEO_CENTERED"] = "1"
|
|
|
|
def init_screen(self):
|
|
full = False
|
|
if self.fullscreen_requested():
|
|
full = True
|
|
|
|
# Added pygame.SCALED to avoid fullscreen bug
|
|
# <https://www.pygame.org/docs/ref/display.html#pygame.display.toggle_fullscreen>
|
|
try:
|
|
self.set_screen(pygame.SCALED, fs=full)
|
|
except AttributeError:
|
|
self.set_screen(fs=full)
|
|
|
|
def fullscreen_requested(self):
|
|
return not self.check_command_line(self.windowed_flag) and self.fullscreen_enabled
|
|
|
|
def set_screen(self, flags=0x0, dimensions=None, fs=None):
|
|
"""
|
|
Initialize the pygame display, passing along any flags, and assign the returned display surface to `self.screen`. If no
|
|
dimensions are passed, use the dimensions of the current screen surface if it exists, otherwise get dimensions from the
|
|
configuration. If `fs` is `None`, leave the fullscreen state as is, otherwise read it as a boolean and set fullscreen
|
|
accordingly.
|
|
|
|
@param flags Flags from https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode. To pass multiple flags,
|
|
join them with the OR operator.
|
|
@param dimensions Dimensions of the screen surface as (width, height)
|
|
@param fs Set to True or False or omit to keep current fullscreen state
|
|
"""
|
|
# Try to auto discover dimensions if not provided
|
|
if dimensions is None:
|
|
if pygame.display.get_surface():
|
|
dimensions = pygame.display.get_surface().get_size()
|
|
else:
|
|
dimensions = self.get_configuration("display", "dimensions")
|
|
|
|
if fs is None:
|
|
# Get the current fullscreen state
|
|
fs = bool(pygame.display.get_surface().get_flags() & pygame.FULLSCREEN)
|
|
|
|
# Get a display surface with specified fullscreen state
|
|
if fs:
|
|
self.screen = pygame.display.set_mode(dimensions, flags | pygame.FULLSCREEN)
|
|
else:
|
|
self.screen = pygame.display.set_mode(dimensions, flags)
|
|
|
|
# Redraw the spline interpolator interface if enabled
|
|
if self.get_game().interpolator.gui_enabled:
|
|
self.get_game().interpolator.gui.rearrange()
|
|
|
|
def rotate(self):
|
|
"""
|
|
Rotate the screen surface 90 degrees by resetting it with swapped W and H dimensions.
|
|
"""
|
|
current_width, current_height = self.get_display_surface().get_size()
|
|
self.set_screen(dimensions=(current_height, current_width))
|
|
|
|
def set_caption(self):
|
|
pygame.display.set_caption(self.caption)
|
|
|
|
def set_icon(self):
|
|
if self.icon_path:
|
|
pygame.display.set_icon(image.load(self.icon_path).convert_alpha())
|
|
|
|
def set_mouse_visibility(self, visibility=None):
|
|
if visibility is None:
|
|
visibility = self.mouse_visibility
|
|
return mouse.set_visible(visibility)
|
|
|
|
def get_screen(self):
|
|
return self.screen
|
|
|
|
def get_size(self):
|
|
return self.screen.get_size()
|
|
|
|
def toggle_fullscreen(self, event):
|
|
if self.delegate.compare(event, "toggle-fullscreen"):
|
|
pygame.display.toggle_fullscreen()
|