glimmer-dsl-gtk 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28805ba1382c7a6ce2bb2c7d98c376141f8b2cd0ffc03941b305722ae622c3c7
4
- data.tar.gz: 71112f59b18d77e1dfcd7c56fb365844e3a92026eecfc86adcfcea2efd88c764
3
+ metadata.gz: 908b538188d1ca4fd8c7b96bcc1a890febf29a55d2f37d09a81fa1849b15e7fa
4
+ data.tar.gz: c88057203f02fed224707cf1ee12f5b66f71d6b9b4632986f0ac3b7f973fd59c
5
5
  SHA512:
6
- metadata.gz: 57b97588b9dcbe3de56952abafa445891ed267499e9776e1b20b2703f89524aa5ba908c28cd1eafd0076ee3a74ceb253e2a7c1c190ba517c9723f64101e37ffa
7
- data.tar.gz: 1d7808d2298c7babbb244545d98e6c5c6d42f8a9e9d7a2578480dbe24ce7e0f55987396b26fd9a531c814e9a1272442759fde6773961578db4cccb887ca9c742
6
+ metadata.gz: ec820ca1c3782e2b7311bf917bf97f2369da65fff6cf110f887a60e164775ad2b41df9f25a9c904beffd67f3ebd9b6e7decd57f6a222f1a24a5725c5f396d8c1
7
+ data.tar.gz: 759f1b973090ed6df0ef78daee7a4220b0261090b300a4a9164fcda5a3779d3a831048fa25d9936848903e639358d5942fec4694e6174bb950f908b7abaeec0b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.0.3
4
+
5
+ - samples/elaborate/tetris.rb (basic Tetris implementation)
6
+ - `observe` keyword for observing Model attributes to update the View
7
+ - Ensure `fill`/`stroke` properties in `drawin_area` shapes accept 255-based rgb colors (or rgba with 1-based transparency)
8
+ - Support setting all cairo shape attributes (e.g. `line_width`, `line_cap`, etc...) with declarative syntax (whichever is specified)
9
+ - Upgrade to glimmer 2.6.0
10
+
3
11
  ## 0.0.2
4
12
 
5
13
  - Support re-opening any widget proxy content with `#content {}` block to add more nested widgets inside
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 GTK 0.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 GTK 0.0.3
2
2
  ## Ruby-GNOME Desktop Development GUI Library
