module AsciiParadise::GUI::Gtk::WidgetModule

Constants

HEIGHT
#

HEIGHT

#
NAMESPACE
#

NAMESPACE

#
NUMBERED_LINES_WIDGET_HAS_N_LINES
#

NUMBERED_LINES_WIDGET_HAS_N_LINES

#
TITLE
#

TITLE

#
WIDTH
#

WIDTH

#

Public Class Methods

new( optional_file = nil, run_already = true ) click to toggle source
#

initialize

#
Calls superclass method
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 59
def initialize(
    optional_file = nil,
    run_already   = true
  )
  super(:horizontal)
  reset
  set_use_this_font("Mono #{FONT_SIZE}")
  run if run_already
end
run() click to toggle source
#

AsciiParadise::GUI::Gtk::WidgetModule.run

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 413
def self.run
  require 'gtk_paradise/run'
  r = ::Gtk.run
  r.set_title(
    ::AsciiParadise::GUI::Gtk::WidgetModule::TITLE
  )
  r << ::AsciiParadise::GUI::Gtk::Widget.new
  # r.status_icon # This one does not quite work.
  r.automatic_size_and_automatic_title
  r.top_left_then_run
end

Public Instance Methods

add_all_html_colours_to_the_main_text_view() click to toggle source
#

add_all_html_colours_to_the_main_text_view

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 161
def add_all_html_colours_to_the_main_text_view
  return_the_html_colours.each {|this_html_colour|
    this_html_colour = this_html_colour.to_s
    @hash_all_tags[this_html_colour] =
      @text_buffer.create_tag(nil, { foreground: this_html_colour })
  }
end
border_size?() click to toggle source
#

border_size?

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 252
def border_size?
  10
end
clear_main_buffer() click to toggle source
#

clear_main_buffer

This method is triggered when the user clicks on the clear-button.

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 245
def clear_main_buffer
  set_text_in_the_buffer('')
end
connect_skeleton() click to toggle source
#

