home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / Widgets / KeybWidget.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-27  |  18.9 KB  |  410 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import MenuWidget
  5. import ScorerWidgets
  6. import Raster
  7. import BInput
  8. import BUIx
  9. import MenuText
  10. import ListWidget
  11. import pdb
  12. import math
  13. import Raster
  14. import Bladex
  15. import acts
  16. import netwidgets
  17. AdditionalKeysCallBack = None
  18. DefInfoText = 'Press ENTER to define key, DELETE to delete bindings, ESC to exit'
  19. ActionDescriptor = {
  20.     'Attack': 'Used to kill, mutilate and destroy',
  21.     'Forwards': 'Double-tap FORWARDS to Run.',
  22.     'Backwards': 'Press FORWARDS and BACKWARDS simultaneously to turn 180 degrees.',
  23.     'Select Enemy': 'Press SELECT ENEMY to lock the movement around an enemy',
  24.     'Use': 'To pick up an object, press USE',
  25.     'Jump': 'Press JUMP while running to perform a long jump.',
  26.     'Throw': 'While holding down THROW, press ATTACK.' }
  27.  
  28. class B_ControlItemText(MenuWidget.B_MenuItemText):
  29.     
  30.     def __init__(self, Parent, MenuDescr, StackMenu, font_server = ScorerWidgets.font_server):
  31.         MenuWidget.B_MenuItemText.__init__(self, Parent, MenuDescr, StackMenu, font_server)
  32.         self.SetDrawFunc(self.Draw)
  33.         self.SetAlpha(1.0)
  34.         self.ChangingKey = 0
  35.         self.SelectionFilterUpdated = 0
  36.         self.FilterUpdated = 0
  37.         self.SetColor(220, 10, 10)
  38.         self._foc = -1
  39.         self.ActionName = None
  40.  
  41.     
  42.     def SetText(self, text, ActionName = None):
  43.         MenuWidget.B_MenuItemText.SetText(self, text)
  44.         self.SelectionFilterUpdated = 0
  45.         self.FilterUpdated = 0
  46.         self.ActionName = ActionName
  47.  
  48.     
  49.     def Draw(self, x, y, time):
  50.         if self.GetVisible() == 0:
  51.             return None
  52.         
  53.         foc = self.GetHasFocus()
  54.         if foc != self._foc:
  55.             self.SelectionFilterUpdated = 0
  56.             self.FilterIncX = 0
  57.             self.FilterUpdated = 0
  58.         
  59.         self._foc = foc
  60.         (w, h) = self.GetSize()
  61.         if foc:
  62.             if self.ActionName:
  63.                 if ActionDescriptor.has_key(self.ActionName):
  64.                     netwidgets.LabelName = MenuText.GetMenuText(ActionDescriptor[self.ActionName])
  65.                 else:
  66.                     netwidgets.LabelName = ' '
  67.             
  68.             if self.ChangingKey:
  69.                 mult = math.cos(time * 2)
  70.                 self.SetColor(80 * mult + 150, 80 * mult + 150, 45 * mult + 105)
  71.                 self.DefDraw(x, y, time)
  72.                 return None
  73.             elif self.SelectionFilterUpdated:
  74.                 Raster.SetPosition(x - self.FilterIncX, y - self.FilterIncY)
  75.                 Raster.DrawImage(w + 2 * self.FilterIncX, h + 2 * self.FilterIncY, 'RGB', 'Native', self.SelectionFilter.GetImageBuffer())
  76.                 self.SetColor(220, 220, 150)
  77.                 self.DefDraw(x, y, time)
  78.                 self.SetColor(220, 10, 10)
  79.                 return None
  80.             
  81.         else:
  82.             self.DefDraw(x, y, time)
  83.             return None
  84.         if foc:
  85.             self.SetColor(250, 250, 250)
  86.             self.DefDraw(x, y, time)
  87.             self.SetColor(220, 10, 10)
  88.             if self.FilterIncX > 25:
  89.                 self.SelectionFilterUpdated = 1
  90.             else:
  91.                 self.SelectionFilterUpdated = 0
  92.         
  93.  
  94.  
  95.  
  96. class B_ControlItemTexts(BUIx.B_FrameWidget, MenuWidget.B_MenuTreeItem):
  97.     
  98.     def __init__(self, Parent, MenuDescr, StackMenu):
  99.         BUIx.B_FrameWidget.__init__(self, Parent, MenuDescr['Name'], 400, 15)
  100.         MenuWidget.B_MenuTreeItem.__init__(self, MenuDescr, StackMenu)
  101.         self.wActionName = B_ControlItemText(self, MenuDescr, StackMenu)
  102.         self.wActionKeys = B_ControlItemText(self, MenuDescr, StackMenu)
  103.         self.AddWidget(self.wActionName, 0, 0, BUIx.B_FrameWidget.B_FR_AbsoluteLeft, BUIx.B_FrameWidget.B_FR_Left, BUIx.B_FrameWidget.B_FR_AbsoluteTop, BUIx.B_FrameWidget.B_FR_Top)
  104.         self.AddWidget(self.wActionKeys, 0, 0, BUIx.B_FrameWidget.B_FR_AbsoluteRight, BUIx.B_FrameWidget.B_FR_Right, BUIx.B_FrameWidget.B_FR_AbsoluteTop, BUIx.B_FrameWidget.B_FR_Top)
  105.         self.HasFocus = 0
  106.         self.SetClipDraw(0)
  107.  
  108.     
  109.     def __del__(self):
  110.         BUIx.B_FrameWidget.__del__(self)
  111.         MenuWidget.B_MenuTreeItem.__del__(self)
  112.  
  113.     
  114.     def SetHasFocus(self, foc):
  115.         self.HasFocus = foc
  116.         self.wActionName.SetHasFocus(foc)
  117.         self.wActionKeys.SetHasFocus(foc)
  118.  
  119.     
  120.     def GetHasFocus(self, foc):
  121.         return self.HasFocus
  122.  
  123.  
  124.  
  125. class ControlMenuItem(B_ControlItemTexts):
  126.     
  127.     def __init__(self, Parent, MenuDescr, StackMenu):
  128.         B_ControlItemTexts.__init__(self, Parent, MenuDescr, StackMenu)
  129.         self.ActionName = MenuDescr['Action']
  130.         self.IManager = BInput.GetInputManager()
  131.         oldInputActionsSet = self.IManager.GetInputActionsSet()
  132.         self.IManager.SetInputActionsSet('Default')
  133.         IActions = self.IManager.GetInputActions()
  134.         self.IAction = IActions.Find(self.ActionName)
  135.         self.KeyBounded = []
  136.         self.Parent = Parent
  137.         self.Extras = MenuDescr['kFlags']
  138.         if self.IAction.Name() != 'NULL':
  139.             for i in range(self.IAction.nInputEvents()):
  140.                 IEvent = self.IAction.GetnInputEvent(i)
  141.             
  142.         else:
  143.             print 'Can\xb4t find', self.ActionName
  144.         self.BaseText = MenuDescr['Name']
  145.         self.wActionName.SetText(self.BaseText, self.ActionName)
  146.         self.wActionKeys.SetText(str(self.KeyBounded))
  147.         self.RecalcLayout()
  148.         self.IManager.SetInputActionsSet(oldInputActionsSet)
  149.         self.ListenerName = MenuDescr['Name'] + ' Listener'
  150.         self.Listener = BInput.B_InputListener(self.ListenerName)
  151.         self.Listener.SetPythonFunc(self.ListenDevice)
  152.         self.MouseListenerName = MenuDescr['Name'] + ' MouseListener'
  153.         self.MouseListener = BInput.B_InputListener(self.MouseListenerName)
  154.         self.MouseListener.SetPythonFunc(self.ListenMouseDevice)
  155.         self.PadListenerName = MenuDescr['Name'] + ' PadListener'
  156.         self.PadListener = BInput.B_InputListener(self.PadListenerName)
  157.         self.PadListener.SetPythonFunc(self.ListenPadDevice)
  158.  
  159.     
  160.     def BeginDefineKey(self):
  161.         if len(self.KeyBounded) >= 3:
  162.             self.SetStatusText(MenuText.GetMenuText('Maximun number of key bindings reached.'))
  163.             return None
  164.         
  165.         self.SetStatusText(MenuText.GetMenuText('Press desired key, ESC to end'))
  166.         keyb = self.IManager.GetAttachedDevice('Keyboard')
  167.         if keyb.this != 'NULL':
  168.             keyb.AddListener(self.Listener)
  169.         
  170.         keyb = self.IManager.GetAttachedDevice('Mouse')
  171.         if keyb.this != 'NULL':
  172.             keyb.AddListener(self.MouseListener)
  173.         
  174.         keyb = self.IManager.GetAttachedDevice('Pad')
  175.         if keyb.this != 'NULL':
  176.             keyb.AddListener(self.PadListener)
  177.         
  178.         self.oldInputActionsSet = self.IManager.GetInputActionsSet()
  179.         self.IManager.SetInputActionsSet('MenuRedefine')
  180.         self.wActionName.ChangingKey = 1
  181.         self.wActionKeys.ChangingKey = 1
  182.  
  183.     
  184.     def EndDefineKey(self):
  185.         self.SetStatusText(MenuText.GetMenuText(DefInfoText))
  186.         self.IManager.SetInputActionsSet(self.oldInputActionsSet)
  187.         keyb = self.IManager.GetAttachedDevice('Keyboard')
  188.         if keyb.this != 'NULL':
  189.             keyb.RemoveListener(self.ListenerName)
  190.         
  191.         keyb = self.IManager.GetAttachedDevice('Mouse')
  192.         if keyb.this != 'NULL':
  193.             keyb.RemoveListener(self.MouseListenerName)
  194.         
  195.         keyb = self.IManager.GetAttachedDevice('Pad')
  196.         if keyb.this != 'NULL':
  197.             keyb.RemoveListener(self.PadListenerName)
  198.         
  199.         self.wActionName.ChangingKey = 0
  200.         self.wActionName.SelectionFilterUpdated = 0
  201.         self.wActionName.FilterUpdated = 0
  202.         self.wActionKeys.ChangingKey = 0
  203.         self.wActionKeys.SelectionFilterUpdated = 0
  204.         self.wActionKeys.FilterUpdated = 0
  205.  
  206.     
  207.     def ListenDevice(self, x, y, z):
  208.         if z == 1.0:
  209.             if x == 'Esc':
  210.                 self.EndDefineKey()
  211.             elif x not in self.KeyBounded:
  212.                 keyb = self.IManager.GetAttachedDevice('Keyboard')
  213.                 self.IManager.SetInputActionsSet('Default')
  214.                 if keyb.IsBinded(x):
  215.                     self.SetStatusText(MenuText.GetMenuText('The key <') + x + MenuText.GetMenuText('> is already used!'))
  216.                 elif len(self.KeyBounded) >= 3:
  217.                     self.SetStatusText(MenuText.GetMenuText('You already have 3 keys assigned to that action'))
  218.                 else:
  219.                     self.IAction.AddEvent(keyb, x, 1)
  220.                     for k in self.Extras:
  221.                         if k[0] == 'Press':
  222.                             self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 1)
  223.                         elif k[0] == 'Release':
  224.                             self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 0)
  225.                         else:
  226.                             print "ERROR : '", k[0], "' is not defined yet!"
  227.                     
  228.                     self.KeyBounded.append(x)
  229.                     self.IManager.SetInputActionsSet('MenuRedefine')
  230.                     self.wActionKeys.SetText(str(self.KeyBounded))
  231.                     self.RecalcLayout()
  232.                     self.EndDefineKey()
  233.             
  234.         
  235.  
  236.     
  237.     def ListenMouseDevice(self, x, y, z):
  238.         if x != 'X_Axis' and x != 'Y_Axis' and x != 'Z_Axis':
  239.             if x not in self.KeyBounded:
  240.                 keyb = self.IManager.GetAttachedDevice('Mouse')
  241.                 self.IManager.SetInputActionsSet('Default')
  242.                 if keyb.IsBinded(x) or len(self.KeyBounded) >= 3:
  243.                     self.SetStatusText(MenuText.GetMenuText('The mouse action <') + x + MenuText.GetMenuText('> is already used!'))
  244.                 else:
  245.                     self.IAction.AddEvent(keyb, x, 1)
  246.                     for k in self.Extras:
  247.                         if k[0] == 'Press':
  248.                             self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 1)
  249.                         elif k[0] == 'Release':
  250.                             self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 0)
  251.                         else:
  252.                             print "ERROR : '", k[0], "' is not defined yet!"
  253.                     
  254.                     self.KeyBounded.append(x)
  255.                     self.IManager.SetInputActionsSet('MenuRedefine')
  256.                     self.wActionKeys.SetText(str(self.KeyBounded))
  257.                     self.RecalcLayout()
  258.                     self.EndDefineKey()
  259.             
  260.         
  261.  
  262.     
  263.     def ListenPadDevice(self, x, y, z):
  264.         if x not in self.KeyBounded:
  265.             keyb = self.IManager.GetAttachedDevice('Pad')
  266.             self.IManager.SetInputActionsSet('Default')
  267.             if keyb.IsBinded(x) or len(self.KeyBounded) >= 3:
  268.                 self.SetStatusText(MenuText.GetMenuText('The pad action <') + x + MenuText.GetMenuText('> is already used!'))
  269.             else:
  270.                 self.IAction.AddEvent(keyb, x, 1)
  271.                 for k in self.Extras:
  272.                     if k[0] == 'Press':
  273.                         self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 1)
  274.                     elif k[0] == 'Release':
  275.                         self.IManager.GetInputActions().Find(k[1]).AddEvent(keyb, x, 0)
  276.                     else:
  277.                         print "ERROR : '", k[0], "' is not defined yet!"
  278.                 
  279.                 self.KeyBounded.append(x)
  280.                 self.IManager.SetInputActionsSet('MenuRedefine')
  281.                 self.wActionKeys.SetText(str(self.KeyBounded))
  282.                 self.RecalcLayout()
  283.                 self.EndDefineKey()
  284.         
  285.  
  286.     
  287.     def SuprMenuItem(self):
  288.         self.IManager.SetInputActionsSet('Default')
  289.         self.IAction.RemoveAllEvents()
  290.         for k in self.Extras:
  291.             if k[0] == 'Press':
  292.                 self.IManager.GetInputActions().Find(k[1]).RemoveAllEvents()
  293.             elif k[0] == 'Release':
  294.                 self.IManager.GetInputActions().Find(k[1]).RemoveAllEvents()
  295.             else:
  296.                 print "ERROR : '", k[0], "' is not defined yet!"
  297.         
  298.         self.KeyBounded = []
  299.         self.wActionKeys.SetText('')
  300.         self.wActionKeys.SelectionFilterUpdated = 0
  301.         self.wActionKeys.FilterUpdated = 0
  302.         self.RecalcLayout()
  303.         self.IManager.SetInputActionsSet('Menu')
  304.  
  305.     
  306.     def SetStatusText(self, text):
  307.         self.Parent.SetStatusText(text)
  308.  
  309.     
  310.     def FinalRelease(self):
  311.         self.Parent = None
  312.  
  313.  
  314. MOUSE_CONTROLS = [
  315.     'RightButton',
  316.     'LeftButton',
  317.     'MiddleButton',
  318.     'OtherButton']
  319.  
  320. class B_KeybListWidget(ListWidget.B_ListWidget):
  321.     
  322.     def __init__(self, Parent, Menudesc, StackMenu, VertPos = 0):
  323.         ListWidget.B_ListWidget.__init__(self, Parent, Menudesc, StackMenu, VertPos)
  324.         self.StatusText = BUIx.B_TextWidget(self, 'Status', MenuText.GetMenuText(DefInfoText), ScorerWidgets.font_server, '..\\..\\Data\\Letras menu peq.bmp')
  325.         self.StatusText.SetColor(200, 200, 200)
  326.         self.StatusText.SetAlpha(1)
  327.         self.AddLabel(self.StatusText, 0.5, 25, BUIx.B_Widget.B_LAB_HCenter, BUIx.B_Widget.B_LAB_Bottom, BUIx.B_Widget.B_FR_HRelative, BUIx.B_Widget.B_LAB_HCenter, BUIx.B_Widget.B_FR_AbsoluteTop, BUIx.B_Widget.B_FR_Top)
  328.         self.DownArrow.SetAlpha(1)
  329.         self.SetClipDraw(1)
  330.         self.SetDrawFunc(self.Draw)
  331.  
  332.     
  333.     def __del__(self):
  334.         SaveListConfig()
  335.         if AdditionalKeysCallBack:
  336.             AdditionalKeysCallBack()
  337.         
  338.         ListWidget.B_ListWidget.__del__(self)
  339.  
  340.     
  341.     def Draw(self, x, y, time):
  342.         self.SetClipDraw(1)
  343.         self.DefDraw(x, y, time)
  344.  
  345.     
  346.     def SetStatusText(self, text):
  347.         self.StatusText.SetText(text)
  348.         self.RecalcLabelLayout(BUIx.B_Widget.B_LAB_HCenter, BUIx.B_Widget.B_LAB_Bottom)
  349.  
  350.     
  351.     def FinalRelease(self):
  352.         ListWidget.B_ListWidget.FinalRelease(self)
  353.  
  354.  
  355.  
  356. def SaveReleaseKey(cfgfile, Action, key, adaction, device):
  357.     text = 'Bladex.AssocKey("%s","%s","%s",ON_RELEASE)\n' % (adaction, device, key)
  358.     cfgfile.write(text)
  359.  
  360.  
  361. def NewAction(cfgfile, Action, key, adaction, device):
  362.     text = 'Bladex.AssocKey("%s","%s","%s")\n' % (adaction, device, key)
  363.     cfgfile.write(text)
  364.  
  365.  
  366. def SaveListConfig():
  367.     cfgfile = open('..\\..\\Scripts\\Control.py', 'w')
  368.     cfgfile.write('\n\n# File generated automatically\n')
  369.     cfgfile.write('# DO NOT EDIT: Changes will be lost\n\n\n')
  370.     cfgfile.write('ON_RELEASE=0\n')
  371.     cfgfile.write('ON_PRESS=1\t# default\n\n\n\n')
  372.     cfgfile.write('import BInput\n')
  373.     cfgfile.write('InputManager=BInput.GetInputManager()\n')
  374.     cfgfile.write('InputManager.SetInputActionsSet("Default")  # Me aseguro de definir las acciones en el grupo correcto\n\n\n')
  375.     IManager = BInput.GetInputManager()
  376.     oldInputActionsSet = IManager.GetInputActionsSet()
  377.     IManager.SetInputActionsSet('Default')
  378.     IActions = IManager.GetInputActions()
  379.     for i in acts.ConfigurableActions:
  380.         IAction = IActions.Find(i[1])
  381.         if IAction.Name() == 'NULL':
  382.             print 'Can\xb4t find', self.ActionName
  383.             continue
  384.         
  385.         for j in range(IAction.nInputEvents()):
  386.             IEvent = IAction.GetnInputEvent(j)
  387.             IDevice = IEvent.GetDevice()
  388.             if IDevice == 'Keyboard' and IDevice == 'Mouse' or IDevice == 'Pad':
  389.                 text = 'Bladex.AssocKey("%s","%s","%s")\n' % (IAction.Name(), IDevice, IEvent.GetKey())
  390.                 cfgfile.write(text)
  391.                 for k in i[2]:
  392.                     if k[0] == 'Release':
  393.                         SaveReleaseKey(cfgfile, IAction.Name(), IEvent.GetKey(), k[1], IDevice)
  394.                     elif k[0] == 'Press':
  395.                         NewAction(cfgfile, IAction.Name(), IEvent.GetKey(), k[1], IDevice)
  396.                     else:
  397.                         print "ERROR: '" + k[0] + ' is not defined!'
  398.                 
  399.             
  400.         
  401.     
  402.     IManager.SetInputActionsSet(oldInputActionsSet)
  403.     cfgfile.write('\n# Mouse stuff\nBladex.AssocKey("RotateX","Mouse","X_Axis")\nBladex.AssocKey("RotateY","Mouse","Y_Axis")\n')
  404.     MouseData = Bladex.GetMouseState()
  405.     text = 'Bladex.SetMouseState(%i,%f,%f)\n' % (MouseData[0], MouseData[1], MouseData[2])
  406.     cfgfile.write(text)
  407.     cfgfile.write('\n# Have a nice day.\n\n\n')
  408.     cfgfile.close()
  409.  
  410.