glimmer-dsl-libui 0.2.20 → 0.2.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ require 'glimmer-dsl-libui'
2
+
3
+ require_relative "tic_tac_toe/board"
4
+
5
+ class TicTacToe
6
+ include Glimmer
7
+
8
+ def initialize
9
+ @tic_tac_toe_board = Board.new
10
+ end
11
+
12
+ def launch
13
+ create_gui
14
+ register_observers
15
+ @main_window.show
16
+ end
17
+
18
+ def register_observers
19
+ Glimmer::DataBinding::Observer.proc do |game_status|
20
+ display_win_message if game_status == Board::WIN
21
+ display_draw_message if game_status == Board::DRAW
22
+ end.observe(@tic_tac_toe_board, :game_status)
23
+
24
+ 3.times.map do |row|
25
+ 3.times.map do |column|
26
+ Glimmer::DataBinding::Observer.proc do |sign|
27
+ @cells[row][column].string = sign
28
+ end.observe(@tic_tac_toe_board[row + 1, column + 1], :sign) # board model is 1-based
29
+ end
30
+ end
31
+ end
32
+
33
+ def create_gui
34
+ @main_window = window('Tic-Tac-Toe', 180, 180) {
35
+ resizable false
36
+
37
+ @cells = []
38
+ vertical_box {
39
+ padded false
40
+
41
+ 3.times.map do |row|
42
+ @cells << []
43
+ horizontal_box {
44
+ padded false
45
+
46
+ 3.times.map do |column|
47
+ area {
48
+ path {
49
+ square(0, 0, 60)
50
+
51
+ stroke :black, thickness: 2
52
+ }
53
+ text(23, 19) {
54
+ @cells[row] << string('') {
55
+ font family: 'Arial', size: OS.mac? ? 20 : 16
56
+ }
57
+ }
58
+ on_mouse_up do
59
+ @tic_tac_toe_board.mark(row + 1, column + 1) # board model is 1-based
60
+ end
61
+ }
62
+ end
63
+ }
64
+ end
65
+ }
66
+ }
67
+ end
68
+
69
+ def display_win_message
70
+ display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!")
71
+ end
72
+
73
+ def display_draw_message
74
+ display_game_over_message("Draw!")
75
+ end
76
+
77
+ def display_game_over_message(message_text)
78
+ Glimmer::LibUI.queue_main do
79
+ msg_box('Game Over', message_text)
80
+ @tic_tac_toe_board.reset!
81
+ end
82
+ end
83
+ end
84
+
85
+ TicTacToe.new.launch
Binary file
data/lib/glimmer/libui.rb CHANGED
@@ -26,12 +26,12 @@ module Glimmer
26
26
  class << self
27
27
  include Glimmer::FiddleConsumer
28
28
 
29
- def integer_to_boolean(int, allow_nil: true)
30
- int.nil? ? (allow_nil ? nil : false) : ((int.is_a?(TrueClass) || int.is_a?(FalseClass)) ? int : (int.is_a?(Integer) ? int == 1 : (allow_nil ? nil : false)))
29
+ def integer_to_boolean(int, allow_nil: true, allow_boolean: true)
30
+ int.nil? ? (allow_nil ? nil : false) : (allow_boolean && (int.is_a?(TrueClass) || int.is_a?(FalseClass)) ? int : (int.is_a?(Integer) ? int == 1 : (allow_nil ? nil : false)))
31
31
  end
32
32
 
33
- def boolean_to_integer(bool, allow_nil: true)
34
- bool.nil? ? (allow_nil ? nil : 0) : (bool.is_a?(Integer) ? bool : (bool.is_a?(TrueClass) || bool.is_a?(FalseClass) ? (bool == true ? 1 : 0) : (allow_nil ? nil : 0)))
33
+ def boolean_to_integer(bool, allow_nil: true, allow_integer: true)
34
+ bool.nil? ? (allow_nil ? nil : 0) : (allow_integer && bool.is_a?(Integer) ? bool : (bool.is_a?(TrueClass) || bool.is_a?(FalseClass) ? (bool == true ? 1 : 0) : (allow_nil ? nil : 0)))
35
35
  end
