blib/lib/Games/Axmud/Obj/Zone.pm untested

filter: Statementstmt 0.0% 0 / 481 Branchbran 0.0% 0 / 198 Conditioncond 0.0% 0 / 282 MC/DCmcdc 0.0% 0 / 151 Subroutinesub 0.0% 0 / 54 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 1217 CC 100 SCAR 47.6
CC
100
Cov
0%
adjustMultipleWin
506.0
adjustSingleWin
156.0
restackWin
156.0
CRAP 116.3
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::Zone
16 # Handles a single zone within a zonemap
17  
18 { package Games::Axmud::Obj::Zone;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Called by GA::Obj::WorkspaceGrid->resetZones
34 # Create a new instance of the zone object, which regulates a single area of the workspace
35 # grid in which windows are arranged
36 # (Having created the object and set its size and position instance variables, don't forget
37 # to call $self->resetInternalGrid to set up the zone's internal grid)
38 #
39 # Expected arguments
40 # $number - A unique number for the zone within the parent workspace grid
41 # object
42 # $workspaceGridObj - The parent GA::Obj::WorkspaceGrid object
43 #
44 # Return values
45 # 'undef' on improper arguments
46 # Blessed reference to the newly-created object on success
47  
48 my ($class, $number, $workspaceGridObj, $check) = @_;
49  
50 # Check for improper arguments
51 if (
52 ! defined $class || ! defined $number || ! defined $workspaceGridObj || defined $check
53 ) {
54 return $axmud::CLIENT->writeImproper($class . '->new', @_);
55 }
56  
57 # Setup
58 my $self = {
59 _objName => 'zone',
60 _objClass => $class,
61 _parentFile => undef, # No parent file object
62 _parentWorld => undef, # No parent file object
63 _privFlag => TRUE, # All IVs are private
64  
65 # IVs
66 # ---
67  
68 # The zone's number on the workspace grid (matches GA::Obj::WorkspaceGrid->zoneCount)
69 # - set later on
70 number => $number,
71 # The parent GA::Obj::WorkspaceGrid object
72 workspaceGridObj => $workspaceGridObj,
73  
74 # The zone's coordinates on the workspace grid
75 xPosBlocks => undef,
76 yPosBlocks => undef,
77 # The zone's width and height
78 widthBlocks => undef,
79 heightBlocks => undef,
80  
81 # If this flag is TRUE, the zone is reserved for certain window types
82 reservedFlag => FALSE,
83 # For reserved zones, a hash of window types allowed to use it. Hash in the form
84 # $reservedHash{window_name} = window_type
85 # ...where 'window_type' is one of the window types specified by
86 # GA::Client->constGridWinTypeHash, and 'window_name' is either the same as the
87 # window type, or something different that further limits the type of window allowed
88 # to use it:
89 # window_type window_name
90 # ----------- -----------
91 # main main
92 # map map
93 # protocol Any string chosen by the protocol code (default value is
94 # 'protocol')
95 # fixed Any string chosen by the controlling code (default value is
96 # 'fixed')
97 # custom Any string chosen by the controlling code. For task windows,
98 # the name of the task (e.g. 'status_task', for other windows,
99 # default value is 'custom'
100 # external The 'external' window's name (e.g. 'Notepad')
101 reservedHash => {},
102 # For reserved zones, it's often preferable to use only a single layer. When this flag
103 # is set to TRUE, only a single layer is used by the zone (the default one specified
104 # by GA::Obj::WorkspaceGrid->defaultLayer)
105 multipleLayerFlag => TRUE,
106  
107 # When GA::Client->shareMainWinFlag = FALSE, meaning every session has its own 'main'
108 # window, we probably want to prevent multiple sessions from sharing the same zone
109 # If this IV is defined, this zone is reserved for a single session. The IV's value can
110 # be any non-empty string. All zones with the same ->ownerString are reserved for a
111 # particular session
112 # The first session to place one of its windows into any 'owned' zone claims all of
113 # those zones for itself. If this IV is 'undef', the zone is available for any session
114 # to use (subject to the restrictions above)
115 # NB Even if ->ownerString is set, it is ignored when GA::Client->shareMainWinFlag
116 # = TRUE (meaning all sessions share a single 'main' window). This default behaviour
117 # guards against the user selecting a zonemap such as 'horizontal', which is designed
118 # for GA::Client->shareMainWinFlag = FALSE
119 ownerString => undef,
120 # Once this zone has been 'claimed', the GA::Session that claimed it
121 owner => undef,
122  
123 # The zone's internal grid in 3 dimensions. The internal grid is a sub-section of the
124 # parent workspace grid, using the same grid blocks, but the internal grid has its own
125 # coordinate system
126 # Layers are numbered from 0 to (GA::Obj::WorkspaceGrid->maxLayers - 1)
127 # The x axis is numbered from 0 to ($self->widthBlocks - 1)
128 # The y axis is numbered from 0 to ($self->heightBlocks - 1)
129 # Rather than using a 3D array, we use calls to $self->checkPosnInLayer to check that a
130 # region of the zone is not occupied by an area object (GA::Obj::Area), with each
131 # area object handling a single window in the zone. Each area object stores details
132 # about the portion of the internal grid used by its window
133 # Hash in the form
134 # $areaHash{number} = blessed_reference_to_area_object
135 areaHash => {},
136 # Number of area objects created for this internal grid since the last reset (used to
137 # give every area object a number unique to the internal grid)
138 areaCount => 0,
139 # Sometimes it's useful to set the total maximum number of areas allowed in the zone
140 # (especially for zones reserved for certain types of windows). If set to 0, there is
141 # no maximum
142 areaMax => 0,
143 # It's very often useful to set the maximum number of areas allowed in one layer of
144 # the zone, because this helps to fill a zone with windows easily. Unlike ->areaMax
145 # which is an absolute maximum, if any layer is full then new windows are just moved
146 # to another layer
147 visibleAreaMax => 0,
148  
149 # In which corner the zone should start placing windows ('top_left', 'top_right',
150 # 'bottom_left', 'bottom_right');
151 startCorner => 'top_left',
152 # In which direction the zone should place windows first ('horizontal' move horizontally
153 # after the first window, if possible, 'vertical' move vertically after the first
154 # window, if possible)
155 orientation => 'vertical',
156 # The default winmap names to use in this zone with 'main' windows and other 'internal'
157 # windows. If not specified, use corresponding IVs in GA::Client
158 defaultEnabledWinmap => undef,
159 defaultDisabledWinmap => undef,
160 defaultInternalWinmap => undef,
161  
162 # The sizes of windows within a zone normally depend on $self->areaMax; i.e. if
163 # ->areaMax is set to 2, each area takes up half the zone.
164 # If ->areaMax is set to 0 (meaning unlimited areas), the default area size is set
165 # by the following two IVs. They show the default size of an area in terms of the
166 # zone's internal grid
167 # If either or both of these IVs are set to 0, then the one or both of the default
168 # area sizes GA::Client->customGridWinWidth and ->customGridWinWidth (both
169 # measured in pixels) is used to set them, instead
170 defaultAreaWidth => 0,
171 defaultAreaHeight => 0,
172 # ->adjustMultipleWin can adjust the size of all the areas in a zone, in order to make
173 # a bigger gap (large enough to fit another area) or to close a smaller gap (too
174 # small to fit another area). If such an adjustment has been done, this flag is set
175 # to TRUE; it is set back to FALSE if areas are deleted, and there's enough space
176 # for an area of the default size (defined by $self->widthAdjustBlocks and
177 # ->heightAdjustBlocks)
178 areaAdjustFlag => FALSE,
179 # The size of the adjustment is set by these variables. A positive number means the
180 # default size has increased (e.g. +1 means 1 gridblock), a negative number means the
181 # default size has decreased. If both are set to 0, $self->areaAdjustFlag is also set
182 # back to FALSE
183 widthAdjustBlocks => 0,
184 heightAdjustBlocks => 0,
185 };
186  
187 # Bless the zone layout into existence
188 bless $self, $class;
189  
190 return $self;
191 }
192  
193 ##################
194 # Methods
195  
196 sub resetInternalGrid {
197  
198 # Resets the zone
199 #
200 # Expected arguments
201 # (none besides $self)
202 #
203 # Return values
204 # 'undef' on improper arguments
205 # 1 otherwise
206  
207 my ($self, $check) = @_;
208  
209 # Check for improper arguments
210 if (defined $check) {
211  
212 return $axmud::CLIENT->writeImproper($self->_objClass . '->resetInternalGrid', @_);
213 }
214  
215 # Reset IVs
216 $self->ivEmpty('areaHash');
217 $self->ivPoke('areaCount', 0);
218  
219 return 1;
220 }
221  
222 sub getInternalGridPosn {
223  
224 # Called by GA::Client->createGridWin, $self->findProvWinPosn or by any other function
225 # Finds the coordinates on the workspace, in pixels, of a position of a gridblock in this
226 # zone's internal grid
227 #
228 # Expected arguments
229 # $xPosBlocks, $yPosBlocks
230 # - The coordinates of one gridblock in the zone's internal grid
231 #
232 # Return values
233 # An empty list on improper arguments
234 # Otherwise returns the coordinates in a list in the form
235 # ($xPosPixels, $yPosPixels)
236  
237 my ($self, $xPosBlocks, $yPosBlocks, $check) = @_;
238  
239 # Local variables
240 my (
241 $xPosPixels, $yPosPixels,
242 @emptyList,
243 );
244  
245 # Check for improper arguments
246 if (! defined $xPosBlocks || ! defined $yPosBlocks || defined $check) {
247  
248 $axmud::CLIENT->writeImproper(
249 $self->_objClass . '->getInternalGridPosn',
250 @_,
251 );
252  
253 return @emptyList;
254 }
255  
256 # Find the coordinates of the specified gridblock
257 $xPosPixels = $self->workspaceGridObj->xPosPixels
258 + (($self->xPosBlocks + $xPosBlocks) * $axmud::CLIENT->gridBlockSize);
259 $yPosPixels = $self->workspaceGridObj->yPosPixels
260 + (($self->yPosBlocks + $yPosBlocks) * $axmud::CLIENT->gridBlockSize);
261  
262 return ($xPosPixels, $yPosPixels);
263 }
264  
265 sub checkWinAllowed {
266  
267 # Called by GA::Obj::Desktop->swapGridWin, GA::Obj::Workspace->createGridWin and
268 # ->chooseZone, GA::Obj::WorkspaceGrid->changeWinZone
269 # Checks whether a window of a certain type is allowed in this zone. (Some zones allow
270 # any kind of window; others are reserved for certain window types)
271 #
272 # Expected arguments
273 # $winType, $winName
274 # - The window type and name to check, matching the keys and corresponding
275 # values in $self->reservedHash:
276 #
277 # $winType $winName
278 # -------- --------
279 # main main
280 # map map
281 # protocol Any string chosen by the protocol code, e.g. for MXP (default value
282 # is 'protocol')
283 # fixed Any string chosen by the controlling code (default value is
284 # 'fixed')
285 # custom Any string chosen by the controlling code. For task windows,
286 # the name of the task (e.g. 'status_task', for other windows,
287 # default value is 'custom'
288 # external The external window's name (e.g. 'Notepad')
289 #
290 # Optional arguments
291 # $session - The GA::Session which controls this window, if any
292 #
293 # Return values
294 # 'undef' on improper arguments or if the window isn't allowed in this zone
295 # 1 if it is allowed
296  
297 my ($self, $winType, $winName, $session, $check) = @_;
298  
299 # Check for improper arguments
300 if (! defined $winType || ! defined $winName || defined $check) {
301  
302 return $axmud::CLIENT->writeImproper($self->_objClass . '->checkWinAllowed', @_);
303 }
304  
305 # Check invalid window type ('grid' windows allowed, but 'free' windows are not)
306 if (! $axmud::CLIENT->ivExists('constGridWinTypeHash', $winType)) {
307  
308 # Not allowed
309 return undef;
310 }
311  
312 # Check the zone owner, if it's set
313 if (
314 # (Zone owners ignored in GA::Client->shareMainWinFlag = TRUE)
315 ! $axmud::CLIENT->shareMainWinFlag
316 && $self->owner
317 && (! $session || $self->owner ne $session)
318 ) {
319 # This zone is claimed by a different session
320 return undef;
321 }
322  
323 # If this zone is reserved for certain windows...
324 if ($self->reservedFlag) {
325  
326 # Check reserved zones
327 if (
328 ! $self->ivExists('reservedHash', $winName)
329 || $self->ivShow('reservedHash', $winName) ne $winType
330 ) {
331 # Not allowed
332 return undef;
333 }
334 }
335  
336 # Window is allowed in this zone
337 return 1;
338 }
339  
340 sub findProvWinPosn {
341  
342 # Called by GA::Obj::Workspace->chooseWinPosn and GA::Obj::WorkspaceGrid->changeWinzone
343 # Before we decide where exactly in the zone to put a window, we give it a provisional
344 # position in one of the zone's corners (by default the top-left corner). If that area of
345 # the zone happens to be free, the window can be put there; otherwise
346 # GA::Obj::Workspace->createGridWin can move it to some other part of the zone
347 # However, if both $xPosPixels and $yPosPixels have been set, we use these values as the
348 # provisional position instead
349 # If the specified window width and height, $widthPixels and $yPixels, doesn't fit exactly
350 # into a gridblock, the whole of the gridblock is used (so the window's specified size
351 # will increase slightly)
352 #
353 # Expected arguments
354 # $widthPixels, $heightPixels
355 # - The size of the window in pixels (must not be undef)
356 #
357 # Optional arguments
358 # $xPosPixels, $yPosPixels
359 # - The windows coordinates on the workspace, if they have already been specified (one
360 # or both of these values can be 'undef')
361 #
362 # Return values
363 # An empty list on improper arguments, or if the specified coordinates are not in this
364 # zone
365 # Otherwise, returns a list in the format
366 # ($widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks)
367 # ...showing the provisional position of the window new position of the window on the
368 # zone's internal grid
369  
370 my ($self, $widthPixels, $heightPixels, $xPosPixels, $yPosPixels, $check) = @_;
371  
372 # Local variables
373 my (
374 $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $internalGridXPosPixels,
375 $internalGridYPosPixels, $blockSize,
376 @emptyList,
377 );
378  
379 # Check for improper arguments
380 if (! defined $widthPixels || ! defined $heightPixels || defined $check) {
381  
382 $axmud::CLIENT->writeImproper($self->_objClass . '->findProvWinPosn', @_);
383 return @emptyList;
384 }
385  
386 # Get the position of the internal grid on the workspace (actually, the position of the
387 # top-left gridblock)
388 ($internalGridXPosPixels, $internalGridYPosPixels) = $self->getInternalGridPosn(0, 0);
389  
390 # Set the window's size, in gridblocks; round up decimal values so that the whole of a
391 # partially-filled gridblock is used
392 $blockSize = $axmud::CLIENT->gridBlockSize;
393 # $widthBlocks = sprintf ("%.0f", ($widthPixels / $blockSize));
394 # $heightBlocks = sprintf ("%.0f", ($heightPixels / $blockSize));
395 $widthBlocks = Math::Round::nearest(1, ($widthPixels / $blockSize));
396 $heightBlocks = Math::Round::nearest(1, ($heightPixels / $blockSize));
397 # In case there were partially filled gridblocks, re-set the window's size in pixels
398 $widthPixels = $widthBlocks * $blockSize;
399 $heightPixels = $heightBlocks * $blockSize;
400 # If the window is too big for the zone, reduce its size; also ignore the specified
401 # coordinates so that the window is provisionally placed in the default corner
402 if ($widthBlocks > $self->widthBlocks) {
403  
404 $widthBlocks = $self->widthBlocks;
405 $xPosPixels = undef;
406 $yPosPixels = undef;
407 }
408  
409 if ($heightBlocks > $self->heightBlocks) {
410  
411 $heightBlocks = $self->heightBlocks;
412 $xPosPixels = undef;
413 $yPosPixels = undef;
414 }
415  
416 # If the window's position hasn't been specified (or is no longer specified), set values for
417 # the coordinates of the window, on the zone's internal grid, at its provisional position
418 if (! defined $xPosPixels || ! defined $yPosPixels) {
419  
420 if ($self->startCorner eq 'top_left') {
421  
422 $xPosBlocks = 0;
423 $yPosBlocks = 0;
424  
425 } elsif ($self->startCorner eq 'top_right') {
426  
427 $xPosBlocks = ($self->widthBlocks - $widthBlocks);
428 $yPosBlocks = 0;
429  
430 } elsif ($self->startCorner eq 'bottom_left') {
431  
432 $xPosBlocks = 0;
433 $yPosBlocks = ($self->heightBlocks - $heightBlocks);
434  
435 } elsif ($self->startCorner eq 'bottom_right') {
436  
437 $xPosBlocks = ($self->widthBlocks - $widthBlocks);
438 $yPosBlocks = ($self->heightBlocks - $heightBlocks);
439 }
440  
441 # If the window's position was specified, set values for the coordinates of the window on
442 # the zone's internal grid, as close as possible to the specified position (i.e. move it
443 # slightly up or slightly left if necessary so there are no partially-filled gridblocks)
444 } else {
445  
446 $xPosBlocks = int(
447 ($xPosPixels - $internalGridXPosPixels) / $blockSize
448 );
449 $yPosBlocks = int(
450 ($yPosPixels - $internalGridYPosPixels) / $blockSize
451 );
452 }
453  
454 return ($widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks);
455 }
456  
457 sub placeWin {
458  
459 # Called by GA::Obj::Workspace->chooseWinPosn, GA::Obj::WorkspaceGrid->changeWinzone and
460 # $self->replaceAreaSpace
461 # Places a window inside this zone at a position unoccupied by other windows
462 # A provisional position inside the zone (often at one of the corners), as well as the
463 # window's size, are passed to this function as arguments
464 # If that position is already occupied by another window, this function tries to find room
465 # for it somewhere else in the zone (maybe in a different layer)
466 #
467 # Expected arguments
468 # $layer
469 # - Which layer within the zone to check first (matches a number between 0 and
470 # (GA::Obj::WorkspaceGrid->maxLayers - 1) )
471 # $xPosBlocks, $yPosBlocks
472 # - The window's proposed x/y coordinates on the zone's internal grid (in gridblocks)
473 # $widthBlocks, $heightBlocks
474 # - The window's proposed width and height (in gridblocks)
475 #
476 # Optional arguments
477 # $winObj
478 # - Specified by GA::Obj::WorkspaceGrid->changeWinzone, when it wants to place a
479 # window in the same zone it currently occupies. $winObj is something that
480 # inherits from GA::Generic::Win. If defined, regard any space currently occupied
481 # by this window as available
482 #
483 # Return values
484 # An empty list on improper arguments, or if the window won't fit anywhere inside the zone
485 # Otherwise returns a list in the form
486 # (1, $layer, $xPosBlocks, $yPosBlocks)
487 # ...The '1' represents success; the next three variables are the layer and position in
488 # the zone's internal grid at which the window will fit without overlapping other
489 # windows
490  
491 my (
492 $self, $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $winObj, $check,
493 ) = @_;
494  
495 # Local variables
496 my (
497 $successFlag,
498 @layerList, @emptyList,
499 );
500  
501 # Check for improper arguments
502 if (
503 ! defined $layer || ! defined $xPosBlocks || ! defined $yPosBlocks
504 || ! defined $widthBlocks || ! defined $heightBlocks || defined $check
505 ) {
506 $axmud::CLIENT->writeImproper($self->_objClass . '->placeWin', @_);
507 return @emptyList,
508 }
509  
510 # Get a list of layers, starting with the specified one, then all layers below it, then all
511 # layers above it
512 @layerList = $self->workspaceGridObj->buildLayerList($layer);
513  
514 # Now consider moving the window within the zone's grid. First see if the window will fit
515 # inside this zone's internal grid at the specified position and layer, without
516 # overlapping other windows on the same layer
517 $successFlag = $self->checkPosnInLayer(
518 $layer,
519 $xPosBlocks,
520 $yPosBlocks,
521 $widthBlocks,
522 $heightBlocks,
523 $winObj,
524 );
525  
526 # If the window overlaps an existing window, see if it will fit in another position inside
527 # the grid, at any layer (check the specified layer first, then every layer in @layerList
528 # in turn)
529 if (! $successFlag) {
530  
531 OUTER: foreach my $successiveLayer (@layerList) {
532  
533 ($successFlag, $xPosBlocks, $yPosBlocks) = $self->findSpaceInLayer(
534 $successiveLayer,
535 $widthBlocks,
536 $heightBlocks,
537 $winObj,
538 );
539  
540 if ($successFlag) {
541  
542 # Use this layer, and don't do any more searching
543 $layer = $successiveLayer;
544 last OUTER;
545 }
546 }
547 }
548  
549 # If there's no room anywhere in the zone, on any layer, display a warning message and let
550 # the user decide what to do
551 if (! $successFlag) {
552  
553 return @emptyList;
554  
555 # Otherwise return the layer and position within the zone's internal grid at which the
556 # window will fit, without overlapping other windows
557 } else {
558  
559 return (1, $layer, $xPosBlocks, $yPosBlocks);
560 }
561 }
562  
563 sub checkPosnInLayer {
564  
565 # Called by $self->placeWin and $self->findSpaceInLayer to see if a window will fit in this
566 # zone, at the specified layer and position, without overlapping other windows in the same
567 # layer
568 # (Also called by $self->adjustSingleWin to check whether a small area near the edge of a
569 # zone - for the purposes of this function, the area is a proposed window - to see whether
570 # it's unoccupied)
571 #
572 # Expected arguments
573 # $layer
574 # - Which layer within the zone to check first (matches a number between 0 and
575 # (GA::Obj::WorkspaceGrid->maxLayers - 1) )
576 # $xPosBlocks, $yPosBlocks
577 # - The window's proposed x/y coordinates on the zone's internal grid (in gridblocks)
578 # $widthBlocks, $heightBlocks
579 # - The window's proposed width and height (in gridblocks)
580 #
581 # Optional arguments
582 # $winObj
583 # - Specified by GA::Obj::WorkspaceGrid->changeWinzone, when it wants to place a
584 # window in the same zone it currently occupies. $winObj is something that
585 # inherits from GA::Generic::Win. If defined, regard any space currently occupied
586 # by this window as available
587 #
588 # Return values
589 # 'undef' if the window doesn't fit at the specified position in the grid without
590 # overlapping other windows on the same layer
591 # 1 if it does
592  
593 my (
594 $self, $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $winObj, $check,
595 ) = @_;
596  
597 # Local variables
598 my ($x1, $x2, $y1, $y2);
599  
600 # Check for improper arguments
601 if (
602 ! defined $layer || ! defined $xPosBlocks || ! defined $yPosBlocks
603 || ! defined $widthBlocks || ! defined $heightBlocks || defined $check
604 ) {
605 return $axmud::CLIENT->writeImproper($self->_objClass . '->checkPosnInLayer', @_);
606 }
607  
608 $x1 = $xPosBlocks,
609 $x2 = $x1 + $widthBlocks - 1;
610 $y1 = $yPosBlocks,
611 $y2 = $y1 + $heightBlocks - 1;
612  
613 foreach my $areaObj ($self->ivValues('areaHash')) {
614  
615 if (
616 (! $winObj || $winObj ne $areaObj->winObj)
617 && $areaObj->layer == $layer
618 && (
619 (
620 ($x1 >= $areaObj->leftBlocks && $x1 <= $areaObj->rightBlocks)
621 || ($x2 >= $areaObj->leftBlocks && $x2 <= $areaObj->rightBlocks)
622 ) && (
623 ($y1 >= $areaObj->topBlocks && $y1 <= $areaObj->bottomBlocks)
624 || ($y2 >= $areaObj->topBlocks && $y2 <= $areaObj->bottomBlocks)
625 )
626 )
627 ) {
628 return undef;
629 }
630 }
631  
632 # The window fits at the specified layer and position
633 return 1;
634 }
635  
636 sub findSpaceInLayer {
637  
638 # Called by $self->placeWin
639 # Check to see if a window will fit in this zone, at the specified layer and in any
640 # position, without overlapping other windows in the same layer
641 # When the first suitable position is found, return the window's coordinates in the zone's
642 # internal grid
643 #
644 # Expected arguments
645 # $layer
646 # - Which layer within the zone to check first (matches a number between 0 and
647 # (GA::Obj::WorkspaceGrid->maxLayers - 1) )
648 # $widthBlocks, $heightBlocks
649 # - The window's proposed width and height (in gridblocks)
650 #
651 # Optional arguments
652 # $winObj
653 # - Specified by GA::Obj::WorkspaceGrid->changeWinzone, when it wants to place a
654 # window in the same zone it currently occupies. $winObj is something that
655 # inherits from GA::Generic::Win. If defined, regard any space currently occupied
656 # by this window as available
657 #
658 # Return values
659 # An empty list on improper arguments or if the window doesn't fit at any position in the
660 # grid without overlapping other windows on the same layer
661 # Otherwise returns a list in the form
662 # (1, $posX, $posY)
663 # ...The '1' represents success; the next two variables are the window's coordinates at a
664 # position in the zone's internal grid at the specified layer, at which the window
665 # will fit without overlapping other windows
666  
667 my ($self, $layer, $widthBlocks, $heightBlocks, $winObj, $check) = @_;
668  
669 # Local variables
670 my (
671 $startX, $startY, $step, $numX, $numY,
672 @emptyList,
673 );
674  
675 # Check for improper arguments
676 if (
677 ! defined $layer || ! defined $widthBlocks || ! defined $heightBlocks || defined $check
678 ) {
679 $axmud::CLIENT->writeImproper($self->_objClass . '->findSpaceInLayer', @_);
680 return @emptyList;
681 }
682  
683 # Set the search parameters. The coordinates of the top-left gridblock are (0,0)
684 # $step represents the direction of the search; 1 for left-to-right or top-to-bottom; -1 for
685 # right-to-left or bottom-to-top. The direction depends on $self->orientation which can be
686 # 'horizontal' (move left or right, before moving up and down) or 'vertical' (move up and
687 # down, before moving left and right)
688 if ($self->startCorner eq 'top_left') {
689  
690 # Gridblock at which the top-left corner of a window will be, when it is in the top-left
691 # corner of the search area
692 $startX = 0;
693 $startY = 0;
694 $step = 1;
695  
696 } elsif ($self->startCorner eq 'top_right') {
697  
698 # Gridblock at which the top-left corner of a window will be, when it is in the
699 # top-right corner of the search area
700 $startX = $self->widthBlocks - $widthBlocks;
701 $startY = 0;
702  
703 if ($self->orientation eq 'horizontal') {
704 $step = -1;
705 } else {
706 $step = 1;
707 }
708  
709 } elsif ($self->startCorner eq 'bottom_left') {
710  
711 # Gridblock at which the top-left corner of a window will be, when it is in the
712 # bottom-left corner of the search area
713 $startX = 0;
714 $startY = $self->heightBlocks - $heightBlocks;
715 if ($self->orientation eq 'horizontal') {
716 $step = 1;
717 } else {
718 $step = -1;
719 }
720  
721 } elsif ($self->startCorner eq 'bottom_right') {
722  
723 # Gridblock at which the top-left corner of the first window is, when it is in the
724 # bottom-right corner of the search area
725 $startX = $self->widthBlocks - $widthBlocks;
726 $startY = $self->heightBlocks - $heightBlocks;
727 $step = -1;
728 }
729  
730 # Set more search parameters - the number of possible positions of a window, of size
731 # $widthBlocks and $heightBlocks, in this zone's internal grid
732 $numX = ($self->widthBlocks - $widthBlocks + 1);
733 $numY = ($self->heightBlocks - $heightBlocks + 1);
734  
735 # Conduct the search
736 for (my $posX = $startX; $posX < ($startX + $numX); $posX += $step) {
737  
738 for (my $posY = $startY; $posY < ($startY + $numY); $posY += $step) {
739  
740 # Check that every gridblock in the area starting at grid coordinates $posX/$posY
741 # region is free
742 if (
743 $self->checkPosnInLayer(
744 $layer,
745 $posX,
746 $posY,
747 $widthBlocks,
748 $heightBlocks,
749 $winObj,
750 )
751 ) {
752 # The entire area is free, so a window can be placed at this position, at the
753 # specified layer, without overlapping other windows
754 return (1, $posX, $posY);
755 }
756 }
757 }
758  
759 # There isn't space for this window anywhere in this zone at the specified layer
760 return @emptyList;
761 }
762  
763 sub adjustSingleWin {
764  
765 # Called by GA::Obj::Workspace->chooseWinPosn
766 # If the window's proposed position on a zone's internal grid puts it rather close to the
767 # edge (or edges) of the zone, and if the gaps between the proposed window and the zone's
768 # edge(s) are unoccupied, adjust the size of the window to fill the gap (this prevents
769 # small regions of the zone from always being empty and makes the desktop look nice)
770 #
771 # Expected arguments
772 # $layer - Which layer within the zone to check first (matches a number between 0 and
773 # (GA::Obj::WorkspaceGrid->maxLayers - 1) )
774 # $winXPosBlocks, $winYPosBlocks
775 # - The window's proposed x/y coordinates on the zone's internal grid (in
776 # gridblocks)
777 # $winWidthBlocks, $winHeightBlocks
778 # - The window's proposed width and height (in gridblocks)
779 #
780 # Return values
781 # An empty list on improper arguments
782 # Otherwise returns the list
783 # ($winXPosBlocks, $winYPosBlocks, $winWidthBlocks, $winHeightBlocks)
784 # ...some of which may have been adjusted by this function
785  
786 my (
787 $self, $layer, $winXPosBlocks, $winYPosBlocks, $winWidthBlocks, $winHeightBlocks,
788 $check,
789 ) = @_;
790  
791 # Local variables
792 my (
793 $gapSize, $maxGapSize, $regionXPosBlocks, $regionYPosBlocks, $regionWidthBlocks,
794 $regionHeightBlocks,
795 @emptyList,
796 );
797  
798 # Check for improper arguments
799 if (
800 ! defined $layer || ! defined $winXPosBlocks || ! defined $winYPosBlocks
801 || ! defined $winWidthBlocks || ! defined $winHeightBlocks || defined $check
802 ) {
803 $axmud::CLIENT->writeImproper($self->_objClass . '->adjustSingleWin', @_);
804 return @emptyList;
805 }
806  
807 # If this zone has a maximum number of visible windows, the biggest gap this function will
808 # close is that number minus 1 (bigger gaps shouldn't occur in zones with a maximum number
809 # of visible windows)
810 # Otherwise, the biggest gap is the one defined by the global variable
811 if ($self->visibleAreaMax) {
812  
813 $maxGapSize = ($self->visibleAreaMax - 1);
814 # If this value is 0 (because only one window is allowed), don't need to check for gaps.
815 # Return the unadjusted size of the window
816 if (! $maxGapSize) {
817  
818 return (
819 $winXPosBlocks, $winYPosBlocks, $winWidthBlocks, $winHeightBlocks,
820 );
821 }
822  
823 } else {
824  
825 # Use the default value
826 $maxGapSize = $axmud::CLIENT->gridGapMaxSize;
827 }
828  
829 # Search the region to the left of the window
830 $gapSize = $winXPosBlocks;
831 if ( $gapSize && $gapSize <= $maxGapSize) {
832  
833 # Set the search region, immediately to the left of the window
834 $regionXPosBlocks = 0;
835 $regionYPosBlocks = $winYPosBlocks;
836 $regionWidthBlocks = $gapSize;
837 $regionHeightBlocks = $winHeightBlocks;
838  
839 if (
840 $self->checkPosnInLayer(
841 $layer,
842 $regionXPosBlocks,
843 $regionYPosBlocks,
844 $regionWidthBlocks,
845 $regionHeightBlocks,
846 )
847 ) {
848 # The gap is unoccupied; increase the width of the window
849 $winWidthBlocks += $gapSize;
850 $winXPosBlocks -= $gapSize;
851 }
852 }
853  
854 # Search the region to the right of the window
855 $gapSize = $self->widthBlocks - ($winXPosBlocks + $winWidthBlocks);
856 if ( $gapSize && $gapSize <= $maxGapSize) {
857  
858 # Set the search region, immediately to the right of the window
859 $regionXPosBlocks = $winXPosBlocks + $winWidthBlocks;
860 $regionYPosBlocks = $winYPosBlocks;
861 $regionWidthBlocks = $gapSize;
862 $regionHeightBlocks = $winHeightBlocks;
863  
864 if (
865 $self->checkPosnInLayer(
866 $layer,
867 $regionXPosBlocks,
868 $regionYPosBlocks,
869 $regionWidthBlocks,
870 $regionHeightBlocks,
871 )
872 ) {
873 # The gap is unoccupied; increase the width of the window (don't need to change the
874 # coordinates of the top-left corner)
875 $winWidthBlocks += $gapSize;
876 }
877 }
878  
879 # Search the region immediately above the window
880 $gapSize = $winYPosBlocks;
881 if ( $gapSize && $gapSize <= $maxGapSize) {
882  
883 # Set the search region, immediately above the window
884 $regionXPosBlocks = $winXPosBlocks;
885 $regionYPosBlocks = 0;
886 $regionWidthBlocks = $winWidthBlocks;
887 $regionHeightBlocks = $gapSize;
888  
889 if (
890 $self->checkPosnInLayer(
891 $layer,
892 $regionXPosBlocks,
893 $regionYPosBlocks,
894 $regionWidthBlocks,
895 $regionHeightBlocks,
896 )
897 ) {
898 # The gap is unoccupied; increase the height of the window
899 $winHeightBlocks += $gapSize;
900 $winYPosBlocks -= $gapSize;
901 }
902 }
903  
904 # Search the region immediately below the window
905 $gapSize = $self->heightBlocks - ($winYPosBlocks + $winHeightBlocks);
906 if ( $gapSize && $gapSize <= $maxGapSize) {
907  
908 # Set the search region, immediately below the window
909 $regionXPosBlocks = $winXPosBlocks;
910 $regionYPosBlocks = $winYPosBlocks + $winHeightBlocks;
911 $regionWidthBlocks = $winWidthBlocks;
912 $regionHeightBlocks = $gapSize;
913  
914 if (
915 $self->checkPosnInLayer(
916 $layer,
917 $regionXPosBlocks,
918 $regionYPosBlocks,
919 $regionWidthBlocks,
920 $regionHeightBlocks,
921 )
922 ) {
923 # The gap is unoccupied; increase the height of the window (don't need to change the
924 # coordinates of the top-left corner)
925 $winHeightBlocks += $gapSize;
926 }
927 }
928  
929 # Return the window position and size (some of the values may have been adjusted, or they
930 # may be the same)
931 return ($winXPosBlocks, $winYPosBlocks, $winWidthBlocks, $winHeightBlocks);
932 }
933  
934 sub adjustMultipleWin {
935  
936 # Called by GA::Client->createGridWin and GA::Obj::WorkspaceGrid->changeWinzone
937 # After a new window is created in this zone, if the zone is wide enough (and its
938 # orientation is 'horizontal') or high enough (and its orientation is 'vertical') for only
939 # one window of the zone's default size, then consider expanding all of the windows in the
940 # zone to fill gaps that are bigger than a gridblock or two, but still not big enough to
941 # hold another window
942 # If it would be better to reduce all the window sizes in order to make room for another
943 # window, some time in the future, do that instead. Leave enough room for a window of the
944 # default size
945 # If an adjustment is made, record the size of the adjustment in
946 # $self->widthAdjustBlocks and $self->heightAdjustBlocks, and set the flag
947 # $self->areaAdjustFlag to TRUE
948 # If the adjustment causes windows to return the zone's default size, these three variables
949 # are all reset
950 # This function takes account of the direction in which the workspace is being filled (left
951 # to right, right to left, top to bottom, bottom to top)
952 #
953 # Expected arguments
954 # $layer - Which layer within the zone to check first (matches a number between 0 and
955 # (GA::Obj::WorkspaceGrid->maxLayers - 1) )
956 #
957 # Return values
958 # 'undef' on improper arguments, or if no adjustment is made
959 # 1 if an adjustment is made
960  
961 my ($self, $layer, $check) = @_;
962  
963 # Local variables
964 my (
965 $gapSize, $width, $height, $gapCount, $adjustmentCount,
966 $largestAdjustment, $startBlock, $dir, $blockSize,
967 @areaList,
968 %adjustHash,
969 );
970  
971 # Check for improper arguments
972 if (! defined $layer || defined $check) {
973  
974 return $axmud::CLIENT->writeImproper($self->_objClass . '->adjustMultipleWin', @_);
975 }
976  
977 # Compile a list of areas in this zone, sorted in order of their proximity to the starting
978 # corner
979 @areaList = $self->getSortedAreaList($layer);
980 if (! @areaList) {
981  
982 # No adjustments to make
983 return undef;
984 }
985  
986 # Work out the total width or height (in gridblocks) of all these areas if they were to be
987 # put together in a line (which they probably already are)
988 $width = 0;
989 $height = 0;
990 if ($self->orientation eq 'horizontal') {
991  
992 foreach my $areaObj (@areaList) {
993  
994 $width += $areaObj->widthBlocks;
995 }
996  
997 } elsif ($self->orientation eq 'vertical') {
998  
999 foreach my $areaObj (@areaList) {
1000  
1001 $height += $areaObj->heightBlocks;
1002 }
1003 }
1004  
1005 # Now work out the size of the gap between the end of the line and the total length of the
1006 # zone
1007 if ($self->orientation eq 'horizontal') {
1008 $gapSize = ($self->widthBlocks - $width);
1009 } elsif ($self->orientation eq 'vertical') {
1010 $gapSize = ($self->heightBlocks - $height);
1011 }
1012  
1013 # If there's no gap, then make no adjustments
1014 if (! $gapSize) {
1015  
1016 return undef;
1017  
1018 # If the gap is big enough for a standard-sized window, make no adjustments - the gap
1019 # doesn't need to be filled
1020 } elsif (
1021 ($width && $gapSize >= $self->defaultAreaWidth)
1022 || ($height && $gapSize >= $self->defaultAreaHeight)
1023 ) {
1024 return undef;
1025 }
1026  
1027 # Otherwise, divide up the gap as equally as possible between the windows
1028  
1029 # Copy @areaList into a hash to remember its adjusted width or height (e.g. a value of +1
1030 # means the width or height has increased by 1 gridblock, a value of -1 means the width
1031 # or height has decreased, and a value of 0 means the width or height is the same)
1032 # $adjustHash{area_number} = change_in_size
1033 foreach my $areaObj (@areaList) {
1034  
1035 $adjustHash{$areaObj->number} = 0;
1036 }
1037  
1038 # If the gap is less than half a size of a default-sized window, try to fill the gap by
1039 # making the other area bigger
1040 if (
1041 ($width && $gapSize < (($self->defaultAreaWidth) / 2))
1042 || ($height && $gapSize < (($self->defaultAreaHeight) / 2))
1043 ) {
1044 # Start with the area furthest from the start corner...
1045 @areaList = reverse @areaList;
1046 # ...and increase its height (or width) by one gridblock. When each area has been
1047 # reduced, repeat the cycle until the gap has closed
1048 $gapCount = 0;
1049 $largestAdjustment = 0;
1050  
1051 OUTER: while (1) {
1052  
1053 INNER: foreach my $areaObj (@areaList) {
1054  
1055 $gapCount++;
1056 $adjustHash{$areaObj->number}++;
1057  
1058 if ($largestAdjustment < $adjustHash{$areaObj->number}) {
1059  
1060 $largestAdjustment = $adjustHash{$areaObj->number};
1061 }
1062  
1063 if ($gapCount == $gapSize) {
1064  
1065 last OUTER;
1066 }
1067 }
1068 }
1069  
1070 # Un-reverse the list of areas, so that they're moved in an aesthetically pleasing
1071 # order
1072 @areaList = reverse @areaList;
1073  
1074 # If the gap is more than half a size of a default-sized window, try to increase the gap by
1075 # making the other areas smaller
1076 } else {
1077  
1078 # Start with the area furthest from the start corner...
1079 @areaList = reverse @areaList;
1080 # ...and decrease its height (or width) by one gridblock. When each area has been
1081 # reduced, repeat the cycle until the gap has expanded far enough to accommodate an
1082 # area
1083 $adjustmentCount = 0;
1084 $largestAdjustment = 0;
1085  
1086 OUTER: while (1) {
1087  
1088 $adjustmentCount++;
1089 INNER: foreach my $areaObj (@areaList) {
1090  
1091 $gapSize++;
1092 $adjustHash{$areaObj->number}--;
1093  
1094 if ($largestAdjustment > $adjustHash{$areaObj->number}) {
1095  
1096 $largestAdjustment = $adjustHash{$areaObj->number};
1097 }
1098  
1099 # When all the areas are almost the same size as the expanded gap (e.g. area
1100 # widths 26, 26, 25; gap width 25), stop adjusting
1101 if (
1102 (
1103 $self->orientation eq 'horizontal'
1104 && $gapSize >= ($self->defaultAreaWidth + $largestAdjustment)
1105 ) || (
1106 $self->orientation eq 'vertical'
1107 && $gapSize >= ($self->defaultAreaHeight + $largestAdjustment)
1108 )
1109 ) {
1110 last OUTER;
1111 }
1112 }
1113 }
1114  
1115 # Un-reverse the list of areas, so that they're moved in an aesthetically pleasing
1116 # order
1117 @areaList = reverse @areaList;
1118 }
1119  
1120 # Reset the zone's internal grid
1121 $self->resetInternalGrid();
1122  
1123 # Save the size of the adjustment - if there's a gap big enough for another area,
1124 # GA::Obj::Workspace->createGridWin can use the adjustment in creating an area small
1125 # enough to fit
1126 $self->ivPoke('areaAdjustFlag', TRUE);
1127 if ($self->orientation eq 'horizontal') {
1128 $self->ivPlus('widthAdjustBlocks', $largestAdjustment);
1129 } else {
1130 $self->ivPlus('heightAdjustBlocks', $largestAdjustment);
1131 }
1132  
1133 # Now, move all the areas (and the windows contained in them) into their new positions,
1134 # starting with window closest to the start corner
1135 if ($self->orientation eq 'horizontal') {
1136  
1137 if ($self->startCorner eq 'top_left' || $self->startCorner eq 'bottom_left') {
1138  
1139 $startBlock = 0;
1140 $dir = 1; # Move right
1141  
1142 } else {
1143  
1144 $startBlock = $self->widthBlocks - 1;
1145 $dir = -1; # Move left
1146 }
1147  
1148 } elsif ($self->orientation eq 'vertical') {
1149  
1150 if ($self->startCorner eq 'top_left' || $self->startCorner eq 'top_right') {
1151  
1152 $startBlock = 0;
1153 $dir = 1; # Move down
1154  
1155 } else {
1156  
1157 $startBlock = $self->heightBlocks - 1;
1158 $dir = -1; # Move up
1159 }
1160 }
1161  
1162 foreach my $areaObj (@areaList) {
1163  
1164 my (
1165 $widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks, $widthPixels, $heightPixels,
1166 $xPosPixels, $yPosPixels,
1167 );
1168  
1169 if ($self->orientation eq 'horizontal') {
1170  
1171 # Set the new size of the area, in blocks on the zone's internal grid
1172 $widthBlocks = $areaObj->zoneWidth + $adjustHash{$areaObj->number};
1173 $heightBlocks = $areaObj->zoneHeight;
1174 # Set the new position of the area, in blocks on the zone's internal grid
1175 $yPosBlocks = $areaObj->zoneTop;
1176 if ($dir == 1) {
1177 $xPosBlocks = $startBlock;
1178 } else {
1179 $xPosBlocks = ($startBlock - $widthBlocks + 1);
1180 }
1181  
1182 # Set $startBlock as the position of the next area
1183 $startBlock = $startBlock + ($widthBlocks * $dir);
1184  
1185 } else {
1186  
1187 # Set the new size of the area, in blocks on the zone's internal grid
1188 $widthBlocks = $areaObj->zoneWidth;
1189 $heightBlocks = $areaObj->zoneHeight + $adjustHash{$areaObj->number};
1190 # Set the new position of the area, in blocks on the zone's internal grid
1191 $xPosBlocks = $areaObj->zoneLeft;
1192 if ($dir == 1) {
1193 $yPosBlocks = $startBlock;
1194 } else {
1195 $yPosBlocks = ($startBlock - $heightBlocks + 1);
1196 }
1197  
1198 # Set $startBlock as the position of the next window
1199 $startBlock = $startBlock + ($heightBlocks * $dir);
1200 }
1201  
1202 # Convert these values into pixels on the workspace
1203 $blockSize = $axmud::CLIENT->gridBlockSize;
1204  
1205 $widthPixels = $widthBlocks * $blockSize;
1206 $heightPixels = $heightBlocks * $blockSize;
1207 $xPosPixels = $self->workspaceGridObj->xPosPixels
1208 + (($self->xPosBlocks + $xPosBlocks) * $blockSize);
1209 $yPosPixels = $self->workspaceGridObj->yPosPixels +
1210 (($self->yPosBlocks + $yPosBlocks) * $blockSize);
1211  
1212 # Correct for windows controls, etc
1213 ($xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
1214 = $self->workspaceGridObj->fineTuneWinSize(
1215 $areaObj->winObj->winType,
1216 $xPosPixels,
1217 $yPosPixels,
1218 $widthPixels,
1219 $heightPixels,
1220 );
1221  
1222 # Resize the window and move it to the correct location
1223 $self->workspaceGridObj->workspaceObj->moveResizeWin(
1224 $areaObj->winObj,
1225 $xPosPixels,
1226 $yPosPixels,
1227 $widthPixels,
1228 $heightPixels,
1229 );
1230  
1231 # Update the area object with the window's new location
1232 $areaObj->set_zone(
1233 $layer,
1234 $xPosBlocks,
1235 $yPosBlocks,
1236 $widthBlocks,
1237 $heightBlocks,
1238 );
1239  
1240 $areaObj->set_posn(
1241 $xPosPixels,
1242 $yPosPixels,
1243 $widthPixels,
1244 $heightPixels,
1245 );
1246  
1247 # Restack the windows in this zone - windows in a higher layer are placed above those in
1248 # a lower layer; but windows which are higher than the workspace grid's ->currentLayer
1249 # are minimised
1250 $self->restackWin();
1251 }
1252  
1253 # Adjustment complete
1254 return 1;
1255 }
1256  
1257 sub getSortedAreaList {
1258  
1259 # Called by $self->adjustMultipleWin and $self->replaceAreaSpace
1260 # Returns a list of all areas in the zone, on a specified layer, sorted in the order of
1261 # closeness to the starting corner. (Each area is a space on the grid occupied by a
1262 # single window)
1263 #
1264 # Expected arguments
1265 # $layer - Which layer to check
1266 #
1267 # Return values
1268 # An empty list on improper arguments
1269 # Otherwise, the sorted list. If there are no areas on this layer, returns an empty list
1270  
1271 my ($self, $layer, $check) = @_;
1272  
1273 # Local variables
1274 my (@emptyList, @areaList);
1275  
1276 # Check for improper arguments
1277 if (! defined $layer || defined $check) {
1278  
1279 $axmud::CLIENT->writeImproper($self->_objClass . '->getSortedAreaList', @_);
1280 return @emptyList;
1281 }
1282  
1283 # Compile a list of all windows in this zone, at the specified layer
1284 foreach my $areaObj ($self->ivValues('areaHash')) {
1285  
1286 if ($areaObj->layer == $layer) {
1287  
1288 push (@areaList, $areaObj);
1289 }
1290 }
1291  
1292 # Sort the list, with the area closest to the starting corner at the beginning of the list
1293 if ($self->orientation eq 'horizontal') {
1294  
1295 if ($self->startCorner eq 'top_left' || $self->startCorner eq 'bottom_left') {
1296  
1297 @areaList = sort {$a->leftBlocks <=> $b->leftBlocks} (@areaList);
1298  
1299 } else {
1300  
1301 @areaList = sort {$b->rightBlocks <=> $a->rightBlocks} (@areaList);
1302 }
1303  
1304 } elsif ($self->orientation eq 'vertical') {
1305  
1306 if ($self->startCorner eq 'top_left' || $self->startCorner eq 'top_right') {
1307  
1308 @areaList = sort {$a->topBlocks <=> $b->topBlocks} (@areaList);
1309  
1310 } else {
1311  
1312 @areaList = sort {$b->bottomBlocks <=> $a->bottomBlocks} (@areaList);
1313 }
1314 }
1315  
1316 # Return the sorted list
1317 return @areaList;
1318 }
1319  
1320 sub addArea {
1321  
1322 # Called by GA::Obj::Workspace->createGridWin
1323 # Marks a part of the zone's internal grid as occupied by this window (by creating a new
1324 # GA::Obj::Area object), and updates other IVs
1325 #
1326 # Expected arguments
1327 # $layer - The window's layer on the zone's internal grid
1328 # $xPosBlocks, $yPosBlocks
1329 # - The window's x/y coordinates on the zone's internal grid (in
1330 # gridblocks)
1331 # $widthBlocks, $heightBlocks
1332 # - The window's width and height (in gridblocks)
1333 # $xPosPixels, $yPosPixels
1334 # - The window's x/y coordinates on the workspace (in pixels)
1335 # $widthPixels, $heightPixels
1336 # - The window's width and height (in pixels)
1337 #
1338 # Optional arguments
1339 # $session
1340 # - The GA::Session controlling this window ('undef' if no owner)
1341 # $changeWinObj
1342 # - Specified by GA::Obj::WorkspaceGrid->changeWinzone, when it wants to place a
1343 # window in the same zone it currently occupied. If defined, regard any space
1344 # occupied by this window as available
1345 # - NB If $changeWinObj is specified, it's the same as $winObj
1346 #
1347 # Return values
1348 # 'undef' on improper arguments, or if any part of the zone's internal grid to be occupied
1349 # by the window is already occupied (won't happen if this function is called by
1350 # GA::Client->createGridWin)
1351 # Otherwise returns the GA::Obj::Area object created
1352  
1353 my (
1354 $self, $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels,
1355 $yPosPixels, $widthPixels, $heightPixels, $session, $changeWinObj, $check,
1356 ) = @_;
1357  
1358 # Local variables
1359 my $areaObj;
1360  
1361 # Check for improper arguments
1362 if (
1363 ! defined $layer || ! defined $xPosBlocks || ! defined $yPosBlocks
1364 || ! defined $widthBlocks || ! defined $heightBlocks || ! defined $xPosPixels
1365 || ! defined $yPosPixels || ! defined $widthPixels || ! defined $heightPixels
1366 || defined $check
1367 ) {
1368 return $axmud::CLIENT->writeImproper($self->_objClass . '->addArea', @_);
1369 }
1370  
1371 # Check that the area is unoccupied by other windows (it shouldn't be)
1372 if (
1373 ! $self->checkPosnInLayer(
1374 $layer,
1375 $xPosBlocks,
1376 $yPosBlocks,
1377 $widthBlocks,
1378 $heightBlocks,
1379 $changeWinObj,
1380 )
1381 ) {
1382 return $axmud::CLIENT->writeError(
1383 'Window cannot be placed at the specified position, because the area is already'
1384 . ' occupied',
1385 $self->_objClass . '->addArea',
1386 );
1387 }
1388  
1389 # Create a new area object to store details about the window's size and position
1390 $areaObj = Games::Axmud::Obj::Area->new($self->areaCount, $self);
1391 $self->ivAdd('areaHash', $areaObj->number, $areaObj);
1392 $self->ivIncrement('areaCount');
1393  
1394 # If $self->ownerString is defined, this zone is reserved for a single session. The IV's
1395 # value can be any non-empty string. All zones with the same ->ownerString are reserved
1396 # for a particular session
1397 # The first session to place one of its windows into any 'owned' zone claims all of
1398 # those zones for itself. If $self->ownerString IV is 'undef', the zone is available for
1399 # any session to use (subject to restriction described in the comments in $self->new)
1400 # ->ownerString is ignored in GA::Client->shareMainWinFlag = TRUE
1401 if (
1402 ! $axmud::CLIENT->shareMainWinFlag
1403 && defined $session
1404 && defined $self->ownerString
1405 && $self->ownerString ne ''
1406 && ! defined $self->owner
1407 ) {
1408 # Claim this zone, and all other zones (across all workspaces) with the same
1409 # ->ownerString for this session
1410 $axmud::CLIENT->desktopObj->claimZones($session, $self->ownerString);
1411 }
1412  
1413 # Set its IVs (->set_win is called by GA::Obj::Workspace->createGridWin, once the window
1414 # object has been created)
1415 $areaObj->set_zone($layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks);
1416 $areaObj->set_posn($xPosPixels, $yPosPixels, $widthPixels, $heightPixels);
1417  
1418 return $areaObj;
1419 }
1420  
1421 sub removeArea {
1422  
1423 # Called by GA::Obj::Workspace->createGridWin, GA::Obj::WorkspaceGrid->changeWinzone and
1424 # ->del_gridWin
1425 # Removes the GA::Obj::Area object (which is occupied by a single window) from this zone's
1426 # internal grid
1427 # Optionally reshuffles remaining windows
1428 #
1429 # Expected arguments
1430 # $areaObj - The GA::Obj::Area which should be removed
1431 #
1432 # Optional arguments
1433 # $noShuffleFlag - Set to TRUE when called GA::Obj::WorkspaceGrid->changeWinzone, in
1434 # which case this function doesn't call $self->replaceAreaSpace to
1435 # reshuffle the positions of windows in this zone
1436 #
1437 # Return values
1438 # 'undef' on improper arguments, or if the area doesn't seem to exist in this zone, or
1439 # if an operation to re-shuffle window positions fails
1440 # 1 otherwise
1441  
1442 my ($self, $areaObj, $noShuffleFlag, $check) = @_;
1443  
1444 # Check for improper arguments
1445 if (! defined $areaObj || defined $check) {
1446  
1447 return $axmud::CLIENT->writeImproper($self->_objClass . '->removeArea', @_);
1448 }
1449  
1450 # Update IVs
1451 $self->ivDelete('areaHash', $areaObj->number);
1452  
1453 # If there are no areas left in this zone, and the zone's ->owner is set, tell the
1454 # GA::Obj::Desktop to check all zones across all workspaces. If the session has no windows
1455 # in any of the zones it controls, free up those zones so they're available to another
1456 # session
1457 if ($self->owner && ! $self->areaHash) {
1458  
1459 $axmud::CLIENT->desktopObj->relinquishZones($self->ownerString);
1460 }
1461  
1462 # If the global flag is set, reshuffle the position of all windows in this zone, removing
1463 # any gaps that might have appeared in the middle of the zone, if the removed window
1464 # occupied that space
1465 if (! $noShuffleFlag && $axmud::CLIENT->gridReshuffleFlag) {
1466  
1467 return $self->replaceAreaSpace($areaObj->layer);
1468  
1469 } else {
1470  
1471 return 1;
1472 }
1473 }
1474  
1475 sub replaceAreaSpace {
1476  
1477 # Called by $self->removeArea
1478 # After a window has been removed, it's often desirable to move all the windows in the zone
1479 # (on the same layer) to fill the gap - so, instead of having a gap in the middle, we have
1480 # a gap at the end (the opposite end to the starting corner).
1481 # This function performs that operation
1482 #
1483 # Expected arguments
1484 # $layer - Which layer to reshuffle
1485 #
1486 # Return values
1487 # 'undef' on improper arguments, or if (for some very unlikely reason) one of the windows
1488 # won't fit in the zone
1489 # 1 otherwise
1490  
1491 my ($self, $layer, $check) = @_;
1492  
1493 # Local variables
1494 my (
1495 $blockSize,
1496 @areaList,
1497 );
1498  
1499 # Check for improper arguments
1500 if (! defined $layer || defined $check) {
1501  
1502 return $axmud::CLIENT->writeImproper($self->_objClass . '->replaceAreaSpace', @_);
1503 }
1504  
1505 # Compile a list of GA::Obj::Area objects (one area for each window), sorted in order of
1506 # their proximity to the starting corner
1507 @areaList = $self->getSortedAreaList($layer);
1508  
1509 # Move the windows into position. Start with the closest windows, so that all windows are in
1510 # turn moved closer to the starting corner
1511 $blockSize = $axmud::CLIENT->gridBlockSize;
1512 foreach my $areaObj (@areaList) {
1513  
1514 my (
1515 $widthBlocks, $heightBlocks, $widthPixels, $heightPixels, $xPosBlocks, $yPosBlocks,
1516 $xPosPixels, $yPosPixels, $successFlag,
1517 );
1518  
1519 # Get the area's size on the zone's internal grid. Remove the influence of any past
1520 # fine-tuning by GA::Obj::WorkspaceGrid->fineTuneWinSize and window size adjustments
1521 # made by $self->adjustMultipleWin
1522 $widthBlocks = $areaObj->widthBlocks - $self->widthAdjustBlocks;
1523 $heightBlocks = $areaObj->heightBlocks - $self->heightAdjustBlocks;
1524 # Convert into pixels
1525 $widthPixels = $widthBlocks * $blockSize;
1526 $heightPixels = $heightBlocks * $blockSize;
1527  
1528 # Provisionally place the area's window at the starting corner of the zone's internal
1529 # grid
1530 ($widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks)
1531 = $self->findProvWinPosn($widthPixels, $heightPixels);
1532  
1533 # Move the window into the first unoccupied position in the zone's internal grid
1534 ($successFlag, $layer, $xPosBlocks, $yPosBlocks)
1535 = $self->placeWin(
1536 $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $areaObj->winObj,
1537 );
1538  
1539 # There should be no possible reason why there isn't enough room - but just in case...
1540 if (! $successFlag) {
1541  
1542 return $self->writeError(
1543 'Failed to replaced a formerly occupied part of zone #' . $self->number,
1544 $self->_objClass . '->replaceAreaSpace',
1545 );
1546 }
1547  
1548 # Adjust windows at the edge of the zone, if necessary
1549 if ($axmud::CLIENT->gridGapMaxSize) {
1550  
1551 ($xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks) = $self->adjustSingleWin(
1552 $layer,
1553 $xPosBlocks,
1554 $yPosBlocks,
1555 $widthBlocks,
1556 $heightBlocks,
1557 );
1558 }
1559  
1560 # Get the window's new size and position in pixels (converting from blocks)
1561 ($xPosPixels, $yPosPixels) = $self->getInternalGridPosn($xPosBlocks, $yPosBlocks);
1562 $widthPixels = $widthBlocks * $blockSize;
1563 $heightPixels = $heightBlocks * $blockSize;
1564  
1565 # Fine tune the window size, taking into account window controls and arkward available
1566 # workspace sizes
1567 ($xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
1568 = $self->workspaceGridObj->fineTuneWinSize(
1569 $areaObj->winObj->winType,
1570 $xPosPixels,
1571 $yPosPixels,
1572 $widthPixels,
1573 $heightPixels,
1574 );
1575  
1576 # Resize the window and move it to the correct location
1577 $self->workspaceGridObj->workspaceObj->moveResizeWin(
1578 $areaObj->winObj,
1579 $xPosPixels,
1580 $yPosPixels,
1581 $widthPixels,
1582 $heightPixels,
1583 );
1584  
1585 # Update the area object with the window's new location
1586 $areaObj->set_zone(
1587 $layer,
1588 $xPosBlocks,
1589 $yPosBlocks,
1590 $widthBlocks,
1591 $heightBlocks,
1592 );
1593  
1594 $areaObj->set_posn(
1595 $xPosPixels,
1596 $yPosPixels,
1597 $widthPixels,
1598 $heightPixels,
1599 );
1600 }
1601  
1602 # Cancel any window adjustments - they are no longer valid
1603 $self->ivPoke('areaAdjustFlag', FALSE);
1604 $self->ivPoke('widthAdjustBlocks', 0);
1605 $self->ivPoke('heightAdjustBlocks', 0);
1606  
1607 # Operation complete
1608 return 1;
1609 }
1610  
1611 sub restackWin {
1612  
1613 # Called by GA::Obj::Workspace->createGridWin, ->moveResizeWin and
1614 # $self->adjustMultipleWin
1615 # Restack the windows in this zone - windows in a higher layer are placed above those in a
1616 # lower layer; but windows which are higher than the workspace grid's ->currentLayer are
1617 # minimised
1618 # NB The window manager is under no obligation to obey a request to restack one window above
1619 # another, so this operation is not foolproof
1620 # NB This function does nothing if GA::Obj::Workspace->gridEnableFlag = FALSE or if no
1621 # windows have been created yet (unlikely)
1622 #
1623 # Expected arguments
1624 # (none besides $self)
1625 #
1626 # Return values
1627 # 'undef' on improper arguments or if the windows are not restacked
1628 # 1 otherwise
1629  
1630 my ($self, $session, $check) = @_;
1631  
1632 # Local variables
1633 my (
1634 $prevAreaObj,
1635 @areaList,
1636 );
1637  
1638 # Check for improper arguments
1639 if (defined $check) {
1640  
1641 return $axmud::CLIENT->writeImproper($self->_objClass . '->restackWin', @_);
1642 }
1643  
1644 # Don't do any restacking in if workspace grids are disabled or if no grid windows have been
1645 # created in this workspace grid yet
1646 foreach my $zoneObj ($self->workspaceGridObj->ivValues('zoneHash')) {
1647  
1648 push (@areaList, $zoneObj->ivValues('areaHash'));
1649 }
1650  
1651 if (! @areaList || ! $self->workspaceGridObj->workspaceObj->gridEnableFlag) {
1652  
1653 return undef;
1654 }
1655  
1656 # Each area (GA::Obj::Area) handles a window in the zone
1657 # Sort the areas by layer, with windows in the lowest layer coming first. (To avoid
1658 # unnecessary stacking operations, we'll sort by window number too)
1659 foreach my $areaObj (
1660 sort {
1661 if ($a->layer != $b->layer) {
1662 $a->layer <=> $b->layer;
1663 } else {
1664 $a->number <=> $b->number;
1665 }
1666 }
1667 $self->ivValues('areaHash')
1668 ) {
1669 my ($winObj, $gdkWin, $state, $minFlag);
1670  
1671 $winObj = $areaObj->winObj;
1672 if ($winObj->winWidget) {
1673  
1674 $gdkWin = $winObj->winWidget->get_window();
1675 if ($gdkWin) {
1676  
1677 $state = $gdkWin->get_state();
1678 if ($state =~ m/iconified/) {
1679  
1680 $minFlag = TRUE;
1681 }
1682 }
1683 }
1684  
1685 if ($gdkWin && ! defined $prevAreaObj) {
1686  
1687 # First non-'external' window tested; it will be at the bottom of the stack (we'll
1688 # assume it's not in a layer higher than the current one)
1689 $prevAreaObj = $areaObj;
1690  
1691 } elsif ($prevAreaObj) {
1692  
1693 # If the window is in a layer higher than the current one, just minimise it
1694 if ($areaObj->layer > $self->workspaceGridObj->currentLayer && ! $minFlag) {
1695  
1696 $winObj->minimise();
1697  
1698 } else {
1699  
1700 # This window is at the current layer, or below it
1701  
1702 # If the window is minimised, un-minimise it
1703 if ($minFlag) {
1704  
1705 $winObj->unminimise();
1706 }
1707  
1708 if ($gdkWin) {
1709  
1710 # Restack it just above a window that is either on this level, or on a
1711 # lower level (we can't restack 'external' windows, only minimise/
1712 # unminimise them)
1713 $gdkWin->restack(
1714 $prevAreaObj->winObj->winWidget->get_window(),
1715 TRUE,
1716 );
1717 }
1718 }
1719 }
1720 }
1721  
1722 return 1;
1723 }
1724  
1725 ##################
1726 # Accessors - set
1727  
1728 sub set_areaHeight {
1729  
1730 # Called by GA::Obj::WorkspaceGrid->resetZones
1731  
1732 my ($self, $height, $check) = @_;
1733  
1734 # Check for improper arguments
1735 if (! defined $height || defined $check) {
1736  
1737 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_areaHeight', @_);
1738 }
1739  
1740 $self->ivPoke('defaultAreaHeight', $height);
1741  
1742 return 1;
1743 }
1744  
1745 sub set_areaVars {
1746  
1747 # Called by GA::Obj::WorkspaceGrid->resetZones
1748  
1749 my ($self, $areaMax, $visibleAreaMax, $areaCount, $check) = @_;
1750  
1751 # Check for improper arguments
1752 if (
1753 ! defined $areaMax || ! defined $visibleAreaMax || ! defined $areaCount
1754 || defined $check
1755 ) {
1756 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_areaVars', @_);
1757 }
1758  
1759 $self->ivPoke('areaMax', $areaMax);
1760 $self->ivPoke('visibleAreaMax', $visibleAreaMax);
1761 $self->ivPoke('areaCount', $areaCount);
1762  
1763 return 1;
1764 }
1765  
1766 sub set_areaWidth {
1767  
1768 # Called by GA::Obj::WorkspaceGrid->resetZones
1769  
1770 my ($self, $width, $check) = @_;
1771  
1772 # Check for improper arguments
1773 if (! defined $width || defined $check) {
1774  
1775 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_areaWidth', @_);
1776 }
1777  
1778 $self->ivPoke('defaultAreaWidth', $width);
1779  
1780 return 1;
1781 }
1782  
1783 sub set_orientation {
1784  
1785 # Called by GA::Obj::WorkspaceGrid->resetZones
1786  
1787 my ($self, $startCorner, $orientation, $check) = @_;
1788  
1789 # Check for improper arguments
1790 if (! defined $startCorner || ! defined $orientation || defined $check) {
1791  
1792 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_orientation', @_);
1793 }
1794  
1795 $self->ivPoke('startCorner', $startCorner);
1796 $self->ivPoke('orientation', $orientation);
1797  
1798 return 1;
1799 }
1800  
1801 sub set_owner {
1802  
1803 # Called by GA::Obj::Desktop->claimZones
1804  
1805 my ($self, $session, $check) = @_;
1806  
1807 # Check for improper arguments
1808 if (! defined $session || defined $check) {
1809  
1810 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_owner', @_);
1811 }
1812  
1813 $self->ivPoke('owner', $session);
1814  
1815 return 1;
1816 }
1817  
1818 sub reset_owner {
1819  
1820 # Called by GA::Obj::Desktop->relinquishZones
1821  
1822 my ($self, $check) = @_;
1823  
1824 # Check for improper arguments
1825 if (defined $check) {
1826  
1827 return $axmud::CLIENT->writeImproper($self->_objClass . '->reset_owner', @_);
1828 }
1829  
1830 $self->ivPoke('owner', undef);
1831  
1832 return 1;
1833 }
1834  
1835 sub set_ownerString {
1836  
1837 # Called by GA::Obj::WorkspaceGrid->resetZones
1838  
1839 my ($self, $string, $check) = @_;
1840  
1841 # Check for improper arguments
1842 if (defined $check) {
1843  
1844 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_ownerString', @_);
1845 }
1846  
1847 $self->ivPoke('ownerString', $string);
1848  
1849 return 1;
1850 }
1851  
1852 sub set_posn {
1853  
1854 # Called by GA::Obj::WorkspaceGrid->resetZones
1855  
1856 my ($self, $x, $y, $check) = @_;
1857  
1858 # Check for improper arguments
1859 if (! defined $x || ! defined $y || defined $check) {
1860  
1861 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_posn', @_);
1862 }
1863  
1864 $self->ivPoke('xPosBlocks', $x);
1865 $self->ivPoke('yPosBlocks', $y);
1866  
1867 return 1;
1868 }
1869  
1870 sub set_reserved {
1871  
1872 # Called by GA::Obj::WorkspaceGrid->resetZones
1873  
1874 my ($self, $multipleLayerFlag, $reservedFlag, %reservedHash) = @_;
1875  
1876 # Check for improper arguments
1877 if (! defined $multipleLayerFlag || ! defined $reservedFlag) {
1878  
1879 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_reserved', @_);
1880 }
1881  
1882 $self->ivPoke('multipleLayerFlag', $multipleLayerFlag);
1883 $self->ivPoke('reservedFlag', $reservedFlag);
1884 $self->ivPoke('reservedHash', %reservedHash);
1885  
1886 return 1;
1887 }
1888  
1889 sub set_size {
1890  
1891 # Called by GA::Obj::WorkspaceGrid->resetZones
1892  
1893 my ($self, $width, $height, $check) = @_;
1894  
1895 # Check for improper arguments
1896 if (! defined $width || ! defined $height || defined $check) {
1897  
1898 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_size', @_);
1899 }
1900  
1901 $self->ivPoke('widthBlocks', $width);
1902 $self->ivPoke('heightBlocks', $height);
1903  
1904 return 1;
1905 }
1906  
1907 sub set_winmap {
1908  
1909 # Called by GA::Obj::WorkspaceGrid->resetZones
1910  
1911 my ($self, $enabledWinmap, $disabledWinmap, $internalWinmap, $check) = @_;
1912  
1913 # Check for improper arguments
1914 if (defined $check) {
1915  
1916 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_winmap', @_);
1917 }
1918  
1919 $self->ivPoke('defaultEnabledWinmap', $enabledWinmap);
1920 $self->ivPoke('defaultDisabledWinmap', $disabledWinmap);
1921 $self->ivPoke('defaultInternalWinmap', $internalWinmap);
1922  
1923 return 1;
1924 }
1925  
1926 ##################
1927 # Accessors - get
1928  
1929 sub number
1930 { $_[0]->{number} }
1931 sub workspaceGridObj
1932 { $_[0]->{workspaceGridObj} }
1933  
1934 sub xPosBlocks
1935 { $_[0]->{xPosBlocks} }
1936 sub yPosBlocks
1937 { $_[0]->{yPosBlocks} }
1938 sub widthBlocks
1939 { $_[0]->{widthBlocks} }
1940 sub heightBlocks
1941 { $_[0]->{heightBlocks} }
1942  
1943 sub reservedFlag
1944 { $_[0]->{reservedFlag} }
1945 sub reservedHash
1946 { my $self = shift; return %{$self->{reservedHash}}; }
1947 sub multipleLayerFlag
1948 { $_[0]->{multipleLayerFlag} }
1949  
1950 sub ownerString
1951 { $_[0]->{ownerString} }
1952 sub owner
1953 { $_[0]->{owner} }
1954  
1955 sub areaHash
1956 { my $self = shift; return %{$self->{areaHash}}; }
1957 sub areaCount
1958 { $_[0]->{areaCount} }
1959 sub areaMax
1960 { $_[0]->{areaMax} }
1961 sub visibleAreaMax
1962 { $_[0]->{visibleAreaMax} }
1963  
1964 sub startCorner
1965 { $_[0]->{startCorner} }
1966 sub orientation
1967 { $_[0]->{orientation} }
1968 sub defaultEnabledWinmap
1969 { $_[0]->{defaultEnabledWinmap} }
1970 sub defaultDisabledWinmap
1971 { $_[0]->{defaultDisabledWinmap} }
1972 sub defaultInternalWinmap
1973 { $_[0]->{defaultInternalWinmap} }
1974  
1975 sub defaultAreaWidth
1976 { $_[0]->{defaultAreaWidth} }
1977 sub defaultAreaHeight
1978 { $_[0]->{defaultAreaHeight} }
1979 sub areaAdjustFlag
1980 { $_[0]->{areaAdjustFlag} }
1981 sub widthAdjustBlocks
1982 { $_[0]->{widthAdjustBlocks} }
1983 sub heightAdjustBlocks
1984 { $_[0]->{heightAdjustBlocks} }
1985 }
1986  
1987 # Package must return a true value
1988 1