Added custom hotkey support

This commit is contained in:
Gyedo Jeon 2009-11-10 00:57:31 +00:00
parent df47bb697e
commit 8c3ebf7a52
3 changed files with 178 additions and 8 deletions

View File

@ -0,0 +1,159 @@
import wx
from wx.lib.scrolledpanel import ScrolledPanel
class EditHotKeyDialog(wx.Dialog):
def __init__(self, parent, id, title, key):
wx.Dialog.__init__(self, parent, id, title, size=(250, 240))
self.currKey = key
self.panel = wx.Panel(self, -1)
self.updateUI()
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel, 1, wx.EXPAND, 0)
self.SetSizer(vbox)
self.Layout()
def updateUI(self):
vbox = wx.BoxSizer(wx.VERTICAL)
self.label = wx.StaticText(self.panel, label='')
vbox.Add(self.label)
self.modifierRadio = wx.RadioBox(self.panel, -1, "", choices=['None', 'Shift', 'Control'], majorDimension=1, style=wx.RA_SPECIFY_ROWS)
self.modifierRadio.Bind(wx.EVT_RADIOBOX, self.onChangeModifier)
vbox.Add(self.modifierRadio)
itemPanel = wx.Panel(self.panel)
hbox = wx.BoxSizer(wx.HORIZONTAL)
keyList = ['']
keyList.extend(base.direct.specialKeys)
self.specialKeyCombo = wx.Choice(itemPanel, -1, choices=keyList)
self.specialKeyCombo.Bind(wx.EVT_CHOICE, self.onChangeSpecialKey)
self.keyEntry = wx.TextCtrl(itemPanel, -1, size=(30, 20))
button = wx.Button(itemPanel, -1, 'Apply', size=(50, 20))
button.Bind(wx.EVT_BUTTON, self.onApply)
hbox.Add(self.specialKeyCombo)
hbox.Add(self.keyEntry)
hbox.Add(button)
itemPanel.SetSizer(hbox)
vbox.Add(itemPanel)
self.panel.SetSizer(vbox)
keyDesc = base.direct.hotKeyMap[self.currKey]
self.label.SetLabel(keyDesc[0])
if 'shift' in self.currKey:
self.modifierRadio.SetStringSelection('Shift')
self.specialKeyCombo.Enable(False)
keyStr = self.currKey[len('shift-'):]
elif 'control' in self.currKey:
self.modifierRadio.SetStringSelection('Control')
self.specialKeyCombo.Enable(False)
keyStr = self.currKey[len('control-'):]
else:
self.modifierRadio.SetStringSelection('None')
self.specialKeyCombo.Enable(True)
keyStr = self.currKey
if keyStr in base.direct.specialKeys:
self.keyEntry.SetValue('')
self.keyEntry.Enable(False)
self.specialKeyCombo.SetStringSelection(keyStr)
else:
self.specialKeyCombo.SetStringSelection('')
self.keyEntry.SetValue(keyStr)
def onChangeModifier(self, evt):
if evt.GetString() == 'None':
self.specialKeyCombo.Enable(True)
else:
self.specialKeyCombo.SetStringSelection('')
self.specialKeyCombo.Enable(False)
self.keyEntry.Enable(True)
def onChangeSpecialKey(self, evt):
if evt.GetString() != '':
self.keyEntry.SetValue('')
self.keyEntry.Enable(False)
else:
self.keyEntry.Enable(True)
def onApply(self, evt):
modifier = self.modifierRadio.GetStringSelection()
if modifier == 'Shift':
prefix = 'shift-'
elif modifier == 'Control':
prefix = 'control-'
else:
prefix = ''
specialKey = self.specialKeyCombo.GetStringSelection()
if specialKey == '':
newKeyStr= prefix + self.keyEntry.GetValue().lower()
else:
newKeyStr = specialKey
if newKeyStr != self.currKey:
if newKeyStr in base.direct.hotKeyMap.keys():
print 'a hotkey is to be overridden with', newKeyStr
oldKeyDesc = base.direct.hotKeyMap[newKeyStr]
msg = 'The hotkey is already assigned to %s\n'%oldKeyDesc[0] +\
'Do you want to override this?'
dialog = wx.MessageDialog(None, msg, 'Hot Key exists!',
wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
result = dialog.ShowModal()
if result == wx.ID_YES:
base.direct.hotKeyMap[newKeyStr] = base.direct.hotKeyMap[self.currKey]
base.direct.hotKeyMap['__removed__' + newKeyStr] = oldKeyDesc
del base.direct.hotKeyMap[self.currKey]
else:
base.direct.hotKeyMap[newKeyStr] = base.direct.hotKeyMap[self.currKey]
del base.direct.hotKeyMap[self.currKey]
self.Destroy()
class HotKeyPanel(ScrolledPanel):
def __init__(self, parent):
ScrolledPanel.__init__(self, parent, -1)
self.updateUI()
def updateUI(self):
vbox = wx.BoxSizer(wx.VERTICAL)
keys = base.direct.hotKeyMap.keys()
keys.sort()
for key in keys:
keyDesc = base.direct.hotKeyMap[key]
itemPanel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
space = wx.StaticText(itemPanel, label='', size=(5,20))
hotKey = wx.StaticText(itemPanel, label=key, size=(100, 20))
desc = wx.StaticText(itemPanel, label=keyDesc[0], size=(380, 20))
button = wx.Button(itemPanel, -1, 'Edit', size=(40, 20))
button.Bind(wx.EVT_BUTTON, lambda p0 = None, p1 = key: self.onEdit(p0, p1))
sizer.Add(space)
sizer.Add(hotKey)
sizer.Add(desc, 1, wx.EXPAND)
sizer.Add(button)
itemPanel.SetSizer(sizer)
vbox.Add(itemPanel)
self.SetSizer(vbox)
self.Layout()
self.SetupScrolling(self, scroll_y=True, rate_y=20)
def onEdit(self, evt, key):
editUI = EditHotKeyDialog(self, -1, 'Edit Hot Key', key)
editUI.ShowModal()
editUI.Destroy()
sizer = self.GetSizer()
if sizer is not None:
sizer.DeleteWindows()
self.SetSizer(None)
self.updateUI()
class HotKeyUI(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size=(550, 500))
panel = HotKeyPanel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(panel, 1, wx.EXPAND, 0)
self.SetSizer(vbox)
self.Layout()

View File

@ -154,8 +154,9 @@ class LevelEditorBase(DirectObject):
try:
f = open(self.settingsFile, 'w')
f.write('gridSize %f\n'%self.ui.perspView.grid.gridSize)
f.write('gridSpacing %f\n'%self.ui.perspView.grid.gridSpacing)
f.write('gridSize\n%f\n'%self.ui.perspView.grid.gridSize)
f.write('gridSpacing\n%f\n'%self.ui.perspView.grid.gridSpacing)
f.write('hotKey\n%s\n'%base.direct.hotKeyMap)
f.close()
except:
pass
@ -171,11 +172,15 @@ class LevelEditorBase(DirectObject):
gridSize = 100.0
gridSpacing = 5.0
for line in configLines:
for i in range(0, len(configLines)):
line = configLines[i]
i = i + 1
if line.startswith('gridSize'):
gridSize = float(line.split()[1])
gridSize = float(configLines[i])
elif line.startswith('gridSpacing'):
gridSpacing = float(line.split()[1])
gridSpacing = float(configLines[i])
elif line.startswith('hotKey'):
base.direct.hotKeyMap = eval(configLines[i])
self.ui.updateGrids(gridSize, gridSpacing)
except:

View File

@ -9,6 +9,7 @@ from ViewPort import *
from ObjectPaletteUI import *
from ObjectPropertyUI import *
from SceneGraphUI import *
from HotKeyUI import *
class PandaTextDropTarget(wx.TextDropTarget):
def __init__(self, editor):
@ -65,12 +66,12 @@ class LevelEditorUI(WxAppShell):
self.menuOptions = wx.Menu()
self.menuBar.Insert(2, self.menuOptions, "&Options")
self.gridSizeMenuItem = self.menuOptions.Append(-1, "&Grid Size ")
self.gridSizeMenuItem = self.menuOptions.Append(-1, "&Grid Size")
self.Bind(wx.EVT_MENU, self.onGridSize, self.gridSizeMenuItem)
self.showPandaObjectsMenuItem = self.menuOptions.Append(-1, "&Show Panda Objects", kind = wx.ITEM_CHECK)
self.Bind(wx.EVT_MENU, self.onShowPandaObjects, self.showPandaObjectsMenuItem)
self.hotKeysMenuItem = self.menuOptions.Append(-1, "&Hot Keys")
self.Bind(wx.EVT_MENU, self.onHotKeys, self.hotKeysMenuItem)
def createInterface(self):
self.createMenu()
@ -204,6 +205,11 @@ class LevelEditorUI(WxAppShell):
self.leftView.grid.gridSpacing = newSpacing
self.leftView.grid.updateGrid()
def onHotKeys(self, evt):
hotKeyUI = HotKeyUI(self, -1, 'Hot Key List')
hotKeyUI.ShowModal()
hotKeyUI.Destroy()
class GridSizeUI(wx.Dialog):
def __init__(self, parent, id, title, gridSize, gridSpacing):
wx.Dialog.__init__(self, parent, id, title, size=(250, 240))