Quellcode durchsuchen

优化陈列馆UI

detopoi vor 1 Jahr
Ursprung
Commit
e46fe41260
2 geänderte Dateien mit 155 neuen und 16 gelöschten Zeilen
  1. 41 1
      game/ui/menu/component.py
  2. 114 15
      game/ui/menu/skin_menu.py

+ 41 - 1
game/ui/menu/component.py

@@ -202,7 +202,7 @@ class PygameButton:
         self._render()
 
 
-class PygameButtonBackIMG:
+class PygameButtonBackIMG():
     """
     一个仅使用图片作为按钮的类,不含文字渲染,图片本身包含文字。
     - default_img_path: 默认状态下的背景图路径
@@ -518,3 +518,43 @@ class ButtonParticle:
 
     def is_alive(self):
         return self.life > 0 and self.alpha > 0
+
+
+class PygameIMG:
+    def __init__(self, menu, screen, relx, rely, default_img_path=None, py_img=None, anchor='center'):
+        self.menu = menu
+        self.screen = screen
+        # 加载按钮图片
+        if default_img_path is not None:
+            self.default_img = pygame.image.load(default_img_path).convert_alpha()
+        else:
+            self.default_img = py_img
+        self.relx = relx
+        self.rely = rely
+        self.anchor = anchor  # 'center' 或 'topleft'
+        self.hovered = False
+        # 按钮尺寸使用图片原始尺寸
+        self.width, self.height = self.default_img.get_size()
+        # 计算按钮位置
+        sw, sh = self.screen.get_size()
+        x = int(sw * self.relx)
+        y = int(sh * self.rely)
+        self.x, self.y = x, y
+        if self.anchor == 'center':
+            topleft = (x - self.width//2, y - self.height//2)
+        else:
+            topleft = (x, y)
+        self.bg_rect = pygame.Rect(topleft[0], topleft[1], self.width, self.height)
+
+    def draw(self):
+        # (实时刷新) 根据悬停状态绘制对应图片
+        img = self.default_img
+        self.screen.blit(img, self.bg_rect.topleft)
+
+    def update_image(self, new_default):
+        self.default_img = new_default
+        # self.default_img = pygame.image.load(new_default).convert_alpha()
+        self.draw()
+
+    def handle_event(self, event):
+        pass

+ 114 - 15
game/ui/menu/skin_menu.py

@@ -1,16 +1,14 @@
+import pygame
 import json
 import os
 from datetime import datetime
-import tkinter as tk
 from game.const import game as G
 from game.const import window as W
 from game.const import rouge as R
 from game.sound import sound_player
 from game.utils import local_data_manager as DM
-from game import control
-from game.algo import rougeGen
 from game.file_loader.fileLoader import load_json, load_png, save_encrypt_single_rouge_file, load_rouge_data
-from game.ui.menu.component import PygameButton, PygameButtonBackIMG, PygameButtonBackIMGRoll, Particle
+from game.ui.menu.component import PygameButton, PygameButtonBackIMG, PygameButtonBackIMGRoll, Particle, PygameIMG
 
 current_folder_path = os.getcwd()
 
@@ -30,12 +28,20 @@ cursor_dict = {
 
 cursor_list = ["引路人-黄色荧火", "引路人-森绿荧火", "引路人-小酷比", "引路人-金酷比"]
 
+bomb_cn = {
+    "糖泡-经典": "bomb1",
+    "糖泡-命运": "bomb60",
+    "糖泡-深林": "bomb1436",
+}
+
 bomb_dict = {
-    "bomb1": True,
-    "bomb60": False,
-    "bomb1436": False,
+    "bomb1": {"own": True, "name": "糖泡-经典", "info": "陪伴旅人最初的糖泡外观", "img": pygame.image.load(r"res/img/bomb/bomb1_stand_0_0.png").convert_alpha()},
+    "bomb60": {"own": False, "name": "糖泡-命运", "info": "糖泡伤害额外增加50", "img": pygame.image.load(r"res/img/bomb/bomb60_stand_0_0.png").convert_alpha()},
+    "bomb1436": {"own": False, "name": "糖泡-深林", "info": "最大生命额外增加150", "img": pygame.image.load(r"res/img/bomb/bomb1436_stand_0_0.png").convert_alpha()},
 }
 
+bomb_list = ["糖泡-经典", "糖泡-命运", "糖泡-深林"]
+
 
 class SkinPage:
     def __init__(self):
@@ -74,6 +80,7 @@ class SkinPage:
         cursor_dict["fire_cursor1"]["own"] = True
 
     def load_other_data(self):
+        global bomb_dict
         # 杂项第一次打开
         if not os.path.exists(self.other_file_path):
             self.rougeOB_other_data = {
@@ -104,25 +111,20 @@ class SkinPage:
             save_encrypt_single_rouge_file(self.rougeOB_other_data, "rougeOBOther")
         else:
             self.current_bomb = self.rougeOB_other_data['Bomb_Skin']
+        for bomb in self.rougeOB_other_data['Bomb']:
+            bomb_dict[bomb]["own"] = True
 
         print(f"rougeOB_other_data: {self.rougeOB_other_data}")
 
-        if "bomb60" in self.rougeOB_other_data['Bomb']:
-            has_bomb60 = True
-        if "bomb1436" in self.rougeOB_other_data['Bomb']:
-            has_bomb1436 = True
-
     def draw(self):
         # 引路人
         self.screen.buttons.append(PygameButtonBackIMG(
             self.screen, self.screen.screen, 0.5, 0.3, "res/ui/button/cursor.png", "res/ui/button/cursor_press.png", action=self.draw_cursor, anchor='center')
         )
-
         # 藏宝阁
         self.screen.buttons.append(PygameButtonBackIMG(
             self.screen, self.screen.screen, 0.5, 0.45, "res/ui/button/treasure.png", "res/ui/button/treasure_press.png", action=self.draw_treasure, anchor='center')
         )
-
         # 返回主界面
         self.screen.buttons.append(PygameButtonBackIMG(
             self.screen, self.screen.screen, 0.5, 0.6, "res/ui/button/return.png", "res/ui/button/return_press.png", action=self.screen.refresh, anchor='center')
@@ -179,7 +181,55 @@ class SkinPage:
     def draw_treasure(self):
         self.screen.buttons.clear()
         self.load_other_data()
-        # 返回主界面
+        # ================================= 信息展示 =================================
+        info_button = PygameButton(
+            self.screen.screen, "", 0.5, 0.56, 18, "#2a2a2a",
+            fg_color=(215,220,217), action=None,
+            anchor="center", is_bold=True, fix_shape=(320,80), bg_alpha=159,
+            frosted_glass=True, blur_radius=8, glass_alpha=8, background=self.screen
+        )
+        # ================================= 图片 =================================
+        img_button = PygameButtonBackIMG(
+            self.screen, self.screen.screen, 0.65, 0.29, "res/ui/button/img_board.png", "res/ui/button/img_board.png",
+            action=None, anchor='center', shiny_spark=False
+        )
+        img = PygameIMG(self.screen, self.screen.screen, 0.65, 0.29, py_img=bomb_dict["bomb1"]["img"], anchor='center')
+        # ================================= 选择 =================================
+        choose_button = PygameButtonBackIMG(
+            self.screen, self.screen.screen, 0.34, 0.72, "res/ui/button/choose.png", "res/ui/button/choose_press.png",
+            action=None, anchor='center'
+        )
+        # ================================= 展示框 =================================
+        cy = 0.29
+        cx = 0.35
+        button_blank = 0.13
+        roll_button = PygameButtonBackIMGRoll(
+            self.screen, self.screen.screen, cx, cy, "res/ui/button/cursor_board.png", bomb_list,
+            action=None, roll_action=None, hover_in_action=None, hover_out_action=None,
+            anchor='center', font_name="hk4e_zh-cn.ttf", font_size=24, small_scale=0.7, small_alpha=159, color=(0, 0, 0), shiny_spark=False,
+            related_button=[info_button, choose_button, img],
+            notify_action=[lambda x: bomb_update_notify(roll_button, x, self.current_bomb), lambda _: self.update_choose_bomb_btn(roll_button, choose_button, info_button), lambda _: bomb_img_update(roll_button, img)]
+        )
+        # ================================= 初始化 =================================
+        choose_button.action = lambda: self.choose_bomb(roll_button, info_button, choose_button)  # 追加action
+        roll_button.set_current_text(bomb_dict[self.current_bomb]["name"])
+        bomb_update_notify(roll_button, info_button, self.current_bomb)
+        self.update_choose_bomb_btn(roll_button, choose_button, info_button)
+        # ================================= 添加按钮 =================================
+        self.screen.buttons.append(info_button)
+        self.screen.buttons.append(choose_button)
+        self.screen.buttons.append(roll_button)
+        self.screen.buttons.append(img_button)
+        self.screen.buttons.append(img)
+        # ================================= 上翻页 =================================
+        self.screen.buttons.append(PygameButtonBackIMG(
+            self.screen, self.screen.screen, cx, cy-button_blank, "res/ui/button/up.png", "res/ui/button/up_press.png", action=roll_button.roll_up, anchor='center', shiny_spark=False)
+        )
+        # ================================= 下翻页 =================================
+        self.screen.buttons.append(PygameButtonBackIMG(
+            self.screen, self.screen.screen, cx, cy+button_blank, "res/ui/button/down.png", "res/ui/button/down_press.png", action=roll_button.roll_down, anchor='center', shiny_spark=False)
+        )
+        # ================================= 返回上一级 =================================
         self.screen.buttons.append(PygameButtonBackIMG(
             self.screen, self.screen.screen, 0.66, 0.72, "res/ui/button/return.png", "res/ui/button/return_press.png", action=self.screen.open_setting, anchor='center')
         )
@@ -271,6 +321,40 @@ class SkinPage:
             choose_btn.shiny_spark = False
             choose_btn.action = None
 
+    def choose_bomb(self, roll_btn: PygameButtonBackIMGRoll, info_button, choose_btn):
+        cn_name = roll_btn.get_current_text()
+        current = bomb_cn[cn_name]
+        self.current_bomb = current
+        R.bomb_skin = current
+        self.rougeOB_other_data['Bomb_Skin'] = current
+        save_encrypt_single_rouge_file(self.rougeOB_other_data, "rougeOBOther")
+        roll_btn.set_current_text(bomb_dict[self.current_bomb]["name"])
+        bomb_update_notify(roll_btn, info_button, self.current_bomb)
+        choose_btn.update_image("res/ui/button/chosen.png", "res/ui/button/chosen.png")
+        choose_btn.shiny_spark = False
+        choose_btn.action = None
+
+    def update_choose_bomb_btn(self, roll_button, choose_btn, info_button):
+        # 拿到当前展示的中文名
+        cn_name = roll_button.get_current_text()
+        # 拿到内部 key
+        key = bomb_cn[cn_name]
+        # 拿到是否已解锁
+        owned = bomb_dict[key]["own"]
+        if owned:
+            if self.current_bomb == key:
+                choose_btn.update_image("res/ui/button/chosen.png", "res/ui/button/chosen.png")
+                choose_btn.shiny_spark = False
+                choose_btn.action = None
+            else:
+                choose_btn.update_image("res/ui/button/choose.png", "res/ui/button/choose_press.png")
+                choose_btn.shiny_spark = True
+                choose_btn.action = lambda cn=cn_name: self.choose_bomb(roll_button, info_button, choose_btn)
+        else:
+            choose_btn.update_image("res/ui/button/unlock.png", "res/ui/button/unlock.png")
+            choose_btn.shiny_spark = False
+            choose_btn.action = None
+
 
 def cursor_update_notify(board: PygameButtonBackIMGRoll, observer: PygameButton, chosen):
     cn_name = board.get_current_text()
@@ -281,3 +365,18 @@ def cursor_update_notify(board: PygameButtonBackIMGRoll, observer: PygameButton,
     condition = cursor_dict[current]["condition"]
     observer.set_text(f" {is_chosen}{cn_name} \n {info} \n {condition}({owned}) ")
 
+
+def bomb_update_notify(board: PygameButtonBackIMGRoll, observer: PygameButton, chosen):
+    cn_name = board.get_current_text()
+    current = bomb_cn[cn_name]
+    is_chosen = "<当前选择>" if (current == chosen) else ""
+    owned = "已拥有" if bomb_dict[current]["own"] else "未解锁"
+    info = bomb_dict[current]["info"]
+    observer.set_text(f" {is_chosen}{cn_name} \n {info} \n ({owned}) ")
+
+
+def bomb_img_update(board: PygameButtonBackIMGRoll, img: PygameIMG):
+    cn_name = board.get_current_text()
+    current = bomb_cn[cn_name]
+    img.update_image(bomb_dict[current]["img"])
+