2021-09-08 23:38:34 +08:00
|
|
|
# -------------------------------
|
|
|
|
# Difficult Rocket
|
2023-01-20 14:08:12 +08:00
|
|
|
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
2021-09-08 23:38:34 +08:00
|
|
|
# All rights reserved
|
|
|
|
# -------------------------------
|
|
|
|
|
2021-02-20 21:46:14 +08:00
|
|
|
import os
|
2023-05-02 15:31:28 +08:00
|
|
|
import sys
|
2021-04-10 22:51:08 +08:00
|
|
|
import time
|
2023-04-22 20:52:10 +08:00
|
|
|
import inspect
|
2022-12-25 23:15:49 +08:00
|
|
|
import functools
|
2021-10-25 22:08:00 +08:00
|
|
|
import traceback
|
2021-09-22 06:21:48 +08:00
|
|
|
|
2023-06-11 18:19:14 +08:00
|
|
|
from pathlib import Path
|
2021-09-24 00:04:56 +08:00
|
|
|
from decimal import Decimal
|
2023-07-31 16:42:13 +08:00
|
|
|
from typing import Callable, Dict, List, TYPE_CHECKING, Type
|
2021-08-24 22:31:52 +08:00
|
|
|
|
2022-12-11 10:39:05 +08:00
|
|
|
# third function
|
2024-01-07 20:14:07 +08:00
|
|
|
import tomli
|
|
|
|
import tomli_w
|
2022-12-11 10:39:05 +08:00
|
|
|
import pyglet
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2022-12-11 10:39:05 +08:00
|
|
|
# from pyglet import gl
|
2023-01-23 13:54:05 +08:00
|
|
|
# from pyglet.gl import glClearColor
|
2022-12-11 10:39:05 +08:00
|
|
|
# from pyglet.libs.win32 import _user32
|
2023-06-22 01:44:53 +08:00
|
|
|
from pyglet.graphics import Group, Batch
|
2022-12-11 10:39:05 +08:00
|
|
|
from pyglet.window import Window
|
|
|
|
from pyglet.window import key, mouse
|
2023-06-30 22:59:40 +08:00
|
|
|
from pyglet.gui.widgets import TextEntry
|
2022-12-11 10:39:05 +08:00
|
|
|
|
2021-09-23 06:34:23 +08:00
|
|
|
# Difficult_Rocket function
|
2023-05-02 15:31:28 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from Difficult_Rocket.main import Game
|
2023-06-17 01:28:10 +08:00
|
|
|
from Difficult_Rocket import DR_status
|
2023-02-18 14:04:55 +08:00
|
|
|
from Difficult_Rocket.utils import tools
|
2023-05-24 00:43:11 +08:00
|
|
|
from Difficult_Rocket.command import line
|
2023-06-17 00:51:05 +08:00
|
|
|
from Difficult_Rocket.api.types import Options
|
2022-04-26 22:05:58 +08:00
|
|
|
from Difficult_Rocket.utils.translate import tr
|
2023-06-17 00:51:05 +08:00
|
|
|
from Difficult_Rocket.runtime import DR_runtime
|
2023-04-22 10:39:18 +08:00
|
|
|
from Difficult_Rocket.api.screen import BaseScreen
|
2023-05-12 21:06:56 +08:00
|
|
|
from Difficult_Rocket.utils.thread import new_thread
|
2023-06-22 01:44:53 +08:00
|
|
|
from Difficult_Rocket.client.screen import DRDEBUGScreen
|
2022-03-22 21:18:04 +08:00
|
|
|
from Difficult_Rocket.client.fps.fps_log import FpsLogger
|
2023-04-22 10:39:18 +08:00
|
|
|
from Difficult_Rocket.exception.language import LanguageNotFound
|
2021-02-03 22:04:34 +08:00
|
|
|
|
2023-12-13 12:04:30 +08:00
|
|
|
from lib_not_dr import loggers
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2023-12-13 12:04:30 +08:00
|
|
|
logger = loggers.config.get_logger("client")
|
2023-06-17 18:31:06 +08:00
|
|
|
|
|
|
|
|
2023-01-23 13:54:05 +08:00
|
|
|
class ClientOption(Options):
|
|
|
|
fps: int = 60
|
|
|
|
width: int = 1024
|
|
|
|
height: int = 768
|
|
|
|
file_drop: bool = True
|
|
|
|
fullscreen: bool = False
|
|
|
|
resizeable: bool = True
|
|
|
|
visible: bool = True
|
|
|
|
gui_scale: float = 1.0
|
2023-12-15 12:45:46 +08:00
|
|
|
vsync: bool = False
|
|
|
|
background_color: List[float] = [21 / 255, 22 / 255, 23 / 255, 0.0]
|
2023-06-17 00:51:05 +08:00
|
|
|
caption: str = "Difficult Rocket v{DR_version}"
|
2023-01-23 13:54:05 +08:00
|
|
|
|
|
|
|
def load_file(self) -> None:
|
2023-12-16 22:24:44 +08:00
|
|
|
file = DR_runtime.main_config
|
2023-12-03 16:54:07 +08:00
|
|
|
self.fps = int(file["runtime"]["fps"])
|
|
|
|
self.width = int(file["window"]["width"])
|
|
|
|
self.height = int(file["window"]["height"])
|
|
|
|
self.fullscreen = tools.format_bool(file["window"]["full_screen"])
|
|
|
|
self.resizeable = tools.format_bool(file["window"]["resizable"])
|
|
|
|
self.gui_scale = float(file["window"]["gui_scale"])
|
|
|
|
self.caption = DR_status.format(file["window"]["caption"])
|
2023-12-15 12:45:46 +08:00
|
|
|
self.vsync = tools.format_bool(file["window"]["vsync"])
|
|
|
|
color = (i / 255 for i in file["window"]["background_color"][0:3])
|
|
|
|
alpha = file["window"]["background_color"][3]
|
|
|
|
self.background_color = [*color, alpha]
|
2023-06-17 01:28:10 +08:00
|
|
|
self.caption = DR_runtime.format(self.caption)
|
2023-01-23 13:54:05 +08:00
|
|
|
|
|
|
|
|
2021-08-13 12:25:29 +08:00
|
|
|
class Client:
|
2023-07-31 16:42:13 +08:00
|
|
|
"""
|
|
|
|
客户端
|
|
|
|
"""
|
2023-12-03 16:54:07 +08:00
|
|
|
|
|
|
|
def __init__(self, game: "Game", net_mode="local"):
|
2021-10-01 23:12:01 +08:00
|
|
|
start_time = time.time_ns()
|
2023-12-24 16:09:54 +08:00
|
|
|
# log
|
2023-12-14 11:16:40 +08:00
|
|
|
self.logger = loggers.get_logger("client").set_tag("client")
|
2023-04-05 11:53:40 +08:00
|
|
|
self.logger.info(tr().client.setup.start())
|
2021-08-13 12:25:29 +08:00
|
|
|
# config
|
2023-02-09 14:47:39 +08:00
|
|
|
self.config = ClientOption()
|
2021-02-16 22:28:13 +08:00
|
|
|
# value
|
2023-12-03 16:54:07 +08:00
|
|
|
self.process_id = "Client"
|
|
|
|
self.process_name = "Client process"
|
2021-02-20 21:46:14 +08:00
|
|
|
self.process_pid = os.getpid()
|
2021-02-16 22:28:13 +08:00
|
|
|
self.net_mode = net_mode
|
2023-04-26 11:18:11 +08:00
|
|
|
self.game = game
|
2023-12-03 16:54:07 +08:00
|
|
|
self.window = ClientWindow(
|
|
|
|
game=game,
|
|
|
|
net_mode=self.net_mode,
|
|
|
|
width=self.config.width,
|
|
|
|
height=self.config.height,
|
|
|
|
fullscreen=self.config.fullscreen,
|
|
|
|
caption=self.config.caption,
|
|
|
|
resizable=self.config.resizeable,
|
|
|
|
visible=self.config.visible,
|
|
|
|
file_drops=True,
|
2023-12-15 12:45:46 +08:00
|
|
|
vsync=False,
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
2023-12-15 12:45:46 +08:00
|
|
|
pyglet.gl.glClearColor(*self.config.background_color)
|
2021-10-01 23:12:01 +08:00
|
|
|
end_time = time.time_ns()
|
|
|
|
self.use_time = end_time - start_time
|
2023-12-03 16:54:07 +08:00
|
|
|
self.logger.info(
|
|
|
|
tr().client.setup.use_time().format(Decimal(self.use_time) / 1000000000)
|
|
|
|
)
|
2023-02-03 20:39:40 +08:00
|
|
|
self.logger.debug(tr().client.setup.use_time_ns().format(self.use_time))
|
2021-02-20 21:46:14 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def start(self):
|
2023-07-31 16:42:13 +08:00
|
|
|
"""
|
|
|
|
启动客户端
|
|
|
|
"""
|
2023-01-03 16:24:06 +08:00
|
|
|
DR_runtime.running = True
|
2021-09-22 06:21:48 +08:00
|
|
|
self.window.start_game() # 游戏启动
|
2021-08-24 22:31:52 +08:00
|
|
|
# TODO 写一下服务端启动相关,还是需要服务端啊
|
2021-01-11 21:58:51 +08:00
|
|
|
|
2023-06-08 00:57:03 +08:00
|
|
|
def __repr__(self):
|
2023-12-03 16:54:07 +08:00
|
|
|
return f"<Client {self.process_name} {self.process_pid}>"
|
2023-06-08 00:57:03 +08:00
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2022-05-11 11:11:39 +08:00
|
|
|
def pyglet_load_fonts_folder(folder) -> None:
|
2023-07-31 16:42:13 +08:00
|
|
|
"""
|
|
|
|
递归加载字体文件夹
|
|
|
|
:param folder:
|
|
|
|
:return:
|
|
|
|
"""
|
2023-06-11 18:19:14 +08:00
|
|
|
font_path = Path(folder)
|
|
|
|
if not font_path.exists():
|
|
|
|
font_path.mkdir(parents=True)
|
|
|
|
return None
|
2023-12-13 12:38:26 +08:00
|
|
|
logger.info(tr().client.load.font.start().format(font_path), tag="font")
|
2023-08-27 01:52:45 +08:00
|
|
|
start_time = time.time_ns()
|
|
|
|
for dir_path, dir_names, file_names in os.walk(font_path):
|
|
|
|
dir_path = Path(dir_path)
|
|
|
|
for file_name in file_names:
|
|
|
|
file_name = Path(file_name)
|
2023-12-03 16:54:07 +08:00
|
|
|
if file_name.suffix in (".ttf", ".otf"):
|
|
|
|
logger.debug(
|
2023-12-13 12:38:26 +08:00
|
|
|
tr().client.load.font.file().format(str(dir_path / file_name)),
|
|
|
|
tag="font",
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
2023-06-17 18:31:06 +08:00
|
|
|
try:
|
2023-08-27 01:52:45 +08:00
|
|
|
pyglet.font.add_file(str(dir_path / file_name))
|
2023-06-17 18:31:06 +08:00
|
|
|
except Exception:
|
2023-12-03 16:54:07 +08:00
|
|
|
logger.error(
|
|
|
|
tr()
|
|
|
|
.client.load.font.error()
|
|
|
|
.format(str(dir_path / file_name), traceback.format_exc())
|
|
|
|
)
|
2023-08-27 01:52:45 +08:00
|
|
|
end_time = time.time_ns()
|
|
|
|
use_time = end_time - start_time
|
2023-12-13 12:38:26 +08:00
|
|
|
logger.info(
|
|
|
|
tr().client.load.font.use_time().format(use_time / 1000000000), tag="font"
|
|
|
|
)
|
2022-05-11 11:11:39 +08:00
|
|
|
|
|
|
|
|
2023-06-18 15:42:35 +08:00
|
|
|
def _call_back(call_back: Callable) -> Callable:
|
2023-06-22 01:44:53 +08:00
|
|
|
"""
|
2023-08-27 01:52:45 +08:00
|
|
|
>>> def call_back_example():
|
2023-06-22 01:44:53 +08:00
|
|
|
>>> pass
|
2023-08-27 01:52:45 +08:00
|
|
|
>>> @_call_back(call_back_example)
|
2023-06-22 01:44:53 +08:00
|
|
|
>>> def on_draw(self):
|
|
|
|
>>> pass
|
|
|
|
用于在调用窗口函数后调用指定函数 的装饰器
|
|
|
|
:param call_back: 需要调用的函数
|
|
|
|
:return: 包装后的函数
|
|
|
|
"""
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2023-06-18 15:42:35 +08:00
|
|
|
def wrapper(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
def warp(self: "ClientWindow", *args, **kwargs):
|
|
|
|
result = func(self, *args, **kwargs)
|
2023-08-27 01:52:45 +08:00
|
|
|
# call_back(self)
|
2023-06-18 15:42:35 +08:00
|
|
|
return result
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2023-06-18 15:42:35 +08:00
|
|
|
return warp
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2023-06-18 15:42:35 +08:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
def _call_screen_after(func: Callable) -> Callable:
|
2023-06-22 01:44:53 +08:00
|
|
|
"""
|
|
|
|
>>> @_call_screen_after
|
|
|
|
>>> def on_draw(self):
|
|
|
|
>>> pass
|
|
|
|
用于在调用窗口函数后调用子窗口函数 的装饰器
|
|
|
|
:param func: 需要包装的函数
|
|
|
|
:return: 包装后的函数
|
|
|
|
"""
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@functools.wraps(func)
|
2023-04-19 00:44:41 +08:00
|
|
|
def warped(self: "ClientWindow", *args, **kwargs):
|
2022-12-25 23:15:49 +08:00
|
|
|
result = func(self, *args, **kwargs)
|
2023-04-26 11:18:11 +08:00
|
|
|
for title, a_screen in self.screen_list.items():
|
2023-04-19 00:44:41 +08:00
|
|
|
a_screen.window_pointer = self
|
|
|
|
# 提前帮子窗口设置好指针
|
2022-12-25 23:15:49 +08:00
|
|
|
if hasattr(a_screen, func.__name__):
|
2022-12-26 11:46:05 +08:00
|
|
|
try:
|
2023-04-19 00:42:31 +08:00
|
|
|
getattr(a_screen, func.__name__)(*args, **kwargs, window=self)
|
2023-01-27 21:09:37 +08:00
|
|
|
except Exception:
|
2022-12-26 11:46:05 +08:00
|
|
|
traceback.print_exc()
|
2022-12-25 23:15:49 +08:00
|
|
|
return result
|
|
|
|
|
2023-12-16 22:24:44 +08:00
|
|
|
warped.__signature__ = inspect.signature(func) # type: ignore
|
2022-12-25 23:15:49 +08:00
|
|
|
return warped
|
|
|
|
|
|
|
|
|
|
|
|
def _call_screen_before(func: Callable) -> Callable:
|
2023-06-22 01:44:53 +08:00
|
|
|
"""
|
|
|
|
>>> @_call_screen_before
|
|
|
|
>>> def on_draw(self):
|
|
|
|
>>> pass
|
|
|
|
用于在调用窗口函数前调用子窗口函数 的装饰器
|
|
|
|
:param func: 需要包装的函数
|
|
|
|
:return: 包装后的函数
|
|
|
|
"""
|
2023-12-03 16:54:07 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@functools.wraps(func)
|
2023-04-19 00:44:41 +08:00
|
|
|
def warped(self: "ClientWindow", *args, **kwargs):
|
2023-04-26 11:18:11 +08:00
|
|
|
for title, a_screen in self.screen_list.items():
|
2023-04-19 00:44:41 +08:00
|
|
|
a_screen.window_pointer = self
|
|
|
|
# 提前帮子窗口设置好指针
|
2022-12-25 23:15:49 +08:00
|
|
|
if hasattr(a_screen, func.__name__):
|
2022-12-26 11:46:05 +08:00
|
|
|
try:
|
2023-04-19 00:42:31 +08:00
|
|
|
getattr(a_screen, func.__name__)(*args, **kwargs, window=self)
|
2023-01-27 21:09:37 +08:00
|
|
|
except Exception:
|
2022-12-26 11:46:05 +08:00
|
|
|
traceback.print_exc()
|
2022-12-25 23:15:49 +08:00
|
|
|
result = func(self, *args, **kwargs)
|
|
|
|
return result
|
|
|
|
|
2023-12-16 22:24:44 +08:00
|
|
|
warped.__signature__ = inspect.signature(func) # type: ignore
|
2022-12-25 23:15:49 +08:00
|
|
|
return warped
|
2022-08-12 21:07:36 +08:00
|
|
|
|
|
|
|
|
2021-11-06 19:07:32 +08:00
|
|
|
class ClientWindow(Window):
|
2023-12-03 16:54:07 +08:00
|
|
|
def __init__(self, game: "Game", net_mode="local", *args, **kwargs):
|
2021-02-16 12:51:07 +08:00
|
|
|
"""
|
2022-07-16 20:20:23 +08:00
|
|
|
|
|
|
|
@param net_mode:
|
|
|
|
@param args:
|
|
|
|
@param kwargs:
|
2021-02-16 12:51:07 +08:00
|
|
|
"""
|
2022-07-16 20:20:23 +08:00
|
|
|
start_time = time.time_ns()
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-12-24 16:09:54 +08:00
|
|
|
# log
|
2023-12-14 11:16:40 +08:00
|
|
|
self.logger = loggers.get_logger("client").set_tag("window")
|
2023-04-05 11:53:40 +08:00
|
|
|
self.logger.info(tr().window.setup.start())
|
2021-02-03 22:04:34 +08:00
|
|
|
# value
|
2023-04-26 11:18:11 +08:00
|
|
|
self.game = game
|
2021-02-03 22:04:34 +08:00
|
|
|
self.net_mode = net_mode
|
2021-09-22 06:21:48 +08:00
|
|
|
self.run_input = False
|
2023-05-01 21:24:16 +08:00
|
|
|
self.command_list: List[str] = []
|
2021-09-02 22:47:10 +08:00
|
|
|
# FPS
|
2023-12-16 22:24:44 +08:00
|
|
|
self.FPS = Decimal(int(DR_runtime.main_config["runtime"]["fps"]))
|
2023-12-03 16:54:07 +08:00
|
|
|
self.SPF = Decimal("1") / self.FPS
|
2021-12-15 23:28:08 +08:00
|
|
|
self.fps_log = FpsLogger(stable_fps=int(self.FPS))
|
2021-02-20 21:46:14 +08:00
|
|
|
# batch
|
2023-07-02 15:54:50 +08:00
|
|
|
self.main_batch = Batch()
|
2023-06-22 01:44:53 +08:00
|
|
|
self.main_group = Group(0)
|
2021-08-24 22:31:52 +08:00
|
|
|
# frame
|
2021-10-04 20:41:41 +08:00
|
|
|
self.frame = pyglet.gui.Frame(self, order=20)
|
2021-10-01 23:12:01 +08:00
|
|
|
self.M_frame = pyglet.gui.MovableFrame(self, modifier=key.LCTRL)
|
2023-04-26 11:18:11 +08:00
|
|
|
self.screen_list: Dict[str, BaseScreen] = {}
|
2021-02-03 22:04:34 +08:00
|
|
|
# setup
|
|
|
|
self.setup()
|
2021-10-02 20:40:06 +08:00
|
|
|
# 命令显示
|
2023-12-03 16:54:07 +08:00
|
|
|
self.input_box = TextEntry(
|
|
|
|
x=50,
|
|
|
|
y=30,
|
|
|
|
width=300,
|
|
|
|
batch=self.main_batch,
|
|
|
|
text="",
|
|
|
|
group=Group(1000, parent=self.main_group),
|
|
|
|
) # 实例化
|
2022-12-29 10:13:20 +08:00
|
|
|
self.input_box.push_handlers(self)
|
2023-12-03 16:54:07 +08:00
|
|
|
self.input_box.set_handler("on_commit", self.on_input)
|
2023-08-20 21:42:38 +08:00
|
|
|
self.push_handlers(self.input_box)
|
2021-11-06 19:07:32 +08:00
|
|
|
self.input_box.enabled = True
|
2021-10-02 20:40:06 +08:00
|
|
|
# 设置刷新率
|
2023-05-02 15:31:28 +08:00
|
|
|
# pyglet.clock.schedule_interval(self.draw_update, float(self.SPF))
|
2021-10-02 20:40:06 +08:00
|
|
|
# 完成设置后的信息输出
|
2023-02-03 20:31:44 +08:00
|
|
|
self.logger.info(tr().window.os.pid_is().format(os.getpid(), os.getppid()))
|
2021-10-01 23:12:01 +08:00
|
|
|
end_time = time.time_ns()
|
|
|
|
self.use_time = end_time - start_time
|
2022-11-26 21:48:55 +08:00
|
|
|
DR_runtime.client_setup_cause_ns = self.use_time
|
2023-08-27 01:52:45 +08:00
|
|
|
self.logger.info(tr().window.setup.use_time().format(self.use_time / 1000000000))
|
2023-02-03 20:31:44 +08:00
|
|
|
self.logger.debug(tr().window.setup.use_time_ns().format(self.use_time))
|
2021-09-02 22:47:10 +08:00
|
|
|
|
2020-12-16 06:33:33 +08:00
|
|
|
def setup(self):
|
2023-12-03 16:54:07 +08:00
|
|
|
self.set_icon(pyglet.image.load("assets/textures/icon.png"))
|
2021-11-06 19:07:32 +08:00
|
|
|
self.load_fonts()
|
2023-12-03 16:54:07 +08:00
|
|
|
self.screen_list["DR_debug"] = DRDEBUGScreen(self)
|
|
|
|
self.game.dispatch_mod_event("on_client_start", game=self.game, client=self)
|
2021-10-01 23:12:01 +08:00
|
|
|
|
2022-09-03 22:35:57 +08:00
|
|
|
def load_fonts(self) -> None:
|
2023-12-16 22:24:44 +08:00
|
|
|
fonts_folder_path = DR_runtime.main_config["runtime"]["fonts_folder"]
|
2021-11-06 19:07:32 +08:00
|
|
|
# 加载字体路径
|
2022-05-11 11:11:39 +08:00
|
|
|
# 淦,还写了个递归来处理
|
|
|
|
pyglet_load_fonts_folder(fonts_folder_path)
|
2021-10-01 23:12:01 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
def start_game(self) -> None:
|
2023-12-03 16:54:07 +08:00
|
|
|
self.set_icon(pyglet.image.load("assets/textures/icon.png"))
|
2023-05-02 15:31:28 +08:00
|
|
|
try:
|
2023-12-15 12:45:46 +08:00
|
|
|
pyglet.clock.schedule_interval(self.draw_call, float(self.SPF))
|
|
|
|
pyglet.app.run(None)
|
2023-05-02 15:31:28 +08:00
|
|
|
except KeyboardInterrupt:
|
2023-12-13 12:38:26 +08:00
|
|
|
self.logger.warn(
|
|
|
|
"==========client stop. KeyboardInterrupt info==========", tag="starter"
|
|
|
|
)
|
2023-05-02 15:31:28 +08:00
|
|
|
traceback.print_exc()
|
2023-12-13 12:04:30 +08:00
|
|
|
self.logger.warn(
|
2023-12-13 12:38:26 +08:00
|
|
|
"==========client stop. KeyboardInterrupt info end==========",
|
|
|
|
tag="starter",
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
|
|
|
self.dispatch_event("on_close", "input")
|
2023-05-02 15:31:28 +08:00
|
|
|
sys.exit(0)
|
2021-09-22 06:21:48 +08:00
|
|
|
|
2023-12-03 16:54:07 +08:00
|
|
|
@new_thread("window save_info")
|
2021-10-25 22:08:00 +08:00
|
|
|
def save_info(self):
|
2023-04-06 15:31:21 +08:00
|
|
|
self.logger.info(tr().client.config.save.start())
|
2023-12-16 22:24:44 +08:00
|
|
|
config_file = DR_runtime.main_config
|
2023-12-03 16:54:07 +08:00
|
|
|
config_file["window"]["width"] = self.width
|
|
|
|
config_file["window"]["height"] = self.height
|
|
|
|
config_file["runtime"]["language"] = DR_runtime.language
|
2024-01-07 20:14:07 +08:00
|
|
|
tomli_w.dump(config_file, open("./config/main.toml", "wb"))
|
2023-04-06 15:31:21 +08:00
|
|
|
self.logger.info(tr().client.config.save.done())
|
2021-10-25 22:08:00 +08:00
|
|
|
|
2023-04-22 10:39:18 +08:00
|
|
|
"""
|
|
|
|
client api
|
|
|
|
"""
|
|
|
|
|
2023-06-27 01:06:09 +08:00
|
|
|
def add_sub_screen(self, title: str, sub_screen: Type[BaseScreen]):
|
2023-04-26 11:18:11 +08:00
|
|
|
self.screen_list[title] = sub_screen(self)
|
2023-04-22 10:39:18 +08:00
|
|
|
|
2024-05-26 13:16:52 +08:00
|
|
|
def remove_sub_screen(self, title: str) -> BaseScreen:
|
|
|
|
"""
|
|
|
|
去除一个页面, 返回被去掉的页面
|
|
|
|
"""
|
|
|
|
return self.screen_list.pop(title)
|
2023-06-27 01:06:09 +08:00
|
|
|
|
2021-09-22 06:21:48 +08:00
|
|
|
"""
|
|
|
|
draws and some event
|
|
|
|
"""
|
2021-01-29 14:07:40 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2022-06-18 17:53:35 +08:00
|
|
|
def draw_update(self, tick: float):
|
2021-10-25 22:08:00 +08:00
|
|
|
decimal_tick = Decimal(str(tick)[:10])
|
2022-06-04 11:08:30 +08:00
|
|
|
now_FPS = pyglet.clock.get_frequency()
|
2022-12-25 23:15:49 +08:00
|
|
|
self.fps_log.update_tick(now_FPS, decimal_tick)
|
2021-02-21 11:58:47 +08:00
|
|
|
|
2023-12-15 12:45:46 +08:00
|
|
|
def draw_call(self, dt: float):
|
|
|
|
self.switch_to()
|
|
|
|
# self.logger.debug(f"draw call {dt}")
|
|
|
|
self.on_draw(dt)
|
|
|
|
self.flip()
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2023-12-16 22:24:44 +08:00
|
|
|
def on_draw(self, dt: float):
|
2023-06-26 10:30:27 +08:00
|
|
|
while (command := self.game.console.get_command()) is not None:
|
2023-05-14 20:56:19 +08:00
|
|
|
self.on_command(line.CommandText(command))
|
2021-02-22 21:32:13 +08:00
|
|
|
self.clear()
|
2023-12-16 22:24:44 +08:00
|
|
|
self.draw_update(dt)
|
2021-02-20 21:46:14 +08:00
|
|
|
self.draw_batch()
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-09-02 22:47:10 +08:00
|
|
|
def on_resize(self, width: int, height: int):
|
|
|
|
super().on_resize(width, height)
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2022-06-18 17:53:35 +08:00
|
|
|
def on_refresh(self, dt):
|
|
|
|
...
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2022-12-08 09:53:22 +08:00
|
|
|
def on_show(self):
|
|
|
|
# HWND_TOPMOST = -1
|
|
|
|
# _user32.SetWindowPos(self._hwnd, HWND_TOPMOST, 0, 0, self.width, self.height, 0)
|
|
|
|
...
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2022-12-08 09:53:22 +08:00
|
|
|
def on_hide(self):
|
|
|
|
# self.set_location(*self.get_location())
|
2023-12-03 16:54:07 +08:00
|
|
|
print("on hide!")
|
2022-12-08 09:53:22 +08:00
|
|
|
|
2023-07-02 15:54:50 +08:00
|
|
|
@_call_screen_before
|
2021-02-20 21:46:14 +08:00
|
|
|
def draw_batch(self):
|
2023-07-02 15:54:50 +08:00
|
|
|
self.main_batch.draw()
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2021-10-10 16:51:53 +08:00
|
|
|
"""
|
|
|
|
command line event
|
|
|
|
"""
|
|
|
|
|
2023-01-24 23:59:07 +08:00
|
|
|
def on_input(self, message: str) -> None:
|
|
|
|
command_text = line.CommandText(message)
|
|
|
|
self.on_command(command_text)
|
2023-12-03 16:54:07 +08:00
|
|
|
self.input_box.value = ""
|
2023-01-24 23:59:07 +08:00
|
|
|
|
2023-06-18 15:42:35 +08:00
|
|
|
def new_command(self):
|
|
|
|
self.game.console.new_command()
|
|
|
|
|
|
|
|
@_call_back(new_command)
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-11-06 19:07:32 +08:00
|
|
|
def on_command(self, command: line.CommandText):
|
2023-12-03 16:54:07 +08:00
|
|
|
command.text = command.text.rstrip("\n").rstrip(" ").strip("/")
|
2023-06-26 10:14:28 +08:00
|
|
|
self.logger.info(tr().window.command.text().format(f"|{command.text}|"))
|
2023-12-03 16:54:07 +08:00
|
|
|
if command.find("stop"):
|
2023-07-07 20:01:44 +08:00
|
|
|
self.logger.info("command stop!")
|
|
|
|
# HUGE THANKS to Discord @nokiyasos for this fix!
|
|
|
|
pyglet.app.exit()
|
2023-12-03 16:54:07 +08:00
|
|
|
elif command.find("fps"):
|
|
|
|
if command.find("log"):
|
2021-11-06 19:07:32 +08:00
|
|
|
self.logger.debug(self.fps_log.fps_list)
|
2023-12-03 16:54:07 +08:00
|
|
|
elif command.find("max"):
|
2021-11-06 19:07:32 +08:00
|
|
|
self.logger.info(self.fps_log.max_fps)
|
2023-08-27 01:52:45 +08:00
|
|
|
# self.command.push_line(self.fps_log.max_fps, block_line=True)
|
2023-12-03 16:54:07 +08:00
|
|
|
elif command.find("min"):
|
2021-11-06 19:07:32 +08:00
|
|
|
self.logger.info(self.fps_log.min_fps)
|
2023-08-27 01:52:45 +08:00
|
|
|
# self.command.push_line(self.fps_log.min_fps, block_line=True)
|
2023-12-03 16:54:07 +08:00
|
|
|
elif command.find("default"):
|
|
|
|
self.set_size(
|
2023-12-16 22:24:44 +08:00
|
|
|
int(DR_runtime.main_config["window_default"]["width"]),
|
|
|
|
int(DR_runtime.main_config["window_default"]["height"]),
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
|
|
|
elif command.find("lang"):
|
2023-04-05 16:00:38 +08:00
|
|
|
try:
|
|
|
|
lang = command.text[5:]
|
|
|
|
tr._language = lang
|
|
|
|
self.logger.info(tr().language_set_to())
|
|
|
|
except LanguageNotFound:
|
2023-12-03 16:54:07 +08:00
|
|
|
self.logger.info(
|
2023-12-14 09:38:03 +08:00
|
|
|
tr().language_available().format(os.listdir("./config/lang")), tag="command"
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
2023-04-05 16:00:38 +08:00
|
|
|
self.save_info()
|
2023-12-03 16:54:07 +08:00
|
|
|
elif command.find("mods"):
|
|
|
|
if command.find("list"):
|
2023-07-12 12:01:21 +08:00
|
|
|
self.logger.info(tr().mod.list())
|
2023-06-26 10:14:28 +08:00
|
|
|
for mod in self.game.mod_manager.loaded_mod_modules.values():
|
2023-12-03 16:54:07 +08:00
|
|
|
self.logger.info(
|
2023-12-14 09:38:03 +08:00
|
|
|
f"mod: {mod.name} id: {mod.mod_id} version: {mod.version}", tag="command"
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
|
|
|
elif command.find("reload"):
|
2023-06-26 10:14:28 +08:00
|
|
|
if not len(command.text) == 0:
|
|
|
|
print(f"reload mod: |{command.text}|")
|
|
|
|
self.game.mod_manager.reload_mod(command.text, game=self.game)
|
|
|
|
else:
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.info(tr().window.command.mods.reload.no_mod_id(), tag="command")
|
2021-10-10 16:51:53 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2023-04-20 23:51:28 +08:00
|
|
|
def on_message(self, message: line.CommandText):
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.info(tr().window.message.text().format(message), tag="message")
|
2021-10-10 16:51:53 +08:00
|
|
|
|
2021-02-16 22:28:13 +08:00
|
|
|
"""
|
2021-01-28 22:50:28 +08:00
|
|
|
keyboard and mouse input
|
2021-02-16 22:28:13 +08:00
|
|
|
"""
|
2021-01-28 22:50:28 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
|
|
|
def on_activate(self):
|
2022-07-01 13:59:08 +08:00
|
|
|
super().activate()
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
|
|
|
def on_deactivate(self):
|
|
|
|
...
|
|
|
|
|
2023-01-19 22:29:43 +08:00
|
|
|
@_call_screen_before
|
|
|
|
def on_move(self, x, y):
|
|
|
|
...
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2023-08-20 21:42:38 +08:00
|
|
|
def on_mouse_motion(self, x, y, dx, dy):
|
2023-02-09 14:47:39 +08:00
|
|
|
...
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2023-01-19 22:29:43 +08:00
|
|
|
@_call_screen_after
|
|
|
|
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
|
|
|
|
...
|
|
|
|
|
|
|
|
@_call_screen_after
|
|
|
|
def on_mouse_enter(self, x, y):
|
|
|
|
...
|
|
|
|
|
|
|
|
@_call_screen_after
|
|
|
|
def on_mouse_leave(self, x, y):
|
|
|
|
...
|
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers) -> None:
|
2023-02-09 14:47:39 +08:00
|
|
|
...
|
2021-04-09 19:49:02 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_press(self, x, y, button, modifiers) -> None:
|
2023-01-27 21:09:37 +08:00
|
|
|
self.logger.debug(
|
2023-12-03 16:54:07 +08:00
|
|
|
tr()
|
|
|
|
.window.mouse.press()
|
2023-12-14 09:38:03 +08:00
|
|
|
.format([x, y], tr().window.mouse[mouse.buttons_string(button)]()), tag="mouse"
|
2023-01-27 21:09:37 +08:00
|
|
|
)
|
2021-08-13 12:25:29 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_mouse_release(self, x, y, button, modifiers) -> None:
|
2023-01-27 21:09:37 +08:00
|
|
|
self.logger.debug(
|
2023-12-03 16:54:07 +08:00
|
|
|
tr()
|
|
|
|
.window.mouse.release()
|
2023-12-14 09:38:03 +08:00
|
|
|
.format([x, y], tr().window.mouse[mouse.buttons_string(button)]()), tag="mouse"
|
2023-01-27 21:09:37 +08:00
|
|
|
)
|
2021-02-16 22:28:13 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_key_press(self, symbol, modifiers) -> None:
|
2023-12-03 16:54:07 +08:00
|
|
|
if symbol == key.ESCAPE and not (
|
2023-12-13 12:38:26 +08:00
|
|
|
modifiers & ~(key.MOD_NUMLOCK | key.MOD_CAPSLOCK | key.MOD_SCROLLLOCK)
|
2023-12-03 16:54:07 +08:00
|
|
|
):
|
|
|
|
self.dispatch_event("on_close", "window")
|
2023-02-17 23:44:19 +08:00
|
|
|
if symbol == key.SLASH:
|
|
|
|
self.input_box._set_focus(True)
|
2022-12-25 23:15:49 +08:00
|
|
|
self.logger.debug(
|
2023-12-03 16:54:07 +08:00
|
|
|
tr()
|
|
|
|
.window.key.press()
|
2023-12-14 09:38:03 +08:00
|
|
|
.format(key.symbol_string(symbol), key.modifiers_string(modifiers)), tag="key"
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
2020-12-16 06:33:33 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-08-13 12:25:29 +08:00
|
|
|
def on_key_release(self, symbol, modifiers) -> None:
|
2022-12-25 23:15:49 +08:00
|
|
|
self.logger.debug(
|
2023-12-03 16:54:07 +08:00
|
|
|
tr()
|
|
|
|
.window.key.release()
|
2023-12-14 09:38:03 +08:00
|
|
|
.format(key.symbol_string(symbol), key.modifiers_string(modifiers)), tag="key"
|
2023-12-03 16:54:07 +08:00
|
|
|
)
|
2022-12-25 23:15:49 +08:00
|
|
|
|
|
|
|
@_call_screen_after
|
|
|
|
def on_file_drop(self, x, y, paths):
|
|
|
|
...
|
2021-10-02 20:40:06 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-10-02 20:40:06 +08:00
|
|
|
def on_text(self, text):
|
2023-12-03 16:54:07 +08:00
|
|
|
if text == "\r":
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.debug(tr().window.text.new_line(), tag="text")
|
2021-10-02 20:40:06 +08:00
|
|
|
else:
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.debug(tr().window.text.input().format(text), tag="text")
|
2023-12-03 16:54:07 +08:00
|
|
|
if text == "t":
|
2021-11-06 19:07:32 +08:00
|
|
|
self.input_box.enabled = True
|
2021-08-13 12:25:29 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-10-04 20:41:41 +08:00
|
|
|
def on_text_motion(self, motion):
|
|
|
|
motion_string = key.motion_string(motion)
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.debug(tr().window.text.motion().format(motion_string), tag="text")
|
2021-10-04 20:41:41 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_after
|
2021-10-04 20:41:41 +08:00
|
|
|
def on_text_motion_select(self, motion):
|
|
|
|
motion_string = key.motion_string(motion)
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.debug(tr().window.text.motion_select().format(motion_string), tag="text")
|
2021-10-04 20:41:41 +08:00
|
|
|
|
2022-12-25 23:15:49 +08:00
|
|
|
@_call_screen_before
|
2023-12-03 16:54:07 +08:00
|
|
|
def on_close(self, source: str = "window") -> None:
|
|
|
|
self.game.dispatch_mod_event(
|
|
|
|
"on_close", game=self.game, client=self, source=source
|
|
|
|
)
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.info(tr().window.game.stop_get().format(tr().game[source]()), tag="window")
|
|
|
|
self.logger.info(tr().window.game.stop(), tag="window")
|
2023-05-14 15:49:27 +08:00
|
|
|
# self.fps_log.check_list = False
|
2022-12-25 23:15:49 +08:00
|
|
|
DR_runtime.running = False
|
2021-10-23 17:01:59 +08:00
|
|
|
if self.run_input:
|
|
|
|
self.run_input = False
|
2021-10-25 22:08:00 +08:00
|
|
|
self.save_info()
|
2021-10-02 20:40:06 +08:00
|
|
|
super().on_close()
|
2023-12-14 09:38:03 +08:00
|
|
|
self.logger.info(tr().window.game.end(), tag="window")
|
2023-05-14 15:49:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
ClientWindow.register_event_type("on_command")
|