savio 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8a837ea050d5cd7e54c5b9f8cea93183f254790aba04518affabc2112e69d55d
4
+ data.tar.gz: ff533e972e728cc937d0f237c097b4d9fb427d1b0d68241a5544be721d2e25d0
5
+ SHA512:
6
+ metadata.gz: c6f30fe529ceb5f0827cfe1e603cc277ecd4a13b7f40c1105286dfe7313a44d39615ad330e53c3c04e885ad74a004cdb4c768a7e73bd1a1d3265479b75f323ad
7
+ data.tar.gz: ec3e89e754d3cccd0a7c4b735c4e1ebb636f25f78ff34d5a4bd1bac8b5193dc3be1e445cd9200a7c2cdad0320dd4583074dffbf531c6e07429052bb82b4964a8
Binary file
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in savio.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 TheRealSavi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,223 @@
1
+ # | *Sav***IO**
2
+ ![Screenshot](https://github.com/TheRealSavi/savio/blob/master/preview.png)
3
+ ## What is it?
4
+
5
+ SavIO is an input/output library created to be used with **Ruby2D**. It adds multiple ways for the user to interact with your application, including :
6
+
7
+ - Sliders
8
+ - Buttons
9
+ - Text Input
10
+ - Color Picker (Work In Progress)
11
+
12
+
13
+ ## How do they work?
14
+
15
+
16
+ Good question! Part of the goal when developing SavIO was to make it as **intuitive** and **simple** as possible, while also being highly **versatile**, **powerful**, and **customizable**.
17
+
18
+ # | ALL OBJECTS
19
+ #### All SavIO objects inherit these basic properties.
20
+
21
+ #### You can access an array of all SavIO Objects by using Savio.elements
22
+ ##### Example:
23
+
24
+ Savio.elements.each do |element|
25
+ element.x += 1 #Moves all elements over by 1 pixel
26
+ end
27
+
28
+ ##### SavIO Commands:
29
+ Savio.hide #This will hide all elements
30
+ Savio.unhide #This will bring them all back
31
+ Savio.stop #This stops the mouse and keyboard event listeners
32
+ Savio.listen #Starts the mouse and keyboard event listeners
33
+ Savio.listening #returns true or false if its listening or not
34
+
35
+
36
+ ## **Creation:**
37
+
38
+ myAwesomeOBJECT = OBJECTNAME.new(params)
39
+
40
+ ### Params:
41
+
42
+ all SavIO object's parameters are optional, if it is not defined then it will use the deafult.
43
+
44
+ | Variable | Description | Default |
45
+ |--|--|--|
46
+ | x | The x position | 0
47
+ | y | The y Position | 0
48
+ | z | The z Position | 1
49
+ | size | The scaling value | 10
50
+ | enabled | If the slider slides | true
51
+ | displayName | The label on top | "Default"
52
+ | draggingEnabled | If the object itself can be moved around the window | false
53
+ | dragType | "move" or "duplicate" If draggingEnabled is true, this is what happens when it drags | "move"
54
+ | shown | If the object is shown or not | true
55
+
56
+ ### Example:
57
+
58
+ `myAwesomeOBJECT = OBJECTNAME.new(x: 100, y: 30, z:2, size: 13, displayName: "Swag")`
59
+
60
+
61
+ ### Methods:
62
+ ----
63
+ | Method | Description |
64
+ |--|--|
65
+ |.remove() | removes the object from the screen |
66
+ | .add() | adds the object back to the screen |
67
+ |.rebuild() | rebuilds the object |
68
+ |.context() | returns a hash with all the variables that make the object |
69
+
70
+ # | Sliders:
71
+ **On top of** all the basic parameters and methods a **Slider** can **also** use these:
72
+
73
+ ### Params:
74
+ | Variable | Description | Default |
75
+ |--|--|--|
76
+ |length | How long the slider is | 100
77
+ |min | The minimum value of the slider | 0
78
+ |max | The maximum value of the slider | 100
79
+ |value| The value of the slider | Random between min and max
80
+ |showValue| If the value should be shown| true
81
+ |labelColor| The color of the labels| '#F5F5F5'
82
+ |sliderColor|Color of the slider line | '#757575'
83
+ |knobColor| Color of the sliders knob | '#5BB36A'
84
+
85
+ ### Example:
86
+
87
+ slippyTheSlider = Slider.new(x: 830, y: 40, length: 220, draggingEnabled: true, dragType: "duplicate")
88
+
89
+ ### Methods:
90
+ ----
91
+ | Method | Description |
92
+ |--|--|
93
+ |.moveKnob(**x**) | Moves the knob to that **x** pixel location on the screen and finds and sets equivalent value for the slider |
94
+ | .setValue(**value**) | Sets the sliders value to that **value** and moves the knob there |
95
+
96
+ ### Basic Usage:
97
+
98
+ if slippyTheSlider.value == 69
99
+ puts "nice"
100
+ end
101
+
102
+ # | Buttons:
103
+ **On top of** all the basic parameters and methods a **Button** can **also** use these:
104
+
105
+ ### Params:
106
+ | Variable | Description | Default |
107
+ |--|--|--|
108
+ |value | Anything you want to be tied to the button | 0
109
+ |selected | Whether the button is selected or not | false
110
+ |buttonManager | The manager that controls this button | nil
111
+ |enforceManager| When a manager is defined, whether the manager should force this button to follow its rule | true
112
+ |baseColor| The color of the labels| '#F5F5F5'
113
+ |selectedColor|Color of the slider line | '#00B3EC'
114
+ |labelColor| Color of the sliders knob | '#F5F5F5'
115
+
116
+ ### Example:
117
+
118
+ buttonBob = Button.new(
119
+ x: 830, y: 90,
120
+ displayName: "Enable Bob?",
121
+ selectedColor: "purple"
122
+ )
123
+
124
+ ### Methods:
125
+ ----
126
+ | Method | Description |
127
+ |--|--|
128
+ |.select(*enforce*) | Selects the button. if left empty *enforce* will be the buttons **@enforceManager** state. when true the manager will enforce its rule on the button. when false, the button will perform as if it were not controlled. |
129
+ | .deslect(*enforce*) | Deselects the button. *enforce* works the same as .select() *(see above)* |
130
+ |.toggle(*enforce*) | Toggles the buttons Selection state. *enforce* works the same as .select() *(see above)*
131
+
132
+
133
+ ### Basic Usage:
134
+
135
+ if buttonBob.selected == true
136
+ puts "Bob is now enabled! Hi Bob!"
137
+ end
138
+
139
+ # | ButtonManager:
140
+ Now I'm sure after reading how a **button** works you're saying, "What in the hell is a **manager**?"
141
+
142
+ ## Let me explain, it's **very simple**.
143
+ A **ButtonManager** is a simple and easy way to **manage a group of multiple buttons**. More specifically, it **controls** the state of **all the buttons** in its group **depending on** the state of **all the other buttons** in its group.
144
+
145
+ ## This is not considered a standard SavIO Object and does not inherit the typical parameters and methods.
146
+
147
+ ### Creation:
148
+
149
+ theSwagMaster = ButtonManager.new(type: "checkbox")
150
+
151
+
152
+ ### Params:
153
+ | Param |Description | Default|
154
+ |--|--|--|
155
+ | type | either "radio" or "checkbox" Decides how the manager should control its buttons | "radio"
156
+
157
+ ### Methods:
158
+ ----
159
+ | Method | Description |
160
+ |--|--|
161
+ |.addButton(**button**) | Adds the **button** to the group of buttons controlled by the manager. This is done automatically when a buttons **@buttonManager** is set to the manager. If called this way however, it will also automatically set the buttons **@buttonManager** to this manager, so they will always be linked.|
162
+ |.removeButton(**button**, *overwrite*) | Removes the **button** from the group of buttons controlled by the manager. This is done automatically when a buttons **@buttonManager** is changed or removed. *overwrite* is not required and is automatically set to true. When true, this will overwrite the **button**'s **@buttonManager** and set it to nil. When false it will not overwrite the buttons **@buttonManager**. **It is highly recommended not to change this since** it will desynchronize the button and manager and cause issues. It is used internally to prevent recursion when removed the manager from the **button** rather than from the **manager**
163
+ |.toggle(**button**) | Toggles the **button** according to the rule of the manager. This is done automatically by the **button** when **button**.toggle() is called and this manager is used. This is true also for .select() and .deselect().
164
+ | .select(**button**) | Selects the **button** according to the rule of the manager.
165
+ | .deselect(**button**) | Deselects the **button** according to the rule of the manager |
166
+
167
+ ### Variables :
168
+ |Variable|Description | Type |
169
+ |--|--|--|
170
+ | buttons |an array of all the buttons that are controlled by this manager |array |
171
+ | selected | an array of all the buttons that are currently selected and in control of this manager | array |
172
+
173
+
174
+ ### Basic Usage:
175
+
176
+ theSwagMaster.selected.each do |button|
177
+ puts button.to_s + " Is currently selected!"
178
+ end
179
+ --
180
+
181
+ if theSwagMaster.selected.include?(button)
182
+ puts "This button is currently selected!"
183
+ end
184
+
185
+ # | InputBox:
186
+ **On top of** all the basic parameters and methods an **InputBox** can **also** use these:
187
+ ### Params:
188
+ | Variable | Description | Default |
189
+ |--|--|--|
190
+ |selected | Whether it is currently focused | false
191
+ |value | The current text input in the field | **@displayName**
192
+ |displayName | The value shown in the text field when nothing is in it. AKA the default value. if a value was specified, this will be overwritten with it. | **@value** or "Default"
193
+ |length| The length of the text box | **@size** * 10
194
+ |width| The width of the text box| **@size** * 1.2
195
+ |color| The color of the box when not focused| 'gray'
196
+ |activeColor|The color of the box when focused | 'green'
197
+
198
+ ### Example:
199
+
200
+ askMeAnything = InputBox.new(
201
+ x: 830, y: 180, size: 30,
202
+ activeColor: 'purple',
203
+ displayName: "What would you like to ask?"
204
+ )
205
+
206
+ ### Methods:
207
+ ----
208
+ | Method | Description |
209
+ |--|--|
210
+ |.addKey(**key**) | Simulates a key input of given **key** |
211
+ | .updateDisplay() | Adds the line follower marker to the text box |
212
+ |.select() | Focuses the text box and lets you type in it |
213
+ |.deselect() | Loses focus of the text box and finalizes value|
214
+ |.toggle() | Toggles the selection value of the text box |
215
+
216
+ ### Basic Usage:
217
+
218
+ if askMeAnything.value == "Favorite Color?"
219
+ puts "Purple"
220
+ end
221
+
222
+ # | ColorSlider:
223
+ These technically work but I'm not done with them so for now I wont bother with documentation.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "savio"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,81 @@
1
+ require "savio/version"
2
+ require 'ruby2d'
3
+
4
+ module Savio
5
+ class Error < StandardError; end
6
+
7
+ require 'savio/IORenderable.rb'
8
+ require 'savio/InputBox.rb'
9
+ require 'savio/Slider.rb'
10
+ require 'savio/Button.rb'
11
+ require 'savio/ButtonManager.rb'
12
+ require 'savio/hsv2rgb.rb'
13
+ require 'savio/ColorSlider.rb'
14
+ require 'savio/Scene.rb'
15
+ require 'savio/io.rb'
16
+
17
+ def self.makeBool(value)
18
+ case value
19
+ when true, 'true', 1, '1', 't' then true
20
+ when false, 'false', nil, '', 0, '0', 'f' then false
21
+ else
22
+ return "no"
23
+ end
24
+ end
25
+
26
+ def self.guessType(value)
27
+ if value.to_i.to_s == value
28
+ return "int"
29
+ elsif value.to_f.to_s == value
30
+ return "float"
31
+ elsif value == "true" || value == "false"
32
+ return "bool"
33
+ else
34
+ return "str"
35
+ end
36
+ end
37
+
38
+ def self.listen()
39
+ @listening = true
40
+ end
41
+
42
+ def self.stop()
43
+ @listening = false
44
+ end
45
+
46
+ def self.listening
47
+ return @listening
48
+ end
49
+
50
+
51
+ @elements = []
52
+ def self.elements
53
+ @elements
54
+ end
55
+
56
+ def self.addElement(element)
57
+ @elements.push(element)
58
+ end
59
+
60
+ def self.removeElement(element)
61
+ @elements.delete(element)
62
+ end
63
+
64
+
65
+ def self.hide
66
+ @elements.each do |e|
67
+ e.remove
68
+ end
69
+ end
70
+
71
+ def self.unhide
72
+ @elements.each do |e|
73
+ e.add
74
+ end
75
+ end
76
+
77
+
78
+ end
79
+
80
+ include Savio
81
+ Savio.listen
Binary file
@@ -0,0 +1,185 @@
1
+ module Savio
2
+ class Button
3
+ include IORenderable
4
+
5
+ attr_accessor :value, :enforceManager
6
+ attr_reader :selected, :buttonManager
7
+
8
+ @@buttons = []
9
+ def self.buttons
10
+ @@buttons
11
+ end
12
+
13
+ def initialize(args = {})
14
+ super(args)
15
+
16
+ @@buttons.push(self)
17
+
18
+ @value = args[:value] || 0
19
+
20
+ @baseColor = args[:baseColor] || '#F5F5F5'
21
+ @selectedColor = args[:selectedColor] || '#00B3EC'
22
+ @labelColor = args[:labelColor] || '#F5F5F5'
23
+
24
+ @selected = args[:selected] || false
25
+
26
+ @buttonManager = args[:buttonManager] || nil
27
+
28
+ @enforceManager = args[:enforceManager] || true
29
+
30
+ @type = args[:type] || 'toggle'
31
+ if @type != 'toggle' && @type != 'clicker'
32
+ @type = 'toggle'
33
+ end
34
+
35
+ @onClick = Proc.new {}
36
+
37
+ build()
38
+ end
39
+
40
+ def type=(newType)
41
+ if newType == 'toggle' || newType == 'clicker'
42
+ @type = newType
43
+ end
44
+ end
45
+
46
+ def baseColor=(c)
47
+ @baseColor = c
48
+ rebuild()
49
+ end
50
+ def selectedColor=(c)
51
+ @selectedColor = c
52
+ rebuild()
53
+ end
54
+ def labelColor=(c)
55
+ @labelColor = c
56
+ rebuild()
57
+ end
58
+
59
+ def onClick(&proc)
60
+ @onClick = proc
61
+ end
62
+
63
+ def buttonManager=(newManager)
64
+ if @buttonManager != nil
65
+ if newManager.class.name != 'Savio::ButtonManager'
66
+ raise ArgumentError, 'Given object ' + newManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
67
+ end
68
+ @buttonManager.removeButton(self, false)
69
+ end
70
+
71
+ if newManager != nil
72
+ @buttonManager = newManager
73
+ if @buttonManager.buttons.include?(self) != true
74
+ @buttonManager.addButton(self)
75
+ end
76
+ else
77
+ @buttonManager = nil
78
+ end
79
+ end
80
+
81
+ def select(enforce = @enforceManager)
82
+ click()
83
+ if enforce == true && @buttonManager != nil
84
+ @buttonManager.select(self)
85
+ else
86
+ @selectCircle.add
87
+ @selected = true
88
+ if @type == 'clicker'
89
+ if @type == 'clicker'
90
+ fade = Thread.new {
91
+ @selectCircle.add
92
+ sleep(0.06)
93
+ @selectCircle.remove
94
+ }
95
+ end
96
+ deselect(enforce)
97
+ end
98
+ end
99
+ end
100
+
101
+ def deselect(enforce = @enforceManager)
102
+ if enforce == true && @buttonManager != nil
103
+ @buttonManager.deselect(self)
104
+ else
105
+ @selectCircle.remove
106
+ @selected = false
107
+ end
108
+ end
109
+
110
+ def toggle(enforce = @enforceManager)
111
+ if @selected
112
+ deselect(enforce)
113
+ else
114
+ select(enforce)
115
+ end
116
+ end
117
+
118
+ def remove()
119
+ super()
120
+ @nameLabel.remove
121
+ @baseCircle.remove
122
+ @selectCircle.remove
123
+ end
124
+
125
+ def add()
126
+ super()
127
+ @nameLabel.add
128
+ @baseCircle.add
129
+ @selectCircle.add
130
+ if @selected
131
+ select()
132
+ else
133
+ deselect()
134
+ end
135
+ end
136
+
137
+ def click()
138
+ @onClick.call()
139
+ end
140
+
141
+ def build()
142
+ @shown = true
143
+
144
+ @nameLabel = Text.new(
145
+ @displayName.to_s,
146
+ x: @x + @size * 2, y: @y - @size * 1.5,
147
+ size: @size * 2,
148
+ color: @labelColor,
149
+ z: @z
150
+ )
151
+ @baseCircle = Circle.new(
152
+ x: @x, y: @y,
153
+ radius: @size,
154
+ color: @baseColor,
155
+ z: @z
156
+ )
157
+ @selectCircle = Circle.new(
158
+ x: @x, y: @y,
159
+ radius: @size * 0.8,
160
+ color: @selectedColor,
161
+ z: @z+1
162
+ )
163
+
164
+ if @buttonManager == nil
165
+ if @selected
166
+ select()
167
+ else
168
+ deselect()
169
+ end
170
+ else
171
+ if @buttonManager.class.name != 'Savio::ButtonManager'
172
+ raise ArgumentError, 'Given object ' + @buttonManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
173
+ end
174
+ @buttonManager.addButton(self)
175
+
176
+ if @selected
177
+ @buttonManager.select(self)
178
+ else
179
+ @buttonManager.deselect(self)
180
+ end
181
+ end
182
+
183
+ end
184
+ end
185
+ end