Games-Axmud

 view release on metacpan or  search on metacpan

lib/Games/Axmud/Task.pm  view on Meta::CPAN

                $tooltips = 'Click to send \'' . $cmd . '\' (' . $hint . ')';
            } else {
                $tooltips = 'Command not set (' . $hint . ')';
            }

            $thisTableObj = $self->winObj->tableStripObj->addTableObj(
                'Games::Axmud::Table::Button',
                $left, $right, $top, $bottom,
                undef,
                # Init settings
                'func'                      => $self->getMethodRef('cmdCallback'),
                'id'                        => $keycode,
                'text'                      => $self->shortenText($cmd, 12),
                'tooltips'                  => $tooltips,
            );

            $cmdHash{$thisTableObj} = $cmd;
            $cmdKeycodeHash{$keycode} = $thisTableObj;

        } until (! @list);

        # Store all the buttons created, so that $self->moveCallback and ->cmdCallback can work
        #   out which Gtk3::Button corresponds to which command
        $self->ivPoke('compassHash', %compassHash);
        $self->ivPoke('cmdHash', %cmdHash);
        $self->ivPoke('cmdKeycodeHash', %cmdKeycodeHash);

        # Create two radiobuttons to enable/disable the use of the keypad
        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Label',
            30, 39, 0, 11,
            undef,
            # Init settings
            'text'          => 'Keypad',
            'align_y'       => 0.5,
        );

        $radioTableObj = $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::RadioButton',
            40, 49, 0, 11,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('radioButtonCallback'),
            'id'            => 'button_1',
            'text'          => 'Disable',
            'select_flag'   => FALSE,
        );

        $radioTableObj2 = $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::RadioButton',
            50, 59, 0, 11,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('radioButtonCallback'),
            'id'            => 'button_2',
            'text'          => 'Enable',
            'select_flag'   => FALSE,
            'group'         => $radioTableObj->group,
        );

        # Create a combobox and an entry so that the user can set new values in $self->keypadCmdHash
        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Label',
            30, 39, 12, 23,
            undef,
            # Init settings
            'text'      => 'Keycode',
        );

        @comboList = (
            'kp_0',
            'kp_5',
            'kp_multiply',
            'kp_divide',
            'kp_full_stop',
            'kp_enter',
        );
        $comboTableObj = $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::ComboBox',
            40, 59, 12, 23,
            undef,
            # Init settings
            'title'     => 'Select keycode',
            'list_ref'  => \@comboList,
        );

        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Label',
            30, 39, 24, 35,
            undef,
            # Init settings
            'text'      => 'Command',
        );

        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Entry',
            40, 59, 24, 35,
            undef,
            # Init settings
            'func'      => $self->getMethodRef('miniEntryCallback'),
        );

        # Store the table objects for the benefit of the callback functions
        $self->ivPoke('radioTableObj', $radioTableObj);
        $self->ivPoke('radioTableObj2', $radioTableObj2);
        $self->ivPoke('comboTableObj', $comboTableObj);

        # Display all the widgets
        $self->winObj->winShowAll($self->_objClass . '->createWidgets');

        return 1;
    }

    sub shortenText {

        # Called by $self->createWidgets and ->entryCallback
        # If the buttons are assigned very long labels, the task window gets stretched. Prevent this
        #   (hopefully) but limiting the size of the text assigned to button labels
        # Also converts the modified text to upper-case characters, before returning it
        #
        # Expected arguments

lib/Games/Axmud/Task.pm  view on Meta::CPAN


        } else {

            $cmd = $self->ivShow('cmdHash', $tableObj);
            if (! $cmd) {

                # Unrecognised command (or no command set)
                return undef;

            } else {

                # Send the command (interpolation has already been done)
                $self->session->worldCmd($cmd);
                return 1;
            }
        }
    }

    sub radioButtonCallback {

        # Called by a ->signal_connect in a GA::Table::RadioButton object when the user clicks on
        #   one of the radio buttons
        # Enables or disable the keypad macros, as appropriate
        #
        # Expected arguments
        #   $tableObj       - The GA::Table::RadioButton object for the button
        #   $radioButton    - The actual Gtk3::RadioButton clicked
        #   $id             - The callback ID; in this case, the strings 'button_1' or 'button_2'
        #
        # Return values
        #   'undef' on improper arguments
        #   1 otherwise

        my ($self, $tableObj, $radioButton, $id, $check) = @_;

        # Check for improper arguments
        if (! defined $tableObj || ! defined $radioButton || ! defined $id || defined $check) {

            return $axmud::CLIENT->writeImproper($self->_objClass . '->radioButtonCallback', @_);
        }

        # Work out which button was clicked
        if ($id eq 'button_1') {

            # Disable the keypad macros. The TRUE argument means 'don't modify the widget'
            $self->disable(TRUE);

        } elsif ($id eq 'button_2') {

            # Otherwise, enable the keypad macros
            $self->enable(TRUE);
        }

        return 1;
    }

    sub miniEntryCallback {

        # Called by a ->signal_connect in a GA::Table::Entry object when the user types something
        #   in the entry box and presses ENTER
        # Finds the keycode currently displayed in the combobox and updates both the macro and the
        #   Gtk3::Button for that keycode
        #
        # Expected arguments
        #   $entryTableObj  - The GA::Table::Entry object for the entry
        #   $entry          - The actual Gtk3::Entry used
        #   $id             - The callback ID; in this case, an empty string
        #   $text           - The text entered by the user (may be an empty string)
        #
        # Return values
        #   'undef' on improper arguments, if no keycode has been selected in the combobox, if
        #       nothing has been typed in the entry box or for a general error
        #   1 otherwise

        my ($self, $entryTableObj, $entry, $id, $text, $check) = @_;

        # Local variables
        my ($cmd, $keycode, $buttonTableObj, $interfaceObj);

        # Check for improper arguments
        if (
            ! defined $entryTableObj || ! defined $entry || ! defined $id  || ! defined $text
            || defined $check
        ) {
            return $axmud::CLIENT->writeImproper($self->_objClass . '->miniEntryCallback', @_);
        }

        # Get the text entered in the Gtk3::Entry and trim leading/trailing whitespace
        $cmd = $axmud::CLIENT->trimWhitespace($text);

        # Retrieve the last value selected in the combobox, which will be a standard keycode
        $keycode = $self->comboTableObj->get_text();

        if (! defined $keycode || ! $self->ivExists('keypadCmdHash', $keycode) || ! $cmd) {

            # No selection made in the combobox. Reset both the entry and the combobox
            $entryTableObj->set_text();
            $self->comboTableObj->resetCombo();

            return undef;
        }

        # Update the main IV
        $self->ivAdd('keypadCmdHash', $keycode, $cmd);

        # Find the corresponding button
        $buttonTableObj = $self->ivShow('cmdKeycodeHash', $keycode);

        # Change the button's label, and the command sent when the button is clicked (max text
        #   length is 12)
        $buttonTableObj->set_text($self->shortenText($cmd, 12));
        $self->ivAdd('cmdHash', $buttonTableObj, $cmd);

        # Change the button's tooltips
        $buttonTableObj->set_tooltips(
            'Click to send \'' . $cmd . '\' (' . $self->ivShow('keypadHintHash', $keycode) . ')',
        );

        # Find the macro interface corresponding to the selected standard keycode
        $interfaceObj = $self->ivShow('macroHash', $keycode);
        if ($interfaceObj) {

            # Modify the macro to send the new command, instead of the old one
            $interfaceObj->modifyAttribs($self->session, 'response', $cmd);
        }

        # Clear the entry box
        $entryTableObj->set_text();

        # Update the task window to show the new button label
        $self->winObj->winShowAll($self->_objClass . '->miniEntryCallback');

        return 1;
    }

    ##################
    # Accessors - set

    sub set_enabledFlag {

        # Called by GA::Cmd::PermCompass->do

        my ($self, $flag, $check) = @_;

        # Check for improper arguments
        if (! defined $flag || defined $check) {

            return $axmud::CLIENT->writeImproper($self->_objClass . '->set_enabledFlag', @_);
        }

        if (! $flag) {
            $self->ivPoke('enabledFlag', FALSE);
        } else {
            $self->ivPoke('enabledFlag', TRUE);
        }

lib/Games/Axmud/Task.pm  view on Meta::CPAN

            my @list = $profObj->notepadList;

            $self->ivAdd('profNoteHash', $profObj->name, \@list);
        }

        # If the task has recently been updated, quickest way to update the window is to remove all
        #   the current table objects, before creating new ones
        if ($self->hasResetFlag) {

            if (! defined $self->winObj->tableStripObj->removeAllTableObjs()) {

                # Operation failed; task must close
                $self->ivPoke('shutdownFlag', TRUE);
                return 1;
            }
        }

        # Create widgets for the task
        $self->createWidgets();

        return 1;
    }

