This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw_i_menu.py
123 lines (100 loc) · 4.03 KB
/
w_i_menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import abc
from direct.gui.DirectGui import DirectFrame
class IMenu(metaclass=abc.ABCMeta):
def __init__(self, frame=None):
self.hasLoaded = False
self.menuVerticalChoicesList = [
{"event": "Example-Event", "text": "Override this"}]
self.menuVerticalChoice = [0]
self.menuHorizontalChoices = [
{"event": "Example-Event", "text": "Override this"}]
self.menuHorizontalChoice = [0]
self.menuVerticalButtons = []
self.menuHorizontalButtons = []
self.isButtonUp = True
self.init_frame(frame)
def init_frame(self, frame):
if frame:
self.frameMain = frame
else:
self.frameMain = DirectFrame(
frameSize=(base.a2dLeft, base.a2dRight,
base.a2dBottom, base.a2dTop),
frameColor=(0, 0, 0, 0.0))
def show(self):
self.frameMain.show()
def hide(self):
self.frameMain.hide()
def quit(self):
self.frameMain.removeNode()
def readKeys(self, task):
keysPressed = sum(base.directionMap.values())
if keysPressed == 0:
self.isButtonUp = True
if base.commandMap["confirm"]:
self.menuVerticalEvent()
base.messenger.send("playConfirm")
elif base.commandMap["cancel"]:
self.cancelCommand()
base.messenger.send("playCancel")
base.resetButtons()
return task.cont
if not self.isButtonUp:
return task.cont
if base.directionMap["up"]:
self.navigateChoice(-1, self.menuVerticalChoice,
self.menuVerticalChoicesList)
self.isButtonUp = False
elif base.directionMap["down"]:
self.navigateChoice(1, self.menuVerticalChoice,
self.menuVerticalChoicesList)
self.isButtonUp = False
elif base.directionMap["left"]:
self.navigateChoice(-1, self.menuHorizontalChoice,
self.menuHorizontalChoices)
self.isButtonUp = False
self.menuHorizontalEvent()
elif base.directionMap["right"]:
self.navigateChoice(1, self.menuHorizontalChoice,
self.menuHorizontalChoices)
self.isButtonUp = False
self.menuHorizontalEvent()
base.resetButtons()
return task.cont
def navigateChoice(self, value, choice, choiceList):
choice[0] += value
if choice[0] < 0:
choice[0] = len(choiceList) - 1
elif choice[0] > len(choiceList) - 1:
choice[0] = 0
self.updateCheckbuttons()
def updateCheckbuttons(self):
for btn in self.menuVerticalButtons:
index = self.menuVerticalButtons.index(btn)
isPressed = index == self.menuVerticalChoice[0]
btn["indicatorValue"] = isPressed
btn.setIndicatorValue()
for btn in self.menuHorizontalButtons:
index = self.menuHorizontalButtons.index(btn)
isPressed = index == self.menuHorizontalChoice[0]
btn["indicatorValue"] = isPressed
btn.setIndicatorValue()
def menuVerticalEvent(self):
base.messenger.send(
self.menuVerticalChoicesList[self.menuVerticalChoice[0]]["event"])
def menuHorizontalEvent(self):
base.messenger.send(
self.menuHorizontalChoices[self.menuHorizontalChoice[0]]["event"])
def createVerticalButtons(self):
for btn in self.menuVerticalChoicesList:
index = self.menuVerticalChoicesList.index(btn)
self.createButton(
btn['text'],
index,
[btn['event']])
@abc.abstractmethod
def createButton(self, text, index, eventArgs):
raise NotImplementedError('subclass must define this method')
@abc.abstractmethod
def cancelCommand(self):
raise NotImplementedError('subclass must define this method')