Difficult-Rocket/bin/client.py

188 lines
6.8 KiB
Python
Raw Normal View History

2021-02-20 21:46:14 +08:00
"""
2020-12-13 18:39:12 +08:00
writen by shenjackyuanjie
mail: 3695888@qq.com
2021-02-20 21:46:14 +08:00
"""
import os
2021-03-25 23:15:18 +08:00
import sys
2021-04-10 22:51:08 +08:00
import time
2021-08-24 22:31:52 +08:00
import logging
import configparser
if __name__ == '__main__': # been start will not run this
sys.path.append('/bin/libs')
sys.path.append('/bin')
2021-07-08 20:42:22 +08:00
import pyglet
2021-02-22 23:17:16 +08:00
from pyglet.window import key
from pyglet.window import mouse
2020-12-13 18:39:12 +08:00
2021-08-24 22:31:52 +08:00
import tools
import delivery
import drag_sprite
from new_thread import new_thread
2021-02-03 22:04:34 +08:00
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
class Client:
def __init__(self, dev_dic=None, dev_list=None, net_mode='local'):
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
# share memory
self.dev_list = dev_list
self.dev_dic = dev_dic
2021-08-13 12:25:29 +08:00
# config
self.config = tools.config('configs/main.config')
2021-02-22 23:17:16 +08:00
# lang
2021-08-13 12:25:29 +08:00
self.lang = tools.config('configs/lang/%s.json5' % self.config['runtime']['language'], 'client')
# value
self.process_id = 'Client'
self.process_name = 'Client process'
2021-02-20 21:46:14 +08:00
self.process_pid = os.getpid()
self.view = 'space'
self.net_mode = net_mode
2021-08-13 12:25:29 +08:00
self.caption = tools.name_handler(self.config['window']['caption'], {'version': self.config['runtime']['version']})
self.window = ClientWindow(dev_dic=self.dev_dic,
dev_list=self.dev_list,
net_mode=self.net_mode,
width=int(self.config['window']['width']),
height=int(self.config['window']['height']),
fullscreen=tools.format_bool(self.config['window']['full_screen']),
caption=self.caption,
resizable=tools.format_bool(self.config['window']['resizable']),
visible=tools.format_bool(self.config['window']['visible']))
2021-02-20 21:46:14 +08:00
2021-08-24 22:31:52 +08:00
# TODO 写一下服务端启动相关,还是需要服务端啊
2021-01-11 21:58:51 +08:00
2021-08-13 12:25:29 +08:00
class ClientWindow(pyglet.window.Window):
2021-08-13 12:25:29 +08:00
def __init__(self, dev_dic=None, dev_list=None, net_mode='local', *args, **kwargs):
super().__init__(*args, **kwargs)
2021-02-16 12:51:07 +08:00
"""
2021-02-03 22:04:34 +08:00
:param dev_list: 共享内存
:param dev_dic: 共享内存
:param logger: logger
:param net_mode: 网络模式 # local / ip
2021-02-16 12:51:07 +08:00
"""
2021-01-25 12:22:55 +08:00
# logging
2021-08-13 12:25:29 +08:00
self.logger = logging.getLogger('client')
# share memory
2021-01-21 15:05:29 +08:00
self.dev_list = dev_list
self.dev_dic = dev_dic
2021-02-03 22:04:34 +08:00
# value
self.net_mode = net_mode
2021-08-24 22:31:52 +08:00
# configs
pyglet.resource.path = ['textures']
pyglet.resource.reindex()
2021-08-13 12:25:29 +08:00
self.config_file = tools.config('configs/main.config')
self.FPS = int(self.config_file['runtime']['fps'])
self.SPF = 1.0 / self.FPS
2021-02-22 12:28:13 +08:00
# FPS
2021-02-22 21:32:13 +08:00
self.max_fps = [self.FPS, time.time()]
self.min_fps = [self.FPS, time.time()]
self.fps_wait = 5
# lang
2021-08-13 12:25:29 +08:00
self.lang = tools.config('configs/lang/%s.json5' % self.config_file['runtime']['language'], 'client')
2020-12-16 06:33:33 +08:00
# dic
2021-08-24 22:31:52 +08:00
self.textures = {}
2020-12-16 06:33:33 +08:00
# list
2021-02-20 21:46:14 +08:00
# batch
self.part_batch = pyglet.graphics.Batch()
self.label_batch = pyglet.graphics.Batch()
2021-08-24 22:31:52 +08:00
# frame
self.frame = pyglet.gui.Frame(self)
2021-08-13 12:25:29 +08:00
self.logger.info(self.lang['setup.done'])
2021-02-03 22:04:34 +08:00
# setup
self.setup()
2021-08-24 22:31:52 +08:00
self.info_label = pyglet.text.Label(x=10, y=self.height - 10,
anchor_x='left', anchor_y='top',
batch=self.label_batch)
2021-02-22 12:28:13 +08:00
pyglet.clock.schedule_interval(self.update, self.SPF)
2020-12-16 06:33:33 +08:00
2021-08-24 22:31:52 +08:00
@new_thread('client_load_textures')
def load_textures(self):
pass
2020-12-16 06:33:33 +08:00
def setup(self):
2021-08-13 12:25:29 +08:00
self.logger.info(self.lang['os.pid_is'].format(os.getpid(), os.getppid()))
2021-08-24 22:31:52 +08:00
image = pyglet.image.load('textures/Editor/PartButton.png')
self.textures['test'] = drag_sprite.DragSprite(10, 20, image, batch=self.label_batch, drag_by_all=False, drag_out_window=True)
pass
2021-02-20 21:46:14 +08:00
# draws
2021-01-29 14:07:40 +08:00
2021-08-24 22:31:52 +08:00
def update(self, tick):
2021-02-22 21:32:13 +08:00
self.FPS_update()
2021-02-26 13:42:22 +08:00
self.hit_box_update()
2021-02-22 21:32:13 +08:00
def FPS_update(self):
2021-02-22 12:28:13 +08:00
now_FPS = pyglet.clock.get_fps()
2021-02-22 21:32:13 +08:00
if now_FPS > self.max_fps[0]:
self.max_fps = [now_FPS, time.time()]
elif now_FPS < self.min_fps[0]:
self.min_fps = [now_FPS, time.time()]
else:
if (time.time() - self.max_fps[1]) > self.fps_wait:
self.max_fps = [self.FPS, time.time()]
elif (time.time() - self.min_fps[1]) > self.fps_wait:
self.min_fps = [self.FPS, time.time()]
2021-08-24 22:31:52 +08:00
self.info_label.text = 'now FPS: %06d \n max FPS: %02d \n min FPS: %02d' % (now_FPS, self.max_fps[0], self.min_fps[0])
2021-02-21 11:58:47 +08:00
2021-02-26 13:42:22 +08:00
def hit_box_update(self):
2021-08-13 12:25:29 +08:00
pass
2021-02-26 13:42:22 +08:00
2020-12-16 06:33:33 +08:00
def on_draw(self):
2021-02-22 21:32:13 +08:00
self.clear()
2021-02-20 21:46:14 +08:00
self.draw_batch()
def draw_batch(self):
self.part_batch.draw()
self.label_batch.draw()
2021-01-29 14:07:40 +08:00
def draw_label(self):
pass
"""
2021-01-28 22:50:28 +08:00
keyboard and mouse input
"""
2021-01-28 22:50:28 +08:00
2021-08-13 12:25:29 +08:00
def on_mouse_motion(self, x, y, dx, dy) -> None:
2021-04-10 22:48:22 +08:00
# self.logger.debug('按键移动 %s %s %s %s' % (x, y, dx, dy))
pass
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers) -> None:
2021-08-24 22:31:52 +08:00
# self.logger.debug('按键拖拽 %s %s %s %s %s %s' % (x, y, dx, dy, buttons, modifiers))
self.textures['test'].on_mouse_drag(x, y, dx, dy, buttons, modifiers)
2021-04-10 22:48:22 +08:00
pass
2021-08-13 12:25:29 +08:00
def on_mouse_press(self, x, y, button, modifiers) -> None:
2021-02-22 23:17:16 +08:00
if button == mouse.LEFT:
2021-08-24 22:31:52 +08:00
self.logger.debug(self.lang['mouse.press'].format([x, y], self.lang['mouse.left']))
2021-02-22 23:17:16 +08:00
elif button == mouse.RIGHT:
2021-08-24 22:31:52 +08:00
self.logger.debug(self.lang['mouse.press'].format([x, y], self.lang['mouse.right']))
self.textures['test'].on_mouse_press(x, y, button, modifiers)
2021-08-13 12:25:29 +08:00
def on_mouse_release(self, x, y, button, modifiers) -> None:
2021-08-24 22:31:52 +08:00
self.logger.debug(self.lang['mouse.release'].format([x, y], self.lang['mouse.right']))
self.textures['test'].on_mouse_release(x, y, button, modifiers)
2021-08-13 12:25:29 +08:00
def on_key_press(self, symbol, modifiers) -> None:
2021-02-22 23:17:16 +08:00
if symbol == key.ESCAPE and not (modifiers & ~(key.MOD_NUMLOCK |
key.MOD_CAPSLOCK |
key.MOD_SCROLLLOCK)):
self.dispatch_event('on_close')
2020-12-16 06:33:33 +08:00
2021-08-13 12:25:29 +08:00
def on_key_release(self, symbol, modifiers) -> None:
2020-12-16 06:33:33 +08:00
pass
2021-08-13 12:25:29 +08:00
def on_close(self) -> None:
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'))
# pyglet source code
self.has_exit = True
from pyglet import app
if app.event_loop.is_running:
self.close()