2009/8/6 Juan Andrés Gebhard <[email protected]>:
> Hi everyone! I'm trying to make a chess game. To draw the board, I thought
> of placing 8 flows, and 8 stack inside of each flow. And finally, each stack
> would have a PNG image inside.
> Instead of simple stacks, i decided to create a widget, so that it would be
> easier to change the image inside of it (that happens whenever a player
> makes a move).
> The problem with the widget is that i can't put one next to each other. They
> insist on putting themselves below the previous one, which isn't the case
> with simple stacks.
>
> class Casillero < Widget
>     attr_accessor :img, :back, :color
>     def initialize opts = {}
>         @stack = stack :width =>100, :height=>100
>         @back = @stack.background(*opts[:
> color]) if opts[:color]
>     end
> end
>
> Shoes.app{
>     flow :width => 900 do
>         8.times do |i|
>             @c = casillero :color => black if i%2==0
>             @c = casillero :color => white if i%2!=0
>         end
>     end
> }

Now that I read all of this and played around myself.. ;)

Remember that the top-level context in your app block is a flow. This
is also true for widgets. Flows take up the full width by default. So,
part of the solution is to style the top-level widget flow
appropriately.

Here is something I sketched out quick:

class Square < Shoes::Widget
  def initialize(options = {})
    style :width => 100, :height => 100
    background options[:background]
  end
end

Shoes.app :width => 800, :height => 800 do
  8.times do |i|
    start_color = (i % 2 == 0) ? white : black
    flow :height => 100 do
      color = start_color
      8.times do |i|
        square :background => color
        color = color == white ? black : white
      end
    end
  end
end

Seems OK, though I feel like determining the square color could be
done more cleanly somehow.

Reply via email to