ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.34 by elmex, Sun Apr 9 02:44:51 2006 UTC vs.
Revision 1.40 by root, Sun Apr 9 21:41:11 2006 UTC

6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10our $FOCUS; # the widget with current focus
11#our @ACTIVE_WIDGETS;
12 11
13# class methods for events 12# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16sub feed_sdl_button_down_event { } 15sub feed_sdl_button_down_event { }
20 my $class = shift; 19 my $class = shift;
21 20
22 bless { @_ }, $class 21 bless { @_ }, $class
23} 22}
24 23
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
37sub move { 24sub move {
38 my ($self, $x, $y, $z) = @_; 25 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 26 $self->{x} = $x;
40 $self->{y} = $y; 27 $self->{y} = $y;
41 $self->{z} = $z if defined $z; 28 $self->{z} = $z if defined $z;
44sub needs_redraw { 31sub needs_redraw {
45 0 32 0
46} 33}
47 34
48sub size_request { 35sub size_request {
36 require Carp;
49 die "size_request is abtract"; 37 Carp::confess "size_request is abtract";
38}
39
40sub size_allocate {
41 my ($self, $w, $h) = @_;
42
43 $self->{w} = $w;
44 $self->{h} = $h;
50} 45}
51 46
52sub focus_in { 47sub focus_in {
53 my ($widget) = @_; 48 my ($widget) = @_;
54 $FOCUS = $widget; 49 $FOCUS = $widget;
72 67
73sub button_up { 68sub button_up {
74 my ($widget, $sdlev) = @_; 69 my ($widget, $sdlev) = @_;
75} 70}
76 71
77sub w { $_[0]->{w} } 72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} }
78sub h { $_[0]->{h} } 73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
82 77
83sub draw { 78sub draw {
88 $self->_draw; 83 $self->_draw;
89 glPopMatrix; 84 glPopMatrix;
90} 85}
91 86
92sub _draw { 87sub _draw {
93 my ($widget) = @_; 88 my ($self) = @_;
89
90 warn "no draw defined for $self\n";
94} 91}
95 92
96sub bbox { 93sub bbox {
97 my ($self) = @_; 94 my ($self) = @_;
98 my ($w, $h) = $self->size_request; 95 my ($w, $h) = $self->size_request;
102 $self->{x} = $w, 99 $self->{x} = $w,
103 $self->{y} = $h 100 $self->{y} = $h
104 ) 101 )
105} 102}
106 103
104sub find_widget {
105 my ($self, $x, $y) = @_;
106
107 return $self
108 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
109 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
110
111 ()
112}
113
107sub del_parent { $_[0]->{parent} = undef } 114sub del_parent { $_[0]->{parent} = undef }
108 115
109sub set_parent { 116sub set_parent {
110 my ($self, $par) = @_; 117 my ($self, $par) = @_;
111 118
128 my ($self) = @_; 135 my ($self) = @_;
129 136
130 #$self->deactivate; 137 #$self->deactivate;
131} 138}
132 139
140#############################################################################
141
133package Crossfire::Client::Widget::Container; 142package Crossfire::Client::Widget::Container;
134 143
135our @ISA = Crossfire::Client::Widget::; 144our @ISA = Crossfire::Client::Widget::;
136 145
137use SDL::OpenGL; 146sub new {
147 my ($class, @widgets) = @_;
138 148
139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } 149 my $self = $class->SUPER::new (children => []);
140sub get { $_[0]->{child} } 150 $self->add ($_) for @widgets;
151
152 $self
153}
154
155sub add {
156 my ($self, $chld, $expand) = @_;
157
158 $chld->{expand} = $expand;
159 $chld->set_parent ($self);
160
161 @{$self->{children}} =
162 sort { $a->{z} <=> $b->{z} }
163 @{$self->{children}}, $chld;
164
165 $self->size_allocate ($self->{w}, $self->{h});
166}
167
141sub remove { 168sub remove {
169 my ($self, $widget) = @_;
170
171 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
172
173 $self->size_allocate ($self->{w}, $self->{h});
174}
175
176sub find_widget {
177 my ($self, $x, $y) = @_;
178
179 my $res;
180
181 for (@{ $self->{children} }) {
182 $res = $_->find_widget ($x, $y)
183 and return $res;
184 }
185
186 ()
187}
188
189sub size_request {
190 my ($self) = @_;
191
192 my ($hs, $ws) = (0, 0);
193 for (@{$self->{children} || []}) {
194 my ($w, $h) = $_->size_request;
195 $hs += $h;
196 if ($ws < $w) { $ws = $w }
197 }
198
199 return ($ws, $hs);
200}
201
202sub _draw {
203 my ($self) = @_;
204
205 $_->draw for @{$self->{children}};
206}
207
208#############################################################################
209
210package Crossfire::Client::Widget::Bin;
211
212our @ISA = Crossfire::Client::Widget::Container::;
213
214sub child { $_[0]->{children}[0] }
215
216sub size_request {
217 $_[0]{children}[0]->size_request if $_[0]{children}[0];
218}
219
220sub size_allocate {
142 my ($self, $chld) = @_; 221 my ($self, $w, $h) = @_;
143 delete $self->{child} 222 $self->SUPER::size_allocate ($w, $h);
144 if $self->{child} == $chld; 223 $self->{children}[0]->size_allocate ($w, $h)
224 if $self->{children}[0]
145} 225}
146 226
147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 227#############################################################################
148
149sub _draw { die "Containers can't be drawn!" }
150 228
151package Crossfire::Client::Widget::Toplevel; 229package Crossfire::Client::Widget::Toplevel;
152 230
231our @ISA = Crossfire::Client::Widget::Container::;
232
233sub update {
234 my ($self) = @_;
235
236 ::refresh ();
237}
238
239#############################################################################
240
241package Crossfire::Client::Widget::Window;
242
153our @ISA = Crossfire::Client::Widget::; 243our @ISA = Crossfire::Client::Widget::Bin::;
154 244
155use SDL::OpenGL; 245use SDL::OpenGL;
156 246
157sub add { 247sub add {
158 my ($self, $chld) = @_; 248 my ($self, $chld) = @_;
159 249 warn "ADD $chld\n";
160 push @{$self->{childs}}, $chld;
161 @{$self->{childs}} =
162 sort { $a->{z} <=> $b->{z} }
163 @{$self->{childs}};
164
165 $chld->set_parent ($self);
166}
167
168sub remove {
169 my ($self, $chld) = @_;
170 @{$self->{childs}} =
171 sort { $a->{z} <=> $b->{z} }
172 grep { $_ && $_ != $_[0] }
173 @{$self->{childs}}
174}
175
176sub update {
177 my ($self) = @_;
178 ::refresh ();
179}
180
181sub _draw {
182 my ($self) = @_;
183
184 $_->draw for @{$self->{childs}};
185}
186
187package Crossfire::Client::Widget::Window;
188
189our @ISA = Crossfire::Client::Widget::Container::;
190
191use SDL::OpenGL;
192
193sub add {
194 my ($self, $chld) = @_;
195 $self->SUPER::add ($chld); 250 $self->SUPER::add ($chld);
196 $chld->set_parent ($self); 251 $chld->set_parent ($self);
197 $self->update; #TODO: Move this to the size_request event propably?
198} 252}
199 253
200sub remove { 254sub remove {
201 my ($self) = @_; 255 my ($self) = @_;
202 # TODO FIXME: removing a child from a window will crash, see render_chld 256 # TODO FIXME: removing a child from a window will crash, see render_chld
203 $self->update; 257 # $self->update;
204} 258}
205 259
206sub update { 260sub update {
207 my ($self) = @_; 261 my ($self) = @_;
208 $self->render_chld; 262 $self->render_chld;
211sub render_chld { 265sub render_chld {
212 my ($self) = @_; 266 my ($self) = @_;
213 my $chld = $self->get; 267 my $chld = $self->get;
214 my ($w, $h) = $self->size_request; 268 my ($w, $h) = $self->size_request;
215 269
270 require Carp;
271 Carp::cluck "RENDERCHI $w $h";
272 warn "RENDERCHI $w $h\n";
216 $self->{texture} = 273 $self->{texture} =
217 Crossfire::Client::Texture->new_from_opengl ( 274 Crossfire::Client::Texture->new_from_opengl (
218 $w, $h, sub { $chld->draw } 275 $w, $h, sub { $chld->draw }
219 ); 276 );
220 $self->{texture}->upload; 277 $self->{texture}->upload;
221} 278}
222 279
223sub size_request { 280sub size_request {
224 my ($self) = @_; 281 my ($self) = @_;
225 my $chld = $self->get 282 ($self->w, $self->h)
226 or return (0, 0); 283}
227 $chld->size_request 284
285sub size_allocate {
286 my ($self, $w, $h) = @_;
287
288 $self->w ($w);
289 $self->h ($h);
290 $self->get->size_allocate ($w, $h);
291
292 $self->update; #TODO: Move this to the size_request event propably?
228} 293}
229 294
230sub _draw { 295sub _draw {
231 my ($self) = @_; 296 my ($self) = @_;
232 297
233 my ($w, $h) = $self->size_request;#TODO# use width/height of texture 298 my ($w, $h) = ($self->w, $self->h);
234 299
235 my $tex = $self->{texture} 300 my $tex = $self->{texture}
236 or return; 301 or return;
237 302
238 glEnable GL_BLEND; 303 glEnable GL_BLEND;
239 glEnable GL_TEXTURE_2D; 304 glEnable GL_TEXTURE_2D;
240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
241 glBindTexture GL_TEXTURE_2D, $tex->{name}; 306 glBindTexture GL_TEXTURE_2D, $tex->{name};
242
243 glColor 1, 0, 1;
244 307
245 glBegin GL_QUADS; 308 glBegin GL_QUADS;
246 glTexCoord 0, 0; glVertex 0, 0; 309 glTexCoord 0, 0; glVertex 0, 0;
247 glTexCoord 0, 1; glVertex 0, $h; 310 glTexCoord 0, 1; glVertex 0, $h;
248 glTexCoord 1, 1; glVertex $w, $h; 311 glTexCoord 1, 1; glVertex $w, $h;
251 314
252 glDisable GL_BLEND; 315 glDisable GL_BLEND;
253 glDisable GL_TEXTURE_2D; 316 glDisable GL_TEXTURE_2D;
254} 317}
255 318
319#############################################################################
320
256package Crossfire::Client::Widget::Frame; 321package Crossfire::Client::Widget::Frame;
257 322
258our @ISA = Crossfire::Client::Widget::Container::; 323our @ISA = Crossfire::Client::Widget::Bin::;
259 324
260use SDL::OpenGL; 325use SDL::OpenGL;
261 326
262sub size_request { 327sub size_request {
263 my ($self) = @_; 328 my ($self) = @_;
264 my $chld = $self->get 329 my $chld = $self->child
265 or return (0, 0); 330 or return (0, 0);
266 331
267 $chld->move (2, 2); 332 $chld->move (2, 2);
268 333
269 map { $_ + 4 } $chld->size_request; 334 map { $_ + 4 } $chld->size_request;
270} 335}
271 336
337sub size_allocate {
338 my ($self, $w, $h) = @_;
339
340 $self->w ($w);
341 $self->h ($h);
342
343 $self->child->size_allocate ($w - 4, $h - 4);
344 $self->child->move (2, 2);
345}
346
272sub _draw { 347sub _draw {
273 my ($self) = @_; 348 my ($self) = @_;
274 349
275 my $chld = $self->get; 350 my $chld = $self->child;
276 351
277 my ($w, $h) = $chld->size_request; 352 my ($w, $h) = $chld->size_request;
278 353
279 glBegin GL_QUADS; 354 glBegin GL_QUADS;
280 glColor 0, 0, 0; 355 glColor 0, 0, 0;
285 glEnd; 360 glEnd;
286 361
287 $chld->draw; 362 $chld->draw;
288} 363}
289 364
365#############################################################################
366
290package Crossfire::Client::Widget::FancyFrame; 367package Crossfire::Client::Widget::FancyFrame;
291 368
292our @ISA = Crossfire::Client::Widget::Frame::; 369our @ISA = Crossfire::Client::Widget::Frame::;
293 370
294use SDL::OpenGL; 371use SDL::OpenGL;
297 my ($self, $theme) = @_; 374 my ($self, $theme) = @_;
298 $self = $self->SUPER::new; 375 $self = $self->SUPER::new;
299 376
300 $self->{txts} = [ 377 $self->{txts} = [
301 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 378 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
302 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 379 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png)
303 ]; 380 ];
304 $self 381 $self
305
306} 382}
307 383
308sub size_request { 384sub size_request {
309 my ($self) = @_; 385 my ($self) = @_;
386
310 my ($w, $h) = $self->get->size_request; 387 my ($w, $h) = $self->SUPER::size_request;
311 388
312 $h += $self->{txts}->[1]->{height}; 389 $h += $self->{txts}->[1]->{height};
313 $h += $self->{txts}->[4]->{height}; 390 $h += $self->{txts}->[4]->{height};
314 $w += $self->{txts}->[2]->{width}; 391 $w += $self->{txts}->[2]->{width};
315 $w += $self->{txts}->[3]->{width}; 392 $w += $self->{txts}->[3]->{width};
316 393
317 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
318
319 ($w, $h) 394 ($w, $h)
320} 395}
321 396
397sub size_allocate {
398 my ($self, $w, $h) = @_;
399
400 $self->SUPER::size_allocate ($w, $h);
401
402 $h -= $self->{txts}->[1]->{height};
403 $h -= $self->{txts}->[4]->{height};
404 $w -= $self->{txts}->[2]->{width};
405 $w -= $self->{txts}->[3]->{width};
406
407 $h = $h < 0 ? 0 : $h;
408 $w = $w < 0 ? 0 : $w;
409 warn "CHILD:$w $h\n";
410 $self->child->size_allocate ($w, $h);
411 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
412}
413
322sub _draw { 414sub _draw {
323 my ($self) = @_; 415 my ($self) = @_;
324 416
325 my ($w, $h) = $self->size_request; 417 my ($w, $h) = ($self->w, $self->h);
326 my ($cw, $ch) = $self->get->size_request; 418 my ($cw, $ch) = ($self->child->w, $self->child->h);
327 419
328 glEnable GL_BLEND; 420 glEnable GL_BLEND;
329 glEnable GL_TEXTURE_2D; 421 glEnable GL_TEXTURE_2D;
330 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 422 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
331 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 423 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
332
333 424
334 my $top = $self->{txts}->[1]; 425 my $top = $self->{txts}->[1];
335 glBindTexture GL_TEXTURE_2D, $top->{name}; 426 glBindTexture GL_TEXTURE_2D, $top->{name};
336 427
337 glColor 1, 1, 1, 0.8;
338
339 glBegin GL_QUADS; 428 glBegin GL_QUADS;
340 glTexCoord 0, 0; glVertex 0 , 0; 429 glTexCoord 0, 0; glVertex 0 , 0;
341 glTexCoord 0, 1; glVertex 0 , $top->{height}; 430 glTexCoord 0, 1; glVertex 0 , $top->{height};
342 glTexCoord 1, 1; glVertex $w , $top->{height}; 431 glTexCoord 1, 1; glVertex $w , $top->{height};
343 glTexCoord 1, 0; glVertex $w , 0; 432 glTexCoord 1, 0; glVertex $w , 0;
344 glEnd; 433 glEnd;
345 434
346 my $left = $self->{txts}->[3]; 435 my $left = $self->{txts}->[3];
347 glBindTexture GL_TEXTURE_2D, $left->{name}; 436 glBindTexture GL_TEXTURE_2D, $left->{name};
348 437
349 glColor 1, 1, 1, 0.8;
350
351 glBegin GL_QUADS; 438 glBegin GL_QUADS;
352 glTexCoord 0, 0; glVertex 0 , $top->{height}; 439 glTexCoord 0, 0; glVertex 0 , $top->{height};
353 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 440 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
354 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 441 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
355 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 442 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
356 glEnd; 443 glEnd;
357 444
358 my $right = $self->{txts}->[2]; 445 my $right = $self->{txts}->[2];
359 glBindTexture GL_TEXTURE_2D, $right->{name}; 446 glBindTexture GL_TEXTURE_2D, $right->{name};
360 447
361 glColor 1, 1, 1, 0.8;
362
363 glBegin GL_QUADS; 448 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 449 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
365 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 450 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
366 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 451 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
367 glTexCoord 1, 0; glVertex $w , $top->{height}; 452 glTexCoord 1, 0; glVertex $w , $top->{height};
368 glEnd; 453 glEnd;
369 454
370 my $bottom = $self->{txts}->[4]; 455 my $bottom = $self->{txts}->[4];
371 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 456 glBindTexture GL_TEXTURE_2D, $bottom->{name};
372 457
373 glColor 1, 1, 1, 0.8;
374
375 glBegin GL_QUADS; 458 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 459 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
377 glTexCoord 0, 1; glVertex 0 , $h; 460 glTexCoord 0, 1; glVertex 0 , $h;
378 glTexCoord 1, 1; glVertex $w , $h; 461 glTexCoord 1, 1; glVertex $w , $h;
379 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 462 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
380 glEnd; 463 glEnd;
381 464
382 my $bg = $self->{txts}->[0]; 465 my $bg = $self->{txts}->[0];
383 glBindTexture GL_TEXTURE_2D, $bg->{name}; 466 glBindTexture GL_TEXTURE_2D, $bg->{name};
467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
384 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
385 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
386 470
387 glColor 1, 1, 1, 0.8;
388
389 my $rep_x = $cw / $bg->{width}; 471 my $rep_x = $cw / $bg->{width};
390 my $rep_y = $ch / $bg->{height}; 472 my $rep_y = $ch / $bg->{height};
391 473
392 glBegin GL_QUADS; 474 glBegin GL_QUADS;
393 glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; 475 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
394 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; 476 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
395 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; 477 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
396 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; 478 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
397 glEnd; 479 glEnd;
398
399 $self->get->draw;
400 480
401 glDisable GL_BLEND; 481 glDisable GL_BLEND;
402 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
483
484 $self->child->draw;
485
403} 486}
487
488#############################################################################
404 489
405package Crossfire::Client::Widget::Table; 490package Crossfire::Client::Widget::Table;
406 491
407our @ISA = Crossfire::Client::Widget::Container::; 492our @ISA = Crossfire::Client::Widget::Bin::;
408 493
409use SDL::OpenGL; 494use SDL::OpenGL;
410 495
411sub add { 496sub add {
412 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
413 my $old_chld = $self->{childs}[$y][$x]; 498 my $old_chld = $self->{children}[$y][$x];
414 499
415 $self->{childs}[$y][$x] = $chld; 500 $self->{children}[$y][$x] = $chld;
416 $chld->set_parent ($self); 501 $chld->set_parent ($self);
417 $self->update; 502 $self->update;
418} 503}
419 504
420sub max_row_height { 505sub max_row_height {
421 my ($self, $row) = @_; 506 my ($self, $row) = @_;
422 507
423 my $hs = 0; 508 my $hs = 0;
424 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 509 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
425 my $c = $self->{childs}->[$row]->[$xi]; 510 my $c = $self->{children}->[$row]->[$xi];
426 if ($c) { 511 if ($c) {
427 my ($w, $h) = $c->size_request; 512 my ($w, $h) = $c->size_request;
428 if ($hs < $h) { $hs = $h } 513 if ($hs < $h) { $hs = $h }
429 } 514 }
430 } 515 }
433 518
434sub max_col_width { 519sub max_col_width {
435 my ($self, $col) = @_; 520 my ($self, $col) = @_;
436 521
437 my $ws = 0; 522 my $ws = 0;
438 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 523 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
439 my $c = ($self->{childs}->[$yi] || [])->[$col]; 524 my $c = ($self->{children}->[$yi] || [])->[$col];
440 if ($c) { 525 if ($c) {
441 my ($w, $h) = $c->size_request; 526 my ($w, $h) = $c->size_request;
442 if ($ws < $w) { $ws = $w } 527 if ($ws < $w) { $ws = $w }
443 } 528 }
444 } 529 }
448sub size_request { 533sub size_request {
449 my ($self) = @_; 534 my ($self) = @_;
450 535
451 my ($hs, $ws) = (0, 0); 536 my ($hs, $ws) = (0, 0);
452 537
453 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 538 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
454 $hs += $self->max_row_height ($yi); 539 $hs += $self->max_row_height ($yi);
455 } 540 }
456 541
457 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 542 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
458 my $wm = 0; 543 my $wm = 0;
459 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 544 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
460 $wm += $self->max_col_width ($xi) 545 $wm += $self->max_col_width ($xi)
461 } 546 }
462 if ($ws < $wm) { $ws = $wm } 547 if ($ws < $wm) { $ws = $wm }
463 } 548 }
464 549
467 552
468sub _draw { 553sub _draw {
469 my ($self) = @_; 554 my ($self) = @_;
470 555
471 my $y = 0; 556 my $y = 0;
472 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 557 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
473 my $x = 0; 558 my $x = 0;
474 559
475 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 560 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
476 561
477 my $c = $self->{childs}->[$yi]->[$xi]; 562 my $c = $self->{children}->[$yi]->[$xi];
478 if ($c) { 563 if ($c) {
479 $c->move ($x, $y, 0); #TODO: Move to size_request 564 $c->move ($x, $y, 0); #TODO: Move to size_request
480 $c->draw if $c; 565 $c->draw if $c;
481 } 566 }
482 567
485 570
486 $y += $self->max_row_height ($yi); 571 $y += $self->max_row_height ($yi);
487 } 572 }
488} 573}
489 574
575#############################################################################
576
490package Crossfire::Client::Widget::VBox; 577package Crossfire::Client::Widget::VBox;
491 578
492our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = Crossfire::Client::Widget::Container::;
493 580
494use SDL::OpenGL; 581use SDL::OpenGL;
495 582
496sub add { 583sub size_allocate {
497 my ($self, $chld) = @_; 584 my ($self, $w, $h) = @_;
498 push @{$self->{childs}}, $chld;
499 $chld->set_parent ($self);
500 $self->update;
501}
502 585
503sub size_request { 586 $self->w ($w);
504 my ($self) = @_; 587 $self->h ($h);
505 588
506 my ($hs, $ws) = (0, 0); 589 my $exp;
590 my @oth;
591 # find expand widget
507 for (@{$self->{childs} || []}) { 592 for (@{$self->{children}}) {
593 if ($_->{expand}) {
594 $exp = $_;
595 last;
596 }
597 push @oth, $_;
598 }
599
600 my ($ow, $oh);
601
602 # get sizes of other widgets
603 for (@oth) {
508 my ($w, $h) = $_->size_request; 604 my ($w, $h) = $_->size_request;
509 $hs += $h; 605 $oh += $h;
510 if ($ws < $w) { $ws = $w } 606 if ($ow < $w) { $ow = $w }
511 } 607 }
512 608
513 return ($ws, $hs); 609 my $y = 0;
610 for (@{$self->{children}}) {
611 $_->move (0, $y);
612
613 if ($_ == $exp) {
614 $_->size_allocate ($w, $h - $oh);
615 $y += $h - $oh;
616 } else {
617 my ($cw, $h) = $_->size_request;
618 $_->size_allocate ($w, $h);
619 $y += $h;
620 }
621 }
514} 622}
515 623
516sub _draw { 624sub _draw {
517 my ($self) = @_; 625 my ($self) = @_;
518 626
519 my ($x, $y); 627 my ($x, $y);
520 for (@{$self->{childs} || []}) { 628 for (@{$self->{children} || []}) {
521 $_->move (0, $y, 0); #TODO: move to size_request
522 $_->draw; 629 $_->draw;
523 my ($w, $h) = $_->size_request;
524 $y += $h; 630 $y += $_->h;
525 } 631 }
526} 632}
633
634#############################################################################
527 635
528package Crossfire::Client::Widget::Label; 636package Crossfire::Client::Widget::Label;
529 637
530our @ISA = Crossfire::Client::Widget::; 638our @ISA = Crossfire::Client::Widget::;
531 639
575 glEnable GL_TEXTURE_2D; 683 glEnable GL_TEXTURE_2D;
576 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 684 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
577 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 685 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
578 glBindTexture GL_TEXTURE_2D, $tex->{name}; 686 glBindTexture GL_TEXTURE_2D, $tex->{name};
579 687
580 glColor 1, 1, 1, 0.6; # TODO color 688 glColor 1, 0, 0, 1; # TODO color
581 689
582 glBegin GL_QUADS; 690 glBegin GL_QUADS;
583 glTexCoord 0, 0; glVertex 0 , 0; 691 glTexCoord 0, 0; glVertex 0 , 0;
584 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 692 glTexCoord 0, 1; glVertex 0 , $tex->{height};
585 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 693 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
588 696
589 glDisable GL_BLEND; 697 glDisable GL_BLEND;
590 glDisable GL_TEXTURE_2D; 698 glDisable GL_TEXTURE_2D;
591} 699}
592 700
701#############################################################################
702
593package Crossfire::Client::Widget::TextEntry; 703package Crossfire::Client::Widget::TextEntry;
594 704
595our @ISA = Crossfire::Client::Widget::Label::; 705our @ISA = Crossfire::Client::Widget::Label::;
596 706
597use SDL; 707use SDL;
615 $text .= chr $uni; 725 $text .= chr $uni;
616 } 726 }
617 $self->set_text ($text); 727 $self->set_text ($text);
618} 728}
619 729
620 730#############################################################################
621# XXX: TextView isn't neccessary with pango multiline text rendering
622package Crossfire::Client::Widget::TextView;
623
624use strict;
625
626our @ISA = qw/Crossfire::Client::Widget/;
627
628use SDL::OpenGL;
629use SDL::OpenGL::Constants;
630
631sub new {
632 my ($class, $text, $h) = @_;
633 my $self = $class->SUPER::new ();
634
635 $self->{txt_height} = $h;
636 @{$self->{lines}} = split /\r?\n/, $text;
637
638 for (split /\r?\n/, $text) {
639 $self->add_line ($_);
640 }
641 $self
642}
643
644#sub render_lines {
645# my ($self) = @_;
646#
647# $self->{txt_lines} = [];
648#
649# for (@{$self->{lines}}) {
650# push @{$self->{txt_lines}},
651# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
652# }
653#}
654
655sub add_line {
656 my ($self, $line) = @_;
657 push @{$self->{lines}}, $line;
658
659 push @{$self->{txt_lines}},
660 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
661}
662
663sub size_request {
664 my ($self) = @_;
665
666 my $w = 0;
667 my $h = 0;
668
669 for (@{$self->{txt_lines}}) {
670 if ($w < $_->{width}) { $w = $_->{width} }
671 $h += $_->{height};
672 }
673
674 return ($w, $h);
675}
676
677sub draw_line {
678 my ($self, $tex, $y) = @_;
679
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681
682 glColor 1, 0, 1;
683
684 glBegin GL_QUADS;
685 glTexCoord 0, 0; glVertex 0 , $y;
686 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
687 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
688 glTexCoord 1, 0; glVertex $tex->{width}, $y;
689 glEnd;
690}
691
692sub _draw {
693 my ($self) = @_;
694
695 glEnable GL_BLEND;
696 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
698
699 my $l = 0;
700 for (@{$self->{txt_lines}}) {
701 $self->draw_line ($_, $l);
702 $l += $_->{height};
703 }
704
705 glDisable GL_BLEND;
706 glDisable GL_TEXTURE_2D;
707}
708 731
709package Crossfire::Client::Widget::MapWidget; 732package Crossfire::Client::Widget::MapWidget;
710 733
711use strict; 734use strict;
712 735
721sub key_down { 744sub key_down {
722 print "MAPKEYDOWN\n"; 745 print "MAPKEYDOWN\n";
723} 746}
724 747
725sub key_up { 748sub key_up {
749}
750
751sub size_request {
752
753}
754
755sub size_allocate {
726} 756}
727 757
728sub _draw { 758sub _draw {
729 my ($self) = @_; 759 my ($self) = @_;
730 760
750 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 780 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
751 } 781 }
752 782
753 glEnable GL_TEXTURE_2D; 783 glEnable GL_TEXTURE_2D;
754 glEnable GL_BLEND; 784 glEnable GL_BLEND;
755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 785 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
786
787 my $sw4 = ($sw + 3) & ~3;
788 my $lighting = "\x00" x ($sw4 * $sh);
756 789
757 for my $x (0 .. $sw - 1) { 790 for my $x (0 .. $sw - 1) {
758 for my $y (0 .. $sh - 1) { 791 for my $y (0 .. $sh - 1) {
759 792
760 my $cell = $map->[$x + $xofs][$y + $yofs] 793 my $cell = $map->[$x + $xofs][$y + $yofs]
761 or next; 794 or next;
762 795
763 my $darkness = $cell->[0] * (1 / 255); 796 my $darkness = $cell->[0] * (1 / 255);
764 if ($darkness < 0) { 797 if ($darkness < 0) {
765 glColor 0.3, 0.3, 0.3; 798 $darkness = 0.15;
766 } else {
767 glColor $darkness, $darkness, $darkness;
768 } 799 }
800 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
769 801
770 for my $num (grep $_, @$cell[1,2,3]) { 802 for my $num (grep $_, @$cell[1,2,3]) {
771 my $tex = $::CONN->{face}[$num]{texture} || next; 803 my $tex = $::CONN->{face}[$num]{texture} || next;
772 804
773 glBindTexture GL_TEXTURE_2D, $tex->{name}; 805 glBindTexture GL_TEXTURE_2D, $tex->{name};
785 glTexCoord 1, 0; glVertex $px + $w, $py; 817 glTexCoord 1, 0; glVertex $px + $w, $py;
786 glEnd; 818 glEnd;
787 } 819 }
788 } 820 }
789 } 821 }
822
823# if (1) { # higher quality darkness
824# $lighting =~ s/(.)/$1$1$1/gs;
825# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
826#
827# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
828#
829# $lighting = $pb->get_pixels;
830# $lighting =~ s/(.)../$1/gs;
831# }
832
833 $lighting = new Crossfire::Client::Texture
834 width => $sw4,
835 height => $sh,
836 data => $lighting,
837 internalformat => GL_ALPHA4,
838 format => GL_ALPHA;
839
840 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
841 glColor 0, 0, 0, 0.75;
842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
843 glBindTexture GL_TEXTURE_2D, $lighting->{name};
844 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
845 glBegin GL_QUADS;
846 glTexCoord 0, 0; glVertex 0 , 0;
847 glTexCoord 0, 1; glVertex 0 , $sh * 32;
848 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
849 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
850 glEnd;
790 851
791 glDisable GL_TEXTURE_2D; 852 glDisable GL_TEXTURE_2D;
792 glDisable GL_BLEND; 853 glDisable GL_BLEND;
793} 854}
794 855
841 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 902 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
842 $::CONN->send ("command run_stop"); 903 $::CONN->send ("command run_stop");
843 } 904 }
844} 905}
845 906
907#############################################################################
908
909package Crossfire::Client::Widget::Animator;
910
911use SDL::OpenGL;
912
913our @ISA = Crossfire::Client::Widget::Bin::;
914
915sub moveto {
916 my ($self, $x, $y) = @_;
917
918 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
919 $self->{speed} = 0.2;
920 $self->{time} = 1;
921
922 ::animation_start $self;
923}
924
925sub animate {
926 my ($self, $interval) = @_;
927
928 $self->{time} -= $interval * $self->{speed};
929 if ($self->{time} <= 0) {
930 $self->{time} = 0;
931 ::animation_stop $self;
932 }
933
934 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
935
936 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
937 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
938}
939
940sub _draw {
941 my ($self) = @_;
942
943 glPushMatrix;
944 glRotate $self->{time} * 10000, 0, 1, 0;
945 $self->{children}[0]->draw;
946 glPopMatrix;
947}
948
8461; 9491;
847 950

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines