… | |
… | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | our $FOCUS; # the widget with current focus |
10 | use Crossfire::Client; |
11 | #our @ACTIVE_WIDGETS; |
11 | |
|
|
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
13 | |
|
|
14 | our $TOPLEVEL; |
|
|
15 | our $BUTTON_STATE; |
12 | |
16 | |
13 | # class methods for events |
17 | # class methods for events |
14 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
18 | sub feed_sdl_key_down_event { |
15 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
19 | $FOCUS->key_down ($_[0]) if $FOCUS; |
|
|
20 | } |
|
|
21 | |
|
|
22 | sub feed_sdl_key_up_event { |
|
|
23 | $FOCUS->key_up ($_[0]) if $FOCUS; |
|
|
24 | } |
|
|
25 | |
16 | sub feed_sdl_button_down_event { } |
26 | sub feed_sdl_button_down_event { |
|
|
27 | my ($ev) = @_; |
|
|
28 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
29 | |
|
|
30 | if (!$BUTTON_STATE) { |
|
|
31 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
32 | |
|
|
33 | $GRAB = $widget; |
|
|
34 | $GRAB->update if $GRAB; |
|
|
35 | } |
|
|
36 | |
|
|
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
|
|
38 | |
|
|
39 | $GRAB->button_down ($ev) if $GRAB; |
|
|
40 | } |
|
|
41 | |
17 | sub feed_sdl_button_up_event { } |
42 | sub feed_sdl_button_up_event { |
|
|
43 | my ($ev) = @_; |
|
|
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
45 | |
|
|
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
47 | |
|
|
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
49 | |
|
|
50 | if (!$BUTTON_STATE) { |
|
|
51 | my $grab = $GRAB; undef $GRAB; |
|
|
52 | $grab->update if $grab; |
|
|
53 | $GRAB->update if $GRAB; |
|
|
54 | } |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub feed_sdl_motion_event { |
|
|
58 | my ($ev) = @_; |
|
|
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
60 | |
|
|
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
62 | |
|
|
63 | if ($widget != $HOVER) { |
|
|
64 | my $hover = $HOVER; $HOVER = $widget; |
|
|
65 | |
|
|
66 | $hover->update if $hover; |
|
|
67 | $HOVER->update if $HOVER; |
|
|
68 | } |
|
|
69 | |
|
|
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
|
|
71 | } |
18 | |
72 | |
19 | sub new { |
73 | sub new { |
20 | my $class = shift; |
74 | my $class = shift; |
21 | |
75 | |
22 | bless { @_ }, $class |
76 | bless { @_ }, $class |
23 | } |
77 | } |
24 | |
|
|
25 | #sub activate { |
|
|
26 | # push @ACTIVE_WIDGETS, $_[0]; |
|
|
27 | # Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
|
|
28 | #} |
|
|
29 | |
|
|
30 | #sub deactivate { |
|
|
31 | # @ACTIVE_WIDGETS = |
|
|
32 | # sort { $a->{z} <=> $b->{z} } |
|
|
33 | # grep { $_ && $_ != $_[0] } |
|
|
34 | # @ACTIVE_WIDGETS; |
|
|
35 | #} |
|
|
36 | |
78 | |
37 | sub move { |
79 | sub move { |
38 | my ($self, $x, $y, $z) = @_; |
80 | my ($self, $x, $y, $z) = @_; |
39 | $self->{x} = $x; |
81 | $self->{x} = $x; |
40 | $self->{y} = $y; |
82 | $self->{y} = $y; |
… | |
… | |
44 | sub needs_redraw { |
86 | sub needs_redraw { |
45 | 0 |
87 | 0 |
46 | } |
88 | } |
47 | |
89 | |
48 | sub size_request { |
90 | sub size_request { |
|
|
91 | require Carp; |
49 | die "size_request is abtract"; |
92 | Carp::confess "size_request is abtract"; |
|
|
93 | } |
|
|
94 | |
|
|
95 | sub size_allocate { |
|
|
96 | my ($self, $w, $h) = @_; |
|
|
97 | |
|
|
98 | $self->{w} = $w; |
|
|
99 | $self->{h} = $h; |
50 | } |
100 | } |
51 | |
101 | |
52 | sub focus_in { |
102 | sub focus_in { |
53 | my ($widget) = @_; |
103 | my ($self) = @_; |
54 | $FOCUS = $widget; |
104 | |
|
|
105 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
106 | $focus->update if $focus; |
|
|
107 | $FOCUS->update; |
55 | } |
108 | } |
56 | |
109 | |
57 | sub focus_out { |
110 | sub focus_out { |
58 | my ($widget) = @_; |
111 | my ($self) = @_; |
59 | } |
|
|
60 | |
112 | |
61 | sub key_down { |
113 | return unless $FOCUS == $self; |
62 | my ($widget, $sdlev) = @_; |
|
|
63 | } |
|
|
64 | |
114 | |
65 | sub key_up { |
115 | my $focus = $FOCUS; undef $FOCUS; |
66 | my ($widget, $sdlev) = @_; |
116 | $focus->update if $focus; #? |
67 | } |
117 | } |
68 | |
118 | |
|
|
119 | sub mouse_motion { } |
|
|
120 | sub button_up { } |
69 | sub button_down { |
121 | sub button_down { } |
70 | my ($widget, $sdlev) = @_; |
122 | sub key_down { } |
71 | } |
123 | sub key_up { } |
72 | |
124 | |
73 | sub button_up { |
125 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
74 | my ($widget, $sdlev) = @_; |
126 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
75 | } |
|
|
76 | |
|
|
77 | sub w { $_[0]->{w} } |
|
|
78 | sub h { $_[0]->{h} } |
|
|
79 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
127 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
80 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
128 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
81 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
129 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
82 | |
130 | |
83 | sub draw { |
131 | sub draw { |
84 | my ($self) = @_; |
132 | my ($self) = @_; |
85 | |
133 | |
86 | glPushMatrix; |
134 | glPushMatrix; |
87 | glTranslate $self->{x}, $self->{y}, 0; |
135 | glTranslate $self->{x}, $self->{y}, 0; |
88 | $self->_draw; |
136 | $self->_draw; |
|
|
137 | if ($self == $HOVER) { |
|
|
138 | glColor 1, 1, 1, 0.4; |
|
|
139 | glEnable GL_BLEND; |
|
|
140 | glBegin GL_QUADS; |
|
|
141 | glVertex 0 , 0; |
|
|
142 | glVertex $self->{w}, 0; |
|
|
143 | glVertex $self->{w}, $self->{h}; |
|
|
144 | glVertex 0 , $self->{h}; |
|
|
145 | glEnd; |
|
|
146 | glDisable GL_BLEND; |
|
|
147 | } |
89 | glPopMatrix; |
148 | glPopMatrix; |
90 | } |
149 | } |
91 | |
150 | |
92 | sub _draw { |
151 | sub _draw { |
93 | my ($widget) = @_; |
152 | my ($self) = @_; |
|
|
153 | |
|
|
154 | warn "no draw defined for $self\n"; |
94 | } |
155 | } |
95 | |
156 | |
96 | sub bbox { |
157 | sub bbox { |
97 | my ($self) = @_; |
158 | my ($self) = @_; |
98 | my ($w, $h) = $self->size_request; |
159 | my ($w, $h) = $self->size_request; |
… | |
… | |
102 | $self->{x} = $w, |
163 | $self->{x} = $w, |
103 | $self->{y} = $h |
164 | $self->{y} = $h |
104 | ) |
165 | ) |
105 | } |
166 | } |
106 | |
167 | |
|
|
168 | sub find_widget { |
|
|
169 | my ($self, $x, $y) = @_; |
|
|
170 | |
|
|
171 | return $self |
|
|
172 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
173 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
174 | |
|
|
175 | () |
|
|
176 | } |
|
|
177 | |
107 | sub del_parent { $_[0]->{parent} = undef } |
178 | sub del_parent { $_[0]->{parent} = undef } |
108 | |
179 | |
109 | sub set_parent { |
180 | sub set_parent { |
110 | my ($self, $par) = @_; |
181 | my ($self, $par) = @_; |
111 | |
182 | |
… | |
… | |
128 | my ($self) = @_; |
199 | my ($self) = @_; |
129 | |
200 | |
130 | #$self->deactivate; |
201 | #$self->deactivate; |
131 | } |
202 | } |
132 | |
203 | |
|
|
204 | ############################################################################# |
|
|
205 | |
|
|
206 | package Crossfire::Client::Widget::Container; |
|
|
207 | |
|
|
208 | our @ISA = Crossfire::Client::Widget::; |
|
|
209 | |
|
|
210 | sub new { |
|
|
211 | my ($class, @widgets) = @_; |
|
|
212 | |
|
|
213 | my $self = $class->SUPER::new (children => []); |
|
|
214 | $self->add ($_) for @widgets; |
|
|
215 | |
|
|
216 | $self |
|
|
217 | } |
|
|
218 | |
|
|
219 | sub add { |
|
|
220 | my ($self, $chld, $expand) = @_; |
|
|
221 | |
|
|
222 | $chld->{expand} = $expand; |
|
|
223 | $chld->set_parent ($self); |
|
|
224 | |
|
|
225 | @{$self->{children}} = |
|
|
226 | sort { $a->{z} <=> $b->{z} } |
|
|
227 | @{$self->{children}}, $chld; |
|
|
228 | |
|
|
229 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
230 | if $self->{w}; #TODO: check for "realised state" |
|
|
231 | } |
|
|
232 | |
|
|
233 | sub remove { |
|
|
234 | my ($self, $widget) = @_; |
|
|
235 | |
|
|
236 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
|
|
237 | |
|
|
238 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
239 | } |
|
|
240 | |
|
|
241 | sub find_widget { |
|
|
242 | my ($self, $x, $y) = @_; |
|
|
243 | |
|
|
244 | $x -= $self->{x}; |
|
|
245 | $y -= $self->{y}; |
|
|
246 | |
|
|
247 | my $res; |
|
|
248 | |
|
|
249 | for (reverse @{ $self->{children} }) { |
|
|
250 | $res = $_->find_widget ($x, $y) |
|
|
251 | and return $res; |
|
|
252 | } |
|
|
253 | |
|
|
254 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
255 | } |
|
|
256 | |
|
|
257 | sub _draw { |
|
|
258 | my ($self) = @_; |
|
|
259 | |
|
|
260 | $_->draw for @{$self->{children}}; |
|
|
261 | } |
|
|
262 | |
|
|
263 | ############################################################################# |
|
|
264 | |
133 | package Crossfire::Client::Widget::Bin; |
265 | package Crossfire::Client::Widget::Bin; |
134 | |
266 | |
135 | our @ISA = Crossfire::Client::Widget::; |
267 | our @ISA = Crossfire::Client::Widget::Container::; |
136 | |
268 | |
137 | sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } |
|
|
138 | |
|
|
139 | sub get { $_[0]->{child} } |
269 | sub child { $_[0]->{children}[0] } |
140 | |
|
|
141 | sub remove { |
|
|
142 | my ($self, $chld) = @_; |
|
|
143 | delete $self->{child} |
|
|
144 | if $self->{child} == $chld; |
|
|
145 | } |
|
|
146 | |
270 | |
147 | sub size_request { |
271 | sub size_request { |
148 | $_[0]->{child}->size_request if $_[0]->{child} |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
149 | } |
273 | } |
150 | |
274 | |
151 | sub _draw { |
275 | sub size_allocate { |
152 | my ($self) = @_; |
276 | my ($self, $w, $h) = @_; |
153 | |
277 | |
154 | $self->{child}->draw; |
278 | $self->SUPER::size_allocate ($w, $h); |
|
|
279 | $self->{children}[0]->size_allocate ($w, $h) |
|
|
280 | if $self->{children}[0] |
155 | } |
281 | } |
156 | |
282 | |
|
|
283 | ############################################################################# |
|
|
284 | |
157 | package Crossfire::Client::Widget::Toplevel; |
285 | package Crossfire::Client::Widget::Window; |
158 | |
286 | |
159 | our @ISA = Crossfire::Client::Widget::; |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
160 | |
288 | |
161 | use SDL::OpenGL; |
289 | use SDL::OpenGL; |
162 | |
290 | |
163 | sub add { |
291 | sub new { |
164 | my ($self, $chld) = @_; |
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
165 | |
293 | |
166 | push @{$self->{childs}}, $chld; |
294 | my $self = $class->SUPER::new; |
167 | @{$self->{childs}} = |
|
|
168 | sort { $a->{z} <=> $b->{z} } |
|
|
169 | @{$self->{childs}}; |
|
|
170 | |
295 | |
171 | $chld->set_parent ($self); |
296 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
172 | } |
|
|
173 | |
|
|
174 | sub remove { |
|
|
175 | my ($self, $chld) = @_; |
|
|
176 | @{$self->{childs}} = |
|
|
177 | sort { $a->{z} <=> $b->{z} } |
|
|
178 | grep { $_ && $_ != $_[0] } |
|
|
179 | @{$self->{childs}} |
|
|
180 | } |
297 | } |
181 | |
298 | |
182 | sub update { |
299 | sub update { |
183 | my ($self) = @_; |
300 | my ($self) = @_; |
184 | ::refresh (); |
|
|
185 | } |
|
|
186 | |
301 | |
187 | sub _draw { |
|
|
188 | my ($self) = @_; |
|
|
189 | |
|
|
190 | $_->draw for @{$self->{childs}}; |
|
|
191 | } |
|
|
192 | |
|
|
193 | package Crossfire::Client::Widget::Window; |
|
|
194 | |
|
|
195 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
196 | |
|
|
197 | use SDL::OpenGL; |
|
|
198 | |
|
|
199 | sub add { |
|
|
200 | my ($self, $chld) = @_; |
|
|
201 | $self->SUPER::add ($chld); |
|
|
202 | $chld->set_parent ($self); |
|
|
203 | $self->update; #TODO: Move this to the size_request event propably? |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub remove { |
|
|
207 | my ($self) = @_; |
|
|
208 | # TODO FIXME: removing a child from a window will crash, see render_chld |
|
|
209 | $self->update; |
|
|
210 | } |
|
|
211 | |
|
|
212 | sub update { |
|
|
213 | my ($self) = @_; |
|
|
214 | $self->render_chld; |
302 | $self->render_chld; |
|
|
303 | $self->SUPER::update; |
215 | } |
304 | } |
216 | |
305 | |
217 | sub render_chld { |
306 | sub render_chld { |
218 | my ($self) = @_; |
307 | my ($self) = @_; |
219 | my $chld = $self->get; |
|
|
220 | my ($w, $h) = $self->size_request; |
|
|
221 | |
308 | |
222 | $self->{texture} = |
309 | $self->{texture} = |
223 | Crossfire::Client::Texture->new_from_opengl ( |
310 | Crossfire::Client::Texture->new_from_opengl ( |
224 | $w, $h, sub { $chld->draw } |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
225 | ); |
312 | ); |
226 | $self->{texture}->upload; |
|
|
227 | } |
313 | } |
228 | |
314 | |
229 | sub size_request { |
315 | sub size_allocate { |
230 | my ($self) = @_; |
316 | my ($self, $w, $h) = @_; |
231 | my $chld = $self->get |
317 | |
232 | or return (0, 0); |
318 | $self->{w} = $w; |
233 | $chld->size_request |
319 | $self->{h} = $h; |
|
|
320 | |
|
|
321 | $self->child->size_allocate ($w, $h); |
|
|
322 | |
|
|
323 | $self->render_chld; |
234 | } |
324 | } |
235 | |
325 | |
236 | sub _draw { |
326 | sub _draw { |
237 | my ($self) = @_; |
327 | my ($self) = @_; |
238 | |
328 | |
239 | my ($w, $h) = $self->size_request;#TODO# use width/height of texture |
329 | my ($w, $h) = ($self->w, $self->h); |
240 | |
330 | |
241 | my $tex = $self->{texture} |
331 | my $tex = $self->{texture} |
242 | or return; |
332 | or return; |
243 | |
333 | |
244 | glEnable GL_BLEND; |
334 | glEnable GL_BLEND; |
… | |
… | |
255 | |
345 | |
256 | glDisable GL_BLEND; |
346 | glDisable GL_BLEND; |
257 | glDisable GL_TEXTURE_2D; |
347 | glDisable GL_TEXTURE_2D; |
258 | } |
348 | } |
259 | |
349 | |
|
|
350 | ############################################################################# |
|
|
351 | |
260 | package Crossfire::Client::Widget::Frame; |
352 | package Crossfire::Client::Widget::Frame; |
261 | |
353 | |
262 | our @ISA = Crossfire::Client::Widget::Bin::; |
354 | our @ISA = Crossfire::Client::Widget::Bin::; |
263 | |
355 | |
264 | use SDL::OpenGL; |
356 | use SDL::OpenGL; |
265 | |
357 | |
266 | sub size_request { |
358 | sub size_request { |
267 | my ($self) = @_; |
359 | my ($self) = @_; |
268 | my $chld = $self->get |
360 | my $chld = $self->child |
269 | or return (0, 0); |
361 | or return (0, 0); |
270 | |
362 | |
271 | $chld->move (2, 2); |
363 | $chld->move (2, 2); |
272 | |
364 | |
273 | map { $_ + 4 } $chld->size_request; |
365 | map { $_ + 4 } $chld->size_request; |
274 | } |
366 | } |
275 | |
367 | |
|
|
368 | sub size_allocate { |
|
|
369 | my ($self, $w, $h) = @_; |
|
|
370 | |
|
|
371 | $self->{w} = $w; |
|
|
372 | $self->{h} = $h; |
|
|
373 | |
|
|
374 | $self->child->size_allocate ($w - 4, $h - 4); |
|
|
375 | $self->child->move (2, 2); |
|
|
376 | } |
|
|
377 | |
276 | sub _draw { |
378 | sub _draw { |
277 | my ($self) = @_; |
379 | my ($self) = @_; |
278 | |
380 | |
279 | my $chld = $self->get; |
381 | my $chld = $self->child; |
280 | |
382 | |
281 | my ($w, $h) = $chld->size_request; |
383 | my ($w, $h) = $chld->size_request; |
282 | |
384 | |
283 | glBegin GL_QUADS; |
385 | glBegin GL_QUADS; |
284 | glColor 0, 0, 0; |
386 | glColor 0, 0, 0; |
… | |
… | |
289 | glEnd; |
391 | glEnd; |
290 | |
392 | |
291 | $chld->draw; |
393 | $chld->draw; |
292 | } |
394 | } |
293 | |
395 | |
|
|
396 | ############################################################################# |
|
|
397 | |
294 | package Crossfire::Client::Widget::FancyFrame; |
398 | package Crossfire::Client::Widget::FancyFrame; |
295 | |
399 | |
296 | our @ISA = Crossfire::Client::Widget::Frame::; |
400 | our @ISA = Crossfire::Client::Widget::Bin::; |
297 | |
401 | |
298 | use SDL::OpenGL; |
402 | use SDL::OpenGL; |
299 | |
403 | |
300 | sub new { |
404 | my @tex = |
301 | my ($self, $theme) = @_; |
|
|
302 | $self = $self->SUPER::new; |
|
|
303 | |
|
|
304 | $self->{txts} = [ |
|
|
305 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
405 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
306 | qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ |
406 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
307 | ]; |
|
|
308 | $self |
|
|
309 | |
|
|
310 | } |
|
|
311 | |
407 | |
312 | sub size_request { |
408 | sub size_request { |
313 | my ($self) = @_; |
409 | my ($self) = @_; |
|
|
410 | |
314 | my ($w, $h) = $self->get->size_request; |
411 | my ($w, $h) = $self->SUPER::size_request; |
315 | |
412 | |
316 | $h += $self->{txts}->[1]->{height}; |
413 | $h += $tex[1]->{height}; |
317 | $h += $self->{txts}->[4]->{height}; |
414 | $h += $tex[4]->{height}; |
318 | $w += $self->{txts}->[2]->{width}; |
415 | $w += $tex[2]->{width}; |
319 | $w += $self->{txts}->[3]->{width}; |
416 | $w += $tex[3]->{width}; |
320 | |
|
|
321 | $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
|
|
322 | |
417 | |
323 | ($w, $h) |
418 | ($w, $h) |
324 | } |
419 | } |
325 | |
420 | |
|
|
421 | sub size_allocate { |
|
|
422 | my ($self, $w, $h) = @_; |
|
|
423 | |
|
|
424 | $self->SUPER::size_allocate ($w, $h); |
|
|
425 | |
|
|
426 | $h -= $tex[1]->{height}; |
|
|
427 | $h -= $tex[4]->{height}; |
|
|
428 | $w -= $tex[2]->{width}; |
|
|
429 | $w -= $tex[3]->{width}; |
|
|
430 | |
|
|
431 | $h = $h < 0 ? 0 : $h; |
|
|
432 | $w = $w < 0 ? 0 : $w; |
|
|
433 | |
|
|
434 | $self->child->size_allocate ($w, $h); |
|
|
435 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
|
|
436 | } |
|
|
437 | |
326 | sub _draw { |
438 | sub _draw { |
327 | my ($self) = @_; |
439 | my ($self) = @_; |
328 | |
440 | |
329 | my ($w, $h) = $self->size_request; |
441 | my ($w, $h) = ($self->{w}, $self->{h}); |
330 | my ($cw, $ch) = $self->get->size_request; |
442 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
331 | |
443 | |
332 | glEnable GL_BLEND; |
444 | glEnable GL_BLEND; |
333 | glEnable GL_TEXTURE_2D; |
445 | glEnable GL_TEXTURE_2D; |
334 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
446 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
335 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
336 | |
448 | |
337 | |
|
|
338 | my $top = $self->{txts}->[1]; |
449 | my $top = $tex[1]; |
339 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
450 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
340 | |
451 | |
341 | glColor 1, 1, 1, 0.8; |
|
|
342 | |
|
|
343 | glBegin GL_QUADS; |
452 | glBegin GL_QUADS; |
344 | glTexCoord 0, 0; glVertex 0 , 0; |
453 | glTexCoord 0, 0; glVertex 0 , 0; |
345 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
454 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
346 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
455 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
347 | glTexCoord 1, 0; glVertex $w , 0; |
456 | glTexCoord 1, 0; glVertex $w , 0; |
348 | glEnd; |
457 | glEnd; |
349 | |
458 | |
350 | my $left = $self->{txts}->[3]; |
459 | my $left = $tex[3]; |
351 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
460 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
352 | |
461 | |
353 | glColor 1, 1, 1, 0.8; |
|
|
354 | |
|
|
355 | glBegin GL_QUADS; |
462 | glBegin GL_QUADS; |
356 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
463 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
357 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
464 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
358 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
465 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
359 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
466 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
360 | glEnd; |
467 | glEnd; |
361 | |
468 | |
362 | my $right = $self->{txts}->[2]; |
469 | my $right = $tex[2]; |
363 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
470 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
364 | |
471 | |
365 | glColor 1, 1, 1, 0.8; |
|
|
366 | |
|
|
367 | glBegin GL_QUADS; |
472 | glBegin GL_QUADS; |
368 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
473 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
369 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
474 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
370 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
475 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
371 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
476 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
372 | glEnd; |
477 | glEnd; |
373 | |
478 | |
374 | my $bottom = $self->{txts}->[4]; |
479 | my $bottom = $tex[4]; |
375 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
480 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
376 | |
481 | |
377 | glColor 1, 1, 1, 0.8; |
|
|
378 | |
|
|
379 | glBegin GL_QUADS; |
482 | glBegin GL_QUADS; |
380 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
483 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
381 | glTexCoord 0, 1; glVertex 0 , $h; |
484 | glTexCoord 0, 1; glVertex 0 , $h; |
382 | glTexCoord 1, 1; glVertex $w , $h; |
485 | glTexCoord 1, 1; glVertex $w , $h; |
383 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
486 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
384 | glEnd; |
487 | glEnd; |
385 | |
488 | |
386 | my $bg = $self->{txts}->[0]; |
489 | my $bg = $tex[0]; |
387 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
490 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
491 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
388 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
492 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
389 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
493 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
390 | |
494 | |
391 | glColor 1, 1, 1, 0.8; |
|
|
392 | |
|
|
393 | my $rep_x = $cw / $bg->{width}; |
495 | my $rep_x = $cw / $bg->{width}; |
394 | my $rep_y = $ch / $bg->{height}; |
496 | my $rep_y = $ch / $bg->{height}; |
395 | |
497 | |
396 | glBegin GL_QUADS; |
498 | glBegin GL_QUADS; |
397 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
499 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
398 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
500 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
399 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
501 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
400 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
502 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
401 | glEnd; |
503 | glEnd; |
402 | |
|
|
403 | $self->get->draw; |
|
|
404 | |
504 | |
405 | glDisable GL_BLEND; |
505 | glDisable GL_BLEND; |
406 | glDisable GL_TEXTURE_2D; |
506 | glDisable GL_TEXTURE_2D; |
|
|
507 | |
|
|
508 | $self->child->draw; |
|
|
509 | |
407 | } |
510 | } |
|
|
511 | |
|
|
512 | ############################################################################# |
408 | |
513 | |
409 | package Crossfire::Client::Widget::Table; |
514 | package Crossfire::Client::Widget::Table; |
410 | |
515 | |
411 | our @ISA = Crossfire::Client::Widget::Bin::; |
516 | our @ISA = Crossfire::Client::Widget::Bin::; |
412 | |
517 | |
413 | use SDL::OpenGL; |
518 | use SDL::OpenGL; |
414 | |
519 | |
415 | sub add { |
520 | sub add { |
416 | my ($self, $x, $y, $chld) = @_; |
521 | my ($self, $x, $y, $chld) = @_; |
417 | my $old_chld = $self->{childs}[$y][$x]; |
522 | my $old_chld = $self->{children}[$y][$x]; |
418 | |
523 | |
419 | $self->{childs}[$y][$x] = $chld; |
524 | $self->{children}[$y][$x] = $chld; |
420 | $chld->set_parent ($self); |
525 | $chld->set_parent ($self); |
421 | $self->update; |
526 | $self->update; |
422 | } |
527 | } |
423 | |
528 | |
424 | sub max_row_height { |
529 | sub max_row_height { |
425 | my ($self, $row) = @_; |
530 | my ($self, $row) = @_; |
426 | |
531 | |
427 | my $hs = 0; |
532 | my $hs = 0; |
428 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { |
533 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
429 | my $c = $self->{childs}->[$row]->[$xi]; |
534 | my $c = $self->{children}->[$row]->[$xi]; |
430 | if ($c) { |
535 | if ($c) { |
431 | my ($w, $h) = $c->size_request; |
536 | my ($w, $h) = $c->size_request; |
432 | if ($hs < $h) { $hs = $h } |
537 | if ($hs < $h) { $hs = $h } |
433 | } |
538 | } |
434 | } |
539 | } |
… | |
… | |
437 | |
542 | |
438 | sub max_col_width { |
543 | sub max_col_width { |
439 | my ($self, $col) = @_; |
544 | my ($self, $col) = @_; |
440 | |
545 | |
441 | my $ws = 0; |
546 | my $ws = 0; |
442 | for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { |
547 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
443 | my $c = ($self->{childs}->[$yi] || [])->[$col]; |
548 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
444 | if ($c) { |
549 | if ($c) { |
445 | my ($w, $h) = $c->size_request; |
550 | my ($w, $h) = $c->size_request; |
446 | if ($ws < $w) { $ws = $w } |
551 | if ($ws < $w) { $ws = $w } |
447 | } |
552 | } |
448 | } |
553 | } |
… | |
… | |
452 | sub size_request { |
557 | sub size_request { |
453 | my ($self) = @_; |
558 | my ($self) = @_; |
454 | |
559 | |
455 | my ($hs, $ws) = (0, 0); |
560 | my ($hs, $ws) = (0, 0); |
456 | |
561 | |
457 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
562 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
458 | $hs += $self->max_row_height ($yi); |
563 | $hs += $self->max_row_height ($yi); |
459 | } |
564 | } |
460 | |
565 | |
461 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
566 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
462 | my $wm = 0; |
567 | my $wm = 0; |
463 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
568 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
464 | $wm += $self->max_col_width ($xi) |
569 | $wm += $self->max_col_width ($xi) |
465 | } |
570 | } |
466 | if ($ws < $wm) { $ws = $wm } |
571 | if ($ws < $wm) { $ws = $wm } |
467 | } |
572 | } |
468 | |
573 | |
… | |
… | |
471 | |
576 | |
472 | sub _draw { |
577 | sub _draw { |
473 | my ($self) = @_; |
578 | my ($self) = @_; |
474 | |
579 | |
475 | my $y = 0; |
580 | my $y = 0; |
476 | for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { |
581 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
477 | my $x = 0; |
582 | my $x = 0; |
478 | |
583 | |
479 | for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { |
584 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
480 | |
585 | |
481 | my $c = $self->{childs}->[$yi]->[$xi]; |
586 | my $c = $self->{children}->[$yi]->[$xi]; |
482 | if ($c) { |
587 | if ($c) { |
483 | $c->move ($x, $y, 0); #TODO: Move to size_request |
588 | $c->move ($x, $y, 0); #TODO: Move to size_request |
484 | $c->draw if $c; |
589 | $c->draw if $c; |
485 | } |
590 | } |
486 | |
591 | |
… | |
… | |
489 | |
594 | |
490 | $y += $self->max_row_height ($yi); |
595 | $y += $self->max_row_height ($yi); |
491 | } |
596 | } |
492 | } |
597 | } |
493 | |
598 | |
|
|
599 | ############################################################################# |
|
|
600 | |
494 | package Crossfire::Client::Widget::VBox; |
601 | package Crossfire::Client::Widget::VBox; |
495 | |
602 | |
496 | our @ISA = Crossfire::Client::Widget::Bin::; |
603 | our @ISA = Crossfire::Client::Widget::Container::; |
497 | |
604 | |
498 | use SDL::OpenGL; |
605 | use SDL::OpenGL; |
499 | |
606 | |
500 | sub add { |
|
|
501 | my ($self, $chld) = @_; |
|
|
502 | push @{$self->{childs}}, $chld; |
|
|
503 | $chld->set_parent ($self); |
|
|
504 | $self->update; |
|
|
505 | } |
|
|
506 | |
|
|
507 | sub size_request { |
607 | sub size_request { |
508 | my ($self) = @_; |
608 | my ($self) = @_; |
509 | |
609 | |
510 | my ($hs, $ws) = (0, 0); |
610 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
611 | |
|
|
612 | ( |
|
|
613 | (List::Util::max map $_->[0], @alloc), |
|
|
614 | (List::Util::sum map $_->[1], @alloc), |
|
|
615 | ) |
|
|
616 | } |
|
|
617 | |
|
|
618 | sub size_allocate { |
|
|
619 | my ($self, $w, $h) = @_; |
|
|
620 | |
|
|
621 | $self->w ($w); |
|
|
622 | $self->h ($h); |
|
|
623 | |
|
|
624 | my $exp; |
|
|
625 | my @oth; |
|
|
626 | # find expand widget |
511 | for (@{$self->{childs} || []}) { |
627 | for (@{$self->{children}}) { |
|
|
628 | if ($_->{expand}) { |
|
|
629 | $exp = $_; |
|
|
630 | last; |
|
|
631 | } |
|
|
632 | push @oth, $_; |
|
|
633 | } |
|
|
634 | |
|
|
635 | my ($ow, $oh); |
|
|
636 | |
|
|
637 | # get sizes of other widgets |
|
|
638 | for (@oth) { |
512 | my ($w, $h) = $_->size_request; |
639 | my ($w, $h) = $_->size_request; |
513 | $hs += $h; |
640 | $oh += $h; |
514 | if ($ws < $w) { $ws = $w } |
641 | if ($ow < $w) { $ow = $w } |
515 | } |
642 | } |
516 | |
643 | |
517 | return ($ws, $hs); |
644 | my $y = 0; |
518 | } |
|
|
519 | |
|
|
520 | sub _draw { |
|
|
521 | my ($self) = @_; |
|
|
522 | |
|
|
523 | my ($x, $y); |
|
|
524 | for (@{$self->{childs} || []}) { |
645 | for (@{$self->{children}}) { |
525 | $_->move (0, $y, 0); #TODO: move to size_request |
646 | $_->move (0, $y); |
526 | $_->draw; |
647 | |
|
|
648 | if ($_ == $exp) { |
|
|
649 | $_->size_allocate ($w, $h - $oh); |
|
|
650 | $y += $h - $oh; |
|
|
651 | } else { |
527 | my ($w, $h) = $_->size_request; |
652 | my ($cw, $h) = $_->size_request; |
|
|
653 | $_->size_allocate ($w, $h); |
528 | $y += $h; |
654 | $y += $h; |
|
|
655 | } |
529 | } |
656 | } |
530 | } |
657 | } |
|
|
658 | |
|
|
659 | ############################################################################# |
531 | |
660 | |
532 | package Crossfire::Client::Widget::Label; |
661 | package Crossfire::Client::Widget::Label; |
533 | |
662 | |
534 | our @ISA = Crossfire::Client::Widget::; |
663 | our @ISA = Crossfire::Client::Widget::; |
535 | |
664 | |
536 | use SDL::OpenGL; |
665 | use SDL::OpenGL; |
537 | |
666 | |
538 | sub new { |
667 | sub new { |
539 | my ($class, $x, $y, $z, $height, $text) = @_; |
668 | my ($class, $x, $y, $z, $height, $text) = @_; |
540 | |
669 | |
|
|
670 | $height ||= $::FONTSIZE; |
|
|
671 | |
541 | # TODO: color, and make height, xyz etc. optional |
672 | # TODO: color, and make height, xyz etc. optional |
542 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
673 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
543 | |
674 | |
544 | $self->set_text ($text); |
675 | $self->set_text ($text); |
545 | |
676 | |
… | |
… | |
562 | } |
693 | } |
563 | |
694 | |
564 | sub size_request { |
695 | sub size_request { |
565 | my ($self) = @_; |
696 | my ($self) = @_; |
566 | |
697 | |
567 | ( |
698 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
699 | ( |
568 | $self->{texture}{width}, |
700 | $self->{texture}{width}, |
569 | $self->{texture}{height}, |
701 | $self->{texture}{height}, |
|
|
702 | ) |
|
|
703 | } else { |
|
|
704 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
|
|
705 | |
|
|
706 | ($w, $h) |
570 | ) |
707 | } |
|
|
708 | |
571 | } |
709 | } |
572 | |
710 | |
573 | sub _draw { |
711 | sub _draw { |
574 | my ($self) = @_; |
712 | my ($self) = @_; |
575 | |
713 | |
… | |
… | |
579 | glEnable GL_TEXTURE_2D; |
717 | glEnable GL_TEXTURE_2D; |
580 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
718 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
581 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
719 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
582 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
720 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
583 | |
721 | |
584 | glColor 1, 1, 1, 0.6; # TODO color |
722 | glColor 1, 0, 0, 1; # TODO color |
585 | |
723 | |
586 | glBegin GL_QUADS; |
724 | glBegin GL_QUADS; |
587 | glTexCoord 0, 0; glVertex 0 , 0; |
725 | glTexCoord 0, 0; glVertex 0 , 0; |
588 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
726 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
589 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
727 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
… | |
… | |
592 | |
730 | |
593 | glDisable GL_BLEND; |
731 | glDisable GL_BLEND; |
594 | glDisable GL_TEXTURE_2D; |
732 | glDisable GL_TEXTURE_2D; |
595 | } |
733 | } |
596 | |
734 | |
|
|
735 | ############################################################################# |
|
|
736 | |
597 | package Crossfire::Client::Widget::TextEntry; |
737 | package Crossfire::Client::Widget::Entry; |
598 | |
738 | |
599 | our @ISA = Crossfire::Client::Widget::Label::; |
739 | our @ISA = Crossfire::Client::Widget::Label::; |
600 | |
740 | |
601 | use SDL; |
741 | use SDL; |
602 | use SDL::OpenGL; |
742 | use SDL::OpenGL; |
… | |
… | |
605 | my ($self, $ev) = @_; |
745 | my ($self, $ev) = @_; |
606 | |
746 | |
607 | my $mod = $ev->key_mod; |
747 | my $mod = $ev->key_mod; |
608 | my $sym = $ev->key_sym; |
748 | my $sym = $ev->key_sym; |
609 | |
749 | |
610 | $ev->set_unicode (1); |
|
|
611 | my $uni = $ev->key_unicode; |
750 | my $uni = $ev->key_unicode; |
612 | |
751 | |
613 | my $text = $self->get_text; |
752 | my $text = $self->get_text; |
614 | |
753 | |
615 | if ($sym == SDLK_BACKSPACE) { |
754 | if ($sym == SDLK_BACKSPACE) { |
616 | substr $text, -1, 1, ''; |
755 | substr $text, -1, 1, ''; |
617 | |
|
|
618 | } elsif ($uni) { |
756 | } elsif ($uni) { |
619 | $text .= chr $uni; |
757 | $text .= chr $uni; |
|
|
758 | print "$uni <$text>\n";#d# |
620 | } |
759 | } |
|
|
760 | |
621 | $self->set_text ($text); |
761 | $self->set_text ($text); |
622 | } |
762 | } |
623 | |
763 | |
|
|
764 | sub button_down { |
|
|
765 | my ($self, $ev) = @_; |
624 | |
766 | |
625 | # XXX: TextView isn't neccessary with pango multiline text rendering |
767 | $self->focus_in; |
626 | package Crossfire::Client::Widget::TextView; |
|
|
627 | |
|
|
628 | use strict; |
|
|
629 | |
|
|
630 | our @ISA = qw/Crossfire::Client::Widget/; |
|
|
631 | |
|
|
632 | use SDL::OpenGL; |
|
|
633 | use SDL::OpenGL::Constants; |
|
|
634 | |
|
|
635 | sub new { |
|
|
636 | my ($class, $text, $h) = @_; |
|
|
637 | my $self = $class->SUPER::new (); |
|
|
638 | |
|
|
639 | $self->{txt_height} = $h; |
|
|
640 | @{$self->{lines}} = split /\r?\n/, $text; |
|
|
641 | |
|
|
642 | for (split /\r?\n/, $text) { |
|
|
643 | $self->add_line ($_); |
|
|
644 | } |
|
|
645 | $self |
|
|
646 | } |
768 | } |
647 | |
769 | |
648 | #sub render_lines { |
770 | sub _draw { |
649 | # my ($self) = @_; |
771 | my ($self) = @_; |
650 | # |
772 | |
651 | # $self->{txt_lines} = []; |
773 | if ($FOCUS == $self) { |
652 | # |
774 | glColor 1, 1, 1; |
653 | # for (@{$self->{lines}}) { |
775 | } else { |
654 | # push @{$self->{txt_lines}}, |
776 | glColor 0.7, 0.7, 0.7; |
655 | # new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_; |
|
|
656 | # } |
777 | } |
657 | #} |
|
|
658 | |
|
|
659 | sub add_line { |
|
|
660 | my ($self, $line) = @_; |
|
|
661 | push @{$self->{lines}}, $line; |
|
|
662 | |
|
|
663 | push @{$self->{txt_lines}}, |
|
|
664 | new_from_text Crossfire::Client::Texture $line, $self->{txt_height}; |
|
|
665 | } |
|
|
666 | |
|
|
667 | sub size_request { |
|
|
668 | my ($self) = @_; |
|
|
669 | |
|
|
670 | my $w = 0; |
|
|
671 | my $h = 0; |
|
|
672 | |
|
|
673 | for (@{$self->{txt_lines}}) { |
|
|
674 | if ($w < $_->{width}) { $w = $_->{width} } |
|
|
675 | $h += $_->{height}; |
|
|
676 | } |
|
|
677 | |
|
|
678 | return ($w, $h); |
|
|
679 | } |
|
|
680 | |
|
|
681 | sub draw_line { |
|
|
682 | my ($self, $tex, $y) = @_; |
|
|
683 | |
|
|
684 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
685 | |
|
|
686 | glColor 1, 0, 1; |
|
|
687 | |
778 | |
688 | glBegin GL_QUADS; |
779 | glBegin GL_QUADS; |
689 | glTexCoord 0, 0; glVertex 0 , $y; |
780 | glVertex 0 , 0; |
690 | glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; |
781 | glVertex 0 , $self->{h} - 1; |
691 | glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; |
782 | glVertex $self->{w} - 1, $self->{h} - 1; |
692 | glTexCoord 1, 0; glVertex $tex->{width}, $y; |
783 | glVertex $self->{w} - 1, 0; |
693 | glEnd; |
784 | glEnd; |
694 | } |
|
|
695 | |
785 | |
696 | sub _draw { |
786 | $self->SUPER::_draw; |
697 | my ($self) = @_; |
|
|
698 | |
|
|
699 | glEnable GL_BLEND; |
|
|
700 | glEnable GL_TEXTURE_2D; |
|
|
701 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL; |
|
|
702 | |
|
|
703 | my $l = 0; |
|
|
704 | for (@{$self->{txt_lines}}) { |
|
|
705 | $self->draw_line ($_, $l); |
|
|
706 | $l += $_->{height}; |
|
|
707 | } |
|
|
708 | |
|
|
709 | glDisable GL_BLEND; |
|
|
710 | glDisable GL_TEXTURE_2D; |
|
|
711 | } |
787 | } |
|
|
788 | |
|
|
789 | ############################################################################# |
712 | |
790 | |
713 | package Crossfire::Client::Widget::MapWidget; |
791 | package Crossfire::Client::Widget::MapWidget; |
714 | |
792 | |
715 | use strict; |
793 | use strict; |
716 | |
794 | |
… | |
… | |
725 | sub key_down { |
803 | sub key_down { |
726 | print "MAPKEYDOWN\n"; |
804 | print "MAPKEYDOWN\n"; |
727 | } |
805 | } |
728 | |
806 | |
729 | sub key_up { |
807 | sub key_up { |
|
|
808 | } |
|
|
809 | |
|
|
810 | sub size_request { |
|
|
811 | ( |
|
|
812 | 1 + int $::WIDTH / 32, |
|
|
813 | 1 + int $::HEIGHT / 32, |
|
|
814 | ) |
730 | } |
815 | } |
731 | |
816 | |
732 | sub _draw { |
817 | sub _draw { |
733 | my ($self) = @_; |
818 | my ($self) = @_; |
734 | |
819 | |
… | |
… | |
767 | my $cell = $map->[$x + $xofs][$y + $yofs] |
852 | my $cell = $map->[$x + $xofs][$y + $yofs] |
768 | or next; |
853 | or next; |
769 | |
854 | |
770 | my $darkness = $cell->[0] * (1 / 255); |
855 | my $darkness = $cell->[0] * (1 / 255); |
771 | if ($darkness < 0) { |
856 | if ($darkness < 0) { |
772 | $darkness = 0.15; |
857 | $darkness = $cell->[1] ? 0.2 : 0; |
773 | } |
858 | } |
774 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
859 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
775 | |
860 | |
776 | for my $num (grep $_, @$cell[1,2,3]) { |
861 | for my $num (grep $_, @$cell[1,2,3]) { |
777 | my $tex = $::CONN->{face}[$num]{texture} || next; |
862 | my $tex = $::CONN->{face}[$num]{texture} || next; |
… | |
… | |
806 | |
891 | |
807 | $lighting = new Crossfire::Client::Texture |
892 | $lighting = new Crossfire::Client::Texture |
808 | width => $sw4, |
893 | width => $sw4, |
809 | height => $sh, |
894 | height => $sh, |
810 | data => $lighting, |
895 | data => $lighting, |
811 | internalformat => GL_ALPHA4, |
896 | internalformat => GL_ALPHA, |
812 | format => GL_ALPHA; |
897 | format => GL_ALPHA; |
813 | |
898 | |
814 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
899 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
815 | glColor 0, 0, 0, 0.75; |
900 | glColor 0.5, 0.5, 0.5, 1; |
816 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
901 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
817 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
902 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
818 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
903 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
819 | glBegin GL_QUADS; |
904 | glBegin GL_QUADS; |
820 | glTexCoord 0, 0; glVertex 0 , 0; |
905 | glTexCoord 0, 0; glVertex 0 , 0; |
… | |
… | |
876 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
961 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
877 | $::CONN->send ("command run_stop"); |
962 | $::CONN->send ("command run_stop"); |
878 | } |
963 | } |
879 | } |
964 | } |
880 | |
965 | |
|
|
966 | ############################################################################# |
|
|
967 | |
881 | package Crossfire::Client::Widget::Animator; |
968 | package Crossfire::Client::Widget::Animator; |
882 | |
969 | |
883 | use SDL::OpenGL; |
970 | use SDL::OpenGL; |
884 | |
971 | |
885 | our @ISA = Crossfire::Client::Widget::Bin::; |
972 | our @ISA = Crossfire::Client::Widget::Bin::; |
886 | |
973 | |
887 | sub moveto { |
974 | sub moveto { |
888 | my ($self, $x, $y) = @_; |
975 | my ($self, $x, $y) = @_; |
889 | |
976 | |
890 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
891 | $self->{speed} = 0.01; |
978 | $self->{speed} = 2; |
892 | $self->{time} = 1; |
979 | $self->{time} = 1; |
893 | |
980 | |
894 | ::animation_start $self; |
981 | ::animation_start $self; |
895 | } |
982 | } |
896 | |
983 | |
… | |
… | |
911 | |
998 | |
912 | sub _draw { |
999 | sub _draw { |
913 | my ($self) = @_; |
1000 | my ($self) = @_; |
914 | |
1001 | |
915 | glPushMatrix; |
1002 | glPushMatrix; |
916 | glRotate $self->{time} * 10000, 0, 1, 0; |
1003 | glRotate $self->{time} * 1000, 0, 1, 0; |
917 | $self->{child}->draw; |
1004 | $self->{children}[0]->draw; |
918 | glPopMatrix; |
1005 | glPopMatrix; |
919 | } |
1006 | } |
920 | |
1007 | |
921 | 1; |
1008 | ############################################################################# |
922 | |
1009 | |
|
|
1010 | package Crossfire::Client::Widget::Toplevel; |
|
|
1011 | |
|
|
1012 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
1013 | |
|
|
1014 | sub size_request { |
|
|
1015 | ($::WIDTH, $::HEIGHT) |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | sub size_allocate { |
|
|
1019 | my ($self, $w, $h) = @_; |
|
|
1020 | |
|
|
1021 | $self->SUPER::size_allocate ($w, $h); |
|
|
1022 | |
|
|
1023 | $_->size_allocate ($_->size_request) |
|
|
1024 | for @{$self->{children}}; |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | sub update { |
|
|
1028 | my ($self) = @_; |
|
|
1029 | |
|
|
1030 | $self->size_allocate ($self->size_request); |
|
|
1031 | ::refresh (); |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | sub add { |
|
|
1035 | my ($self, $widget) = @_; |
|
|
1036 | |
|
|
1037 | $self->SUPER::add ($widget); |
|
|
1038 | |
|
|
1039 | $widget->size_allocate ($widget->size_request); |
|
|
1040 | } |
|
|
1041 | |
|
|
1042 | sub draw { |
|
|
1043 | my ($self) = @_; |
|
|
1044 | |
|
|
1045 | $self->_draw; |
|
|
1046 | } |
|
|
1047 | |
|
|
1048 | ############################################################################# |
|
|
1049 | |
|
|
1050 | package Crossfire::Client::Widget; |
|
|
1051 | |
|
|
1052 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
|
|
1053 | |
|
|
1054 | 1 |
|
|
1055 | |