3
3
  [![Gem Version](https://badge.fury.io/rb/glimmer-dsl-gtk.svg)](http://badge.fury.io/rb/glimmer-dsl-gtk)
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)
@@ -30,7 +30,7 @@ Linux | Mac | Windows
30
30
  ------|-----|--------
31
31
  ![hello world screenshot Windows](/screenshots/glimmer-dsl-gtk-linux-hello-world.png) | ![hello world screenshot Mac](/screenshots/glimmer-dsl-gtk-mac-hello-world.png) | ![hello world screenshot Windows](/screenshots/glimmer-dsl-gtk-windows-hello-world.png)
32
32
 
33
- NOTE: Glimmer DSL for GTK is currently in early alpha mode (incomplete proof-of-concept). If you want it developed faster, then [open an issue report](https://github.com/AndyObtiva/glimmer-dsl-gtk/issues/new). I have completed some GitHub project features much faster before due to [issue reports](https://github.com/AndyObtiva/glimmer-dsl-gtk/issues) and [pull requests](https://github.com/AndyObtiva/glimmer-dsl-gtk/pulls). Please help make better by contributing, adopting for small or low risk projects, and providing feedback. It is still an early alpha, so the more feedback and issues you report the better. Please help make better by contributing, adopting for small or low risk projects, and providing feedback. It is still an early alpha, so the more feedback and issues you report the better.
33
+ NOTE: Glimmer DSL for GTK is currently in early alpha mode (incomplete proof-of-concept). If you want it developed faster, then [open an issue report](https://github.com/AndyObtiva/glimmer-dsl-gtk/issues/new). I have completed some GitHub project features much faster before due to [issue reports](https://github.com/AndyObtiva/glimmer-dsl-gtk/issues) and [pull requests](https://github.com/AndyObtiva/glimmer-dsl-gtk/pulls). Please help make better by contributing, adopting for small or low risk projects, and providing feedback. It is still an early alpha, so the more feedback and issues you report the better.
34
34
 
35
35
  Other [Glimmer](https://rubygems.org/gems/glimmer) DSL gems you might be interested in:
36
36
  - [glimmer-dsl-swt](https://github.com/AndyObtiva/glimmer-dsl-swt): Glimmer DSL for SWT (JRuby Desktop Development GUI Framework)
@@ -79,7 +79,7 @@ gem install glimmer-dsl-gtk
79
79
 
80
80
  Add the following to `Gemfile`:
81
81
  ```
82
- gem 'glimmer-dsl-gtk', '~> 0.0.2'
82
+ gem 'glimmer-dsl-gtk', '~> 0.0.3'
83
83
  ```
84
84
 
85
85
  And, then run:
@@ -135,6 +135,18 @@ SomeGlimmerApplication.new.launch
135
135
  - Properties: All GTK widget properties can be set via lowercase underscored names (without the 'set_' prefix) nested under widget keywords (e.g. `window {title 'Hello, World'}` sets `title` property of `window`)
136
136
  - Signals: All GTK signals can be wired with `on(signal) { ... }` syntax (e.g. `on(:activate) { do_something }`)
137
137
 
138
+ #### MVC Observer Pattern
139
+
140
+ In Smalltalk-MVC ([Model View Controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) Architectural Pattern), the View is an active View that observes the Model for changes and updates itself.
141
+
142
+ ![MVC](http://3.bp.blogspot.com/-4eW59Ao0ess/ToiBzAiYdZI/AAAAAAAAAOg/SiYa6XHwBFE/s320/Screen+shot+2011-10-02+at+10.22.11+AM.png)
143
+
144
+ This can be achieved with the Glimmer GUI DSL using the `observe` keyword, which takes a model (any object, including `self`) and attribute Symbol or String expression (e.g. `:count` or `'address.street'`).
145
+
146
+ The model is automatically enhanced as an `Glimmer::DataBinding::ObservableModel` / `Glimmer::DataBinding::ObservableHash` / `Glimmer::DataBinding::ObservableArray` depending on its type to support notifying observers of attribute changes (when performed using the attribute writer, which automatically calls added method `notify_observers(attribute)`)
147
+
148
+ Note that it is usually recommended to observe external model objects (not `self`), but `self` is OK in very simple cases or presentation-related attributes only.
149
+
138
150
  ## Girb (Glimmer IRB)
139
151
 
140
152
  You can run the `girb` command (`bin/girb` if you cloned the project locally):
@@ -779,6 +791,198 @@ application('org.glimmer.hello-application') {
779
791
  }.run
780
792
  ```
781
793
 
794
+ #### Tetris
795
+
796
+ [samples/elaborate/tetris.rb](/samples/elaborate/tetris.rb)
797
+
798
+ Linux | Mac | Windows
799
+ ------|-----|--------
800
+ ![tetris](/screenshots/glimmer-dsl-gtk-linux-tetris.png) | ![tetris](/screenshots/glimmer-dsl-gtk-mac-tetris.png) | ![tetris](/screenshots/glimmer-dsl-gtk-windows-tetris.png)
801
+
802
+ Run (via installed gem):
803
+
804
+ ```
805
+ ruby -r glimmer-dsl-gtk -e "require 'samples/elaborate/tetris'"
806
+ ```
807
+
808
+ Run (via locally cloned project):
809
+
810
+ ```
811
+ ruby -r ./lib/glimmer-dsl-gtk.rb samples/elaborate/tetris.rb
812
+ ```
813
+
814
+ Code:
815
+
816
+ ```ruby
817
+ require 'glimmer-dsl-gtk'
818
+
819
+ require_relative 'tetris/model/game'
820
+
821
+ class Tetris
822
+ include Glimmer
823
+
824
+ BLOCK_SIZE = 25
825
+ BEVEL_CONSTANT = 20
826
+ COLOR_GRAY = [192, 192, 192]
827
+
828
+ def initialize
829
+ @game = Model::Game.new
830
+ end
831
+
832
+ def launch
833
+ create_gui
834
+ register_observers
835
+ @game.start!
836
+ @main_window.show
837
+ end
838
+
839
+ def create_gui
840
+ @main_window = window {
841
+ title 'Glimmer Tetris'
842
+ default_size Model::Game::PLAYFIELD_WIDTH * BLOCK_SIZE, Model::Game::PLAYFIELD_HEIGHT * BLOCK_SIZE # + 98
843
+
844
+ box(:vertical) {
845
+ @playfield_blocks = playfield(playfield_width: @game.playfield_width, playfield_height: @game.playfield_height, block_size: BLOCK_SIZE)
846
+ }
847
+
848
+ on(:key_press_event) do |widget, key_event|
849
+ case key_event.keyval
850
+ when 65364 # down arrow
851
+ @game.down!
852
+ when 32 # space
853
+ @game.down!(instant: true)
854
+ when 65362 # up arrow
855
+ case @game.up_arrow_action
856
+ when :instant_down
857
+ @game.down!(instant: true)
858
+ when :rotate_right
859
+ @game.rotate!(:right)
860
+ when :rotate_left
861
+ @game.rotate!(:left)
862
+ end
863
+ when 65361 # left arrow
864
+ @game.left!
865
+ when 65363 # right arrow
866
+ @game.right!
867
+ when 65506 # right shift
868
+ @game.rotate!(:right)
869
+ when 65505 # left shift
870
+ @game.rotate!(:left)
871
+ else
872
+ # Do Nothing
873
+ end
874
+ end
875
+ }
876
+ end
877
+
878
+ def register_observers
879
+ observe(@game, :game_over) do |game_over|
880
+ if game_over
881
+ show_game_over_dialog
882
+ else
883
+ start_moving_tetrominos_down
884
+ end
885
+ end
886
+
887
+ @game.playfield_height.times do |row|
888
+ @game.playfield_width.times do |column|
889
+ observe(@game.playfield[row][column], :color) do |new_color|
890
+ color = new_color
891
+ block = @playfield_blocks[row][column]
892
+ block[:background_square].fill = color
893
+ block[:top_bevel_edge].fill = [color[0] + 4*BEVEL_CONSTANT, color[1] + 4*BEVEL_CONSTANT, color[2] + 4*BEVEL_CONSTANT]
894
+ block[:right_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
895
+ block[:bottom_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
896
+ block[:left_bevel_edge].fill = [color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT]
897
+ block[:border_square].stroke = new_color == Model::Block::COLOR_CLEAR ? COLOR_GRAY : color
898
+ block[:drawing_area].queue_draw
899
+ false
900
+ end
901
+ end
902
+ end
903
+ end
904
+
905
+ def playfield(playfield_width: , playfield_height: , block_size: , &extra_content)
906
+ blocks = []
907
+ box(:vertical) {
908
+ playfield_height.times.map do |row|
909
+ blocks << []
910
+ box(:horizontal) {
911
+ playfield_width.times.map do |column|
912
+ blocks.last << block(row: row, column: column, block_size: block_size)
913
+ end
914
+ }
915
+ end
916
+
917
+ extra_content&.call
918
+ }
919
+ blocks
920
+ end
921
+
922
+ def block(row: , column: , block_size: , &extra_content)
923
+ block = {}
924
+ bevel_pixel_size = 0.16 * block_size.to_f
925
+ color = Model::Block::COLOR_CLEAR
926
+ block[:drawing_area] = drawing_area {
927
+ size_request block_size, block_size
928
+
929
+ block[:background_square] = square(0, 0, block_size) {
930
+ fill *color
931
+ }
932
+
933
+ block[:top_bevel_edge] = polygon(0, 0, block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size, bevel_pixel_size) {
934
+ fill color[0] + 4*BEVEL_CONSTANT, color[1] + 4*BEVEL_CONSTANT, color[2] + 4*BEVEL_CONSTANT
935
+ }
936
+
937
+ block[:right_bevel_edge] = polygon(block_size, 0, block_size - bevel_pixel_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size, block_size) {
938
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
939
+ }
940
+
941
+ block[:bottom_bevel_edge] = polygon(block_size, block_size, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size, block_size - bevel_pixel_size) {
942
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
943
+ }
944
+
945
+ block[:left_bevel_edge] = polygon(0, 0, 0, block_size, bevel_pixel_size, block_size - bevel_pixel_size, bevel_pixel_size, bevel_pixel_size) {
946
+ fill color[0] - BEVEL_CONSTANT, color[1] - BEVEL_CONSTANT, color[2] - BEVEL_CONSTANT
947
+ }
948
+
949
+ block[:border_square] = square(0, 0, block_size) {
950
+ stroke *COLOR_GRAY
951
+ }
952
+
953
+ extra_content&.call
954
+ }
955
+ block
956
+ end
957
+
958
+ def start_moving_tetrominos_down
959
+ unless @tetrominos_start_moving_down
960
+ @tetrominos_start_moving_down = true
961
+ GLib::Timeout.add(@game.delay*1000) do
962
+ @game.down! if !@game.game_over? && !@game.paused?
963
+ true
964
+ end
965
+ end
966
+ end
967
+
968
+ def show_game_over_dialog
969
+ message_dialog(@main_window) { |md|
970
+ title 'Game Over!'
971
+ text "Score: #{@game.high_scores.first.score}\nLines: #{@game.high_scores.first.lines}\nLevel: #{@game.high_scores.first.level}"
972
+
973
+ on(:response) do
974
+ md.destroy
975
+ end
976
+ }.show
977
+
978
+ @game.restart!
979
+ false
980
+ end
981
+ end
982
+
983
+ Tetris.new.launch
984
+ ```
985
+
782
986
  ## Resources
783
987
 
784
988
  - Ruby-Gnome Project: https://github.com/ruby-gnome/ruby-gnome
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
Binary file
@@ -0,0 +1,35 @@
1
+ # Copyright (c) 2021-2022 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 'glimmer/dsl/static_expression'
23
+ require 'glimmer/dsl/top_level_expression'
24
+ require 'glimmer/dsl/observe_expression'
25
+
26
+ module Glimmer
27
+ module DSL
28
+ module Gtk
29
+ class ObserveExpression < StaticExpression
30
+ include TopLevelExpression
31
+ include Glimmer::DSL::ObserveExpression
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,76 @@
1
+ # Copyright (c) 2021 Andy Maleh
2
+ #
3
+ # GNU LESSER GENERAL PUBLIC LICENSE
4
+ # Version 3, 29 June 2007
5
+ #
6
+ # Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
7
+ #
8
+ # Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
9
+ #
10
+ # This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
11
+ #
12
+ # 0. Additional Definitions.
13
+ # As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
14
+ #
15
+ # “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
16
+ #
17
+ # An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
18
+ #
19
+ # A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
20
+ #
21
+ # The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
22
+ #
23
+ # The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
24
+ #
25
+ # 1. Exception to Section 3 of the GNU GPL.
26
+ # You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
27
+ #
28
+ # 2. Conveying Modified Versions.
29
+ # If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
30
+ #
31
+ # a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
32
+ # b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
33
+ # 3. Object Code Incorporating Material from Library Header Files.
34
+ # The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
35
+ #
36
+ # a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
37
+ # b) Accompany the object code with a copy of the GNU GPL and this license document.
38
+ # 4. Combined Works.
39
+ # You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
40
+ #
41
+ # a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
42
+ # b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
43
+ # c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
44
+ # d) Do one of the following:
45
+ # 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
46
+ # 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
47
+ # e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
48
+ # 5. Combined Libraries.
49
+ # You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
50
+ #
51
+ # a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
52
+ # b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
53
+ # 6. Revised Versions of the GNU Lesser General Public License.
54
+ # The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
55
+ #
56
+ # Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
57
+ #
58
+ # If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
59
+
60
+ module Glimmer
61
+ module Gtk
62
+ # Represents Gtk shape objects drawn on area widget, like rectangle, arc, and path
63
+ class Shape
64
+ class Square < Shape
65
+ def draw_shape(drawing_area_widget, cairo_context)
66
+ # expecting 3 args (x, y, length), so duplicate length again to become width/height for rectangle
67
+ draw_args = args.dup
68
+ draw_args << draw_args.last
69
+ cairo_context.rectangle(*draw_args)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ Dir[File.expand_path("./#{File.basename(__FILE__, '.rb')}/*.rb", __dir__)].each {|f| require f}
@@ -104,8 +104,13 @@ module Glimmer
104
104
  end
105
105
  end
106
106
 
107
+ SHAPE_FILL_PROPERTIES = [:fill_rule]
108
+ SHAPE_STROKE_PROPERTIES = [:dash, :font_face, :font_matrix, :font_options, :font_size, :line_cap, :line_join, :line_width, :miter_limit, :scaled_font]
109
+ SHAPE_GENERAL_PROPERTIES = [:matrix, :operator, :tolerance]
110
+
107
111
  attr_reader :parent, :args, :keyword, :block
108
- attr_accessor :fill, :stroke, :dash, :fill_rule, :font_face, :font_matrix, :font_options, :font_size, :line_cap, :line_join, :line_width, :matrix, :miter_limit, :operator, :scaled_font, :source, :source_color, :source_color_row, :source_gdk_color, :source_gdk_rgba, :source_pixbuf, :source_pixbuf_raw, :source_rgb, :source_rgba, :source_rgba_raw, :source_window, :tolerance
112
+ attr_accessor :fill, :stroke
113
+ attr_accessor *(SHAPE_FILL_PROPERTIES + SHAPE_STROKE_PROPERTIES + SHAPE_GENERAL_PROPERTIES)
109
114
  # TODO consider automatically setting attribute accessors by looking up set_xyz methods on cairo context
110
115
 
111
116
  def initialize(keyword, parent, args, &block)
@@ -158,24 +163,35 @@ module Glimmer
158
163
  end
159
164
 
160
165
  def draw_fill(drawing_area_widget, cairo_context)
161
- if fill.size == 3
162
- cairo_context.set_source_rgb(*fill)
163
- elsif fill.size == 4
164
- cairo_context.set_source_rgba(*fill)
166
+ the_fill = normalize_one_based_color(fill)
167
+ if the_fill.size == 3
168
+ cairo_context.set_source_rgb(*the_fill)
169
+ elsif the_fill.size == 4
170
+ cairo_context.set_source_rgba(*the_fill)
171
+ end
172
+ (SHAPE_FILL_PROPERTIES + SHAPE_GENERAL_PROPERTIES).each do |property|
173
+ cairo_context.send("set_#{property}", send(property)) if send(property)
165
174
  end
166
175
  cairo_context.fill
167
176
  end
168
177
 
169
178
  def draw_stroke(drawing_area_widget, cairo_context)
170
- if stroke.size == 3
171
- cairo_context.set_source_rgb(*stroke)
172
- elsif stroke.size == 4
173
- cairo_context.set_source_rgba(*stroke)
179
+ the_stroke = normalize_one_based_color(stroke)
180
+ if the_stroke.size == 3
181
+ cairo_context.set_source_rgb(*the_stroke)
182
+ elsif the_stroke.size == 4
183
+ cairo_context.set_source_rgba(*the_stroke)
184
+ end
185
+ (SHAPE_STROKE_PROPERTIES + SHAPE_GENERAL_PROPERTIES).each do |property|
186
+ cairo_context.send("set_#{property}", send(property)) if send(property)
174
187
  end
175
- cairo_context.set_line_width(line_width)
176
188
  cairo_context.stroke
177
189
  end
178
190
 
191
+ def normalize_one_based_color(rgb)
192
+ rgb.each_with_index.map {|single_color, i| i == 3 ? single_color : single_color / 255.0}
193
+ end
194
+
179
195
  def respond_to?(method_name, include_private = false, &block)
180
196
  method_name = method_name.to_s
181
197
  (method_name.start_with?('set_') && super("#{method_name.sub('set_', '')}=", include_private)) ||
@@ -0,0 +1,48 @@
1
+ # Copyright (c) 2021-2022 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 Tetris
23
+ module Model
24
+ class Block
25
+ COLOR_CLEAR = [255, 255, 255]
26
+
27
+ attr_accessor :color
28
+
29
+ # Initializes with color. Default color (gray) signifies an empty block
30
+ def initialize(color = COLOR_CLEAR)
31
+ @color = color
32
+ end
33
+
34
+ # Clears block color. `quietly` option indicates if it should not notify observers by setting value quietly via variable not attribute writer.
35
+ def clear
36
+ self.color = COLOR_CLEAR unless self.color == COLOR_CLEAR
37
+ end
38
+
39
+ def clear?
40
+ self.color == COLOR_CLEAR
41
+ end
42
+
43
+ def occupied?
44
+ !clear?
45
+ end
46
+ end
47
+ end
48
+ end