connect_skeleton

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 442
def connect_skeleton
  abort_on_exception
  @box_vertical = gtk_vbox
  @label_for_the_title = gtk_label
  @box_vertical.minimal(@label_for_the_title)
  hbox = gtk_hbox
  @numbered_lines = ::Gtk::NumberedLines.new {{ upper_range: NUMBERED_LINES_WIDGET_HAS_N_LINES }}
  @numbered_lines.set_use_this_font("Mono #{FONT_SIZE}")
  hbox.minimal(@numbered_lines, 1)
  hbox.maximal(@scrolled_window)
  @box_vertical.minimal(hbox, 1)
  @combo_box.on_changed {
    sync_combo_box_to_main_gtk_entry
    stop_the_current_iteration
  }
  @combo_box.active = 92
  @combo_box.add_tearoffs = true # Add some tearoffs.
  # ======================================================================= #
  # === hbox_for_the_combo_box_and_the_start_button
  #
  # Add the start button next, together with a combo-box on the
  # left hand side.
  # ======================================================================= #
  hbox_for_the_combo_box_and_the_start_button = gtk_hbox
  hbox_for_the_combo_box_and_the_start_button << @combo_box
  hbox_for_the_combo_box_and_the_start_button.maximal(
    create_and_return_the_start_button
  )
  @box_vertical.minimal(hbox_for_the_combo_box_and_the_start_button)
  # ======================================================================= #
  # === hbox_for_the_horizontal_button_box
  #
  # Next we will add a small panel with buttons that can be used to
  # clear the main dataset.
  # ======================================================================= #
  button1 = button('_Clear')
  button1.fancy_tooltip = ' Click this button to <b>clear the buffer</b> above. '
  button1.on_click_event {
    clear_main_buffer
  }
  # ======================================================================= #
  # == The stop-the-animation button
  # ======================================================================= #
  @button_stop_the_animation = button('_Stop')
  @button_stop_the_animation.fancy_tooltip = ' Click this button to <b>stop the animation</b> above. '
  @button_stop_the_animation.on_click_event {
    do_stop_the_animation
  }
  text = '_Run a random AsciiParadise animation'
  @button_random_ascii_animation = button(text)
  @button_random_ascii_animation.on_click_event {
    do_show_a_random_ascii_paradise_animation
  }
  @button_random_ascii_animation.hint =
    ' Click this button to <b>run a random ASCII animation</b>. '
  # ======================================================================= #
  # === The three buttons
  #
  # Three buttons will be added next.
  # ======================================================================= #
  hbox_for_the_horizontal_button_box = gtk_hbox
  hbox_for_the_horizontal_button_box.minimal(button1)
  hbox_for_the_horizontal_button_box.minimal(@button_stop_the_animation)
  hbox_for_the_horizontal_button_box.maximal(@button_random_ascii_animation)
  @box_vertical.minimal(hbox_for_the_horizontal_button_box)
  # ======================================================================= #
  # Next, add the label and combo-box to pick a colour.
  # ======================================================================= #
  hbox_for_label_and_combo_box_for_picking_a_colour = gtk_hbox
  hbox_for_label_and_combo_box_for_picking_a_colour.minimal(
    text('Pick a colour to use for the animation (default is <b>black</b>) ➡️ ',
    1)
  )
  index = ::Colours.html_colours.find_index {|entry| entry == 'black' }
  @combo_box_for_picking_a_colour.active = index
  hbox_for_label_and_combo_box_for_picking_a_colour.maximal(
    @combo_box_for_picking_a_colour
  )
  @box_vertical.minimal(hbox_for_label_and_combo_box_for_picking_a_colour, 1)
  # ======================================================================= #
  # Next add some control-elements, such as how fast the animation shall be.
  # ======================================================================= #
  hbox_with_control_elements = gtk_hbox
  label_for_delay = gtk_bold_label(
    'Determine how fast the animation shall be:'
  )
  label_for_delay.is_selectable
  label_for_delay.hint = 'The lower the value to the right, '\
    'the faster the animation will run.'
  hbox_with_control_elements << label_for_delay
  # ======================================================================= #
  # Add the Gtk::Entry that holds the amount of seconds between the
  # individual frames.
  # ======================================================================= #
  @entry_for_delay = entry(@default_delay)
  @entry_for_delay.to_the_middle
  # ======================================================================= #
  # When the user hits enter, run the animation at once.
  # ======================================================================= #
  @entry_for_delay.on_key_pressed(:enter) {
    start_button?.signal_emit(:clicked)
  }
  # ======================================================================= #
  # Next, add the HScale button.
  # ======================================================================= #
  scale_button = gtk_hscale(0.1, 3.0, 0.05)
  scale_button.set_digits(2)
  scale_button.set_value(@default_delay.to_f) # We have to sue the above default.
  scale_button.signal_connect(:value_changed) { |inner_widget|
    inner_value = inner_widget.value
    @entry_for_delay.set_text(inner_value.to_s)
  }
  if ::Gtk.use_gtk2?
    hbox_with_control_elements.pack_start(@entry_for_delay,true,false,1)
  else
    hbox_with_control_elements.pack_start(
      @entry_for_delay, expand: true, fill: false, padding: 1
    )
  end

  hbox_with_control_elements << scale_button
  @box_vertical.minimal(hbox_with_control_elements)
  # ======================================================================= #
  # === Determine how often the animated ASCII should be run
  # ======================================================================= #
  label = bold_label('Determine how often to run: ')
  label.is_selectable
  hbox_with_label_and_entry_run_n_times = gtk_hbox
  if ::Gtk.use_gtk2?
    hbox_with_label_and_entry_run_n_times.pack_start(label,true,false,1)
  else
    hbox_with_label_and_entry_run_n_times.pack_start(
      label, expand: true, fill: false, padding: 1
    )
  end
  # ======================================================================= #
  # Add the pointer emoji next:
  # ======================================================================= #
  hbox_with_label_and_entry_run_n_times.minimal(gtk_label('👉')) # A slight indicator.
  hbox_with_label_and_entry_run_n_times.maximal(@entry_run_n_times, 2)
  @box_vertical.minimal(hbox_with_label_and_entry_run_n_times)

  @array_buttons = []
  @array_buttons << @button_stop_the_animation
  @array_buttons << button1
  @array_buttons << @button_random_ascii_animation
  @array_buttons << @start_button_run_this_ascii_component

  process_each_button
  hbox = gtk_hbox
  hbox.maximal(@entry_which_component_to_use)
  @box_vertical.minimal(hbox)
  add(@box_vertical)
