forked from rubygame/rubygame
/
event_triggers.rb
240 lines (185 loc) · 4.5 KB
/
event_triggers.rb
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#--
# Rubygame -- Ruby code and bindings to SDL to facilitate game creation
# Copyright (C) 2004-2008 John Croisant
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#++
require 'rubygame'
module Rubygame
class AllTrigger
def initialize( *triggers )
@triggers = triggers
end
def match?( event )
@triggers.all? { |trigger| trigger.match? event }
end
end
class AnyTrigger
def initialize( *triggers )
@triggers = triggers
end
def match?( event )
@triggers.any? { |trigger| trigger.match? event }
end
end
class AttrTrigger
def initialize( attributes )
@attributes = attributes
end
def match?( event )
@attributes.all? { |key, value|
event.respond_to?(key) and (event.send(key) == value)
}
end
end
class BlockTrigger
def initialize( &block )
raise ArgumentError, "BlockTrigger needs a block" unless block_given?
@block = block
end
def match?( event )
@block.call( event ) == true
end
end
# class CollisionTrigger
#
# # type can be :start, :hold, :end, or :any
# def initialize( a=:any, b=:any, type=:any )
# @a, @b, @type = a, b, type
# end
#
# def match?( event )
# matching_types =
# case( event )
# when CollisionStartEvent
# [:start, :any]
# when CollisionEvent
# [:hold, :any]
# when CollisionEndEvent
# [:end, :any]
# else
# []
# end
#
# matching_types.include?(@type) and _has_objects?( event )
# end
#
# private
#
# # True if the event concerns the object(s) this trigger
# # is watching. It's not important that the event's pair order
# # matches the trigger's pair order.
# def _has_objects?( event )
# obs = [event.a, event.a.sprite, event.b, event.b.sprite]
#
# (@a == :any or obs.include?(@a)) and \
# (@b == :any or obs.include?(@b))
# end
#end
class InstanceOfTrigger
def initialize( klass )
@klass = klass
end
def match?( event )
event.instance_of?( @klass )
end
end
class KeyPressTrigger
def initialize( key=:any, mods=:any )
@key = key
@mods = mods
end
def match?( event )
if event.kind_of?( Events::KeyPressed )
((@key == :any) or (event.key == @key)) and \
((@mods == :any) or (@mods == :none and event.modifiers == [])\
or (event.modifiers == @mods))
end
end
end
class KeyReleaseTrigger
def initialize( key=:any, mods=:any )
@key = key
@mods = mods
end
def match?( event )
if event.kind_of?( Events::KeyReleased )
((@key == :any) or (event.key == @key)) and \
((@mods == :any) or (@mods == :none and event.modifiers == [])\
or (event.modifiers == @mods))
end
end
end
class KindOfTrigger
def initialize( klass )
@klass = klass
end
def match?( event )
event.kind_of?( @klass )
end
end
class MouseClickTrigger
def initialize( button=:any, area=:anywhere )
@button = button
@area = area
end
def match?( event )
if event.kind_of?( Events::MousePressed )
((@button == :any) or (event.button == @button)) and \
((@area == :anywhere) or (@area.contain_vect?( event.world_pos )))
else
false
end
end
end
class MouseHoverTrigger
def initialize( button=:any, area=:anywhere )
@button = button
@area = area
end
def match?( event )
if event.kind_of?( Events::MouseMoved )
((@button == :any) or (event.buttons.include?(@button))) and \
((@area == :anywhere) or (@area.contain_vect?( event.world_pos )))
else
false
end
end
end
class MouseReleaseTrigger
def initialize( button=:any, area=:anywhere )
@button = button
@area = area
end
def match?( event )
if event.kind_of?( Events::MouseReleased )
((@button == :any) or (event.button == @button)) and \
((@area == :anywhere) or (@area.contain_vect?( event.world_pos )))
else
false
end
end
end
# class TickTrigger
# def match?( event )
# event.kind_of?( Events::ClockTicked )
# end
# end
class YesTrigger
def match?( event )
true
end
end
end