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

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 262 CC 12 SCAR 29.4
CC
12
Cov
0%
new
90.0
new
6.0
set_style
6.0
CRAP 19.0
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::MapLabel
16 # The object that handles a label on the Automapper window's map
17 #
18 # Games::Axmud::Obj::MapLabelSet
19 # The object that stores a set of text attributes, such as colour, size and style, which apply to
20 # multiple labels
21  
22 { package Games::Axmud::Obj::MapLabel;
23  
24 use strict;
25 use warnings;
26 # use diagnostics;
27  
28 use Glib qw(TRUE FALSE);
29  
30 our @ISA = qw(Games::Axmud);
31  
32 ##################
33 # Constructors
34  
35 sub new {
36  
37 # Creates a new instance of the map label object, which handles a label on the Automapper
38 # window's map
39 #
40 # Expected arguments
41 # $session - The GA::Session which created this object (not stored as an IV)
42 # $name - The initial contents of the label, e.g. 'Town Hall' (no maximum size, and
43 # empty strings are acceptable)
44 # $region - The name of the regionmap to which this label belongs
45 # $xPosPixels, $yPosPixels
46 # - The map coordinates of the pixel in which the top-left corner of the label
47 # is situated
48 # $level - The map level on which the label is drawn (matches
49 # GA::Obj::Regionmap->currentLevel)
50 #
51 # Optional arguments
52 # $style - The name of the map label style to use (a GA::Obj::MapLabelStyle). If
53 # defined, that style is applied to the label's text. If not defined,
54 # the style depends on IVs in this object
55 #
56 # Return values
57 # 'undef' on improper arguments
58 # Blessed reference to the newly-created object on success
59  
60 my (
61 $class, $session, $name, $region, $xPosPixels, $yPosPixels, $level, $style, $check,
62 ) = @_;
63  
64 # Check for improper arguments
65 if (
66 ! defined $class || ! defined $session || ! defined $name || ! defined $region
67 || ! defined $xPosPixels || ! defined $yPosPixels || ! defined $level || defined $check
68 ) {
69 return $axmud::CLIENT->writeImproper($class . '->new', @_);
70 }
71  
72 # Setup
73 my $self = {
74 _objName => $name,
75 _objClass => $class,
76 _parentFile => 'worldmodel',
77 _parentWorld => $session->currentWorld->name,
78 _privFlag => FALSE, # All IVs are public
79  
80 # Object IVs
81 # ----------
82  
83 # The contents of the label, e.g. 'Town Hall'. No maximum size, and newline characters
84 # are acceptable for multi-line labels; but the name must contain at least one non-
85 # space character
86 name => $name,
87 # The label's number in the regionmap (set later by GA::Obj::Regionmap->storeLabel)
88 number => undef,
89 # The name of the regionmap to which this label belongs
90 region => $region,
91 # An ID for the label, so the automapper window can tell apart labels from different
92 # regions. The ID combines ->region and ->number, e.g. 'town_42' (set later by
93 # GA::Obj::Regionmap->storeLabel)
94 id => undef,
95  
96 # The pixel at which the top-left corner of the label is drawn
97 xPosPixels => $xPosPixels,
98 yPosPixels => $yPosPixels,
99 # The map level on which the label is drawn
100 level => $level,
101  
102 # The name of the map label style to use (a GA::Obj::MapLabelStyle)
103 style => $style,
104  
105 # If $self->style is 'undef', a custom style is applied using the following IVs (if
106 # $self->style is set, the following IVs are ignored)
107  
108 # Text colour (an RGB tag like '#ABCDEF', case-insensitive)
109 textColour =>
110 $session->worldModelObj->defaultSchemeObj->mapLabelColour,
111 # Underlay colour (an RGB tag, only used if defined)
112 underlayColour => undef,
113 # Text's relative size (a number in the range 0.5-10, with the default size being 1)
114 relSize => 1,
115 # Style attributes (all values TRUE or FALSE)
116 italicsFlag => FALSE,
117 boldFlag => FALSE,
118 underlineFlag => FALSE,
119 strikeFlag => FALSE,
120 # If TRUE, a box is drawn around the text
121 boxFlag => FALSE,
122 # The angle of rotation (in degrees). 0 represents normal text; acceptable values are
123 # 0-359
124 rotateAngle => 0,
125 };
126  
127 # Bless the object into existence
128 bless $self, $class;
129 return $self;
130 }
131  
132 ##################
133 # Methods
134  
135 ##################
136 # Accessors - set
137  
138 sub set_style {
139  
140 # Setting a style resets the style IVs that are no longer used
141  
142 my ($self, $session, $style, $check) = @_;
143  
144 # Check for improper arguments
145 if (! defined $session || ! defined $style || defined $check) {
146  
147 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_style', @_);
148 }
149  
150 # Update IVs
151 $self->ivPoke('style', $style);
152  
153 $self->ivPoke('textColour', $session->worldModelObj->defaultSchemeObj->mapLabelColour);
154 $self->ivUndef('underlayColour');
155 $self->ivPoke('relSize', 1);
156 $self->ivPoke('italicsFlag', FALSE);
157 $self->ivPoke('boldFlag', FALSE);
158 $self->ivPoke('italicsFlag', FALSE);
159 $self->ivPoke('underlineFlag', FALSE);
160 $self->ivPoke('strikeFlag', FALSE);
161 $self->ivPoke('boxFlag', FALSE);
162 $self->ivPoke('rotateAngle', 0);
163  
164 return 1;
165 }
166  
167 sub reset_style {
168  
169 my ($self, $check) = @_;
170  
171 # Check for improper arguments
172 if (defined $check) {
173  
174 return $axmud::CLIENT->writeImproper($self->_objClass . '->reset_style', @_);
175 }
176  
177 # Update IVs
178 $self->ivUndef('style');
179  
180 return 1;
181 }
182  
183 ##################
184 # Accessors - get
185  
186 sub name
187 { $_[0]->{name} }
188 sub number
189 { $_[0]->{number} }
190 sub region
191 { $_[0]->{region} }
192 sub id
193 { $_[0]->{id} }
194  
195 sub xPosPixels
196 { $_[0]->{xPosPixels} }
197 sub yPosPixels
198 { $_[0]->{yPosPixels} }
199 sub level
200 { $_[0]->{level} }
201  
202 sub style
203 { $_[0]->{style} }
204  
205 sub textColour
206 { $_[0]->{textColour} }
207 sub underlayColour
208 { $_[0]->{underlayColour} }
209 sub relSize
210 { $_[0]->{relSize} }
211 sub italicsFlag
212 { $_[0]->{italicsFlag} }
213 sub boldFlag
214 { $_[0]->{boldFlag} }
215 sub underlineFlag
216 { $_[0]->{underlineFlag} }
217 sub strikeFlag
218 { $_[0]->{strikeFlag} }
219 sub boxFlag
220 { $_[0]->{boxFlag} }
221 sub rotateAngle
222 { $_[0]->{rotateAngle} }
223 }
224  
225 { package Games::Axmud::Obj::MapLabelStyle;
226  
227 use strict;
228 use warnings;
229 # use diagnostics;
230  
231 use Glib qw(TRUE FALSE);
232  
233 our @ISA = qw(Games::Axmud);
234  
235 ##################
236 # Constructors
237  
238 sub new {
239  
240 # Creates a new instance of the map label style object, which stores a set of text
241 # attributes, such as colour, size and style, that apply to muliple labels
242 #
243 # Expected arguments
244 # $session - The GA::Session which created this object (not stored as an IV)
245 # $name - A unique string name for this profile (min 1 char, max 16 chars,
246 # containing any text)
247 #
248 # Optional arguments
249 # $textColour - The text colour to use (an RGB tag like '#ABCDEF', case-insensitive). If
250 # 'undef' or an invalid value, the default text colour is applied
251 # $underlayColour
252 # - The underlay colour to use (an RGB tag like '#ABCDEF', case-insensitive).
253 # If 'undef' or an invalid value, no underlay colour is applied
254 # $relSize - The text's relative size (a number in the range 0.5-10, with the default
255 # size being 1). If 'undef' or an invalid value, the default size is
256 # applied
257 #
258 # Return values
259 # 'undef' on improper arguments
260 # Blessed reference to the newly-created object on success
261  
262 my ($class, $session, $name, $textColour, $underlayColour, $relSize, $check) = @_;
263  
264 # Local variables
265 my ($type, $underlayFlag);
266  
267 # Check for improper arguments
268 if (! defined $class || ! defined $session || ! defined $name || defined $check) {
269  
270 return $axmud::CLIENT->writeImproper($class . '->new', @_);
271 }
272  
273 # Check that $name is valid and not already in use by another set
274 if ($name eq '' || length ($name) > 16) {
275  
276 return $axmud::CLIENT->writeError(
277 'Registry naming error: invalid name \'' . $name . '\'',
278 $class . '->new',
279 );
280 }
281  
282 # Apply default values
283 if (defined $textColour) {
284  
285 ($type, $underlayFlag) = $axmud::CLIENT->checkColourTags($textColour, 'rgb');
286 }
287  
288 if (! defined $textColour || ! $type || $underlayFlag) {
289  
290 if (! $session->worldModelObj) {
291  
292 # World model not created yet; use emergency default
293 $textColour = '#000000';
294  
295 } else {
296  
297 $textColour = $session->worldModelObj->defaultSchemeObj->mapLabelColour;
298 }
299  
300 } else {
301  
302 $textColour = uc($textColour);
303 }
304  
305 if (defined $underlayColour) {
306  
307 ($type, $underlayFlag) = $axmud::CLIENT->checkColourTags($underlayColour, 'rgb');
308  
309 if (! $type || $underlayFlag) {
310  
311 $underlayColour = undef;
312 }
313 }
314  
315 if (! defined $relSize || ! $axmud::CLIENT->floatCheck($relSize, 0.5, 10)) {
316  
317 $relSize = 1;
318 }
319  
320 # Setup
321 my $self = {
322 _objName => $name,
323 _objClass => $class,
324 _parentFile => 'worldmodel',
325 _parentWorld => $session->currentWorld->name,
326 _privFlag => FALSE, # All IVs are public
327  
328 # Object IVs
329 # ----------
330  
331 # A unique string name for this profile (max 16 chars, containing A-Za-z0-9_ - 1st char
332 # can't be number, non-Latin alphabets acceptable. Must not exist as a key in the
333 # global hash of reserved names, $axmud::CLIENT->constReservedHash)
334 name => $name,
335  
336 # Text colour (an RGB tag like '#ABCDEF', case-insensitive)
337 textColour => $textColour,
338 # Underlay colour (an RGB tag, only used if defined)
339 underlayColour => $underlayColour,
340 # Text's relative size (a number in the range 0.5-10, with the default size being 1)
341 relSize => $relSize,
342 # Style attributes (all values TRUE or FALSE)
343 italicsFlag => FALSE,
344 boldFlag => FALSE,
345 underlineFlag => FALSE,
346 strikeFlag => FALSE,
347 # If TRUE, a box is drawn around the text
348 boxFlag => FALSE,
349 # The angle of rotation (in degrees). 0 represents normal text; acceptable values are
350 # 0-359
351 rotateAngle => 0,
352 };
353  
354 # Bless the object into existence
355 bless $self, $class;
356 return $self;
357 }
358  
359 ##################
360 # Methods
361  
362 ##################
363 # Accessors - set
364  
365 ##################
366 # Accessors - get
367  
368 sub name
369 { $_[0]->{name} }
370  
371 sub textColour
372 { $_[0]->{textColour} }
373 sub underlayColour
374 { $_[0]->{underlayColour} }
375 sub relSize
376 { $_[0]->{relSize} }
377 sub italicsFlag
378 { $_[0]->{italicsFlag} }
379 sub boldFlag
380 { $_[0]->{boldFlag} }
381 sub underlineFlag
382 { $_[0]->{underlineFlag} }
383 sub strikeFlag
384 { $_[0]->{strikeFlag} }
385 sub boxFlag
386 { $_[0]->{boxFlag} }
387 sub rotateAngle
388 { $_[0]->{rotateAngle} }
389 }
390  
391 # Package must return a true value
392 1