rwayland 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/example/01_wayland-book.rb +2 -4
- data/example/02_memopad.rb +192 -0
- data/example/03_image_pool.rb +64 -0
- data/example/04_cursor_theme.rb +93 -0
- data/example/miw/point.rb +31 -0
- data/example/miw/rectangle.rb +221 -0
- data/example/miw/size.rb +30 -0
- data/example/miw/wl/popup_window.rb +8 -0
- data/example/miw/wl/private.rb +235 -0
- data/example/miw/wl/toplevel.rb +648 -0
- data/example/miw/wl/window.rb +338 -0
- data/example/miw/wl.rb +60 -0
- data/lib/wayland/cairo_support.rb +7 -1
- data/lib/wayland/cursor_support/cursor.rb +81 -0
- data/lib/wayland/cursor_support/xcursor.rb +227 -0
- data/lib/wayland/cursor_support.rb +21 -0
- data/lib/wayland/object_manager.rb +0 -10
- data/lib/wayland/protocol.rb +1 -0
- data/lib/wayland/shared_memory.rb +89 -9
- data/lib/wayland/util/image_pool.rb +56 -0
- data/lib/wayland/wl/display.rb +13 -0
- metadata +16 -5
- data/example/02_window.rb +0 -180
- data/example/rwaylandtk/custom_wl_interface.rb +0 -182
- data/example/rwaylandtk/toplevel_window.rb +0 -208
- data/example/rwaylandtk.rb +0 -27
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 30fb04226f0252800fc03384d1587d0a9c722ea2ecf6108fb43aad6f0b291d55
|
|
4
|
+
data.tar.gz: 6125cf17c822977a0e2524fb234a1c885d21f3a9356885b97b6e09faf6fef7db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2a4ab4d426f67d6fbc5391ce3b1e651730fe551f888533d674243770b25b5ddcde505c493e7485e20bc0dafbb9c01696f48a516167f66b6823e9f3bcb6db2d8b
|
|
7
|
+
data.tar.gz: 5ac89a432e0fbb0923d8c7249e3d64b43a83338028eb13fe6b02938482dee6143e24b510ed177e79c5d0bc09d11928e2c3dcecddc8f4a2398c213824cc8e952b
|
data/example/01_wayland-book.rb
CHANGED
|
@@ -36,11 +36,9 @@ module WaylandBook
|
|
|
36
36
|
|
|
37
37
|
shared_memory = Wayland::SharedMemory.new size: size
|
|
38
38
|
|
|
39
|
-
data = Fiddle::Pointer.new shared_memory.address
|
|
40
39
|
pool = display[:wl_shm].create_pool(shared_memory.fd, size)
|
|
41
40
|
buffer = pool.create_buffer(0, width, height, stride, Wayland::Wl::Shm[:format].xrgb8888)
|
|
42
41
|
pool.destroy
|
|
43
|
-
shared_memory.close
|
|
44
42
|
|
|
45
43
|
offset = state.offset.to_i % 8
|
|
46
44
|
|
|
@@ -50,9 +48,9 @@ module WaylandBook
|
|
|
50
48
|
height.times do |y|
|
|
51
49
|
width.times do |x|
|
|
52
50
|
if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8)
|
|
53
|
-
|
|
51
|
+
shared_memory.write(y * stride + x * 4, ff6666ee, 4)
|
|
54
52
|
else
|
|
55
|
-
|
|
53
|
+
shared_memory.write(y * stride + x * 4, ffeeeeee, 4)
|
|
56
54
|
end
|
|
57
55
|
end
|
|
58
56
|
end
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
$: << File.dirname(__FILE__)
|
|
2
|
+
require "pango"
|
|
3
|
+
require "miw/wl"
|
|
4
|
+
require "miw/wl/toplevel"
|
|
5
|
+
|
|
6
|
+
class ToplevelWindow
|
|
7
|
+
def initialize(width, height)
|
|
8
|
+
@window = MiW::Wl::Toplevel.new(self, width, height)
|
|
9
|
+
@text = File.read __FILE__
|
|
10
|
+
@ty = 0
|
|
11
|
+
@bpx = 0
|
|
12
|
+
@bpy = 0
|
|
13
|
+
@cursor = 0
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def draw(context, rect)
|
|
17
|
+
# background
|
|
18
|
+
size = @window.client_size
|
|
19
|
+
width = size.width
|
|
20
|
+
height = size.height
|
|
21
|
+
context.set_source_color [0.8, 0.8, 0.8, 1.0]
|
|
22
|
+
context.rectangle 0, 0, width, height
|
|
23
|
+
context.fill
|
|
24
|
+
|
|
25
|
+
layout = context.create_pango_layout
|
|
26
|
+
layout.font_description = "monospace 10"
|
|
27
|
+
layout.text = @text
|
|
28
|
+
|
|
29
|
+
if @cursor.nil?
|
|
30
|
+
lx = @bpx
|
|
31
|
+
ly = @bpy - @ty
|
|
32
|
+
_, @cursor, _ = layout.xy_to_index lx * Pango::SCALE, ly * Pango::SCALE
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Cursor
|
|
36
|
+
pos = layout.index_to_pos(@cursor)
|
|
37
|
+
context.set_source_color [0, 0.9, 0, 1]
|
|
38
|
+
w = pos.width > 0 ? pos.width / Pango::SCALE : 10
|
|
39
|
+
h = pos.height > 0 ? pos.height / Pango::SCALE : 10
|
|
40
|
+
context.rectangle pos.x / Pango::SCALE, pos.y / Pango::SCALE + @ty, w, h
|
|
41
|
+
context.fill
|
|
42
|
+
|
|
43
|
+
# Text
|
|
44
|
+
context.move_to 0, @ty
|
|
45
|
+
context.set_source_color [0.2, 0.3, 0.2, 1]
|
|
46
|
+
context.show_pango_layout layout
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def hit_test(x, y)
|
|
50
|
+
:client
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def resized
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def activated(active)
|
|
57
|
+
@active = active
|
|
58
|
+
invalidate
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def pointer_enter(*_)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def pointer_leave(*_)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def pointer_motion(time, x, y)
|
|
68
|
+
@px = x.to_i
|
|
69
|
+
@py = y.to_i
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def pointer_button(time, button, state)
|
|
73
|
+
if state == 1
|
|
74
|
+
@cursor = nil
|
|
75
|
+
@bpx = @px
|
|
76
|
+
@bpy = @py
|
|
77
|
+
invalidate
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def pointer_axis(time, axis, value)
|
|
82
|
+
if axis == 0
|
|
83
|
+
@ty -= value * 2
|
|
84
|
+
@bpy -= value * 2
|
|
85
|
+
end
|
|
86
|
+
invalidate
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def line_head(index)
|
|
90
|
+
answer = 0
|
|
91
|
+
if index > 0
|
|
92
|
+
h = @text.rindex("\n", index - 1)
|
|
93
|
+
answer = h + 1 if h
|
|
94
|
+
end
|
|
95
|
+
answer
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def next_line(index)
|
|
99
|
+
n = @text.index("\n", index)
|
|
100
|
+
(n ? n + 1 : nil)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def cursor_up
|
|
104
|
+
return if @cursor <= 0
|
|
105
|
+
@pd ||= 0
|
|
106
|
+
lh0 = line_head @cursor
|
|
107
|
+
return if lh0 == 0
|
|
108
|
+
d = [@cursor - lh0, @pd].max
|
|
109
|
+
lh1 = line_head lh0 - 1
|
|
110
|
+
if lh1 + d < lh0
|
|
111
|
+
@pd = d
|
|
112
|
+
@cursor = lh1 + d
|
|
113
|
+
else
|
|
114
|
+
@pd = d
|
|
115
|
+
@cursor = lh0 - 1
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def cursor_down
|
|
120
|
+
@pd ||= 0
|
|
121
|
+
lh1 = next_line @cursor
|
|
122
|
+
if lh1
|
|
123
|
+
lh2 = next_line(lh1) || @text.size
|
|
124
|
+
lh0 = line_head @cursor
|
|
125
|
+
d = [@cursor - lh0, @pd].max
|
|
126
|
+
if lh1 + d < lh2
|
|
127
|
+
@pd = 0
|
|
128
|
+
@cursor = lh1 + d
|
|
129
|
+
else
|
|
130
|
+
@pd = d
|
|
131
|
+
@cursor = lh2 - 1
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def key_down(key)
|
|
137
|
+
case key
|
|
138
|
+
when 0x20..0x7e
|
|
139
|
+
@text.insert @cursor, key.chr
|
|
140
|
+
@cursor += 1
|
|
141
|
+
when 0xff0d
|
|
142
|
+
@text.insert @cursor, "\n"
|
|
143
|
+
@cursor += 1
|
|
144
|
+
@pd = 0
|
|
145
|
+
when 0xff51 # LEFT
|
|
146
|
+
@cursor -= 1 if @cursor > 0
|
|
147
|
+
@pd = 0
|
|
148
|
+
when 0xff52 # UP
|
|
149
|
+
cursor_up
|
|
150
|
+
when 0xff53 # RIGHT
|
|
151
|
+
@cursor += 1 if @cursor < @text.size
|
|
152
|
+
@pd = 0
|
|
153
|
+
when 0xff54 # DOWN
|
|
154
|
+
cursor_down
|
|
155
|
+
else
|
|
156
|
+
p [key.to_s(16)]
|
|
157
|
+
end
|
|
158
|
+
invalidate
|
|
159
|
+
@window.lasy_update
|
|
160
|
+
@window.commit_if_needed
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def pulse
|
|
164
|
+
return unless @key
|
|
165
|
+
@key_repeat_timeout -= 1
|
|
166
|
+
if @key_repeat_timeout == 0
|
|
167
|
+
@key_repeat_timeout = 2
|
|
168
|
+
key_down @key
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def keyboard_key(time, key, state)
|
|
173
|
+
if state == 1
|
|
174
|
+
key_down key
|
|
175
|
+
@key = key
|
|
176
|
+
@key_repeat_timeout = 10
|
|
177
|
+
else
|
|
178
|
+
@key = nil
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def invalidate
|
|
183
|
+
size = @window.client_size
|
|
184
|
+
@window.invalidate_client 0, 0, size.width, size.height
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
if __FILE__ == $0
|
|
189
|
+
MiW::Wl.init(cursor_theme: "default", cursor_size: 24)
|
|
190
|
+
toplevel_window = ToplevelWindow.new 640, 480
|
|
191
|
+
MiW::Wl.main_loop
|
|
192
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'wayland'
|
|
2
|
+
require 'wayland/shared_memory'
|
|
3
|
+
require 'wayland/util/image_pool'
|
|
4
|
+
require 'cairo'
|
|
5
|
+
require 'logger'
|
|
6
|
+
|
|
7
|
+
module Example03
|
|
8
|
+
|
|
9
|
+
SIZE = 128
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def main
|
|
14
|
+
display = Wayland::Wl::Display.connect
|
|
15
|
+
display.set_logger Logger.new(STDOUT)
|
|
16
|
+
display.get_registry
|
|
17
|
+
display.roundtrip
|
|
18
|
+
|
|
19
|
+
# surface
|
|
20
|
+
compositor = display[:wl_compositor]
|
|
21
|
+
wl_surface = compositor.create_surface
|
|
22
|
+
|
|
23
|
+
# image pool
|
|
24
|
+
ipool = Wayland::Util::ImagePool.new display[:wl_shm]
|
|
25
|
+
|
|
26
|
+
image = Cairo::ImageSurface.new Cairo::FORMAT_ARGB32, SIZE, SIZE
|
|
27
|
+
context = Cairo::Context.new image
|
|
28
|
+
|
|
29
|
+
# circle
|
|
30
|
+
context.set_source_rgb 1, 1, 1
|
|
31
|
+
context.rectangle 0, 0, SIZE, SIZE
|
|
32
|
+
context.fill
|
|
33
|
+
|
|
34
|
+
context.line_width = 5
|
|
35
|
+
context.set_source_rgb 0.5, 0.5, 0.5
|
|
36
|
+
context.circle SIZE / 2, SIZE / 2, SIZE / 4
|
|
37
|
+
context.stroke_preserve
|
|
38
|
+
|
|
39
|
+
ipool.add_image SIZE, SIZE, SIZE * 4, Wayland::Wl::Shm[:format].xrgb8888, image.data
|
|
40
|
+
|
|
41
|
+
# fill
|
|
42
|
+
context.fill
|
|
43
|
+
|
|
44
|
+
ipool.add_image SIZE, SIZE, SIZE * 4, Wayland::Wl::Shm[:format].xrgb8888, image.data
|
|
45
|
+
|
|
46
|
+
xdg_surface = display[:xdg_wm_base].get_xdg_surface wl_surface
|
|
47
|
+
xdg_surface.on :configure do |serial|
|
|
48
|
+
xdg_surface.ack_configure serial
|
|
49
|
+
buffer = ipool.get_buffer 0
|
|
50
|
+
wl_surface.attach buffer, 0, 0
|
|
51
|
+
wl_surface.commit
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
xdg_toplevel = xdg_surface.get_toplevel
|
|
55
|
+
xdg_toplevel.set_title("Example Client")
|
|
56
|
+
wl_surface.commit
|
|
57
|
+
|
|
58
|
+
loop { display.dispatch }
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
if __FILE__ == $0
|
|
63
|
+
Example03.main
|
|
64
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
$: << File.dirname(__FILE__)
|
|
2
|
+
require "miw/wl"
|
|
3
|
+
require "miw/wl/toplevel"
|
|
4
|
+
require "wayland/cursor_support"
|
|
5
|
+
|
|
6
|
+
class ToplevelWindow
|
|
7
|
+
def initialize(width, height, cursor_theme)
|
|
8
|
+
@window = MiW::Wl::Toplevel.new(self, width, height)
|
|
9
|
+
@cursor_theme = cursor_theme
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def draw(context, rect)
|
|
13
|
+
size = @window.client_size
|
|
14
|
+
width = size.width
|
|
15
|
+
height = size.height
|
|
16
|
+
context.set_source_color [0.8, 0.8, 0.8, 1.0]
|
|
17
|
+
context.rectangle 0, 0, width, height
|
|
18
|
+
context.fill
|
|
19
|
+
|
|
20
|
+
# cursors
|
|
21
|
+
x = y = 0
|
|
22
|
+
max_height = 0
|
|
23
|
+
@cursor_theme.names.each do |name|
|
|
24
|
+
cursor = @cursor_theme[name]
|
|
25
|
+
cursor.count.times do |index|
|
|
26
|
+
cursor_image = cursor[index]
|
|
27
|
+
data = cursor_image.pixels
|
|
28
|
+
stride = cursor_image.width * 4
|
|
29
|
+
max_height = [max_height, cursor_image.height].max
|
|
30
|
+
# p [cursor_image.width, cursor_image.height, data.bytesize, stride * cursor_image.height]
|
|
31
|
+
image_surface = Cairo::ImageSurface.new data,
|
|
32
|
+
Cairo::Format::ARGB32,
|
|
33
|
+
cursor_image.width,
|
|
34
|
+
cursor_image.height,
|
|
35
|
+
stride
|
|
36
|
+
context.set_source image_surface, x, y
|
|
37
|
+
context.rectangle x, y, cursor_image.width, cursor_image.height
|
|
38
|
+
context.fill
|
|
39
|
+
x += cursor_image.width
|
|
40
|
+
if x + cursor_image.width > @window.client_size.width
|
|
41
|
+
y += max_height
|
|
42
|
+
x = 0
|
|
43
|
+
max_height = 0
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def hit_test(x, y)
|
|
50
|
+
:client
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def resized
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def activated(active)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def pointer_enter(*_)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def pointer_leave(*_)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def pointer_motion(time, x, y)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def pointer_button(time, button, state)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def pointer_axis(time, axis, value)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def pulse
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def keyboard_key(time, key, state)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def invalidate
|
|
81
|
+
size = @window.client_size
|
|
82
|
+
@window.invalidate_client 0, 0, size.width, size.height
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
if __FILE__ == $0
|
|
87
|
+
MiW::Wl.init(cursor_theme: "default", cursor_size: 24)
|
|
88
|
+
display = MiW::Wl.display_instance
|
|
89
|
+
wl_shm = display[:wl_shm]
|
|
90
|
+
cursor_theme = MiW::Wl.cursor_theme
|
|
91
|
+
toplevel_window = ToplevelWindow.new 640, 480, cursor_theme
|
|
92
|
+
MiW::Wl.main_loop
|
|
93
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
module MiW
|
|
3
|
+
module PointMixin
|
|
4
|
+
def offset_by(a1, a2 = nil)
|
|
5
|
+
if a2
|
|
6
|
+
self.x += a1
|
|
7
|
+
self.y += a2
|
|
8
|
+
else
|
|
9
|
+
self.x += a1.x
|
|
10
|
+
self.y += a1.y
|
|
11
|
+
end
|
|
12
|
+
self
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def +(other)
|
|
16
|
+
self.dup.offset_by other.x, other.y
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def -(other)
|
|
20
|
+
self.dup.offset_by -other.x, -other.y
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def to_a
|
|
24
|
+
[x, y]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class Point < Struct.new(:x, :y)
|
|
29
|
+
include PointMixin
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
|
|
2
|
+
require 'miw/point'
|
|
3
|
+
|
|
4
|
+
module MiW
|
|
5
|
+
module RectangleMixin
|
|
6
|
+
def left; x end
|
|
7
|
+
def top; y end
|
|
8
|
+
def right; x + width end
|
|
9
|
+
def bottom; y + height end
|
|
10
|
+
|
|
11
|
+
def left_top; Point.new left, top end
|
|
12
|
+
def left_bottom; Point.new left, bottom end
|
|
13
|
+
def right_top; Point.new right, top end
|
|
14
|
+
def right_bottom; Point.new right, bottom end
|
|
15
|
+
|
|
16
|
+
def [](key)
|
|
17
|
+
case key
|
|
18
|
+
when String, Symbol
|
|
19
|
+
self.__send__ key.to_sym
|
|
20
|
+
else
|
|
21
|
+
super
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def []=(key, value)
|
|
26
|
+
case key
|
|
27
|
+
when String, Symbol
|
|
28
|
+
sym = "#{key}=".to_sym
|
|
29
|
+
self.__send__ sym, value
|
|
30
|
+
else
|
|
31
|
+
super
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def valid?
|
|
36
|
+
width >= 0 && height >= 0
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def hit_test(x, y)
|
|
40
|
+
ix = (x < self.x ? 0 : (x < self.right ? 1 : 2))
|
|
41
|
+
iy = (y < self.y ? 0 : (y < self.bottom ? 1 : 2))
|
|
42
|
+
return ix + iy * 3
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def contain?(*a)
|
|
46
|
+
case a.length
|
|
47
|
+
when 1
|
|
48
|
+
aleft, atop, aright, abottom = a.first.to_a
|
|
49
|
+
when 2
|
|
50
|
+
aleft, atop = *a
|
|
51
|
+
aright, abottom = aleft, atop
|
|
52
|
+
when 4
|
|
53
|
+
aleft, atop, aright, abottom = *a
|
|
54
|
+
else
|
|
55
|
+
raise ArgumentError, "wrong number of arguments #{a.length} for 1, 2 or 4"
|
|
56
|
+
end
|
|
57
|
+
left <= aleft && top <= atop && right >= aright && bottom >= abottom
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def union(*a)
|
|
61
|
+
case a.length
|
|
62
|
+
when 1
|
|
63
|
+
oleft, otop, w, h = a.first.to_a
|
|
64
|
+
when 4
|
|
65
|
+
oleft, otop, w, h = *a
|
|
66
|
+
else
|
|
67
|
+
raise ArgumentError
|
|
68
|
+
end
|
|
69
|
+
oright = oleft + w
|
|
70
|
+
obottom = otop + h
|
|
71
|
+
nleft = left < oleft ? left : oleft
|
|
72
|
+
ntop = top < otop ? top : otop
|
|
73
|
+
nright = right > oright ? right : oright
|
|
74
|
+
nbottom = bottom > obottom ? bottom : obottom
|
|
75
|
+
self.x = nleft
|
|
76
|
+
self.y = ntop
|
|
77
|
+
self.width = nright - nleft
|
|
78
|
+
self.height = nbottom - ntop
|
|
79
|
+
self
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def intersect(*a)
|
|
83
|
+
case a.length
|
|
84
|
+
when 1
|
|
85
|
+
other = a.first
|
|
86
|
+
oleft = other.x
|
|
87
|
+
otop = other.y
|
|
88
|
+
oright = oleft + other.width
|
|
89
|
+
obottom = otop + other.height
|
|
90
|
+
when 4
|
|
91
|
+
oleft = a[0]
|
|
92
|
+
otop = a[1]
|
|
93
|
+
oright = oleft + a[2]
|
|
94
|
+
obottom = otop + a[3]
|
|
95
|
+
else
|
|
96
|
+
raise ArgumentError
|
|
97
|
+
end
|
|
98
|
+
right = self.right
|
|
99
|
+
bottom = self.bottom
|
|
100
|
+
nright = right < oright ? right : oright
|
|
101
|
+
nbottom = bottom < obottom ? bottom : obottom
|
|
102
|
+
self.x = oleft if self.x < oleft
|
|
103
|
+
self.y = otop if self.y < otop
|
|
104
|
+
self.width = nright - x
|
|
105
|
+
self.height = nbottom - y
|
|
106
|
+
self
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def inset_by(*a)
|
|
110
|
+
case a.length
|
|
111
|
+
when 1
|
|
112
|
+
if a[0].respond_to? :left
|
|
113
|
+
left = a[0].left
|
|
114
|
+
top = a[0].top
|
|
115
|
+
right = a[0].right
|
|
116
|
+
bottom = a[0].bottom
|
|
117
|
+
elsif Numeric === a[0]
|
|
118
|
+
left = right = top = bottom = a[0]
|
|
119
|
+
else
|
|
120
|
+
raise TypeError, "wrong type of argument 0: should be Numeric or Rectangle"
|
|
121
|
+
end
|
|
122
|
+
when 2
|
|
123
|
+
left = right = a[0]
|
|
124
|
+
top = bottom = a[1]
|
|
125
|
+
when 4
|
|
126
|
+
left, top, right, bottom = a
|
|
127
|
+
else
|
|
128
|
+
raise ArgumentError, "wrong number of arguments #{a.length} for 2 or 4"
|
|
129
|
+
end
|
|
130
|
+
self.x += left
|
|
131
|
+
self.width -= left + right
|
|
132
|
+
self.y += top
|
|
133
|
+
self.height -= top + bottom
|
|
134
|
+
self
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def offset_by(a1, a2 = nil)
|
|
138
|
+
if a2 == nil
|
|
139
|
+
self.x += a1.x
|
|
140
|
+
self.y += a1.y
|
|
141
|
+
else
|
|
142
|
+
self.x += a1
|
|
143
|
+
self.y += a2
|
|
144
|
+
end
|
|
145
|
+
self
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def offset_to(a1, a2 = nil)
|
|
149
|
+
if a2 == nil
|
|
150
|
+
self.x = a1.x
|
|
151
|
+
self.y = a1.y
|
|
152
|
+
else
|
|
153
|
+
self.x = a1
|
|
154
|
+
self.y = a2
|
|
155
|
+
end
|
|
156
|
+
self
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def resize_by(a1, a2 = nil)
|
|
160
|
+
if a2
|
|
161
|
+
self.width += a1
|
|
162
|
+
self.height += a2
|
|
163
|
+
else
|
|
164
|
+
self.width += a1.width
|
|
165
|
+
self.height += a1.height
|
|
166
|
+
end
|
|
167
|
+
self
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def resize_to(a1, a2 = nil)
|
|
171
|
+
if a2
|
|
172
|
+
self.width = a1
|
|
173
|
+
self.height = a2
|
|
174
|
+
else
|
|
175
|
+
self.width = a1.width
|
|
176
|
+
self.height = a1.height
|
|
177
|
+
end
|
|
178
|
+
self
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def int_all
|
|
182
|
+
self.x = self.x.to_i
|
|
183
|
+
self.y = self.y.to_i
|
|
184
|
+
self.width = self.width.to_i
|
|
185
|
+
self.height = self.height.to_i
|
|
186
|
+
self
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def float_all
|
|
190
|
+
self.x = self.x.to_f
|
|
191
|
+
self.y = self.y.to_f
|
|
192
|
+
self.width = self.width.to_f
|
|
193
|
+
self.height = self.height.to_f
|
|
194
|
+
self
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def &(other)
|
|
198
|
+
self.dup.intersect other
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def |(other)
|
|
202
|
+
self.dup.union other
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def size
|
|
206
|
+
Size.new(width, height)
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def to_a
|
|
210
|
+
[x, y, width, height]
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def box
|
|
214
|
+
[x, y, x + width, y + height]
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
class Rectangle < Struct.new(:x, :y, :width, :height)
|
|
219
|
+
include RectangleMixin
|
|
220
|
+
end
|
|
221
|
+
end
|
data/example/miw/size.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
module MiW
|
|
3
|
+
module SizeMixin
|
|
4
|
+
def resize_by(a1, a2 = nil)
|
|
5
|
+
if a2
|
|
6
|
+
self.width += a1
|
|
7
|
+
self.height += a2
|
|
8
|
+
else
|
|
9
|
+
self.width += a1.width
|
|
10
|
+
self.height += a1.height
|
|
11
|
+
end
|
|
12
|
+
self
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def resize_to(a1, a2 = nil)
|
|
16
|
+
if a2
|
|
17
|
+
self.width = a1
|
|
18
|
+
self.height = a2
|
|
19
|
+
else
|
|
20
|
+
self.width = a1.width
|
|
21
|
+
self.height = a1.height
|
|
22
|
+
end
|
|
23
|
+
self
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Size < Struct.new(:width, :height)
|
|
28
|
+
include SizeMixin
|
|
29
|
+
end
|
|
30
|
+
end
|