Files
AVA2/upstream/python-root/ui_mobile_widgets.py
T

251 lines
8.5 KiB
Python

# -*- coding: utf-8 -*-
# 移动端 HUD 的通用控件(docs/MOBILE-ARCHITECTURE.md 第 4 步)。
# 圆形边框由原生 wndMgr.RegisterDisc 用几何绘制(渐变底 + 上亮下暗的立体边),不依赖贴图;
# 图标只用 AVA2 自己的 UI/技能/物品贴图。
import ui
import grp
import wndMgr
import player
import skill
import item
import constInfo
import mouseModule
def Color(r, g, b, a=1.0):
return grp.GenerateColor(r, g, b, a)
# 配色:暗底 + 古铜边,按下变亮;攻击键暗红
STYLE_FRAME = (Color(0.16, 0.14, 0.13, 0.80), Color(0.05, 0.04, 0.04, 0.85), Color(0.85, 0.68, 0.40), Color(0.32, 0.22, 0.12), 3.0)
STYLE_FRAME_DOWN = (Color(0.40, 0.32, 0.22, 0.90), Color(0.12, 0.09, 0.06, 0.90), Color(1.0, 0.85, 0.50), Color(0.45, 0.32, 0.16), 3.0)
STYLE_ATTACK = (Color(0.55, 0.12, 0.08, 0.85), Color(0.18, 0.03, 0.02, 0.90), Color(0.92, 0.74, 0.42), Color(0.36, 0.22, 0.10), 5.0)
STYLE_ATTACK_DOWN = (Color(0.90, 0.30, 0.15, 0.95), Color(0.40, 0.08, 0.04, 0.95), Color(1.0, 0.90, 0.55), Color(0.50, 0.32, 0.14), 5.0)
STYLE_JOY_BASE = (Color(0.10, 0.10, 0.12, 0.25), Color(0.05, 0.05, 0.06, 0.45), Color(0.80, 0.66, 0.42, 0.55), Color(0.30, 0.22, 0.12, 0.55), 3.0)
STYLE_JOY_KNOB = (Color(0.75, 0.66, 0.50, 0.70), Color(0.35, 0.28, 0.18, 0.75), Color(1.0, 0.88, 0.60, 0.85), Color(0.40, 0.30, 0.16, 0.85), 2.0)
COOLDOWN_COLOR = Color(0.0, 0.0, 0.0, 0.55)
class Disc(ui.Window):
"""圆形边框(原生 CDisc)。不接收点击。"""
def __init__(self, layer="UI"):
ui.Window.__init__(self, layer)
self.AddFlag("not_pick")
def __del__(self):
ui.Window.__del__(self)
def RegisterWindow(self, layer):
self.hWnd = wndMgr.RegisterDisc(self, layer)
def SetStyle(self, style):
inner, outer, rimTop, rimBottom, rimWidth = style
wndMgr.SetDiscColors(self.hWnd, inner, outer, rimTop, rimBottom, float(rimWidth))
def SetArc(self, fraction, color=COOLDOWN_COLOR):
wndMgr.SetDiscArc(self.hWnd, float(fraction), color)
def MakeDisc(parent, x, y, diameter, style):
disc = Disc()
disc.SetParent(parent)
disc.SetPosition(x, y)
disc.SetSize(diameter, diameter)
disc.SetStyle(style)
disc.Show()
return disc
class RoundButton(ui.Window):
"""圆形按钮:圆框 + 居中图标(AVA2 贴图)或短文字。按下变亮,松开时在按钮内才触发。"""
def __init__(self, diameter, style=STYLE_FRAME, downStyle=STYLE_FRAME_DOWN):
ui.Window.__init__(self)
self.SetSize(diameter, diameter)
self.diameter = diameter
self.style = style
self.downStyle = downStyle
self.event = None
self.disc = MakeDisc(self, 0, 0, diameter, style)
self.icon = None
self.label = None
def __del__(self):
ui.Window.__del__(self)
def SetIcon(self, fileName, scale=1.0):
icon = ui.ExpandedImageBox()
icon.SetParent(self)
icon.AddFlag("not_pick")
try:
icon.LoadImage(fileName)
except:
return False
if scale != 1.0:
icon.SetScale(scale, scale)
# ExpandedImageBox.SetScale 已把窗口尺寸换成缩放后的大小
w = icon.GetWidth()
h = icon.GetHeight()
icon.SetPosition((self.diameter - w) / 2, (self.diameter - h) / 2)
icon.Show()
self.icon = icon
return True
def SetLabel(self, text):
label = ui.TextLine()
label.SetParent(self)
label.AddFlag("not_pick")
label.SetPosition(self.diameter / 2, self.diameter / 2 - 6)
label.SetHorizontalAlignCenter()
label.SetOutline()
label.SetText(text)
label.Show()
self.label = label
def SetEvent(self, event):
self.event = event
def OnMouseLeftButtonDown(self):
self.disc.SetStyle(self.downStyle)
return True
def OnMouseLeftButtonUp(self):
self.disc.SetStyle(self.style)
if self.IsIn() and self.event:
self.event()
return True
class ShortcutHitArea(ui.Window):
def __init__(self, event):
ui.Window.__init__(self)
self.event = event
def OnMouseLeftButtonUp(self):
self.event(0)
return True
class QuickSlotDisc(ui.Window):
"""一个快捷栏格子:圆框 + 原版 SlotWindow 绘制技能/物品/表情图标、数量和冷却。
slotIndex 为 None 时显示当前快捷页的 localIndex(与 PC 任务栏 1-4/F1-F4 同一份数据);
否则为固定的全局格子(药水栏)。轻点直接使用;手上拖着图标时轻点则放入该格。"""
SLOT_SIZE = 32
def __init__(self, diameter, localIndex=None, globalIndex=None):
ui.Window.__init__(self)
self.AddFlag("not_pick")
self.SetSize(diameter, diameter)
self.localIndex = localIndex
self.globalIndex = globalIndex
self.disc = MakeDisc(self, 0, 0, diameter, STYLE_FRAME)
self.hit_area = ShortcutHitArea(ui.__mem_func__(self.__OnSelectItem))
self.hit_area.SetParent(self)
self.hit_area.SetSize(diameter, diameter)
self.hit_area.GetTouchSkillSlot = ui.__mem_func__(self.GetTouchSkillSlot)
self.hit_area.Show()
off = (diameter - self.SLOT_SIZE) / 2
slot = ui.SlotWindow()
slot.SetParent(self)
slot.SetPosition(0, 0)
slot.SetSize(diameter, diameter)
slot.AppendSlot(0, off, off, self.SLOT_SIZE, self.SLOT_SIZE)
slot.SetSlotStyle(wndMgr.SLOT_STYLE_NONE)
slot.SetSelectEmptySlotEvent(ui.__mem_func__(self.__OnSelectEmpty))
slot.SetSelectItemSlotEvent(ui.__mem_func__(self.__OnSelectItem))
slot.Show()
self.slot = slot
slot.GetTouchSkillSlot = ui.__mem_func__(self.GetTouchSkillSlot)
def __del__(self):
ui.Window.__del__(self)
def GetQuickSlot(self):
if self.globalIndex is not None:
return player.GetGlobalQuickSlot(self.globalIndex)
return player.GetLocalQuickSlot(self.localIndex)
def GetTouchSkillSlot(self):
if mouseModule.mouseController.isAttached():
return -1
(Type, Position) = self.GetQuickSlot()
return Position if Type == player.SLOT_TYPE_SKILL and player.GetSkillIndex(Position) else -1
def __Use(self):
if self.globalIndex is not None:
player.RequestUseGlobalQuickSlot(self.globalIndex)
else:
player.RequestUseLocalQuickSlot(self.localIndex)
# uitaskbar.TaskBar.AddQuickSlot,目标换成本格
def __Add(self):
attachedType = mouseModule.mouseController.GetAttachedType()
attachedSlot = mouseModule.mouseController.GetAttachedSlotNumber()
attachedItem = mouseModule.mouseController.GetAttachedItemIndex()
if player.SLOT_TYPE_QUICK_SLOT == attachedType:
if self.globalIndex is not None:
player.RequestMoveGlobalQuickSlotToGlobalQuickSlot(attachedSlot, self.globalIndex)
else:
player.RequestMoveGlobalQuickSlotToLocalQuickSlot(attachedSlot, self.localIndex)
else:
pos = attachedItem if player.SLOT_TYPE_EMOTION == attachedType else attachedSlot
if player.SLOT_TYPE_INVENTORY == attachedType and not item.CanAddToQuickSlotItem(player.GetItemIndex(attachedSlot)):
pos = None
if pos is not None:
if self.globalIndex is not None:
player.RequestAddGlobalQuickSlot(self.globalIndex, attachedType, pos)
else:
player.RequestAddLocalQuickSlot(self.localIndex, attachedType, pos)
mouseModule.mouseController.DeattachObject()
self.Refresh()
def __OnSelectEmpty(self, slotIndex):
if mouseModule.mouseController.isAttached():
self.__Add()
def __OnSelectItem(self, slotIndex):
if mouseModule.mouseController.isAttached():
self.__Add()
else:
self.__Use()
# uitaskbar.TaskBar.RefreshQuickSlot 的单格版本
def Refresh(self):
slot = self.slot
(Type, Position) = self.GetQuickSlot()
if player.SLOT_TYPE_NONE == Type:
slot.ClearSlot(0)
elif player.SLOT_TYPE_INVENTORY == Type:
itemIndex = player.GetItemIndex(Position)
itemCount = player.GetItemCount(Position)
if itemCount <= 1:
itemCount = 0
if constInfo.IS_AUTO_POTION(itemIndex):
if 0 != int(player.GetItemMetinSocket(Position, 0)):
slot.ActivateSlot(0)
else:
slot.DeactivateSlot(0)
slot.SetItemSlot(0, itemIndex, itemCount)
elif player.SLOT_TYPE_SKILL == Type:
skillIndex = player.GetSkillIndex(Position)
if 0 == skillIndex:
slot.ClearSlot(0)
else:
if skill.SKILL_TYPE_GUILD == skill.GetSkillType(skillIndex):
import guild
skillGrade = 0
skillLevel = guild.GetSkillLevel(Position)
else:
skillGrade = player.GetSkillGrade(Position)
skillLevel = player.GetSkillLevel(Position)
slot.SetSkillSlotNew(0, skillIndex, skillGrade, skillLevel)
slot.SetSlotCountNew(0, skillGrade, skillLevel)
if player.IsSkillCoolTime(Position):
(coolTime, elapsedTime) = player.GetSkillCoolTime(Position)
slot.SetSlotCoolTime(0, coolTime, elapsedTime)
if player.IsSkillActive(Position):
slot.ActivateSlot(0)
else:
slot.DeactivateSlot(0)
elif player.SLOT_TYPE_EMOTION == Type:
slot.SetEmotionSlot(0, Position)
slot.SetSlotCount(0, 0)
slot.RefreshSlot()