-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCP_ContextMenu.cpp
168 lines (133 loc) · 5.18 KB
/
GCP_ContextMenu.cpp
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
#include "GCP_ContextMenu.h"
#include "GCP_Button.h"
#include "GCP_Vector.h"
using namespace gcp;
//Êîíòåêñòíîå ìåíþ ýòî íàáîð êíîïîê, êîòîðûå îòêðûâàþòñÿ íàïðèìåð íà ïðàâûé êëèê ïî ôîðìî÷êå
GCP_ContextMenu::GCP_ContextMenu()
{
_isVisible = false;
_iType = GCP_MENU_MVERTICAL; //ìåíþ ìîæåò áûòü âåðòèêàëüíûì ëèáî ãîðèçîíòàëüíûì
}
GCP_ContextMenu::~GCP_ContextMenu(){
//for(int i =0; i<menu.size(); i++)
// delete menu.at(i); //óäàëèì âñå êíîïêè êîòîðûå åñòü â ìåíþ. òàê êàê óêàçàòåëè íà íèõ õðàíÿòñÿ òîëüêî çäåñü
}
void GCP_ContextMenu::setLock(bool flag)
{
_isLocked = flag; //îòêëþ÷àåò îáðàáîòêó ñîáûòèé êîíêòåêñòíîãî ìåíþ. (íóæíî íàïðèìåð ÷òîáû çàìîðîçèòü èíòåðôåéñ)
}
/*void GCP_ContextMenu::initTexture(SDL_Renderer* screen)
{
unsigned int iMenuSize = menu.size();
for(unsigned int i=0; i<iMenuSize; i++)
menu.at(i)->initTexture(screen); //Ñå÷àñ ýòà ôóíêöèÿ íå äåëàåò íè÷åãî. íî ïî õîðîøåìó äîëæíà îñóùåñòâëÿòü ïðåäçàãðóçêó êàðòèíîê êíîïîê
}*/
void GCP_ContextMenu::addButton(const GCP_SPointer<GCP_Button> &button) {
menu.push_back(button);
}
void GCP_ContextMenu::addButton(const string &type)
{
_iSeparators.push_back( menu.size()); //Â ìåíþ ìîæíî äîáàâëÿòü ðàçäåëèòåëè. Ðàçäåëèòåëü íå êíîïêà, à îòñòóï / ëèáî ëèíèÿ
}
void GCP_ContextMenu::open(int x, int y)
{
_isVisible = true; //Îòêðûòü ìåíá â çàäàíûõ êîîðäèíàòàõ
_position.setTopLeft(x, y);
}
void GCP_ContextMenu::close(void *obj)
{
_isVisible = false;
}
gcp_formEvent GCP_ContextMenu::OnMouseGlobalLeftHoldMotion(const GCP_Event &event) {
//Context menu is not dragable
return gcp_formEvent();
}
gcp_formEvent GCP_ContextMenu::OnEvent(const GCP_Event &event)
{
gcp_formEvent evt;
evt.isEventInsideForm = false;
evt.isEventOnFormHead = false;
evt.isFormDragged = false;
if(!isVisible() || _isLocked)
return evt;
unsigned int iMenuSize = menu.size();
for(unsigned int i=0; i<iMenuSize; i++){
menu.at(i)->OnEvent(event); //Ãëîáàëüíîå ñîáûòèå
if(!event.isPassingOnlyGlobalEvent)
if(menu.at(i)->checkCollisionBox(event.mousex, event.mousey)){
menu.at(i)->OnEvent(MakeEventLocal(event)); //ëîêàëüíîå ñîáûòèå
}
}
basicOnEvent(event);
return evt;
}
void GCP_ContextMenu::OnDraw(const GCP_Event &event)
{
if(!isVisible())
return;
//Âûâîä êíîïîê íà ýêðàí
//Calculate Size For OutOfSurface Correlation
int maxMenuWidth = 0; //Ìàêñèìàëüíàÿ øèðèíà êîìïîíåíòîâ â ìåíþ
int maxMenuHeight = 0; //Ìàêñèìàëüíàÿ âûñîòà êîìïîíåíòîâ â ìåíþ
int stackWidth=0, stackHeight=0; //Ñóììàðíàÿ øèðèíà è âûñîòà ñàìîãî ìåíþ
unsigned int iMenuSize = menu.size();
for(unsigned int i=0; i<iMenuSize; i++) {
if (menu.at(i)->getPosition().height()>maxMenuHeight)
maxMenuHeight = menu.at(i)->getPosition().height();
if (menu.at(i)->getPosition().height()>maxMenuWidth)
maxMenuWidth = menu.at(i)->getPosition().width();
if (_iType == GCP_MENU_MVERTICAL){
stackHeight += menu.at(i)->getPosition().height();
stackWidth = maxMenuWidth;
}
if (_iType == GCP_MENU_MHORIZONTAL){
stackWidth += menu.at(i)->getPosition().width();
stackHeight = maxMenuHeight;
}
}
//Äâèãàåì ìåíþ åñëè îíî âûõîäèò çà áóôåð / ôîðìó
GCP_Point<int> normPos = GCP_Math::normalizeRectInRect<int>(
GCP_Rect<int>(_position.x(), _position.y(), stackWidth + iMenuSize, stackHeight + iMenuSize),
event.drawRect, 1);
_position.setTopLeft((int)normPos.X, (int)normPos.Y);
//Òåïåðü êîãäà ìû åãî ïîäâèíóëè íàì íàäî ïîäâèíóòü âñå êîìïîíåíòû óæå ñ ó÷åòîì òîãî ÷òî ñäâèíóëè íàøå ìåíþ
//!!! ðàçäåëèòåëè íå ó÷èòûâàëèñü â öèêëå âûøå!!!
stackWidth=0; //recalc for calculationg drawing coordinates
stackHeight=0;
unsigned int currentSeparator=0;
for(unsigned int i=0; i<iMenuSize; i++) {
if (_iType == GCP_MENU_MVERTICAL){
if(currentSeparator < _iSeparators.size())
if(i==_iSeparators.at(currentSeparator)) {
stackHeight += 5; currentSeparator++;
}
menu.at(i)->setPosition(_position.x(), _position.y() + stackHeight);
stackHeight += menu.at(i)->getPosition().height()+1;
stackWidth = maxMenuWidth;
}
if (_iType == GCP_MENU_MHORIZONTAL){
if(currentSeparator < _iSeparators.size())
if(i==_iSeparators.at(currentSeparator)){
stackWidth += 5;currentSeparator++;
}
menu.at(i)->setPosition(_position.x() + stackWidth, _position.y());
stackWidth += menu.at(i)->getPosition().width()+1;
stackHeight = maxMenuHeight;
}
}
//if(!isContextMenuBlocking)
//_isContextMenuOpened = false;
//Ðèñóåì âñå íàøè êîìïîíåíòû
for(unsigned int i=0; i<iMenuSize; i++){
menu.at(i)->setWidthHeight(maxMenuWidth,maxMenuHeight);
//menu.at(i)->setColor(cBackColor, cTextColor, cBackColorHover, cTextColorHover);
//menu.at(i)->iRoundCff = iRoundCff;
menu.at(i)->OnDraw( event);
//if(!isContextMenuBlocking)
//_isContextMenuOpened = (menu.at(i)->_isContextMenuOpened || _isContextMenuOpened);
}
//Âñïëûâàþùèå ïîäñêàçêè ðèñóþòñÿ ïîâåðõ êîìïîíåíò
for(unsigned int i=0; i<iMenuSize; i++)
menu.at(i)->OnDrawInfo( event);
GCP_Draw::Render()->Draw_Round(_position.x(), _position.y(), stackWidth, stackHeight, getStyle()->roundCff, c_black);
}//OnDraw