Class: Ray::View

Inherits:
Object
  • Object
show all
Includes:
PP
Defined in:
ext/view.c,
lib/ray/view.rb,
ext/view.c

Overview

A view is a way to apply some transformations to all the drawables at once, by defining a projection. It also determines the region of the screen where the view will draw.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PP

#pretty_print_attributes

Constructor Details

#initialize(center, size, viewport = [0, 0, 1, 1]) ⇒ Object #initialize(matrix) ⇒ Object

Overloads:

  • #initialize(center, size, viewport = [0, 0, 1, 1]) ⇒ Object

    Parameters:

  • #initialize(matrix) ⇒ Object

    Parameters:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'ext/view.c', line 38

static
VALUE ray_view_init(int argc, VALUE *argv, VALUE self) {
  say_view *view = ray_rb2view(self);

  if (argc == 1) {
    say_view_set_matrix(view, ray_rb2matrix(argv[0]));
  }
  else {
    VALUE center, size, viewport = Qnil;
    rb_scan_args(argc, argv, "21", &center, &size, &viewport);

    say_view_set_center(view, ray_convert_to_vector2(center));
    say_view_set_size(view, ray_convert_to_vector2(size));

    if (!NIL_P(viewport)) {
      say_view_set_viewport(view, ray_convert_to_rect(viewport));
    }
  }

  return self;
}

Class Method Details

.clip(rect, target_size) ⇒ Ray::View

Returns A view that allows to clip rendering to a given region of the target, without scaling.

Parameters:

  • rect (Ray::Rect)

    Rect to show, in pixels.

  • target_size (Ray::Vector2)

    Size of the complete target, in pixels.

Returns:

  • (Ray::View)

    A view that allows to clip rendering to a given region of the target, without scaling.



10
11
12
13
14
15
16
17
18
19
# File 'lib/ray/view.rb', line 10

def self.clip(rect, target_size)
  rect, target_size = rect.to_rect, target_size.to_vector2

  viewport = Ray::Rect[rect.x / target_size.w,
                       rect.y / target_size.h,
                       rect.w / target_size.w,
                       rect.h / target_size.y]

 Ray::View.new(rect.center, rect.size, viewport)
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
# File 'lib/ray/view.rb', line 21

def ==(other)
  other.is_a?(Ray::View) && self.matrix == other.matrix
end

#centerObject

See Also:



89
90
91
92
# File 'ext/view.c', line 89

static
VALUE ray_view_center(VALUE self) {
  return ray_vector2_to_rb(say_view_get_center(ray_rb2view(self)));
}

#center=(center) ⇒ Object

Sets the center of the world

Parameters:



99
100
101
102
103
104
# File 'ext/view.c', line 99

static
VALUE ray_view_set_center(VALUE self, VALUE val) {
  rb_check_frozen(self);
  say_view_set_center(ray_rb2view(self), ray_convert_to_vector2(val));
  return val;
}

#hObject



54
# File 'lib/ray/view.rb', line 54

def h; size.h;   end

#h=(val) ⇒ Object



59
# File 'lib/ray/view.rb', line 59

def h=(val); self.size   = [w, val]; end

#initialize_copy(orig) ⇒ Object



60
61
62
63
64
# File 'ext/view.c', line 60

static
VALUE ray_view_init_copy(VALUE self, VALUE orig) {
  say_view_copy(ray_rb2view(self), ray_rb2view(orig));
  return self;
}

#inspectObject



61
62
63
# File 'lib/ray/view.rb', line 61

def inspect
  "#<#{self.class} center=#{center} size=#{size} viewport=#{viewport}>"
end

#matrixObject

See Also:



133
134
135
136
# File 'ext/view.c', line 133

static
VALUE ray_view_matrix(VALUE self) {
  return ray_matrix2rb(say_view_get_matrix(ray_rb2view(self)));
}

#matrix=(mat) ⇒ Object

Sets the projection matrix

Setting the projection matrix causes the center and the size of the view to be ignored. Passing nil would cancel this.

Parameters:



147
148
149
150
151
152
153
154
155
156
157
# File 'ext/view.c', line 147

static
VALUE ray_view_set_matrix(VALUE self, VALUE val) {
  rb_check_frozen(self);

  if (NIL_P(val))
    say_view_set_matrix(ray_rb2view(self), NULL);
  else
    say_view_set_matrix(ray_rb2view(self), ray_rb2matrix(val));

  return val;
}

#pretty_print(q) ⇒ Object



65
66
67
# File 'lib/ray/view.rb', line 65

def pretty_print(q)
  pretty_print_attributes q, ["center", "size", "matrix", "viewport"]
end

#rectRay::Rect

Returns Rect representing the part of the world that can be seen through this view.

Returns:

  • (Ray::Rect)

    Rect representing the part of the world that can be seen through this view.



44
45
46
47
48
49
# File 'lib/ray/view.rb', line 44

def rect
  size = self.size
  pos  = self.center

  Ray::Rect[pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y]
end

#sizeObject

See Also:

  • view=


69
70
71
72
# File 'ext/view.c', line 69

static
VALUE ray_view_size(VALUE self) {
  return ray_vector2_to_rb(say_view_get_size(ray_rb2view(self)));
}

#size=(val) ⇒ Object

Sets the size of the visible world

Parameters:



79
80
81
82
83
84
# File 'ext/view.c', line 79

static
VALUE ray_view_set_size(VALUE self, VALUE val) {
  rb_check_frozen(self);
  say_view_set_size(ray_rb2view(self), ray_convert_to_vector2(val));
  return val;
}

#unzoom_by(value) ⇒ Object

Decreases zoom (making object appear smaller).

Parameters:

See Also:



38
39
40
# File 'lib/ray/view.rb', line 38

def unzoom_by(value)
  self.size *= value
end

#viewportObject

See Also:



109
110
111
112
# File 'ext/view.c', line 109

static
VALUE ray_view_viewport(VALUE self) {
  return ray_rect2rb(say_view_get_viewport(ray_rb2view(self)));
}

#viewport=(rect) ⇒ Object

Sets the region of the world where rendering will be done

All the components of the rects are values between 0 and 1, allowing to use the same rect for two targets of different size

Parameters:



123
124
125
126
127
128
# File 'ext/view.c', line 123

static
VALUE ray_view_set_viewport(VALUE self, VALUE val) {
  rb_check_frozen(self);
  say_view_set_viewport(ray_rb2view(self), ray_convert_to_rect(val));
  return val;
}

#wObject



53
# File 'lib/ray/view.rb', line 53

def w; size.w;   end

#w=(val) ⇒ Object



58
# File 'lib/ray/view.rb', line 58

def w=(val); self.size   = [val, h]; end

#xObject



51
# File 'lib/ray/view.rb', line 51

def x; center.x; end

#x=(val) ⇒ Object



56
# File 'lib/ray/view.rb', line 56

def x=(val); self.center = [val, y]; end

#yObject



52
# File 'lib/ray/view.rb', line 52

def y; center.y; end

#y=(val) ⇒ Object



57
# File 'lib/ray/view.rb', line 57

def y=(val); self.center = [x, val]; end

#zoom_by(value) ⇒ Object

Increases zoom (making object appear bigger). The center of the view and its viewport aren’t affected by this.

Parameters:

See Also:



30
31
32
# File 'lib/ray/view.rb', line 30

def zoom_by(value)
  self.size /= value
end