summaryrefslogtreecommitdiffhomepage
path: root/dragon/grid.rb
blob: 2cfa04ea274ef7e21159939b6a553bafe0b9d1e0 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# coding: utf-8
# Copyright 2019 DragonRuby LLC
# MIT License
# grid.rb has been released under MIT (*only this file*).
# Contributors outside of DragonRuby who also hold Copyright: _Tradam

module GTK
  class Grid
    include Serialize

    # Returns which direction y is positive in. Negative means upward.
    #
    # @return [Float] `-1.0` or `1.0`
    attr_accessor :screen_y_direction

    # The coordinate system currently in use.
    #
    # @return [Symbol] `:bottom_left`, `:center` or `:top_left`
    attr_accessor :name

    # Returns the "x" coordinate indicating the bottom of the screen.
    #
    # @return [Float]
    attr_accessor :bottom

    # Returns the "x" coordinate indicating the top of the screen.
    #
    # @return [Float]
    attr_accessor :top

    # Returns the "y" coordinate indicating the left of the screen.
    #
    # @return [Float]
    attr_accessor :left

    # Returns the "y" coordinate indicating the right of the screen.
    #
    # @return [Float]
    attr_accessor :right

    # Returns the "x" coordinate indicating the center of the screen.
    #
    # @return [Float]
    attr_accessor :center_x

    # Returns the "y" coordinate indicating the center of the screen.
    #
    # @return [Float]
    attr_accessor :center_y

    # Returns the bottom left and top right coordinates in a single list.
    #
    # @return [[Float, Float, Float, Float]]
    attr_accessor :rect

    # Returns the "x" coordinate of the origin.
    #
    # @return [Float]
    attr_accessor :origin_x

    # Returns the "y" coordinate of the origin.
    #
    # @return [Float]
    attr_accessor :origin_y

    attr_accessor :left_margin, :bottom_margin

    def initialize runtime
      @runtime = runtime
      @ffi_draw = runtime.ffi_draw
      origin_bottom_left!
    end

    # Returns `x` plus the origin "x".
    #
    # @return [Float]
    def transform_x x
      @origin_x + x
    end

    # Returns `x` minus the origin "x".
    #
    # @return [Float]
    def untransform_x x
      x - @origin_x
    end

    # Returns `y` plus the origin "y".
    #
    # @return [Float]
    def transform_y y
      @origin_y + y * screen_y_direction
    end

    # Returns `y` minus the origin "y".
    #
    # @return [Float]
    def untransform_y y
      @origin_y + y * screen_y_direction
    end

    def ffi_draw
      @ffi_draw
    end

    def ffi_draw= value
      @ffi_draw = value
    end

    # Sets the rendering coordinate system to have its origin in the bottom left.
    #
    # @return [void]
    # @gtk
    def origin_bottom_left!
      return if @name == :bottom_left
      @name = :bottom_left
      @origin_x = 0.0
      @origin_y = @runtime.logical_height
      @left   = 0.0
      @right  = @runtime.logical_width
      @top    = @runtime.logical_height
      @bottom = 0.0
      @left_margin = 0.0
      @bottom_margin = 0.0
      @center_x = @runtime.logical_width.half
      @center_y = @runtime.logical_height.half
      @rect   = [@left, @bottom, @runtime.logical_width, @runtime.logical_height].rect
      @center = [@center_x, @center_y].point
      @screen_y_direction = -1.0
      @ffi_draw.set_grid @origin_x, @origin_y, @screen_y_direction
    end

    # Sets the rendering coordinate system to have its origin in the center.
    #
    # @return [void]
    # @gtk
    def origin_center!
      return if @name == :center
      @name = :center
      @origin_x = @runtime.logical_width.half
      @origin_y = @runtime.logical_height.half
      @left   =  [email protected]_width.half
      @right  =   @runtime.logical_width.half
      @top    =   @runtime.logical_height.half
      @bottom =  [email protected]_height.half
      @center_x = 0.0
      @center_y = 0.0
      @rect   = [@left, @bottom, @runtime.logical_width, @runtime.logical_height].rect
      @center = [@center_x, @center_y].point
      @screen_y_direction = -1.0
      @ffi_draw.set_grid @origin_x, @origin_y, @screen_y_direction
    end

    # Sets the rendering coordinate system to have its origin in the top left.
    #
    # @return [void]
    # @gtk
    def origin_top_left!
      return if @name == :top_left
      @name = :top_left
      @origin_x = 0.0
      @origin_y = 0.0
      @left   = 0.0
      @right  = @runtime.logical_width
      @top    = 0.0
      @bottom = @runtime.logical_height
      @center_x = @runtime.logical_width.half
      @center_y = @runtime.logical_height.half
      @rect   = [@left, @runtime.logical_height, @runtime.logical_width, @top].rect
      @center = [@center_x, @center_y].point
      @screen_y_direction = 1.0
      @ffi_draw.set_grid @origin_x, @origin_y, @screen_y_direction
    end

    # The logical width used for rendering.
    #
    # @return [Float]
    def w
      @runtime.logical_width
    end

    # Half the logical width used for rendering.
    #
    # @return [Float]
    def w_half
      w.half
    end

    # The logical height used for rendering.
    #
    # @return [Float]
    def h
      @runtime.logical_height
    end

    # Half the logical height used for rendering.
    #
    # @return [Float]
    def h_half
      h.half
    end

    # Returns the coordinates indicating the center of the screen.
    #
    # @return [[Float, Float]]
    def center
      @center
    end

    # Returns the coordinates indicating the bottom right of the screen.
    #
    # @return [[Float, Float]]
    def bottom_right
      [@right, @bottom].point
    end

    def x
      0
    end

    def y
      0
    end
  end
end