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.77 by root, Tue Apr 11 22:14:13 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
7
8use CFClient;
9
10our ($FOCUS, $HOVER, $GRAB); # various widgets
11
12our $TOPLEVEL;
13our $BUTTON_STATE;
14
15# class methods for events
16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS;
18}
19
20sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS;
22}
23
24sub feed_sdl_button_down_event {
25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
27
28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y);
30
31 $GRAB = $widget;
32 $GRAB->update if $GRAB;
33 }
34
35 $BUTTON_STATE |= 1 << ($ev->button - 1);
36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
38}
39
40sub feed_sdl_button_up_event {
41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49
50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub 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, $HOVER->translate ($x, $y)) if $HOVER;
71}
72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
6 78
7use SDL::OpenGL; 79use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9
10our $FOCUS; # the widget with current focus
11
12# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
15sub feed_sdl_button_down_event { }
16sub feed_sdl_button_up_event { }
17 80
18sub new { 81sub new {
19 my $class = shift; 82 my $class = shift;
20 83
21 bless { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 w => -1,
89 h => -1,
90 @_
91 }, $class
22} 92}
23 93
24sub move { 94sub move {
25 my ($self, $x, $y, $z) = @_; 95 my ($self, $x, $y, $z) = @_;
26 $self->{x} = $x; 96 $self->{x} = $x;
35sub size_request { 105sub size_request {
36 require Carp; 106 require Carp;
37 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
38} 108}
39 109
110sub _size_allocate {
111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
117
118 $self->{w} = $w;
119 $self->{h} = $h;
120
121 1
122}
123
40sub size_allocate { 124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
128}
129
130# translate global koordinates to local coordinate system
131sub translate {
41 my ($self, $w, $h) = @_; 132 my ($self, $x, $y) = @_;
42 $self->w ($w); 133
43 $self->h ($h); 134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
44} 135}
45 136
46sub focus_in { 137sub focus_in {
47 my ($widget) = @_; 138 my ($self) = @_;
48 $FOCUS = $widget; 139
140 return if $FOCUS == $self;
141
142 my $focus = $FOCUS; $FOCUS = $self;
143 $focus->update if $focus;
144 $FOCUS->update;
49} 145}
50 146
51sub focus_out { 147sub focus_out {
52 my ($widget) = @_; 148 my ($self) = @_;
53}
54 149
150 return unless $FOCUS == $self;
151
152 my $focus = $FOCUS; undef $FOCUS;
153 $focus->update if $focus; #?
154}
155
156sub mouse_motion { }
157sub button_up { }
55sub key_down { 158sub key_down { }
56 my ($widget, $sdlev) = @_;
57}
58
59sub key_up { 159sub key_up { }
60 my ($widget, $sdlev) = @_;
61}
62 160
63sub button_down { 161sub button_down {
64 my ($widget, $sdlev) = @_; 162 my ($self, $ev, $x, $y) = @_;
65}
66 163
67sub button_up { 164 $self->focus_in;
68 my ($widget, $sdlev) = @_;
69} 165}
70 166
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 172
77sub draw { 173sub draw {
78 my ($self) = @_; 174 my ($self) = @_;
175
176 return unless $self->{h} && $self->{w};
79 177
80 glPushMatrix; 178 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 180 $self->_draw;
83 glPopMatrix; 181 glPopMatrix;
182
183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 0, 0, 1, 0.2;
187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS;
190 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y;
192 glVertex $x + $self->{w}, $y + $self->{h};
193 glVertex $x , $y + $self->{h};
194 glEnd;
195 glDisable GL_BLEND;
196 }
84} 197}
85 198
86sub _draw { 199sub _draw {
87 my ($self) = @_; 200 my ($self) = @_;
88 201
128 241
129 $self->{parent}->update 242 $self->{parent}->update
130 if $self->{parent}; 243 if $self->{parent};
131} 244}
132 245
246sub connect {
247 my ($self, $signal, $cb) = @_;
248
249 push @{ $self->{cb}{$signal} }, $cb;
250}
251
252sub emit {
253 my ($self, $signal, @args) = @_;
254
255 $_->($self, @args)
256 for @{$self->{cb}{$signal} || []};
257}
258
133sub DESTROY { 259sub DESTROY {
134 my ($self) = @_; 260 my ($self) = @_;
135 261
136 #$self->deactivate; 262 #$self->deactivate;
137} 263}
138 264
139############################################################################# 265#############################################################################
140 266
141package Crossfire::Client::Widget::Container; 267package CFClient::UI::DrawBG;
142 268
143our @ISA = Crossfire::Client::Widget::; 269our @ISA = CFClient::UI::Base::;
270
271use strict;
272use SDL::OpenGL;
144 273
145sub new { 274sub new {
275 my $class = shift;
276
277 # range [value, low, high, page]
278
279 $class->SUPER::new (
280 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1],
282 @_
283 )
284}
285
286sub _draw {
287 my ($self) = @_;
288
289 my ($w, $h) = @$self{qw(w h)};
290
291 glEnable GL_BLEND;
292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
293 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
294
295 glBegin GL_QUADS;
296 glVertex 0 , 0;
297 glVertex 0 , $h;
298 glVertex $w, $h;
299 glVertex $w, 0;
300 glEnd;
301
302 glDisable GL_BLEND;
303}
304
305#############################################################################
306
307package CFClient::UI::Empty;
308
309our @ISA = CFClient::UI::Base::;
310
311sub size_request {
312 (0, 0)
313}
314
315sub draw { }
316
317#############################################################################
318
319package CFClient::UI::Container;
320
321our @ISA = CFClient::UI::Base::;
322
323sub new {
146 my ($class, @widgets) = @_; 324 my ($class, %arg) = @_;
147 325
326 my $children = delete $arg{children} || [];
327
148 my $self = $class->SUPER::new (children => []); 328 my $self = $class->SUPER::new (children => [], %arg);
149 $self->add ($_) for @widgets; 329 $self->add ($_) for @$children;
150 330
151 $self 331 $self
152} 332}
153 333
154sub add { 334sub add {
155 my ($self, $chld, $expand) = @_; 335 my ($self, $chld) = @_;
156 336
157 $chld->{expand} = $expand;
158 $chld->set_parent ($self); 337 $chld->set_parent ($self);
159 338
160 @{$self->{children}} = 339 $self->{children} = [
161 sort { $a->{z} <=> $b->{z} } 340 sort { $a->{z} <=> $b->{z} }
162 @{$self->{children}}, $chld; 341 @{$self->{children}}, $chld
342 ];
163 343
164 $self->size_allocate ($self->{w}, $self->{h}); 344 $self->{w} = $self->{h} = -1;
345 $self->update;
165} 346}
166 347
167sub remove { 348sub remove {
168 my ($self, $widget) = @_; 349 my ($self, $widget) = @_;
169 350
170 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 351 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
171 352
172 $self->size_allocate ($self->{w}, $self->{h}); 353 $self->size_allocate (0, 0, $self->{w}, $self->{h});
173} 354}
174 355
175sub find_widget { 356sub find_widget {
176 my ($self, $x, $y) = @_; 357 my ($self, $x, $y) = @_;
177 358
359 $x -= $self->{x};
360 $y -= $self->{y};
361
178 my $res; 362 my $res;
179 363
180 for (@{ $self->{children} }) { 364 for (reverse @{ $self->{children} }) {
181 $res = $_->find_widget ($x, $y) 365 $res = $_->find_widget ($x, $y)
182 and return $res; 366 and return $res;
183 } 367 }
184 368
369 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
370}
371
372sub _draw {
373 my ($self) = @_;
374
375 $_->draw for @{$self->{children}};
376}
377
378#############################################################################
379
380package CFClient::UI::Bin;
381
382our @ISA = CFClient::UI::Container::;
383
384sub new {
385 my ($class, %arg) = @_;
386
387 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
388
389 $class->SUPER::new (children => [$child], %arg)
390}
391
392sub add {
393 my ($self, $widget) = @_;
394
395 $self->{children} = [];
185 () 396
397 $self->SUPER::add ($widget);
186} 398}
399
400sub remove {
401 my ($self, $widget) = @_;
402
403 $self->SUPER::remove ($widget);
404
405 $self->{children} = [new CFClient::UI::Empty]
406 unless @{$self->{children}};
407}
408
409sub child { $_[0]->{children}[0] }
187 410
188sub size_request { 411sub 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}
200
201sub _draw {
202 my ($self) = @_;
203
204 $_->draw for @{$self->{children}};
205}
206
207#############################################################################
208
209package Crossfire::Client::Widget::Bin;
210
211our @ISA = Crossfire::Client::Widget::Container::;
212
213sub child { $_[0]->{children}[0] }
214
215sub size_request {
216 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 412 $_[0]{children}[0]->size_request
217} 413}
218 414
219sub size_allocate { 415sub size_allocate {
220 my ($self, $w, $h) = @_; 416 my ($self, $x, $y, $w, $h) = @_;
221 $self->SUPER::size_allocate ($w, $h); 417
418 $self->_size_allocate ($x, $y, $w, $h) or return;
419
222 $self->{children}[0]->size_allocate ($w, $h) 420 $self->{children}[0]->size_allocate (0, 0, $w, $h);
223 if $self->{children}[0]
224} 421}
225 422
226############################################################################# 423#############################################################################
227 424
228package Crossfire::Client::Widget::Toplevel; 425package CFClient::UI::Window;
229 426
230our @ISA = Crossfire::Client::Widget::Container::; 427our @ISA = CFClient::UI::Bin::;
428
429use SDL::OpenGL;
430
431sub new {
432 my ($class, %arg) = @_;
433
434 my $self = $class->SUPER::new (%arg);
435}
231 436
232sub update { 437sub update {
233 my ($self) = @_; 438 my ($self) = @_;
234 439
235 ::refresh (); 440 # 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; 441 $self->render_chld;
442 $self->SUPER::update;
262} 443}
263 444
264sub render_chld { 445sub render_chld {
265 my ($self) = @_; 446 my ($self) = @_;
266 my $chld = $self->get;
267 my ($w, $h) = $self->size_request;
268 447
269 require Carp;
270 Carp::cluck "RENDERCHI $w $h";
271 warn "RENDERCHI $w $h\n";
272 $self->{texture} = 448 $self->{texture} =
273 Crossfire::Client::Texture->new_from_opengl ( 449 CFClient::Texture->new_from_opengl (
274 $w, $h, sub { $chld->draw } 450 $self->{w}, $self->{h}, sub { $self->child->draw }
275 ); 451 );
276 $self->{texture}->upload;
277}
278
279sub size_request {
280 my ($self) = @_;
281 ($self->w, $self->h)
282} 452}
283 453
284sub size_allocate { 454sub size_allocate {
285 my ($self, $w, $h) = @_; 455 my ($self, $x, $y, $w, $h) = @_;
286 456
287 $self->w ($w); 457 $self->_size_allocate ($x, $y, $w, $h) or return;
288 $self->h ($h); 458
289 $self->get->size_allocate ($w, $h); 459 $self->child->size_allocate (0, 0, $w, $h);
290 460
291 $self->update; #TODO: Move this to the size_request event propably? 461 $self->render_chld;
292} 462}
293 463
294sub _draw { 464sub _draw {
295 my ($self) = @_; 465 my ($self) = @_;
296 466
298 468
299 my $tex = $self->{texture} 469 my $tex = $self->{texture}
300 or return; 470 or return;
301 471
302 glEnable GL_BLEND; 472 glEnable GL_BLEND;
473 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
303 glEnable GL_TEXTURE_2D; 474 glEnable GL_TEXTURE_2D;
304 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 475 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
305 glBindTexture GL_TEXTURE_2D, $tex->{name};
306 476
307 glBegin GL_QUADS; 477 $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 478
314 glDisable GL_BLEND; 479 glDisable GL_BLEND;
315 glDisable GL_TEXTURE_2D; 480 glDisable GL_TEXTURE_2D;
316} 481}
317 482
318############################################################################# 483#############################################################################
319 484
320package Crossfire::Client::Widget::Frame; 485package CFClient::UI::Frame;
321 486
322our @ISA = Crossfire::Client::Widget::Bin::; 487our @ISA = CFClient::UI::Bin::;
323 488
324use SDL::OpenGL; 489use SDL::OpenGL;
325 490
326sub size_request { 491sub size_request {
327 my ($self) = @_; 492 my ($self) = @_;
332 497
333 map { $_ + 4 } $chld->size_request; 498 map { $_ + 4 } $chld->size_request;
334} 499}
335 500
336sub size_allocate { 501sub size_allocate {
337 my ($self, $w, $h) = @_; 502 my ($self, $x, $y, $w, $h) = @_;
503
504 $self->_size_allocate ($x, $y, $w, $h) or return;
338 505
339 $self->w ($w);
340 $self->h ($h);
341
342 $self->child->size_allocate ($w - 4, $h - 4); 506 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
343 $self->child->move (2, 2);
344} 507}
345 508
346sub _draw { 509sub _draw {
347 my ($self) = @_; 510 my ($self) = @_;
348 511
350 513
351 my ($w, $h) = $chld->size_request; 514 my ($w, $h) = $chld->size_request;
352 515
353 glBegin GL_QUADS; 516 glBegin GL_QUADS;
354 glColor 0, 0, 0; 517 glColor 0, 0, 0;
355 glTexCoord 0, 0; glVertex 0 , 0; 518 glVertex 0 , 0;
356 glTexCoord 0, 1; glVertex 0 , $h + 4; 519 glVertex 0 , $h + 4;
357 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 520 glVertex $w + 4 , $h + 4;
358 glTexCoord 1, 0; glVertex $w + 4 , 0; 521 glVertex $w + 4 , 0;
359 glEnd; 522 glEnd;
360 523
361 $chld->draw; 524 $chld->draw;
362} 525}
363 526
364############################################################################# 527#############################################################################
365 528
366package Crossfire::Client::Widget::FancyFrame; 529package CFClient::UI::FancyFrame;
367 530
368our @ISA = Crossfire::Client::Widget::Frame::; 531our @ISA = CFClient::UI::Bin::;
369 532
370use SDL::OpenGL; 533use SDL::OpenGL;
371 534
372sub new { 535my @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 $_ } 536 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/ 537 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 538
383sub size_request { 539sub size_request {
384 my ($self) = @_; 540 my ($self) = @_;
385 541
542 my ($w, $h) =
543 $self->{user_w} && $self->{user_h}
544 ? ($self->{user_w}, $self->{user_h})
386 my ($w, $h) = $self->SUPER::size_request; 545 : ($self->SUPER::size_request);
387 546
388 $h += $self->{txts}->[1]->{height}; 547 $h += $tex[1]->{h};
389 $h += $self->{txts}->[4]->{height}; 548 $h += $tex[4]->{h};
390 $w += $self->{txts}->[2]->{width}; 549 $w += $tex[2]->{w};
391 $w += $self->{txts}->[3]->{width}; 550 $w += $tex[3]->{w};
392 551
393 ($w, $h) 552 ($w, $h)
394} 553}
395 554
396sub size_allocate { 555sub size_allocate {
397 my ($self, $w, $h) = @_; 556 my ($self, $x, $y, $w, $h) = @_;
398 557
399 $self->w ($w); 558 $self->_size_allocate ($x, $y, $w, $h) or return;
400 $self->h ($h); 559
401 $h -= $self->{txts}->[1]->{height}; 560 $h -= $tex[1]{h};
402 $h -= $self->{txts}->[4]->{height}; 561 $h -= $tex[4]{h};
403 $w -= $self->{txts}->[2]->{width}; 562 $w -= $tex[2]{w};
404 $w -= $self->{txts}->[3]->{width}; 563 $w -= $tex[3]{w};
405 564
406 $h = $h < 0 ? 0 : $h; 565 $h = $h < 0 ? 0 : $h;
407 $w = $w < 0 ? 0 : $w; 566 $w = $w < 0 ? 0 : $w;
408 warn "CHILD:$w $h\n"; 567
409 $self->child->size_allocate ($w, $h); 568 my $child = $self->child;
410 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 569
570 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
571}
572
573sub button_down {
574 my ($self, $ev, $x, $y) = @_;
575
576 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
577 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
578
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
580 my ($bw, $bh) = ($self->{w}, $self->{h});
581
582 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_;
584
585 ($x, $y) = ($ev->motion_x, $ev->motion_y);
586
587 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy;
589 $self->update;
590 };
591
592 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $tex[1]{h}) {
594
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
596 my ($bx, $by) = ($self->{x}, $self->{y});
597
598 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_;
600
601 ($x, $y) = ($ev->motion_x, $ev->motion_y);
602
603 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update;
605 };
606 }
607}
608
609sub button_up {
610 my ($self, $ev, $x, $y) = @_;
611
612 delete $self->{motion};
613}
614
615sub mouse_motion {
616 my ($self, $ev, $x, $y) = @_;
617
618 $self->{motion}->($ev, $x, $y) if $self->{motion};
411} 619}
412 620
413sub _draw { 621sub _draw {
414 my ($self) = @_; 622 my ($self) = @_;
415 623
416 my ($w, $h) = ($self->w, $self->h); 624 my ($w, $h) = ($self->{w}, $self->{h});
417 my ($cw, $ch) = ($self->child->w, $self->child->h); 625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
418 626
419 glEnable GL_BLEND; 627 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
420 glEnable GL_TEXTURE_2D; 629 glEnable GL_TEXTURE_2D;
421 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
422 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
423 631
424 my $top = $self->{txts}->[1]; 632 my $top = $tex[1];
425 glBindTexture GL_TEXTURE_2D, $top->{name}; 633 $top->draw_quad (0, 0, $w, $top->{h});
426 634
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]; 635 my $left = $tex[3];
435 glBindTexture GL_TEXTURE_2D, $left->{name}; 636 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
436 637
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]; 638 my $right = $tex[2];
445 glBindTexture GL_TEXTURE_2D, $right->{name}; 639 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
446 640
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]; 641 my $bottom = $tex[4];
455 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 642 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
456 643
457 glBegin GL_QUADS; 644 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 645
464 my $bg = $self->{txts}->[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 646 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 648 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 649 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 650
470 my $rep_x = $cw / $bg->{width}; 651 my $rep_x = $cw / $bg->{w};
471 my $rep_y = $ch / $bg->{height}; 652 my $rep_y = $ch / $bg->{h};
472 653
473 glBegin GL_QUADS; 654 $bg->draw_quad ($left->{w}, $top->{h}, $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 655
656 glDisable GL_TEXTURE_2D;
480 glDisable GL_BLEND; 657 glDisable GL_BLEND;
481 glDisable GL_TEXTURE_2D;
482 658
483 $self->child->draw; 659 $self->child->draw;
484 660
485} 661}
486 662
487############################################################################# 663#############################################################################
488 664
489package Crossfire::Client::Widget::Table; 665package CFClient::UI::Table;
490 666
491our @ISA = Crossfire::Client::Widget::Bin::; 667our @ISA = CFClient::UI::Base::;
668
669use List::Util qw(max sum);
492 670
493use SDL::OpenGL; 671use SDL::OpenGL;
494 672
495sub add { 673sub add {
496 my ($self, $x, $y, $chld) = @_; 674 my ($self, $x, $y, $chld) = @_;
497 my $old_chld = $self->{children}[$y][$x];
498 675
499 $self->{children}[$y][$x] = $chld; 676 $self->{children}[$y][$x] = $chld;
500 $chld->set_parent ($self); 677 $chld->set_parent ($self);
678
679 $self->{w} = $self->{h} = -1;
501 $self->update; 680 $self->update;
502} 681}
503 682
504sub max_row_height { 683sub get_wh {
505 my ($self, $row) = @_; 684 my ($self) = @_;
506 685
507 my $hs = 0; 686 my (@w, @h);
508 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 687
688 for my $y (0 .. $#{$self->{children}}) {
509 my $c = $self->{children}->[$row]->[$xi]; 689 my $row = $self->{children}[$y]
510 if ($c) { 690 or next;
691
692 for my $x (0 .. $#$row) {
693 my $widget = $row->[$x]
694 or next;
511 my ($w, $h) = $c->size_request; 695 my ($w, $h) = $widget->size_request;
512 if ($hs < $h) { $hs = $h } 696
697 $w[$x] = max $w[$x], $w;
698 $h[$y] = max $h[$y], $h;
513 } 699 }
514 } 700 }
515 return $hs;
516}
517 701
518sub max_col_width { 702 (\@w, \@h)
703}
704
705sub size_request {
519 my ($self, $col) = @_; 706 my ($self) = @_;
520 707
708 my ($ws, $hs) = $self->get_wh;
709
710 (
711 (List::Util::sum @$ws),
712 (List::Util::sum @$hs),
713 )
714}
715
716sub size_allocate {
717 my ($self, $x, $y, $w, $h) = @_;
718
719 $self->_size_allocate ($x, $y, $w, $h) or return;
720
721 my ($ws, $hs) = $self->get_wh;
722
723 my $req_w = List::Util::sum @$ws;
724 my $req_h = List::Util::sum @$hs;
725
726 # linearly scale sizes
727 $_ *= $w / $req_w for @$ws;
728 $_ *= $h / $req_h for @$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
521 my $ws = 0; 736 my $x = 0;
522 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 737 my $row_h = $hs->[$r];
523 my $c = ($self->{children}->[$yi] || [])->[$col]; 738
524 if ($c) { 739 for my $c (0 .. $#$row) {
525 my ($w, $h) = $c->size_request; 740 my $widget = $row->[$c]
526 if ($ws < $w) { $ws = $w } 741 or next;
742
743 my $col_w = $ws->[$c];
744
745 $widget->size_allocate ($x, $y, $col_w, $row_h);
746
747 $x += $col_w;
527 } 748 }
749
750 $y += $row_h;
528 } 751 }
529 return $ws; 752
530} 753}
754
755sub find_widget {
756 my ($self, $x, $y) = @_;
757
758 $x -= $self->{x};
759 $y -= $self->{y};
760
761 my $res;
762
763 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
764 $res = $_->find_widget ($x, $y)
765 and return $res;
766 }
767
768 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
769}
770
771sub _draw {
772 my ($self) = @_;
773
774 for (grep $_, @{$self->{children}}) {
775 $_->draw for grep $_, @$_;
776 }
777}
778
779#############################################################################
780
781package CFClient::UI::HBox;
782
783# TODO: wrap into common Box base class
784
785our @ISA = CFClient::UI::Container::;
531 786
532sub size_request { 787sub size_request {
533 my ($self) = @_; 788 my ($self) = @_;
534 789
790 my @alloc = map [$_->size_request], @{$self->{children}};
791
792 (
793 (List::Util::sum map $_->[0], @alloc),
794 (List::Util::max map $_->[1], @alloc),
795 )
796}
797
798sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_;
800
801 $self->_size_allocate ($x, $y, $w, $h) or return;
802
803 return unless $self->{w};
804
535 my ($hs, $ws) = (0, 0); 805 ($h, $w) = ($w, $h);
536 806
537 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 807 my $children = $self->{children};
538 $hs += $self->max_row_height ($yi);
539 }
540 808
541 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 809 my @h = map +($_->size_request)[0], @$children;
542 my $wm = 0; 810
543 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 811 my $req_h = List::Util::sum @h;
544 $wm += $self->max_col_width ($xi) 812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ = $h[$_] * $h / $req_h for @h;
816 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1;
819
820 for (0 .. $#$children) {
821 my $child = $children->[$_];
822
823 my $alloc_h = $h[$_];
824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
825 $h[$_] = $alloc_h;
545 } 826 }
546 if ($ws < $wm) { $ws = $wm }
547 } 827 }
548
549 return ($ws, $hs);
550}
551
552sub _draw {
553 my ($self) = @_;
554 828
555 my $y = 0; 829 my $y = 0;
556 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 for (0 .. $#$children) {
831 my $child = $children->[$_];
557 my $x = 0; 832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
558 834
559 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 835 $y += $h;
836 }
837}
560 838
561 my $c = $self->{children}->[$yi]->[$xi]; 839#############################################################################
562 if ($c) { 840
563 $c->move ($x, $y, 0); #TODO: Move to size_request 841package CFClient::UI::VBox;
564 $c->draw if $c; 842
843# TODO: wrap into common Box base class
844
845our @ISA = CFClient::UI::Container::;
846
847sub size_request {
848 my ($self) = @_;
849
850 my @alloc = map [$_->size_request], @{$self->{children}};
851
852 (
853 (List::Util::max map $_->[0], @alloc),
854 (List::Util::sum map $_->[1], @alloc),
855 )
856}
857
858sub size_allocate {
859 my ($self, $x, $y, $w, $h) = @_;
860
861 $self->_size_allocate ($x, $y, $w, $h) or return;
862
863 return unless $self->{h};
864
865 my $children = $self->{children};
866
867 my @h = map +($_->size_request)[1], @$children;
868
869 my $req_h = List::Util::sum @h;
870
871 if ($req_h > $h) {
872 # ah well, not enough space
873 $_ = $h[$_] * $h / $req_h for @h;
874 } else {
875 my $exp = List::Util::sum map $_->{expand}, @$children;
876 $exp ||= 1;
565 } 877
878 for (0 .. $#$children) {
879 my $child = $children->[$_];
566 880
567 $x += $self->max_col_width ($xi); 881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
568 } 882 }
569
570 $y += $self->max_row_height ($yi);
571 } 883 }
572}
573 884
574############################################################################# 885 my $y = 0;
886 for (0 .. $#$children) {
887 my $child = $children->[$_];
888 my $h = $h[$_];
889 $child->size_allocate (0, $y, $w, $h);
575 890
576package Crossfire::Client::Widget::VBox; 891 $y += $h;
892 }
893}
577 894
578our @ISA = Crossfire::Client::Widget::Container::; 895#############################################################################
896
897package CFClient::UI::Label;
898
899our @ISA = CFClient::UI::Base::;
579 900
580use SDL::OpenGL; 901use SDL::OpenGL;
581 902
582sub size_allocate {
583 my ($self, $w, $h) = @_;
584
585 $self->w ($w);
586 $self->h ($h);
587
588 my $exp;
589 my @oth;
590 # find expand widget
591 for (@{$self->{children}}) {
592 if ($_->{expand}) {
593 $exp = $_;
594 last;
595 }
596 push @oth, $_;
597 }
598
599 my ($ow, $oh);
600
601 # get sizes of other widgets
602 for (@oth) {
603 my ($w, $h) = $_->size_request;
604 $oh += $h;
605 if ($ow < $w) { $ow = $w }
606 }
607
608 my $y = 0;
609 for (@{$self->{children}}) {
610 $_->move (0, $y);
611
612 if ($_ == $exp) {
613 $_->size_allocate ($w, $h - $oh);
614 $y += $h - $oh;
615 } else {
616 my ($cw, $h) = $_->size_request;
617 $_->size_allocate ($w, $h);
618 $y += $h;
619 }
620 }
621}
622
623sub _draw {
624 my ($self) = @_;
625
626 my ($x, $y);
627 for (@{$self->{children} || []}) {
628 $_->draw;
629 $y += $_->h;
630 }
631}
632
633#############################################################################
634
635package Crossfire::Client::Widget::Label;
636
637our @ISA = Crossfire::Client::Widget::;
638
639use SDL::OpenGL;
640
641sub new { 903sub new {
642 my ($class, $x, $y, $z, $height, $text) = @_; 904 my ($class, %arg) = @_;
643 905
644 # TODO: color, and make height, xyz etc. optional 906 my $self = $class->SUPER::new (
645 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 907 fg => [1, 1, 1],
908 height => $::FONTSIZE,
909 text => "",
910 align => -1,
911 padding => 2,
912 layout => new CFClient::Layout,
913 %arg
914 );
646 915
647 $self->set_text ($text); 916 $self->set_text ($self->{text});
648 917
649 $self 918 $self
919}
920
921sub escape_text {
922 local $_ = $_[1];
923
924 s/&/&amp;/g;
925 s/>/&gt;/g;
926 s/</&lt;/g;
927
928 $_[1]
650} 929}
651 930
652sub set_text { 931sub set_text {
653 my ($self, $text) = @_; 932 my ($self, $text) = @_;
654 933
655 $self->{text} = $text; 934 $self->{text} = $text;
656 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 935 $self->{layout}->set_markup ($text);
657 936
937 delete $self->{texture};
658 $self->update; 938 $self->update;
659} 939}
660 940
661sub get_text { 941sub get_text {
662 my ($self, $text) = @_; 942 my ($self, $text) = @_;
665} 945}
666 946
667sub size_request { 947sub size_request {
668 my ($self) = @_; 948 my ($self) = @_;
669 949
950 $self->{layout}->set_width;
951 $self->{layout}->set_height ($self->{height});
952 my ($w, $h) = $self->{layout}->size;
953
670 ( 954 (
671 $self->{texture}{width}, 955 $w + $self->{padding} * 2,
672 $self->{texture}{height}, 956 $h + $self->{padding} * 2,
673 ) 957 )
674} 958}
675 959
960sub size_allocate {
961 my ($self, $x, $y, $w, $h) = @_;
962
963 $self->_size_allocate ($x, $y, $w, $h) or return;
964
965 delete $self->{texture};
966}
967
968sub update {
969 my ($self) = @_;
970
971 delete $self->{texture};
972 $self->SUPER::update;
973}
974
676sub _draw { 975sub _draw {
677 my ($self) = @_; 976 my ($self) = @_;
678 977
679 my $tex = $self->{texture}; 978 my $tex = $self->{texture} ||= do {
979 $self->{layout}->set_width ($self->{w});
980 new_from_layout CFClient::Texture $self->{layout}
981 };
680 982
681 glEnable GL_BLEND; 983 glEnable GL_BLEND;
984 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
682 glEnable GL_TEXTURE_2D; 985 glEnable GL_TEXTURE_2D;
683 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
684 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
685 glBindTexture GL_TEXTURE_2D, $tex->{name};
686 987
687 glColor 1, 0, 0, 1; # TODO color 988 glColor @{$self->{fg}};
688 989
689 glBegin GL_QUADS; 990 my $x =
690 glTexCoord 0, 0; glVertex 0 , 0; 991 $self->{align} < 0 ? $self->{padding}
691 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 992 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
692 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 993 : ($self->{w} - $tex->{w}) * 0.5;
693 glTexCoord 1, 0; glVertex $tex->{width}, 0;
694 glEnd;
695 994
995 $tex->draw_quad ($x, $self->{padding});
996
997 glDisable GL_TEXTURE_2D;
696 glDisable GL_BLEND; 998 glDisable GL_BLEND;
697 glDisable GL_TEXTURE_2D;
698} 999}
699 1000
700############################################################################# 1001#############################################################################
701 1002
702package Crossfire::Client::Widget::TextEntry; 1003package CFClient::UI::Entry;
703 1004
704our @ISA = Crossfire::Client::Widget::Label::; 1005our @ISA = CFClient::UI::Label::;
705 1006
706use SDL; 1007use SDL;
707use SDL::OpenGL; 1008use SDL::OpenGL;
708 1009
1010sub new {
1011 my $class = shift;
1012
1013 $class->SUPER::new (
1014 fg => [1, 1, 1],
1015 bg => [0, 0, 0, 0.2],
1016 active_bg => [1, 1, 1],
1017 active_fg => [0, 0, 0],
1018 @_
1019 )
1020}
1021
1022sub _set_text {
1023 my ($self, $text) = @_;
1024
1025 $self->{last_activity} = $::NOW;
1026
1027 $self->{text} = $text;
1028 $self->{layout}->set_width ($self->{w});
1029
1030 $text =~ s/./*/g if $self->{hidden};
1031
1032 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1033
1034 $text = substr $text, 0, $self->{cursor};
1035 utf8::encode $text;
1036
1037 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1038}
1039
1040sub size_request {
1041 my ($self) = @_;
1042
1043 my ($w, $h) = $self->SUPER::size_request;
1044
1045 ($w + 1, $h) # add 1 for cursor
1046}
1047
1048sub size_allocate {
1049 my ($self, $x, $y, $w, $h) = @_;
1050
1051 $self->SUPER::size_allocate ($x, $y, $w, $h);
1052
1053 $self->_set_text ($self->{text});
1054}
1055
1056sub set_text {
1057 my ($self, $text) = @_;
1058
1059 $self->{cursor} = length $text;
1060 $self->_set_text ($text);
1061 $self->update;
1062}
1063
709sub key_down { 1064sub key_down {
710 my ($self, $ev) = @_; 1065 my ($self, $ev) = @_;
711 1066
712 my $mod = $ev->key_mod; 1067 my $mod = $ev->key_mod;
713 my $sym = $ev->key_sym; 1068 my $sym = $ev->key_sym;
714 1069
715 $ev->set_unicode (1);
716 my $uni = $ev->key_unicode; 1070 my $uni = $ev->key_unicode;
717 1071
718 my $text = $self->get_text; 1072 my $text = $self->get_text;
719 1073
720 if ($sym == SDLK_BACKSPACE) { 1074 if ($sym == SDLK_BACKSPACE) {
721 substr $text, -1, 1, ''; 1075 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
722 1076 } elsif ($sym == SDLK_DELETE) {
1077 substr $text, $self->{cursor}, 1, "";
1078 } elsif ($sym == SDLK_LEFT) {
1079 --$self->{cursor} if $self->{cursor};
1080 } elsif ($sym == SDLK_RIGHT) {
1081 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1082 } elsif ($sym == SDLK_HOME) {
1083 $self->{cursor} = 0;
1084 } elsif ($sym == SDLK_END) {
1085 $self->{cursor} = length $text;
723 } elsif ($uni) { 1086 } elsif ($uni) {
724 $text .= chr $uni; 1087 substr $text, $self->{cursor}++, 0, chr $uni;
725 } 1088 }
1089
726 $self->set_text ($text); 1090 $self->_set_text ($text);
1091 $self->update;
727} 1092}
728 1093
729############################################################################# 1094sub focus_in {
1095 my ($self) = @_;
730 1096
1097 $self->{last_activity} = $::NOW;
1098
1099 $self->SUPER::focus_in;
1100}
1101
1102sub button_down {
1103 my ($self, $ev, $x, $y) = @_;
1104
1105 $self->SUPER::button_down ($ev, $x, $y);
1106
1107 my $idx = $self->{layout}->xy_to_index ($x, $y);
1108
1109 # byte-index to char-index
1110 my $text = $self->{text};
1111 utf8::encode $text;
1112 $self->{cursor} = length substr $text, 0, $idx;
1113
1114 $self->_set_text ($self->{text});
1115 $self->update;
1116}
1117
1118sub mouse_motion {
1119 my ($self, $ev, $x, $y) = @_;
1120# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1121}
1122
1123sub _draw {
1124 my ($self) = @_;
1125
1126 local $self->{fg} = $self->{fg};
1127
1128 if ($FOCUS == $self) {
1129 glColor @{$self->{active_bg}};
1130 $self->{fg} = $self->{active_fg};
1131 } else {
1132 glColor @{$self->{bg}};
1133 }
1134
1135 glEnable GL_BLEND;
1136 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1137 glBegin GL_QUADS;
1138 glVertex 0 , 0;
1139 glVertex 0 , $self->{h};
1140 glVertex $self->{w}, $self->{h};
1141 glVertex $self->{w}, 0;
1142 glEnd;
1143 glDisable GL_BLEND;
1144
1145 $self->SUPER::_draw;
1146
1147 #TODO: force update every cursor change :(
1148 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1149 glColor @{$self->{fg}};
1150 glBegin GL_LINES;
1151 glVertex $self->{cur_x}, $self->{cur_y};
1152 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1153 glEnd;
1154 }
1155}
1156
1157#############################################################################
1158
1159package CFClient::UI::Slider;
1160
1161use strict;
1162
1163use SDL::OpenGL;
1164
1165our @ISA = CFClient::UI::DrawBG::;
1166
1167sub new {
1168 my $class = shift;
1169
1170 # range [value, low, high, page]
1171
1172 my $self = $class->SUPER::new (
1173 fg => [1, 1, 1],
1174 active_fg => [0, 0, 0],
1175 range => [0, 0, 100, 10],
1176 req_w => 40,
1177 req_h => 10,
1178 vertical => 0,
1179 @_
1180 );
1181
1182 $self
1183}
1184
1185sub size_request {
1186 my ($self) = @_;
1187
1188 my $w = $self->{req_w};
1189 my $h = $self->{req_h};
1190
1191 $self->{vertical} ? ($h, $w) : ($w, $h)
1192}
1193
1194sub button_down {
1195 my ($self, $ev, $x, $y) = @_;
1196
1197 $self->SUPER::button_down ($ev, $x, $y);
1198 $self->mouse_motion ($ev, $x, $y);
1199}
1200
1201sub mouse_motion {
1202 my ($self, $ev, $x, $y) = @_;
1203
1204 if ($GRAB == $self) {
1205 my ($value, $lo, $hi, $page) = @{$self->{range}};
1206
1207 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1208
1209 $x = $x * ($hi - $lo) / $w + $lo;
1210 $x = $lo if $x < $lo;
1211 $x = $hi - $page if $x > $hi - $page;
1212 $self->{range}[0] = $x;
1213
1214 $self->emit (changed => $x);
1215 $self->update;
1216 }
1217}
1218
1219sub _draw {
1220 my ($self) = @_;
1221
1222 $self->SUPER::_draw ();
1223
1224 my ($w, $h) = @$self{qw(w h)};
1225
1226 if ($self->{vertical}) {
1227 # draw a vertical slider like a rotated horizontal slider
1228
1229 glRotate 90, 0, 0, 1;
1230 glTranslate 0, -$self->{w}, 0;
1231
1232 ($w, $h) = ($h, $w);
1233 }
1234
1235 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1236 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1237
1238 my ($value, $lo, $hi, $page) = @{$self->{range}};
1239
1240 $page = int $page * $w / ($hi - $lo);
1241 $value = int +($value - $lo) * $w / ($hi - $lo);
1242
1243 $w -= $page;
1244 $page &= ~1;
1245 glTranslate $page * 0.5, 0, 0;
1246
1247 glColor @$fg;
1248 glBegin GL_LINES;
1249 glVertex 0, 0; glVertex 0, $h;
1250 glVertex $w - 1, 0; glVertex $w - 1, $h;
1251 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1252 glEnd;
1253
1254 my $knob_a = $value - $page * 0.5;
1255 my $knob_b = $value + $page * 0.5;
1256
1257 glBegin GL_QUADS;
1258 glColor @$fg;
1259 glVertex $knob_a, 0;
1260 glVertex $knob_a, $h;
1261 glVertex $knob_b, $h;
1262 glVertex $knob_b, 0;
1263
1264 if ($knob_a < $knob_b - 2) {
1265 glColor @$bg;
1266 glVertex $knob_a + 1, 1;
1267 glVertex $knob_a + 1, $h - 1;
1268 glVertex $knob_b - 1, $h - 1;
1269 glVertex $knob_b - 1, 1;
1270 }
1271 glEnd;
1272}
1273
1274#############################################################################
1275
731package Crossfire::Client::Widget::MapWidget; 1276package CFClient::UI::MapWidget;
732 1277
733use strict; 1278use strict;
734 1279
735use List::Util qw(min max); 1280use List::Util qw(min max);
736 1281
737use SDL; 1282use SDL;
738use SDL::OpenGL; 1283use SDL::OpenGL;
739use SDL::OpenGL::Constants;
740 1284
741our @ISA = Crossfire::Client::Widget::; 1285our @ISA = CFClient::UI::Base::;
1286
1287sub new {
1288 my $class = shift;
1289
1290 $class->SUPER::new (
1291 z => -1,
1292 list => (glGenLists 1),
1293 @_
1294 )
1295}
742 1296
743sub key_down { 1297sub key_down {
744 print "MAPKEYDOWN\n"; 1298 print "MAPKEYDOWN\n";
745} 1299}
746 1300
747sub key_up { 1301sub key_up {
748} 1302}
749 1303
750sub size_request { 1304sub size_request {
751 1305 (
1306 1 + 32 * int $::WIDTH / 32,
1307 1 + 32 * int $::HEIGHT / 32,
1308 )
752} 1309}
753 1310
754sub size_allocate { 1311sub update {
755} 1312 my ($self) = @_;
756 1313
1314 $self->{need_update} = 1;
1315 $self->SUPER::update;
1316}
1317
757sub _draw { 1318sub draw {
758 my ($self) = @_; 1319 my ($self) = @_;
759 1320
1321 if (delete $self->{need_update}) {
1322 glNewList $self->{list}, GL_COMPILE;
1323
760 my $mx = $::CONN->{mapx}; 1324 my $mx = $::CONN->{mapx};
761 my $my = $::CONN->{mapy}; 1325 my $my = $::CONN->{mapy};
762 1326
763 my $map = $::CONN->{map}; 1327 my $map = $::CONN->{map};
764 1328
765 my ($xofs, $yofs); 1329 my ($xofs, $yofs);
766 1330
767 my $sw = 1 + int $::WIDTH / 32; 1331 my $sw = 1 + int $::WIDTH / 32;
768 my $sh = 1 + int $::HEIGHT / 32; 1332 my $sh = 1 + int $::HEIGHT / 32;
769 1333
770 if ($::CONN->{mapw} > $sw) { 1334 if ($::CONN->{mapw} > $sw) {
771 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1335 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
772 } else { 1336 } else {
773 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1337 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
774 } 1338 }
775 1339
776 if ($::CONN->{maph} > $sh) { 1340 if ($::CONN->{maph} > $sh) {
777 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1341 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
778 } else { 1342 } else {
779 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1343 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
780 } 1344 }
781 1345
782 glEnable GL_TEXTURE_2D; 1346 glEnable GL_TEXTURE_2D;
783 glEnable GL_BLEND; 1347 glEnable GL_BLEND;
1348 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
784 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
785 1350
786 my $sw4 = ($sw + 3) & ~3; 1351 my $sw4 = ($sw + 3) & ~3;
787 my $lighting = "\x00" x ($sw4 * $sh); 1352 my $darkness = "\x00" x ($sw4 * $sh);
788 1353
789 for my $x (0 .. $sw - 1) { 1354 for my $x (0 .. $sw - 1) {
1355 my $row = $map->[$x + $xofs];
790 for my $y (0 .. $sh - 1) { 1356 for my $y (0 .. $sh - 1) {
791 1357
792 my $cell = $map->[$x + $xofs][$y + $yofs] 1358 my $cell = $row->[$y + $yofs]
793 or next; 1359 or next;
794 1360
795 my $darkness = $cell->[0] * (1 / 255); 1361 my $dark = $cell->[0];
796 if ($darkness < 0) { 1362 if ($dark < 0) {
797 $darkness = 0.15; 1363 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1364 } else {
1365 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
798 } 1366 }
799 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
800 1367
801 for my $num (grep $_, @$cell[1,2,3]) { 1368 for my $num (grep $_, @$cell[1,2,3]) {
802 my $tex = $::CONN->{face}[$num]{texture} || next; 1369 my $tex = $::CONN->{face}[$num]{texture} || next;
1370
1371 my ($w, $h) = @$tex{qw(w h)};
803 1372
804 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1373 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
805
806 my $w = $tex->{width};
807 my $h = $tex->{height};
808
809 my $px = ($x + 1) * 32 - $w;
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; 1374 }
818 } 1375 }
819 } 1376 }
820 }
821 1377
822# if (1) { # higher quality darkness 1378# if (1) { # higher quality darkness
823# $lighting =~ s/(.)/$1$1$1/gs; 1379# $lighting =~ s/(.)/$1$1$1/gs;
824# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1380# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
825# 1381#
827# 1383#
828# $lighting = $pb->get_pixels; 1384# $lighting = $pb->get_pixels;
829# $lighting =~ s/(.)../$1/gs; 1385# $lighting =~ s/(.)../$1/gs;
830# } 1386# }
831 1387
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;
840 glColor 0, 0, 0, 0.75;
841 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1388 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 1389
1390 $darkness = new CFClient::Texture
1391 w => $sw4,
1392 h => $sh,
1393 data => $darkness,
1394 internalformat => GL_ALPHA,
1395 format => GL_ALPHA;
1396
1397 glColor 0.45, 0.45, 0.45, 1;
1398 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1399
851 glDisable GL_TEXTURE_2D; 1400 glDisable GL_TEXTURE_2D;
852 glDisable GL_BLEND; 1401 glDisable GL_BLEND;
1402
1403 glEndList;
1404 }
1405
1406 glCallList $self->{list};
853} 1407}
854 1408
855my %DIR = ( 1409my %DIR = (
856 SDLK_KP8, [1, "north"], 1410 SDLK_KP8, [1, "north"],
857 SDLK_KP9, [2, "northeast"], 1411 SDLK_KP9, [2, "northeast"],
903 } 1457 }
904} 1458}
905 1459
906############################################################################# 1460#############################################################################
907 1461
908package Crossfire::Client::Widget::Animator; 1462package CFClient::UI::Animator;
909 1463
910use SDL::OpenGL; 1464use SDL::OpenGL;
911 1465
912our @ISA = Crossfire::Client::Widget::Bin::; 1466our @ISA = CFClient::UI::Bin::;
913 1467
914sub moveto { 1468sub moveto {
915 my ($self, $x, $y) = @_; 1469 my ($self, $x, $y) = @_;
916 1470
917 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1471 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
918 $self->{speed} = 0.2; 1472 $self->{speed} = 0.001;
919 $self->{time} = 1; 1473 $self->{time} = 1;
920 1474
921 ::animation_start $self; 1475 ::animation_start $self;
922} 1476}
923 1477
938 1492
939sub _draw { 1493sub _draw {
940 my ($self) = @_; 1494 my ($self) = @_;
941 1495
942 glPushMatrix; 1496 glPushMatrix;
943 glRotate $self->{time} * 10000, 0, 1, 0; 1497 glRotate $self->{time} * 1000, 0, 1, 0;
944 $self->{children}[0]->draw; 1498 $self->{children}[0]->draw;
945 glPopMatrix; 1499 glPopMatrix;
946} 1500}
947 1501
9481; 1502#############################################################################
949 1503
1504package CFClient::UI::Toplevel;
1505
1506our @ISA = CFClient::UI::Container::;
1507
1508sub size_request {
1509 ($::WIDTH, $::HEIGHT)
1510}
1511
1512sub size_allocate {
1513 my ($self, $x, $y, $w, $h) = @_;
1514
1515 $self->_size_allocate ($x, $y, $w, $h);
1516
1517 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1518 for @{$self->{children}};
1519}
1520
1521sub translate {
1522 my ($self, $x, $y) = @_;
1523
1524 ($x, $y)
1525}
1526
1527sub update {
1528 my ($self) = @_;
1529
1530 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1531 ::refresh ();
1532}
1533
1534sub add {
1535 my ($self, $widget) = @_;
1536
1537 $self->SUPER::add ($widget);
1538
1539 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1540}
1541
1542sub draw {
1543 my ($self) = @_;
1544
1545 $self->_draw;
1546}
1547
1548#############################################################################
1549
1550package CFClient::UI;
1551
1552$TOPLEVEL = new CFClient::UI::Toplevel;
1553
15541
1555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines