Files
m2dev-client/assets/root/uiTeleport.py
2026-04-16 18:33:07 +02:00

174 lines
4.9 KiB
Python

import time
import background
import net
import player
import quest
import ui
class TeleportWindow(ui.BoardWithTitleBar):
TITLE = "Teleport System"
PRESET_BUTTONS = (
(1, "Village 1"),
(2, "Village 2"),
(3, "Valley"),
(4, "Desert"),
(5, "Sohan"),
(6, "Fireland"),
(7, "Devil"),
(8, "Cave"),
)
def __init__(self):
ui.BoardWithTitleBar.__init__(self)
self.lastRefreshTime = 0.0
self.slotRows = []
self.AddFlag("float")
self.AddFlag("movable")
self.SetSize(310, 255)
self.SetTitleName("Teleport")
self.SetCloseEvent(self.Hide)
self.__CreateChildren()
self.Hide()
def __del__(self):
ui.BoardWithTitleBar.__del__(self)
def __CreateChildren(self):
self.statusLine = self.__CreateValueLine(15, 36)
self.mapLine = self.__CreateValueLine(15, 56)
self.coordLine = self.__CreateValueLine(15, 76)
self.slotSummaryLine = self.__CreateValueLine(15, 96)
self.cooldownLine = self.__CreateValueLine(15, 116)
for index, presetData in enumerate(self.PRESET_BUTTONS):
(presetId, label) = presetData
row = index // 4
column = index % 4
button = self.__CreateButton(15 + column * 71, 142 + row * 24, 64, label)
button.SetEvent(self.__SendPresetWarp, presetId)
for slot in range(1, 6):
label = self.__CreateValueLine(15, 196 + (slot - 1) * 11)
saveButton = self.__CreateButton(100, 192 + (slot - 1) * 11, 42, "Save")
saveButton.SetEvent(self.__SendSaveSlot, slot)
useButton = self.__CreateButton(148, 192 + (slot - 1) * 11, 42, "Use")
useButton.SetEvent(self.__SendUseSlot, slot)
self.slotRows.append((label, saveButton, useButton))
def __CreateValueLine(self, x, y):
textLine = ui.TextLine()
textLine.SetParent(self)
textLine.SetPosition(x, y)
textLine.SetOutline()
textLine.Show()
return textLine
def __CreateButton(self, x, y, width, text):
button = ui.Button()
button.SetParent(self)
button.SetPosition(x, y)
button.SetUpVisual("d:/ymir work/ui/public/small_thin_button_01.sub")
button.SetOverVisual("d:/ymir work/ui/public/small_thin_button_02.sub")
button.SetDownVisual("d:/ymir work/ui/public/small_thin_button_03.sub")
button.SetDisableVisual("d:/ymir work/ui/public/small_thin_button_01.sub")
button.SetSize(width, 17)
button.SetText(text)
button.Show()
return button
def __GetTeleportQuestData(self):
questCount = min(quest.GetQuestCount(), quest.QUEST_MAX_NUM)
for questIndex in range(questCount):
(questName, questIcon, questCounterName, questCounterValue) = quest.GetQuestData(questIndex)
if questName != self.TITLE:
continue
maxSlots = 3
if "/" in questCounterName:
parts = questCounterName.split("/")
try:
maxSlots = int(parts[-1].strip())
except:
maxSlots = 3
(clockName, clockValue) = quest.GetQuestLastTime(questIndex)
return {
"savedCount" : max(questCounterValue, 0),
"maxSlots" : max(maxSlots, 3),
"clockName" : clockName,
"clockValue" : max(clockValue, 0),
}
return None
def __FormatCooldown(self, seconds):
return "%02d:%02d" % (seconds // 60, seconds % 60)
def __RefreshSlotRows(self, maxSlots):
for slot, row in enumerate(self.slotRows, 1):
(label, saveButton, useButton) = row
if slot <= maxSlots:
label.SetText("Slot %d" % slot)
saveButton.Enable()
useButton.Enable()
else:
label.SetText("Slot %d (VIP)" % slot)
saveButton.Disable()
useButton.Disable()
def Refresh(self):
teleportData = self.__GetTeleportQuestData()
(x, y, z) = player.GetMainCharacterPosition()
currentMap = background.GetCurrentMapName()
self.mapLine.SetText("Map: %s" % currentMap)
self.coordLine.SetText("Coords: %d, %d" % (x // 100, y // 100))
if not teleportData:
self.statusLine.SetText("Status: Waiting for teleport quest")
self.slotSummaryLine.SetText("Saved: -")
self.cooldownLine.SetText("Cooldown: -")
self.__RefreshSlotRows(3)
return
self.statusLine.SetText("Status: %s" % ("Cooldown" if teleportData["clockValue"] > 0 else "Ready"))
self.slotSummaryLine.SetText("Saved: %d / %d" % (teleportData["savedCount"], teleportData["maxSlots"]))
if teleportData["clockValue"] > 0 and len(teleportData["clockName"]) > 0:
self.cooldownLine.SetText("Cooldown: %s" % self.__FormatCooldown(teleportData["clockValue"]))
else:
self.cooldownLine.SetText("Cooldown: Ready")
self.__RefreshSlotRows(teleportData["maxSlots"])
def Show(self):
self.Refresh()
ui.BoardWithTitleBar.Show(self)
def OnUpdate(self):
currentTime = time.time()
if currentTime - self.lastRefreshTime < 0.2:
return
self.lastRefreshTime = currentTime
self.Refresh()
def __SendTeleportCommand(self, action, arg):
net.SendChatPacket("/teleport_system %s %d" % (action, arg), 0)
self.lastRefreshTime = 0.0
self.Refresh()
def __SendSaveSlot(self, slot):
self.__SendTeleportCommand("save", slot)
def __SendUseSlot(self, slot):
self.__SendTeleportCommand("saved", slot)
def __SendPresetWarp(self, presetId):
self.__SendTeleportCommand("preset", presetId)