summaryrefslogtreecommitdiff
path: root/vgui2/src/ImageBorder.cpp
blob: 51cb0fdf67feacb363ef0fc8924141636ccd20f5 (plain) (blame)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include <stdio.h>
#include <string.h>

#include <vgui_controls/Panel.h>
#include "vgui/IPanel.h"
#include "vgui/IScheme.h"
#include "vgui/ISurface.h"

#include "vgui_internal.h"
#include "ImageBorder.h"
#include "KeyValues.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
ImageBorder::ImageBorder()
{
	_name = NULL;
	m_eBackgroundType = IBorder::BACKGROUND_TEXTURED;

	m_pszImageName = NULL;
	m_iTextureID = g_pSurface->CreateNewTextureID();
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
ImageBorder::~ImageBorder()
{
	if ( vgui::surface() && m_iTextureID != -1 )
	{
		vgui::surface()->DestroyTextureID( m_iTextureID );
		m_iTextureID = -1;
	}

	delete [] _name;
	if ( m_pszImageName )
	{
		delete [] m_pszImageName;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::SetImage(const char *imageName)
{
	if ( m_pszImageName )
	{
		delete [] m_pszImageName;
		m_pszImageName = NULL;
	}

	if (*imageName)
	{
		int len = Q_strlen(imageName) + 1 + 5;	// 5 for "vgui/"
		delete [] m_pszImageName;
		m_pszImageName = new char[ len ];
		Q_snprintf( m_pszImageName, len, "vgui/%s", imageName );

		g_pSurface->DrawSetTextureFile( m_iTextureID, m_pszImageName, true, false);
	}	
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::SetInset(int left,int top,int right,int bottom)
{
	_inset[SIDE_LEFT] = left;
	_inset[SIDE_TOP] = top;
	_inset[SIDE_RIGHT] = right;
	_inset[SIDE_BOTTOM] = bottom;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::GetInset(int& left,int& top,int& right,int& bottom)
{
	left = _inset[SIDE_LEFT];
	top = _inset[SIDE_TOP];
	right = _inset[SIDE_RIGHT];
	bottom = _inset[SIDE_BOTTOM];
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::Paint(int x, int y, int wide, int tall)
{
	Paint(x, y, wide, tall, -1, 0, 0);
}

//-----------------------------------------------------------------------------
// Purpose: Draws the border with the specified size
//-----------------------------------------------------------------------------
void ImageBorder::Paint(int x, int y, int wide, int tall, int breakSide, int breakStart, int breakEnd)
{
	if ( !m_pszImageName || !m_pszImageName[0] )
		return;

	g_pSurface->DrawSetColor( 255, 255, 255, 255 );
	g_pSurface->DrawSetTexture( m_iTextureID );

	float uvx = 0;
	float uvy = 0;
	float uvw = 1.0;
	float uvh = 1.0;
	Vector2D uv11( uvx, uvy );
	Vector2D uv21( uvx+uvw, uvy );
	Vector2D uv22( uvx+uvw, uvy+uvh );
	Vector2D uv12( uvx, uvy+uvh );

	if ( m_bTiled )
	{
		int imageWide, imageTall;
		g_pSurface->DrawGetTextureSize( m_iTextureID, imageWide, imageTall );

		int y = 0;
		while ( y < tall )
		{
			int x = 0;
			while (x < wide)
			{
				vgui::Vertex_t verts[4];
				verts[0].Init( Vector2D( x, y ), uv11 );
				verts[1].Init( Vector2D( x+imageWide, y ), uv21 );
				verts[2].Init( Vector2D( x+imageWide, y+imageTall ), uv22 );
				verts[3].Init( Vector2D( x, y+imageTall ), uv12  );

				g_pSurface->DrawTexturedPolygon( 4, verts );	

				x += imageWide;
			}

			y += imageTall;
		}
	}
	else
	{
		vgui::Vertex_t verts[4];
		verts[0].Init( Vector2D( x, y ), uv11 );
		verts[1].Init( Vector2D( x+wide, y ), uv21 );
		verts[2].Init( Vector2D( x+wide, y+tall ), uv22 );
		verts[3].Init( Vector2D( x, y+tall ), uv12  );

		g_pSurface->DrawTexturedPolygon( 4, verts );	
	}

	g_pSurface->DrawSetTexture(0);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::Paint(VPANEL panel)
{
	// get panel size
	int wide, tall;
	ipanel()->GetSize( panel, wide, tall );
	Paint(0, 0, wide, tall, -1, 0, 0);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ImageBorder::ApplySchemeSettings(IScheme *pScheme, KeyValues *inResourceData)
{
	m_eBackgroundType = (backgroundtype_e)inResourceData->GetInt("backgroundtype");
	m_bTiled = inResourceData->GetInt( "tiled" );

	const char *imageName = inResourceData->GetString("image", "");
	SetImage( imageName );

	m_bPaintFirst = inResourceData->GetInt("paintfirst", true );
}

//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
const char *ImageBorder::GetName()
{
	if (_name)
		return _name;
	return "";
}

//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void ImageBorder::SetName(const char *name)
{
	if (_name)
	{
		delete [] _name;
	}

	int len = Q_strlen(name) + 1;
	_name = new char[ len ];
	Q_strncpy( _name, name, len );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
IBorder::backgroundtype_e ImageBorder::GetBackgroundType()
{
	return m_eBackgroundType;
}