gglib 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,349 @@
1
+ module GGLib
2
+
3
+ class DebugConsole < Widget
4
+ @@allow=false
5
+ def initialize(name="unnamed", theme=Themes::blank)
6
+ super(name, 0, 0, 640, 480, theme)
7
+ @vline=0
8
+ @oldtext=[]
9
+ @textm=false
10
+ clear
11
+ stickFocus
12
+ end
13
+ def onKeyPress(key)
14
+ if key==WidgetEvent::KeyDelete
15
+ thisline=@text[@line]
16
+ if @textm and thisline.length>0
17
+ @text[@line]=thisline.slice(0,thisline.length-1)
18
+ elsif thisline.length>3
19
+ @text[@line]=thisline.slice(0,thisline.length-1)
20
+ end
21
+ elsif key==WidgetEvent::KeySpace
22
+ @text[@line]+=" "
23
+ elsif key==WidgetEvent::KeyEscape
24
+ sleep
25
+ elsif key==WidgetEvent::KeyEnter
26
+ if @textm
27
+ if @text[@line]=="/endtext"
28
+ @textm=false
29
+ @line+=1
30
+ @text[@line]=">> "
31
+ else
32
+ @line+=1
33
+ @text[@line]=""
34
+ end
35
+ return
36
+ end
37
+ @oldtext.push @text[@line]
38
+ cmd=@text[@line].slice(3,@text[@line].length-3)
39
+ if cmd.slice(cmd.size-3,3)=="/nl"
40
+ @text[@line]=@text[@line].slice(0,@text[@line].length-3)
41
+ @text[@line+1]="|> "
42
+ @line+=1
43
+ else
44
+ runcmd(cmd)
45
+ end
46
+ @vline=@oldtext.size
47
+ elsif key==WidgetEvent::KeyUp
48
+ if @vline-1 >= 0 and
49
+ @vline-=1
50
+ @text[@line]=@oldtext[@vline]
51
+ end
52
+ elsif key==WidgetEvent::KeyDown
53
+ if @vline+1 < @oldtext.size and
54
+ @vline+=1
55
+ @text[@line]=@oldtext[@vline]
56
+ end
57
+ end
58
+ end
59
+ def feedText(char)
60
+ @text[@line]+=char.to_s unless char=="`"
61
+ end
62
+ def justify(ln)
63
+ fin=""
64
+ size=ln.size
65
+ while ln.size>70
66
+ fin+=ln.slice(0,70)+"\fNL"
67
+ ln=ln.slice(70,ln.size)
68
+ end
69
+ fin+=ln.to_s
70
+ return fin
71
+ end
72
+ def respond(text)
73
+ text=text.to_s
74
+ @line+=1
75
+ if text==""
76
+ text="<no output>"
77
+ end
78
+ text=justify(text) if text.index("\fNL")==nil
79
+ text=text.split("\fNL")
80
+ mult=false
81
+ text.each { |ln|
82
+ if ln.slice(0,4)=="\fERR"
83
+ @text[@line]="!> "+ln.strip
84
+ else
85
+ @text[@line]="^> "+ln
86
+ end
87
+ @line+=1
88
+ @text[@line]=">> "
89
+ }
90
+ end
91
+ def clear
92
+ @text=[]
93
+ @text[0]="CONSOLE"
94
+ @text[1]="#########"
95
+ @text[2]="<Press ` (backquote) or enter '/exit' to exit>"
96
+ @text[3]=">> "
97
+ @line=3
98
+ @vline=@oldtext.size-1
99
+ end
100
+ private
101
+ def runcmd(cmd)
102
+ if cmd.slice(0,1)=="/"
103
+ if cmd=="/exit"
104
+ sleep
105
+ return
106
+ elsif cmd=="/clear"
107
+ clear
108
+ return
109
+ elsif cmd=="/help"
110
+ respond("Enter a Ruby command to execute it\fNLEnter '/' followed by a word to execute a console command\fNLFor a list of console commands, enter '/list'")
111
+ return
112
+ elsif cmd=="/list"
113
+ respond("/clear\fNL/clearhist\fNL/endtext\fNL/exit\fNL/help\fNL/hist\fNL/list\fNL/text")
114
+ return
115
+ elsif cmd=="/hist"
116
+ resp=""
117
+ @oldtext.each { |item|
118
+ resp+="\fNL"
119
+ resp+=item
120
+ }
121
+ respond(resp)
122
+ return
123
+ elsif cmd=="/text"
124
+ @textm=true
125
+ @line+=1
126
+ @text[@line]=""
127
+ return
128
+ elsif cmd=="/clearhist"
129
+ @oldtext=[]
130
+ @vline=0
131
+ return
132
+ end
133
+ return
134
+ end
135
+ output=eval("begin\n"+cmd+"\nrescue\n'\fERR: '+$@[0]+': '+$!\nend",TOPLEVEL_BINDING)
136
+ respond(output)
137
+ end
138
+ public
139
+ def sleep
140
+ @text=[]
141
+ @line=0
142
+ @vline=0
143
+ super
144
+ end
145
+ def wakeUp
146
+ @oldtext=[]
147
+ @vline=0
148
+ @text=false
149
+ clear
150
+ stickFocus
151
+ super
152
+ end
153
+ def onStickyFocus
154
+ $window.setTextInput(@textinput)
155
+ end
156
+ def onStickyBlur
157
+ $window.setTextInput(nil)
158
+ end
159
+ def DebugConsole.enabled=(bool)
160
+ @@allow=bool
161
+ end
162
+ def DebugConsole.enabled
163
+ return @@allow
164
+ end
165
+ def draw
166
+ i=0
167
+ @text.each {|line|
168
+ @theme.font.default.draw(line, 10, 10+i*@theme.font.default.height, ZOrder::Text, 1.0, 1.0, 0xffffffff)
169
+ i+=1
170
+ }
171
+ end
172
+ end
173
+
174
+ class TracePanel < Widget
175
+ @@allow=false
176
+ def initialize(name)
177
+ super(name, 0,0,640,60, Themes::tracePanel)
178
+ clear
179
+ end
180
+ def onKeyPress(key)
181
+ if key==WidgetEvent::KeyDelete
182
+ sleep
183
+ elsif key==WidgetEvent::KeyEnter
184
+ clear
185
+ end
186
+ end
187
+ def sput(text)
188
+ @line+=1
189
+ @text[@line]=text.to_s
190
+ end
191
+ def put(text)
192
+ @line+=1
193
+ @text[@line]=text.to_s
194
+ wakeUp
195
+ end
196
+ def <<(text)
197
+ sput(text)
198
+ end
199
+ def *(nullarg=nil)
200
+ wakeUp
201
+ end
202
+ def ~()
203
+ sleep
204
+ end
205
+ def pause
206
+ gets
207
+ end
208
+ def clear
209
+ @text=[]
210
+ @text[0]="TRACE OUTPUT"
211
+ @text[1]="Press DELETE to close, ENTER to clear. (Click to activate.)"
212
+ @line=2
213
+ end
214
+ def TracePanel.enabled=(bool)
215
+ if bool==true
216
+ @@allow=true
217
+ else
218
+ @@allow=false
219
+ end
220
+ end
221
+ def TracePanel.enabled
222
+ return @@allow
223
+ end
224
+ def draw
225
+ i=0
226
+ @text.each {|line|
227
+ @them.font.default.draw(line, 10, 10+i*@font.height, ZOrder::Text, 1.0, 1.0, 0xffffffff)
228
+ i+=1
229
+ }
230
+ end
231
+ end
232
+
233
+ class TextBox < Widget
234
+ CaretColor=0xff646464
235
+ TextColor=0xff646464
236
+ SelColor=0xff33FFFF
237
+ TextPadding=5
238
+ def initialize(name, x, y, len=12, theme=Themes::blank, w=nil,height=nil)
239
+ if width!=nil and height!=nil
240
+ super(name, x, y, x+w, y+h, theme)
241
+ else
242
+ @theme = theme.newInstance(self) #Get specialized instance early
243
+ @theme.setCoords(1,1,1,1)
244
+ super(name, x, y, x+@theme.width, y+@theme.height, theme)
245
+ @theme.setCoords(@x1, @y1, @x2, @y2)
246
+ end
247
+ @textinput=Gosu::TextInput.new
248
+ @offsety=((@theme.height-@theme.font.editable.height)/2).floor
249
+ @maxlength=len
250
+ @x=x
251
+ @y=y
252
+ @drawcursor=0
253
+ @realwidth=@x + TextPadding
254
+ end
255
+ def onMouseOver
256
+ @theme.setOverState unless self.hasStickyFocus?
257
+ end
258
+ def onMouseOut
259
+ @theme.setDefaultState unless self.hasStickyFocus?
260
+ end
261
+ def onMouseClick
262
+ stickFocus
263
+ end
264
+ def onStickyFocus
265
+ @theme.setOverState
266
+ $window.setTextInput(@textinput)
267
+ end
268
+ def onStickyBlur
269
+ @theme.setDefaultState
270
+ $window.setTextInput(nil)
271
+ end
272
+ def width
273
+ return @maxlength
274
+ end
275
+ def text
276
+ return @textinput.text
277
+ end
278
+ def text=(val)
279
+ val=val.slice(0,12) if val.size>12
280
+ @textinput.text = val
281
+ end
282
+ def draw
283
+ text=@textinput.text.slice(0,12)
284
+ if self.hasStickyFocus?
285
+ pos_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.caret_pos]) + TextPadding
286
+ sel_x = @x + @theme.font.editable.text_width(@textinput.text[0...@textinput.selection_start]) + TextPadding
287
+ @realwidth+=@theme.font.editable.text_width(text)
288
+ if pos_x > @realwidth
289
+ pos_x=@realwidth+1
290
+ sel_x=@realwidth+1
291
+ @textinput.text=text
292
+ end
293
+ @realwidth-=@theme.font.editable.text_width(text)
294
+ if @drawcursor < 18
295
+ $window.draw_line(pos_x, @y+@offsety, CaretColor, pos_x, @y+@theme.font.editable.height+@offsety, CaretColor, ZOrder::Text)
296
+ elsif @drawcursor > 36
297
+ @drawcursor=0
298
+ end
299
+ @drawcursor+=1
300
+ $window.draw_quad(sel_x, @y+@offsety, @theme.font.selcolor, pos_x, @y+@offsety, @theme.font.selcolor, sel_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, pos_x, @y+@theme.font.editable.height+@offsety, @theme.font.selcolor, ZOrder::Text-0.5)
301
+ end
302
+ @theme.font.editable.draw(text, @x+TextPadding, @y+@offsety, ZOrder::Text, 1, 1, @theme.font.color)
303
+ end
304
+ end
305
+
306
+ class Button < Widget
307
+ TextColor=0xff000000
308
+ attr_accessor :text
309
+ def initialize(name, text, x, y, onClickPrc=Proc.new{}, theme = Themes::blank)
310
+ super(name, x, y, x+100, y+30, theme)
311
+ @text=text
312
+ @onClickPrc=onClickPrc
313
+ @drawx=x+((100-@theme.font.default.text_width(@text))/2).floor
314
+ @drawy=y+((30-@theme.font.default.height)/2).floor
315
+ end
316
+ def x1=
317
+ super
318
+ @drawx=x+((100-@theme.font.default.text_width(@text))/2).floor
319
+ end
320
+ def y1=
321
+ super
322
+ @drawy=y+((30-@theme.font.default.height)/2).floor
323
+ end
324
+ def onClick
325
+ @onClickPrc.call(self)
326
+ end
327
+ def onMouseOver
328
+ @theme.setOverState
329
+ end
330
+ def onMouseOut
331
+ @theme.setDefaultState
332
+ end
333
+ def draw
334
+ @theme.font.default.draw(@text, @drawx, @drawy, ZOrder::Text, 1, 1, @theme.font.color)
335
+ end
336
+ end
337
+
338
+ class Label < Widget
339
+ attr_accessor :text
340
+ def initialize(name, text, x, y, theme = Themes::blank)
341
+ @text = text
342
+ super(name,x,y,x+100,y+30,theme)
343
+ end
344
+ def draw
345
+ @theme.font.default.draw(@text, x, y, ZOrder::Text, 1, 1, @theme.font.color)
346
+ end
347
+ end
348
+
349
+ end #module GGLib
@@ -0,0 +1,13 @@
1
+ $gglroot = File.expand_path(File.dirname(__FILE__))
2
+
3
+ $theme_init_hook = Proc.new { }
4
+
5
+ require $gglroot+"/carray"
6
+ require $gglroot+"/tile"
7
+ require $gglroot+"/image"
8
+ require $gglroot+"/camera"
9
+ require $gglroot+"/widget"
10
+ require $gglroot+"/state"
11
+ require $gglroot+"/mouse"
12
+ require $gglroot+"/window"
13
+ require $gglroot+"/theme"
@@ -0,0 +1,177 @@
1
+ module GGLib
2
+
3
+ class Animation
4
+ @@animations = {}
5
+ attr_reader :animObj,:name,:src,:speed, :width, :height, :loop
6
+ def initialize(name,src,speed,width,height,loop=true)
7
+ @animObj=Gosu::Image::load_tiles($window,src,width,height,false)
8
+ @src=src
9
+ @name=name
10
+ @width=width
11
+ @height=height
12
+ @speed=speed
13
+ @loop=loop
14
+ @started=false
15
+ @done=false
16
+ @@animations[@name]=self
17
+ end
18
+ def del
19
+ @@animations.delete_at(@name)
20
+ end
21
+ def Animation.get(at)
22
+ if @@animations[at] !=nil
23
+ return @@animations[at]
24
+ else
25
+ puts caller
26
+ STDIN.gets
27
+ raise "Animation resource '#{at}' does not exist."
28
+ end
29
+ end
30
+ def data
31
+ return @animObj
32
+ end
33
+ def done
34
+ return true if @loop and (Gosu::milliseconds / @speed % @animObj.size)==@animObj.size-1
35
+ return false if @loop
36
+ return nil
37
+ end
38
+ def restart
39
+ @started=false
40
+ @done=false
41
+ end
42
+ def draw(x,y,z)
43
+ if @loop
44
+ @animObj[(Gosu::milliseconds / @speed % @animObj.size)].draw(x,y,z) #looping mode
45
+ return true #signifies that the animation is still playing
46
+ else
47
+ if not @started
48
+ start=(Gosu::milliseconds / @speed % @animObj.size)
49
+ @started=true
50
+ @map=[]
51
+ i=0
52
+ start.upto(@animObj.size-1) { |index|
53
+ @map[index]=i
54
+ i+=1
55
+ }
56
+ if start-1>=0
57
+ 0.upto(start-1) { |index|
58
+ @map[index]=i
59
+ i+=1
60
+ }
61
+ end
62
+ end
63
+ index=@map[(Gosu::milliseconds / @speed % @animObj.size)]
64
+ if not @done and index < @animObj.size - 1 #single play mode
65
+ @animObj[index].draw(x,y,z)
66
+ return true #singifies that the animation is still playing
67
+ else
68
+ @done=true
69
+ @animObj[@animObj.size - 1].draw(x,y,z)
70
+ return false #signifies that the animation is done. The last frame will be played continueuosly. The caller can take further action
71
+ end
72
+ end
73
+ end
74
+ def Animation.deleteAllAnimations
75
+ @@animations={}
76
+ end
77
+ end
78
+
79
+ class MapImage < Animation
80
+ BuildingSpriteRoot="img/sprites/map/buildings/"
81
+ ItemSpriteRoot="img/sprites/map/items/"
82
+ TerrainSpriteRoot="img/sprites/map/terrain/"
83
+ Animation=true
84
+ Image=false
85
+ def initialize(name,src,speed,width,height,loop=true)
86
+ src=subVars(src)
87
+ super
88
+ end
89
+ def initialize(name,src)
90
+ src=subVars(src)
91
+ @src=src
92
+ @name=name
93
+ @animObj=Gosu::Image.new($window,src,width,height,false)
94
+ @type=Image
95
+ end
96
+ def draw(x,y,z)
97
+ if Animation
98
+ super
99
+ else
100
+ @animObj.draw(x,y,z)
101
+ end
102
+ end
103
+ def MapImage.get(at)
104
+ return Animation.get(at)
105
+ end
106
+ end
107
+
108
+ class Image
109
+ attr_reader :obj,:x,:y,:z,:id,:src,:descr
110
+ def initialize(x,y,z,src,hardBorders=true,tracer="None",no=nil)
111
+ @obj=Gosu::Image.new($window,src,hardBorders)
112
+ @src=src
113
+ @x=x
114
+ @y=y
115
+ @z=z
116
+ @id=no
117
+ @descr=tracer
118
+ end
119
+ def draw(x=@x,y=@y,z=@z)
120
+ @obj.draw(x,y,z)
121
+ end
122
+ def setPos(x,y=@y,z=@z)
123
+ @x=x
124
+ @y=y
125
+ @z=z
126
+ end
127
+ def del
128
+ @obj,@x,@y,@z,@id=nil
129
+ end
130
+ end
131
+
132
+ class Texture < Image
133
+ attr_reader :obj,:x1,:y1,:x2,:y2,:z,:id,:src,:descr
134
+ def initialize(x1,y1,x2,y2,z,src,hardBorders=true,tracer="None",no=nil)
135
+ @obj=Gosu::Image.new($window,src,hardBorders)
136
+ @src=src.to_sym
137
+ @x1=x1
138
+ @y1=y1
139
+ @x2=x2
140
+ @y2=y2
141
+ @z=z
142
+ @id=no
143
+ @descr=tracer
144
+ end
145
+ def x
146
+ return @x1
147
+ end
148
+ def y
149
+ return @y1
150
+ end
151
+ def draw(x1=@x1,y1=@y1,x2=@x2,y2=@y2,z=@z)
152
+ @obj.draw_as_quad(x1, y1, 0xffffffff, x2, y1, 0xffffffff, x1, y2, 0xffffffff, x2, y2, 0xffffffff, z)
153
+ end
154
+ end
155
+
156
+ class ZOrder
157
+ Bottom=0
158
+ Background=1
159
+ MapOverlay=2
160
+ Tile=3
161
+ Terrain=3
162
+ BuildingTile=4
163
+ Building=4
164
+ Object=5
165
+ UnclassifiedImage=6
166
+ Default=6
167
+ Character=7
168
+ SceneOverlay=8
169
+ Button=9
170
+ Widget=9
171
+ GUI=9
172
+ Text=13
173
+ Cursor=14
174
+ Top=15
175
+ end
176
+
177
+ end #module GGLib