Class: Wordle::View::AppView
- Inherits:
-
Object
- Object
- Wordle::View::AppView
- Includes:
- Glimmer::UI::CustomShell
- Defined in:
- app/wordle/view/app_view.rb
Constant Summary collapse
- COLOR_TO_BACKGROUND_COLOR_MAP =
{ green: rgb(106, 170, 100), yellow: rgb(201, 180, 88), gray: rgb(120, 124, 126), }
- COLOR_TO_TEXT_COLOR_MAP =
{ green: :white, yellow: :white, gray: :white, }
- ALPHABET_LAYOUTS =
{ alphabetical: [ %w[A B C D E F G H I J], %w[K L M N O P Q R S], %w[T U V W X Y Z], ], qwerty: [ %w[Q W E R T Y U I O P], %w[A S D F G H J K L], %w[Z X C V B N M], ], }
- CONFIG_FILE =
File.join(Dir.home, '.glimmer_wordle')
Instance Method Summary collapse
- #alphabet_container ⇒ Object
- #alphabet_font ⇒ Object
- #alphabet_layout=(value) ⇒ Object
- #alphabet_layout_alphabets ⇒ Object
- #alphabet_row(alphabet_characters, &block) ⇒ Object
- #alphabets ⇒ Object
- #app_menu_bar ⇒ Object
- #display_about_dialog ⇒ Object
- #display_instructions_dialog ⇒ Object
- #display_share_text_dialog ⇒ Object
- #dispose_alphabet_container_children ⇒ Object
- #do_backspace ⇒ Object
- #do_guess ⇒ Object
- #do_left ⇒ Object
- #do_restart ⇒ Object
- #do_right ⇒ Object
- #do_type(character) ⇒ Object
- #emoji_result ⇒ Object
- #guess_button ⇒ Object
- #highlighted_letter_index ⇒ Object
- #invalid_word?(word) ⇒ Boolean
- #letter_font ⇒ Object
- #load_config ⇒ Object
- #new_config ⇒ Object
- #rebuild_alphabet_container ⇒ Object
- #save_config ⇒ Object
- #update_alphabet_background_colors ⇒ Object
- #update_guess_word_background_colors(guess_result) ⇒ Object
- #valid_character?(character) ⇒ Boolean
- #word_filled_up? ⇒ Boolean
- #word_guesser ⇒ Object
Instance Method Details
#alphabet_container ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/wordle/view/app_view.rb', line 193 def alphabet_container @alphabet_container = composite { layout_data(:center, :center, true, false) grid_layout { margin_width 0 margin_height 0 vertical_spacing 0 } background Display.system_dark_theme? ? :black : :white alphabets } end |
#alphabet_font ⇒ Object
465 466 467 468 469 470 |
# File 'app/wordle/view/app_view.rb', line 465 def alphabet_font the_font = {style: :bold, height: 28} the_font.merge!(name: 'Helvetica', height: 21) if OS.linux? the_font.merge!(name: 'Arial', height: 25) if OS.windows? the_font end |
#alphabet_layout=(value) ⇒ Object
496 497 498 499 |
# File 'app/wordle/view/app_view.rb', line 496 def alphabet_layout=(value) @alphabet_layout = value save_config end |
#alphabet_layout_alphabets ⇒ Object
258 259 260 |
# File 'app/wordle/view/app_view.rb', line 258 def alphabet_layout_alphabets ALPHABET_LAYOUTS[@alphabet_layout].reduce(:+) end |
#alphabet_row(alphabet_characters, &block) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'app/wordle/view/app_view.rb', line 232 def alphabet_row(alphabet_characters, &block) canvas { block.call background Display.system_dark_theme? ? :black : :white @alphabet_rectangles ||= [] @alphabet_borders ||= [] @alphabet_letters ||= [] alphabet_characters.each_with_index do |alphabet_character, i| @alphabet_rectangles << rectangle(1 + i*32, @alphabet_row_offset_y, 28, 28) { background Display.system_dark_theme? ? :black : :transparent @alphabet_borders << rectangle { foreground Display.system_dark_theme? ? :white : :gray line_width 2 } @alphabet_letters << text(alphabet_character, :default, [:default, OS.linux? ? 5 : (OS.windows? ? 1 : 0)]) { font alphabet_font foreground Display.system_dark_theme? ? :white : :black } } end } end |
#alphabets ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'app/wordle/view/app_view.rb', line 209 def alphabets alphabet_row(ALPHABET_LAYOUTS[@alphabet_layout][0]) { layout_data(:center, :center, true, false) { width_hint 318 height_hint 50 } } alphabet_row(ALPHABET_LAYOUTS[@alphabet_layout][1]) { layout_data(:center, :center, true, false) { width_hint 288 height_hint 50 } } alphabet_row(ALPHABET_LAYOUTS[@alphabet_layout][2]) { layout_data(:center, :center, true, false) { width_hint 222 height_hint 50 } } end |
#app_menu_bar ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/wordle/view/app_view.rb', line 99 def { { text '&Game' { text '&Restart' { do_restart } } { text 'E&xit' { exit(0) } } } { text '&View' { text 'Alphabet &Layout' (:radio) { text '&Alphabetical' selection @alphabet_layout == :alphabetical { self.alphabet_layout = :alphabetical rebuild_alphabet_container } } (:radio) { text '&Qwerty' selection @alphabet_layout == :qwerty { self.alphabet_layout = :qwerty rebuild_alphabet_container } } } } { text '&Help' { text '&Instructions' { display_instructions_dialog } } { text '&About' { display_about_dialog } } } } end |
#display_about_dialog ⇒ Object
186 187 188 189 190 191 |
# File 'app/wordle/view/app_view.rb', line 186 def display_about_dialog (body_root) { text 'About' "Glimmer Wordle #{VERSION}\n\n#{LICENSE}" }.open end |
#display_instructions_dialog ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'app/wordle/view/app_view.rb', line 171 def display_instructions_dialog (body_root) { text 'Instructions' <<~MULTI_LINE_STRING Make 6 guesses for a 5-letter word. If you enter a letter that is part of the word, and at the right location, it becomes green, If you enter a letter that is part of the word, but at the wrong location, it becomes yellow. If you enter a letter that is not part of the word, it becomes red. MULTI_LINE_STRING }.open end |
#display_share_text_dialog ⇒ Object
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'app/wordle/view/app_view.rb', line 439 def display_share_text_dialog result = "#{@five_letter_word.answer.upcase}\n\n#{emoji_result}" Clipboard.copy(result) @share_text_dialog = dialog(body_root) { grid_layout text 'Share Result' label { layout_data :center, :center, true, false text 'Result is copied to clipboard!' } styled_text { layout_data :fill, :fill, true, true editable false caret nil alignment :center font name: 'Segoe UI Emoji' if OS.windows? text result } } @share_text_dialog.open end |
#dispose_alphabet_container_children ⇒ Object
479 480 481 482 483 484 |
# File 'app/wordle/view/app_view.rb', line 479 def dispose_alphabet_container_children @alphabet_rectangles.clear @alphabet_borders.clear @alphabet_letters.clear @alphabet_container.children.each(&:dispose) end |
#do_backspace ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'app/wordle/view/app_view.rb', line 308 def do_backspace @letter = @letters[highlighted_letter_index] if @letter.string != '' index_to_delete = highlighted_letter_index else index_to_delete = [highlighted_letter_index - 1, 0].max end @letter = @letters[index_to_delete] @letter.string = '' @borders.each { |caret| caret.foreground = :gray} # refactor this reusable code into a method that highlights the caret @borders[index_to_delete].foreground = :title_background end |
#do_guess ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'app/wordle/view/app_view.rb', line 321 def do_guess return if !word_filled_up? word = @letters.map(&:string).join if invalid_word?(word) { text 'Invalid Word' "The word you entered is not an allowed guess!\n\nPlease try another word!" }.open return end guess_result = @five_letter_word.guess(word) update_guess_word_background_colors(guess_result) update_alphabet_background_colors if @five_letter_word.status == :in_progress @guess_button.dispose body_root.content { word_guesser } else @guess_button.dispose body_root.content { @restart_button = { layout_data :center, :center, true, false text 'Restart' focus true do do_restart end } } display_share_text_dialog end body_root.layout(true, true) body_root.pack(true) end |
#do_left ⇒ Object
373 374 375 376 377 |
# File 'app/wordle/view/app_view.rb', line 373 def do_left index = [highlighted_letter_index - 1, 0].max @borders.each { |caret| caret.foreground = :gray} # refactor this reusable code into a method that highlights the caret @borders[index].foreground = :title_background end |
#do_restart ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'app/wordle/view/app_view.rb', line 385 def do_restart @share_text_dialog&.close alphabet_layout_alphabets.each_with_index do |alphabet_character, i| @alphabet_borders[i].foreground = :gray @alphabet_rectangles[i].background = :white @alphabet_letters[i].foreground = :black end @restart_button&.dispose @canvasses.dup.each(&:dispose) @canvasses.clear @guess_button&.dispose body_root.content { word_guesser } body_root.layout(true, true) body_root.pack(true) @five_letter_word.refresh end |
#do_right ⇒ Object
379 380 381 382 383 |
# File 'app/wordle/view/app_view.rb', line 379 def do_right index = [highlighted_letter_index + 1, @letters.count - 1].min @borders.each { |caret| caret.foreground = :gray} # refactor this reusable code into a method that highlights the caret @borders[index].foreground = :title_background end |
#do_type(character) ⇒ Object
363 364 365 366 367 368 369 370 371 |
# File 'app/wordle/view/app_view.rb', line 363 def do_type(character) index = highlighted_letter_index @letter = @letters[index] @letter.string = character.upcase if @letters.any? {|letter| letter.string == ''} @borders.each { |caret| caret.foreground = :gray} # refactor this reusable code into a method that highlights the caret @borders[index == 4 ? 4 : index + 1].foreground = :title_background end end |
#emoji_result ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'app/wordle/view/app_view.rb', line 520 def emoji_result result = '' @five_letter_word.guess_results.each do |row| row.each do |result_color| case result_color when :green result << "🟩" when :yellow result << "🟨" when :gray result << "⬜" end end result << "\n" end result end |
#guess_button ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'app/wordle/view/app_view.rb', line 295 def @guess_button = { layout_data :center, :center, true, false text 'Guess' background Display.system_dark_theme? ? :black : :white foreground Display.system_dark_theme? ? :white : :black do do_guess end } end |
#highlighted_letter_index ⇒ Object
359 360 361 |
# File 'app/wordle/view/app_view.rb', line 359 def highlighted_letter_index @borders.each_with_index.find {|border, i| border.foreground.first == color(:title_background).swt_color }.last end |
#invalid_word?(word) ⇒ Boolean
431 432 433 |
# File 'app/wordle/view/app_view.rb', line 431 def invalid_word?(word) !Model::FiveLetterWord::WORLD_ALLOWED_GUESSES.include?(word.downcase) end |
#letter_font ⇒ Object
472 473 474 475 476 477 |
# File 'app/wordle/view/app_view.rb', line 472 def letter_font the_font = {style: :bold, height: 40} the_font.merge!(name: 'Helvetica', height: 30) if OS.linux? the_font.merge!(name: 'Arial', height: 32) if OS.windows? the_font end |
#load_config ⇒ Object
513 514 515 516 517 518 |
# File 'app/wordle/view/app_view.rb', line 513 def load_config File.exist?(CONFIG_FILE) ? YAML.load(File.read(CONFIG_FILE)) : {} rescue => e puts e. {} end |
#new_config ⇒ Object
501 502 503 504 505 |
# File 'app/wordle/view/app_view.rb', line 501 def new_config { alphabet_layout: @alphabet_layout } end |
#rebuild_alphabet_container ⇒ Object
486 487 488 489 490 491 492 493 494 |
# File 'app/wordle/view/app_view.rb', line 486 def rebuild_alphabet_container dispose_alphabet_container_children @alphabet_container.content { alphabets } @alphabet_container.layout(true, true) @alphabet_container.pack(true) update_alphabet_background_colors end |
#save_config ⇒ Object
507 508 509 510 511 |
# File 'app/wordle/view/app_view.rb', line 507 def save_config File.write(CONFIG_FILE, YAML.dump(new_config)) rescue => e puts e. end |
#update_alphabet_background_colors ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 |
# File 'app/wordle/view/app_view.rb', line 415 def update_alphabet_background_colors alphabet_layout_alphabets.each_with_index do |alphabet_character, i| result_color = @five_letter_word.colored_alphabets[alphabet_character.downcase] if result_color background_color = COLOR_TO_BACKGROUND_COLOR_MAP[result_color] @alphabet_borders[i].foreground = background_color @alphabet_rectangles[i].background = background_color @alphabet_letters[i].foreground = COLOR_TO_TEXT_COLOR_MAP[result_color] end end end |
#update_guess_word_background_colors(guess_result) ⇒ Object
405 406 407 408 409 410 411 412 413 |
# File 'app/wordle/view/app_view.rb', line 405 def update_guess_word_background_colors(guess_result) guess_result.each_with_index do |result_color, i| background_color = COLOR_TO_BACKGROUND_COLOR_MAP[result_color] @borders[i].foreground = background_color @rectangles[i].background = background_color @letters[i].foreground = COLOR_TO_TEXT_COLOR_MAP[result_color] async_exec { @canvasses.last.redraw } end end |
#valid_character?(character) ⇒ Boolean
435 436 437 |
# File 'app/wordle/view/app_view.rb', line 435 def valid_character?(character) ((65..90).to_a + (97..122).to_a).map {|n| n.chr}.include?(character) end |
#word_filled_up? ⇒ Boolean
427 428 429 |
# File 'app/wordle/view/app_view.rb', line 427 def word_filled_up? @letters.find {|letter| letter.string == ''}.nil? end |
#word_guesser ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'app/wordle/view/app_view.rb', line 262 def word_guesser @canvasses ||= [] margin_x = 5 margin_y = 5 @canvasses << canvas { layout_data(:center, :center, true, false) { width_hint 230 height_hint 50 } background Display.system_dark_theme? ? :black : :white focus true @rectangles = [] @borders = [] @letters = [] 5.times do |i| @rectangles << rectangle(margin_x + i*45, margin_y, 40, 40) { background Display.system_dark_theme? ? :black : :transparent @borders << rectangle { foreground i == 0 ? :title_background : :gray line_width 2 } @letters << text('', :default, [:default, OS.linux? ? 6 : (OS.windows? ? 1 : 0)]) { font letter_font foreground Display.system_dark_theme? ? :white : :black } } end } end |