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.43 by root, Sun Apr 9 22:12:11 2006 UTC vs.
Revision 1.124 by elmex, Mon Apr 17 20:29:37 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 $ROOT;
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 = $ROOT->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->coord2local ($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 || $ROOT->find_widget ($x, $y);
45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($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 || $ROOT->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for (@$vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
84}
85
86#############################################################################
87
88package CFClient::UI::Base;
89
90use strict;
6 91
7use SDL::OpenGL; 92use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9
10use Crossfire::Client;
11
12our $FOCUS; # the widget with current focus
13
14# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
17sub feed_sdl_button_down_event { }
18sub feed_sdl_button_up_event { }
19 93
20sub new { 94sub new {
21 my $class = shift; 95 my $class = shift;
22 96
23 bless { @_ }, $class 97 my $self = bless {
98 x => 0,
99 y => 0,
100 z => 0,
101 w => -1,
102 h => -1,
103 @_
104 }, $class;
105
106 for (keys %$self) {
107 if (/^connect_(.*)$/) {
108 $self->connect ($1 => delete $self->{$_});
109 }
110 }
111
112 $self
24} 113}
25 114
26sub move { 115sub move {
27 my ($self, $x, $y, $z) = @_; 116 my ($self, $x, $y, $z) = @_;
28 $self->{x} = $x; 117 $self->{x} = int $x;
29 $self->{y} = $y; 118 $self->{y} = int $y;
30 $self->{z} = $z if defined $z; 119 $self->{z} = $z if defined $z;
31} 120}
32 121
33sub needs_redraw { 122sub needs_redraw {
34 0 123 0
37sub size_request { 126sub size_request {
38 require Carp; 127 require Carp;
39 Carp::confess "size_request is abtract"; 128 Carp::confess "size_request is abtract";
40} 129}
41 130
42sub size_allocate { 131sub _size_allocate {
43 my ($self, $w, $h) = @_; 132 my ($self, $x, $y, $w, $h) = @_;
133
134 $self->{x} = $x;
135 $self->{y} = $y;
136
137 return unless $self->{w} != $w || $self->{h} != $h;
44 138
45 $self->{w} = $w; 139 $self->{w} = $w;
46 $self->{h} = $h; 140 $self->{h} = $h;
141
142 1
143}
144
145sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_;
147
148 $self->_size_allocate ($x, $y, $w, $h);
149}
150
151# return top left coordinates
152sub _topleft {
153 my ($self, $x, $y) = @_;
154
155 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
156}
157
158# translate global coordinates to local coordinate system
159sub coord2local {
160 my ($self, $x, $y) = @_;
161
162 my ($X, $Y) = $self->_topleft;
163 ($x - $X, $y - $Y)
164}
165
166# translate local coordinates to global coordinate system
167sub coord2global {
168 my ($self, $x, $y) = @_;
169
170 my ($X, $Y) = $self->_topleft;
171 ($x + $X, $y + $Y)
47} 172}
48 173
49sub focus_in { 174sub focus_in {
50 my ($widget) = @_; 175 my ($self) = @_;
51 $FOCUS = $widget; 176
177 return if $FOCUS == $self;
178 return unless $self->{can_focus};
179
180 my $focus = $FOCUS; $FOCUS = $self;
181
182 $self->emit (focus_in => $focus);
183
184 $focus->update if $focus;
185 $FOCUS->update;
52} 186}
53 187
54sub focus_out { 188sub focus_out {
55 my ($widget) = @_; 189 my ($self) = @_;
56}
57 190
191 return unless $FOCUS == $self;
192
193 my $focus = $FOCUS; undef $FOCUS;
194
195 $self->emit (focus_out => $focus);
196
197 $focus->update if $focus; #?
198}
199
200sub mouse_motion { }
201sub button_up { }
58sub key_down { 202sub key_down { }
59 my ($widget, $sdlev) = @_;
60}
61
62sub key_up { 203sub key_up { }
63 my ($widget, $sdlev) = @_;
64}
65 204
66sub button_down { 205sub button_down {
67 my ($widget, $sdlev) = @_; 206 my ($self, $ev, $x, $y) = @_;
68}
69 207
70sub button_up { 208 $self->focus_in;
71 my ($widget, $sdlev) = @_;
72} 209}
73 210
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 211sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 212sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 213sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 214sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 215sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 216
80sub draw { 217sub draw {
81 my ($self) = @_; 218 my ($self) = @_;
219
220 return unless $self->{h} && $self->{w};
82 221
83 glPushMatrix; 222 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 223 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 224 $self->_draw;
86 glPopMatrix; 225 glPopMatrix;
226
227 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)};
229
230 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS;
234 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y;
236 glVertex $x + $self->{w}, $y + $self->{h};
237 glVertex $x , $y + $self->{h};
238 glEnd;
239 glDisable GL_BLEND;
240 }
87} 241}
88 242
89sub _draw { 243sub _draw {
90 my ($self) = @_; 244 my ($self) = @_;
91 245
92 warn "no draw defined for $self\n"; 246 warn "no draw defined for $self\n";
93}
94
95sub bbox {
96 my ($self) = @_;
97 my ($w, $h) = $self->size_request;
98 (
99 $self->{x},
100 $self->{y},
101 $self->{x} = $w,
102 $self->{y} = $h
103 )
104} 247}
105 248
106sub find_widget { 249sub find_widget {
107 my ($self, $x, $y) = @_; 250 my ($self, $x, $y) = @_;
108 251
111 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 254 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
112 255
113 () 256 ()
114} 257}
115 258
116sub del_parent { $_[0]->{parent} = undef }
117
118sub set_parent { 259sub set_parent {
119 my ($self, $par) = @_; 260 my ($self, $parent) = @_;
120 261
121 $self->{parent} = $par;
122 Scalar::Util::weaken $self->{parent}; 262 Scalar::Util::weaken ($self->{parent} = $parent);
123}
124
125sub get_parent {
126 $_[0]->{parent}
127} 263}
128 264
129sub update { 265sub update {
130 my ($self) = @_; 266 my ($self) = @_;
131 267
132 $self->{parent}->update 268 $self->{parent}->update
133 if $self->{parent}; 269 if $self->{parent};
134} 270}
135 271
272sub connect {
273 my ($self, $signal, $cb) = @_;
274
275 push @{ $self->{cb}{$signal} }, $cb;
276}
277
278sub emit {
279 my ($self, $signal, @args) = @_;
280
281 $_->($self, @args)
282 for @{$self->{cb}{$signal} || []};
283}
284
136sub DESTROY { 285sub DESTROY {
137 my ($self) = @_; 286 my ($self) = @_;
138 287
139 #$self->deactivate; 288 #$self->deactivate;
140} 289}
141 290
142############################################################################# 291#############################################################################
143 292
144package Crossfire::Client::Widget::Container; 293package CFClient::UI::DrawBG;
145 294
146our @ISA = Crossfire::Client::Widget::; 295our @ISA = CFClient::UI::Base::;
296
297use strict;
298use SDL::OpenGL;
147 299
148sub new { 300sub new {
301 my $class = shift;
302
303 # range [value, low, high, page]
304
305 $class->SUPER::new (
306 bg => [0, 0, 0, 0.2],
307 active_bg => [1, 1, 1, 0.5],
308 @_
309 )
310}
311
312sub _draw {
313 my ($self) = @_;
314
315 my ($w, $h) = @$self{qw(w h)};
316
317 glEnable GL_BLEND;
318 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
319 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
320
321 glBegin GL_QUADS;
322 glVertex 0 , 0;
323 glVertex 0 , $h;
324 glVertex $w, $h;
325 glVertex $w, 0;
326 glEnd;
327
328 glDisable GL_BLEND;
329}
330
331#############################################################################
332
333package CFClient::UI::Empty;
334
335our @ISA = CFClient::UI::Base::;
336
337sub size_request {
338 (0, 0)
339}
340
341sub draw { }
342
343#############################################################################
344
345package CFClient::UI::Container;
346
347our @ISA = CFClient::UI::Base::;
348
349sub new {
149 my ($class, @widgets) = @_; 350 my ($class, %arg) = @_;
150 351
352 my $children = delete $arg{children} || [];
353
151 my $self = $class->SUPER::new (children => []); 354 my $self = $class->SUPER::new (children => [], %arg);
152 $self->add ($_) for @widgets; 355 $self->add ($_) for @$children;
153 356
154 $self 357 $self
155} 358}
156 359
157sub add { 360sub add {
158 my ($self, $chld, $expand) = @_; 361 my ($self, $child) = @_;
159 362
160 $chld->{expand} = $expand;
161 $chld->set_parent ($self); 363 $child->set_parent ($self);
162 364
365 use sort 'stable';
366
163 @{$self->{children}} = 367 $self->{children} = [
164 sort { $a->{z} <=> $b->{z} } 368 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 369 @{$self->{children}}, $child
370 ];
166 371
167 $self->size_allocate ($self->{w}, $self->{h}) 372 $self->{w} = $self->{h} = -1;
168 if $self->{w}; #TODO: check for "realised state" 373 $self->update;
169} 374}
170 375
171sub remove { 376sub remove {
172 my ($self, $widget) = @_; 377 my ($self, $widget) = @_;
173 378
174 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
175 380
176 $self->size_allocate ($self->{w}, $self->{h}); 381 $self->size_allocate (0, 0, $self->{w}, $self->{h});
177} 382}
178 383
179sub find_widget { 384sub find_widget {
180 my ($self, $x, $y) = @_; 385 my ($self, $x, $y) = @_;
181 386
387 $x -= $self->{x};
388 $y -= $self->{y};
389
182 my $res; 390 my $res;
183 391
184 for (@{ $self->{children} }) { 392 for (reverse @{ $self->{children} }) {
185 $res = $_->find_widget ($x, $y) 393 $res = $_->find_widget ($x, $y)
186 and return $res; 394 and return $res;
187 } 395 }
188 396
189 () 397 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
190} 398}
191 399
192sub _draw { 400sub _draw {
193 my ($self) = @_; 401 my ($self) = @_;
194 402
195 $_->draw for @{$self->{children}}; 403 $_->draw for @{$self->{children}};
196} 404}
197 405
198############################################################################# 406#############################################################################
199 407
200package Crossfire::Client::Widget::Bin; 408package CFClient::UI::Bin;
201 409
202our @ISA = Crossfire::Client::Widget::Container::; 410our @ISA = CFClient::UI::Container::;
203 411
204sub child { $_[0]->{children}[0] } 412sub new {
413 my ($class, %arg) = @_;
205 414
206sub size_request { 415 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
207 $_[0]{children}[0]->size_request if $_[0]{children}[0];
208}
209 416
210sub size_allocate { 417 $class->SUPER::new (children => [$child], %arg)
211 my ($self, $w, $h) = @_;
212
213 $self->SUPER::size_allocate ($w, $h);
214 $self->{children}[0]->size_allocate ($w, $h)
215 if $self->{children}[0]
216}
217
218#############################################################################
219
220package Crossfire::Client::Widget::Toplevel;
221
222our @ISA = Crossfire::Client::Widget::Container::;
223
224sub update {
225 my ($self) = @_;
226
227 ::refresh ();
228} 418}
229 419
230sub add { 420sub add {
231 my ($self, $widget) = @_; 421 my ($self, $widget) = @_;
232 422
423 $self->{children} = [];
424
233 $self->SUPER::add ($widget); 425 $self->SUPER::add ($widget);
234
235 $widget->size_allocate ($widget->size_request);
236} 426}
237 427
238############################################################################# 428sub remove {
429 my ($self, $widget) = @_;
239 430
240package Crossfire::Client::Widget::Window; 431 $self->SUPER::remove ($widget);
241 432
242our @ISA = Crossfire::Client::Widget::Bin::; 433 $self->{children} = [new CFClient::UI::Empty]
434 unless @{$self->{children}};
435}
436
437sub child { $_[0]->{children}[0] }
438
439sub size_request {
440 $_[0]{children}[0]->size_request
441}
442
443sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_;
445
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h);
449}
450
451#############################################################################
452
453package CFClient::UI::Window;
454
455our @ISA = CFClient::UI::Bin::;
243 456
244use SDL::OpenGL; 457use SDL::OpenGL;
245 458
246sub new { 459sub new {
247 my ($class, $x, $y, $z, $w, $h) = @_; 460 my ($class, %arg) = @_;
248 461
249 my $self = $class->SUPER::new; 462 my $self = $class->SUPER::new (%arg);
250
251 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
252} 463}
253 464
254sub update { 465sub update {
255 my ($self) = @_; 466 my ($self) = @_;
256 467
468 # we want to do this delayed...
257 $self->render_chld; 469 $self->render_chld;
258 $self->SUPER::update; 470 $self->SUPER::update;
259} 471}
260 472
261sub render_chld { 473sub render_chld {
262 my ($self) = @_; 474 my ($self) = @_;
263 475
264 $self->{texture} = 476 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
265 Crossfire::Client::Texture->new_from_opengl ( 477 glClearColor 0, 0, 0, 1;
266 $self->{w}, $self->{h}, sub { $self->child->draw } 478 glClear GL_COLOR_BUFFER_BIT;
267 ); 479 $self->child->draw;
480 };
268} 481}
269 482
270sub size_allocate { 483sub size_allocate {
271 my ($self, $w, $h) = @_; 484 my ($self, $x, $y, $w, $h) = @_;
272 485
273 $self->{w} = $w; 486 $self->_size_allocate ($x, $y, $w, $h) or return;
274 $self->{h} = $h;
275 487
276 $self->child->size_allocate ($w, $h); 488 $self->child->size_allocate (0, 0, $w, $h);
277 489
278 $self->render_chld; 490 $self->render_chld;
279} 491}
280 492
281sub _draw { 493sub _draw {
285 497
286 my $tex = $self->{texture} 498 my $tex = $self->{texture}
287 or return; 499 or return;
288 500
289 glEnable GL_BLEND; 501 glEnable GL_BLEND;
502 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
290 glEnable GL_TEXTURE_2D; 503 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 504 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293 505
294 glBegin GL_QUADS; 506 $tex->draw_quad (0, 0, $w, $h);
295 glTexCoord 0, 0; glVertex 0, 0;
296 glTexCoord 0, 1; glVertex 0, $h;
297 glTexCoord 1, 1; glVertex $w, $h;
298 glTexCoord 1, 0; glVertex $w, 0;
299 glEnd;
300 507
301 glDisable GL_BLEND; 508 glDisable GL_BLEND;
302 glDisable GL_TEXTURE_2D; 509 glDisable GL_TEXTURE_2D;
303} 510}
304 511
305############################################################################# 512#############################################################################
306 513
307package Crossfire::Client::Widget::Frame; 514package CFClient::UI::Frame;
308 515
309our @ISA = Crossfire::Client::Widget::Bin::; 516our @ISA = CFClient::UI::Bin::;
310 517
311use SDL::OpenGL; 518use SDL::OpenGL;
312 519
313sub size_request { 520sub size_request {
314 my ($self) = @_; 521 my ($self) = @_;
319 526
320 map { $_ + 4 } $chld->size_request; 527 map { $_ + 4 } $chld->size_request;
321} 528}
322 529
323sub size_allocate { 530sub size_allocate {
324 my ($self, $w, $h) = @_; 531 my ($self, $x, $y, $w, $h) = @_;
325 532
326 $self->{w} = $w; 533 $self->_size_allocate ($x, $y, $w, $h) or return;
327 $self->{h} = $h;
328 534
329 $self->child->size_allocate ($w - 4, $h - 4); 535 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
330 $self->child->move (2, 2);
331} 536}
332 537
333sub _draw { 538sub _draw {
334 my ($self) = @_; 539 my ($self) = @_;
335 540
337 542
338 my ($w, $h) = $chld->size_request; 543 my ($w, $h) = $chld->size_request;
339 544
340 glBegin GL_QUADS; 545 glBegin GL_QUADS;
341 glColor 0, 0, 0; 546 glColor 0, 0, 0;
342 glTexCoord 0, 0; glVertex 0 , 0; 547 glVertex 0 , 0;
343 glTexCoord 0, 1; glVertex 0 , $h + 4; 548 glVertex 0 , $h + 4;
344 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 549 glVertex $w + 4 , $h + 4;
345 glTexCoord 1, 0; glVertex $w + 4 , 0; 550 glVertex $w + 4 , 0;
346 glEnd; 551 glEnd;
347 552
348 $chld->draw; 553 $chld->draw;
349} 554}
350 555
351############################################################################# 556#############################################################################
352 557
353package Crossfire::Client::Widget::FancyFrame; 558package CFClient::UI::FancyFrame;
354 559
355our @ISA = Crossfire::Client::Widget::Bin::; 560our @ISA = CFClient::UI::Bin::;
356 561
357use SDL::OpenGL; 562use SDL::OpenGL;
358 563
359my @tex = 564my @tex =
360 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
361 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 566 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
362 567
568sub new {
569 my $class = shift;
570
571 # TODO: user_x, user_y, overwrite moveto?
572
573 $class->SUPER::new (
574 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8,
577 @_
578 )
579}
580
363sub size_request { 581sub size_request {
364 my ($self) = @_; 582 my ($self) = @_;
365 583
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585
366 my ($w, $h) = $self->SUPER::size_request; 586 my ($w, $h) = $self->SUPER::size_request;
367 587
368 $h += $tex[1]->{height}; 588 (
369 $h += $tex[4]->{height}; 589 $w + $self->{border} * 2,
370 $w += $tex[2]->{width}; 590 $h + $self->{border} * 2,
371 $w += $tex[3]->{width}; 591 )
372
373 ($w, $h)
374} 592}
375 593
376sub size_allocate { 594sub size_allocate {
377 my ($self, $w, $h) = @_; 595 my ($self, $x, $y, $w, $h) = @_;
378 596
379 $self->SUPER::size_allocate ($w, $h); 597 $self->_size_allocate ($x, $y, $w, $h) or return;
380 598
381 $h -= $tex[1]->{height}; 599 $h -= List::Util::max 0, $self->{border} * 2;
382 $h -= $tex[4]->{height}; 600 $w -= List::Util::max 0, $self->{border} * 2;
383 $w -= $tex[2]->{width};
384 $w -= $tex[3]->{width};
385 601
386 $h = $h < 0 ? 0 : $h; 602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h);
387 $w = $w < 0 ? 0 : $w; 603}
388 604
389 $self->child->size_allocate ($w, $h); 605sub button_down {
390 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 606 my ($self, $ev, $x, $y) = @_;
607
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border}
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) {
610
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
612 my ($bw, $bh) = ($self->{w}, $self->{h});
613
614 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_;
616
617 ($x, $y) = ($ev->motion_x, $ev->motion_y);
618
619 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy;
621 $self->update;
622 };
623
624 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) {
626
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
628 my ($bx, $by) = ($self->{x}, $self->{y});
629
630 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_;
632
633 ($x, $y) = ($ev->motion_x, $ev->motion_y);
634
635 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update;
637 };
638 }
639}
640
641sub button_up {
642 my ($self, $ev, $x, $y) = @_;
643
644 delete $self->{motion};
645}
646
647sub mouse_motion {
648 my ($self, $ev, $x, $y) = @_;
649
650 $self->{motion}->($ev, $x, $y) if $self->{motion};
391} 651}
392 652
393sub _draw { 653sub _draw {
394 my ($self) = @_; 654 my ($self) = @_;
395 655
396 my ($w, $h) = ($self->{w}, $self->{h}); 656 my ($w, $h ) = ($self->{w}, $self->{h});
397 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 657 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
398 658
399 glEnable GL_BLEND; 659 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
400 glEnable GL_TEXTURE_2D; 661 glEnable GL_TEXTURE_2D;
401 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
402 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
403 663
404 my $top = $tex[1]; 664 glColor @{ $self->{border_bg} };
405 glBindTexture GL_TEXTURE_2D, $top->{name}; 665 $tex[1]->draw_quad (0, 0, $w, $self->{border});
406 666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch);
407 glBegin GL_QUADS; 667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch);
408 glTexCoord 0, 0; glVertex 0 , 0; 668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border});
409 glTexCoord 0, 1; glVertex 0 , $top->{height};
410 glTexCoord 1, 1; glVertex $w , $top->{height};
411 glTexCoord 1, 0; glVertex $w , 0;
412 glEnd;
413
414 my $left = $tex[3];
415 glBindTexture GL_TEXTURE_2D, $left->{name};
416
417 glBegin GL_QUADS;
418 glTexCoord 0, 0; glVertex 0 , $top->{height};
419 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
420 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
421 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
422 glEnd;
423
424 my $right = $tex[2];
425 glBindTexture GL_TEXTURE_2D, $right->{name};
426
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
429 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
430 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
431 glTexCoord 1, 0; glVertex $w , $top->{height};
432 glEnd;
433
434 my $bottom = $tex[4];
435 glBindTexture GL_TEXTURE_2D, $bottom->{name};
436
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
439 glTexCoord 0, 1; glVertex 0 , $h;
440 glTexCoord 1, 1; glVertex $w , $h;
441 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
442 glEnd;
443 669
444 my $bg = $tex[0]; 670 my $bg = $tex[0];
445 glBindTexture GL_TEXTURE_2D, $bg->{name};
446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
447 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
448 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
449 671
672 # TODO: repeat texture not scale
450 my $rep_x = $cw / $bg->{width}; 673 my $rep_x = $cw / $bg->{w};
451 my $rep_y = $ch / $bg->{height}; 674 my $rep_y = $ch / $bg->{h};
452 675
453 glBegin GL_QUADS; 676 glColor @{ $self->{bg} };
454 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
455 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
456 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
457 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
458 glEnd;
459 677
678 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
682
683 glDisable GL_TEXTURE_2D;
460 glDisable GL_BLEND; 684 glDisable GL_BLEND;
461 glDisable GL_TEXTURE_2D;
462 685
463 $self->child->draw; 686 $self->child->draw;
464
465} 687}
466 688
467############################################################################# 689#############################################################################
468 690
469package Crossfire::Client::Widget::Table; 691package CFClient::UI::Table;
470 692
471our @ISA = Crossfire::Client::Widget::Bin::; 693our @ISA = CFClient::UI::Base::;
694
695use List::Util qw(max sum);
472 696
473use SDL::OpenGL; 697use SDL::OpenGL;
474 698
699sub new {
700 my $class = shift;
701
702 $class->SUPER::new (
703 col_expand => [],
704 @_
705 )
706}
707
475sub add { 708sub add {
476 my ($self, $x, $y, $chld) = @_; 709 my ($self, $x, $y, $child) = @_;
477 my $old_chld = $self->{children}[$y][$x];
478 710
711 $child->set_parent ($self);
479 $self->{children}[$y][$x] = $chld; 712 $self->{children}[$y][$x] = $child;
480 $chld->set_parent ($self); 713
714 $self->{w} = $self->{h} = -1;
481 $self->update; 715 $self->update;
482} 716}
483 717
484sub max_row_height { 718# TODO: move to container class maybe? send childs a signal on removal?
719sub clear {
485 my ($self, $row) = @_; 720 my ($self) = @_;
486 721
487 my $hs = 0; 722 delete $self->{children};
488 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 723 $self->update;
724}
725
726sub get_wh {
727 my ($self) = @_;
728
729 my (@w, @h);
730
731 for my $y (0 .. $#{$self->{children}}) {
489 my $c = $self->{children}->[$row]->[$xi]; 732 my $row = $self->{children}[$y]
490 if ($c) { 733 or next;
734
735 for my $x (0 .. $#$row) {
736 my $widget = $row->[$x]
737 or next;
491 my ($w, $h) = $c->size_request; 738 my ($w, $h) = $widget->size_request;
492 if ($hs < $h) { $hs = $h } 739
740 $w[$x] = max $w[$x], $w;
741 $h[$y] = max $h[$y], $h;
493 } 742 }
494 } 743 }
495 return $hs;
496}
497 744
498sub max_col_width { 745 (\@w, \@h)
746}
747
748sub size_request {
499 my ($self, $col) = @_; 749 my ($self) = @_;
500 750
751 my ($ws, $hs) = $self->get_wh;
752
753 (
754 (sum @$ws),
755 (sum @$hs),
756 )
757}
758
759sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763
764 my ($ws, $hs) = $self->get_wh;
765
766 my $req_w = sum @$ws;
767 my $req_h = sum @$hs;
768
769 # TODO: nicer code && do row_expand
770 my @col_expand = @{$self->{col_expand}};
771 @col_expand = (1) x @$ws unless @col_expand;
772 my $col_expand = (sum @col_expand) || 1;
773
774 # linearly scale sizes
775 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
776 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
777
778 CFClient::UI::harmonize $ws;
779 CFClient::UI::harmonize $hs;
780
781 my $y;
782
783 for my $r (0 .. $#{$self->{children}}) {
784 my $row = $self->{children}[$r]
785 or next;
786
501 my $ws = 0; 787 my $x = 0;
502 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 788 my $row_h = $hs->[$r];
503 my $c = ($self->{children}->[$yi] || [])->[$col]; 789
504 if ($c) { 790 for my $c (0 .. $#$row) {
505 my ($w, $h) = $c->size_request; 791 my $col_w = $ws->[$c];
506 if ($ws < $w) { $ws = $w } 792
793 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h);
795 }
796
797 $x += $col_w;
507 } 798 }
799
800 $y += $row_h;
508 } 801 }
509 return $ws; 802
510} 803}
804
805sub find_widget {
806 my ($self, $x, $y) = @_;
807
808 $x -= $self->{x};
809 $y -= $self->{y};
810
811 my $res;
812
813 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
814 $res = $_->find_widget ($x, $y)
815 and return $res;
816 }
817
818 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
819}
820
821sub _draw {
822 my ($self) = @_;
823
824 for (grep $_, @{$self->{children}}) {
825 $_->draw for grep $_, @$_;
826 }
827}
828
829#############################################################################
830
831package CFClient::UI::HBox;
832
833# TODO: wrap into common Box base class
834
835our @ISA = CFClient::UI::Container::;
511 836
512sub size_request { 837sub size_request {
513 my ($self) = @_; 838 my ($self) = @_;
514 839
840 my @alloc = map [$_->size_request], @{$self->{children}};
841
842 (
843 (List::Util::sum map $_->[0], @alloc),
844 (List::Util::max map $_->[1], @alloc),
845 )
846}
847
848sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852
515 my ($hs, $ws) = (0, 0); 853 ($h, $w) = ($w, $h);
516 854
517 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 855 my $children = $self->{children};
518 $hs += $self->max_row_height ($yi);
519 }
520 856
521 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 857 my @h = map +($_->size_request)[0], @$children;
522 my $wm = 0; 858
523 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 859 my $req_h = List::Util::sum @h;
524 $wm += $self->max_col_width ($xi) 860
861 if ($req_h > $h) {
862 # ah well, not enough space
863 $_ *= $h / $req_h for @h;
864 } else {
865 my $exp = List::Util::sum map $_->{expand}, @$children;
866 $exp ||= 1;
867
868 for (0 .. $#$children) {
869 my $child = $children->[$_];
870
871 my $alloc_h = $h[$_];
872 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
873 $h[$_] = $alloc_h;
525 } 874 }
526 if ($ws < $wm) { $ws = $wm }
527 } 875 }
528 876
529 return ($ws, $hs); 877 CFClient::UI::harmonize \@h;
530}
531
532sub _draw {
533 my ($self) = @_;
534 878
535 my $y = 0; 879 my $y = 0;
536 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 880 for (0 .. $#$children) {
881 my $child = $children->[$_];
537 my $x = 0; 882 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w);
538 884
539 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 885 $y += $h;
540
541 my $c = $self->{children}->[$yi]->[$xi];
542 if ($c) {
543 $c->move ($x, $y, 0); #TODO: Move to size_request
544 $c->draw if $c;
545 }
546
547 $x += $self->max_col_width ($xi);
548 }
549
550 $y += $self->max_row_height ($yi);
551 } 886 }
552} 887}
553 888
554############################################################################# 889#############################################################################
555 890
556package Crossfire::Client::Widget::VBox; 891package CFClient::UI::VBox;
557 892
893# TODO: wrap into common Box base class
894
558our @ISA = Crossfire::Client::Widget::Container::; 895our @ISA = CFClient::UI::Container::;
559
560use SDL::OpenGL;
561 896
562sub size_request { 897sub size_request {
563 my ($self) = @_; 898 my ($self) = @_;
564 899
565 my @alloc = map [$_->size_request], @{$self->{children}}; 900 my @alloc = map [$_->size_request], @{$self->{children}};
569 (List::Util::sum map $_->[1], @alloc), 904 (List::Util::sum map $_->[1], @alloc),
570 ) 905 )
571} 906}
572 907
573sub size_allocate { 908sub size_allocate {
574 my ($self, $w, $h) = @_; 909 my ($self, $x, $y, $w, $h) = @_;
575 910
576 $self->w ($w); 911 $self->_size_allocate ($x, $y, $w, $h);
577 $self->h ($h);
578 912
579 my $exp; 913 my $children = $self->{children};
580 my @oth; 914
581 # find expand widget 915 my @h = map +($_->size_request)[1], @$children;
582 for (@{$self->{children}}) { 916
583 if ($_->{expand}) { 917 my $req_h = List::Util::sum @h;
918
919 if ($req_h > $h) {
920 # ah well, not enough space
921 $_ *= $h / $req_h for @h;
922 } else {
923 my $exp = List::Util::sum map $_->{expand}, @$children;
584 $exp = $_; 924 $exp ||= 1;
585 last; 925
926 for (0 .. $#$children) {
927 my $child = $children->[$_];
928
929 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
586 } 930 }
587 push @oth, $_;
588 } 931 }
589 932
590 my ($ow, $oh); 933 CFClient::UI::harmonize \@h;
591
592 # get sizes of other widgets
593 for (@oth) {
594 my ($w, $h) = $_->size_request;
595 $oh += $h;
596 if ($ow < $w) { $ow = $w }
597 }
598 934
599 my $y = 0; 935 my $y = 0;
600 for (@{$self->{children}}) { 936 for (0 .. $#$children) {
601 $_->move (0, $y); 937 my $child = $children->[$_];
602 938 my $h = $h[$_];
603 if ($_ == $exp) {
604 $_->size_allocate ($w, $h - $oh);
605 $y += $h - $oh;
606 } else {
607 my ($cw, $h) = $_->size_request;
608 $_->size_allocate ($w, $h); 939 $child->size_allocate (0, $y, $w, $h);
940
609 $y += $h; 941 $y += $h;
610 }
611 } 942 }
612} 943}
613 944
614############################################################################# 945#############################################################################
615 946
616package Crossfire::Client::Widget::Label; 947package CFClient::UI::Label;
617 948
618our @ISA = Crossfire::Client::Widget::; 949our @ISA = CFClient::UI::Base::;
619 950
620use SDL::OpenGL; 951use SDL::OpenGL;
621 952
622sub new { 953sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 954 my ($class, %arg) = @_;
624 955
625 # TODO: color, and make height, xyz etc. optional 956 my $self = $class->SUPER::new (
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 957 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE,
959 text => "",
960 align => -1,
961 valign => -1,
962 padding => 2,
963 layout => new CFClient::Layout,
964 %arg
965 );
627 966
628 $self->set_text ($text); 967 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
968
969 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
629 971
630 $self 972 $self
973}
974
975sub escape_text {
976 local $_ = $_[1];
977
978 s/&/&amp;/g;
979 s/>/&gt;/g;
980 s/</&lt;/g;
981
982 $_[1]
631} 983}
632 984
633sub set_text { 985sub set_text {
634 my ($self, $text) = @_; 986 my ($self, $text) = @_;
635 987
988 $self->{layout}->set_text ($text);
989
990 delete $self->{texture};
991 $self->update;
992}
993
994sub set_markup {
995 my ($self, $markup) = @_;
996
997 $self->{layout}->set_markup ($markup);
998
999 delete $self->{texture};
1000 $self->update;
1001}
1002
1003sub size_request {
1004 my ($self) = @_;
1005
1006 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize});
1008
1009 my ($w, $h) = $self->{layout}->size;
1010
1011 (
1012 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2,
1014 )
1015}
1016
1017sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021
1022 delete $self->{texture};
1023}
1024
1025sub update {
1026 my ($self) = @_;
1027
1028 delete $self->{texture};
1029 $self->SUPER::update;
1030}
1031
1032sub _draw {
1033 my ($self) = @_;
1034
1035 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize});
1038 new_from_layout CFClient::Texture $self->{layout}
1039 };
1040
1041 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1043 glEnable GL_TEXTURE_2D;
1044 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1045
1046 glColor @{$self->{fg}};
1047
1048 $self->{ox} = int (
1049 $self->{align} < 0 ? $self->{padding}
1050 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1051 : ($self->{w} - $tex->{w}) * 0.5
1052 );
1053
1054 $self->{oy} = int (
1055 $self->{valign} < 0 ? $self->{padding}
1056 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1057 : ($self->{h} - $tex->{h}) * 0.5
1058 );
1059
1060 $tex->draw_quad ($self->{ox}, $self->{oy});
1061
1062 glDisable GL_TEXTURE_2D;
1063 glDisable GL_BLEND;
1064}
1065
1066#############################################################################
1067
1068package CFClient::UI::EntryBase;
1069
1070our @ISA = CFClient::UI::Label::;
1071
1072use SDL;
1073use SDL::OpenGL;
1074
1075sub new {
1076 my $class = shift;
1077
1078 $class->SUPER::new (
1079 fg => [1, 1, 1],
1080 bg => [0, 0, 0, 0.2],
1081 active_bg => [1, 1, 1, 0.5],
1082 active_fg => [0, 0, 0],
1083 can_hover => 1,
1084 can_focus => 1,
1085 valign => 0,
1086 @_
1087 )
1088}
1089
1090sub _set_text {
1091 my ($self, $text) = @_;
1092
1093 delete $self->{cur_h};
1094
1095 return if $self->{text} eq $text;
1096
1097 $self->{last_activity} = $::NOW;
636 $self->{text} = $text; 1098 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
638 1099
1100 $text =~ s/./*/g if $self->{hidden};
1101 $self->{layout}->set_text ("$text ");
1102
1103 $self->emit (changed => $self->{text});
1104}
1105
1106sub get_text {
1107 $_[0]{text}
1108}
1109
1110sub size_request {
1111 my ($self) = @_;
1112
1113 my ($w, $h) = $self->SUPER::size_request;
1114
1115 ($w + 1, $h) # add 1 for cursor
1116}
1117
1118sub size_allocate {
1119 my ($self, $x, $y, $w, $h) = @_;
1120
1121 $self->SUPER::size_allocate ($x, $y, $w, $h);
1122
1123 $self->_set_text ($self->{text});
1124}
1125
1126sub set_text {
1127 my ($self, $text) = @_;
1128
1129 $self->{cursor} = length $text;
1130 $self->_set_text ($text);
639 $self->update; 1131 $self->update;
640} 1132}
641 1133
642sub get_text { 1134sub key_down {
643 my ($self, $text) = @_; 1135 my ($self, $ev) = @_;
644 1136
645 $self->{text} 1137 my $mod = $ev->key_mod;
1138 my $sym = $ev->key_sym;
1139
1140 my $uni = $ev->key_unicode;
1141
1142 my $text = $self->get_text;
1143
1144 if ($sym == SDLK_BACKSPACE) {
1145 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1146 } elsif ($sym == SDLK_DELETE) {
1147 substr $text, $self->{cursor}, 1, "";
1148 } elsif ($sym == SDLK_LEFT) {
1149 --$self->{cursor} if $self->{cursor};
1150 } elsif ($sym == SDLK_RIGHT) {
1151 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1152 } elsif ($sym == SDLK_HOME) {
1153 $self->{cursor} = 0;
1154 } elsif ($sym == SDLK_END) {
1155 $self->{cursor} = length $text;
1156 } elsif ($sym == SDLK_ESCAPE) {
1157 $self->emit ('escape');
1158 } elsif ($uni) {
1159 substr $text, $self->{cursor}++, 0, chr $uni;
1160 }
1161
1162 $self->_set_text ($text);
1163 $self->update;
1164}
1165
1166sub focus_in {
1167 my ($self) = @_;
1168
1169 $self->{last_activity} = $::NOW;
1170
1171 $self->SUPER::focus_in;
1172}
1173
1174sub button_down {
1175 my ($self, $ev, $x, $y) = @_;
1176
1177 $self->SUPER::button_down ($ev, $x, $y);
1178
1179 my $idx = $self->{layout}->xy_to_index ($x, $y);
1180
1181 # byte-index to char-index
1182 my $text = $self->{text};
1183 utf8::encode $text;
1184 $self->{cursor} = length substr $text, 0, $idx;
1185
1186 $self->_set_text ($self->{text});
1187 $self->update;
1188}
1189
1190sub mouse_motion {
1191 my ($self, $ev, $x, $y) = @_;
1192# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1193}
1194
1195sub _draw {
1196 my ($self) = @_;
1197
1198 local $self->{fg} = $self->{fg};
1199
1200 if ($FOCUS == $self) {
1201 glColor @{$self->{active_bg}};
1202 $self->{fg} = $self->{active_fg};
1203 } else {
1204 glColor @{$self->{bg}};
1205 }
1206
1207 glEnable GL_BLEND;
1208 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1209 glBegin GL_QUADS;
1210 glVertex 0 , 0;
1211 glVertex 0 , $self->{h};
1212 glVertex $self->{w}, $self->{h};
1213 glVertex $self->{w}, 0;
1214 glEnd;
1215 glDisable GL_BLEND;
1216
1217 $self->SUPER::_draw;
1218
1219 #TODO: force update every cursor change :(
1220 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1221
1222 unless (exists $self->{cur_h}) {
1223 my $text = substr $self->{text}, 0, $self->{cursor};
1224 utf8::encode $text;
1225
1226 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1227 }
1228
1229 glColor @{$self->{fg}};
1230 glBegin GL_LINES;
1231 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1232 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1233 glEnd;
1234 }
1235}
1236
1237package CFClient::UI::Entry;
1238
1239our @ISA = CFClient::UI::EntryBase::;
1240
1241use SDL;
1242use SDL::OpenGL;
1243
1244sub key_down {
1245 my ($self, $ev) = @_;
1246
1247 my $sym = $ev->key_sym;
1248
1249 if ($sym == SDLK_RETURN) {
1250 $self->emit (activate => $self->get_text);
1251 $self->update;
1252
1253 } else {
1254 $self->SUPER::key_down ($ev);
1255 }
1256
1257}
1258
1259#############################################################################
1260
1261package CFClient::UI::Button;
1262
1263our @ISA = CFClient::UI::Label::;
1264
1265use SDL;
1266use SDL::OpenGL;
1267
1268my @tex =
1269 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1270 qw(b1_button_active.png);
1271
1272sub new {
1273 my $class = shift;
1274
1275 $class->SUPER::new (
1276 padding => 4,
1277 fg => [1, 1, 1],
1278 bg => [1, 1, 1, 0.2],
1279 active_fg => [0, 0, 1],
1280 can_hover => 1,
1281 @_
1282 )
1283}
1284
1285sub button_up {
1286 my ($self, $ev, $x, $y) = @_;
1287
1288 if ($x >= 0 && $x < $self->{w}
1289 && $y >= 0 && $y < $self->{h}) {
1290 $self->emit ("activate");
1291 }
1292}
1293
1294sub _draw {
1295 my ($self) = @_;
1296
1297 local $self->{fg} = $self->{fg};
1298
1299 if ($GRAB == $self) {
1300 $self->{fg} = $self->{active_fg};
1301 }
1302
1303 glEnable GL_BLEND;
1304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1305 glEnable GL_TEXTURE_2D;
1306 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1307 glColor 0, 0, 0, 1;
1308
1309 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1310
1311 glDisable GL_TEXTURE_2D;
1312 glDisable GL_BLEND;
1313
1314 $self->SUPER::_draw;
1315}
1316
1317#############################################################################
1318
1319package CFClient::UI::CheckBox;
1320
1321our @ISA = CFClient::UI::DrawBG::;
1322
1323my @tex =
1324 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1325 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1326
1327use SDL;
1328use SDL::OpenGL;
1329
1330sub new {
1331 my $class = shift;
1332
1333 $class->SUPER::new (
1334 padding => 2,
1335 fg => [1, 1, 1],
1336 active_fg => [1, 1, 0],
1337 state => 0,
1338 can_hover => 1,
1339 @_
1340 )
646} 1341}
647 1342
648sub size_request { 1343sub size_request {
649 my ($self) = @_; 1344 my ($self) = @_;
650 1345
1346 ($self->{padding} * 2 + 6) x 2
1347}
1348
1349sub size_allocate {
1350 my ($self, $x, $y, $w, $h) = @_;
1351
1352 $self->_size_allocate ($x, $y, $w, $h);
1353}
1354
1355sub button_down {
1356 my ($self, $ev, $x, $y) = @_;
1357
1358 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1359 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1360 $self->{state} = !$self->{state};
1361 $self->emit (changed => $self->{state});
651 ( 1362 }
652 $self->{texture}{width},
653 $self->{texture}{height},
654 )
655} 1363}
656 1364
657sub _draw { 1365sub _draw {
658 my ($self) = @_; 1366 my ($self) = @_;
659 1367
660 my $tex = $self->{texture}; 1368 $self->SUPER::_draw;
1369
1370 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1371
1372 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1373
1374 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
661 1375
662 glEnable GL_BLEND; 1376 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 1377 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1378 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1379
1380 my $tex = $self->{state} ? $tex[1] : $tex[0];
1381
1382 $tex->draw_quad (0, 0, $s, $s);
1383
1384 glDisable GL_TEXTURE_2D;
1385 glDisable GL_BLEND;
1386}
1387
1388#############################################################################
1389
1390package CFClient::UI::VGauge;
1391
1392our @ISA = CFClient::UI::Base::;
1393
1394use SDL::OpenGL;
1395
1396my %tex = (
1397 food => [
1398 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1399 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1400 ],
1401 grace => [
1402 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1403 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1404 ],
1405 hp => [
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1407 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1408 ],
1409 mana => [
1410 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1411 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1412 ],
1413);
1414
1415# eg. VGauge->new (gauge => 'food'), default gauge: food
1416sub new {
1417 my $class = shift;
1418
1419 my $self = $class->SUPER::new (gauge => 'food', @_);
1420
1421 $self
1422}
1423
1424sub size_request {
1425 my ($self) = @_;
1426
1427 ($self->{w}, $self->{h})
1428}
1429
1430sub set_max {
1431 my ($self, $max) = @_;
1432 $self->{max_val} = $max;
1433}
1434
1435sub set_value {
1436 my ($self, $val, $max) = @_;
1437
1438 $self->set_max ($max)
1439 if defined $max;
1440
1441 $max = $self->{max_val};
1442 $self->{val} = $val;
1443
1444 $self->update;
1445}
1446
1447sub _draw {
1448 my ($self) = @_;
1449
1450 my $tex = $tex{$self->{gauge}};
1451
1452 my ($w, $h) = ($self->{w}, $self->{h});
1453
1454 my $ycut = $self->{val} / ($self->{max_val} || 1);
1455 $ycut = 1 if $self->{val} > $self->{max_val};
1456
1457 my $t1 = $tex->[0];
1458 my $t2 = $tex->[1];
1459
1460 glEnable GL_BLEND;
1461 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1462 glEnable GL_TEXTURE_2D;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1463 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1464
1465 my $h1 = $self->{h} - $ycut * $self->{h};
1466 my $h2 = $ycut * $self->{h};
1467
1468 my $yp = 0;
1469
666 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1470 glBindTexture (GL_TEXTURE_2D, $t1->{name});
667
668 glColor 1, 0, 0, 1; # TODO color
669
670 glBegin GL_QUADS; 1471 glBegin (GL_QUADS);
671 glTexCoord 0, 0; glVertex 0 , 0; 1472 glTexCoord (0, 0); glVertex (0 , $yp);
672 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 1473 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
673 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 1474 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
674 glTexCoord 1, 0; glVertex $tex->{width}, 0; 1475 glTexCoord (1, 0); glVertex (0 + $w, $yp);
675 glEnd; 1476 glEnd ();
1477
1478 $yp += $h1;
1479
1480 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1481 glBegin (GL_QUADS);
1482 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1483 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1484 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1485 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1486 glEnd ();
676 1487
677 glDisable GL_BLEND; 1488 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 1489 glDisable GL_TEXTURE_2D;
679} 1490}
680 1491
681############################################################################# 1492#############################################################################
682 1493
683package Crossfire::Client::Widget::TextEntry; 1494package CFClient::UI::Slider;
684 1495
685our @ISA = Crossfire::Client::Widget::Label::; 1496use strict;
1497
1498use SDL::OpenGL;
1499
1500our @ISA = CFClient::UI::DrawBG::;
1501
1502my @tex =
1503 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1504 qw(s1_slider.png s1_slider_bg.png);
1505
1506sub new {
1507 my $class = shift;
1508
1509 # range [value, low, high, page]
1510
1511 # TODO: 0-width page
1512 # TODO: req_w/h are wrong with vertical
1513 # TODO: calculations are off
1514 my $self = $class->SUPER::new (
1515 fg => [1, 1, 1],
1516 active_fg => [0, 0, 0],
1517 range => [0, 0, 100, 10],
1518 req_w => 40,
1519 req_h => 13,
1520 vertical => 0,
1521 can_hover => 1,
1522 inner_pad => 5,
1523 @_
1524 );
1525
1526 $self
1527}
1528
1529sub size_request {
1530 my ($self) = @_;
1531
1532 my $w = $self->{req_w};
1533 my $h = $self->{req_h};
1534
1535 $self->{vertical} ? ($h, $w) : ($w, $h)
1536}
1537
1538sub button_down {
1539 my ($self, $ev, $x, $y) = @_;
1540
1541 $self->SUPER::button_down ($ev, $x, $y);
1542 $self->mouse_motion ($ev, $x, $y);
1543}
1544
1545sub mouse_motion {
1546 my ($self, $ev, $x, $y) = @_;
1547
1548 if ($GRAB == $self) {
1549 my ($value, $lo, $hi, $page) = @{$self->{range}};
1550
1551 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1552
1553 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1554 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1555
1556 $x -= $inner_pad_px; # substract the padding
1557 $x = $x * ($hi - $lo) / $inner_w + $lo;
1558 $x = $lo if $x < $lo;
1559 $x = $hi - $page if $x > $hi - $page;
1560 $self->{range}[0] = $x;
1561
1562 $self->emit (changed => $x);
1563 $self->update;
1564 }
1565}
1566
1567# the inner_* stuff is for generating a padding for the slider handle,
1568# so that the handle doesn't leave the texture. This calculation isn't 100%
1569# correct propably, but it does the job for now
1570sub _calc_inner_pad_px {
1571 my ($self, $w) = @_;
1572 ($w / 100) * $self->{inner_pad} # % to pixels
1573}
1574
1575sub _draw {
1576 my ($self) = @_;
1577
1578 $self->SUPER::_draw ();
1579
1580 my ($w, $h) = @$self{qw(w h)};
1581
1582 if ($self->{vertical}) {
1583 # draw a vertical slider like a rotated horizontal slider
1584
1585 glRotate 90, 0, 0, 1;
1586 glTranslate 0, -$self->{w}, 0;
1587
1588 ($w, $h) = ($h, $w);
1589 }
1590
1591 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1592 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1593
1594 my ($value, $lo, $hi, $page) = @{$self->{range}};
1595
1596 $hi = $value + 1 if $lo == $hi;
1597
1598 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1599 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1600
1601 $page = int $page * $inner_w / ($hi - $lo);
1602 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1603
1604 $w -= $page;
1605 $page &= ~1;
1606 glTranslate $page * 0.5, 0, 0;
1607 $page ||= 2;
1608
1609 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1610 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1611
1612 glEnable GL_BLEND;
1613 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1614 glEnable GL_TEXTURE_2D;
1615 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1616
1617 # draw background
1618 $tex[1]->draw_quad (0, 0, $w, $h);
1619
1620 # draw handle
1621 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1622
1623 glDisable GL_BLEND;
1624 glDisable GL_TEXTURE_2D;
1625}
1626
1627#############################################################################
1628
1629package CFClient::UI::TextView;
1630
1631our @ISA = CFClient::UI::HBox::;
1632
1633use SDL::OpenGL;
1634
1635sub new {
1636 my $class = shift;
1637
1638 my $self = $class->SUPER::new (
1639 req_w => $::WIDTH / 6,
1640 req_h => $::HEIGHT / 6,
1641 fontsize => $::FONTSIZE,
1642 @_,
1643
1644 layout => (new CFClient::Layout),
1645 par => [],
1646 height => 0,
1647 children => [
1648 (new CFClient::UI::Empty expand => 1),
1649 (new CFClient::UI::Slider vertical => 1),
1650 ],
1651 );
1652
1653 $self->{children}[1]->connect (changed => sub {
1654 $self->update;
1655 });
1656
1657 $self
1658}
1659
1660sub set_fontsize {
1661 my ($self, $fontsize) = @_;
1662
1663 $self->{fontsize} = $fontsize;
1664 $self->reflow;
1665}
1666
1667sub text_height {
1668 my ($self, $text) = @_;
1669
1670 my $layout = $self->{layout};
1671
1672 $layout->set_height ($self->{fontsize});
1673 $layout->set_width ($self->{w});
1674 $layout->set_text ($text);
1675
1676 ($layout->size)[1]
1677}
1678
1679sub reflow {
1680 my ($self) = @_;
1681
1682 $self->{need_reflow}++;
1683 $self->update;
1684}
1685
1686sub size_request {
1687 my ($self) = @_;
1688
1689 ($self->{req_w}, $self->{req_h})
1690}
1691
1692sub size_allocate {
1693 my ($self, $x, $y, $w, $h) = @_;
1694
1695 $self->SUPER::size_allocate ($x, $y, $w, $h);
1696
1697 $self->{layout}->set_height ($self->{fontsize});
1698 $self->{layout}->set_width ($self->{w});
1699
1700 $self->reflow;
1701}
1702
1703sub add_paragraph {
1704 my ($self, $color, $text) = @_;
1705
1706 #TODO: intelligently "reformat" paragraph
1707
1708 my $height = $self->text_height ($text);
1709
1710 $self->{height} += $height;
1711
1712 push @{$self->{par}}, [$height, $color, $text];
1713
1714 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1715 $self->{children}[1]->update;
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->SUPER::update;
1722
1723 return unless $self->{h} > 0;
1724
1725 delete $self->{texture};
1726
1727 $ROOT->on_refresh ($self, sub {
1728 if (delete $self->{need_reflow}) {
1729 my $height = 0;
1730
1731 $height += $_->[0] = $self->text_height ($_->[2])
1732 for @{$self->{par}};
1733
1734 $self->{height} = $height;
1735
1736 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1737
1738 delete $self->{texture};
1739 }
1740
1741 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1742 glClearColor 0, 0, 0, 1;
1743 glClear GL_COLOR_BUFFER_BIT;
1744
1745 glEnable GL_BLEND;
1746 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1747 glEnable GL_TEXTURE_2D;
1748 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1749
1750 my $top = int $self->{children}[1]{range}[0];
1751
1752 my $y0 = $top;
1753 my $y1 = $top + $self->{h};
1754
1755 my $y = 0;
1756
1757 my $layout = $self->{layout};
1758
1759 for my $par (@{$self->{par}}) {
1760 my $h = $par->[0];
1761
1762 if ($y0 < $y + $h && $y < $y1) {
1763 $layout->set_text ($par->[2]);
1764
1765 glColor @{ $par->[1] };
1766 my ($W, $H) = $layout->size;
1767 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1768 }
1769
1770 $y += $h;
1771 }
1772
1773 glDisable GL_TEXTURE_2D;
1774 glDisable GL_BLEND;
1775 };
1776 });
1777}
1778
1779sub _draw {
1780 my ($self) = @_;
1781
1782 if ($self->{texture}) {
1783 glEnable GL_TEXTURE_2D;
1784 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1785 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1786 glDisable GL_TEXTURE_2D;
1787 }
1788
1789 $self->{children}[1]->draw;
1790
1791}
1792
1793#############################################################################
1794
1795package CFClient::UI::MapWidget;
1796
1797use strict;
1798
1799use List::Util qw(min max);
686 1800
687use SDL; 1801use SDL;
688use SDL::OpenGL; 1802use SDL::OpenGL;
689 1803
690sub key_down { 1804our @ISA = CFClient::UI::Base::;
691 my ($self, $ev) = @_;
692 1805
693 my $mod = $ev->key_mod; 1806sub new {
694 my $sym = $ev->key_sym; 1807 my $class = shift;
695 1808
696 $ev->set_unicode (1); 1809 $class->SUPER::new (
697 my $uni = $ev->key_unicode; 1810 z => -1,
698 1811 can_focus => 1,
699 my $text = $self->get_text; 1812 list => (glGenLists 1),
700 1813 @_
701 if ($sym == SDLK_BACKSPACE) {
702 substr $text, -1, 1, '';
703
704 } elsif ($uni) {
705 $text .= chr $uni;
706 } 1814 )
707 $self->set_text ($text);
708} 1815}
709
710#############################################################################
711
712package Crossfire::Client::Widget::MapWidget;
713
714use strict;
715
716use List::Util qw(min max);
717
718use SDL;
719use SDL::OpenGL;
720use SDL::OpenGL::Constants;
721
722our @ISA = Crossfire::Client::Widget::;
723 1816
724sub key_down { 1817sub key_down {
725 print "MAPKEYDOWN\n"; 1818 print "MAPKEYDOWN\n";
726} 1819}
727 1820
728sub key_up { 1821sub key_up {
729} 1822}
730 1823
1824sub button_down {
1825 my ($self, $ev, $x, $y) = @_;
1826
1827 $self->focus_in;
1828
1829 if ($ev->button == 2) {
1830 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1831 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1832
1833 $self->{motion} = sub {
1834 my ($ev, $x, $y) = @_;
1835
1836 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1837
1838 $::CFG->{map_shift_x} = $bw + $x - $ox;
1839 $::CFG->{map_shift_y} = $bh + $y - $oy;
1840
1841 $self->update;
1842 };
1843 }
1844}
1845
1846sub button_up {
1847 my ($self, $ev, $x, $y) = @_;
1848
1849 delete $self->{motion};
1850}
1851
1852sub mouse_motion {
1853 my ($self, $ev, $x, $y) = @_;
1854
1855 $self->{motion}->($ev, $x, $y) if $self->{motion};
1856}
1857
731sub size_request { 1858sub size_request {
732 1859 (
1860 1 + 32 * int $::WIDTH / 32,
1861 1 + 32 * int $::HEIGHT / 32,
1862 )
733} 1863}
734 1864
735sub size_allocate { 1865sub update {
736} 1866 my ($self) = @_;
737 1867
1868 $self->{need_update} = 1;
1869 $self->SUPER::update;
1870}
1871
738sub _draw { 1872sub draw {
739 my ($self) = @_; 1873 my ($self) = @_;
740 1874
741 my $mx = $::CONN->{mapx}; 1875 if (delete $self->{need_update}) {
742 my $my = $::CONN->{mapy}; 1876 glNewList $self->{list}, GL_COMPILE;
743 1877
744 my $map = $::CONN->{map}; 1878 if ($::MAP) {
745
746 my ($xofs, $yofs);
747
748 my $sw = 1 + int $::WIDTH / 32; 1879 my $sw = int $::WIDTH / 32;
749 my $sh = 1 + int $::HEIGHT / 32; 1880 my $sh = int $::HEIGHT / 32;
750 1881
751 if ($::CONN->{mapw} > $sw) { 1882 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
752 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1883 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
753 } else {
754 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
755 }
756 1884
757 if ($::CONN->{maph} > $sh) { 1885 glTranslate $sx0 - 32, $sy0 - 32, 0;
758 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
759 } else {
760 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
761 }
762 1886
1887 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1888
1889 if ($::CFG->{fow_enable}) {
1890 if ($::CFG->{fow_smooth}) { # smooth fog of war
1891 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1892 glConvolutionFilter2D
1893 GL_CONVOLUTION_2D,
1894 GL_ALPHA,
1895 3, 3,
1896 GL_ALPHA, GL_FLOAT,
1897 pack "f*",
1898 0.1, 0.1, 0.1,
1899 0.1, 0.2, 0.1,
1900 0.1, 0.1, 0.1,
1901 ;
1902 glEnable GL_CONVOLUTION_2D;
1903 }
1904
1905 $self->{fow_texture} = new CFClient::Texture
1906 w => $w,
1907 h => $h,
1908 data => $data,
1909 internalformat => GL_ALPHA,
1910 format => GL_ALPHA;
1911
1912 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1913
1914 glEnable GL_BLEND;
1915 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
763 glEnable GL_TEXTURE_2D; 1916 glEnable GL_TEXTURE_2D;
764 glEnable GL_BLEND;
765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1917 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
766 1918
767 my $sw4 = ($sw + 3) & ~3; 1919 glColor +($::CFG->{fow_intensity}) x 3, 1;
768 my $lighting = "\x00" x ($sw4 * $sh); 1920 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
769 1921
770 for my $x (0 .. $sw - 1) { 1922 glDisable GL_TEXTURE_2D;
771 for my $y (0 .. $sh - 1) { 1923 glDisable GL_BLEND;
772
773 my $cell = $map->[$x + $xofs][$y + $yofs]
774 or next;
775
776 my $darkness = $cell->[0] * (1 / 255);
777 if ($darkness < 0) {
778 $darkness = 0.15;
779 } 1924 }
780 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
781 1925
782 for my $num (grep $_, @$cell[1,2,3]) { 1926 # HACK BEGIN
783 my $tex = $::CONN->{face}[$num]{texture} || next; 1927 {
1928 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1929 my ($w, $h) = (250, 250);
784 1930
785 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1931 glEnable GL_BLEND;
1932 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1933 glEnable GL_TEXTURE_2D;
1934 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
786 1935
787 my $w = $tex->{width}; 1936 $self->{mapmap_texture} =
788 my $h = $tex->{height}; 1937 new CFClient::Texture
1938 w => $w,
1939 h => $h,
1940 data => $::MAP->mapmap ($w, $h),
1941 type => GL_UNSIGNED_INT_8_8_8_8_REV;
789 1942
790 my $px = ($x + 1) * 32 - $w; 1943 $self->{mapmap_texture}->draw_quad (100, 100);
791 my $py = ($y + 1) * 32 - $h;
792 1944
793 glBegin GL_QUADS; 1945 glDisable GL_TEXTURE_2D;
794 glTexCoord 0, 0; glVertex $px , $py; 1946 glDisable GL_BLEND;
795 glTexCoord 0, 1; glVertex $px , $py + $h;
796 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
797 glTexCoord 1, 0; glVertex $px + $w, $py;
798 glEnd;
799 } 1947 }
1948 # HACK END
800 } 1949 }
801 }
802 1950
803# if (1) { # higher quality darkness 1951 glEndList;
804# $lighting =~ s/(.)/$1$1$1/gs;
805# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
806#
807# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
808#
809# $lighting = $pb->get_pixels;
810# $lighting =~ s/(.)../$1/gs;
811# } 1952 }
812 1953
813 $lighting = new Crossfire::Client::Texture 1954 glPushMatrix;
814 width => $sw4, 1955 glCallList $self->{list};
815 height => $sh, 1956 glPopMatrix;
816 data => $lighting,
817 internalformat => GL_ALPHA4,
818 format => GL_ALPHA;
819 1957
820 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1958 if ($FOCUS != $self) {
821 glColor 0, 0, 0, 0.75; 1959 glColor 64/255, 64/255, 64/255;
822 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1960 glLogicOp GL_AND;
823 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 1961 glEnable GL_COLOR_LOGIC_OP;
824 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
825 glBegin GL_QUADS; 1962 glBegin GL_QUADS;
826 glTexCoord 0, 0; glVertex 0 , 0; 1963 glVertex 0, 0;
827 glTexCoord 0, 1; glVertex 0 , $sh * 32; 1964 glVertex 0, $::HEIGHT;
828 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; 1965 glVertex $::WIDTH, $::HEIGHT;
829 glTexCoord 1, 0; glVertex $sw4 * 32, 0; 1966 glVertex $::WIDTH, 0;
830 glEnd; 1967 glEnd;
831 1968 glDisable GL_COLOR_LOGIC_OP;
832 glDisable GL_TEXTURE_2D; 1969 }
833 glDisable GL_BLEND;
834} 1970}
835 1971
836my %DIR = ( 1972my %DIR = (
837 SDLK_KP8, [1, "north"], 1973 SDLK_KP8, [1, "north"],
838 SDLK_KP9, [2, "northeast"], 1974 SDLK_KP9, [2, "northeast"],
854 1990
855 my $mod = $ev->key_mod; 1991 my $mod = $ev->key_mod;
856 my $sym = $ev->key_sym; 1992 my $sym = $ev->key_sym;
857 1993
858 if ($sym == SDLK_KP5) { 1994 if ($sym == SDLK_KP5) {
859 $::CONN->send ("command stay fire"); 1995 $::CONN->user_send ("stay fire");
1996 } elsif ($sym == SDLK_a) {
1997 $::CONN->user_send ("apply");
1998 } elsif ($sym == SDLK_QUOTE) {
1999 $self->emit ('activate_console');
2000 } elsif ($sym == SDLK_SLASH) {
2001 $self->emit ('activate_console' => '/');
860 } elsif (exists $DIR{$sym}) { 2002 } elsif (exists $DIR{$sym}) {
861 if ($mod & KMOD_SHIFT) { 2003 if ($mod & KMOD_SHIFT) {
862 $self->{shft}++; 2004 $self->{shft}++;
863 $::CONN->send ("command fire $DIR{$sym}[0]"); 2005 $::CONN->user_send ("fire $DIR{$sym}[0]");
864 } elsif ($mod & KMOD_CTRL) { 2006 } elsif ($mod & KMOD_CTRL) {
865 $self->{ctrl}++; 2007 $self->{ctrl}++;
866 $::CONN->send ("command run $DIR{$sym}[0]"); 2008 $::CONN->user_send ("run $DIR{$sym}[0]");
867 } else { 2009 } else {
868 $::CONN->send ("command $DIR{$sym}[1]"); 2010 $::CONN->user_send ("$DIR{$sym}[1]");
869 } 2011 }
870 } 2012 }
871} 2013}
872 2014
873sub key_up { 2015sub key_up {
875 2017
876 my $mod = $ev->key_mod; 2018 my $mod = $ev->key_mod;
877 my $sym = $ev->key_sym; 2019 my $sym = $ev->key_sym;
878 2020
879 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 2021 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
880 $::CONN->send ("command fire_stop"); 2022 $::CONN->user_send ("fire_stop");
881 } 2023 }
882 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 2024 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
883 $::CONN->send ("command run_stop"); 2025 $::CONN->user_send ("run_stop");
884 } 2026 }
885} 2027}
886 2028
887############################################################################# 2029#############################################################################
888 2030
889package Crossfire::Client::Widget::Animator; 2031package CFClient::UI::Animator;
890 2032
891use SDL::OpenGL; 2033use SDL::OpenGL;
892 2034
893our @ISA = Crossfire::Client::Widget::Bin::; 2035our @ISA = CFClient::UI::Bin::;
894 2036
895sub moveto { 2037sub moveto {
896 my ($self, $x, $y) = @_; 2038 my ($self, $x, $y) = @_;
897 2039
898 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 2040 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
899 $self->{speed} = 0.2; 2041 $self->{speed} = 0.001;
900 $self->{time} = 1; 2042 $self->{time} = 1;
901 2043
902 ::animation_start $self; 2044 ::animation_start $self;
903} 2045}
904 2046
919 2061
920sub _draw { 2062sub _draw {
921 my ($self) = @_; 2063 my ($self) = @_;
922 2064
923 glPushMatrix; 2065 glPushMatrix;
924 glRotate $self->{time} * 10000, 0, 1, 0; 2066 glRotate $self->{time} * 1000, 0, 1, 0;
925 $self->{children}[0]->draw; 2067 $self->{children}[0]->draw;
926 glPopMatrix; 2068 glPopMatrix;
927} 2069}
928 2070
9291; 2071#############################################################################
930 2072
2073package CFClient::UI::Flopper;
2074
2075our @ISA = CFClient::UI::Button::;
2076
2077sub new {
2078 my $class = shift;
2079
2080 my $self = $class->SUPER::new (
2081 state => 0,
2082 connect_activate => \&toggle_flopper,
2083 @_
2084 );
2085
2086 if ($self->{state}) {
2087 $self->{state} = 0;
2088 $self->toggle_flopper;
2089 }
2090
2091 $self
2092}
2093
2094sub toggle_flopper {
2095 my ($self) = @_;
2096
2097 # TODO: use animation
2098 if ($self->{state} = !$self->{state}) {
2099 $CFClient::UI::ROOT->add ($self->{other});
2100 $self->{other}->move ($self->coord2global (0, $self->{h}));
2101 $self->emit ("open");
2102 } else {
2103 $CFClient::UI::ROOT->remove ($self->{other});
2104 $self->emit ("close");
2105 }
2106
2107 $self->emit (changed => $self->{state});
2108}
2109
2110#############################################################################
2111
2112package CFClient::UI::Root;
2113
2114our @ISA = CFClient::UI::Container::;
2115
2116use SDL::OpenGL;
2117
2118sub size_request {
2119 ($::WIDTH, $::HEIGHT)
2120}
2121
2122sub size_allocate {
2123 my ($self, $x, $y, $w, $h) = @_;
2124
2125 $self->_size_allocate ($x, $y, $w, $h);
2126
2127 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2128 for @{$self->{children}};
2129}
2130
2131sub _topleft {
2132 my ($self, $x, $y) = @_;
2133
2134 ($x, $y)
2135}
2136
2137sub update {
2138 my ($self) = @_;
2139
2140 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
2141 ::refresh ();
2142}
2143
2144sub add {
2145 my ($self, $widget) = @_;
2146
2147 $self->SUPER::add ($widget);
2148
2149 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2150}
2151
2152sub on_refresh {
2153 my ($self, $id, $cb) = @_;
2154
2155 $self->{refresh_hook}{$id} = $cb;
2156}
2157
2158sub draw {
2159 my ($self) = @_;
2160
2161 while (my $rcb = delete $self->{refresh_hook}) {
2162 $_->() for values %$rcb;
2163 }
2164
2165 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2166 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2167 glClear GL_COLOR_BUFFER_BIT;
2168
2169 glMatrixMode GL_PROJECTION;
2170 glLoadIdentity;
2171 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2172 glMatrixMode GL_MODELVIEW;
2173 glLoadIdentity;
2174
2175 $self->_draw;
2176}
2177
2178#############################################################################
2179
2180package CFClient::UI;
2181
2182$ROOT = new CFClient::UI::Root;
2183
21841
2185

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines