luit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/luit.rb +475 -0
  3. metadata +65 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a36840cb8966b118302e9dfda4492a37a44ee2d2
4
+ data.tar.gz: 4c7b37f0e08ece521132aa4e267fcb0c5a34d52f
5
+ SHA512:
6
+ metadata.gz: aa63d2e56f31916765db9703290be65c3a04c2c77d4e65f4026e6e537fee0d354d18015e866347b205e27f08d23ef4ed238b0840906b70a6f3599acb1ddc6b1f
7
+ data.tar.gz: ad5a2a98177786ee1ed75fb6ae4d3d85b038e545f7afc3f12b22e38b99113270000e7521b6d56750816ee1b0ece99e17e40b4e1795b6a1d8b1717b909c8525eb
@@ -0,0 +1,475 @@
1
+ # Leddys UI Toolkit, LUIT
2
+ # Copyrigth 2017 Martin Larsson
3
+ # martin.99.larsson@telia.com
4
+ #
5
+ # a holder is the object wich will recieve a callback when something happens to an element
6
+ # this could be button presse, or other similar events
7
+ # the id is assigned by the holder to know wich element called the callback method
8
+
9
+ require 'gosu'
10
+
11
+ module LUIT
12
+ class << self
13
+ attr_accessor :uiColor, :uiColorLight, :touchDown
14
+ attr_reader :window, :z
15
+ end
16
+
17
+ def self.config(window:, uiColor: 0xff_555555, uiColorLight: 0xff_888888, z: 100)
18
+ @window = window
19
+ @uiColor = uiColor
20
+ @uiColorLight = uiColorLight
21
+ @z = z
22
+ @touchDown = false
23
+ end
24
+
25
+ def self.mX
26
+ @window.mouse_x
27
+ end
28
+
29
+ def self.mY
30
+ @window.mouse_y
31
+ end
32
+
33
+ #Raspberry pi touchscreen hack
34
+ def self.updateTouch()
35
+ if @touchDown
36
+ @window.mouse_x = 0
37
+ @window.mouse_y = 0
38
+ @touchDown = false
39
+ else
40
+ if @window.mouse_x != 0 or @window.mouse_y != 0
41
+ @touchDown = true
42
+ end
43
+ end
44
+ end
45
+
46
+ class LUITElement
47
+ attr_reader :id, :x, :y, :w, :h, :hover
48
+ def initialize(holder, id, x, y, w, h)
49
+ @holder = holder
50
+ @id = id
51
+ @x = x
52
+ @y = y
53
+ @w = w
54
+ @h = h
55
+ @msDown = false
56
+ @hover = true
57
+ end
58
+
59
+ def draw(x = 0, y = 0)
60
+ end
61
+
62
+ def draw_rel(x = 0, y = 0)
63
+ draw(x - (@w/2), y - (@h/2))
64
+ end
65
+
66
+ def updateHover(x, y)
67
+ @hover = LUIT.mX.between?(x, x + @w) && LUIT.mY.between?(y, y + @h)
68
+ end
69
+
70
+ def update(x = 0, y = 0)
71
+ if Gosu::button_down?(Gosu::MsLeft) or LUIT.touchDown
72
+ if !@msDown
73
+ button_down(Gosu::MsLeft)
74
+ end
75
+ @msDown = true
76
+ else
77
+ @msDown = false
78
+ end
79
+ end
80
+
81
+ def update_rel(x = 0, y = 0)
82
+ update(x - (@w/2), y - (@h/2))
83
+ end
84
+
85
+ def button_down(id)
86
+ if id == Gosu::MsLeft && @hover
87
+ @holder.onClick(@id)
88
+ end
89
+ end
90
+ end
91
+
92
+ class ClickArea < LUITElement
93
+ def initialize(holder, id, x, y, w = 0, h = 0)
94
+ h = [1, h].max
95
+ w = [1, w].max
96
+ super(holder, id, x, y, w, h)
97
+ end
98
+
99
+ def draw(x = 0, y = 0)
100
+ Gosu::draw_rect(@x + x, @y + y, @w, @h, @hover ? LUIT.uiColorLight : LUIT.uiColor)
101
+ end
102
+
103
+ def update(x = 0, y = 0)
104
+ x += @x
105
+ y += @y
106
+ updateHover(x, y)
107
+ super(x, y)
108
+ end
109
+ end
110
+
111
+ class TextArea < LUITElement
112
+ attr_reader :text
113
+ def initialize(holder, id, x, y, maxChar, h)
114
+ h = [10, h].max
115
+ @font = Gosu::Font.new(h)
116
+ w = @font.text_width("W" * maxChar) + 20
117
+ @maxChar = maxChar
118
+ super(holder, id, x, y, w, h + 20)
119
+ @typing = false
120
+ @field = Gosu::TextInput.new
121
+ @window = LUIT.window
122
+ end
123
+
124
+ def text
125
+ return @field.text
126
+ end
127
+
128
+ def button_down(id)
129
+ if @hover
130
+ @typing = true
131
+ else
132
+ if @typing
133
+ @typing = false
134
+ @holder.onClick(@id)
135
+ end
136
+ end
137
+ end
138
+
139
+ def draw(x = 0, y = 0)
140
+ x = x + @x
141
+ y = y + @y
142
+ Gosu::draw_rect(x, y, @w, @h, @typing ? 0xff_ffffff : LUIT.uiColor, LUIT.z)
143
+ @font.draw(@field.text, x + 10, y + 10, LUIT.z + 1, 1, 1, 0xff000000)
144
+ end
145
+
146
+ def update(x = 0, y = 0)
147
+ super
148
+ x += @x
149
+ y += @y
150
+ updateHover(x, y)
151
+ if @field.text.size > @maxChar
152
+ @field.text = @field.text[0..@maxChar]
153
+ end
154
+ if @typing
155
+ @window.text_input = @field
156
+ elsif @window.text_input == @field
157
+ @window.text_input = nil
158
+ end
159
+ if Gosu::button_down?(Gosu::KbReturn) && @typing
160
+ @typing = false
161
+ @window.text_input = nil
162
+ @holder.onClick(@id)
163
+ end
164
+ end
165
+ end
166
+
167
+ class ScannerInput
168
+ attr_reader :scanning
169
+ def initialize(holder, id, window)
170
+ @field = Gosu::TextInput.new
171
+ @window = window
172
+ @holder = holder
173
+ @id = id
174
+ end
175
+
176
+ def update
177
+ if Gosu::button_down?(Gosu::KbReturn) && @scanning
178
+ @holder.onScan(@field.text)
179
+ @field.text = ""
180
+ end
181
+ end
182
+
183
+ def stop
184
+ @scanning = false
185
+ @window.text_input = nil if @window.text_input == @field
186
+ end
187
+
188
+ def scan
189
+ @scanning = true
190
+ @window.text_input = @field
191
+ end
192
+ end
193
+
194
+ class ClickPoint < LUITElement
195
+ def initialize(holder, id, x, y, r)
196
+ @r = [1, r].max
197
+ super(holder, id, x - @r, y - @r, @r * 2, @r * 2)
198
+ end
199
+
200
+ def update(x = 0, y = 0)
201
+ if Gosu::distance($mx, $my, x, y) <= @r
202
+ @hover = true
203
+ else
204
+ @hover = false
205
+ end
206
+ end
207
+ end
208
+
209
+ class Button < LUITElement
210
+ #w and h will auto adjust to the text size + 10px padding if its not set (or set lower than acceptable)
211
+ def initialize(holder, id, x, y, text, w = 0, h = 0)
212
+ h = [50, h].max
213
+ @text = text
214
+ @buttonColor = LUIT.uiColor
215
+ @font = Gosu::Font.new(h - 20)
216
+ @textW = @font.text_width(@text)
217
+ w = @textW + 20 if w < @textW + 20
218
+ super(holder, id, x, y, w, h)
219
+ end
220
+
221
+ def draw(x = 0, y = 0)
222
+ Gosu::draw_rect(x + @x, y + @y, @w, @h, @hover ? LUIT.uiColorLight : LUIT.uiColor, LUIT.z)
223
+ @font.draw_rel(@text, @x + x + @w / 2, @y + y + @h / 2, LUIT.z + 1, 0.5, 0.5)
224
+ end
225
+
226
+ def update(x = 0, y = 0)
227
+ x += @x
228
+ y += @y
229
+ updateHover(x, y)
230
+ super(x, y)
231
+ end
232
+ end
233
+
234
+ class Slider < LUITElement
235
+ attr_accessor :value
236
+ def initialize(holder, id, x, y, range)
237
+ super(holder, id, x, y, range + 10, 30)
238
+ @range = range
239
+ @value = 0
240
+ @buttonColor = LUIT.uiColor
241
+ end
242
+
243
+ def draw(x = 0, y = 0)
244
+ Gosu::draw_rect(@x + x, @y + y + 10, @w, 10, @buttonColor, LUIT.z)
245
+ Gosu::draw_rect(@x + x + @value, @y + y, 10, @h, @buttonColor, LUIT.z + 1)
246
+ end
247
+
248
+ def updateHover(x, y)
249
+ @hover = LUIT.mX.between?(x - 10, x + @w + 20) && LUIT.mY.between?(y - 10, y + @h + 20)
250
+ end
251
+
252
+ def update(x = 0, y = 0)
253
+ x += @x
254
+ y += @y
255
+ updateHover(x, y)
256
+ if @hover && (Gosu::button_down?(Gosu::MsLeft) or LUIT.touchDown)
257
+ @value = LUIT.mX - (x + 5)
258
+ @value = 0 if @value < 0
259
+ @value = @range if @value > @range
260
+ end
261
+ @buttonColor = @hover ? LUIT.uiColorLight : LUIT.uiColor
262
+ end
263
+
264
+ def button_down(id)
265
+ end
266
+ end
267
+
268
+ class VerticalSlider < LUITElement
269
+ attr_accessor :value
270
+ def initialize(holder, id, x, y, range)
271
+ super(holder, id, x, y, 30, range + 10)
272
+ @range = range
273
+ @value = 0
274
+ @buttonColor = LUIT.uiColor
275
+ end
276
+
277
+ def draw(x = 0, y = 0)
278
+ Gosu::draw_rect(@x + x + 10, @y + y, 10, @h, @buttonColor, LUIT.z)
279
+ Gosu::draw_rect(@x + x, @y + y + @value, @w, 10, @buttonColor, LUIT.z + 1)
280
+ end
281
+
282
+ def updateHover(x, y)
283
+ @hover = LUIT.mX.between?(x - 10, x + @w + 20) && LUIT.mY.between?(y - 10, y + @h + 20)
284
+ end
285
+
286
+ def update(x = 0, y = 0)
287
+ x += @x
288
+ y += @y
289
+ updateHover(x, y)
290
+ if @hover && (Gosu::button_down?(Gosu::MsLeft) or LUIT.touchDown)
291
+ @value = LUIT.mY - (y + 5)
292
+ @value = 0 if @value < 0
293
+ @value = @range if @value > @range
294
+ end
295
+ @buttonColor = @hover ? LUIT.uiColorLight : LUIT.uiColor
296
+ end
297
+
298
+ def button_down(id)
299
+ end
300
+ end
301
+
302
+ class Toggle < LUITElement
303
+ attr_accessor :value
304
+ def initialize(holder, id, x, y, size = 30)
305
+ h = [30, size].max
306
+ w = h * 2
307
+ super(holder, id, x, y, w, h)
308
+ @buttonColor = LUIT.uiColor
309
+ @value = false
310
+ end
311
+
312
+ def draw(x = 0, y = 0)
313
+ x += @x
314
+ y += @y
315
+ Gosu::draw_rect(x, y, @w, @h, @buttonColor, LUIT.z)
316
+ @value ? v = 1 : v = 0
317
+ Gosu::draw_rect(x + 4 + (@h * v), y + 4, @h - 8, @h - 8, @value ? 0xff_ffffff : 0xff_000000, LUIT.z + 1)
318
+ end
319
+
320
+ def update(x = 0, y = 0)
321
+ x += @x
322
+ y += @y
323
+ updateHover(x, y)
324
+ @buttonColor = @hover ? LUIT.uiColorLight : LUIT.uiColor
325
+ super
326
+ end
327
+
328
+ def button_down(id)
329
+ if id == Gosu::MsLeft && @hover
330
+ @value = !@value
331
+ @holder.onClick(@id)
332
+ end
333
+ end
334
+ end
335
+
336
+ class List < LUITElement
337
+ attr_reader :contents
338
+ def initialize(holder, id, x, y, h, s = 10)
339
+ super(holder, id, x, y, 50, h)
340
+ @spaceing = s
341
+ @contents = []
342
+ @totalh = 0
343
+ @focus = 0
344
+ @scrollbar = VerticalSlider.new(self, "scroll", 0, 0, @h - 10)
345
+ end
346
+
347
+ def <<(item)
348
+ @contents << item
349
+ @totalh += item.h + @spaceing
350
+ @w = @contents.max_by{|x| x.w}.w
351
+ end
352
+
353
+ def draw(x = 0, y = 0)
354
+ x += @x
355
+ y += @y
356
+ prevh = 0
357
+ @contents.each do |item|
358
+ if !((prevh - @focus + item.h) <= 0 || (prevh - @focus) >= @h)
359
+ item.draw(x + 30, y + prevh - @focus)
360
+ end
361
+ prevh += item.h + @spaceing
362
+ end
363
+ @scrollbar.draw(x, y)
364
+ end
365
+
366
+ def update(x = 0, y = 0)
367
+ x += @x
368
+ y += @y
369
+ prevh = 0
370
+ @contents.each do |item|
371
+ if !((prevh - @focus + item.h) < 0 || (prevh - @focus) > @h)
372
+ item.update(x + 30, y + prevh - @focus)
373
+ end
374
+ prevh += item.h + @spaceing
375
+ end
376
+ @scrollbar.update(x, y)
377
+ if @totalh > @h
378
+ v = @scrollbar.value / (@h - 10)
379
+ @focus = (@totalh - @h) * v
380
+ else
381
+ @focus = 0
382
+ end
383
+ end
384
+ end
385
+
386
+ class Icon
387
+ attr_reader :x, :y, :w, :h
388
+ def initialize(path, x, y, w, h)
389
+ @icon = Gosu::Image.new(path)
390
+ @scalex = (w / @icon.width)
391
+ @scaley = (h / @icon.height)
392
+ @x = x
393
+ @y = y
394
+ @w = w
395
+ @h = h
396
+ end
397
+
398
+ def draw(x = 0, y = 0)
399
+ @icon.draw(@x + x, @y + y, 1, @scalex, @scaley)
400
+ end
401
+
402
+ def update(x = 0, y = 0)
403
+ end
404
+ end
405
+
406
+ @decimalValue = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
407
+ @romanNumeral = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
408
+ def self.to_roman(number)
409
+ return '0' if number == 0
410
+ romanized = ''
411
+ for index in 0...@decimalValue.length do
412
+ while @decimalValue[index] <= number do
413
+ romanized += @romanNumeral[index]
414
+ number -= @decimalValue[index]
415
+ end
416
+ end
417
+ return romanized
418
+ end
419
+ end
420
+
421
+ #run file directly to run test window
422
+ if __FILE__ == $0
423
+ class Test < Gosu::Window
424
+ def initialize
425
+ super(1000, 1000, false)
426
+
427
+ LUIT.config(window: self)
428
+
429
+ @LUITElements = []
430
+ @LUITElements << LUIT::Button.new(self, 1, 0, 0, "Test")
431
+ @LUITElements << LUIT::Button.new(self, 2, 111, 111, "Big button", 200, 70)
432
+ @LUITElements << LUIT::Slider.new(self, 3, 300, 300, 300)
433
+ @vslider = LUIT::VerticalSlider.new(self, 3, 250, 250, 300)
434
+ @LUITElements << @vslider
435
+ @LUITElements << LUIT::Toggle.new(self, 4, 500, 500)
436
+ @LUITElements << LUIT::ClickArea.new(self, "click", 900, 900, 100, 100)
437
+ @texter = LUIT::TextArea.new(self, "text", 300, 100, 32, 20)
438
+ @LUITElements << @texter
439
+ @LUITElements << LUIT::TextArea.new(self, "text2", 300, 200, 32, 20)
440
+ @list = LUIT::List.new(self, "list", 0, 200, 300, 0)
441
+ @LUITElements << @list
442
+ 20.times do
443
+ @list << LUIT::Button.new(self, 1, 0, 0, "Test", 0, 50)
444
+ end
445
+ @font = Gosu::Font.new(30)
446
+ end
447
+
448
+ def draw
449
+ @LUITElements.each {|e| e.draw}
450
+ @font.draw(LUIT::to_roman(@vslider.value), 600, 250, 2)
451
+ end
452
+
453
+ def update
454
+ @LUITElements.each {|e| e.update}
455
+ end
456
+
457
+ def button_down(id)
458
+ if id == Gosu::KbSpace
459
+ @list << LUIT::Button.new(self, 1, 0, 0, "Test", 0, 50)
460
+ end
461
+ end
462
+
463
+ def onClick(id)
464
+ puts id
465
+ if id == "text"
466
+ puts @texter.text
467
+ end
468
+ end
469
+
470
+ def needs_cursor?
471
+ return true
472
+ end
473
+ end
474
+ Test.new.show
475
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leddy231
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.11.3.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.11'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.11.3.1
33
+ description: A UI Toolkit for use with Gosu. Includes buttons, sliders, toggles, text
34
+ areas and more.
35
+ email: martin.99.larsson@telia.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - lib/luit.rb
41
+ homepage:
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.6.9
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Leddys UI Toolkit
65
+ test_files: []