36
36
 
37
37
  def degrees_to_radians(degrees)
@@ -83,6 +83,7 @@ module Glimmer
83
83
  value = value.chars.map {|char| [char, char]}.flatten.join if value.length == 3
84
84
  value = "0x#{value}"
85
85
  end
86
+ value = "0x#{value[1..-1]}" if value.start_with?('#')
86
87
  value = value.to_i(16)
87
88
  end
88
89
  if value.is_a?(Integer)
@@ -100,11 +101,39 @@ module Glimmer
100
101
  enum_symbol_values(enum_name).keys
101
102
  end
102
103
 
104
+ def enum_names
105
+ [
106
+ :align,
107
+ :at,
108
+ :attribute_type,
109
+ :draw_brush_type,
110
+ :draw_fill_mode,
111
+ :draw_line_cap,
112
+ :draw_line_join,
113
+ :draw_text_align,
114
+ :ext_key,
115
+ :modifier,
116
+ :table_model_column,
117
+ :table_value_type,
118
+ :text_italic,
119
+ :text_stretch,
120
+ :text_weight,
121
+ :underline,
122
+ :underline_color
123
+ ]
124
+ end
125
+
103
126
  # Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
104
127
  def enum_symbol_values(enum_name)
105
128
  enum_name = enum_name.to_s.underscore.to_sym
106
129
  @enum_symbols ||= {}
107
- @enum_symbols[enum_name] ||= ::LibUI.constants.select { |c| c.to_s.start_with?(enum_name.to_s.camelcase(:upper)) }.map { |c| [c.to_s.underscore.sub("#{enum_name}_", '').to_sym, ::LibUI.const_get(c)] }.to_h
130
+ @enum_symbols[enum_name] ||= ::LibUI.constants.select do |c|
131
+ c.to_s.match(/#{enum_name.to_s.camelcase(:upper)}[A-Z]/)
132
+ end.map do |c|
133
+ [c.to_s.underscore.sub("#{enum_name}_", '').to_sym, ::LibUI.const_get(c)]
134
+ end.reject do |key, value|
135
+ enum_name == :underline && key.to_s.start_with?('color')
136
+ end.to_h
108
137
  end
109
138
 
110
139
  def enum_value_to_symbol(enum_name, enum_value)
@@ -145,7 +174,7 @@ module Glimmer
145
174
  # If block returns true at any point, the timer continues for another repetition regardless of `repeat:` keyword arg value
146
175
  def timer(time_in_seconds = 0.1, repeat: true, &block)
147
176
  closure = fiddle_closure_block_caller(4, [0]) do
148
- result = boolean_to_integer(block.call)
177
+ result = boolean_to_integer(block.call, allow_integer: false)
149
178
  repeat -= 1 if repeat.is_a?(Integer)
150
179
  if result.nil?
151
180
  if (repeat == true || (repeat.is_a?(Integer) && repeat > 0))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glimmer-dsl-libui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.20
4
+ version: 0.2.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-04 00:00:00.000000000 Z
11
+ date: 2021-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -252,11 +252,21 @@ files:
252
252
  - examples/method_based_custom_keyword.rb
253
253
  - examples/midi_player.rb
254
254
  - examples/simple_notepad.rb
255
+ - examples/snake.rb
256
+ - examples/snake/model/apple.rb
257
+ - examples/snake/model/game.rb
258
+ - examples/snake/model/snake.rb
259
+ - examples/snake/model/vertebra.rb
260
+ - examples/snake/presenter/cell.rb
261
+ - examples/snake/presenter/grid.rb
255
262
  - examples/tetris.rb
256
263
  - examples/tetris/model/block.rb
257
264
  - examples/tetris/model/game.rb
258
265
  - examples/tetris/model/past_game.rb
259
266
  - examples/tetris/model/tetromino.rb
267
+ - examples/tic_tac_toe.rb
268
+ - examples/tic_tac_toe/board.rb
269
+ - examples/tic_tac_toe/cell.rb
260
270
  - examples/timer.rb
261
271
  - glimmer-dsl-libui.gemspec
262
272
  - icons/glimmer.png