savio 0.1.5 → 0.1.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a8f5301cac1dd32ee16091ccc8591adb75ffaa16914a432ff98a930977dc3ed
4
- data.tar.gz: 20c6ef7442e96b9f3d05180240fbf98391281b4304238754c4c3b7be248726ad
3
+ metadata.gz: 9975ed5556bca2236e60870fe41481ccb6067da3817721d3acfc8bbe337f8dfe
4
+ data.tar.gz: 6a15f45c3c8bba686a1f70d604b532f037007635842985ebf01ade3b96999ca3
5
5
  SHA512:
6
- metadata.gz: 8ee92641fa82e6ce0c73609e8367ae10df5968f5adf6a3313103201fadf665db4f4324cdbcff0f2f336e9e7b7f40b0ca0f143a5c5584ec469c20631980616a49
7
- data.tar.gz: 0e95bd3a5840fd132b65e06d9df92d75fca22c1432e3f6177b9380546e5fc7a60677ad774ad487e28957c955feb7066a90ce3a0e9e8ed2ec137ce4ed14694d4c
6
+ metadata.gz: 312d42bc7d9c08dac70b0e60ef7da172ca909acbf00b5a5dc9865702dff19be46823a7360b7ce731a83b46a0e8bdc2bdc18e69788ec319757f481748f7c1d458
7
+ data.tar.gz: ebcda30b1685410d07c59f06cc71c6116007b198c5f8b86a2e3ffbf78c1e57dcc60faa58c633969a76e9a37916748cacbaefd35cb6d0bf49df6d36d5d244cb6c
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -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"
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # | *Sav***IO**
2
+ [![Gem Version](https://badge.fury.io/rb/savio.svg)](https://badge.fury.io/rb/savio)
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
+ ## How to install?
13
+
14
+ Easy! do:
15
+
16
+ gem install savio
17
+
18
+ then in your program do:
19
+
20
+ require "savio"
21
+
22
+ ## How do they work?
23
+
24
+
25
+ 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**.
26
+
27
+ # | ALL OBJECTS
28
+ #### All SavIO objects inherit these basic properties.
29
+
30
+ #### You can access an array of all SavIO Objects by using Savio.elements
31
+ ##### Example:
32
+
33
+ Savio.elements.each do |element|
34
+ element.x += 1 #Moves all elements over by 1 pixel
35
+ end
36
+
37
+ ##### SavIO Commands:
38
+ Savio.hide #This will hide all elements
39
+ Savio.unhide #This will bring them all back
40
+ Savio.stop #This stops the mouse and keyboard event listeners
41
+ Savio.listen #Starts the mouse and keyboard event listeners
42
+ Savio.listening #returns true or false if its listening or not
43
+
44
+
45
+ ## **Creation:**
46
+
47
+ myAwesomeOBJECT = OBJECTNAME.new(params)
48
+
49
+ ### Params:
50
+
51
+ all SavIO object's parameters are optional, if it is not defined then it will use the default.
52
+
53
+ | Variable | Description | Default |
54
+ |--|--|--|
55
+ | x | The x position | 0
56
+ | y | The y Position | 0
57
+ | z | The z Position | 1
58
+ | size | The scaling value | 10
59
+ | enabled | If the object can be interacted with by the user | true
60
+ | displayName | The name of the object | ""
61
+ | draggingEnabled | If the object itself can be moved around the window | false
62
+ | dragType | "move" or "duplicate" If draggingEnabled is true, this is what happens when it drags | "move"
63
+ | shown | If the object is shown or not | true
64
+
65
+ ### Example:
66
+
67
+ `myAwesomeOBJECT = OBJECTNAME.new(x: 100, y: 30, z:2, size: 13, displayName: "Swag")`
68
+
69
+
70
+ ### Methods:
71
+ ----
72
+ | Method | Description |
73
+ |--|--|
74
+ |.remove() | removes the object from the screen |
75
+ | .add() | adds the object back to the screen |
76
+ |.rebuild() | rebuilds the object |
77
+ |.context() | returns a hash with all the variables that make the object |
78
+
79
+ # | Sliders:
80
+ **On top of** all the basic parameters and methods a **Slider** can **also** use these:
81
+
82
+ ### Params:
83
+ | Variable | Description | Default |
84
+ |--|--|--|
85
+ |length | How long the slider is | 100
86
+ |min | The minimum value of the slider | 0
87
+ |max | The maximum value of the slider | 100
88
+ |value| The value of the slider | Random between min and max
89
+ |style|'knob' or 'fill' changes the style the slider is shown with|'knob' |
90
+ |showValue| If the value label should be shown| true
91
+ |labelColor| The color of the labels| '#F5F5F5'
92
+ |sliderColor| Color of the slider line | '#757575'
93
+ |knobColor| Color of the sliders knob | '#5BB36A'
94
+
95
+ ### Example:
96
+
97
+ slippyTheSlider = Slider.new(x: 830, y: 40, length: 220, draggingEnabled: true, dragType: "duplicate")
98
+
99
+ ### Methods:
100
+ ----
101
+ | Method | Description |
102
+ |--|--|
103
+ |.moveKnob(**x**) | Moves the knob to that **x** pixel location on the screen and finds and sets equivalent value for the slider |
104
+ |.setValue(**value**)|Sets the sliders value to that value and moves the knob there (same as .value=)|
105
+ | .value = **value** | Sets the sliders value to that **value** and moves the knob there (same as .setValue)|
106
+ |.onChange| Calls the given proc any time the sliders **value** is updated(see basic usage)|
107
+
108
+ ### Basic Usage:
109
+
110
+ if slippyTheSlider.value == 69
111
+ puts "nice"
112
+ end
113
+
114
+ -----
115
+
116
+ slippyTheSlider.onChange do
117
+ puts "Value Changed! new value is: " + slippyTheSlider.value.to_s
118
+ end
119
+
120
+ # | Buttons:
121
+ **On top of** all the basic parameters and methods a **Button** can **also** use these:
122
+
123
+ ### Params:
124
+ | Variable | Description | Default |
125
+ |--|--|--|
126
+ |selected | Whether the button is selected or not | false
127
+ |type | Whether the button will act normally('toggle') or instantly deselect itself('clicker') | 'toggle'
128
+ |style|'box' or 'badge'. Determines the style the button should be rendered with| 'badge'
129
+ |length|Only used for the 'box' style. Determines the length of the button| @size * 10
130
+ |height|Only used for the 'box' style. Determines the height of the button| @size * 1.2
131
+ |cooldownTime|Time needed to wait in seconds until the button may be clicked again| 0.0
132
+ |buttonManager | The manager that controls this button | nil
133
+ |enforceManager| When a manager is defined, whether the manager should force this button to follow its rule | true
134
+ |baseColor| Color shown when the button is deselected| '#F5F5F5'
135
+ |selectedColor|Color shown when the button is selected | '#00B3EC'
136
+ |labelColor| Color of the buttons displayName label | '#F5F5F5'
137
+
138
+ ### Example:
139
+
140
+ clickyBob = Button.new(
141
+ x: 830, y: 90,
142
+ displayName: "Enable Bob?",
143
+ selectedColor: "purple",
144
+ type : 'clicker'
145
+ )
146
+ -----
147
+ anotherButton = Button.new(
148
+ x: 830, y: 150,
149
+ displayName: "Enable flux capacitors?",
150
+ )
151
+
152
+
153
+ ### Methods:
154
+ ----
155
+ | Method | Description |
156
+ |--|--|
157
+ |.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. |
158
+ |.deselect(*enforce*) | Deselects the button. *enforce* works the same as .select() *(see above)* |
159
+ |.toggle(*enforce*) | Toggles the buttons Selection state. *enforce* works the same as .select() *(see above)*
160
+ |.selected = **bool** | Selects or deselects the button whether given true or false |
161
+ |.timeLastClicked| returns the unix time of the last click
162
+ | .onClick | Takes in a proc that will be ran every time the button gets selected (See example and basic usage)|
163
+
164
+ ### Basic Usage:
165
+
166
+ clickyBob.onClick do
167
+ puts "Bob is now enabled! Hi Bob!"
168
+ end
169
+ -----
170
+ if anotherButton.selected == true do
171
+ puts "Flux capacitors now enabled"
172
+ end
173
+
174
+ # | ButtonManager:
175
+ Now I'm sure after reading how a **button** works you're saying, "What in the hell is a **manager**?"
176
+
177
+ ## Let me explain, it's **very simple**.
178
+ 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.
179
+
180
+ ## This is not considered a standard SavIO Object and does not inherit the typical parameters and methods.
181
+
182
+ ### Creation:
183
+
184
+ theSwagMaster = ButtonManager.new(type: "checkbox")
185
+
186
+
187
+ ### Params:
188
+ | Param |Description | Default|
189
+ |--|--|--|
190
+ | type | either "radio" or "checkbox" Decides how the manager should control its buttons | "radio"
191
+
192
+ ### Methods:
193
+ ----
194
+ | Method | Description |
195
+ |--|--|
196
+ |.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.|
197
+ |.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**
198
+ |.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().
199
+ | .select(**button**) | Selects the **button** according to the rule of the manager.
200
+ | .deselect(**button**) | Deselects the **button** according to the rule of the manager |
201
+
202
+ ### Variables :
203
+ |Variable|Description | Type |
204
+ |--|--|--|
205
+ | buttons |an array of all the buttons that are controlled by this manager |array |
206
+ | selected | an array of all the buttons that are currently selected and in control of this manager | array |
207
+
208
+
209
+ ### Basic Usage:
210
+
211
+ theSwagMaster.selected.each do |button|
212
+ puts button.to_s + " Is currently selected!"
213
+ end
214
+ --
215
+
216
+ if theSwagMaster.selected.include?(button)
217
+ puts "This button is currently selected!"
218
+ end
219
+
220
+ # | InputBox:
221
+ **On top of** all the basic parameters and methods an **InputBox** can **also** use these:
222
+ ### Params:
223
+ | Variable | Description | Default |
224
+ |--|--|--|
225
+ |selected | Whether it is currently focused | false
226
+ |value | The current text input in the field | **@displayName**
227
+ |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"
228
+ |length| The length of the text box | **@size** * 10
229
+ |width| The width of the text box| **@size** * 1.2
230
+ |color| The color of the box when not focused| '#F5F5F5'
231
+ |activeColor|The color of the box when focused | '#5BB36A'
232
+ |activeTextColor| The color of the text when focused | '#F5F5F5'
233
+ |inactiveTextColor| The color of the text when not focused | '#757575'
234
+
235
+ ### Example:
236
+
237
+ askMeAnything = InputBox.new(
238
+ x: 830, y: 180, size: 30,
239
+ activeColor: 'purple',
240
+ displayName: "What would you like to ask?"
241
+ )
242
+
243
+ ### Methods:
244
+ ----
245
+ | Method | Description |
246
+ |--|--|
247
+ |.addKey(**key**) | Simulates a key input of given **key** |
248
+ | .updateDisplay() | Adds the line follower marker to the text box |
249
+ |.select() | Focuses the text box and lets you type in it |
250
+ |.deselect() | Loses focus of the text box and finalizes value|
251
+ |.toggle() | Toggles the selection value of the text box |
252
+ |.selected=(**bool**) | Selects or deselects the text box based on the **bool** |
253
+
254
+ ### Basic Usage:
255
+
256
+ if askMeAnything.value == "Favorite Color?"
257
+ puts "Purple"
258
+ end
259
+
260
+ # | ColorSlider:
261
+ These technically work but I'm not done with them so for now I wont bother with documentation.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -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__)
data/bin/setup ADDED
@@ -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
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,253 @@
1
+ module Savio
2
+ class Button
3
+ include IORenderable
4
+
5
+ attr_accessor :value, :enforceManager
6
+ attr_reader :selected, :buttonManager, :style, :length, :height, :type, :timeLastClicked, :cooldownTime
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
+ @baseColor = args[:baseColor] || Savio::Colors::White
19
+ @selectedColor = args[:selectedColor] || Savio::Colors::Blue
20
+ @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
21
+ @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::White
22
+
23
+ @cooldownTime = args[:cooldownTime] || 0.0
24
+ @timeLastClicked = 0.0
25
+
26
+ @selected = args[:selected] || false
27
+
28
+ @buttonManager = args[:buttonManager] || nil
29
+ @enforceManager = args[:enforceManager] || true
30
+
31
+ @type = args[:type] || 'toggle'
32
+ if @type != 'toggle' && @type != 'clicker'
33
+ @type = 'toggle'
34
+ end
35
+
36
+ @style = args[:style] || 'badge'
37
+ if @style != 'box' && @style != 'badge'
38
+ @style = 'badge'
39
+ end
40
+
41
+ if @style == 'box'
42
+ @size *= 2
43
+ @labelActiveColor = args[:labelActiveColor] || Savio::Colors::White
44
+ @labelInactiveColor = args[:labelInactiveColor] || Savio::Colors::Gray
45
+ end
46
+
47
+ @length = args[:length] || @size * 10
48
+ @height = args[:height] || @size * 2
49
+
50
+ @onClick = Proc.new {}
51
+
52
+ build()
53
+ end
54
+
55
+ def size=(size)
56
+ @length = size * 10
57
+ @height = size * 2
58
+ super(size)
59
+ end
60
+ def type=(newType)
61
+ if newType == 'toggle' || newType == 'clicker'
62
+ @type = newType
63
+ end
64
+ end
65
+ def style=(style)
66
+ if style == 'box' || style == 'badge'
67
+ @style = style
68
+ rebuild()
69
+ end
70
+ end
71
+
72
+ def cooldownTime=(cooldown)
73
+ @cooldownTime = cooldown.to_f
74
+ end
75
+
76
+ def baseColor=(c)
77
+ @baseColor = c
78
+ rebuild()
79
+ end
80
+ def selectedColor=(c)
81
+ @selectedColor = c
82
+ rebuild()
83
+ end
84
+ def labelColor=(c)
85
+ @labelColor = c
86
+ rebuild()
87
+ end
88
+
89
+ def selected=(bool)
90
+ if bool == true
91
+ select()
92
+ elsif bool == false
93
+ deselect()
94
+ end
95
+ end
96
+
97
+ def onClick(&proc)
98
+ @onClick = proc
99
+ end
100
+
101
+ def buttonManager=(newManager)
102
+ if @buttonManager != nil
103
+ if newManager.class.name != 'Savio::ButtonManager'
104
+ raise ArgumentError, 'Given object ' + newManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
105
+ end
106
+ @buttonManager.removeButton(self, false)
107
+ end
108
+
109
+ if newManager != nil
110
+ @buttonManager = newManager
111
+ if @buttonManager.buttons.include?(self) != true
112
+ @buttonManager.addButton(self)
113
+ end
114
+ else
115
+ @buttonManager = nil
116
+ end
117
+ end
118
+
119
+ def select(enforce = @enforceManager)
120
+ if Time.now.to_f - @timeLastClicked.to_f >= @cooldownTime.to_f
121
+ @timeLastClicked = Time.now.to_f
122
+ click()
123
+ if enforce == true && @buttonManager != nil
124
+ @buttonManager.select(self)
125
+ else
126
+ @selectCircle.add
127
+ @nameLabel.color = @labelActiveColor
128
+ @selected = true
129
+ if @type == 'clicker'
130
+ fade = Thread.new {
131
+ @selectCircle.add
132
+ @nameLabel.color = @labelActiveColor
133
+ sleep(0.06)
134
+ @selectCircle.remove
135
+ @nameLabel.color = @labelInactiveColor
136
+ }
137
+ deselect(enforce)
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ def deselect(enforce = @enforceManager)
144
+ if enforce == true && @buttonManager != nil
145
+ @buttonManager.deselect(self)
146
+ else
147
+ @selectCircle.remove
148
+ @nameLabel.color = @labelInactiveColor
149
+ @selected = false
150
+ end
151
+ end
152
+
153
+ def toggle(enforce = @enforceManager)
154
+ if @selected
155
+ deselect(enforce)
156
+ else
157
+ select(enforce)
158
+ end
159
+ end
160
+
161
+ def remove()
162
+ super()
163
+ @nameLabel.remove
164
+ @baseCircle.remove
165
+ @selectCircle.remove
166
+ end
167
+
168
+ def add()
169
+ super()
170
+ @nameLabel.add
171
+ @baseCircle.add
172
+ @selectCircle.add
173
+ if @selected
174
+ select()
175
+ else
176
+ deselect()
177
+ end
178
+ end
179
+
180
+ def click()
181
+ @onClick.call()
182
+ end
183
+
184
+ def build()
185
+ @shown = true
186
+ case @style
187
+ when 'badge'
188
+ @baseCircle = Circle.new(
189
+ x: @x, y: @y,
190
+ radius: @size,
191
+ color: @baseColor,
192
+ z: @z
193
+ )
194
+ @selectCircle = Circle.new(
195
+ x: @x, y: @y,
196
+ radius: @size * 0.8,
197
+ color: @selectedColor,
198
+ z: @z+1
199
+ )
200
+ @nameLabel = Text.new(
201
+ @displayName.to_s,
202
+ x: @x + @size * 2, y: @y - @size * 1.5,
203
+ size: @size * 2,
204
+ color: @labelInactiveColor,
205
+ z: @z
206
+ )
207
+ @nameLabel.y = @baseCircle.y - @baseCircle.radius / 4 - @nameLabel.height / 2
208
+ when 'box'
209
+ @baseCircle = Rectangle.new(
210
+ x: @x, y: @y,
211
+ height: @height, width: @length,
212
+ color: @baseColor,
213
+ z: @z
214
+ )
215
+ @selectCircle = Rectangle.new(
216
+ x: @x + (@height * 0.1), y: @y + (@height * 0.1),
217
+ height: @height - (@height * 0.2), width: @length - (@height * 0.2),
218
+ color: @selectedColor,
219
+ z: @z+1
220
+ )
221
+ @nameLabel = Text.new(
222
+ @displayName.to_s,
223
+ x: @x, y: @y,
224
+ size: @size,
225
+ color: @labelInactiveColor,
226
+ z: @z+2
227
+ )
228
+ @nameLabel.x = @baseCircle.x + @baseCircle.width / 2 - @nameLabel.width / 2
229
+ @nameLabel.y = @baseCircle.y + @baseCircle.height / 2 - @nameLabel.height / 2
230
+ end
231
+
232
+ if @buttonManager == nil
233
+ if @selected
234
+ select()
235
+ else
236
+ deselect()
237
+ end
238
+ else
239
+ if @buttonManager.class.name != 'Savio::ButtonManager'
240
+ raise ArgumentError, 'Given object ' + @buttonManager.to_s + ' is not a ButtonManager. Must be of type ButtonManager'
241
+ end
242
+ @buttonManager.addButton(self)
243
+
244
+ if @selected
245
+ @buttonManager.select(self)
246
+ else
247
+ @buttonManager.deselect(self)
248
+ end
249
+ end
250
+
251
+ end
252
+ end
253
+ end