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