ursina_ui.py 10.6 KB
Newer Older
三月三net's avatar
三月三net 已提交
1 2 3 4 5 6 7 8 9
# -*- 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, \
三月三net's avatar
三月三net 已提交
10
    load_texture, held_keys, Button, ButtonList, destroy
三月三net's avatar
三月三net 已提交
11
from ursina.prefabs.first_person_controller import FirstPersonController
三月三net's avatar
三月三net 已提交
12

三月三net's avatar
三月三net 已提交
13
from simulators.ursina.ui_component import UiSlider, SwithButton, UiButton
三月三net's avatar
三月三net 已提交
14 15
from simulators.ursina.ursina_config import UrsinaConfig
from simulators.ursina.ursina_event import UrsinaEvent
三月三net's avatar
三月三net 已提交
16
from ursina import WindowPanel, InputField, Button, Slider, ButtonGroup, Panel
三月三net's avatar
三月三net 已提交
17 18 19 20


class UrsinaUI:

三月三net's avatar
三月三net 已提交
21
    def ui_component_init(self):
三月三net's avatar
三月三net 已提交
22

三月三net's avatar
三月三net 已提交
23 24 25 26
        self.start_button_text = "●"  # 》●▲○◎
        self.pause_button_text = "〓"  # 〓 || ‖
        self.no_trail_button_text = "○ "
        self.trail_button_text = "○--"
三月三net's avatar
三月三net 已提交
27

三月三net's avatar
三月三net 已提交
28
        application.time_scale = 0.5
三月三net's avatar
三月三net 已提交
29
        self.slider_body_spin_factor = UiSlider(text='自转速度', min=0.01, max=30, default=1)
三月三net's avatar
三月三net 已提交
30
        self.slider_body_size_factor = UiSlider(text='天体缩放', min=0.01, max=10, default=1)
三月三net's avatar
三月三net 已提交
31 32 33
        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)
三月三net's avatar
三月三net 已提交
34

三月三net's avatar
三月三net 已提交
35
        self.slider_body_size_factor.on_value_changed = self.on_slider_body_size_changed
三月三net's avatar
三月三net 已提交
36 37 38
        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
三月三net's avatar
三月三net 已提交
39 40
        self.slider_trail_length.on_value_changed = self.on_slider_trail_length_changed

三月三net's avatar
三月三net 已提交
41
        self.on_off_switch = SwithButton((self.pause_button_text,
三月三net's avatar
三月三net 已提交
42 43 44
                                          self.start_button_text),
                                         default=self.start_button_text,
                                         tooltips=('暂停', '运行'))
三月三net's avatar
三月三net 已提交
45
        self.on_off_switch.selected_color = color.red
三月三net's avatar
三月三net 已提交
46

三月三net's avatar
三月三net 已提交
47 48 49 50 51 52
        self.sec_per_time_switch = SwithButton(("默认", "天", "周", "月", "年", "十年", "百年"),
                                               default="默认",
                                               tooltips=("系统默认", "每秒相当于1天", "每秒相当于1周",
                                                         "每秒相当于1个月",
                                                         "每秒相当于1年", "每秒相当于十年", "每秒相当于1百年"))

三月三net's avatar
三月三net 已提交
53 54 55
        self.on_off_trail = SwithButton((self.no_trail_button_text, self.trail_button_text),
                                        default=self.no_trail_button_text,
                                        tooltips=('天体运行无轨迹', '天体运行有拖尾轨迹'))
三月三net's avatar
三月三net 已提交
56 57
        self.on_off_trail.on_value_changed = self.on_off_trail_changed

三月三net's avatar
三月三net 已提交
58
        self.point_button = UiButton(text='寻找', on_click=self.on_searching_bodies_click)
三月三net's avatar
三月三net 已提交
59
        self.reset_button = UiButton(text='重置', on_click=self.on_reset_button_click)
三月三net's avatar
三月三net 已提交
60

三月三net's avatar
三月三net 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        # button1 = Button(text='Button 1', scale=(0.1, 0.1), position=(-0.1, 0))
        # button2 = Button(text='Button 2', scale=(0.1, 0.1), position=(0.1, 0))

        # btn_settings = UiButton(text='操作设置', on_click=self.on_point_button_click)
        # btn_settings.position = window.top_left
        # btn_settings.y = 0.5
        # # btn_settings.scale = (0.1,0.1)
        # # btn_settings.y = 0
        # btn_settings.scale = (.25, .025),
        # btn_settings.origin = (-.5, .5),
        # btn_settings.pressed_scale = 1,
        # if btn_settings.text_entity:
        #     btn_settings.text_entity.x = .05
        #     btn_settings.text_entity.origin = (-.5, 0)
        #     btn_settings.text_entity.scale *= .8
三月三net's avatar
三月三net 已提交
76 77

        self.on_off_switch.on_value_changed = self.on_off_switch_changed
三月三net's avatar
三月三net 已提交
78 79 80
        wp = WindowPanel(
            title='',
            content=(
三月三net's avatar
三月三net 已提交
81
                Text('方位控制: Q W E A S D + 鼠标右键', font='msyhl.ttc'),
三月三net's avatar
三月三net 已提交
82 83 84 85
                # InputField(name='name_field'),
                # Button(text='Submit', color=color.azure),
                self.point_button,
                self.reset_button,
三月三net's avatar
三月三net 已提交
86
                self.sec_per_time_switch,
三月三net's avatar
三月三net 已提交
87
                self.on_off_switch,
三月三net's avatar
三月三net 已提交
88 89
                self.on_off_trail,
                self.slider_trail_length,
三月三net's avatar
三月三net 已提交
90
                self.slider_body_size_factor,
三月三net's avatar
三月三net 已提交
91 92 93 94
                self.slider_body_spin_factor,
                self.slider_run_speed_factor,
                self.slider_control_speed_factor

三月三net's avatar
三月三net 已提交
95
            ), ignore_paused=True, color=color.rgba(0.0, 0.0, 0.0, 0.5), popup=True
三月三net's avatar
三月三net 已提交
96
        )
三月三net's avatar
三月三net 已提交
97 98 99
        self.sec_per_time_switch.x = -0.5
        self.on_off_switch.x = -0.2
        self.on_off_trail.x = -0.2
三月三net's avatar
三月三net 已提交
100
        wp.y = 0.5  # wp.panel.scale_y / 2 * wp.scale_y  # center the window panel
三月三net's avatar
三月三net 已提交
101
        wp.x = 0.6  # wp.scale_x + 0.1
三月三net's avatar
三月三net 已提交
102
        # wp.x = 0#wp.panel.scale_x / 2 * wp.scale_x
三月三net's avatar
三月三net 已提交
103 104
        self.wp = wp

三月三net's avatar
三月三net 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    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)

三月三net's avatar
三月三net 已提交
121
    def on_off_trail_changed(self):
三月三net's avatar
三月三net 已提交
122
        if self.on_off_trail.value == self.trail_button_text:
三月三net's avatar
三月三net 已提交
123 124 125 126
            UrsinaConfig.show_trail = True
        else:
            UrsinaConfig.show_trail = False

三月三net's avatar
三月三net 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    def bodies_button_list_click(self, item):
        print("select->", item)

        destroy(self.bodies_button_list)

    def on_searching_bodies_click(self):
        results = UrsinaEvent.on_searching_bodies()
        if len(results) > 0:
            sub_name, bodies = results[0]
            # print(results[0])
            button_dict = {}

            for body in bodies:
                def callback_action(b=body):
                    self.bodies_button_list_click(b)

                button_dict[body.name] = callback_action

            self.bodies_button_list = ButtonList(button_dict, font=UrsinaConfig.CN_FONT, button_height=1.5)
            # self.bodies_button_list.input = self.bodies_button_list_input
三月三net's avatar
三月三net 已提交
147 148 149 150 151

    def on_reset_button_click(self):
        UrsinaEvent.on_reset()

    def on_off_switch_changed(self):
三月三net's avatar
三月三net 已提交
152
        if self.on_off_switch.value == self.pause_button_text:
三月三net's avatar
三月三net 已提交
153 154
            self.on_off_switch.selected_color = color.green
            application.paused = True
三月三net's avatar
三月三net 已提交
155 156 157 158
            for c in self.wp.children:
                if not c.ignore_paused:
                    # c.enabled = True
                    c.disabled = False
三月三net's avatar
三月三net 已提交
159 160 161
        else:
            self.on_off_switch.selected_color = color.red
            application.paused = False
三月三net's avatar
三月三net 已提交
162 163 164 165
            for c in self.wp.children:
                if not c.ignore_paused:
                    # c.enabled = True
                    c.disabled = False
三月三net's avatar
三月三net 已提交
166

三月三net's avatar
三月三net 已提交
167 168 169
    def on_slider_trail_length_changed(self):
        UrsinaConfig.trail_length = int(self.slider_trail_length.value)

三月三net's avatar
三月三net 已提交
170 171 172 173 174 175
    def on_slider_control_speed_changed(self):
        application.time_scale = self.slider_control_speed_factor.value

    def on_slider_body_spin_changed(self):
        UrsinaConfig.body_spin_factor = self.slider_body_spin_factor.value

三月三net's avatar
三月三net 已提交
176 177 178
    def on_slider_body_size_changed(self):
        UrsinaConfig.body_size_factor = self.slider_body_size_factor.value

三月三net's avatar
三月三net 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    def on_slider_run_speed_changed(self):
        UrsinaConfig.run_speed_factor = self.slider_run_speed_factor.value

    def show_text_time_scale_info(self):
        if self.text_time_scale_info is not None:
            self.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 show_button(self):
        b = Button(scale=(0, .25), text='zzz')

    #  if key == "escape":
    #             if mouse.locked:
    #                 self.on_disable()
    #             else:
    #                 sys.exit()

    # 按空格键则暂停
    def pause_handler_input(self, key):
        import sys
        time_scales = [0.05, 0.1, 0.2, 0.5, 1, 5, 10, 20, 30]
        if key == "escape":
            sys.exit()
        # print(key)
        elif 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]

        self.show_text_time_scale_info()