glimmer-dsl-libui 0.2.2 → 0.2.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: e2d658816439f96cc50c56c93adb7a41798defb44470f47101d65d64fc09bb0d
4
- data.tar.gz: 37abc1ee1f738d73110b9de2e5e9648c0f20d1a81fc928101b6f4d8bf5ca8e85
3
+ metadata.gz: 7033d9033941a3dac53c927ae7ddfdd27127eb4286a936d36e86acb226e57f99
4
+ data.tar.gz: e88c6c0fd4dd246e4f4f7d9f38cc3c8d10f31e0c3cbbd329de4bdd70b4618cf0
5
5
  SHA512:
6
- metadata.gz: c7cbb8b5b0a37fef0d7ac89e4ddc7a18a73d9be0d82dbc58d801cc4a4bc1504dd60f803c8268083eb96fc52c2e807cbd300689a3744c9f43c7b5f1f15c778902
7
- data.tar.gz: 63cfe0c72200e81b196e5cc4fa47473239bc2919db52c027abc5f62ea8de48573a793174d6d61fd2852cc2e19bbb0a5466f0fb6a881af7075bb1e8307c364384
6
+ metadata.gz: 97140fa3ac706b51f054a047d310172c7469eadca3b90307ff8174443c7f9655a6369e59e6f0ce68c52e7dc1189e5c53abd6654b89e0d5b52f2eed638f347d32
7
+ data.tar.gz: 24687499c2bd93b4514d6e7d472e57a788d50315b944bc7289a871b7286b91e46e43fd7651b28b0f1ac4a074f80efdae385a1f3b6d3cdbeaab2cbf46cc0f4c30
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.2.3
4
+
5
+ - Update examples/midi_player.rb to read sounds locally from gem
6
+ - Support examples/basic_draw_text.rb
7
+ - Support dynamic `text` control to be called in `area` `on_draw` listener
8
+ - Support `text` control `default_font` property
9
+ - Support `string` control nestable under `text` to represent an attributed/unattributed string (depending on nestable properties)
10
+ - Support `string` control property `color`
11
+ - Support enum symbols for `align` property of `text` control
12
+ - Support enum symbols for `:italic` font descriptor key (in addition to numbers)
13
+ - Support enum symbols for `:stretch` font descriptor key (in addition to numbers)
14
+ - Support enum symbols for `:weight` font descriptor key (in addition to numbers)
15
+
3
16
  ## 0.2.2
4
17
 
