Class: Kita::Application

Inherits:
Gtk::Application
  • Object
show all
Includes:
Menu
Defined in:
lib/kita.rb

Overview

LocaleSettings.new Main application class

Instance Method Summary collapse

Methods included from Menu

#menu_load

Constructor Details

#initializeApplication

Returns a new instance of Application.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kita.rb', line 19

def initialize
  super 'uk.danbishop.kita', :handles_open
  # Construct a Gtk::Builder instance
  @builder = Gtk::Builder.new(file: "#{File.expand_path(File.dirname(__dir__))}/ui/builder.ui")
  # Set menu toggles to match settings
  menu_load
  # Now connect signals
  @builder.connect_signals { |handler| method(handler) }
  # Initiate Question class
  @question = Question.new
  @button_signals = {}
  @sound = Audio.new
  new_question
  setup_about_box
  build_main_window
end

Instance Method Details

#build_main_windowObject



72
73
74
75
76
77
78
79
# File 'lib/kita.rb', line 72

def build_main_window
  signal_connect :activate do
    # Connect signal handlers to the constructed widgets
    window = @builder['window']
    window.signal_connect('destroy') { Gtk.main_quit }
    window.present
  end
end

#correct_button(buttons, question) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/kita.rb', line 91

def correct_button(buttons, question)
  # Choose random letter and remove it from array
  correct = buttons.delete(buttons.sample)
  correct_button = @builder["answer_#{correct}"]
  correct_button.set_label(question[:answer])
  correct_signal = correct_button.signal_connect('clicked') do
    new_question
  end
  @button_signals[correct] = correct_signal
end

#eeObject



67
68
69
70
# File 'lib/kita.rb', line 67

def ee
  @ee += 1
  @sound.play('これは北です') if @ee == 3
end

#new_questionObject



81
82
83
84
85
86
87
88
89
# File 'lib/kita.rb', line 81

def new_question
  reset_buttons
  question = @question.new_question
  @builder['question'].set_markup("<span font='72'>#{question[:question]}</span>")
  buttons = %w[a b c d]
  correct_button(buttons, question)
  wrong_buttons(buttons, question)
  @sound.play("#{question[:question].hiragana}#{[1, 2].sample}") if Settings.sound
end

#repeat_button_clickObject



46
47
48
# File 'lib/kita.rb', line 46

def repeat_button_click
  @sound.repeat
end

#reset_buttonsObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/kita.rb', line 120

def reset_buttons
  return nil if @button_signals.empty?

  @button_signals.each do |letter, signal|
    button = @builder["answer_#{letter}"]
    button.signal_handler_disconnect(signal)
    button.set_sensitive(true)
  end
  @button_signals = {}
end

#setup_about_boxObject



36
37
38
39
40
41
42
43
44
# File 'lib/kita.rb', line 36

def setup_about_box
  @ee = 0
  about_box = @builder['about_box']
  about_box.version = VERSION
  @builder['menu_about'].signal_connect('clicked') do
    about_box.run
    about_box.hide
  end
end

#toggle_hiraganaObject



56
57
58
59
60
# File 'lib/kita.rb', line 56

def toggle_hiragana
  Settings.hiragana = !Settings.hiragana
  (@builder['katakana_switch'].set_active(true) && Settings.katakana = true) if !Settings.katakana && !Settings.hiragana
  save_settings
end

#toggle_katakanaObject



50
51
52
53
54
# File 'lib/kita.rb', line 50

def toggle_katakana
  Settings.katakana = !Settings.katakana
  (@builder['hiragana_switch'].set_active(true) && Settings.hiragana = true) if !Settings.katakana && !Settings.hiragana
  save_settings
end

#toggle_soundObject



62
63
64
65
# File 'lib/kita.rb', line 62

def toggle_sound
  Settings.sound = !Settings.sound
  save_settings
end

#wrong_button_click(button) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/kita.rb', line 111

def wrong_button_click(button)
  button.set_sensitive(false)
  label = button.child
  correction = @question.type == 'hiragana' ? button.label.hiragana : button.label.katakana
  label.set_markup(
    "<span color='#d40000'>#{button.label} #{correction}</span>"
  )
end

#wrong_buttons(buttons, question) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/kita.rb', line 102

def wrong_buttons(buttons, question)
  buttons.each_with_index do |letter, i|
    button = @builder.get_object("answer_#{letter}")
    button.set_label(question[:choices][i])
    signal = button.signal_connect('clicked') { wrong_button_click(button) }
    @button_signals[letter] = signal
  end
end