summaryrefslogtreecommitdiffhomepage
path: root/dragon/geometry_docs.rb
blob: cf8ed14eeb0e5fea90551ee779f434c84affe696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
48
49
50
51
52
# coding: utf-8
# Copyright 2019 DragonRuby LLC
# MIT License
# geometry_docs.rb has been released under MIT (*only this file*).

module GeometryDocs
  def docs_method_sort_order
    [:docs_class, :docs_scale_rect]
  end

  def docs_class
    <<-S
* DOCS: ~Geometry~

The Geometry module contains methods for calculations that are frequently used in game development.

S
  end

  def docs_scale_rect
    <<-S
* DOCS: ~GTK::Geometry#scale_rect~

Given an array with 4 elements representing a rect [x, y, w, h], this function returns a scaled rect. It accepts three arguments:

~ratio~: the ratio by which to scale the rect. A ratio of 2 will double the dimensions of the rect while a ratio of 0.5 will halve its dimensions. 

~anchor_x~ and ~anchor_y~ specify the point within the rect from which to resize it. Setting both to 0 will affect the width and height of the rect, leaving x and y unchanged. Setting both to 0.5 will scale all sides of the rect proportionally from the center.

The ~scale_rect~ method can be applied directly to a sprite or other primitives. See CHEATSHEET: How to Scale a Sprite.

#+begin_src ruby
  def tick args
    #       x,   y,   w,   h
    my_rect = [100, 100, 200, 200]

    # halve the dimensions of the rect: 
    #                             ratio, anchor_x, anchor_y
    new_rect = my_rect.scale_rect(0.5,   0.5,      0.5)
  
    puts new_rect # => [150.0, 150.0, 100.0, 100.0]
  end
#+end_src

S
  end
end

module Geometry
  extend Docs
  extend GeometryDocs
end