end
create_and_return_the_start_button() click to toggle source
#

create_and_return_the_start_button

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 314
def create_and_return_the_start_button
  # ======================================================================= #
  # === The start button                            (action tag, start tag)
  #
  # The start button that will be used to run a new animation. It is a
  # button with bold text.
  # ======================================================================= #
  @start_button_run_this_ascii_component = button(
    DEFAULT_TITLE_FOR_THE_START_BUTTON
  )
  # @start_button_run_this_ascii_component.make_bold(:retain_mnemonic) # Let's use a bold-button.
  # ^^^ The above still does not seem to want to retain the mnemonics.
  # ======================================================================= #
  # === Add a tooltip to that start button as well.
  # ======================================================================= #
  @start_button_run_this_ascii_component.fancy_tooltip = 
    ' Click this button to <b>run the specified ascii-component</b>. '
  # ======================================================================= #
  # Next determine what happens when the start-button is clicked.
  # ======================================================================= #
  @start_button_run_this_ascii_component.on_clicked {
    # ===================================================================== #
    # We must determine which component is to be used.
    # ===================================================================== #
    component = return_text_from_the_currently_selected_entry_in_the_combo_box
    if component.empty?
      _ = 'Please supply an entry for the dataset.'
      e _
      set_text_in_buffer(_)
    else
      dataset = ::AsciiParadise.return_dataset_for(component.to_sym) # Returns false if it does not find the input.
      if dataset
        update_the_buffer_n_times_with_this_dataset(dataset)
      else
        set_text(
          'This component ('+@entry_which_component_to_use.text.to_s+
          ') has no .dataset? method.'
        )
      end
    end
  }
  return @start_button_run_this_ascii_component
end
create_combo_boxes() click to toggle source
#

create_combo_boxes

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 361
def create_combo_boxes
  # ======================================================================= #
  # === @combo_box_for_picking_a_colour
  # ======================================================================= #
  @combo_box_for_picking_a_colour = combo_box_text
  @combo_box_for_picking_a_colour.populate(
    ::Colours.html_colours
  )
  @combo_box_for_picking_a_colour.bblack1
  @combo_box_for_picking_a_colour.hint = 'You can also change the '\
    'colour as the text-view buffer iterates through.'
  # ======================================================================= #
  # === @combo_box
  #
  # Next, add the combo box that has all AsciiParadise components,
  # and a hbox that keeps them all there.
  # ======================================================================= #
  @combo_box = gtk_combo_box(
    return_valid_and_available_ascii_paradise_components
  )
  @combo_box.bblack1
end
create_gtk_entries() click to toggle source
#

create_gtk_entries (entries tag, entry tag)

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 218
def create_gtk_entries
  # ======================================================================= #
  # === @entry_run_n_times
  # ======================================================================= #
  #@entry_run_n_times = entry(DEFAULT_RUN_N_TIMES.to_s)
  @entry_run_n_times = gtk_spin_button(1, 1000, 1)
  @entry_run_n_times.to_the_middle
  @entry_run_n_times.very_light_yellow_background_colour
  # ======================================================================= #
  # === @entry_which_component_to_use
  # ======================================================================= #
  @entry_which_component_to_use = gtk_entry { :align_to_the_center }
  @entry_which_component_to_use.make_bold
end
create_skeleton() click to toggle source
#

create_skeleton

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 387
def create_skeleton
  create_combo_boxes
  create_gtk_entries
  create_text_buffer_and_textview
  create_the_scrolled_window
end
create_text_buffer_and_textview() click to toggle source
#