#   sub doShutdown {}           # Inherited from generic task

#   sub doReset {}              # Inherited from generic task

    sub createWidgets {

        # Set up the widgets used in the task window
        #
        # Expected arguments
        #   (none besides $self)
        #
        # Return values
        #   'undef' on improper arguments
        #   1 otherwise

        my ($self, $check) = @_;

        # Local variables
        my (
            $string, $textTableObj, $comboTableObj,
            @comboList,
            %profHash,
        );

        # Check for improper arguments
        if (defined $check) {

            return $axmud::CLIENT->writeImproper($self->_objClass . '->createWidgets', @_);
        }

        # Add a textview to display the notes
        if ($self->session->currentWorld->notepadList) {
            $string = join("\n", $self->session->currentWorld->notepadList);
        } else {
            $string = '';
        }

        # I experimented with the table coordinates to stop the combobox/buttons appearing too
        #   high. For unknown reasons, the best arrangement is to leave a one-row gap between the
        #   textview and the combobox/buttons beneath it
        $textTableObj = $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::TextView',
            0, 59, 0, 57,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('textViewCallback'),
            'text'          => $string,
            'edit_flag'     => TRUE,
            'colour_scheme' => 'edit',
            'spacing'       => 0,
        );

        # Add a combo to switch between profiles, displaying a list of current profiles in reverse
        #   priority order (so that the current world is always at the top)
        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Label',
            0, 4, 58, 59,
            undef,
            # Init settings
            'text'          => 'Profile:      ',
        );

        %profHash = $self->session->currentProfHash;
        foreach my $category ($self->session->profPriorityList) {

            my $profObj = $profHash{$category};

            if (defined $profObj) {

                delete $profHash{$category};
                push (@comboList, $profObj->name);
            }
        }

        @comboList = reverse @comboList;        # Current world to top of list
        foreach my $profObj (sort {lc($a->name) cmp lc($b->name)} (values %profHash)) {

            push (@comboList, $profObj->name);
        }

        $comboTableObj = $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::ComboBox',
            5, 24, 58, 59,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('comboCallback'),
            'list_ref'      => \@comboList,
        );

        # Add buttons - one to empty the textview of notes, another to restore the displayed notes
        #   to what they were, when the task started (or was last reset)
        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Button',
            30, 39, 58, 59,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('clipboardCallback'),
            'text'          => 'Paste',
            'tooltips'      => 'Insert any selected text in the session\'s default tab',
        );

