PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/gui.as

https://github.com/joodie/bassline-experiment
ActionScript | 238 lines | 188 code | 32 blank | 18 comment | 19 complexity | 1786aaa693c30dbabfcf18f73dc1ee05 MD5 | raw file
  1. // Flash Synthesizer / Bassline Experiment
  2. // Copyright (C) 2009 Joost Diepenmaat - joost@zeekat.nl
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import flash.display.DisplayObject;
  18. import flash.display.Shape;
  19. import flash.display.Sprite;
  20. import flash.text.TextField;
  21. import flash.text.TextFormat;
  22. import flash.text.TextFieldAutoSize;
  23. import flash.display.SimpleButton;
  24. import flash.events.*
  25. var buttonTextFormat:TextFormat = new TextFormat()
  26. buttonTextFormat.font = "Verdana"
  27. buttonTextFormat.color = 0xffffff
  28. buttonTextFormat.size = 5
  29. buttonTextFormat.bold = true
  30. var titleTextFormat:TextFormat = new TextFormat()
  31. titleTextFormat.font = "Verdana"
  32. titleTextFormat.color = 0xffffff
  33. titleTextFormat.size = 25
  34. titleTextFormat.bold = true
  35. var knobs:Array = []
  36. function drawRoundedRectangle(graphics:flash.display.Graphics,color:uint,alpha:Number,x1:Number,y1:Number,x2:Number,y2:Number,rounded:Number = 2.0):void {
  37. with (graphics) {
  38. lineStyle(1,0)
  39. beginFill(color,alpha)
  40. moveTo(x1+rounded,y1)
  41. lineTo(x2-rounded,y1)
  42. curveTo(x2,y1,x2,y1+rounded)
  43. lineTo(x2,y2-rounded)
  44. curveTo(x2,y2,x2-rounded,y2)
  45. lineTo(x1+rounded,y2)
  46. curveTo(x1,y2,x1,y2-rounded)
  47. lineTo(x1,y1+rounded)
  48. curveTo(x1,y1,x1+rounded,y1)
  49. endFill()
  50. }
  51. }
  52. function drawLabel(sprite:Sprite, text:String, color:uint=0xffffff,bgcolor:uint=0xd0d0d0, bgalpha:Number=0.5):void {
  53. drawRoundedRectangle(sprite.graphics, bgcolor, bgalpha,0,0,20,20)
  54. var label:TextField = new TextField()
  55. label.selectable = false
  56. label.defaultTextFormat = buttonTextFormat
  57. label.width = 1
  58. label.autoSize = TextFieldAutoSize.LEFT
  59. label.text = text
  60. label.y = 10
  61. sprite.addChild(label)
  62. label.x = (20.0 - label.textWidth) / 2.0 - 1.7
  63. }
  64. class ToggleButton extends Sprite {
  65. public var onState:Shape
  66. public var offState:Shape
  67. public var onOff:Function
  68. public var onOn:Function
  69. private var state:Boolean
  70. public function ToggleButton(text:String,startState:Boolean,upcolor:uint,downcolor:uint,bgcolor:uint = 0xd0d0d0,bgalpha:Number=0.5) {
  71. buttonMode = true
  72. useHandCursor = true
  73. drawLabel(this, text, 0xffffff,bgcolor, bgalpha)
  74. onState = createShape(10,0, downcolor)
  75. offState = createShape(10,0, upcolor)
  76. onState.y = offState.y = 2
  77. onState.x = offState.x = 5
  78. state = startState
  79. addEventListener(MouseEvent.CLICK, function():void {
  80. if (onOn != null && onOff != null) {
  81. setValue(!state)
  82. }
  83. })
  84. updateState()
  85. addChild(onState)
  86. addChild(offState)
  87. knobs.push(this)
  88. }
  89. public function setValue(val:Boolean):void {
  90. state = val
  91. if (state && onOn != null) onOn()
  92. if (!state && onOff != null) onOff()
  93. updateState()
  94. }
  95. private function updateState():void {
  96. if (state) {
  97. onState.visible=true
  98. offState.visible=false
  99. }
  100. else {
  101. onState.visible=false
  102. offState.visible=true
  103. }
  104. }
  105. private function createShape(size:Number,edgeColor:uint,fillColor:uint):Shape {
  106. var sp:Shape = new Shape()
  107. with (sp.graphics) {
  108. beginFill(fillColor)
  109. drawCircle(size/2,size/2,size/2)
  110. endFill()
  111. lineStyle(size/10,edgeColor)
  112. drawCircle(size/2,size/2,size/2)
  113. lineStyle(undefined)
  114. beginFill(0,0.3)
  115. drawCircle(size/2,size/2,size/3)
  116. endFill()
  117. lineStyle(size/10,0xffffe0,0.3)
  118. moveTo(size*.80,size*.5)
  119. curveTo(size*.80,size*.80,size*.5,size*.80)
  120. }
  121. return sp
  122. }
  123. public function randomize():void {
  124. setValue(Math.random() < 0.5)
  125. }
  126. }
  127. class DialButton extends Sprite {
  128. public var shape:Shape = new Shape()
  129. public var onChange:Function
  130. private var value:Number
  131. private var min:Number
  132. private var max:Number
  133. private var color:uint
  134. public function DialButton(text:String,startValue:Number,min:Number,max:Number,color:uint=0xd0d0d0) {
  135. this.min = min
  136. this.max = max
  137. buttonMode = false
  138. value = startValue
  139. drawLabel(this, text)
  140. // shape is the dialing part of the graphics:
  141. // the value is indicated by rotating shape
  142. with (shape.graphics) {
  143. clear()
  144. rotation = 0
  145. lineStyle(1,0)
  146. beginFill(color)
  147. drawCircle(0,0,5)
  148. endFill()
  149. moveTo(0,-5)
  150. lineTo(0,-1)
  151. }
  152. shape.x = 10
  153. shape.y = 7
  154. updateDisplay()
  155. addChild(shape)
  156. var y:Number = 0.0
  157. function drag(event:MouseEvent):void {
  158. var delta:Number = ((event.stageY - y) * (max - min)) / 100
  159. setValue(Math.min(max,Math.max(min,value - delta)))
  160. }
  161. function upevent(event:MouseEvent):void {
  162. root.removeEventListener(MouseEvent.MOUSE_MOVE,drag)
  163. root.removeEventListener(MouseEvent.MOUSE_UP,upevent)
  164. }
  165. addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
  166. y = event.stageY
  167. root.addEventListener(MouseEvent.MOUSE_MOVE,drag)
  168. root.addEventListener(MouseEvent.MOUSE_UP, upevent)
  169. })
  170. knobs.push(this)
  171. }
  172. public function setValue(val:Number):void {
  173. value = val
  174. if (onChange != null) onChange(value)
  175. updateDisplay()
  176. }
  177. private function updateDisplay():void {
  178. shape.rotation = (value-min)* (320/(max - min)) - 160
  179. }
  180. public function randomize():void {
  181. setValue(Math.random() * (max - min) + min)
  182. }
  183. }
  184. class VUMeter extends Sprite {
  185. private var indicator:Shape = new Shape()
  186. public function VUMeter() {
  187. drawRoundedRectangle(graphics, 0, 0.5, 0,0,20,95)
  188. addChild(indicator)
  189. }
  190. public function setValue(val:Number):void {
  191. indicator.graphics.clear()
  192. var color:uint
  193. val = Math.min(1,Math.max(0,Math.abs(val)))
  194. if (val * 83 < 8) return;
  195. if (val < 0.5) {
  196. color = 0xa0a000
  197. }
  198. else if (val < 0.8) {
  199. color = 0x00f000
  200. }
  201. else {
  202. color = 0xff0000
  203. }
  204. drawRoundedRectangle(indicator.graphics, color, 1, 4,8 + (1 - val) * 83,16,91)
  205. }
  206. }