create_text_buffer_and_textview

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 618
def create_text_buffer_and_textview
  @text_buffer = gtk_text_buffer
  # ======================================================================= #
  # === @text_view
  # ======================================================================= #
  @text_view = gtk_text_view(@text_buffer)
  @text_view.the_cursor_is_not_visible
  @text_view.use_this_font = :hack_12
  @text_view.indent = 10
  @text_view.set_left_margin(2)
  @text_view.set_editable(true)
  @text_view.set_name('black')
  add_all_html_colours_to_the_main_text_view
end
create_the_scrolled_window() click to toggle source
#

create_the_scrolled_window

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 428
def create_the_scrolled_window
  @scrolled_window = gtk_scrolled_window(@text_view)
  @scrolled_window.set_name('scrolledwindow1')
  @scrolled_window.width_height(880, 380)
  if @scrolled_window.respond_to? :min_content_height
    @scrolled_window.min_content_width  = 880
    @scrolled_window.min_content_height = 380
  end
  @scrolled_window.show_scrollbars
end
do_handle_CSS() click to toggle source
#

do_handle_CSS

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 110
def do_handle_CSS
  append_gtk_paradise_css_rules
  append_project_css_file
end
do_show_a_random_ascii_paradise_animation() click to toggle source
#

do_show_a_random_ascii_paradise_animation (random tag)

This method can be used to pick a random animation.

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 399
def do_show_a_random_ascii_paradise_animation
  array = return_valid_and_available_ascii_paradise_components
  sample = array.sample
  dataset = ::AsciiParadise.return_dataset_for(sample.to_sym) # Returns false if it does not find the input.
  if dataset
    update_the_buffer_n_times_with_this_dataset(dataset)
    @combo_box.active = array.index(sample)
    set_top_title(sample.to_s)
  end
end
do_stop_the_animation() click to toggle source
#

do_stop_the_animation

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 118
def do_stop_the_animation
  @thread_is_running = false
end
Also aliased as: stop_the_current_iteration
entry_which_component_to_use?() click to toggle source
#

entry_which_component_to_use?

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 236
def entry_which_component_to_use?
  @entry_which_component_to_use
end
Also aliased as: main_entry?
main_entry?()
process_each_button() click to toggle source
#

process_each_button

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 151
def process_each_button
  @array_buttons.each {|this_button|
    this_button.mediumaquamarine
    this_button.set_name('button1')
  }
end
reset() click to toggle source
#

reset

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 72
def reset
  reset_the_internal_variables
  # ======================================================================= #
  # === @configuration
  # ======================================================================= #
  @configuration = [true, __dir__, NAMESPACE]
  # ======================================================================= #
  # === @title
  # ======================================================================= #
  set_title(TITLE)
  # ======================================================================= #
  # === @height
  # ======================================================================= #
  set_height(HEIGHT)
  # ======================================================================= #
  # === @width
  # ======================================================================= #
  set_width(WIDTH)
  # ======================================================================= #
  # === @default_delay
  # ======================================================================= #
  @default_delay = DEFAULT_DELAY
  # ======================================================================= #
  # === @thread_is_running
  # ======================================================================= #
  @thread_is_running = false
  # ======================================================================= #
  # === @hash_all_tags
  # ======================================================================= #
  @hash_all_tags = {}
  infer_the_size_automatically
  increase_font_size
  do_handle_CSS
end
return_text_from_the_currently_selected_entry_in_the_combo_box() click to toggle source
#

return_text_from_the_currently_selected_entry_in_the_combo_box

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 600
def return_text_from_the_currently_selected_entry_in_the_combo_box
  @combo_box.active_text.to_s
end
return_the_html_colours() click to toggle source
#

return_the_html_colours

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 172
def return_the_html_colours
  ::Colours.html_colours
end
return_valid_and_available_ascii_paradise_components() click to toggle source
#

return_valid_and_available_ascii_paradise_components

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 179
def return_valid_and_available_ascii_paradise_components
  ::AsciiParadise.available_components.reject {|entry|
      (entry == 'Counter') or
      (entry == 'ProgressBar') or
      (entry == 'RotatingDna') or
      (entry == 'RotatingSlash') or
      (entry == 'RotatingCube')
    }
