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

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.34 by elmex, Sun Apr 9 02:44:51 2006 UTC vs.
Revision 1.73 by root, Tue Apr 11 18:06:53 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines