rename some folders

This commit is contained in:
沈瑗杰 2021-10-23 17:01:59 +08:00
parent 9394aa62d4
commit b45b041d23
64 changed files with 78 additions and 58 deletions

View File

@ -19,6 +19,7 @@ from Difficult_Rocket.api.new_thread import new_thread
# from libs.pyglet
from libs import pyglet
from libs.pyglet import font
from libs.pyglet.text import Label
from libs.pyglet.window import key
from libs.pyglet.gui import widgets
@ -56,11 +57,6 @@ class CommandLine(widgets.WidgetBase):
self.command_split = 25
self.command_distance = 20
self.document = UnformattedDocument(text=self.text)
self.document.set_style(0, len(self.document.text), dict(color=(122, 173, 153, 255)))
font = self.document.get_font()
height = font.ascent - font.descent
# group
self._user_group = group
bg_group = Group(order=0, parent=group)
@ -87,22 +83,10 @@ class CommandLine(widgets.WidgetBase):
batch=batch, group=fg_group)
self._outline.opacity = color[3]
self.document = UnformattedDocument(text=self.text)
self.document.set_style(0, len(self.document.text), dict(color=(200, 132, 123, 255),
font_size=font_size, font_name=translate.鸿蒙简体))
font = self.document.get_font()
height = font.ascent - font.descent
self.layout = IncrementalTextLayout(document=self.document,
width=width, height=height,
batch=batch, multiline=False)
self.layout.position = x, y
self.caret = Caret(self.layout, batch=batch, color=(100, 0 ,0))
self.caret.visible = False
self.editing = False
def _update_position(self):
self.caret.position = self._x, self.y
pass
def update_groups(self, order):
self._line.group = Group(order=order + 1, parent=self._user_group)
@ -119,7 +103,6 @@ class CommandLine(widgets.WidgetBase):
assert type(value) is str, 'CommandLine\'s text must be string!'
self._text = value
self._line.text = value
self.document.text = value
@property
def command_view(self):
@ -164,11 +147,10 @@ class CommandLine(widgets.WidgetBase):
self._editing = value
self._line.visible = value
self._outline.visible = value
self.caret.visible = value
for label in self._label:
label.visible = value
@new_thread('command wait')
@new_thread('command wait', log_thread=False)
def wait(self, wait):
self._label[0].visible = True
time.sleep(wait)
@ -177,7 +159,6 @@ class CommandLine(widgets.WidgetBase):
def on_text(self, text):
if self.editing:
self.caret.on_text(text)
if text in ('\r', '\n'): # goto a new line
if not self.text:
pass
@ -197,11 +178,9 @@ class CommandLine(widgets.WidgetBase):
self.editing = True
self.text = '/'
self._text_position = 1
self.caret.on_text_motion(key.MOTION_RIGHT)
def on_text_motion(self, motion):
if self.editing:
self.caret.on_text_motion(motion)
# edit motion
if motion == key.MOTION_DELETE: # 确保不越界
self.text = f'{self.text[:self._text_position]}{self.text[self._text_position + 1:]}' # 简单粗暴的删除
@ -229,22 +208,22 @@ class CommandLine(widgets.WidgetBase):
def on_text_motion_select(self, motion):
if self.editing:
self.caret.on_text_motion_select(motion)
pass
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.editing:
self.caret.on_mouse_drag(x, y, dx, dy, buttons, modifiers)
pass
def on_mouse_press(self, x, y, buttons, modifiers):
if self.editing:
self.caret.on_mouse_press(x, y, buttons, modifiers)
pass
def on_command(self, command):
def on_command(self, command: text):
if self.editing:
return
"""give command to it"""
def on_message(self, message):
def on_message(self, message: text):
if self.editing:
return
"""give message to it"""

View File

@ -30,7 +30,7 @@ class FunctionThread(threading.Thread):
__NONE = object()
def __init__(self, target, name, args, kwargs):
super().__init__(target=target, args=args, kwargs=kwargs, name=name, daemon=True)
super().__init__(target=target, args=args, kwargs=kwargs, name=name)
self.__return_value = self.__NONE
self.__error = None
@ -39,6 +39,7 @@ class FunctionThread(threading.Thread):
self.__return_value = target(*args_, **kwargs_)
except Exception as e:
self.__error = e
print(e)
raise e from None
self._target = wrapped_target
@ -51,9 +52,21 @@ class FunctionThread(threading.Thread):
raise RuntimeError('The thread is still running')
raise self.__error
return self.__return_value
def join(self, timeout: Optional[float] = None) -> None:
super().join(timeout)
def start(self) -> None:
try:
super().start()
except:
raise
def new_thread(thread_name: Optional[str or Callable] = None):
def new_thread(thread_name: Optional[str or Callable] = None,
daemon: bool = False,
log_thread: bool = True):
"""
Use a new thread to execute the decorated function
The function return value will be set to the thread instance that executes this function
@ -63,8 +76,10 @@ def new_thread(thread_name: Optional[str or Callable] = None):
@functools.wraps(func) # to preserve the origin function information
def wrap(*args, **kwargs):
thread = FunctionThread(target=func, args=args, kwargs=kwargs, name=thread_name)
thread.daemon = daemon
thread.start()
crash.all_thread.append(thread)
if log_thread:
crash.all_thread.append(thread)
return thread
# bring the signature of the func to the wrap function
# so inspect.getfullargspec(func) works correctly

View File

@ -25,10 +25,11 @@ if __name__ == '__main__': # been start will not run this
sys.path.append('/bin')
# Difficult_Rocket function
from Difficult_Rocket import guis
from Difficult_Rocket.api import command
from Difficult_Rocket.api.translate import tr
from Difficult_Rocket.api import tools, new_thread, translate
from . import guis
from .api import command
from .api.Exp import *
from .api.translate import tr
from .api import tools, new_thread, translate
# libs function
local_lib = True
@ -112,8 +113,8 @@ class ClientWindow(pyglet.window.Window):
self.setup()
# 命令显示
self.command_group = pyglet.graphics.Group(0)
self.command = command.CommandLine(x=50, y=30,
width=self.width-100, height=40,
self.command = command.CommandLine(x=50, y=30, # 实例化
width=self.width - 100, height=40,
length=int(self.game_config['command']['show']),
batch=self.label_batch, group=self.command_group)
self.push_handlers(self.command)
@ -135,35 +136,41 @@ class ClientWindow(pyglet.window.Window):
self.logger.debug(tr.lang('window', 'setup.use_time_ns').format(self.use_time))
def setup(self):
self.load_fonts()
# print(pyglet.font.have_font('HarmonyOS_Sans_Black'))
self.load_fonts().join()
# print(pyglet.font.load(translate.鸿蒙简体))
# @new_thread('client load_fonts')
@new_thread('client load_fonts')
def load_fonts(self):
file_path = './libs/fonts/HarmonyOS Sans/'
file_path = './libs/fonts/HarmonyOS_Sans/'
ttf_files = os.listdir(file_path)
self.logger.info(tr.lang('window', 'fonts.found').format(ttf_files))
for ttf_file in ttf_files:
pyglet.font.add_directory(f'{file_path}{ttf_file}')
for ttf_folder in ttf_files:
for ttf_file in os.listdir(f'{file_path}{ttf_folder}'):
if not ttf_file[-4:] == '.ttf': continue
pyglet.font.add_file(f'{file_path}{ttf_folder}/{ttf_file}')
# print(f'{file_path}{ttf_file}')
@new_thread('client load_editor')
# @new_thread('client load_editor')
def load_Editor(self):
pass
def start_game(self) -> None:
self.run_input = True
# self.read_thread = threading.Thread(target=self.read_input, name='client_read_input')
# self.read_thread.start()
# crash.all_thread.append(self.read_thread)
self.read_input()
pyglet.app.run()
@new_thread('read_input', daemon=True)
def read_input(self):
self.logger.debug('read_input start')
while self.run_input:
get = input('<<<')
self.logger.info('<<<')
get = input()
if get in ('', ' ', '\n', '\r'):
continue
self.logger.info(get)
if get == 'stop':
self.run_input = False
self.dispatch_event('on_close', 'a') # source = 'command'
self.logger.debug('read_input end')
"""
@ -199,15 +206,16 @@ class ClientWindow(pyglet.window.Window):
self.part_batch.draw()
self.label_batch.draw()
"""
command line event
"""
def on_command(self, command):
def on_command(self, command: command.CommandLine.text):
self.logger.info(tr.lang('window', 'command.text').format(command))
if command == 'stop':
self.dispatch_event('on_close', 'command') # source = command
def on_message(self, message):
def on_message(self, message: command.CommandLine.text):
self.logger.info(tr.lang('window', 'message.text').format(message))
"""
@ -250,12 +258,15 @@ class ClientWindow(pyglet.window.Window):
motion_string = key.motion_string(motion)
self.logger.debug(tr.lang('window', 'text.motion_select').format(motion_string))
def on_close(self) -> None:
self.run_input = False
# self.input_line.join()
def on_close(self, source: str = 'window') -> None:
self.logger.info(tr.lang('window', 'game.stop_get').format(tr.lang('window', f'game.{source}_stop')))
self.logger.info(tr.lang('window', 'game.stop'))
if self.run_input:
self.run_input = False
config_file = configparser.ConfigParser()
config_file.read('configs/main.config')
config_file['window']['width'] = str(self.width)
config_file['window']['height'] = str(self.height)
config_file.write(open('configs/main.config', 'w', encoding='utf-8'))
super().on_close()
self.logger.info(tr.lang('window', 'game.end'))

View File

@ -11,7 +11,7 @@ github: @shenjackyuanjie
gitee: @shenjackyuanjie
"""
from Difficult_Rocket.api import translate
from ..api import translate
from libs import pyglet
from libs.pyglet.gui import widgets

