Class: GamesAndRpgParadise::GUI::AlignThree
- Inherits:
-
Object
- Object
- GamesAndRpgParadise::GUI::AlignThree
- Defined in:
- lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb
Overview
GamesAndRpgParadise::GUI::AlignThree
Defined Under Namespace
Classes: SwingButton
Instance Method Summary collapse
-
#adjacent_buttons_with_same_value(swing_button) ⇒ Object
adjacent_buttons_with_same_value.
-
#check_adjacent_count_same_value(swing_button) ⇒ Object
# === check_adjacent_count_same_value ========================================================================= #.
-
#create_buttons ⇒ Object
# === create_buttons.
-
#define_buttons_colours ⇒ Object
# === define_buttons_colours.
-
#do_handle_the_click_event(swing_button, evt) ⇒ Object
# === do_handle_the_click_event.
-
#initialize ⇒ AlignThree
constructor
# === initialize ========================================================================= #.
-
#reset ⇒ Object
# === reset ========================================================================= #.
Constructor Details
#initialize ⇒ AlignThree
#
initialize
#
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 36 def initialize reset # button/window display settings: @frame.set_size( @button_width * @button_cols+((@button_cols-1) * @button_spacing), @button_height * @button_cols+((@button_rows-1) * @button_spacing) ) # Create the swing gridlayout frame next: @frame.set_layout( java.awt.GridLayout.new(@button_rows, @button_cols, @button_spacing, @button_spacing) ) @frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE) @frame.setResizable(false) @frame.show end |
Instance Method Details
#adjacent_buttons_with_same_value(swing_button) ⇒ Object
adjacent_buttons_with_same_value
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 191 def () coordinates = [] # top coordinates << { row: .row-1, column: .column } if .row > 1 # right coordinates << { row: .row, column: .column+1 } if .column < @button_cols # bottom coordinates << { row: .row+1, column: .column } if .row < @button_rows # left coordinates << { row: .row, column: .column-1 } if .column > 1 @buttons.select {|| coordinates.include?({row: .row, column: .column}) }.select {|| .value == .value } end |
#check_adjacent_count_same_value(swing_button) ⇒ Object
#
check_adjacent_count_same_value
#
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 174 def check_adjacent_count_same_value() checked = [] not_checked = [] while !not_checked.empty? = not_checked.pop checked << ().each do || next if checked.include? next if not_checked.include? not_checked << end end checked end |
#create_buttons ⇒ Object
#
create_buttons
This method will create all the buttons that are placed in our 2D grid here.
#
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 114 def (1 .. @button_rows).each { |row| (1 .. @button_cols).each { |column| # create a new swing button = SwingButton.new(javax.swing.JButton.new('0'), row, column, 0) ..setOpaque true ..setBorderPainted false ..setBackground Color.new(*@button_colors[.value]) # add "click" action listener ..add_action_listener { |event| do_handle_the_click_event(, event) } @buttons << } } @buttons.each {|| @frame.add(.) } end |
#define_buttons_colours ⇒ Object
#
define_buttons_colours
This method will define RGB values for a button value.
#
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 93 def @button_colors = [ [255, 255, 255], #ffffff white [255, 153, 153], #ff9999 pink [255, 153, 102], #ff9966 orange [255, 255, 153], #ffff99 yellow [153, 255, 153], #99ff99 green [102, 255, 204], #66ffcc green/blue [153, 255, 255], #99ffff light blue [102, 204, 255], #66ccff blue [153, 153, 255], #9999ff purple [255, 153, 255], #ff99ff purple/pink ] end |
#do_handle_the_click_event(swing_button, evt) ⇒ Object
#
do_handle_the_click_event
button click event handler
#
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 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 139 def do_handle_the_click_event(, evt) # for now, if the button has already been clicked, return return if .value > 0 .value += 1 # looping until nothing left to collapse begin # check for adjacent similar values, todo = check_adjacent_count_same_value # update single button if .size < @adjacent_count ..setText .value.to_s ..setBackground Color.new(*@button_colors[.value]) return end .each { || # clicked button case when .value += 1 ..setText .value.to_s ..setBackground Color.new(*@button_colors[.value]) # adjacent buttons else .value = 0 ..setText .value.to_s ..setBackground Color.new(*@button_colors[.value]) end } end until check_adjacent_count_same_value().size < @adjacent_count end |
#reset ⇒ Object
#
reset
#
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/games_and_rpg_paradise/games/misc/gui/jruby/align_three/align_three.rb', line 56 def reset @frame = JFrame.new # ======================================================================= # # Keep track of all buttons, via an Array: # ======================================================================= # @buttons = [] # ======================================================================= # # === adjacent_count # # Define how many in a row collapse. # ======================================================================= # @adjacent_count = 3 # ======================================================================= # # === @button_width # ======================================================================= # @button_width = 100 # ======================================================================= # # === @button_height # ======================================================================= # @button_height = 100 # ======================================================================= # # === @button_rows # ======================================================================= # @button_rows = 5 # ======================================================================= # # === @button_cols # ======================================================================= # @button_cols = 5 @button_spacing = 5 end |