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

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 175 CC 22 SCAR 43.5
CC
22
Cov
0%
repair
182.0
new
42.0
setupStandardColourScheme
30.0
CRAP 77.6
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::ColourScheme
16 # Colour scheme object, used by pane objects (GA::Table::Pane) to set colours and fonts in the pane
17 # object's textview(s)
18  
19 { package Games::Axmud::Obj::ColourScheme;
20  
21 use strict;
22 use warnings;
23 # use diagnostics;
24  
25 use Glib qw(TRUE FALSE);
26  
27 our @ISA = qw(Games::Axmud);
28  
29 ##################
30 # Constructors
31  
32 sub new {
33  
34 # Called by GA::Client->createStandardColourSchemes or GA::Cmd::AddColourScheme->do
35 # Creates the GA::Obj::ColourScheme, used by pane objects (GA::Table::Pane) to set colours
36 # and fonts in the pane object's textview(s)
37 #
38 # Expected arguments
39 # $name - Unique name for this colour scheme (max 16 chars, containing
40 # A-Za-z0-9_ - 1st char can't be number, non-Latin alphabets acceptable.
41 # Must not exist as a key in the global hash of reserved names,
42 # $axmud::CLIENT->constReservedHash)
43 #
44 # Return values
45 # 'undef' on improper arguments or if $name is invalid
46 # Blessed reference to the newly-created object on success
47  
48 my ($class, $name, $check) = @_;
49  
50 # Check for improper arguments
51 if (! defined $class || ! defined $name || defined $check) {
52  
53 return $axmud::CLIENT->writeImproper($class . '->new', @_);
54 }
55  
56 # Check that $name is valid and not already in use by another colour scheme
57 # NB There is a colour scheme for every type of 'grid' or 'free' window (except 'dialogue'
58 # windows), with the same name as the window object's ->winType, and those names would
59 # normally be refused as invalid by GA::Client->nameCheck
60 if (
61 ! $axmud::CLIENT->ivExists('constGridWinTypeHash', $name)
62 && ! $axmud::CLIENT->ivExists('constFreeWinTypeHash', $name)
63 && ! $axmud::CLIENT->nameCheck($name, 16)
64 ) {
65 return $axmud::CLIENT->writeError(
66 'Registry naming error: invalid name \'' . $name . '\'',
67 $class . '->new',
68 );
69  
70 } elsif ($axmud::CLIENT->ivExists('colourSchemeHash', $name)) {
71  
72 return $axmud::CLIENT->writeError(
73 'Registry naming error: colour scheme \'' . $name . '\' already exists',
74 $class . '->new',
75 );
76 }
77  
78 # Setup
79 my $self = {
80 _objName => 'colour_scheme_' . $name,
81 _objClass => $class,
82 _parentFile => 'winmaps',
83 _parentWorld => undef, # No parent world object
84 _privFlag => FALSE, # All IVs are public
85  
86 # IVs
87 # ---
88  
89 # Unique name for this colour scheme (max 16 chars)
90 name => $name,
91  
92 # The colour scheme. Each value is an Axmud colour tag
93 textColour => $axmud::CLIENT->constTextColour,
94 underlayColour => $axmud::CLIENT->constUnderlayColour,
95 backgroundColour => $axmud::CLIENT->constBackgroundColour,
96 # The font and fontsize
97 font => $axmud::CLIENT->constFont,
98 fontSize => $axmud::CLIENT->constFontSize,
99  
100 # Word wrapping setting - 'no_wrap', 'wrap_char', 'wrap_word', 'wrap_word_char'
101 # NB Changing this setting may not change the appearance of a 'main' window's textviews
102 # because NAWS tells the world the size of the textview, and the world itself wraps
103 # text
104 wrapMode => 'wrap_word_char',
105  
106 # The user can specify that certain Axmud colour tags (standard, xterm or RGB) can be
107 # overridden with a different tag or ignored altogether, when displayed in a textview
108 # object (GA::Obj::TextView)
109 # The overrides do not apply to the textview object's default text, underlay and
110 # background colours, stored in GA::Obj::TextView->textColour, ->underlayColour and
111 # ->backgroundColour. These default colours might have been set from a colour scheme
112 # like this one, or might have been set when applying/releasing the textview object's
113 # monochrome mode
114 # In this hash, the key is any Axmud colour tag (xterm and RGB tags should be converted
115 # to upper-case). The corresponding value is the replacement colour tag, or 'undef'
116 # if the colour tag should be ignored altogether
117 overrideHash => {},
118 # Flag set to TRUE if all Axmud colour tags should be ignored altogether, leaving the
119 # textview object able to use only its default text, underlay and background colours
120 overrideAllFlag => FALSE,
121 };
122  
123 # Bless the object into existence
124 bless $self, $class;
125  
126 # If this is a standard colour scheme (with the same name as a type of 'grid' or 'free'
127 # window), set the default colours/fonts
128 if (
129 $axmud::CLIENT->ivExists('constWinmapNameHash', $name)
130 || $axmud::CLIENT->ivExists('constFreeWinTypeHash', $name)
131 ) {
132 if (! $self->setupStandardColourScheme()) {
133  
134 return $axmud::CLIENT->writeError(
135 'Can\'t set up the standard colour scheme \'' . $name . '\'',
136 $class . '->new',
137 );
138 }
139 }
140  
141 return $self;
142 }
143  
144 ##################
145 # Methods
146  
147 sub setupStandardColourScheme {
148  
149 # Called by $self->new whenever the specified name of the colour scheme is the name of a
150 # type of 'grid' or 'free' window
151 # (Could be called by any other function in order to reset a standard colour scheme)
152 # Sets default colours/fonts for this colour scheme
153 #
154 # Return values
155 # 'undef' on improper arguments or if $name isn't one of the standard colour scheme names
156 # 1 otherwise
157  
158 my ($self, $check) = @_;
159  
160 # Check for improper arguments
161 if (defined $check) {
162  
163 return $axmud::CLIENT->writeImproper(
164 $self->_objClass . '->setupStandardColourScheme',
165 @_,
166 );
167 }
168  
169 # Default colour schemes for 'grid' windows
170 if ($axmud::CLIENT->ivExists('constGridWinTypeHash', $self->name)) {
171  
172 $self->ivPoke('textColour', $axmud::CLIENT->constTextColour);
173 $self->ivPoke('backgroundColour', $axmud::CLIENT->constBackgroundColour);
174 $self->ivPoke('font', $axmud::CLIENT->constFont);
175 $self->ivPoke('fontSize', $axmud::CLIENT->constFontSize);
176  
177 return 1;
178  
179 # Default colour schemes for 'free' windows
180 } elsif ($axmud::CLIENT->ivExists('constFreeWinTypeHash', $self->name)) {
181  
182 $self->ivPoke('textColour', '#000000');
183 $self->ivPoke('backgroundColour', '#FFFFFF');
184 $self->ivPoke('font', $axmud::CLIENT->constFont);
185  
186 if ($self->name eq 'viewer') {
187  
188 # (Fit a little more help text into the data viewer window)
189 $self->ivPoke('fontSize', ($axmud::CLIENT->constFontSize - 1));
190  
191 } else {
192  
193 $self->ivPoke('fontSize', $axmud::CLIENT->constFontSize);
194 }
195  
196 return 1;
197  
198 } else {
199  
200 # Not a standard colour scheme
201 return undef;
202 }
203 }
204  
205 ##################
206 # Accessors - set
207  
208 sub repair {
209  
210 # Can be called by anything that's about to apply a colour scheme to a window (for example,
211 # called by Games::Axmud::Cmd::UpdateColourScheme->do
212 # Checks that the scheme's IVs are valid and attempts to repair any problems; incorrect
213 # colour tags could cause multiple Gtk errors or even a crash
214 #
215 # Checks that the underlay colour uses an underlay colour tag and not a normal text colour
216 # tag. If so, converts one to the other
217 # Checks that the colour tags are recognised and replaced them with default ones, if not
218 # Checks that the font size is an actual integer
219 #
220 # Expected arguments
221 # (none besides $self)
222 #
223 # Return values
224 # 'undef' on improper arguments
225 # 1 otherwise
226  
227 my ($self, $check) = @_;
228  
229 # Local variables
230 my ($type, $underlayFlag);
231  
232 # Check for improper arguments
233 if (defined $check) {
234  
235 return $axmud::CLIENT->writeImproper($self->_objClass . '->repair', @_);
236 }
237  
238 if (! defined $self->textColour) {
239  
240 $self->ivPoke('textColour', $axmud::CLIENT->constTextColour);
241  
242 } else {
243  
244 ($type, $underlayFlag) = $axmud::CLIENT->checkColourTags($self->textColour);
245 if (! $type) {
246  
247 # Invalid colour tag
248 $self->ivPoke('textColour', $axmud::CLIENT->constTextColour);
249  
250 } elsif ($underlayFlag) {
251  
252 # Underlay colour tag; convert it
253 $self->ivPoke('textColour', $axmud::CLIENT->swapColours($self->textColour));
254 }
255 }
256  
257 if (! defined $self->underlayColour) {
258  
259 $self->ivPoke('underlayColour', $axmud::CLIENT->constUnderlayColour);
260  
261 } else {
262  
263 ($type, $underlayFlag) = $axmud::CLIENT->checkColourTags($self->underlayColour);
264 if (! $type) {
265  
266 # Invalid colour tag
267 $self->ivPoke('underlayColour', $axmud::CLIENT->constUnderlayColour);
268  
269 } elsif (! $underlayFlag) {
270  
271 # Text colour tag; convert it
272 $self->ivPoke('underlayColour', $axmud::CLIENT->swapColours($self->underlayColour));
273 }
274 }
275  
276 if (! defined $self->backgroundColour) {
277  
278 $self->ivPoke('backgroundColour', $axmud::CLIENT->constBackgroundColour);
279  
280 } else {
281  
282 ($type, $underlayFlag) = $axmud::CLIENT->checkColourTags($self->backgroundColour);
283 if (! $type) {
284  
285 # Invalid colour tag
286 $self->ivPoke('backgroundColour', $axmud::CLIENT->constBackgroundColour);
287  
288 } elsif ($underlayFlag) {
289  
290 # Underlay colour tag; convert it
291 $self->ivPoke(
292 'backgroundColour',
293 $axmud::CLIENT->swapColours($self->backgroundColour),
294 );
295 }
296 }
297  
298 if (! defined $self->font) {
299  
300 $self->ivPoke('font', $axmud::CLIENT->constFont);
301  
302 }
303  
304 if (! defined $self->fontSize || ! $axmud::CLIENT->floatCheck($self->fontSize, 0)) {
305  
306 $self->ivPoke('fontSize', $axmud::CLIENT->constFontSize);
307 }
308  
309 # Operation complete
310 return 1;
311 }
312  
313 ##################
314 # Accessors - get
315  
316 sub name
317 { $_[0]->{name} }
318  
319 sub textColour
320 { $_[0]->{textColour} }
321 sub underlayColour
322 { $_[0]->{underlayColour} }
323 sub backgroundColour
324 { $_[0]->{backgroundColour} }
325 sub font
326 { $_[0]->{font} }
327 sub fontSize
328 { $_[0]->{fontSize} }
329  
330 sub wrapMode
331 { $_[0]->{wrapMode} }
332  
333 sub overrideHash
334 { my $self = shift; return %{$self->{overrideHash}}; }
335 sub overrideAllFlag
336 { $_[0]->{overrideAllFlag} }
337 }
338  
339 # Package must return a true value
340 1