| 1 | 1 | ▶ | use v5.12; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Wx; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | package App::GUI::Juliagraph::Frame::Panel::ColorPicker; | ||
| 6 | use base qw/Wx::Panel/; | ||
| 7 | use App::GUI::Juliagraph::Widget::ColorDisplay; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ( $class, $parent, $colors ) = @_; | ||
| 11 | return unless ref $parent and ref $colors eq 'HASH'; | ||
| 12 | |||
| 13 | my $self = $class->SUPER::new( $parent, -1 ); | ||
| 14 | |||
| 15 | $self->{'colors'} = { %$colors }; # $frame->{'config'}->get_value('color') | ||
| 16 | $self->{'color_names'} = [ sort keys %{$self->{'colors'}} ]; | ||
| 17 | $self->{'color_index'} = 0; | ||
| 18 | |||
| 19 | my $btnw = 46; my $btnh = 17;# button width and height | ||
| 20 | $self->{'select'} = Wx::ComboBox->new( $self, -1, $self->current_color_name, [-1,-1], [170, -1], $self->{'color_names'}); | ||
| 21 | $self->{'<'} = Wx::Button->new( $self, -1, '<', [-1,-1], [ 30, 17] ); | ||
| 22 | $self->{'>'} = Wx::Button->new( $self, -1, '>', [-1,-1], [ 30, 17] ); | ||
| 23 | $self->{'load'} = Wx::Button->new( $self, -1, 'Load', [-1,-1], [$btnw, $btnh] ); | ||
| 24 | $self->{'del'} = Wx::Button->new( $self, -1, 'Del', [-1,-1], [$btnw, $btnh] ); | ||
| 25 | $self->{'save'} = Wx::Button->new( $self, -1, 'Save', [-1,-1], [$btnw, $btnh] ); | ||
| 26 | $self->{'display'} = App::GUI::Juliagraph::Widget::ColorDisplay->new( $self, 25, 10, 0, $self->get_current_color ); | ||
| 27 | |||
| 28 | $self->{'select'}->SetToolTip("select color in list directly"); | ||
| 29 | $self->{'<'}->SetToolTip("go to previous color in list"); | ||
| 30 | $self->{'>'}->SetToolTip("go to next color in list"); | ||
| 31 | $self->{'load'}->SetToolTip("use displayed color on the right side as color of selected state"); | ||
| 32 | $self->{'save'}->SetToolTip("copy selected state color into color storage"); | ||
| 33 | $self->{'del'}->SetToolTip("delete displayed color from storage"); | ||
| 34 | $self->{'display'}->SetToolTip("color monitor"); | ||
| 35 | |||
| 36 | Wx::Event::EVT_COMBOBOX( $self, $self->{'select'}, sub { | ||
| 37 | my ($win, $evt) = @_; $self->{'color_index'} = $evt->GetInt; $self->update_display }); | ||
| 38 | Wx::Event::EVT_BUTTON( $self, $self->{'<'}, sub { $self->{'color_index'}--; $self->update_display }); | ||
| 39 | Wx::Event::EVT_BUTTON( $self, $self->{'>'}, sub { $self->{'color_index'}++; $self->update_display }); | ||
| 40 | Wx::Event::EVT_BUTTON( $self, $self->{'load'}, sub { $parent->set_current_color( $self->get_current_color ) }); | ||
| 41 | Wx::Event::EVT_BUTTON( $self, $self->{'del'}, sub { | ||
| 42 | delete $self->{'colors'}{ $self->current_color_name }; | ||
| 43 | $self->update_select(); | ||
| 44 | }); | ||
| 45 | Wx::Event::EVT_BUTTON( $self, $self->{'save'}, sub { | ||
| 46 | my $name; | ||
| 47 | while (1){ | ||
| 48 | my $dialog = Wx::TextEntryDialog->new ( $self, "Please insert the color name", 'Request Dialog'); | ||
| 49 | return if $dialog->ShowModal == &Wx::wxID_CANCEL; | ||
| 50 | $name = $dialog->GetValue(); | ||
| 51 | last unless exists $self->{'colors'}{ $name }; | ||
| 52 | } | ||
| 53 | $self->{'colors'}{ $name } = [ $self->GetParent->get_current_color->rgb ]; | ||
| 54 | $self->update_select(); | ||
| 55 | for (0 .. $#{$self->{'color_names'}}){ | ||
| 56 | $self->{'color_index'} = $_ if $name eq $self->{'color_names'}[$_]; | ||
| 57 | } | ||
| 58 | $self->update_display(); | ||
| 59 | }); | ||
| 60 | |||
| 61 | my $std_attr = &Wx::wxALIGN_LEFT | &Wx::wxALIGN_CENTER_VERTICAL| &Wx::wxGROW; | ||
| 62 | my $tb_attr = $std_attr | &Wx::wxTOP| &Wx::wxBOTTOM; | ||
| 63 | my $button_attr = &Wx::wxLEFT | $tb_attr; | ||
| 64 | my $sizer = Wx::BoxSizer->new(&Wx::wxHORIZONTAL); | ||
| 65 | $sizer->AddSpacer( 5 ); | ||
| 66 | $sizer->Add( $self->{'del'}, 0, $button_attr, 10 ); | ||
| 67 | $sizer->AddSpacer( 15 ); | ||
| 68 | $sizer->Add( $self->{'select'}, 0, $tb_attr, 10 ); | ||
| 69 | $sizer->Add( $self->{'<'}, 0, $tb_attr, 10 ); | ||
| 70 | $sizer->Add( $self->{'>'}, 0, $tb_attr, 10 ); | ||
| 71 | $sizer->AddSpacer( 15 ); | ||
| 72 | $sizer->Add( $self->{'display'}, 0, $tb_attr, 10); | ||
| 73 | $sizer->AddSpacer( 10 ); | ||
| 74 | $sizer->Add( $self->{'load'}, 0, $button_attr, 10 ); | ||
| 75 | $sizer->AddSpacer( 5 ); | ||
| 76 | $sizer->Add( $self->{'save'}, 0, $button_attr, 10 ); | ||
| 77 | $sizer->Add( 0, 0, &Wx::wxEXPAND | &Wx::wxGROW); | ||
| 78 | $self->SetSizer($sizer); | ||
| 79 | |||
| 80 | $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub current_color_name { $_[0]->{'color_names'}->[ $_[0]->{'color_index'} ] } | ||
| 84 | |||
| 85 | sub get_current_color { | ||
| 86 | my ( $self ) = @_; | ||
| 87 | my $color = $self->{'colors'}->{ $self->current_color_name }; | ||
| 88 | {red=> $color->[0], green=> $color->[1], blue=> $color->[2] }; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub update_select { | ||
| 92 | my ( $self ) = @_; | ||
| 93 | $self->{'color_names'} = [ sort keys %{$self->{'colors'}} ]; | ||
| 94 | $self->{'select'}->Clear (); | ||
| 95 | $self->{'select'}->Append( $_) for @{$self->{'color_names'}}; | ||
| 96 | $self->update_display(); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub update_display { | ||
| 100 | my ( $self ) = @_; | ||
| 101 | $self->{'color_index'} = $#{$self->{'color_names'}} if $self->{'color_index'} < 0; | ||
| 102 | $self->{'color_index'} = 0 if $self->{'color_index'} > $#{$self->{'color_names'}}; | ||
| 103 | $self->{'select'}->SetSelection( $self->{'color_index'} ); | ||
| 104 | $self->{'display'}->set_color( $self->get_current_color ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub get_config { $_[0]->{'colors'} } | ||
| 108 | |||
| 109 | 1; | ||