lib/Games/Axmud/Task.pm  view on Meta::CPAN

            # Init settings
            'func'          => $self->getMethodRef('emptyCallback'),
            'text'          => 'Empty',
            'tooltips'      => 'Empty the notes for this profile',
        );

        $self->winObj->tableStripObj->addTableObj(
            'Games::Axmud::Table::Button',
            50, 59, 58, 59,
            undef,
            # Init settings
            'func'          => $self->getMethodRef('restoreCallback'),
            'text'          => 'Restore',
            'tooltips'      => 'Restore the notes that existed when this task started',
        );

        # Store the widgets created, so that callbacks can access them
        $self->ivPoke('textTableObj', $textTableObj);
        $self->ivPoke('comboTableObj', $comboTableObj);

        # Display all the widgets
        $self->winObj->winShowAll($self->_objClass . '->createWidgets');

        return 1;
    }

    ##################
    # Response methods

    sub textViewCallback {

        # Called by a ->signal_connect in a GA::Table::TextView object when the user edits the
        #   textview's contents
        # Stores the textview's contents in the selected profile's IV
        #
        # Expected arguments
        #   $tableObj   - The GA::Table::TextView object for the textview
        #   $textView   - The actual Gtk3::TextView edited
        #   $buffer     - The textview's Gtk3::Buffer
        #   $id         - The callback ID; in this case, an empty string
        #   $text       - The contents of the buffer (a string containing one or more lines
        #                   separated by newline characters)
        #
        # Return values
        #   'undef' on improper arguments
        #   1 otherwise

        my ($self, $tableObj, $textView, $buffer, $id, $text, $check) = @_;

        # Local variables
        my ($profile, $profObj);

        # Check for improper arguments
        if (
            ! defined $tableObj || ! defined $textView || ! defined $buffer || ! defined $id
            || ! defined $text || defined $check
        ) {
            return $axmud::CLIENT->writeImproper($self->_objClass . '->textViewCallback', @_);
        }

        # Get the combobox's active profile
        $profile = $self->comboTableObj->get_text();
        $profObj = $self->session->ivShow('profHash', $profile);

        # Update the selected profile's IV
        if ($profObj) {

            if ($text) {
                $profObj->ivPoke('notepadList', split("\n", $text));
            } else {
                $profObj->ivEmpty('notepadList');
            }
        }

        return 1;
    }

    sub comboCallback {

        # Called by a ->signal_connect in a GA::Table::Button object when the user selects a
        #   profile in the combobox
        # Updates the contents of the textview to show notes for that profile
        #
        # Expected arguments
        #   $tableObj   - The GA::Table::ComboBox object for the combobox
        #   $comboBox   - The actual Gtk3::ComboBox used
        #   $id         - The callback ID; in this case, an empty string
        #   $profile    - The selected world profile
        #
        # Return values
        #   'undef' on improper arguments
        #   1 otherwise

        my ($self, $tableObj, $comboBox, $id, $profile, $check) = @_;

        # Local variables
        my $profObj;

        # Check for improper arguments
        if (
            ! defined $tableObj || ! defined $comboBox || ! defined $id || ! defined $profile
            || defined $check
        ) {
            return $axmud::CLIENT->writeImproper($self->_objClass . '->comboCallback', @_);
        }

        # Get the corresponding profile object
        $profObj = $self->session->ivShow('profHash', $profile);

        # Update the textview
        if ($profObj) {

            $self->textTableObj->set_text(join("\n", $profObj->notepadList));
        }

        return 1;
    }

    sub clipboardCallback {

        # Called by a ->signal_connect in a GA::Table::Button object when the user clicks on the
        #   'Empty notes' button
        # Pastes the contents of the clipboard into the textview
        #
        # Expected arguments
        #   $tableObj   - The GA::Table::Button object for the button
        #   $button     - The actual Gtk3::Button clicked
        #   $id         - The callback ID; in this case, a keycode
        #
        # Return values
        #   'undef' on improper arguments
        #   1 otherwise

        my ($self, $tableObj, $button, $id, $check) = @_;

        # Local variables
        my ($textViewObj, $startIter, $endIter, $slice);

        # Check for improper arguments
        if (! defined $tableObj || ! defined $button || ! defined $id || defined $check) {

            return $axmud::CLIENT->writeImproper($self->_objClass . '->clipboardCallback', @_);
        }

        $textViewObj = $self->session->defaultTabObj->textViewObj;



( run in 1.852 second using v1.01-cache-2.11-cpan-8dfa8b56332 )