forked from scarsty/kys-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.cpp
More file actions
202 lines (189 loc) · 5.48 KB
/
UI.cpp
File metadata and controls
202 lines (189 loc) · 5.48 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "UI.h"
#include "Font.h"
#include "GameUtil.h"
#include "Save.h"
UI::UI()
{
//注意,此处约定childs_[0]为子UI,创建好对应的指针,需要显示哪个赋值到childs_[0]即可
ui_status_ = std::make_shared<UIStatus>();
ui_item_ = std::make_shared<UIItem>();
ui_system_ = std::make_shared<UISystem>();
ui_status_->setPosition(300, 0);
ui_item_->setPosition(300, 0);
ui_system_->setPosition(300, 0);
addChild(ui_status_);
//貌似这里不能直接调用其他单例,静态量的创建顺序不确定
button_status_ = std::make_shared<Button>();
button_status_->setTexture("title", 122);
button_item_ = std::make_shared<Button>();
button_item_->setTexture("title", 124);
button_system_ = std::make_shared<Button>();
button_system_->setTexture("title", 125);
addChild(button_status_, 10, 10);
addChild(button_item_, 90, 10);
addChild(button_system_, 170, 10);
heads_ = std::make_shared<Menu>();
addChild(heads_);
for (int i = 0; i < TEAMMATE_COUNT; i++)
{
heads_->addChild(std::make_shared<Head>(), 20, 60 + i * 90);
}
heads_->getChild(0)->setState(NodePass);
//addChild(heads_);
result_ = -1; //非负:物品id,负数:其他情况,再定
}
UI::~UI()
{
}
void UI::onEntrance()
{
}
void UI::draw()
{
Engine::getInstance()->fillColor({ 0, 0, 0, 128 }, 0, 0, -1, -1);
}
void UI::dealEvent(BP_Event& e)
{
for (int i = 0; i < TEAMMATE_COUNT; i++)
{
std::shared_ptr<Head> head = std::dynamic_pointer_cast<Head>(heads_->getChild(i));
auto role = Save::getInstance()->getTeamMate(i);
head->setRole(role);
if (role == nullptr)
{
continue;
}
if (head->getState() == NodePass)
{
ui_status_->setRole(role);
current_head_ = i;
}
head->setText("");
//如在物品栏则判断是否在使用,或者可以使用,设置对应的头像状态
if (childs_[0] == ui_item_)
{
Item* item = ui_item_->getCurrentItem();
if (item)
{
if (role->Equip0 == item->ID || role->Equip1 == item->ID || role->PracticeItem == item->ID)
{
head->setText("使用中");
//Font::getInstance()->draw("使用中", 25, x + 5, y + 60, { 255,255,255,255 });
}
if (role->canUseItem(item))
{
head->setState(NodePass);
}
}
}
}
//这里设定当前头像为Pass,令其不变暗,因为检测事件是先检测子节点,所以这里可以生效
if (childs_[0] == ui_status_)
{
heads_->getChild(current_head_)->setState(NodePass);
}
childs_[current_button_]->setState(NodePass);
//快捷键切换
{
int cb = current_button_;
if (e.type == BP_KEYUP)
{
switch (e.key.keysym.sym)
{
case BPK_1:
cb = 0;
break;
case BPK_2:
cb = 1;
break;
case BPK_3:
cb = 2;
break;
default:
break;
}
}
if (e.type == BP_CONTROLLERAXISMOTION)
{
if (e.caxis.axis == BP_CONTROLLER_AXIS_TRIGGERLEFT && e.caxis.value == 0)
{
cb = GameUtil::limit(cb - 1, 0, 3);
}
if (e.caxis.axis == BP_CONTROLLER_AXIS_TRIGGERRIGHT && e.caxis.value == 0)
{
cb = GameUtil::limit(cb + 1, 0, 3);
}
}
if (cb != current_button_)
{
current_button_ = cb;
switch (current_button_)
{
case 0:
childs_[0] = ui_status_;
setAllChildState(NodeNormal);
button_status_->setState(NodePress);
break;
case 1:
childs_[0] = ui_item_;
setAllChildState(NodeNormal);
button_item_->setState(NodePress);
break;
case 2:
childs_[0] = ui_system_;
setAllChildState(NodeNormal);
button_system_->setState(NodePress);
break;
default:
break;
}
}
}
//仅在状态部分,左侧头像才接收事件
if (childs_[0] == ui_status_)
{
heads_->setDealEvent(1);
}
else
{
heads_->setDealEvent(0);
}
}
void UI::onPressedOK()
{
//这里检测是否使用了物品,返回物品的id
if (childs_[0] == ui_item_)
{
auto item = ui_item_->getCurrentItem();
if (item && item->ItemType == 0)
{
setExit(true);
return;
}
}
if (childs_[0] == ui_system_)
{
if (ui_system_->getResult() == 0)
{
setExit(true);
return;
}
}
//按钮的响应
if (button_status_->getState() == NodePress)
{
childs_[0] = ui_status_;
current_button_ = button_status_->getTag();
}
if (button_item_->getState() == NodePress)
{
childs_[0] = ui_item_;
current_button_ = button_item_->getTag();
}
if (button_system_->getState() == NodePress)
{
childs_[0] = ui_system_;
current_button_ = button_system_->getTag();
}
//current_button_ = childs_[0]->getTag();
}