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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines