1 /** 2 Color picker 3 4 Copyright: (c) Enalye 2017 5 License: Zlib 6 Authors: Enalye 7 */ 8 9 module atelier.ui.colorpicker; 10 11 import atelier.core; 12 import atelier.render; 13 import atelier.common; 14 15 import atelier.ui; 16 17 /+ 18 class ColorViewer: GuiElement { 19 Color color; 20 21 this() {} 22 23 override void onEvent(Event event) {} 24 override void update(float deltaTime) {} 25 override void draw() { 26 drawFilledRect(center - size / 2f, size, color); 27 } 28 } 29 30 class ColorPicker: GuiElement { 31 private { 32 DropDownList _blendList; 33 Slider _redSlider, _blueSlider, _greenSlider, _alphaSlider; 34 ColorViewer _viewer; 35 } 36 37 @property { 38 Color color() const { 39 return Color(_redSlider.value01, _blueSlider.value01, 40 _greenSlider.value01, _alphaSlider.value01); 41 } 42 43 Blend blend() const { 44 switch(_blendList.selected) { 45 case 0: 46 return Blend.AlphaBlending; 47 case 1: 48 return Blend.AdditiveBlending; 49 case 2: 50 return Blend.ModularBlending; 51 case 3: 52 return Blend.NoBlending; 53 default: 54 throw new Exception("Invalid kind of blending"); 55 } 56 } 57 } 58 59 this(Color newColor = Color.white, Blend newBlend = Blend.AlphaBlending) { 60 Slider makeSlider(VContainer container, string title) { 61 auto slider = new HSlider; 62 slider.min = 0; 63 slider.min = 255; 64 slider.step = 255; 65 slider.size = Vec2f(255f, 10f); 66 67 auto hc = new HContainer; 68 hc.spacing = Vec2f(10f, 0f); 69 hc.addChildGui(new Label(title)); 70 hc.addChildGui(slider); 71 container.addChildGui(hc); 72 return slider; 73 } 74 75 auto container = new VContainer; 76 container.spacing = Vec2f(0f, 10f); 77 _redSlider = makeSlider(container, "R"); 78 _blueSlider = makeSlider(container, "G"); 79 _greenSlider = makeSlider(container, "B"); 80 _alphaSlider = makeSlider(container, "A"); 81 82 _blendList = new DropDownList(Vec2f(200f, 25f), 4U); 83 foreach(mode; [ 84 "Alpha Blending", "Additive Blending", 85 "Modular Blending", "No Blending"]) 86 _blendList.add(mode); 87 container.addChildGui(_blendList); 88 89 _redSlider.value01 = newColor.r; 90 _blueSlider.value01 = newColor.g; 91 _greenSlider.value01 = newColor.b; 92 _alphaSlider.value01 = newColor.a; 93 94 _viewer = new ColorViewer; 95 _viewer.size = Vec2f(100f, 100f); 96 97 auto hc2 = new HContainer; 98 hc2.spacing = Vec2f(20f, 5f); 99 hc2.addChildGui(container); 100 hc2.addChildGui(_viewer); 101 102 super("Couleur", hc2.size); 103 layout.addChildGui(hc2); 104 105 switch(newBlend) with(Blend) { 106 case AlphaBlending: 107 _blendList.selected = 0U; 108 break; 109 case AdditiveBlending: 110 _blendList.selected = 1U; 111 break; 112 case ModularBlending: 113 _blendList.selected = 2U; 114 break; 115 case NoBlending: 116 _blendList.selected = 3U; 117 break; 118 default: 119 _blendList.selected = 0U; 120 } 121 } 122 123 override void update(float deltaTime) { 124 super.update(deltaTime); 125 _viewer.color = color(); 126 } 127 }+/