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.39 by root, Sun Apr 9 21:39:08 2006 UTC vs.
Revision 1.65 by root, Tue Apr 11 14:04:27 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
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 10use CFClient;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
15sub feed_sdl_button_down_event { } 26sub 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, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
16sub feed_sdl_button_up_event { } 42sub 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 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
17 74
18sub new { 75sub new {
19 my $class = shift; 76 my $class = shift;
20 77
21 bless { @_ }, $class 78 bless {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
22} 84}
23 85
24sub move { 86sub move {
25 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
26 $self->{x} = $x; 88 $self->{x} = $x;
37 Carp::confess "size_request is abtract"; 99 Carp::confess "size_request is abtract";
38} 100}
39 101
40sub size_allocate { 102sub size_allocate {
41 my ($self, $w, $h) = @_; 103 my ($self, $w, $h) = @_;
104
42 $self->w ($w); 105 $self->{w} = $w;
43 $self->h ($h); 106 $self->{h} = $h;
107}
108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
44} 114}
45 115
46sub focus_in { 116sub focus_in {
47 my ($widget) = @_; 117 my ($self) = @_;
48 $FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
49} 122}
50 123
51sub focus_out { 124sub focus_out {
52 my ($widget) = @_; 125 my ($self) = @_;
53}
54 126
55sub key_down { 127 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 128
59sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
61} 131}
62 132
133sub mouse_motion { }
134sub button_up { }
63sub button_down { 135sub button_down { }
64 my ($widget, $sdlev) = @_; 136sub key_down { }
65} 137sub key_up { }
66 138
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 144
77sub draw { 145sub draw {
78 my ($self) = @_; 146 my ($self) = @_;
79 147
80 glPushMatrix; 148 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 150 $self->_draw;
151 if ($self == $HOVER) {
152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
154 glBegin GL_QUADS;
155 glVertex 0 , 0;
156 glVertex $self->{w}, 0;
157 glVertex $self->{w}, $self->{h};
158 glVertex 0 , $self->{h};
159 glEnd;
160 glDisable GL_BLEND;
161 }
83 glPopMatrix; 162 glPopMatrix;
84} 163}
85 164
86sub _draw { 165sub _draw {
87 my ($self) = @_; 166 my ($self) = @_;
136 #$self->deactivate; 215 #$self->deactivate;
137} 216}
138 217
139############################################################################# 218#############################################################################
140 219
141package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
142 221
143our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
144 223
145sub new { 224sub new {
146 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
147 226
227 my $children = delete $arg{children} || [];
228
148 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
149 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
150 231
151 $self 232 $self
152} 233}
153 234
154sub add { 235sub add {
159 240
160 @{$self->{children}} = 241 @{$self->{children}} =
161 sort { $a->{z} <=> $b->{z} } 242 sort { $a->{z} <=> $b->{z} }
162 @{$self->{children}}, $chld; 243 @{$self->{children}}, $chld;
163 244
164 $self->size_allocate ($self->{w}, $self->{h}); 245 $self->size_allocate ($self->{w}, $self->{h})
246 if $self->{w}; #TODO: check for "realised state"
165} 247}
166 248
167sub remove { 249sub remove {
168 my ($self, $widget) = @_; 250 my ($self, $widget) = @_;
169 251
173} 255}
174 256
175sub find_widget { 257sub find_widget {
176 my ($self, $x, $y) = @_; 258 my ($self, $x, $y) = @_;
177 259
260 $x -= $self->{x};
261 $y -= $self->{y};
262
178 my $res; 263 my $res;
179 264
180 for (@{ $self->{children} }) { 265 for (reverse @{ $self->{children} }) {
181 $res = $_->find_widget ($x, $y) 266 $res = $_->find_widget ($x, $y)
182 and return $res; 267 and return $res;
183 } 268 }
184 269
185 () 270 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
186}
187
188sub size_request {
189 my ($self) = @_;
190
191 my ($hs, $ws) = (0, 0);
192 for (@{$self->{children} || []}) {
193 my ($w, $h) = $_->size_request;
194 $hs += $h;
195 if ($ws < $w) { $ws = $w }
196 }
197
198 return ($ws, $hs);
199} 271}
200 272
201sub _draw { 273sub _draw {
202 my ($self) = @_; 274 my ($self) = @_;
203 275
204 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
205} 277}
206 278
207############################################################################# 279#############################################################################
208 280
209package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
210 282
211our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
212 284
213sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
214 286
215sub size_request { 287sub size_request {
216 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
217} 289}
218 290
219sub size_allocate { 291sub size_allocate {
220 my ($self, $w, $h) = @_; 292 my ($self, $w, $h) = @_;
293
221 $self->SUPER::size_allocate ($w, $h); 294 $self->SUPER::size_allocate ($w, $h);
222 $self->{children}[0]->size_allocate ($w, $h) 295 $self->{children}[0]->size_allocate ($w, $h)
223 if $self->{children}[0] 296 if $self->{children}[0]
224} 297}
225 298
226############################################################################# 299#############################################################################
227 300
228package Crossfire::Client::Widget::Toplevel; 301package CFClient::Widget::Window;
229 302
230our @ISA = Crossfire::Client::Widget::Container::; 303our @ISA = CFClient::Widget::Bin::;
304
305use SDL::OpenGL;
306
307sub new {
308 my ($class, %arg) = @_;
309
310 my $self = $class->SUPER::new (%arg);
311}
231 312
232sub update { 313sub update {
233 my ($self) = @_; 314 my ($self) = @_;
234 315
235 ::refresh (); 316 # we want to do this delayed...
236}
237
238#############################################################################
239
240package Crossfire::Client::Widget::Window;
241
242our @ISA = Crossfire::Client::Widget::Bin::;
243
244use SDL::OpenGL;
245
246sub add {
247 my ($self, $chld) = @_;
248 warn "ADD $chld\n";
249 $self->SUPER::add ($chld);
250 $chld->set_parent ($self);
251}
252
253sub remove {
254 my ($self) = @_;
255 # TODO FIXME: removing a child from a window will crash, see render_chld
256 # $self->update;
257}
258
259sub update {
260 my ($self) = @_;
261 $self->render_chld; 317 $self->render_chld;
318 $self->SUPER::update;
262} 319}
263 320
264sub render_chld { 321sub render_chld {
265 my ($self) = @_; 322 my ($self) = @_;
266 my $chld = $self->get;
267 my ($w, $h) = $self->size_request;
268 323
269 require Carp;
270 Carp::cluck "RENDERCHI $w $h";
271 warn "RENDERCHI $w $h\n";
272 $self->{texture} = 324 $self->{texture} =
273 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
274 $w, $h, sub { $chld->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
275 ); 327 );
276 $self->{texture}->upload;
277}
278
279sub size_request {
280 my ($self) = @_;
281 ($self->w, $self->h)
282} 328}
283 329
284sub size_allocate { 330sub size_allocate {
285 my ($self, $w, $h) = @_; 331 my ($self, $w, $h) = @_;
286 332
287 $self->w ($w); 333 $self->{w} = $w;
288 $self->h ($h); 334 $self->{h} = $h;
335
289 $self->get->size_allocate ($w, $h); 336 $self->child->size_allocate ($w, $h);
290 337
291 $self->update; #TODO: Move this to the size_request event propably? 338 $self->render_chld;
292} 339}
293 340
294sub _draw { 341sub _draw {
295 my ($self) = @_; 342 my ($self) = @_;
296 343
300 or return; 347 or return;
301 348
302 glEnable GL_BLEND; 349 glEnable GL_BLEND;
303 glEnable GL_TEXTURE_2D; 350 glEnable GL_TEXTURE_2D;
304 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 351 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
305 glBindTexture GL_TEXTURE_2D, $tex->{name};
306 352
307 glBegin GL_QUADS; 353 $tex->draw_quad (0, 0, $w, $h);
308 glTexCoord 0, 0; glVertex 0, 0;
309 glTexCoord 0, 1; glVertex 0, $h;
310 glTexCoord 1, 1; glVertex $w, $h;
311 glTexCoord 1, 0; glVertex $w, 0;
312 glEnd;
313 354
314 glDisable GL_BLEND; 355 glDisable GL_BLEND;
315 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
316} 357}
317 358
318############################################################################# 359#############################################################################
319 360
320package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
321 362
322our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
323 364
324use SDL::OpenGL; 365use SDL::OpenGL;
325 366
326sub size_request { 367sub size_request {
327 my ($self) = @_; 368 my ($self) = @_;
333 map { $_ + 4 } $chld->size_request; 374 map { $_ + 4 } $chld->size_request;
334} 375}
335 376
336sub size_allocate { 377sub size_allocate {
337 my ($self, $w, $h) = @_; 378 my ($self, $w, $h) = @_;
338 379
339 $self->w ($w); 380 $self->{w} = $w;
340 $self->h ($h); 381 $self->{h} = $h;
341 382
342 $self->child->size_allocate ($w - 4, $h - 4); 383 $self->child->size_allocate ($w - 4, $h - 4);
343 $self->child->move (2, 2); 384 $self->child->move (2, 2);
344} 385}
345 386
350 391
351 my ($w, $h) = $chld->size_request; 392 my ($w, $h) = $chld->size_request;
352 393
353 glBegin GL_QUADS; 394 glBegin GL_QUADS;
354 glColor 0, 0, 0; 395 glColor 0, 0, 0;
355 glTexCoord 0, 0; glVertex 0 , 0; 396 glVertex 0 , 0;
356 glTexCoord 0, 1; glVertex 0 , $h + 4; 397 glVertex 0 , $h + 4;
357 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 398 glVertex $w + 4 , $h + 4;
358 glTexCoord 1, 0; glVertex $w + 4 , 0; 399 glVertex $w + 4 , 0;
359 glEnd; 400 glEnd;
360 401
361 $chld->draw; 402 $chld->draw;
362} 403}
363 404
364############################################################################# 405#############################################################################
365 406
366package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
367 408
368our @ISA = Crossfire::Client::Widget::Frame::; 409our @ISA = CFClient::Widget::Bin::;
369 410
370use SDL::OpenGL; 411use SDL::OpenGL;
371 412
372sub new { 413my @tex =
373 my ($self, $theme) = @_;
374 $self = $self->SUPER::new;
375
376 $self->{txts} = [
377 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 414 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
378 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 415 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
379 ];
380 $self
381}
382 416
383sub size_request { 417sub size_request {
384 my ($self) = @_; 418 my ($self) = @_;
385 419
386 my ($w, $h) = $self->SUPER::size_request; 420 my ($w, $h) = $self->SUPER::size_request;
387 421
388 $h += $self->{txts}->[1]->{height}; 422 $h += $tex[1]->{height};
389 $h += $self->{txts}->[4]->{height}; 423 $h += $tex[4]->{height};
390 $w += $self->{txts}->[2]->{width}; 424 $w += $tex[2]->{width};
391 $w += $self->{txts}->[3]->{width}; 425 $w += $tex[3]->{width};
392 426
393 ($w, $h) 427 ($w, $h)
394} 428}
395 429
396sub size_allocate { 430sub size_allocate {
397 my ($self, $w, $h) = @_; 431 my ($self, $w, $h) = @_;
398 432
399 $self->w ($w); 433 $self->SUPER::size_allocate ($w, $h);
400 $self->h ($h); 434
401 $h -= $self->{txts}->[1]->{height}; 435 $h -= $tex[1]->{height};
402 $h -= $self->{txts}->[4]->{height}; 436 $h -= $tex[4]->{height};
403 $w -= $self->{txts}->[2]->{width}; 437 $w -= $tex[2]->{width};
404 $w -= $self->{txts}->[3]->{width}; 438 $w -= $tex[3]->{width};
405 439
406 $h = $h < 0 ? 0 : $h; 440 $h = $h < 0 ? 0 : $h;
407 $w = $w < 0 ? 0 : $w; 441 $w = $w < 0 ? 0 : $w;
408 warn "CHILD:$w $h\n"; 442
409 $self->child->size_allocate ($w, $h); 443 $self->child->size_allocate ($w, $h);
410 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 444 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
411} 445}
412 446
413sub _draw { 447sub _draw {
414 my ($self) = @_; 448 my ($self) = @_;
415 449
416 my ($w, $h) = ($self->w, $self->h); 450 my ($w, $h) = ($self->{w}, $self->{h});
417 my ($cw, $ch) = ($self->child->w, $self->child->h); 451 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
418 452
419 glEnable GL_BLEND; 453 glEnable GL_BLEND;
420 glEnable GL_TEXTURE_2D; 454 glEnable GL_TEXTURE_2D;
421 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
423 457
424 my $top = $self->{txts}->[1]; 458 my $top = $tex[1];
425 glBindTexture GL_TEXTURE_2D, $top->{name}; 459 $top->draw_quad (0, 0, $w, $top->{height});
426 460
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex 0 , 0;
429 glTexCoord 0, 1; glVertex 0 , $top->{height};
430 glTexCoord 1, 1; glVertex $w , $top->{height};
431 glTexCoord 1, 0; glVertex $w , 0;
432 glEnd;
433
434 my $left = $self->{txts}->[3]; 461 my $left = $tex[3];
435 glBindTexture GL_TEXTURE_2D, $left->{name}; 462 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
436 463
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $top->{height};
439 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
440 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
441 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
442 glEnd;
443
444 my $right = $self->{txts}->[2]; 464 my $right = $tex[2];
445 glBindTexture GL_TEXTURE_2D, $right->{name}; 465 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
446 466
447 glBegin GL_QUADS;
448 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
449 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
450 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
451 glTexCoord 1, 0; glVertex $w , $top->{height};
452 glEnd;
453
454 my $bottom = $self->{txts}->[4]; 467 my $bottom = $tex[4];
455 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 468 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
456 469
457 glBegin GL_QUADS; 470 my $bg = $tex[0];
458 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
459 glTexCoord 0, 1; glVertex 0 , $h;
460 glTexCoord 1, 1; glVertex $w , $h;
461 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
462 glEnd;
463
464 my $bg = $self->{txts}->[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 471 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 475
470 my $rep_x = $cw / $bg->{width}; 476 my $rep_x = $cw / $bg->{width};
471 my $rep_y = $ch / $bg->{height}; 477 my $rep_y = $ch / $bg->{height};
472 478
473 glBegin GL_QUADS; 479 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
474 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
475 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
476 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
477 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
478 glEnd;
479 480
480 glDisable GL_BLEND; 481 glDisable GL_BLEND;
481 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
482 483
483 $self->child->draw; 484 $self->child->draw;
484 485
485} 486}
486 487
487############################################################################# 488#############################################################################
488 489
489package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
490 491
491our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
492 493
493use SDL::OpenGL; 494use SDL::OpenGL;
494 495
495sub add { 496sub add {
496 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
571 } 572 }
572} 573}
573 574
574############################################################################# 575#############################################################################
575 576
576package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
577 578
578our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
579 580
580use SDL::OpenGL; 581use SDL::OpenGL;
582
583sub size_request {
584 my ($self) = @_;
585
586 my @alloc = map [$_->size_request], @{$self->{children}};
587
588 (
589 (List::Util::max map $_->[0], @alloc),
590 (List::Util::sum map $_->[1], @alloc),
591 )
592}
581 593
582sub size_allocate { 594sub size_allocate {
583 my ($self, $w, $h) = @_; 595 my ($self, $w, $h) = @_;
584 596
585 $self->w ($w); 597 $self->w ($w);
618 $y += $h; 630 $y += $h;
619 } 631 }
620 } 632 }
621} 633}
622 634
623sub _draw {
624 my ($self) = @_;
625
626 my ($x, $y);
627 for (@{$self->{children} || []}) {
628 $_->draw;
629 $y += $_->h;
630 }
631}
632
633############################################################################# 635#############################################################################
634 636
635package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
636 638
637our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
638 640
639use SDL::OpenGL; 641use SDL::OpenGL;
640 642
641sub new { 643sub new {
642 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
643 645
644 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
645 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 647 my $self = $class->SUPER::new (
648 color => [1, 1, 1],
649 height => $::FONTSIZE,
650 text => "",
651 layout => new CFClient::Layout,
652 %arg
653 );
646 654
647 $self->set_text ($text); 655 $self->set_text ($self->{text});
648 656
649 $self 657 $self
650} 658}
651 659
652sub set_text { 660sub set_text {
653 my ($self, $text) = @_; 661 my ($self, $text) = @_;
654 662
655 $self->{text} = $text; 663 $self->{text} = $text;
656 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
657 665
658 $self->update; 666 delete $self->{texture};
659} 667}
660 668
661sub get_text { 669sub get_text {
662 my ($self, $text) = @_; 670 my ($self, $text) = @_;
663 671
665} 673}
666 674
667sub size_request { 675sub size_request {
668 my ($self) = @_; 676 my ($self) = @_;
669 677
670 ( 678 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size
681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
682# (
671 $self->{texture}{width}, 683# $self->{texture}{width},
672 $self->{texture}{height}, 684# $self->{texture}{height},
673 ) 685# )
686# } else {
687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
688#
689# ($w, $h)
690# }
691}
692
693sub size_allocate {
694 my ($self, $w, $h) = @_;
695
696 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture};
674} 698}
675 699
676sub _draw { 700sub _draw {
677 my ($self) = @_; 701 my ($self) = @_;
678 702
679 my $tex = $self->{texture}; 703 my $tex = $self->{texture} ||= do {
704 $self->{layout}->set_width ($self->{w});
705 new_from_layout CFClient::Texture $self->{layout};
706 };
680 707
681 glEnable GL_BLEND; 708 glEnable GL_BLEND;
682 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
683 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
684 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
685 glBindTexture GL_TEXTURE_2D, $tex->{name};
686 712
687 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
688 714
689 glBegin GL_QUADS; 715 $tex->draw_quad (0, 0);
690 glTexCoord 0, 0; glVertex 0 , 0;
691 glTexCoord 0, 1; glVertex 0 , $tex->{height};
692 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
693 glTexCoord 1, 0; glVertex $tex->{width}, 0;
694 glEnd;
695 716
696 glDisable GL_BLEND; 717 glDisable GL_BLEND;
697 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
698} 719}
699 720
700############################################################################# 721#############################################################################
701 722
702package Crossfire::Client::Widget::TextEntry; 723package CFClient::Widget::Entry;
703 724
704our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
705 726
706use SDL; 727use SDL;
707use SDL::OpenGL; 728use SDL::OpenGL;
708 729
709sub key_down { 730sub key_down {
710 my ($self, $ev) = @_; 731 my ($self, $ev) = @_;
711 732
712 my $mod = $ev->key_mod; 733 my $mod = $ev->key_mod;
713 my $sym = $ev->key_sym; 734 my $sym = $ev->key_sym;
714 735
715 $ev->set_unicode (1);
716 my $uni = $ev->key_unicode; 736 my $uni = $ev->key_unicode;
717 737
718 my $text = $self->get_text; 738 my $text = $self->get_text;
719 739
720 if ($sym == SDLK_BACKSPACE) { 740 if ($sym == SDLK_BACKSPACE) {
721 substr $text, -1, 1, ''; 741 substr $text, -1, 1, '';
722
723 } elsif ($uni) { 742 } elsif ($uni) {
724 $text .= chr $uni; 743 $text .= chr $uni;
725 } 744 }
745
726 $self->set_text ($text); 746 $self->set_text ($text);
727} 747}
728 748
729############################################################################# 749sub button_down {
750 my ($self, $ev) = @_;
730 751
752 $self->focus_in;
753}
754
755sub mouse_motion {
756 my ($self, $ev, $x, $y) = @_;
757 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
758}
759
760sub _draw {
761 my ($self) = @_;
762
763 if ($FOCUS == $self) {
764 glColor 1, 1, 1;
765 } else {
766 glColor 0.7, 0.7, 0.7;
767 }
768
769 glBegin GL_QUADS;
770 glVertex 0 , 0;
771 glVertex 0 , $self->{h} - 1;
772 glVertex $self->{w} - 1, $self->{h} - 1;
773 glVertex $self->{w} - 1, 0;
774 glEnd;
775
776 $self->SUPER::_draw;
777}
778
779#############################################################################
780
731package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
732 782
733use strict; 783use strict;
734 784
735use List::Util qw(min max); 785use List::Util qw(min max);
736 786
737use SDL; 787use SDL;
738use SDL::OpenGL; 788use SDL::OpenGL;
739use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
740 790
741our @ISA = Crossfire::Client::Widget::; 791our @ISA = CFClient::Widget::;
792
793sub new {
794 my $class = shift;
795
796 $class->SUPER::new (
797 z => -1,
798 list => (glGenLists 1),
799 @_
800 )
801}
742 802
743sub key_down { 803sub key_down {
744 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
745} 805}
746 806
747sub key_up { 807sub key_up {
748} 808}
749 809
750sub size_request { 810sub size_request {
751 811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
752} 815}
753 816
754sub size_allocate { 817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
755} 821}
756 822
757sub _draw { 823sub _draw {
758 my ($self) = @_; 824 my ($self) = @_;
759 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
760 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
761 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
762 831
763 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
764 833
765 my ($xofs, $yofs); 834 my ($xofs, $yofs);
766 835
767 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
768 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
769 838
770 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
771 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
772 } else { 841 } else {
773 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 842 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
774 } 843 }
775 844
776 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
777 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
778 } else { 847 } else {
779 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 848 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
780 } 849 }
781 850
782 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
783 glEnable GL_BLEND; 852 glEnable GL_BLEND;
784 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
785 854
786 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
787 my $lighting = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
788 857
789 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
859 my $row = $map->[$x + $xofs];
790 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
791 861
792 my $cell = $map->[$x + $xofs][$y + $yofs] 862 my $cell = $row->[$y + $yofs]
793 or next; 863 or next;
794 864
795 my $darkness = $cell->[0] * (1 / 255); 865 my $dark = $cell->[0];
796 if ($darkness < 0) { 866 if ($dark < 0) {
797 $darkness = 0.15; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
868 } else {
869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
798 } 870 }
799 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
800 871
801 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
802 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
803 874
804 glBindTexture GL_TEXTURE_2D, $tex->{name};
805
806 my $w = $tex->{width}; 875 my $w = $tex->{width};
807 my $h = $tex->{height}; 876 my $h = $tex->{height};
808 877
809 my $px = ($x + 1) * 32 - $w; 878 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
810 my $py = ($y + 1) * 32 - $h;
811
812 glBegin GL_QUADS;
813 glTexCoord 0, 0; glVertex $px , $py;
814 glTexCoord 0, 1; glVertex $px , $py + $h;
815 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
816 glTexCoord 1, 0; glVertex $px + $w, $py;
817 glEnd; 879 }
818 } 880 }
819 } 881 }
820 }
821 882
822# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
823# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
824# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 885# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
825# 886#
827# 888#
828# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
829# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
830# } 891# }
831 892
832 $lighting = new Crossfire::Client::Texture
833 width => $sw4,
834 height => $sh,
835 data => $lighting,
836 internalformat => GL_ALPHA4,
837 format => GL_ALPHA;
838
839 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 893 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glColor 0, 0, 0, 0.75;
841 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
842 glBindTexture GL_TEXTURE_2D, $lighting->{name};
843 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
844 glBegin GL_QUADS;
845 glTexCoord 0, 0; glVertex 0 , 0;
846 glTexCoord 0, 1; glVertex 0 , $sh * 32;
847 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
848 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
849 glEnd;
850 895
896 $darkness = new CFClient::Texture
897 width => $sw4,
898 height => $sh,
899 data => $darkness,
900 internalformat => GL_ALPHA,
901 format => GL_ALPHA;
902
903 glColor 0.45, 0.45, 0.45, 1;
904 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
905
851 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
852 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
853} 913}
854 914
855my %DIR = ( 915my %DIR = (
856 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
857 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
903 } 963 }
904} 964}
905 965
906############################################################################# 966#############################################################################
907 967
908package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
909 969
910use SDL::OpenGL; 970use SDL::OpenGL;
911 971
912our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
913 973
914sub moveto { 974sub moveto {
915 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
916 976
917 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
918 $self->{speed} = 0.2; 978 $self->{speed} = 0.001;
919 $self->{time} = 1; 979 $self->{time} = 1;
920 980
921 ::animation_start $self; 981 ::animation_start $self;
922} 982}
923 983
938 998
939sub _draw { 999sub _draw {
940 my ($self) = @_; 1000 my ($self) = @_;
941 1001
942 glPushMatrix; 1002 glPushMatrix;
943 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
944 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
945 glPopMatrix; 1005 glPopMatrix;
946} 1006}
947 1007
9481; 1008#############################################################################
949 1009
1010package CFClient::Widget::Toplevel;
1011
1012our @ISA = CFClient::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub 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
1027sub translate {
1028 my ($self, $x, $y) = @_;
1029
1030 ($x, $y)
1031}
1032
1033sub update {
1034 my ($self) = @_;
1035
1036 $self->size_allocate ($self->size_request);
1037 ::refresh ();
1038}
1039
1040sub add {
1041 my ($self, $widget) = @_;
1042
1043 $self->SUPER::add ($widget);
1044
1045 $widget->size_allocate ($widget->size_request);
1046}
1047
1048sub draw {
1049 my ($self) = @_;
1050
1051 $self->_draw;
1052}
1053
1054#############################################################################
1055
1056package CFClient::Widget;
1057
1058$TOPLEVEL = new CFClient::Widget::Toplevel;
1059
10601
1061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines