Class: RbSDL2::MessageBox::MessageBoxData

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_sdl2/message_box.rb

Instance Method Summary collapse

Constructor Details

#initialize(buttons: nil, colors: nil, escape_key: nil, level: nil, message: nil, return_key: nil, title: nil, window: nil) ⇒ MessageBoxData

Returns a new instance of MessageBoxData.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rb_sdl2/message_box.rb', line 15

def initialize(buttons: nil, colors: nil, escape_key: nil, level: nil, message: nil,
               return_key: nil, title: nil, window: nil)
  @st = ::SDL2::SDL_MessageBoxData.new.tap do |data|
    button_data = *buttons
    data[:numbuttons] = num_buttons = button_data.length
    data[:buttons] = @buttons = num_buttons.nonzero? &&
      MessageBoxButtonDataArray.new(num_buttons).tap do |data_ary|
        @button_texts = []
        num_buttons.times do |idx|
          st, (text, *) = data_ary[idx], button_data[idx]
          st[:buttonid] = idx
          st[:flags] = case idx
                       when escape_key then ::SDL2::SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
                       when return_key then ::SDL2::SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
                       else 0
                       end
          st[:text] = @button_texts[idx] =
            ::FFI::MemoryPointer.from_string(text.to_s.encode(Encoding::UTF_8))
        end
      end
    data[:colorScheme] = @color_scheme = colors &&
      ::SDL2::SDL_MessageBoxColorScheme.new.tap do |st|
        # r, g, b, a 形式だった場合にエラーを出さない。
        st[:colors].each.with_index { |c, i| c[:r], c[:g], c[:b] = colors[i] }
      end
    data[:flags] = MessageBoxFlags.to_num(level)
    data[:message] = @message =
      ::FFI::MemoryPointer.from_string(message.to_s.encode(Encoding::UTF_8))
    data[:title] = @title =
      ::FFI::MemoryPointer.from_string(title.to_s.encode(Encoding::UTF_8))
    data[:window] = window
  end
end

Instance Method Details

#to_ptrObject



49
# File 'lib/rb_sdl2/message_box.rb', line 49

def to_ptr = @st.to_ptr