Advertisement
samuel-roberto

UiShop.py ~ ShopDialog2

Feb 24th, 2015
3,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.84 KB | None | 0 0
  1. class ShopDialog2(ui.ScriptWindow):
  2.  
  3.     def __init__(self):
  4.         ui.ScriptWindow.__init__(self)
  5.         self.tooltipItem = 0
  6.         self.xShopStart = 0
  7.         self.yShopStart = 0
  8.         self.questionDialog = None
  9.         self.popup = None
  10.         self.itemBuyQuestionDialog = None
  11.  
  12.     def __del__(self):
  13.         ui.ScriptWindow.__del__(self)
  14.  
  15.     def Refresh(self):
  16.         getItemID=shop.GetItemID
  17.         getItemCount=shop.GetItemCount
  18.         setItemID=self.itemSlotWindow.SetItemSlot
  19.         for i in xrange(shop.SHOP_SLOT_COUNT):
  20.             itemCount = getItemCount(i)
  21.             if itemCount <= 1:
  22.                 itemCount = 0
  23.             setItemID(i, getItemID(i), itemCount)
  24.  
  25.         wndMgr.RefreshSlot(self.itemSlotWindow.GetWindowHandle())
  26.  
  27.     def SetItemData(self, pos, itemID, itemCount, itemPrice):
  28.         shop.SetItemData(pos, itemID, itemCount, itemPrice)
  29.  
  30.     def LoadDialog(self):
  31.         try:
  32.             PythonScriptLoader = ui.PythonScriptLoader()
  33.             PythonScriptLoader.LoadScriptFile(self, "UIScript/shopdialog2.py")
  34.         except:
  35.             import exception
  36.             exception.Abort("ShopDialog.LoadDialog.LoadObject")
  37.  
  38.         try:
  39.             GetObject = self.GetChild
  40.             self.itemSlotWindow = GetObject("ItemSlot")
  41.             self.btnBuy = GetObject("BuyButton")
  42.             self.btnSell = GetObject("SellButton")
  43.             self.btnClose = GetObject("CloseButton")
  44.             self.titleBar = GetObject("TitleBar")
  45.         except:
  46.             import exception
  47.             exception.Abort("ShopDialog.LoadDialog.BindObject")
  48.  
  49.         self.itemSlotWindow.SetSlotStyle(wndMgr.SLOT_STYLE_NONE)
  50.         self.itemSlotWindow.SAFE_SetButtonEvent("LEFT", "EMPTY", self.SelectEmptySlot)
  51.         self.itemSlotWindow.SAFE_SetButtonEvent("LEFT", "EXIST", self.SelectItemSlot)
  52.         self.itemSlotWindow.SAFE_SetButtonEvent("RIGHT", "EXIST", self.UnselectItemSlot)
  53.  
  54.         self.itemSlotWindow.SetOverInItemEvent(ui.__mem_func__(self.OverInItem))
  55.         self.itemSlotWindow.SetOverOutItemEvent(ui.__mem_func__(self.OverOutItem))
  56.  
  57.         self.btnBuy.SetToggleUpEvent(ui.__mem_func__(self.CancelShopping))
  58.         self.btnBuy.SetToggleDownEvent(ui.__mem_func__(self.OnBuy))
  59.  
  60.         self.btnSell.SetToggleUpEvent(ui.__mem_func__(self.CancelShopping))
  61.         self.btnSell.SetToggleDownEvent(ui.__mem_func__(self.OnSell))
  62.  
  63.         self.btnClose.SetEvent(ui.__mem_func__(self.AskClosePrivateShop))
  64.  
  65.         self.titleBar.SetCloseEvent(ui.__mem_func__(self.Close))
  66.  
  67.         self.Refresh()
  68.  
  69.     def Destroy(self):
  70.         self.Close()
  71.         self.ClearDictionary()
  72.  
  73.         self.tooltipItem = 0
  74.         self.itemSlotWindow = 0
  75.         self.btnBuy = 0
  76.         self.btnSell = 0
  77.         self.btnClose = 0
  78.         self.titleBar = 0
  79.         self.questionDialog = None
  80.         self.popup = None
  81.  
  82.     def Open(self, vid):
  83.  
  84.         isPrivateShop = FALSE
  85.         isMainPlayerPrivateShop = FALSE
  86.  
  87.         import chr
  88.         if chr.IsNPC(vid):
  89.             isPrivateShop = FALSE
  90.         else:
  91.             isPrivateShop = TRUE
  92.  
  93.         if player.IsMainCharacterIndex(vid):
  94.  
  95.             isMainPlayerPrivateShop = TRUE
  96.  
  97.             self.btnBuy.Hide()
  98.             self.btnSell.Hide()
  99.             self.btnClose.Show()
  100.  
  101.         else:
  102.  
  103.             isMainPlayerPrivateShop = FALSE
  104.  
  105.             self.btnBuy.Show()
  106.             self.btnSell.Show()
  107.             self.btnClose.Hide()
  108.  
  109.         shop.Open(isPrivateShop, isMainPlayerPrivateShop)
  110.         self.Refresh()
  111.         self.SetTop()
  112.         self.Show()
  113.  
  114.         (self.xShopStart, self.yShopStart, z) = player.GetMainCharacterPosition()
  115.  
  116.     def Close(self):
  117.         self.OnCloseQuestionDialog()
  118.         shop.Close()
  119.         net.SendShopEndPacket()
  120.         self.CancelShopping()
  121.         self.tooltipItem.HideToolTip()
  122.         self.Hide()
  123.  
  124.     def AskClosePrivateShop(self):
  125.         questionDialog = uiCommon.QuestionDialog()
  126.         questionDialog.SetText(localeInfo.PRIVATE_SHOP_CLOSE_QUESTION)
  127.         questionDialog.SetAcceptEvent(ui.__mem_func__(self.OnClosePrivateShop))
  128.         questionDialog.SetCancelEvent(ui.__mem_func__(self.OnCloseQuestionDialog))
  129.         questionDialog.Open()
  130.         self.questionDialog = questionDialog
  131.  
  132.         return TRUE
  133.  
  134.     def OnClosePrivateShop(self):
  135.         net.SendChatPacket("/close_shop")
  136.         self.OnCloseQuestionDialog()
  137.         return TRUE
  138.  
  139.     def OnPressEscapeKey(self):
  140.         self.Close()
  141.         return TRUE
  142.  
  143.     def OnPressExitKey(self):
  144.         self.Close()
  145.         return TRUE
  146.  
  147.     def OnBuy(self):
  148.         chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.SHOP_BUY_INFO)
  149.         app.SetCursor(app.BUY)
  150.         self.btnSell.SetUp()
  151.  
  152.     def OnSell(self):
  153.         chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.SHOP_SELL_INFO)
  154.         app.SetCursor(app.SELL)
  155.         self.btnBuy.SetUp()
  156.  
  157.     def CancelShopping(self):
  158.         self.btnBuy.SetUp()
  159.         self.btnSell.SetUp()
  160.         app.SetCursor(app.NORMAL)
  161.  
  162.     def __OnClosePopupDialog(self):
  163.         self.pop = None
  164.  
  165.     def SellAttachedItem(self):
  166.  
  167.         if shop.IsPrivateShop():
  168.             mouseModule.mouseController.DeattachObject()
  169.             return
  170.  
  171.         attachedSlotType = mouseModule.mouseController.GetAttachedType()
  172.         attachedSlotPos = mouseModule.mouseController.GetAttachedSlotNumber()
  173.         attachedCount = mouseModule.mouseController.GetAttachedItemCount()
  174.         if player.SLOT_TYPE_INVENTORY == attachedSlotType:
  175.  
  176.             itemIndex = player.GetItemIndex(attachedSlotPos)
  177.             item.SelectItem(itemIndex)
  178.  
  179.             if item.IsAntiFlag(item.ANTIFLAG_SELL):
  180.                 popup = uiCommon.PopupDialog()
  181.                 popup.SetText(localeInfo.SHOP_CANNOT_SELL_ITEM)
  182.                 popup.SetAcceptEvent(self.__OnClosePopupDialog)
  183.                 popup.Open()
  184.                 self.popup = popup
  185.  
  186.             elif player.IsValuableItem(attachedSlotPos):
  187.  
  188.                 itemPrice = item.GetISellItemPrice()
  189.  
  190.                 if item.Is1GoldItem():
  191.                     itemPrice = attachedCount / itemPrice / 5
  192.                 else:
  193.                     itemPrice = itemPrice * max(1, attachedCount) / 5
  194.  
  195.                 itemName = item.GetItemName()
  196.  
  197.                 questionDialog = uiCommon.QuestionDialog()
  198.                 questionDialog.SetText(localeInfo.DO_YOU_SELL_ITEM(itemName, attachedCount, itemPrice))
  199.  
  200.                 questionDialog.SetAcceptEvent(lambda arg1=attachedSlotPos, arg2=attachedCount: self.OnSellItem(arg1, arg2))
  201.                 questionDialog.SetCancelEvent(ui.__mem_func__(self.OnCloseQuestionDialog))
  202.                 questionDialog.Open()
  203.                 self.questionDialog = questionDialog
  204.  
  205.             else:
  206.                 self.OnSellItem(attachedSlotPos, attachedCount)
  207.  
  208.         else:
  209.             snd.PlaySound("sound/ui/loginfail.wav")
  210.  
  211.         mouseModule.mouseController.DeattachObject()
  212.  
  213.     def OnSellItem(self, slotPos, count):
  214.         net.SendShopSellPacketNew(slotPos, count)
  215.         snd.PlaySound("sound/ui/money.wav")
  216.         self.OnCloseQuestionDialog()
  217.  
  218.     def OnCloseQuestionDialog(self):
  219.         if self.questionDialog:
  220.             self.questionDialog.Close()
  221.  
  222.         self.questionDialog = None
  223.  
  224.     def SelectEmptySlot(self, selectedSlotPos):
  225.  
  226.         isAttached = mouseModule.mouseController.isAttached()
  227.         if isAttached:
  228.             self.SellAttachedItem()
  229.  
  230.     def UnselectItemSlot(self, selectedSlotPos):
  231.         if shop.IsPrivateShop():
  232.             self.AskBuyItem(selectedSlotPos)
  233.         else:
  234.             net.SendShopBuyPacket(selectedSlotPos)
  235.  
  236.     def SelectItemSlot(self, selectedSlotPos):
  237.  
  238.         isAttached = mouseModule.mouseController.isAttached()
  239.         if isAttached:
  240.             self.SellAttachedItem()
  241.  
  242.         else:
  243.  
  244.             if TRUE == shop.IsMainPlayerPrivateShop():
  245.                 return
  246.  
  247.             curCursorNum = app.GetCursor()
  248.             if app.BUY == curCursorNum:
  249.                 self.AskBuyItem(selectedSlotPos)
  250.  
  251.             elif app.SELL == curCursorNum:
  252.                 chat.AppendChat(chat.CHAT_TYPE_INFO, localeInfo.SHOP_SELL_INFO)
  253.  
  254.             else:
  255.                 selectedItemID = shop.GetItemID(selectedSlotPos)
  256.                 itemCount = shop.GetItemCount(selectedSlotPos)
  257.  
  258.                 type = player.SLOT_TYPE_SHOP
  259.                 if shop.IsPrivateShop():
  260.                     type = player.SLOT_TYPE_PRIVATE_SHOP
  261.  
  262.                 mouseModule.mouseController.AttachObject(self, type, selectedSlotPos, selectedItemID, itemCount)
  263.                 mouseModule.mouseController.SetCallBack("INVENTORY", ui.__mem_func__(self.DropToInventory))
  264.                 snd.PlaySound("sound/ui/pick.wav")
  265.  
  266.     def DropToInventory(self):
  267.         attachedSlotPos = mouseModule.mouseController.GetAttachedSlotNumber()
  268.         self.AskBuyItem(attachedSlotPos)
  269.  
  270.     def AskBuyItem(self, slotPos):
  271.         itemIndex = shop.GetItemID(slotPos)
  272.         itemPrice = shop.GetItemPrice(slotPos)
  273.         itemCount = shop.GetItemCount(slotPos)
  274.  
  275.         item.SelectItem(itemIndex)
  276.         itemName = item.GetItemName()
  277.  
  278.         itemBuyQuestionDialog = uiCommon.QuestionDialog()
  279.         itemBuyQuestionDialog.SetText(localeInfo.DO_YOU_BUY_ITEM(itemName, itemCount, localeInfo.NumberToMoneyString(itemPrice)))
  280.         itemBuyQuestionDialog.SetAcceptEvent(lambda arg=TRUE: self.AnswerBuyItem(arg))
  281.         itemBuyQuestionDialog.SetCancelEvent(lambda arg=FALSE: self.AnswerBuyItem(arg))
  282.         itemBuyQuestionDialog.Open()
  283.         itemBuyQuestionDialog.pos = slotPos
  284.         self.itemBuyQuestionDialog = itemBuyQuestionDialog
  285.  
  286.     def AnswerBuyItem(self, flag):
  287.  
  288.         if flag:
  289.             pos = self.itemBuyQuestionDialog.pos
  290.             net.SendShopBuyPacket(pos)
  291.  
  292.         self.itemBuyQuestionDialog.Close()
  293.         self.itemBuyQuestionDialog = None
  294.  
  295.     def SetItemToolTip(self, tooltipItem):
  296.         self.tooltipItem = tooltipItem
  297.  
  298.     def OverInItem(self, slotIndex):
  299.         if mouseModule.mouseController.isAttached():
  300.             return
  301.  
  302.         if 0 != self.tooltipItem:
  303.             self.tooltipItem.SetShopItem(slotIndex)
  304.  
  305.     def OverOutItem(self):
  306.         if 0 != self.tooltipItem:
  307.             self.tooltipItem.HideToolTip()
  308.  
  309.     def OnUpdate(self):
  310.  
  311.         USE_SHOP_LIMIT_RANGE = 1000
  312.  
  313.         (x, y, z) = player.GetMainCharacterPosition()
  314.         if abs(x - self.xShopStart) > USE_SHOP_LIMIT_RANGE or abs(y - self.yShopStart) > USE_SHOP_LIMIT_RANGE:
  315.             self.Close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement