summaryrefslogtreecommitdiff
path: root/viewGridNavigator.cpp
blob: 50d0951c26c6eba41d787eaa658f3a769d311c6d (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
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
203
204
205
206
207
208
#include "viewGridNavigator.h"
#include <iostream>
#include "plexSdOsd.h"
#include "pictureCache.h"

unsigned int cGridElement::AbsoluteGridIdCounter = 0;

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

cViewGridNavigator::cViewGridNavigator(skindesignerapi::cOsdView* rootView)
{
	m_columns = 2;
	m_rows = 2;
	m_newDimensions = true;
	m_setIterator = true;

	m_pGrid = NULL;
	m_pRootView = rootView;
}

void cViewGridNavigator::SetViewGrid(std::shared_ptr<skindesignerapi::cViewGrid> grid)
{
	if (grid) {
		if(m_pGrid) m_pGrid->Clear();
		m_pGrid = std::shared_ptr<skindesignerapi::cViewGrid>(grid);
	}
}

void cViewGridNavigator::ReDraw(cGridElement* element)
{
	if(element) {
		cMutexLock MutexLock(&cPlexSdOsd::RedrawMutex);
		if (!element->IsVisible()) {
			std::cout << "ReDraw element not visible" << std::endl;
			return;
		}
		double x, y;
		element->GetPosition(x, y);
		element->AddTokens(m_pGrid);
		double width = 1.0 / m_columns;
		double height = 1.0 / m_rows;
		m_pGrid->SetGrid(element->GridElementId(), x, y, width, height);
		Flush();
	}
}

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

	//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;
			(*it)->SetPosition(-1,-1);
			// Remove Queued Downloads
			cPictureCache::GetInstance().Remove(*it);
		}
	}

	std::vector<cGridElement*>::iterator begin = m_firstElementIter + startOffset;
	std::vector<cGridElement*>::iterator end = m_firstElementIter + endOffset;
	if(begin < m_vElements.begin()) begin = m_vElements.begin();
	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;
	}

	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;
	cMutexLock MutexLock(&cPlexSdOsd::RedrawMutex);
	if(obj->IsNew() || m_newDimensions) {
		// fill data
		obj->SetPosition(x, y);
		obj->AddTokens(m_pGrid, true, std::bind(&cViewGridNavigator::ReDraw, this, std::placeholders::_1));
		m_pGrid->SetGrid(obj->GridElementId(), x, y, width, height);
		obj->InitFinished();
		// set GridDimensions
		m_pGrid->AddIntToken("columns", m_columns);
		m_pGrid->AddIntToken("rows", m_rows);
		m_pGrid->AddIntToken("position", obj->AbsolutePosition);
		m_pGrid->AddIntToken("totalcount", m_vElements.size());
	} else {
		obj->SetPosition(x, y);
		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;
}

bool cViewGridNavigator::NavigateDown()
{
	if (m_activeElementIter == m_vElements.end() - 1) return false;
	auto next = m_activeElementIter + m_columns;
	if(next >= m_vElements.end()) next = m_vElements.end()-1;

	// scroll down?
	if(!(*next)->IsVisible()) {
		FilterElements(m_columns);
	}

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

bool cViewGridNavigator::NavigateUp()
{
	if (m_activeElementIter == m_vElements.begin()) return false;
	auto next = m_activeElementIter - m_columns;
	if(next < m_vElements.begin()) next = m_vElements.begin();

	//scroll up?
	if(!(*next)->IsVisible()) {
		FilterElements(-m_columns);
	}

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

bool cViewGridNavigator::NavigateLeft()
{
	if (m_activeElementIter == m_vElements.begin()) return false;
	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;
	return true;
}

bool cViewGridNavigator::NavigateRight()
{
	if (m_activeElementIter == m_vElements.end() - 1) return false;
	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;
	return true;
}