5
18
  - Automatically add `vertical_box` parent to `area` if it did not have a box parent (otherwise, it seems not to show up on Linux, even when directly under `grid`)
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.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.3
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
  [![Maintainability](https://api.codeclimate.com/v1/badges/ce2853efdbecf6ebdc73/maintainability)](https://codeclimate.com/github/AndyObtiva/glimmer-dsl-libui/maintainability)
@@ -197,7 +197,7 @@ Other [Glimmer](https://rubygems.org/gems/glimmer) DSL gems you might be interes
197
197
 
198
198
  ## Table of Contents
199
199
 
200
- - [Glimmer DSL for LibUI 0.2.2](#-glimmer-dsl-for-libui-022)
200
+ - [Glimmer DSL for LibUI 0.2.3](#-glimmer-dsl-for-libui-023)
201
201
  - [Glimmer GUI DSL Concepts](#glimmer-gui-dsl-concepts)
202
202
  - [Usage](#usage)
203
203
  - [Girb (Glimmer IRB)](#girb-glimmer-irb)
@@ -244,6 +244,7 @@ Other [Glimmer](https://rubygems.org/gems/glimmer) DSL gems you might be interes
244
244
  - [Login](#login)
245
245
  - [Timer](#timer)
246
246
  - [Color The Circles](#color-the-circles)
247
+ - [Basic Draw Text](#basic-draw-text)
247
248
  - [Contributing to glimmer-dsl-libui](#contributing-to-glimmer-dsl-libui)
248
249
  - [Help](#help)
249
250
  - [Issues](#issues)
@@ -331,7 +332,7 @@ gem install glimmer-dsl-libui
331
332
  Or install via Bundler `Gemfile`:
332
333
 
333
334
  ```ruby
334
- gem 'glimmer-dsl-libui', '~> 0.2.2'
335
+ gem 'glimmer-dsl-libui', '~> 0.2.3'
335
336
  ```
336
337
 
337
338
  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.
@@ -1276,9 +1277,7 @@ window('Notepad', 500, 300) {
1276
1277
 
1277
1278
  ### Midi Player
1278
1279
 
1279
- This example has prerequisites:
1280
- - Install [TiMidity](http://timidity.sourceforge.net) and ensure `timidity` command is in `PATH` (can be installed via [Homebrew](https://brew.sh) on Mac or [apt-get](https://help.ubuntu.com/community/AptGet/Howto) on Linux).
1281
- - Add `*.mid` files to `~/Music` directory (you may copy the ones included in [sounds](sounds) directory)
1280
+ To run this example, install [TiMidity](http://timidity.sourceforge.net) and ensure `timidity` command is in `PATH` (can be installed via [Homebrew](https://brew.sh) on Mac or [apt-get](https://help.ubuntu.com/community/AptGet/Howto) on Linux).
1282
1281
 
1283
1282
  [examples/midi_player.rb](examples/midi_player.rb)
1284
1283
 
@@ -1417,7 +1416,7 @@ class TinyMidiPlayer
1417
1416
 
1418
1417
  def initialize
1419
1418
  @pid = nil
1420
- @music_directory = File.expand_path(ARGV[0] || '~/Music/')
1419
+ @music_directory = File.expand_path('../sounds', __dir__)
1421
1420
  @midi_files = Dir.glob(File.join(@music_directory, '**/*.mid'))
1422
1421
  .sort_by { |path| File.basename(path) }
1423
1422
  at_exit { stop_midi }
@@ -4538,6 +4537,8 @@ window('Login') {
4538
4537
 
4539
4538
  ### Timer
4540
4539
 
4540
+ To run this example, install [TiMidity](http://timidity.sourceforge.net) and ensure `timidity` command is in `PATH` (can be installed via [Homebrew](https://brew.sh) on Mac or [apt-get](https://help.ubuntu.com/community/AptGet/Howto) on Linux).
4541
+
4541
4542
  [examples/timer.rb](examples/timer.rb)
4542
4543
 
4543
4544
  Run with this command from the root of the project if you cloned the project:
@@ -4939,6 +4940,102 @@ end
4939
4940
  ColorTheCircles.new.launch
4940
4941
  ```
4941
4942
 
4943
+ ### Basic Draw Text
4944
+
4945
+ [examples/basic_draw_text.rb](examples/basic_draw_text.rb)
4946
+
4947
+ Run with this command from the root of the project if you cloned the project:
4948
+
4949
+ ```
4950
+ ruby -r './lib/glimmer-dsl-libui' examples/basic_draw_text.rb
4951
+ ```
4952
+
4953
+ Run with this command if you installed the [Ruby gem](https://rubygems.org/gems/glimmer-dsl-libui):
4954
+
4955
+ ```
4956
+ ruby -r glimmer-dsl-libui -e "require 'examples/basic_draw_text'"
4957
+ ```
4958
+
4959
+ Mac
4960
+
4961
+ ![glimmer-dsl-libui-mac-basic-draw-text.png](images/glimmer-dsl-libui-mac-basic-draw-text.png)
4962
+
4963
+ Linux
4964
+
4965
+ ![glimmer-dsl-libui-linux-basic-draw-text.png](images/glimmer-dsl-libui-linux-basic-draw-text.png)
4966
+
4967
+ New [Glimmer DSL for LibUI](https://rubygems.org/gems/glimmer-dsl-libui) Version:
4968
+
4969
+ ```ruby
4970
+ require 'glimmer-dsl-libui'
4971
+
4972
+ # Michael Ende (1929-1995)
4973
+ # The Neverending Story is a fantasy novel by German writer Michael Ende,
4974
+ # The English version, translated by Ralph Manheim, was published in 1983.
4975
+ class BasicDrawText
4976
+ include Glimmer
4977
+
4978
+ def alternating_color_string(&block)
4979
+ @index ||= 0
4980
+ @index += 1
4981
+ string {
4982
+ if @index.odd?
4983
+ color r: 0.5, g: 0, b: 0.25, a: 0.7
4984
+ else
4985
+ color r: 0, g: 0.5, b: 0, a: 0.7
4986
+ end
4987
+
4988
+ block.call + "\n\n"
4989
+ }
4990
+ end
4991
+
4992
+ def launch
4993
+ window('Michael Ende (1929-1995) The Neverending Story', 600, 400) {
4994
+ margined true
4995
+
4996
+ area {
4997
+ on_draw do |area_draw_params|
4998
+ text(0, 0, area_draw_params[:area_width]) {
4999
+ align 0
5000
+ default_font family: 'Georgia', size: 13, weight: 500, italic: 0, stretch: 4
5001
+
5002
+ alternating_color_string {
5003
+ ' At last Ygramul sensed that something was coming toward ' \
5004
+ 'her. With the speed of lightning, she turned about, confronting ' \
5005
+ 'Atreyu with an enormous steel-blue face. Her single eye had a ' \
5006
+ 'vertical pupil, which stared at Atreyu with inconceivable malignancy. '
5007
+ }
5008
+ alternating_color_string {
5009
+ ' A cry of fear escaped Bastian. '
5010
+ }
5011
+ alternating_color_string {
5012
+ ' A cry of terror passed through the ravine and echoed from ' \
5013
+ 'side to side. Ygramul turned her eye to left and right, to see if ' \
5014
+ 'someone else had arrived, for that sound could not have been ' \
5015
+ 'made by the boy who stood there as though paralyzed with ' \
5016
+ 'horror. '
5017
+ }
5018
+ alternating_color_string {
5019
+ ' Could she have heard my cry? Bastion wondered in alarm. ' \
5020
+ "But that's not possible. "
5021
+ }
5022
+ alternating_color_string {
5023
+ ' And then Atreyu heard Ygramuls voice. It was very high ' \
5024
+ 'and slightly hoarse, not at all the right kind of voice for that ' \
5025
+ 'enormous face. Her lips did not move as she spoke. It was the ' \
5026
+ 'buzzing of a great swarm of hornets that shaped itself into ' \
5027
+ 'words. '
5028
+ }
5029
+ }
5030
+ end
5031
+ }
5032
+ }.show
5033
+ end
5034
+ end
5035
+
5036
+ BasicDrawText.new.launch
5037
+ ```
5038
+
4942
5039
  ## Contributing to glimmer-dsl-libui
4943
5040
 
4944
5041
  - Check out the latest master to make sure the feature hasn't been
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -0,0 +1,67 @@
1
+ require 'glimmer-dsl-libui'
2
+
3
+ # Michael Ende (1929-1995)
4
+ # The Neverending Story is a fantasy novel by German writer Michael Ende,
5
+ # The English version, translated by Ralph Manheim, was published in 1983.
6
+ class BasicDrawText
7
+ include Glimmer
8
+
9
+ def alternating_color_string(&block)
10
+ @index ||= 0
11
+ @index += 1
12
+ string {
13
+ if @index.odd?
14
+ color r: 0.5, g: 0, b: 0.25, a: 0.7
15
+ else
16
+ color r: 0, g: 0.5, b: 0, a: 0.7
17
+ end
18
+
19
+ block.call + "\n\n"
20
+ }
21
+ end
22
+
23
+ def launch
24
+ window('Michael Ende (1929-1995) The Neverending Story', 600, 400) {
25
+ margined true
26
+
27
+ area {
28
+ on_draw do |area_draw_params|
29
+ text(0, 0, area_draw_params[:area_width]) {
30
+ align 0
31
+ default_font family: 'Georgia', size: 13, weight: 500, italic: 0, stretch: 4
32
+
33
+ alternating_color_string {
34
+ ' At last Ygramul sensed that something was coming toward ' \
35
+ 'her. With the speed of lightning, she turned about, confronting ' \
36
+ 'Atreyu with an enormous steel-blue face. Her single eye had a ' \
37
+ 'vertical pupil, which stared at Atreyu with inconceivable malignancy. '
38
+ }
39
+ alternating_color_string {
40
+ ' A cry of fear escaped Bastian. '
41
+ }
42
+ alternating_color_string {
43
+ ' A cry of terror passed through the ravine and echoed from ' \
44
+ 'side to side. Ygramul turned her eye to left and right, to see if ' \
45
+ 'someone else had arrived, for that sound could not have been ' \
46
+ 'made by the boy who stood there as though paralyzed with ' \
47
+ 'horror. '
48
+ }
49
+ alternating_color_string {
50
+ ' Could she have heard my cry? Bastion wondered in alarm. ' \
51
+ "But that's not possible. "
52
+ }
53
+ alternating_color_string {
54
+ ' And then Atreyu heard Ygramuls voice. It was very high ' \
55
+ 'and slightly hoarse, not at all the right kind of voice for that ' \
56
+ 'enormous face. Her lips did not move as she spoke. It was the ' \
57
+ 'buzzing of a great swarm of hornets that shaped itself into ' \
58
+ 'words. '
59
+ }
60
+ }
61
+ end
62
+ }
63
+ }.show
64
+ end
65
+ end
66
+
67
+ BasicDrawText.new.launch
@@ -9,7 +9,7 @@ class TinyMidiPlayer
9
9
 
10
10
  def initialize
11
11
  @pid = nil
12
- @music_directory = File.expand_path(ARGV[0] || '~/Music/')
12
+ @music_directory = File.expand_path('../sounds', __dir__)
13
13
  @midi_files = Dir.glob(File.join(@music_directory, '**/*.mid'))
14
14
  .sort_by { |path| File.basename(path) }
15
15
  at_exit { stop_midi }
Binary file
@@ -40,7 +40,6 @@ module Glimmer
40
40
  super
41
41
  parent.post_add_content
42
42
  end
43
-
44
43
  end
45
44
  end
46
45
  end
@@ -22,6 +22,7 @@
22
22
  require 'glimmer/dsl/expression'
23
23
  require 'glimmer/libui/control_proxy'
24
24
  require 'glimmer/libui/shape'
25
+ require 'glimmer/libui/attributed_string'
25
26
 
26
27
  module Glimmer
27
28
  module DSL
@@ -30,7 +31,8 @@ module Glimmer
30
31
  def can_interpret?(parent, keyword, *args, &block)
31
32
  (
32
33
  parent.is_a?(Glimmer::LibUI::ControlProxy) or
33
- parent.is_a?(Glimmer::LibUI::Shape)
34
+ parent.is_a?(Glimmer::LibUI::Shape) or
35
+ parent.is_a?(Glimmer::LibUI::AttributedString)
34
36
  ) and
35
37
  block.nil? and
36
38
  parent.respond_to?(keyword, *args)
@@ -0,0 +1,48 @@
1
+ # Copyright (c) 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 'glimmer/dsl/static_expression'
23
+ require 'glimmer/dsl/parent_expression'
24
+ require 'glimmer/libui/control_proxy/text_proxy'
25
+ require 'glimmer/libui/attributed_string'
26
+
27
+ module Glimmer
28
+ module DSL
29
+ module Libui
30
+ class StringExpression < StaticExpression
31
+ include ParentExpression
32
+
33
+ def can_interpret?(parent, keyword, *args, &block)
34
+ super and
35
+ parent.is_a?(Glimmer::LibUI::ControlProxy::TextProxy)
36
+ end
37
+
38
+ def interpret(parent, keyword, *args, &block)
39
+ Glimmer::LibUI::AttributedString.new(keyword, parent, args, &block)
40
+ end
41
+
42
+ def add_content(parent, keyword, *args, &block)
43
+ parent.post_add_content
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 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 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/control_proxy/area_proxy'
24
+ require 'glimmer/libui/parent'
25
+ require 'glimmer/libui/control_proxy/transformable'
26
+
27
+ module Glimmer
28
+ module LibUI
29
+ class AttributedString
30
+ attr_accessor :string
31
+ attr_reader :block, :keyword, :parent_proxy, :args
32
+
33
+ def initialize(keyword, parent_proxy, args, &block)
34
+ @keyword = keyword
35
+ @parent_proxy = parent_proxy
36
+ @args = args
37
+ @string = @args.first || ''
38
+ @block = block
39
+ post_add_content if @block.nil?
40
+ end
41
+
42
+ def font(value = nil)
43
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
44
+ end
45
+
46
+ def color(value = nil)
47
+ if value.nil?
48
+ @color
49
+ else
50
+ @color = Glimmer::LibUI.interpret_color(value)
51
+ end
52
+ end
53
+
54
+ def background(value = nil)
55
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
56
+ end
57
+
58
+ def underline(value = nil)
59
+ # UI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, start, start + @string.size)
60
+ end
61
+
62
+ def post_add_content
63
+ block_result = block&.call
64
+ @string = block_result if block_result.is_a?(String)
65
+ @parent_proxy&.post_initialize_child(self)
66
+ end
67
+
68
+ def draw(area_draw_params)
69
+ @start = ::LibUI.attributed_string_len(@parent_proxy.attributed_string)
70
+ ::LibUI.attributed_string_append_unattributed(@parent_proxy.attributed_string, @string)
71
+ unless color.nil?
72
+ color_attribute = ::LibUI.new_color_attribute(@color[:r], @color[:g], @color[:b], @color[:a] || 1.0)
73
+ ::LibUI.attributed_string_set_attribute(@parent_proxy.attributed_string, color_attribute, @start, @start + @string.size)
74
+ end
75
+ end
76
+
77
+ def destroy
78
+ @parent_proxy&.children&.delete(self)
79
+ end
80
+
81
+ def redraw
82
+ area_proxy&.queue_redraw_all
83
+ end
84
+
85
+ def area_proxy
86
+ @parent_proxy.parent_proxy
87
+ end
88
+ end
89
+ end
90
+ end
@@ -180,7 +180,7 @@ module Glimmer
180
180
  end
181
181
 
182
182
  def ext_key_to_symbol(ext_key_value)
183
- Glimmer::LibUI.enum_symbols(:ext_key)[ext_key_value - 1].to_s.to_sym if ext_key_value > 0
183
+ Glimmer::LibUI.enum_value_to_symbol(:ext_key, ext_key_value)
184
184
  end
185
185
 
186
186
  def modifiers_to_symbols(modifiers_value)
@@ -34,9 +34,9 @@ module Glimmer
34
34
  {
35
35
  family: @font_descriptor.Family.to_s,
36
36
  size: @font_descriptor.Size,
37
- weight: @font_descriptor.Weight,
38
- italic: @font_descriptor.Italic,
39
- stretch: @font_descriptor.Stretch
37
+ weight: Glimmer::LibUI.enum_value_to_symbol(:text_weight, @font_descriptor.Weight),
38
+ italic: Glimmer::LibUI.enum_value_to_symbol(:text_italic, @font_descriptor.Italic),
39
+ stretch: Glimmer::LibUI.enum_value_to_symbol(:text_stretch, @font_descriptor.Stretch),
40
40
  }
41
41
  end
42
42
 
@@ -34,13 +34,11 @@ module Glimmer
34
34
  include Parent
35
35
  prepend Transformable
36
36
 
37
- # TODO support mode without parent proxy
38
37
  def initialize(keyword, parent, args, &block)
39
38
  @keyword = keyword
40
39
  @parent_proxy = parent
41
40
  @args = args
42
41
  @block = block
43
- @enabled = true
44
42
  post_add_content if @block.nil?
45
43
  end
46
44
 
@@ -0,0 +1,158 @@
1
+ # Copyright (c) 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 'glimmer/libui/control_proxy'
23
+ require 'glimmer/libui/control_proxy/area_proxy'
24
+ require 'glimmer/libui/parent'
25
+ require 'glimmer/libui/control_proxy/transformable'
26
+
27
+ module Glimmer
28
+ module LibUI
29
+ class ControlProxy
30
+ # Proxy for LibUI text objects
31
+ #
32
+ # Follows the Proxy Design Pattern
33
+ class TextProxy < ControlProxy
34
+ include Parent
35
+ prepend Transformable
36
+
37
+ def initialize(keyword, parent, args, &block)
38
+ @keyword = keyword
39
+ @parent_proxy = parent
40
+ @args = args
41
+ @block = block
42
+ post_add_content if @block.nil?
43
+ end
44
+
45
+ def post_add_content
46
+ super
47
+ if @parent_proxy.nil? && AreaProxy.current_area_draw_params
48
+ draw(AreaProxy.current_area_draw_params)
49
+ destroy
50
+ end
51
+ end
52
+
53
+ def draw(area_draw_params)
54
+ children.dup.each {|child| child.draw(area_draw_params)}
55
+ build_control
56
+ ::LibUI.draw_text(area_draw_params[:context], @libui, x, y)
57
+ ::LibUI.draw_free_text_layout(@libui)
58
+ end
59
+
60
+ def destroy
61
+ @parent_proxy&.children&.delete(self)
62
+ ControlProxy.control_proxies.delete(self)
63
+ end
64
+
65
+ def redraw
66
+ @parent_proxy&.queue_redraw_all
67
+ end
68
+
69
+ def x(value = nil)
70
+ if value.nil?
71
+ @x ||= args[0] || 0
72
+ else
73
+ @x = value
74
+ end
75
+ end
76
+ alias x= x
77
+ alias set_x x
78
+
79
+ def y(value = nil)
80
+ if value.nil?
81
+ @y ||= args[1] || 0
82
+ else
83
+ @y = value
84
+ end
85
+ end
86
+ alias y= y
87
+ alias set_y y
88
+
89
+ def width(value = nil)
90
+ if value.nil?
91
+ @width ||= args[2] || (AreaProxy.current_area_draw_params && AreaProxy.current_area_draw_params[:width])
92
+ else
93
+ @width = value
94
+ end
95
+ end
96
+ alias width= width
97
+ alias set_width width
98
+
99
+ def attributed_string
100
+ @attributed_string ||= ::LibUI.new_attributed_string('')
101
+ end
102
+
103
+ def default_font(value = nil)
104
+ if value.nil?
105
+ @default_font ||= {}
106
+ else
107
+ @default_font = value
108
+ end
109
+ end
110
+ alias default_font= default_font
111
+ alias set_default_font default_font
112
+
113
+ def default_font_descriptor
114
+ @default_font_descriptor ||= ::LibUI::FFI::FontDescriptor.malloc
115
+ @default_font_descriptor.Family = default_font[:family] || 'Helvetica'
116
+ @default_font_descriptor.Size = default_font[:size] || 12.0
117
+ @default_font_descriptor.Weight = Glimmer::LibUI.enum_symbol_to_value(:text_weight, default_font[:weight], default_symbol: :normal)
118
+ @default_font_descriptor.Italic = Glimmer::LibUI.enum_symbol_to_value(:text_italic, default_font[:italic], default_symbol: :normal)
119
+ @default_font_descriptor.Stretch = Glimmer::LibUI.enum_symbol_to_value(:text_stretch, default_font[:stretch], default_symbol: :normal)
120
+ @default_font_descriptor
121
+ end
122
+
123
+ def align(value = nil)
124
+ if value.nil?
125
+ @align ||= {}
126
+ else
127
+ @align = value
128
+ end
129
+ end
130
+ alias align= align
131
+ alias set_align align
132
+
133
+ def draw_text_layout_params
134
+ @draw_text_layout_params ||= ::LibUI::FFI::DrawTextLayoutParams.malloc
135
+ @draw_text_layout_params.String = attributed_string
136
+ @draw_text_layout_params.DefaultFont = default_font_descriptor
137
+ @draw_text_layout_params.Width = width
138
+ @draw_text_layout_params.Align = Glimmer::LibUI.enum_symbol_to_value(:draw_text_align, align, default_symbol: :center)
139
+ @draw_text_layout_params
140
+ end
141
+
142
+ private
143
+
144
+ def build_control
145
+ @libui = ::LibUI.draw_new_text_layout(draw_text_layout_params)
146
+ end
147
+
148
+ def init_draw_brush(draw_brush, draw_brush_args)
149
+ draw_brush.Type = Glimmer::LibUI.enum_symbol_to_value(:draw_brush_type, draw_brush_args[:type])
150
+ draw_brush.R = (draw_brush_args[:r] || draw_brush_args[:red]).to_f / 255.0
151
+ draw_brush.G = (draw_brush_args[:g] || draw_brush_args[:green]).to_f / 255.0
152
+ draw_brush.B = (draw_brush_args[:b] || draw_brush_args[:blue]).to_f / 255.0
153
+ draw_brush.A = (draw_brush_args[:a] || draw_brush_args[:alpha])
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
data/lib/glimmer/libui.rb CHANGED
@@ -96,18 +96,28 @@ module Glimmer
96
96
  value
97
97
  end
98
98
 
99
- # Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
100
99
  def enum_symbols(enum_name)
100
+ enum_symbol_values(enum_name).keys
101
+ end
102
+
103
+ # Returns ruby underscored symbols for enum values starting with enum name (camelcase, e.g. 'ext_key')
104
+ def enum_symbol_values(enum_name)
101
105
  enum_name = enum_name.to_s.underscore.to_sym
102
106
  @enum_symbols ||= {}
103
- @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 }
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
108
+ end
109
+
110
+ def enum_value_to_symbol(enum_name, enum_value)
111
+ enum_symbol_values(enum_name).invert[enum_value]
104
112
  end
105
113
 
106
- def enum_symbol_to_value(enum_name, enum_symbol, default_index: 0)
114
+ def enum_symbol_to_value(enum_name, enum_symbol, default_symbol: nil, default_index: 0)
107
115
  if enum_symbol.is_a?(Integer)
108
116
  enum_symbol
109
117
  elsif enum_symbols(enum_name).include?(enum_symbol.to_s.to_sym)
110
- ::LibUI.const_get("#{enum_name}_#{enum_symbol}".camelcase(:upper))
118
+ enum_symbol_values(enum_name)[enum_symbol]
119
+ elsif default_symbol
120
+ enum_symbol_to_value(enum_name, default_symbol)
111
121
  else
112
122
  enum_symbol_to_value(enum_name, enum_symbols(enum_name)[default_index])
113
123
  end
@@ -130,7 +140,7 @@ module Glimmer
130
140
 
131
141
  # Calls block on the main GUI event loop after time_in_seconds delay, repeating indefinitely by default
132
142
  # If `repeat:` keyword arg is passed with an Integer value, it repeats for that number of times
133
- # If `repeat:` keyword arg is passed with false or 0, then the block is only called onces
143
+ # If `repeat:` keyword arg is passed with false or 0, then the block is only called once
134
144
  # If block returns false at any point, the timer is stopped from further repetitions regardless of `repeat:` keyword arg value
135
145
  # If block returns true at any point, the timer continues for another repetition regardless of `repeat:` keyword arg value
136
146
  def timer(time_in_seconds = 0.1, repeat: true, &block)
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.2
4
+ version: 0.2.3
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-10-05 00:00:00.000000000 Z
11
+ date: 2021-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer
@@ -215,6 +215,7 @@ files:
215
215
  - examples/basic_area.rb
216
216
  - examples/basic_area2.rb
217
217
  - examples/basic_button.rb
218
+ - examples/basic_draw_text.rb
218
219
  - examples/basic_entry.rb
219
220
  - examples/basic_table.rb
220
221
  - examples/basic_table_button.rb
@@ -254,9 +255,11 @@ files:
254
255
  - lib/glimmer/dsl/libui/property_expression.rb
255
256
  - lib/glimmer/dsl/libui/save_file_expression.rb
256
257
  - lib/glimmer/dsl/libui/shape_expression.rb
258
+ - lib/glimmer/dsl/libui/string_expression.rb
257
259
  - lib/glimmer/dsl/libui/tab_item_expression.rb
258
260
  - lib/glimmer/fiddle_consumer.rb
259
261
  - lib/glimmer/libui.rb
262
+ - lib/glimmer/libui/attributed_string.rb
260
263
  - lib/glimmer/libui/control_proxy.rb
261
264
  - lib/glimmer/libui/control_proxy/area_proxy.rb
262
265
  - lib/glimmer/libui/control_proxy/area_proxy/scrolling_area_proxy.rb
@@ -310,6 +313,7 @@ files:
310
313
  - lib/glimmer/libui/control_proxy/radio_buttons_proxy.rb
311
314
  - lib/glimmer/libui/control_proxy/tab_item_proxy.rb
312
315
  - lib/glimmer/libui/control_proxy/table_proxy.rb
316
+ - lib/glimmer/libui/control_proxy/text_proxy.rb
313
317
  - lib/glimmer/libui/control_proxy/transformable.rb
314
318
  - lib/glimmer/libui/control_proxy/window_proxy.rb
315
319
  - lib/glimmer/libui/parent.rb