| 1 | package TUI::ColorSel::ColorSelector; | ||||||||||||||
| 2 | # ABSTRACT: interactive color selection view | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 6 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | our $VERSION = '2.000003'; | |||||||||||||
| 10 | 6 | $VERSION =~ tr/_//d; | |||||||||||||
| 11 | 6 | our $AUTHORITY = 'cpan:BRICKPOOL'; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 6 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 6 | our @EXPORT = qw( | |||||||||||||
| 15 | TColorSelector | ||||||||||||||
| 16 | new_TColorSelector | ||||||||||||||
| 17 | ); | ||||||||||||||
| 18 | |||||||||||||||
| 19 | 6 | ▶ | use Storable; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 6 | ▶ | use TUI::toolkit; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 6 | ▶ | use TUI::toolkit::Types qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | is_Object | ||||||||||||||
| 23 | :types | ||||||||||||||
| 24 | 6 | ); | |||||||||||||
| 25 | |||||||||||||||
| 26 | 6 | ▶ | use TUI::Drivers::Const qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 27 | :evXXXX | ||||||||||||||
| 28 | kbLeft | ||||||||||||||
| 29 | kbRight | ||||||||||||||
| 30 | kbUp | ||||||||||||||
| 31 | kbDown | ||||||||||||||
| 32 | 6 | ); | |||||||||||||
| 33 | 6 | ▶ | use TUI::Drivers::HardwareInfo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 6 | ▶ | use TUI::Drivers::Util qw( ctrlToArrow ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 6 | ▶ | use TUI::Objects::Rect; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 6 | ▶ | use TUI::ColorSel::Const qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | :cmXXXX | ||||||||||||||
| 38 | :csXXXX | ||||||||||||||
| 39 | 6 | ); | |||||||||||||
| 40 | 6 | ▶ | use TUI::Views::Const qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | ofSelectable | ||||||||||||||
| 42 | ofFirstClick | ||||||||||||||
| 43 | ofFramed | ||||||||||||||
| 44 | 6 | ); | |||||||||||||
| 45 | 6 | ▶ | use TUI::Views::DrawBuffer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 6 | ▶ | use TUI::Views::View; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | 6 | ▶ | use TUI::Views::Util qw( message ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | sub TColorSelector() { __PACKAGE__ } | ||||||||||||||
| 50 | sub name() { 'TColorSelector' } | ||||||||||||||
| 51 | 0 | ▶ | sub new_TColorSelector { __PACKAGE__->from(@_) } | ||||||||||||
|
Subroutine
new_TColorSelector: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | 6 | extends TView; | |||||||||||||
| 54 | |||||||||||||||
| 55 | # declare global variables | ||||||||||||||
| 56 | 6 | our $icon = "\xDB"; # cp437: "█" | |||||||||||||
| 57 | # our $icon = encode( cp437 => "█" ); | ||||||||||||||
| 58 | |||||||||||||||
| 59 | # protected attributes | ||||||||||||||
| 60 | 6 | has color => ( is => 'ro', default => 0 ); | |||||||||||||
| 61 | 6 | has selType => ( is => 'ro', default => csBackground ); | |||||||||||||
| 62 | |||||||||||||||
| 63 | # predeclare private methods | ||||||||||||||
| 64 | my ( | ||||||||||||||
| 65 | 6 | $colorChanged, | |||||||||||||
| 66 | ); | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | ▶ | sub BUILDARGS { # \%args (%args) | ||||||||||||
|
Subroutine
BUILDARGS: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 69 | 0 | state $sig = signature( | |||||||||||||
| 70 | method => 1, | ||||||||||||||
| 71 | named => [ | ||||||||||||||
| 72 | bounds => Object, | ||||||||||||||
| 73 | selType => PositiveOrZeroInt, { alias => 'aSelType' }, | ||||||||||||||
| 74 | ], | ||||||||||||||
| 75 | caller_level => +1, | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | 0 | my ( $class, $args ) = $sig->( @_ ); | |||||||||||||
| 78 | 0 | return { %$args }; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 0 | ▶ | sub BUILD { # void (\%args) | ||||||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 82 | 0 | my ( $self, $args ) = @_; | |||||||||||||
| 83 | 0 | ▶ | assert ( @_ == 2 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | ▶ | assert ( is_Object $self ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | $self->{options} |= ofSelectable | ofFirstClick | ofFramed; | |||||||||||||
| 86 | 0 | $self->{eventMask} |= evBroadcast; | |||||||||||||
| 87 | 0 | return; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | ▶ | sub from { # $obj ($bounds, $aSelType) | ||||||||||||
|
Subroutine
from: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 91 | 0 | state $sig = signature( | |||||||||||||
| 92 | method => 1, | ||||||||||||||
| 93 | pos => [Object, Str], | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | 0 | my ( $class, @args ) = $sig->( @_ ); | |||||||||||||
| 96 | 0 | return $class->new( bounds => $args[0], selType => $args[1] ); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | ▶ | sub draw { # void () | ||||||||||||
|
Subroutine
draw: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 100 | 0 | state $sig = signature( | |||||||||||||
| 101 | method => Object, | ||||||||||||||
| 102 | pos => [], | ||||||||||||||
| 103 | ); | ||||||||||||||
| 104 | 0 | my ( $self ) = $sig->( @_ ); | |||||||||||||
| 105 | 0 | my $b = TDrawBuffer->new(); | |||||||||||||
| 106 | 0 | $b->moveChar( 0, ' ', 0x70, $self->{size}{x} ); | |||||||||||||
| 107 | 0 | ▶ | for ( my $i = 0 ; $i <= $self->{size}{y} ; $i++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 108 | 0 | ▶ | if ( $i < 4 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 0 | ▶ | for ( my $j = 0 ; $j < 4 ; $j++ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 0 | my $c = $i * 4 + $j; | |||||||||||||
| 111 | 0 | $b->moveChar( $j * 3, $icon, $c, 3 ); | |||||||||||||
| 112 | 0 | ▶ | if ( $c == $self->{color} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | # We do not use the INVERSE BULLET "\x8" (U+25D8) character, because | ||||||||||||||
| 114 | # some Unicode fonts display the character differently than IBM437. | ||||||||||||||
| 115 | 0 | ▶ | if ( THardwareInfo->getColorCount() > 8 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | # Use the BULLET ("\x7", U+2022) character and invert the color. | ||||||||||||||
| 117 | 0 | $b->putChar( $j * 3 + 1, "\x7" ); | |||||||||||||
| 118 | 0 | ▶ | $b->putAttribute( $j * 3 + 1, $c == 0 ? 0x07 : ( $c << 4 ) ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | } | ||||||||||||||
| 120 | else { | ||||||||||||||
| 121 | # Use the UPPER HALF BLOCK ("\xDF", U+2580) character | ||||||||||||||
| 122 | # if only 8 colors are available. | ||||||||||||||
| 123 | 0 | $b->putChar( $j * 3 + 1, "\xdf" ); | |||||||||||||
| 124 | 0 | ▶ | $b->putAttribute( $j * 3 + 1, $c == 0 ? 0x70 : $c ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | } | ||||||||||||||
| 126 | } | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | 0 | $self->writeLine( 0, $i, $self->{size}{x}, 1, $b ); | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | 0 | return; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 0 | ▶ | sub handleEvent { # void ($event) | ||||||||||||
|
Subroutine
handleEvent: not called CC=21 SCAR=59.6
Pod
documented
| |||||||||||||||
| 135 | 6 | ▶ | no warnings 'uninitialized'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 136 | 0 | state $sig = signature( | |||||||||||||
| 137 | method => Object, | ||||||||||||||
| 138 | pos => [Object], | ||||||||||||||
| 139 | ); | ||||||||||||||
| 140 | 0 | my ( $self, $event ) = $sig->( @_ ); | |||||||||||||
| 141 | |||||||||||||||
| 142 | 6 | ▶ | use constant width => 4; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | 0 | $self->SUPER::handleEvent( $event ); | |||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | my $oldColor = $self->{color}; | |||||||||||||
| 147 | 0 | ▶ | my $maxCol = $self->{selType} == csBackground ? 7 : 15; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | 0 | SWITCH: for ( $event->{what} ) { | |||||||||||||
| 149 | 0 | ▶ | evMouseDown == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 0 | ▶ | do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | 0 | ▶ | if ( $self->mouseInView( $event->{mouse}{where} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | my $mouse = $self->makeLocal( $event->{mouse}{where} ); | |||||||||||||
| 153 | 0 | $self->{color} = $mouse->{y} * 4 + int( $mouse->{x} / 3 ); | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | else { | ||||||||||||||
| 156 | 0 | $self->{color} = $oldColor; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | 0 | $self->$colorChanged(); | |||||||||||||
| 159 | 0 | $self->drawView(); | |||||||||||||
| 160 | } while ( $self->mouseEvent( $event, evMouseMove ) ); | ||||||||||||||
| 161 | 0 | last; | |||||||||||||
| 162 | }; | ||||||||||||||
| 163 | 0 | ▶ | evKeyDown == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 0 | SWITCH: for ( ctrlToArrow( $event->{keyDown}{keyCode} ) ) { | |||||||||||||
| 165 | 0 | ▶ | kbLeft == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | 0 | ▶ | if ( $self->{color} > 0 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 167 | 0 | $self->{color}--; | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | else { | ||||||||||||||
| 170 | 0 | $self->{color} = $maxCol; | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | 0 | last; | |||||||||||||
| 173 | }; | ||||||||||||||
| 174 | 0 | ▶ | kbRight == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | 0 | ▶ | if ( $self->{color} < $maxCol ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | 0 | $self->{color}++; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | else { | ||||||||||||||
| 179 | 0 | $self->{color} = 0; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | 0 | last; | |||||||||||||
| 182 | }; | ||||||||||||||
| 183 | 0 | ▶ | kbUp == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 184 | 0 | ▶ | if ( $self->{color} > width- 1 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 185 | 0 | $self->{color} -= width; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | elsif ( $self->{color} == 0 ) { | ||||||||||||||
| 188 | 0 | $self->{color} = $maxCol; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | else { | ||||||||||||||
| 191 | 0 | $self->{color} += $maxCol - width; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | 0 | last; | |||||||||||||
| 194 | }; | ||||||||||||||
| 195 | 0 | ▶ | kbDown == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | 0 | ▶ | if ( $self->{color} < $maxCol - ( width- 1 ) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 197 | 0 | $self->{color} += width; | |||||||||||||
| 198 | } | ||||||||||||||
| 199 | elsif ( $self->{color} == $maxCol ) { | ||||||||||||||
| 200 | 0 | $self->{color} = 0; | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | else { | ||||||||||||||
| 203 | 0 | $self->{color} -= $maxCol - width; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | 0 | last; | |||||||||||||
| 206 | }; | ||||||||||||||
| 207 | DEFAULT: { | ||||||||||||||
| 208 | 0 | return; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | } #/ SWITCH: for ( ctrlToArrow( $event...)) | ||||||||||||||
| 211 | 0 | last; | |||||||||||||
| 212 | }; | ||||||||||||||
| 213 | 0 | ▶ | evBroadcast == $_ and do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 214 | 0 | ▶ | if ( $event->{message}{command} == cmColorSet ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 215 | 0 | ▶ | if ( $self->{selType} == csBackground ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 216 | 0 | $self->{color} = $event->{message}{infoByte} >> 4; | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | else { | ||||||||||||||
| 219 | 0 | $self->{color} = $event->{message}{infoByte} & 0x0f; | |||||||||||||
| 220 | } | ||||||||||||||
| 221 | 0 | $self->drawView(); | |||||||||||||
| 222 | 0 | return; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | else { | ||||||||||||||
| 225 | 0 | return; | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | }; | ||||||||||||||
| 228 | DEFAULT: { | ||||||||||||||
| 229 | 0 | return; | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | } | ||||||||||||||
| 232 | 0 | $self->drawView(); | |||||||||||||
| 233 | 0 | $self->$colorChanged(); | |||||||||||||
| 234 | 0 | $self->clearEvent( $event ); | |||||||||||||
| 235 | 0 | return; | |||||||||||||
| 236 | } | ||||||||||||||
| 237 | |||||||||||||||
| 238 | 0 | ▶ | $colorChanged = sub { # void () | ||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||
| 239 | 0 | my ( $self ) = @_; | |||||||||||||
| 240 | 0 | ▶ | assert ( @_ == 1 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 241 | 0 | ▶ | assert ( is_Object $self ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 242 | 0 | ▶ | my $msg = $self->{selType} == csForeground | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | ? cmColorForegroundChanged | ||||||||||||||
| 244 | : cmColorBackgroundChanged; | ||||||||||||||
| 245 | 0 | message( $self->{owner}, evBroadcast, $msg, 0+ $self->{color} ); | |||||||||||||
| 246 | 0 | return; | |||||||||||||
| 247 | 6 | }; | |||||||||||||
| 248 | |||||||||||||||
| 249 | 0 | ▶ | sub write :method { # void ($os) | ||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 250 | 0 | state $sig = signature( | |||||||||||||
| 251 | method => Object, | ||||||||||||||
| 252 | pos => [Object], | ||||||||||||||
| 253 | ); | ||||||||||||||
| 254 | 0 | my ( $self, $os ) = $sig->( @_ ); | |||||||||||||
| 255 | 0 | $self->SUPER::write( $os ); | |||||||||||||
| 256 | $os->put( | ||||||||||||||
| 257 | $self->{color}, | ||||||||||||||
| 258 | $self->{selType} | ||||||||||||||
| 259 | 0 | ); | |||||||||||||
| 260 | 0 | return; | |||||||||||||
| 261 | } | ||||||||||||||
| 262 | |||||||||||||||
| 263 | 0 | ▶ | sub read :method { # $self ($is) | ||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 264 | 0 | state $sig = signature( | |||||||||||||
| 265 | method => Object, | ||||||||||||||
| 266 | pos => [Object], | ||||||||||||||
| 267 | ); | ||||||||||||||
| 268 | 0 | my ( $self, $is ) = $sig->( @_ ); | |||||||||||||
| 269 | 0 | $self->SUPER::read( $is ); | |||||||||||||
| 270 | 0 | $self->{color} = $is->get(); | |||||||||||||
| 271 | 0 | $self->{selType} = $is->get(); | |||||||||||||
| 272 | 0 | return $self; | |||||||||||||
| 273 | } | ||||||||||||||
| 274 | |||||||||||||||
| 275 | 0 | ▶ | sub build { # $obj () | ||||||||||||
|
Subroutine
build: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 276 | 0 | my ( $class ) = @_; | |||||||||||||
| 277 | 0 | ▶ | assert ( @_ == 1 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 278 | 0 | ▶ | assert ( is_Str $class ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 279 | 0 | ▶ | assert ( Storable::is_retrieving ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 280 | 0 | return $class->new( | |||||||||||||
| 281 | bounds => TRect->new(), | ||||||||||||||
| 282 | selType => csBackground, | ||||||||||||||
| 283 | ); | ||||||||||||||
| 284 | } | ||||||||||||||
| 285 | |||||||||||||||
| 286 | 6 | 1 | |||||||||||||
| 287 | |||||||||||||||
| 288 | __END__ | ||||||||||||||