summaryrefslogtreecommitdiff
path: root/viewGridNavigator.cpp
blob: 78781ca0c358932623edf0eed88d112041ec0456 (plain)
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
#include "viewGridNavigator.h"

unsigned int cGridElement::AbsoluteGridIdCounter = 0;

cGridElement::cGridElement()
{
	m_iGridId = AbsoluteGridIdCounter++;
	Position = -1;
	m_bInit = true;
}

cViewGridNavigator::cViewGridNavigator(cViewGrid* viewGrid)
{
	m_columns = 2;
	m_rows = 2;
	m_newDimensions = true;
	m_setIterator = true;

	m_pGrid = std::shared_ptr<cViewGrid>(viewGrid);
}


void cViewGridNavigator::FilterElements(int scrollOffset)
{
	int startOffset = scrollOffset;
	int endOffset = startOffset + (m_rows * m_columns);
	if(scrollOffset < 0) {
		endOffset = (m_rows * m_columns) + scrollOffset;
	}

	std::vector<cGridElement*>::iterator begin = m_firstElementIter + startOffset;
	std::vector<cGridElement*>::iterator end = m_firstElementIter + endOffset;
	if(end > m_vElements.end()) end = m_vElements.end();

	int pos = 0;
	for(std::vector<cGridElement*>::iterator it = begin; it != end; ++it) {
		cGridElement *elem = *it;
		elem->Position = pos++;
		SetGridElementData(elem);
	}

	if(m_setIterator) {
		m_activeElementIter = begin;
		m_pGrid->SetCurrent((*m_activeElementIter)->GridElementId(), true);
		m_setIterator = false;
	}

	//remove non visible elements from grid
	if(scrollOffset !=0 ) {
		auto startIt = m_firstElementIter;
		auto endIt = m_firstElementIter + scrollOffset;
		if(scrollOffset < 0) {
			startIt = m_lastElementIter + scrollOffset;
			endIt = m_lastElementIter;
		}
		if(startIt < m_vElements.begin()) startIt = m_vElements.begin();
		if(endIt > m_vElements.end()) endIt = m_vElements.end();

		for(std::vector<cGridElement*>::iterator it = startIt; it != endIt; ++it) {
			m_pGrid->Delete((*it)->GridElementId());
			(*it)->Dirty();
			(*it)->Position = -1;
		}
	}

	m_firstElementIter = begin;
	m_lastElementIter = end;
	m_newDimensions = false;
}

void cViewGridNavigator::SetGridElementData(cGridElement *obj)
{
	// calculate position
	double row, column, x, y, height, width;

	row = floor(obj->Position / m_columns);
	column = obj->Position - (row * m_columns);
	width = 1.0 / m_columns;
	height = 1.0 / m_rows;
	x = width * column;
	y= height * row;

	//std::cout << "ID: " << obj->GridElementId() << "\tPos: " << obj->Position << "\t\tx: " << x << "\t\ty: " << y << "\t\twi: " << width << "\t\thei: " <<  height << "\tCol: " << column << "\tRow: " << row << std::endl;

	if(obj->IsNew() || m_newDimensions) {
		// fill data
		obj->AddTokens(m_pGrid);
		m_pGrid->SetGrid(obj->GridElementId(), x, y, width, height);
		obj->InitFinished();
	} else {
		m_pGrid->MoveGrid(obj->GridElementId(), x, y, width, height);
	}
}

void cViewGridNavigator::SetGridDimensions(int rows, int columns)
{
	m_rows = rows;
	m_columns = columns;
	m_newDimensions = true;
}

void cViewGridNavigator::NavigateDown()
{
	if(m_activeElementIter + m_columns >= m_vElements.end()) return;
	auto next = m_activeElementIter + m_columns;
	// scroll down?
	if(!(*next)->IsVisible()) {
		FilterElements(m_columns);
	}

	m_pGrid->SetCurrent((*m_activeElementIter)->GridElementId(), false);
	m_pGrid->SetCurrent((*next)->GridElementId(), true);
	m_activeElementIter = next;
}

void cViewGridNavigator::NavigateUp()
{
	if(m_activeElementIter - m_columns < m_vElements.begin()) return;
	auto next = m_activeElementIter - m_columns;
	//scroll up?
	if(!(*next)->IsVisible()) {
		FilterElements(-m_columns);
	}

	m_pGrid->SetCurrent((*m_activeElementIter)->GridElementId(), false);
	m_pGrid->SetCurrent((*next)->GridElementId(), true);
	m_activeElementIter = next;
}

void cViewGridNavigator::NavigateLeft()
{
	auto next = m_activeElementIter - 1;
	if(next < m_vElements.begin()) next = m_vElements.begin();

	if(!(*next)->IsVisible()) {
		auto temp = m_activeElementIter;
		FilterElements(-m_columns);
		m_activeElementIter = temp;
	}

	m_pGrid->SetCurrent((*m_activeElementIter)->GridElementId(), false);
	m_pGrid->SetCurrent((*next)->GridElementId(), true);
	m_activeElementIter = next;
}

void cViewGridNavigator::NavigateRight()
{
	auto next = m_activeElementIter + 1;
	if(next >= m_vElements.end()) next = m_vElements.end()-1;

	if(!(*next)->IsVisible()) {
		FilterElements(m_columns);
	}

	m_pGrid->SetCurrent((*m_activeElementIter)->GridElementId(), false);
	m_pGrid->SetCurrent((*next)->GridElementId(), true);
	m_activeElementIter = next;
}