glimmer-dsl-libui 0.2.20 → 0.2.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +155 -8
- data/VERSION +1 -1
- data/examples/tic_tac_toe/board.rb +145 -0
- data/examples/tic_tac_toe/cell.rb +48 -0
- data/examples/tic_tac_toe.rb +85 -0
- data/glimmer-dsl-libui.gemspec +0 -0
- data/lib/glimmer/libui.rb +29 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47a6ef9ebfc0564ae4489dc2981baad90d307aa8beaf0b109c8830370db4f02b
|
4
|
+
data.tar.gz: 3752be2f3972bc7ab9c054444ff99934d4e1e2cb47bb7724059e69b96d6793b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e20361b29c5ccbe1f7a9de7a4929c7384d3db8ca8deafe016ee35e1f239dd2152500018919586e4ad173bb67bfb746fc2a062db449817d33a745a8871b82d455
|
7
|
+
data.tar.gz: 98b75a7e30e0d05f79b7669e26d3f4afeeeaf19ed57e1acfb56a760c5eb052739f4f4d890840c47eb43b2b841e849f44dd727d0ba0bc06189cfea606cec38380
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 0.2.21
|
4
|
+
|
5
|
+
- examples/tic_tac_toe.rb
|
6
|
+
- `Glimmer::LibUI::enum_names` provides all possible enum names to use with `Glimmer::LibUI::enum_symbols(enum_name)`
|
7
|
+
- Document all `Glimmer::LibUI` custom operations
|
8
|
+
- Fix issue with retrieving `Glimmer::LibUI::enum_symbols` for `:at` enum name
|
9
|
+
|
3
10
|
## 0.2.20
|
4
11
|
|
5
12
|
- Improve examples/tetris.rb with menus, high score dialog, and options
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for LibUI 0.2.
|
1
|
+
# [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=85 />](https://github.com/AndyObtiva/glimmer) Glimmer DSL for LibUI 0.2.21
|
2
2
|
## Prerequisite-Free Ruby Desktop Development GUI Library
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/glimmer-dsl-libui.svg)](http://badge.fury.io/rb/glimmer-dsl-libui)
|
4
4
|
[![Join the chat at https://gitter.im/AndyObtiva/glimmer](https://badges.gitter.im/AndyObtiva/glimmer.svg)](https://gitter.im/AndyObtiva/glimmer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
@@ -278,6 +278,7 @@ Other [Glimmer](https://rubygems.org/gems/glimmer) DSL gems you might be interes
|
|
278
278
|
- [Custom Draw Text](#custom-draw-text)
|
279
279
|
- [Method-Based Custom Keyword](#method-based-custom-keyword)
|
280
280
|
- [Tetris](#tetris)
|
281
|
+
- [Tic Tac Toe](#tic-tac-toe)
|
281
282
|
- [Applications](#applications)
|
282
283
|
- [Manga2PDF](#manga2pdf)
|
283
284
|
- [Befunge98 GUI](#befunge98-gui)
|
@@ -370,7 +371,7 @@ gem install glimmer-dsl-libui
|
|
370
371
|
Or install via Bundler `Gemfile`:
|
371
372
|
|
372
373
|
```ruby
|
373
|
-
gem 'glimmer-dsl-libui', '~> 0.2.
|
374
|
+
gem 'glimmer-dsl-libui', '~> 0.2.21'
|
374
375
|
```
|
375
376
|
|
376
377
|
Add `require 'glimmer-dsl-libui'` at the top, and then `include Glimmer` into the top-level main object for testing or into an actual class for serious usage.
|
@@ -545,6 +546,35 @@ All operations that could normally be called on `LibUI` can also be called on `G
|
|
545
546
|
- `Glimmer::LibUI::queue_main(&block)`: queues an operation to be run on the main event loop at the earliest opportunity possible
|
546
547
|
- `Glimmer::LibUI::timer(time_in_seconds=0.1, repeat: true, &block)`: calls block after time_in_seconds has elapsed, repeating indefinitely unless repeat is `false` or an `Integer` for finite number of repeats. Block can return `false` or `true` to override next repetition.
|
547
548
|
|
549
|
+
There are additional useful `Glimmer::LibUI` operations that are not found in `LibUI`, which mostly help if you would like to do advanced lower level [LibUI](https://github.com/kojix2/LibUI) programming:
|
550
|
+
- `Glimmer::LibUI::integer_to_boolean(int, allow_nil: true)`
|
551
|
+
- `Glimmer::LibUI::boolean_to_integer(int, allow_nil: true)`
|
552
|
+
- `Glimmer::LibUI::degrees_to_radians(degrees)`
|
553
|
+
- `Glimmer::LibUI::interpret_color(value)`: interprets a color in any form like `String`, `Symbol`, or hex into an rgb `Hash`
|
554
|
+
- `Glimmer::LibUI::hex_to_rgb(value)`: converts a hex color to an rgb `Hash`
|
555
|
+
- `Glimmer::LibUI::enum_names`: provides all possible enum names to use with `Glimmer::LibUI::enum_symbols(enum_name)`
|
556
|
+
- `Glimmer::LibUI::enum_symbols(enum_name)`: returns all possible values for an enum. `enum_name` can be:
|
557
|
+
- `:draw_brush_type`: `[:solid, :linear_gradient, :radial_gradient, :image]`
|
558
|
+
- `:draw_line_cap`: `[:flat, :round, :square]`
|
559
|
+
- `:draw_line_join`: `[:miter, :round, :bevel]`
|
560
|
+
- `:draw_fill_mode`: `[:winding, :alternate]`
|
561
|
+
- `:attribute_type`: attributes for attributed `string`s: `[:family, :size, weight, :italic, :stretch, :color, :background, :underline, :underline_color, :features]`
|
562
|
+
- `:text_weight`: `[:minimum, :thin, :ultra_light, :light, :book, :normal, :medium, :semi_bold, :bold, :ultra_bold, :heavy, :ultra_heavy, :maximum]`
|
563
|
+
- `:text_italic`: `[:normal, :oblique, :italic]`
|
564
|
+
- `:text_stretch`: `[:ultra_condensed, :extra_condensed, :condensed, :semi_condensed, :normal, :semi_expanded, :expanded, :extra_expanded, :ultra_expanded]`
|
565
|
+
- `:underline`: `[:none, :single, :double, :suggestion, :color_custom, :color_spelling, :color_grammar, :color_auxiliary]`
|
566
|
+
- `:underline_color`: `[:custom, :spelling, :grammar, :auxiliary]`
|
567
|
+
- `:draw_text_align`: `[:left, :center, :right]`
|
568
|
+
- `:modifier`: `[:ctrl, :alt, :shift, :super]`
|
569
|
+
- `:ext_key`: `[:escape, :insert, :delete, :home, :end, :page_up, :page_down, :up, :down, :left, :right, :f1, :f2, :f3, :f4, :f5, :f6, :f7, :f8, :f9, :f10, :f11, :f12, :n0, :n1, :n2, :n3, :n4, :n5, :n6, :n7, :n8, :n9, :n_dot, :n_enter, :n_add, :n_subtract, :n_multiply, :n_divide]`
|
570
|
+
- `:at`: for inserting `grid` controls: `[:leading, :top, :trailing, :bottom]`
|
571
|
+
- `:align`: `[:fill, :start, :center, :end]`
|
572
|
+
- `:table_value_type`: `[:string, :image, :int, :color]`
|
573
|
+
- `:table_model_column`: `[:never_editable, :always_editable]`
|
574
|
+
- `Glimmer::LibUI::enum_symbol_to_value(enum_name, enum_symbol, default_symbol: nil, default_index: 0)`
|
575
|
+
- `Glimmer::LibUI::enum_value_to_symbol(enum_name, enum_value)`
|
576
|
+
- `Glimmer::LibUI::x11_colors`: returns all [X11 colors](https://en.wikipedia.org/wiki/X11_color_names): `[:alice_blue, :antique_white, :aqua, :aquamarine, :azure, :beige, :bisque, :rebecca_purple, :becca_purple, :blanched_almond, :blue, :blue_violet, :brown, :burly_wood, :burlywood, :cadet_blue, :carnation, :cayenne, :chartreuse, :chocolate, :coral, :cornflower_blue, :cornsilk, :crimson, :cyan, :dark_blue, :dark_cyan, :dark_golden_rod, :dark_goldenrod, :dark_gray, :dark_grey, :dark_green, :dark_khaki, :dark_magenta, :dark_olive_green, :darkolive_green, :dark_orange, :dark_orchid, :dark_red, :dark_salmon, :darksalmon, :dark_sea_green, :dark_slate_blue, :dark_slate_gray, :dark_slate_grey, :dark_turquoise, :dark_violet, :darkorange, :deep_pink, :deep_sky_blue, :dim_gray, :dim_grey, :dodger_blue, :feldspar, :fire_brick, :firebrick, :floral_white, :forest_green, :fuchsia, :gainsboro, :ghost_white, :gold, :golden_rod, :goldenrod, :gray, :grey, :gray10, :grey10, :gray20, :grey20, :gray30, :grey30, :gray40, :grey40, :gray50, :grey50, :gray60, :grey60, :gray70, :grey70, :gray80, :grey80, :gray90, :grey90, :green, :green_yellow, :honey_dew, :honeydew, :hot_pink, :indian_red, :indigo, :ivory, :khaki, :lavender, :lavender_blush, :lawn_green, :lemon_chiffon, :light_blue, :light_coral, :light_cyan, :light_golden_rod_yellow, :light_goldenrod_yellow, :light_gray, :light_grey, :light_green, :light_pink, :light_salmon, :lightsalmon, :light_sea_green, :light_sky_blue, :light_slate_blue, :light_slate_gray, :light_slate_grey, :light_steel_blue, :lightsteel_blue, :light_yellow, :lime, :lime_green, :linen, :magenta, :maroon, :medium_aqua_marine, :medium_aquamarine, :medium_blue, :medium_orchid, :medium_purple, :medium_sea_green, :medium_slate_blue, :medium_spring_green, :medium_turquoise, :medium_violet_red, :midnight_blue, :mint_cream, :misty_rose, :moccasin, :navajo_white, :navy, :old_lace, :olive, :olive_drab, :olivedrab, :orange, :orange_red, :orchid, :pale_golden_rod, :pale_goldenrod, :pale_green, :pale_turquoise, :pale_violet_red, :papaya_whip, :peach_puff, :peachpuff, :peru, :pink, :plum, :powder_blue, :purple, :red, :rosy_brown, :royal_blue, :saddle_brown, :salmon, :sandy_brown, :sea_green, :sea_shell, :seashell, :sienna, :silver, :sky_blue, :slate_blue, :slate_gray, :slate_grey, :snow, :spring_green, :steel_blue, :tan, :teal, :thistle, :tomato, :turquoise, :violet, :violet_red, :wheat, :white_smoke, :yellow, :yellow_green, :metallic, :white, :black, :gray_scale, :grey_scale]`
|
577
|
+
|
548
578
|
### Extra Dialogs
|
549
579
|
|
550
580
|
- `open_file(window as Glimmer::LibUI::WindowProxy = ControlProxy::main_window_proxy)`: returns selected file (`String`) or `nil` if cancelled
|
@@ -873,7 +903,7 @@ Note that `area`, `path`, and nested shapes are all truly declarative, meaning t
|
|
873
903
|
|
874
904
|
`fill` and `stroke` accept [X11](https://en.wikipedia.org/wiki/X11_color_names) color `Symbol`s/`String`s like `:skyblue` and `'sandybrown'` or 6-number hex or 3-number hex-shorthand (as `Integer` or `String` with or without `0x` prefix)
|
875
905
|
|
876
|
-
Available [X11](https://en.wikipedia.org/wiki/X11_color_names)
|
906
|
+
Available [X11 colors](https://en.wikipedia.org/wiki/X11_color_names) can be obtained through `Glimmer::LibUI.x11_colors` method.
|
877
907
|
|
878
908
|
Check [Basic Transform](#basic-transform) example for use of [X11](https://en.wikipedia.org/wiki/X11_color_names) colors.
|
879
909
|
|
@@ -1090,8 +1120,8 @@ window('Method-Based Custom Keyword') {
|
|
1090
1120
|
|
1091
1121
|
To learn more about the [LibUI](https://github.com/kojix2/LibUI) API exposed through [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui):
|
1092
1122
|
- Check out [LibUI ffi.rb](https://github.com/kojix2/LibUI/blob/main/lib/libui/ffi.rb)
|
1093
|
-
- Check out the [libui C
|
1094
|
-
- Check out the [Go UI (Golang LibUI)
|
1123
|
+
- Check out the [libui C Headers](https://github.com/andlabs/libui/blob/master/ui.h)
|
1124
|
+
- Check out the [Go UI (Golang LibUI) API Documentation](https://pkg.go.dev/github.com/andlabs/ui) for an alternative well-documented [libui](https://github.com/andlabs/libui) reference.
|
1095
1125
|
|
1096
1126
|
## Packaging
|
1097
1127
|
|
@@ -6236,7 +6266,7 @@ window('Method-Based Custom Keyword') {
|
|
6236
6266
|
|
6237
6267
|
### Tetris
|
6238
6268
|
|
6239
|
-
Glimmer Tetris utilizes many small areas to represent Tetromino blocks because this ensures smaller redraws per tetromino block color change, thus higher performance than
|
6269
|
+
Glimmer Tetris utilizes many small areas to represent Tetromino blocks because this ensures smaller redraws per tetromino block color change, thus achieving higher performance than redrawing one large area on every little change.
|
6240
6270
|
|
6241
6271
|
[examples/tetris.rb](examples/tetris.rb)
|
6242
6272
|
|
@@ -6619,6 +6649,122 @@ end
|
|
6619
6649
|
Tetris.new.launch
|
6620
6650
|
```
|
6621
6651
|
|
6652
|
+
### Tic Tac Toe
|
6653
|
+
|
6654
|
+
[examples/tic_tac_toe.rb](examples/tic_tac_toe.rb)
|
6655
|
+
|
6656
|
+
Run with this command from the root of the project if you cloned the project:
|
6657
|
+
|
6658
|
+
```
|
6659
|
+
ruby -r './lib/glimmer-dsl-libui' examples/tic_tac_toe.rb
|
6660
|
+
```
|
6661
|
+
|
6662
|
+
Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
|
6663
|
+
|
6664
|
+
```
|
6665
|
+
ruby -r glimmer-dsl-libui -e "require 'examples/tic_tac_toe'"
|
6666
|
+
```
|
6667
|
+
|
6668
|
+
Mac
|
6669
|
+
|
6670
|
+
![glimmer-dsl-libui-mac-tic-tac-toe.png](images/glimmer-dsl-libui-mac-tic-tac-toe.png)
|
6671
|
+
|
6672
|
+
![glimmer-dsl-libui-mac-tic-tac-toe-player-o-wins.png](images/glimmer-dsl-libui-mac-tic-tac-toe-player-o-wins.png)
|
6673
|
+
|
6674
|
+
![glimmer-dsl-libui-mac-tic-tac-toe-player-x-wins.png](images/glimmer-dsl-libui-mac-tic-tac-toe-player-x-wins.png)
|
6675
|
+
|
6676
|
+
![glimmer-dsl-libui-mac-tic-tac-toe-draw.png](images/glimmer-dsl-libui-mac-tic-tac-toe-draw.png)
|
6677
|
+
|
6678
|
+
New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
|
6679
|
+
|
6680
|
+
```ruby
|
6681
|
+
require 'glimmer-dsl-libui'
|
6682
|
+
|
6683
|
+
require_relative "tic_tac_toe/board"
|
6684
|
+
|
6685
|
+
class TicTacToe
|
6686
|
+
include Glimmer
|
6687
|
+
|
6688
|
+
def initialize
|
6689
|
+
@tic_tac_toe_board = Board.new
|
6690
|
+
end
|
6691
|
+
|
6692
|
+
def launch
|
6693
|
+
create_gui
|
6694
|
+
register_observers
|
6695
|
+
@main_window.show
|
6696
|
+
end
|
6697
|
+
|
6698
|
+
def register_observers
|
6699
|
+
Glimmer::DataBinding::Observer.proc do |game_status|
|
6700
|
+
display_win_message if game_status == Board::WIN
|
6701
|
+
display_draw_message if game_status == Board::DRAW
|
6702
|
+
end.observe(@tic_tac_toe_board, :game_status)
|
6703
|
+
|
6704
|
+
3.times.map do |row|
|
6705
|
+
3.times.map do |column|
|
6706
|
+
Glimmer::DataBinding::Observer.proc do |sign|
|
6707
|
+
@cells[row][column].string = sign
|
6708
|
+
end.observe(@tic_tac_toe_board[row + 1, column + 1], :sign) # board model is 1-based
|
6709
|
+
end
|
6710
|
+
end
|
6711
|
+
end
|
6712
|
+
|
6713
|
+
def create_gui
|
6714
|
+
@main_window = window('Tic-Tac-Toe', 180, 180) {
|
6715
|
+
resizable false
|
6716
|
+
|
6717
|
+
@cells = []
|
6718
|
+
vertical_box {
|
6719
|
+
padded false
|
6720
|
+
|
6721
|
+
3.times.map do |row|
|
6722
|
+
@cells << []
|
6723
|
+
horizontal_box {
|
6724
|
+
padded false
|
6725
|
+
|
6726
|
+
3.times.map do |column|
|
6727
|
+
area {
|
6728
|
+
path {
|
6729
|
+
square(0, 0, 60)
|
6730
|
+
|
6731
|
+
stroke :black, thickness: 2
|
6732
|
+
}
|
6733
|
+
text(23, 19) {
|
6734
|
+
@cells[row] << string('') {
|
6735
|
+
font family: 'Arial', size: 20
|
6736
|
+
}
|
6737
|
+
}
|
6738
|
+
on_mouse_up do
|
6739
|
+
@tic_tac_toe_board.mark(row + 1, column + 1) # board model is 1-based
|
6740
|
+
end
|
6741
|
+
}
|
6742
|
+
end
|
6743
|
+
}
|
6744
|
+
end
|
6745
|
+
}
|
6746
|
+
}
|
6747
|
+
end
|
6748
|
+
|
6749
|
+
def display_win_message
|
6750
|
+
display_game_over_message("Player #{@tic_tac_toe_board.winning_sign} has won!")
|
6751
|
+
end
|
6752
|
+
|
6753
|
+
def display_draw_message
|
6754
|
+
display_game_over_message("Draw!")
|
6755
|
+
end
|
6756
|
+
|
6757
|
+
def display_game_over_message(message_text)
|
6758
|
+
Glimmer::LibUI.queue_main do
|
6759
|
+
msg_box('Game Over', message_text)
|
6760
|
+
@tic_tac_toe_board.reset!
|
6761
|
+
end
|
6762
|
+
end
|
6763
|
+
end
|
6764
|
+
|
6765
|
+
TicTacToe.new.launch
|
6766
|
+
```
|
6767
|
+
|
6622
6768
|
## Applications
|
6623
6769
|
|
6624
6770
|
Here are some applications built with [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui)
|
@@ -6649,9 +6795,10 @@ https://github.com/iraamaro/i3off-gtk-ruby
|
|
6649
6795
|
|
6650
6796
|
## Resources
|
6651
6797
|
|
6652
|
-
- [libui C Library](https://github.com/andlabs/libui)
|
6653
|
-
- [LibUI Ruby Bindings](https://github.com/kojix2/LibUI)
|
6654
6798
|
- [Code Master Blog](https://andymaleh.blogspot.com/search/label/LibUI)
|
6799
|
+
- [LibUI Ruby Bindings](https://github.com/kojix2/LibUI)
|
6800
|
+
- [libui C Library](https://github.com/andlabs/libui)
|
6801
|
+
- [Go UI (Golang LibUI) API Documentation](https://pkg.go.dev/github.com/andlabs/ui)
|
6655
6802
|
|
6656
6803
|
## Help
|
6657
6804
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.21
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
require_relative 'cell'
|
23
|
+
|
24
|
+
class TicTacToe
|
25
|
+
class Board
|
26
|
+
DRAW = :draw
|
27
|
+
IN_PROGRESS = :in_progress
|
28
|
+
WIN = :win
|
29
|
+
attr :winning_sign
|
30
|
+
attr_accessor :game_status
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
@sign_state_machine = {nil => "X", "X" => "O", "O" => "X"}
|
34
|
+
build_grid
|
35
|
+
@winning_sign = Cell::EMPTY
|
36
|
+
@game_status = IN_PROGRESS
|
37
|
+
end
|
38
|
+
|
39
|
+
#row and column numbers are 1-based
|
40
|
+
def mark(row, column)
|
41
|
+
self[row, column].mark(current_sign)
|
42
|
+
game_over? #updates winning sign
|
43
|
+
end
|
44
|
+
|
45
|
+
def current_sign
|
46
|
+
@current_sign = @sign_state_machine[@current_sign]
|
47
|
+
end
|
48
|
+
|
49
|
+
def [](row, column)
|
50
|
+
@grid[row-1][column-1]
|
51
|
+
end
|
52
|
+
|
53
|
+
def game_over?
|
54
|
+
win? or draw?
|
55
|
+
end
|
56
|
+
|
57
|
+
def win?
|
58
|
+
win = (row_win? or column_win? or diagonal_win?)
|
59
|
+
self.game_status=WIN if win
|
60
|
+
win
|
61
|
+
end
|
62
|
+
|
63
|
+
def reset!
|
64
|
+
(1..3).each do |row|
|
65
|
+
(1..3).each do |column|
|
66
|
+
self[row, column].reset!
|
67
|
+
end
|
68
|
+
end
|
69
|
+
@winning_sign = Cell::EMPTY
|
70
|
+
@current_sign = nil
|
71
|
+
self.game_status=IN_PROGRESS
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def build_grid
|
77
|
+
@grid = []
|
78
|
+
3.times do |row_index| #0-based
|
79
|
+
@grid << []
|
80
|
+
3.times { @grid[row_index] << Cell.new }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def row_win?
|
85
|
+
(1..3).each do |row|
|
86
|
+
if row_has_same_sign(row)
|
87
|
+
@winning_sign = self[row, 1].sign
|
88
|
+
return true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
false
|
92
|
+
end
|
93
|
+
|
94
|
+
def column_win?
|
95
|
+
(1..3).each do |column|
|
96
|
+
if column_has_same_sign(column)
|
97
|
+
@winning_sign = self[1, column].sign
|
98
|
+
return true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
false
|
102
|
+
end
|
103
|
+
|
104
|
+
#needs refactoring if we ever decide to make the board size dynamic
|
105
|
+
def diagonal_win?
|
106
|
+
if (self[1, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[3, 3].sign) and self[1, 1].marked
|
107
|
+
@winning_sign = self[1, 1].sign
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
if (self[3, 1].sign == self[2, 2].sign) and (self[2, 2].sign == self[1, 3].sign) and self[3, 1].marked
|
111
|
+
@winning_sign = self[3, 1].sign
|
112
|
+
return true
|
113
|
+
end
|
114
|
+
false
|
115
|
+
end
|
116
|
+
|
117
|
+
def draw?
|
118
|
+
@board_full = true
|
119
|
+
3.times do |x|
|
120
|
+
3.times do |y|
|
121
|
+
@board_full = false if self[x, y].empty
|
122
|
+
end
|
123
|
+
end
|
124
|
+
self.game_status = DRAW if @board_full
|
125
|
+
@board_full
|
126
|
+
end
|
127
|
+
|
128
|
+
def row_has_same_sign(number)
|
129
|
+
row_sign = self[number, 1].sign
|
130
|
+
[2, 3].each do |column|
|
131
|
+
return false unless row_sign == (self[number, column].sign)
|
132
|
+
end
|
133
|
+
true if self[number, 1].marked
|
134
|
+
end
|
135
|
+
|
136
|
+
def column_has_same_sign(number)
|
137
|
+
column_sign = self[1, number].sign
|
138
|
+
[2, 3].each do |row|
|
139
|
+
return false unless column_sign == (self[row, number].sign)
|
140
|
+
end
|
141
|
+
true if self[1, number].marked
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
class TicTacToe
|
23
|
+
class Cell
|
24
|
+
EMPTY = ""
|
25
|
+
attr_accessor :sign, :empty
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
reset!
|
29
|
+
end
|
30
|
+
|
31
|
+
def mark(sign)
|
32
|
+
self.sign = sign
|
33
|
+
end
|
34
|
+
|
35
|
+
def reset!
|
36
|
+
self.sign = EMPTY
|
37
|
+
end
|
38
|
+
|
39
|
+
def sign=(sign_symbol)
|
40
|
+
@sign = sign_symbol
|
41
|
+
self.empty = sign == EMPTY
|
42
|
+
end
|
43
|
+
|
44
|
+
def marked
|
45
|
+
!empty
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -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: 20
|
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
|
data/glimmer-dsl-libui.gemspec
CHANGED
Binary file
|
data/lib/glimmer/libui.rb
CHANGED
@@ -100,11 +100,39 @@ module Glimmer
|
|
100
100
|
enum_symbol_values(enum_name).keys
|
101
101
|
end
|
102
102
|
|
103
|
+
def enum_names
|
104
|
+
[
|
105
|
+
:align,
|
106
|
+
:at,
|
107
|
+
:attribute_type,
|
108
|
+
:draw_brush_type,
|
109
|
+
:draw_fill_mode,
|
110
|
+
:draw_line_cap,
|
111
|
+
:draw_line_join,
|
112
|
+
:draw_text_align,
|
113
|
+
:ext_key,
|
114
|
+
:modifier,
|
115
|
+
:table_model_column,
|
116
|
+
:table_value_type,
|
117
|
+
:text_italic,
|
118
|
+
:text_stretch,
|
119
|
+
:text_weight,
|
120
|
+
:underline,
|
121
|
+
:underline_color
|
122
|
+
]
|
123
|
+
end
|
124
|
+
|
103
125
|
# Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
|
104
126
|
def enum_symbol_values(enum_name)
|
105
127
|
enum_name = enum_name.to_s.underscore.to_sym
|
106
128
|
@enum_symbols ||= {}
|
107
|
-
@enum_symbols[enum_name] ||= ::LibUI.constants.select
|
129
|
+
@enum_symbols[enum_name] ||= ::LibUI.constants.select do |c|
|
130
|
+
c.to_s.match(/#{enum_name.to_s.camelcase(:upper)}[A-Z]/)
|
131
|
+
end.map do |c|
|
132
|
+
[c.to_s.underscore.sub("#{enum_name}_", '').to_sym, ::LibUI.const_get(c)]
|
133
|
+
end.reject do |key, value|
|
134
|
+
enum_name == :underline && key.to_s.start_with?('color')
|
135
|
+
end.to_h
|
108
136
|
end
|
109
137
|
|
110
138
|
def enum_value_to_symbol(enum_name, enum_value)
|
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.
|
4
|
+
version: 0.2.21
|
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-
|
11
|
+
date: 2021-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: glimmer
|
@@ -257,6 +257,9 @@ files:
|
|
257
257
|
- examples/tetris/model/game.rb
|
258
258
|
- examples/tetris/model/past_game.rb
|
259
259
|
- examples/tetris/model/tetromino.rb
|
260
|
+
- examples/tic_tac_toe.rb
|
261
|
+
- examples/tic_tac_toe/board.rb
|
262
|
+
- examples/tic_tac_toe/cell.rb
|
260
263
|
- examples/timer.rb
|
261
264
|
- glimmer-dsl-libui.gemspec
|
262
265
|
- icons/glimmer.png
|