From 00341327e1501129cd5e31201417308071c9a641 Mon Sep 17 00:00:00 2001 From: march3 Date: Thu, 16 Mar 2023 20:11:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scenes/func.py | 3 +- scenes/solar_system_1.py | 4 +- simulators/ursina/ui_component.py | 38 ++++++++++++++++ simulators/ursina/ursina_ui.py | 61 +++++++++++++------------ simulators/ursina_simulator.py | 76 +------------------------------ simulators/views/ursina_view.py | 5 ++ 6 files changed, 80 insertions(+), 107 deletions(-) create mode 100644 simulators/ursina/ui_component.py diff --git a/scenes/func.py b/scenes/func.py index 6553a72..081c840 100644 --- a/scenes/func.py +++ b/scenes/func.py @@ -75,7 +75,8 @@ def ursina_run(bodies, def callback_update(): for ursina_view in simulator.ursina_views: simulator.check_and_evolve() - ursina_view.update() + if ursina_view.appeared: + ursina_view.update() # print('....') import sys diff --git a/scenes/solar_system_1.py b/scenes/solar_system_1.py index 7cdfeb6..a27faa4 100644 --- a/scenes/solar_system_1.py +++ b/scenes/solar_system_1.py @@ -7,7 +7,7 @@ # python_version :3.8 # ============================================================================== from bodies import Sun, Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, Moon -from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY +from common.consts import SECONDS_PER_WEEK, SECONDS_PER_DAY, SECONDS_PER_YEAR from scenes.func import mayavi_run, ursina_run if __name__ == '__main__': @@ -36,4 +36,4 @@ if __name__ == '__main__': # mayavi_run(bodies, SECONDS_PER_WEEK, view_azimuth=-45) # 使用 ursina 查看的运行效果 - ursina_run(bodies, SECONDS_PER_DAY, position=(0, 0, 0)) \ No newline at end of file + ursina_run(bodies, SECONDS_PER_YEAR, position=(0, 0, 0)) \ No newline at end of file diff --git a/simulators/ursina/ui_component.py b/simulators/ursina/ui_component.py new file mode 100644 index 0000000..9bda482 --- /dev/null +++ b/simulators/ursina/ui_component.py @@ -0,0 +1,38 @@ +# -*- coding:utf-8 -*- +# title :ursina UI组件 +# description :ursina UI组件 +# author :Python超人 +# date :2023-02-11 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \ + load_texture, held_keys, Button +from ursina.prefabs.first_person_controller import FirstPersonController +from simulators.ursina.ursina_config import UrsinaConfig +from simulators.ursina.ursina_event import UrsinaEvent +from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup + + +class UiSlider(Slider): + def __init__(self, text, min=0.01, max=3, default=1): + super().__init__(text=text, + height=Text.size, + y=-.6, + step=.01, + min=min, + max=max, + default=default, + color=color.rgba(0.0, 0.0, 0.0, 0.5)) + self.label.scale = 1 + self.height = Text.size/1.2 + # self.text_entity + # self.update() + + +class SwithButton(ButtonGroup): + def __int__(self, options, default): + super().__init__(options, min_selection=1, y=0, default=default, + selected_color=color.green, ignore_paused=True, + color=color.rgba(0.0, 0.0, 0.0, 0.5)) + self.label.scale = 0.8 \ No newline at end of file diff --git a/simulators/ursina/ursina_ui.py b/simulators/ursina/ursina_ui.py index adcba8c..f417d28 100644 --- a/simulators/ursina/ursina_ui.py +++ b/simulators/ursina/ursina_ui.py @@ -9,51 +9,32 @@ from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \ load_texture, held_keys, Button from ursina.prefabs.first_person_controller import FirstPersonController + +from simulators.ursina.ui_component import UiSlider, SwithButton from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup class UrsinaUI: - def __init__(self): - Text.default_font = 'simsun.ttc' - # self.pause_handler = Entity(ignore_paused=True) - # 加载中文字体文件 - - # text_time_scale = "1" - # self.text_time_scale_info = None - # self.pause_handler.input = self.pause_handler_input - # self.show_text_time_scale_info() - # key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]" - # key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True) - # # self.show_button() - self.slider_body_spin_factor = Slider(text="自转速度", y=-.6, step=.01, min=0.01, max=3, default=1, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) - self.slider_run_speed_factor = Slider(text="运行速度", y=-.1, step=.01, min=0.01, max=500, default=1, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) + def ui_component_init(self): + Text.default_font = 'simsun.ttc' application.time_scale = 0.5 - self.slider_control_speed_factor = Slider(text="控制速度", y=-.1, step=.01, min=0.01, max=30, default=0.5, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) + self.slider_body_spin_factor = UiSlider(text='自转速度', min=0.01, max=30, default=1) + self.slider_run_speed_factor = UiSlider(text="运行速度", min=0.01, max=800, default=1) + self.slider_control_speed_factor = UiSlider(text="控制速度", min=0.01, max=30, default=application.time_scale) + self.slider_trail_length = UiSlider(text="拖尾长度", min=30, max=500, default=UrsinaConfig.trail_length) self.slider_body_spin_factor.on_value_changed = self.on_slider_body_spin_changed self.slider_run_speed_factor.on_value_changed = self.on_slider_run_speed_changed self.slider_control_speed_factor.on_value_changed = self.on_slider_control_speed_changed - - self.slider_trail_length = Slider(text="拖尾长度", y=-.1, step=1, min=30, max=500, default=UrsinaConfig.trail_length, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) self.slider_trail_length.on_value_changed = self.on_slider_trail_length_changed - - self.on_off_switch = ButtonGroup(('||', '○'), min_selection=1, y=0, default='○', - selected_color=color.green, ignore_paused=True, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) + self.on_off_switch = SwithButton(('||', '○'), default='○') self.on_off_switch.selected_color = color.red - self.on_off_trail = ButtonGroup((' ', '...'), min_selection=1, y=0, default=' ', - selected_color=color.green, ignore_paused=True, - color=color.rgba(0.0, 0.0, 0.0, 0.5)) - + self.on_off_trail = SwithButton((' ', '...'), default=' ') self.on_off_trail.on_value_changed = self.on_off_trail_changed self.point_button = Button(text='寻找', origin=(0, 0), y=2, @@ -61,6 +42,7 @@ class UrsinaUI: self.reset_button = Button(text='重置', origin=(0, 0), y=2, on_click=self.on_reset_button_click, color=color.rgba(0.0, 0.0, 0.0, 0.5)) self.on_off_switch.on_value_changed = self.on_off_switch_changed + wp = WindowPanel( title='', content=( @@ -79,9 +61,28 @@ class UrsinaUI: ), ignore_paused=True, color=color.rgba(0.0, 0.0, 0.0, 0.5) ) wp.y = 0.5 # wp.panel.scale_y / 2 * wp.scale_y # center the window panel - wp.x = -wp.scale_x + wp.x = 0.6 # wp.scale_x + 0.1 + # wp.x = 0#wp.panel.scale_x / 2 * wp.scale_x self.wp = wp + def __init__(self): + self.ui_component_init() + + # self.pause_handler = Entity(ignore_paused=True) + # 加载中文字体文件 + + # text_time_scale = "1" + # self.text_time_scale_info = None + # self.pause_handler.input = self.pause_handler_input + # self.show_text_time_scale_info() + # key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]" + # key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True) + # # self.show_button() + # slider_text = Text(text='自转速度', scale=1, position=(-0.6, 0.3)) + # slider = Slider(scale=0.5, position=(-0.6, 0), min=0, max=10, step=1, text=slider_text) + + + def on_off_trail_changed(self): if self.on_off_trail.value == "...": UrsinaConfig.show_trail = True diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index d59e3f9..b3e110a 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -19,7 +19,7 @@ from common.system import System import time import datetime import math -from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight # , SunLight +from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight from scenes.func import ursina_run @@ -143,22 +143,7 @@ class UrsinaSimulator(Simulator): return lights - # def create_asteroids(self): - # """ - # 小行星 - # :return: - # """ - # from simulators.views.ursina_mesh import create_torus,create_body_torus - # textureAsteroids = '../textures/asteroids.png' - # body_torus = create_torus(9, 10, 64) - # asteroids = Entity(model=body_torus, texture=textureAsteroids, scale=10, rotation=(90, 0, 0), double_sided=True) - # asteroids.x = 0 - # asteroids.y = 0 - # asteroids.z = 0 - # asteroids.set_light_off() - def run(self, dt, **kwargs): - from ursina import EditorCamera, PointLight, SpotLight, AmbientLight, DirectionalLight # 设定时间间隔为0.01秒 interval = 0.01 self.evolve_dt = dt * interval @@ -185,66 +170,9 @@ class UrsinaSimulator(Simulator): if cosmic_bg is not None and os.path.exists(cosmic_bg): self.cosmic_background(cosmic_bg) - # self.create_asteroids() - - ui = UrsinaUI() - # mouse.visible = True EditorCamera(ignore_paused=True) - # pause_handler = Entity(ignore_paused=True) - # # 加载中文字体文件 - # Text.default_font = 'simsun.ttc' - # # text_time_scale = "1" - # text_time_scale_info = None - # - # def show_text_time_scale_info(): - # nonlocal text_time_scale_info - # if text_time_scale_info is not None: - # text_time_scale_info.disable() - # text_time_scale = "控制倍率:" + str(application.time_scale).ljust(4, " ") - # text_time_scale_info = Text(text=text_time_scale, position=(-0.8, 0.5), origin=(-1, 1), background=True) - # - # # 按空格键则暂停 - # def pause_handler_input(key): - # nonlocal text_time_scale_info - # time_scales = [0.05, 0.1, 0.2, 0.5, 1, 5, 10, 20, 30] - # # print(key) - # if key == 'space': - # application.paused = not application.paused # Pause/unpause the game. - # elif key == 'tab': - # # application.time_scale 属性控制游戏时间流逝的速度。 - # # 具体来说,它是一个浮点数,用于调整游戏时间流逝速度的比例,其默认值为 1.0,表示正常速度。 - # # 当你将它设置为小于 1.0 的值时,游戏时间会变慢,而设置为大于 1.0 的值时,游戏时间则会变快。 - # for idx, time_scale in enumerate(time_scales): - # if float(application.time_scale) == time_scale: - # if idx < len(time_scales) - 1: - # application.time_scale = time_scales[idx + 1] - # break - # else: - # application.time_scale = time_scales[0] - # elif key == '+': - # UrsinaConfig.run_speed_factor *= 2 - # elif key == "= up": - # UrsinaConfig.body_spin_factor *= 2 - # # if application.time_scale in time_scales: - # # idx = time_scales.index(application.time_scale) - # # if idx < len(time_scales) - 1: - # # application.time_scale = time_scales[idx + 1] - # elif key == '-': - # UrsinaConfig.run_speed_factor *= 0.5 - # elif key == "- up": - # UrsinaConfig.body_spin_factor *= 0.5 - # # if application.time_scale in time_scales: - # # idx = time_scales.index(application.time_scale) - # # if idx > 0: - # # application.time_scale = time_scales[idx - 1] - # - # show_text_time_scale_info() - # - # pause_handler.input = pause_handler_input - # show_text_time_scale_info() - # key_info_str = "退出[按2次ESC] 方位控制[鼠标QWEASD] 开始暂停[空格] 控制倍率[Tab - +]" - # key_info = Text(text=key_info_str, position=(-0.8, 0.5), origin=(-1, 1), background=True) + self.app.run() diff --git a/simulators/views/ursina_view.py b/simulators/views/ursina_view.py index 5356071..f391c56 100644 --- a/simulators/views/ursina_view.py +++ b/simulators/views/ursina_view.py @@ -160,6 +160,7 @@ class Planet(Entity): try: pos = self.position except Exception as e: + print(self.body_view.body) self.destroy_all() return trails_keys = self.trails.keys() @@ -226,6 +227,7 @@ class Planet(Entity): # 天体旋转 self.rotation_y -= self.rotspeed except Exception as e: + print(self.body_view.body) self.destroy_all() return @@ -281,6 +283,7 @@ class Planet(Entity): # 删除拖尾 for entity, pos in self.trails.items(): destroy(entity) + self.trails.clear() def destroy_all(self): # 从天体系统中移除自己(TODO:暂时还不能移除) @@ -290,6 +293,8 @@ class Planet(Entity): # 如果有行星环,则删除行星环 if hasattr(self, "ring"): destroy(self.ring) + self.body_view.body.appeared = False + self.body_view.appeared = False # 最后删除自己 destroy(self) -- GitLab