end
run() click to toggle source
#

run

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 636
def run
  create_skeleton_then_connect_skeleton
end
run_n_times?() click to toggle source
#

run_n_times?

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 307
def run_n_times?
  @entry_run_n_times.text.to_i
end
run_the_main_loop( dataset, run_n_times = run_n_times? ) click to toggle source
#

run_the_main_loop

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 259
def run_the_main_loop(
    dataset, run_n_times = run_n_times?
  )
  Thread.new { # Use a thread here.
    @start_button_run_this_ascii_component.grey_out
    @button_random_ascii_animation.grey_out
    @thread_is_running = true
    run_n_times.times {
      # =================================================================== #
      # Iterate over the dataset there.
      # =================================================================== #
      dataset.each {|entry|
        break unless @thread_is_running == true
        # ================================================================= #
        # Since this is an individual frame, we can simply set the buffer.
        # ================================================================= #
        set_in_buffer(entry)
        sleep(@sleep_for_this_duration)
      }
    }
    @start_button_run_this_ascii_component.grey_in
    @button_random_ascii_animation.grey_in
  }
end
set_in_buffer(i)
set_text(i)
set_text_in_buffer(i)
set_text_in_the_buffer(i) click to toggle source
#

set_text_in_the_buffer

If an Array is given to this method then it will be converted into a String.

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 195
def set_text_in_the_buffer(i)
  if i.is_a? Array
    i = i.join(N)
  end
  # ======================================================================= #
  # We also must sync to use the right colour, by making use of CSS.
  # ======================================================================= #
  colour_to_use = @combo_box_for_picking_a_colour.active_text.to_s
  @text_buffer.clear # Clear the old dataset, just in case.
  @text_view.set_name(colour_to_use) # Pointless, but whatever.
  @text_buffer.insert(
    @text_buffer.start_iter,
    i,
    tags: [ @hash_all_tags[colour_to_use] ]
  )
  # @text_buffer.set_text(i) # ← This was the old variant up to 22.01.2021
end
set_top_title(title)
Alias for: set_top_title_to
set_top_title_to(title) click to toggle source
#

set_top_title_to

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 125
def set_top_title_to(title)
  # ======================================================================= #
  # === Set a new title for the top-label
  # ======================================================================= #
  @label_for_the_title.set_markup(
    %Q[<markup><span weight='bold' foreground='darkblue'>#{title}</span></markup>], true
  )
end
Also aliased as: set_top_title
start_button?() click to toggle source
#

start_button?

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 144
def start_button?
  @start_button_run_this_ascii_component
end
stop_the_current_iteration()
sync_combo_box_to_main_gtk_entry() click to toggle source
#

sync_combo_box_to_main_gtk_entry

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 607
def sync_combo_box_to_main_gtk_entry
  if @combo_box.active_iter
    main_entry?.set_text(
      return_text_from_the_currently_selected_entry_in_the_combo_box
    )
  end
end
update_the_buffer_n_times_with_this_dataset( dataset, run_n_times = run_n_times?, sleep_for_this_duration = @entry_for_delay.text ) click to toggle source
#

update_the_buffer_n_times_with_this_dataset

This method will update the text-buffer containing the “frame”.

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 289
def update_the_buffer_n_times_with_this_dataset(
    dataset,
    run_n_times             = run_n_times?,
    sleep_for_this_duration = @entry_for_delay.text
  )
  if sleep_for_this_duration and sleep_for_this_duration.empty?
    sleep_for_this_duration = DEFAULT_DELAY
  else
    sleep_for_this_duration = sleep_for_this_duration.to_f
  end
  @sleep_for_this_duration = sleep_for_this_duration
  set_top_title_to(@entry_which_component_to_use.text.to_s)
  run_the_main_loop(dataset, run_n_times?)
end
use_which_delay?() click to toggle source
#

use_which_delay?

#
# File lib/ascii_paradise/gui/shared_code/animated_frame/widget_module.rb, line 137
def use_which_delay?
  @entry_for_delay.text.to_f
end