1 /** 2 Grimoire 3 Copyright (c) 2017 Enalye 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages arising 7 from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute 11 it freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; 14 you must not claim that you wrote the original software. 15 If you use this software in a product, an acknowledgment 16 in the product documentation would be appreciated but 17 is not required. 18 19 2. Altered source versions must be plainly marked as such, 20 and must not be misrepresented as being the original software. 21 22 3. This notice may not be removed or altered from any source distribution. 23 */ 24 25 module ui.list.loglist; 26 27 import std.conv: to; 28 29 import core.all; 30 import render.all; 31 import common.all; 32 33 import ui.widget; 34 import ui.layout; 35 import ui.slider; 36 37 private class LogContainer: WidgetGroup { 38 public { 39 View view; 40 LogLayout layout; 41 } 42 43 this(Vec2f size) { 44 createGui(size); 45 } 46 47 override void onEvent(Event event) { 48 pushView(view, false); 49 super.onEvent(event); 50 popView(); 51 } 52 53 override void update(float deltaTime) { 54 pushView(view, false); 55 super.update(deltaTime); 56 popView(); 57 } 58 59 override void draw() { 60 view.setColorMod(Color.white, Blend.AlphaBlending); 61 pushView(view, true); 62 layout.draw(); 63 popView(); 64 view.draw(_position); 65 } 66 67 protected void createGui(Vec2f newSize) { 68 _isFrame = true; 69 isLocked = true; 70 layout = new LogLayout; 71 view = new View(to!Vec2u(newSize)); 72 view.position = Vec2f.zero; 73 size(newSize); 74 addChild(layout); 75 } 76 } 77 78 class LogList: WidgetGroup { 79 protected { 80 LogContainer _container; 81 Slider _slider; 82 Vec2f _lastMousePos = Vec2f.zero; 83 uint _idElementSelected = 0u; 84 } 85 86 @property { 87 uint selected() const { return _idElementSelected; } 88 } 89 90 this(Vec2f newSize) { 91 isLocked = true; 92 _slider = new VScrollbar; 93 _slider.value01 = 1f; 94 _container = new LogContainer(newSize); 95 96 super.addChild(_slider); 97 super.addChild(_container); 98 99 size(newSize); 100 position(Vec2f.zero); 101 } 102 103 override void onEvent(Event event) { 104 super.onEvent(event); 105 if(event.type == EventType.MouseDown || event.type == EventType.MouseUp || event.type == EventType.MouseUpdate) { 106 _lastMousePos = event.position; 107 if(_slider.isInside(event.position)) 108 _slider.onEvent(event); 109 else if(event.type == EventType.MouseDown) { 110 auto widgets = _container.layout.children; 111 foreach(uint id, const Widget widget; widgets) { 112 if(widget.isHovered) 113 _idElementSelected = id; 114 } 115 } 116 } 117 if(!isOnInteractableWidget(_lastMousePos) && event.type == EventType.MouseWheel) 118 _slider.onEvent(event); 119 } 120 121 override void onPosition() { 122 _slider.position = _position - Vec2f((_size.x - _slider.size.x) / 2f, 0f); 123 _container.position = _position + Vec2f(_slider.size.x / 2f, 0f); 124 } 125 126 override void onSize() { 127 _slider.size = Vec2f(10f, _size.y); 128 _container.size = Vec2f(_size.x - _slider.size.x, _size.y); 129 _container.view.renderSize = _container.size.to!Vec2u; 130 onPosition(); 131 } 132 133 override void update(float deltaTime) { 134 _slider.update(deltaTime); 135 float min = _container.view.size.y / 2f; 136 float max = _container.layout.size.y - _container.view.size.y / 2f; 137 float exceedingHeight = _container.layout.size.y - _container.view.size.y; 138 139 if(exceedingHeight < 0f) { 140 _slider.max = 0; 141 _slider.step = 0; 142 } 143 else { 144 _slider.max = exceedingHeight / (_container.view.size.y / 50f); 145 _slider.step = to!uint(_slider.max); 146 } 147 _container.view.position = Vec2f(0f, lerp(min, max, _slider.offset)); 148 } 149 150 private void repositionContainer() { 151 _container.layout.position = Vec2f(5f + _container.layout.size.x / 2f - _container.size.x / 2f, _container.layout.size.y / 2f); 152 } 153 154 override void addChild(Widget widget) { 155 _container.layout.addChild(widget); 156 repositionContainer(); 157 } 158 159 override void removeChildren() { 160 _container.layout.removeChildren(); 161 repositionContainer(); 162 } 163 164 override void removeChild(uint id) { 165 _container.layout.removeChild(id); 166 repositionContainer(); 167 } 168 169 override int getChildrenCount() { 170 return _container.layout.getChildrenCount(); 171 } 172 }