summaryrefslogtreecommitdiff
path: root/plugingui/texturedbox.cc
blob: 21bf9466bfe5cdad7eae364c9f3cf270964d7ea6 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            texturedbox.cc
 *
 *  Sun Jun  5 12:22:15 CEST 2016
 *  Copyright 2016 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "texturedbox.h"

#include <cassert>

namespace GUI
{

TexturedBox::TexturedBox(ImageCache& image_cache, const std::string& filename,
                         std::size_t x0, std::size_t y0,
                         std::size_t dx1, std::size_t dx2, std::size_t dx3,
                         std::size_t dy1, std::size_t dy2, std::size_t dy3)
	: seg_a(image_cache, filename, x0            , y0            , dx1, dy1)
	, seg_b(image_cache, filename, x0 + dx1      , y0            , dx2, dy1)
	, seg_c(image_cache, filename, x0 + dx1 + dx2, y0            , dx3, dy1)
	, seg_d(image_cache, filename, x0            , y0 + dy1      , dx1, dy2)
	, seg_e(image_cache, filename, x0 + dx1      , y0 + dy1      , dx2, dy2)
	, seg_f(image_cache, filename, x0 + dx1 + dx2, y0 + dy1      , dx3, dy2)
	, seg_g(image_cache, filename, x0            , y0 + dy1 + dy2, dx1, dy3)
	, seg_h(image_cache, filename, x0 + dx1      , y0 + dy1 + dy2, dx2, dy3)
	, seg_i(image_cache, filename, x0 + dx1 + dx2, y0 + dy1 + dy2, dx3, dy3)
	, dx1(dx1)
	, dx2(dx2)
	, dx3(dx3)
	, dy1(dy1)
	, dy2(dy2)
	, dy3(dy3)
	, _width(dx1 + dx2 + dx3)
	, _height(dy1 + dy2 + dy3)
{
}

std::size_t TexturedBox::width() const
{
	return _width;
}

std::size_t TexturedBox::height() const
{
	return _height;
}

void TexturedBox::setSize(std::size_t width, std::size_t height)
{
	_width = width;
	_height = height;
}

const Colour& TexturedBox::getPixel(std::size_t x, std::size_t y) const
{
	assert(x < _width);
	assert(y < _height);

	if(y < dy1) // row 1
	{
		if(x < dx1) // col 1
		{
			return seg_a.getPixel(x, y);
		}
		else if(x < (_width - dx3)) // col 2
		{
			float scale = (float)(x - dx1) / (float)(_width - dx1 - dx3);
			assert(seg_b.width() == dx2);
			return seg_b.getPixel(scale * dx2, y);
		}
		else // col 3
		{
			return seg_c.getPixel(x - (_width - dx3), y);
		}
	}
	else if(y < (_height - dy3)) // row 2
	{
		if(x < dx1) // col 1
		{
			// TODO: Apply vertical scale
			float scale = (float)(y - dy1) / (float)(_height - dy1 - dy3);
			return seg_d.getPixel(x, scale * dy2);
		}
		else if(x < (_width - dx3)) // col 2
		{
			float scale_x = (float)(x - dx1) / (float)(_width - dx1 - dx3);
			float scale_y = (float)(y - dy1) / (float)(_height - dy1 - dy3);
			return seg_e.getPixel(scale_x * dx2, scale_y * dy2);
		}
		else // col 3
		{
			float scale = (float)(y - dy1) / (float)(_height - dy1 - dy3);
			return seg_f.getPixel(x - (_width - dx3), scale * dy2);
		}
	}
	else // row 3
	{
		if(x < dx1) // col 1
		{
			return seg_g.getPixel(x, y - (_height - dy3));
		}
		else if(x < (_width - dx3)) // col 2
		{
			float scale = (float)(x - dx1) / (float)(_width - dx1 - dx3);
			return seg_h.getPixel(scale * dx2, y - (_height - dy3));
		}
		else // col 3
		{
			return seg_i.getPixel(x - (_width - dx3), y - (_height - dy3));
		}
	}

	return outOfRange;
}

const std::uint8_t* TexturedBox::line(std::size_t y) const
{
	// TODO: Gather line into temporary buffer?
	return nullptr;
}

bool TexturedBox::hasAlpha() const
{
	return true;
}

} // GUI::