File: Text_button.cc

package info (click to toggle)
exult 1.2-4
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 9,040 kB
  • ctags: 10,543
  • sloc: cpp: 99,373; sh: 8,333; ansic: 4,659; makefile: 988; yacc: 769; lex: 313; xml: 19
file content (169 lines) | stat: -rw-r--r-- 4,784 bytes parent folder | download | duplicates (8)
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
/*
Copyright (C) 2001 The Exult Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "Text_button.h"
#include "gamewin.h"
#include "font.h"
#include "iwin8.h"
#include "Gump.h"

#define TB_FONTNUM			2

// Palette Indices
#define TB_OUTER_BORDER			133
#define TB_OUTER_BORDER_CORNER		142
#define TB_OUTER_BORDER_PUSHED_TOP	144
#define TB_OUTER_BORDER_PUSHED_LEFT	140

#define TB_INNER_BORDER_HIGHLIGHT	138
#define TB_INNER_BORDER_LOWLIGHT	142
#define TB_INNER_BORDER_CORNER		141
#define TB_INNER_BORDER_TR_HIGH		137
#define TB_INNER_BORDER_TR_CORNER	138
#define TB_INNER_BORDER_BL_CORNER	144

#define TB_BACKGROUND			140
#define TB_RT_HIGHLIGHT			139

Text_button::Text_button(Gump *p, const std::string &str, int x, int y, int w, int h)
	: Gump_button(p, 0, x, y, SF_OTHER), text(str), width(w), height(h)
{
	init();
}

void Text_button::init()
{
	// Must be at least 11 units high
	if (height < 11) height = 11;

	// Text y is based on gump height of 11
	text_y = 2 + (height - 11)/2;

	// We will get the text width
	int text_width = sman->get_font(TB_FONTNUM)->get_text_width(
								text.c_str());

	if (width < text_width + 4) width = text_width + 4;

	// We want to find the starting point for the text (horizontal)
	text_x = (width - text_width) >> 1;
}

void Text_button::paint()
{
	Image_window8 *iwin = gwin->get_win();

	int offset = 0;
	int px = x;
	int py = y;

	if (parent)
	{
		px += parent->get_x();
		py += parent->get_y();
	}

	// The the push dependant edges
	if (pushed)
	{
		// Top left corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py);
		// Bottom left corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py+height-1);
		// Top right corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py);
		// Top edge
		iwin->fill8(TB_OUTER_BORDER_PUSHED_TOP, width-2, 1, px+1, py);
		// Left edge
		iwin->fill8(TB_OUTER_BORDER_PUSHED_TOP, 1, height-2, px, py+1);

		offset = 1;
	}
	else
	{
		// Bottom right corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py+height-1);
		// Bottom left corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px, py+height-1);
		// Top right corner
		iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+width-1, py+height-1);
		// Bottom edge
		iwin->fill8(TB_OUTER_BORDER, width-2, 1, px+1, py+height-1);
		// Right edge
		iwin->fill8(TB_OUTER_BORDER, 1, height-2, px+width-1, py+1);
	}

	// 'Outer' Top and Left Edges

	// Top left corner
	iwin->fill8(TB_OUTER_BORDER_CORNER, 1, 1, px+offset, py+offset);
	// Top edge
	iwin->fill8(TB_OUTER_BORDER, width-2, 1, px+1+offset, py+offset);
	// Left edge
	iwin->fill8(TB_OUTER_BORDER, 1, height-2, px+offset, py+1+offset);
	
	// 'Inner' Edges

	// Top left corner
	iwin->fill8(TB_INNER_BORDER_CORNER, 1, 1, px+offset+1, py+offset+1);
	// Top Right corner
	iwin->fill8(TB_INNER_BORDER_TR_CORNER, 1, 1, px+width+offset-2, py+offset+1);
	// Top Right Highlight 1
	iwin->fill8(TB_INNER_BORDER_TR_HIGH, 1, 1, px+width+offset-3, py+offset+1);
	// Top Right Highlight 1
	iwin->fill8(TB_INNER_BORDER_TR_HIGH, 1, 1, px+width+offset-2, py+offset+2);
	// Bottom left corner
	iwin->fill8(TB_INNER_BORDER_BL_CORNER, 1, 1, px+offset+1, py+height+offset-2);

	// Top edge
	iwin->fill8(TB_INNER_BORDER_HIGHLIGHT, width-5, 1, px+2+offset, py+offset+1);
	// Left edge
	iwin->fill8(TB_INNER_BORDER_LOWLIGHT, 1, height-4, px+offset+1, py+2+offset);
	// Right edge
	iwin->fill8(TB_INNER_BORDER_HIGHLIGHT, 1, height-5, px+width+offset-2, py+3+offset);
	// Bottom edge
	iwin->fill8(TB_INNER_BORDER_LOWLIGHT, width-4, 1, px+2+offset, py+height+offset-2);

	// Background Fill 
	iwin->fill8(TB_BACKGROUND, width-4, height-4, px+2+offset, py+2+offset);
	// Top Right Highligh on Background 
	iwin->fill8(TB_RT_HIGHLIGHT, 1, 1, px+width+offset-3, py+offset+2);

	sman->paint_text(TB_FONTNUM, text.c_str(), px+text_x+offset, 
							py+text_y+offset);
}

int Text_button::on_widget(int mx, int my)
{
	int px = x;
	int py = y;

	if (parent)
	{
		px += parent->get_x();
		py += parent->get_y();
	}

	if (mx < px || mx >= px + width) return 0;
	if (my < py || my >= py + height) return 0;
	return 1;
}