-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEBlockManager.cpp
More file actions
171 lines (116 loc) · 2.99 KB
/
Copy pathEBlockManager.cpp
File metadata and controls
171 lines (116 loc) · 2.99 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
#include "EBlockManager.h"
#include <iostream>
#include "imgui.h"
void EBlockManager::createBlock()
{
//Crete new block on the heap.
EBlock* newBlock = new EBlock();
newBlock->textFont = blockFont;
newBlock->Init();
drawingGroup.push_back(newBlock->drawingGroup);
blocks.push_back(newBlock);
}
void EBlockManager::deleteBlock(EBlock* block)
{
drawingGroup.removeAt(&block->drawingGroup);
for (int b = 0; b < blocks.size(); b++) {
if (blocks[b] == block) {
blocks.erase(blocks.begin() + b);
}
}
delete block;
}
void EBlockManager::deleteSelectedBlocks()
{
for (int s = 0; s < selectedBlocks.size(); s++) {
deleteBlock(selectedBlocks[s]);
}
selectedBlocks.clear();
}
void EBlockManager::Update(sf::Event event)
{
for (int b = 0; b < blocks.size(); b++) {
blocks[b]->Update();
}
if (selectedBlocks.size() > 0) {
for (int s = 0; s < selectedBlocks.size(); s++) {
}
}
//Selected blocks event stuff
if (event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left) {
//For all blocks
bool hitBlock = false;
for (int b = 0; b < blocks.size(); b++) {
sf::Vector2i pixelPos = sf::Mouse::getPosition(*window);
if (blocks[b]->isMouseHovering(window->mapPixelToCoords(pixelPos))) {
//Person clicked on block
hitBlock = true;
//if (selectedBlocks.size() == 0) {
unselectAllBlocks();
selectBlock(blocks[b]);
//}
}
}
if (!hitBlock) {
unselectAllBlocks();
}
holdingMouse = true;
} if (event.mouseButton.button == sf::Mouse::Right) {
if (selectedBlocks.size() > 0) {
for (int s = 0; s < selectedBlocks.size(); s++) {
sf::Vector2i pixelPos = sf::Mouse::getPosition(*window);
selectedBlocks[s]->setPosition(window->mapPixelToCoords(pixelPos));
}
}
}
}
if (event.type == sf::Event::MouseButtonReleased) {
if (event.mouseButton.button == sf::Mouse::Left) {
holdingMouse = false;
}
}
//User holding mouse button moving
for (int b = 0; b < blocks.size(); b++) {
sf::Vector2i pixelPos = sf::Mouse::getPosition(*window);
//Person clicked on block
//for moving the block
if (blocks[b]->selected_b && holdingMouse) {
blocks[b]->setPosition(window->mapPixelToCoords(pixelPos));
}
}
}
void EBlockManager::UpdateBlocks()
{
for (int b = 0; b < blocks.size(); b++) {
blocks[b]->Update();
}
}
void EBlockManager::Shutdown()
{
for (int b = 0; b < blocks.size(); b++) {
delete blocks[b];
}
}
void EBlockManager::unselectAllBlocks()
{
for (int b = 0; b < selectedBlocks.size(); b++) {
unselectBlock(selectedBlocks[b]);
}
}
void EBlockManager::selectBlock(EBlock* block)
{
block->readyForSelect();
selectedBlocks.push_back(block);
}
void EBlockManager::unselectBlock(EBlock* block)
{
bool found;
for (int b = 0; b < selectedBlocks.size(); b++) {
if (selectedBlocks[b] == block) {
block->readyForDeselect();
selectedBlocks.erase(selectedBlocks.begin() + b);
selectedBlocks.shrink_to_fit();
}
}
}