Class: GosuWrapper

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/gosu_wrapper.rb,
lib/gosu_wrapper/util.rb,
lib/gosu_wrapper/colors.rb,
lib/gosu_wrapper/buttons.rb,
lib/gosu_wrapper/grid_helpers.rb

Defined Under Namespace

Modules: Util Classes: GridHelpers

Constant Summary collapse

Colors =
{
  none: color::NONE,
  black: color::BLACK,
  grap: color::GRAY,
  white: color::WHITE,
  aqua: color::AQUA,
  red: color::RED,
  green: color::GREEN,
  blue: color::BLUE,
  fuchsia: color::FUCHSIA,
  cyan: color::CYAN,
}
Buttons =
{
  up: Gosu::KB_UP,
  down: Gosu::KB_DOWN,
  right: Gosu::KB_RIGHT,
  left: Gosu::KB_LEFT,
  enter: Gosu::KB_ENTER,
  escape: Gosu::KB_ESCAPE
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

method_missing_for

Constructor Details

#initialize(width:, height:, attributes:) ⇒ GosuWrapper

Returns a new instance of GosuWrapper.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gosu_wrapper.rb', line 13

def initialize(width:, height:, attributes:)
  @window_constructor = Class.new(Gosu::Window) do
    const_set("Attributes", attributes)
    attr_accessor *attributes
  end
  @window_attributes = @window_constructor::Attributes
  @window = @window_constructor.new(width, height)
  @window.window_height = height
  @window.window_width = width
  @hooks = []
  @helpers = []
  add_default_helpers
end

Instance Attribute Details

#helpersObject (readonly)

Returns the value of attribute helpers.



10
11
12
# File 'lib/gosu_wrapper.rb', line 10

def helpers
  @helpers
end

#hooksObject (readonly)

Returns the value of attribute hooks.



10
11
12
# File 'lib/gosu_wrapper.rb', line 10

def hooks
  @hooks
end

#windowObject (readonly)

Returns the value of attribute window.



10
11
12
# File 'lib/gosu_wrapper.rb', line 10

def window
  @window
end

#window_attributesObject (readonly)

Returns the value of attribute window_attributes.



10
11
12
# File 'lib/gosu_wrapper.rb', line 10

def window_attributes
  @window_attributes
end

#window_constructorObject (readonly)

Returns the value of attribute window_constructor.



10
11
12
# File 'lib/gosu_wrapper.rb', line 10

def window_constructor
  @window_constructor
end

Instance Method Details

#add_default_helpersObject



27
28
29
30
31
# File 'lib/gosu_wrapper.rb', line 27

def add_default_helpers
  add_helper :div_window_into,  GridHelpers.div_window_into
  add_helper :div_section_into, GridHelpers.div_section_into
  add_helper :draw_grid,        GridHelpers.draw_grid
end

#add_helper(name, &blk) ⇒ Object



91
92
93
94
# File 'lib/gosu_wrapper.rb', line 91

def add_helper(name, &blk)
  helpers << name
  define_method_on_window(name, &blk)
end

#add_hook(name, &blk) ⇒ Object



86
87
88
89
# File 'lib/gosu_wrapper.rb', line 86

def add_hook(name, &blk)
  hooks << name
  define_method_on_window(name, &blk)
end

#buttonsObject



114
115
116
# File 'lib/gosu_wrapper.rb', line 114

def buttons
  Buttons
end

#colorsObject



110
111
112
# File 'lib/gosu_wrapper.rb', line 110

def colors
  Colors
end

#config(&blk) ⇒ Object

config is the same as scope, but returns self



131
132
133
134
# File 'lib/gosu_wrapper.rb', line 131

def config(&blk)
  scope &blk
  self
end

#define_method_on_window(name, &blk) ⇒ Object

Methods are defined on the window’s anonyous class The generated method’s body is always invoked with the App instance’s scope.



79
80
81
82
83
84
# File 'lib/gosu_wrapper.rb', line 79

def define_method_on_window(name, &blk)
  app = self
  window.define_singleton_method(name) do |*args, **keywords|
    app.scope(*args, **keywords, &blk)
  end
end

#draw_rect(start_x:, start_y:, end_x:, end_y:, color:, **_) ⇒ Object



104
105
106
107
108
# File 'lib/gosu_wrapper.rb', line 104

def draw_rect(start_x:, start_y:, end_x:, end_y:, color:, **_)
  width = end_x - start_x
  height = end_y - start_y
  Gosu.draw_rect(start_x, start_y, width, height, color)
end

#image(path:) ⇒ Object



100
101
102
# File 'lib/gosu_wrapper.rb', line 100

def image(path:)
  Gosu::Image.new(path)
end

#invoke(name, *args, **keywords, &blk) ⇒ Object Also known as: call_helper, dispatch, call_hook

Call a method which is defined on window. Although this could have been overloaded onto get_<attr>, to avoid namin conflicts that’s limited to instance variables



139
140
141
# File 'lib/gosu_wrapper.rb', line 139

def invoke(name, *args, **keywords, &blk)
  window.send(name, *args, **keywords, &blk)
end

#scope(*args, **keywords, &blk) ⇒ Object

A wrapper over instance_exec i.e. app.scope { set_x 200 } Always returns self (app) Be aware that this only sets the scope for one level i.e. if you call a method in the passed block, it’ll use a different scope



122
123
124
125
126
127
128
# File 'lib/gosu_wrapper.rb', line 122

def scope(*args, **keywords, &blk)
  if keywords.blank?
    instance_exec *args, &blk
  else
    instance_exec *args, **keywords, &blk
  end
end

#showObject



96
97
98
# File 'lib/gosu_wrapper.rb', line 96

def show
  window.show
end