View File

@ -40,7 +40,13 @@
'message.text': '输入信息: {}',
'libs.local': '正在使用本地 pyglet 库 版本为: {}',
'libs.outer': '正在使用全局 pyglet 库 版本为: {}\n(可能会造成bug因为本地库版本为2.0dev9)',
'fonts.found': '在字体列表中找到以下字体库: {}'
'fonts.found': '在字体列表中找到以下字体库: {}',
'game.input_stop': '控制台',
'game.command_stop': '游戏内命令行',
'game.window_stop': '窗口',
'game.stop_get': '从{}传入关闭指令,关闭游戏中',
'game.stop': '游戏正在关闭,保存数据中···',
'game.end': '游戏已经关闭'
},
'server': {
'setup.done': '服务端载入完成',

View File

@ -8,7 +8,7 @@ write_py_v = 3.8.10
[window]
style = None
width = 1217
height = 918
height = 896
visible = true
caption = Difficult Rocket {version}
resizable = true

View File

@ -28,6 +28,7 @@
- `lang/en-us.json5` now up to date with `lang/zh-CN.json5`
- `translate/Lang.翻译` same as `Lang.lang`
- `command/CommandLine` to render command line
- `@new_thread` now can option if log this thread to `crash` or not
### Translate
@ -39,6 +40,12 @@
- `text.motion_select`
- `setup.use_time_ns`
- `fonts.found`
- `game.input_stop`
- `game.command_stop`
- `game.window_stop`
- `game.stop_get`
- `game.stop`
- `game.end`
- `client`
- `setup.use_time_ns`

2
start.cmd Normal file
View File

@ -0,0 +1,2 @@
python Difficult_Rocket.py
pause