1 /** 2 Copyright: Copyright (c) 2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.application.application; 8 9 import std.stdio : writeln; 10 import anchovy.core.interfaces.iwindow; 11 import anchovy.gui; 12 13 public import anchovy.gui.application.eventaggregator; 14 public import anchovy.gui.application.fpshelper; 15 16 class Application(WindowType) 17 { 18 IWindow window; 19 EventAggregator!WindowType aggregator; 20 FpsHelper fpsHelper; 21 TimerManager timerManager; 22 bool isRunning = true; 23 Color clearColor = Color(50, 10, 45); 24 25 IRenderer renderer; 26 IGuiRenderer guiRenderer; 27 28 GuiContext context; 29 TemplateManager templateManager; 30 31 this(uvec2 windowSize, string caption) 32 { 33 window = new WindowType(); 34 35 aggregator = new EventAggregator!WindowType(this, window); 36 37 window.init(windowSize, caption); 38 } 39 40 void run(in string[] args) 41 { 42 init(args); 43 load(args); 44 45 double lastTime = window.elapsedTime; 46 double newTime; 47 48 while(isRunning) 49 { 50 window.processEvents(); 51 52 newTime = window.elapsedTime; 53 54 update(newTime - lastTime); 55 56 lastTime = newTime; 57 58 draw(); 59 60 fpsHelper.sleepAfterFrame(lastTime - window.elapsedTime); 61 } 62 63 unload(); 64 65 window.releaseWindow; 66 } 67 68 string[] getHardwareInfo() 69 { 70 import core.cpuid; 71 import anchovy.utils.string : ZToString; 72 73 return [ 74 "CPU vendor: " ~ vendor, 75 "CPU name: " ~ processor, 76 "Cores: " ~ to!string(coresPerCPU), 77 "Threads: " ~ to!string(threadsPerCPU), 78 "CPU chache levels: " ~ to!string(cacheLevels), 79 "GPU vendor: " ~ ZToString(glGetString(GL_VENDOR)), 80 "Renderer: " ~ ZToString(glGetString(GL_RENDERER)), 81 "OpenGL version: " ~ ZToString(glGetString(GL_VERSION)), 82 "GLSL version: " ~ ZToString(glGetString(GL_SHADING_LANGUAGE_VERSION)), 83 ]; 84 } 85 86 void init(in string[] args) 87 { 88 89 dstring cyrillicChars = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяє"d; 90 91 // ----------------------------- Setting renderer ----------------------------- 92 renderer = new Ogl3Renderer(window); 93 renderer.setClearColor(clearColor); 94 95 // ----------------------------- Skin loading --------------------------------- 96 string graySkinSource = cast(string)read("skingray.json"); 97 auto skinParser = new JsonGuiSkinParser; 98 auto graySkin = skinParser.parse(graySkinSource); 99 100 // ----------------------------- Gui renderer --------------------------------- 101 guiRenderer = new SkinnedGuiRenderer(renderer, graySkin); 102 guiRenderer.fontManager.charCache ~= cyrillicChars; 103 graySkin.loadResources(guiRenderer); 104 105 timerManager = new TimerManager(delegate double(){return window.elapsedTime;}); 106 107 // ----------------------------- Template classes ----------------------------- 108 auto templateParser = new TemplateParser; 109 templateManager = new TemplateManager(templateParser); 110 111 // ----------------------------- Setting context ------------------------------ 112 context = new GuiContext(guiRenderer, timerManager, templateManager, graySkin); 113 context.setClipboardStringCallback = (dstring newStr) => window.clipboardString = to!string(newStr); 114 context.getClipboardStringCallback = delegate dstring(){return to!dstring(window.clipboardString);}; 115 context.attachDefaultBehaviors(); 116 context.attachDefaultLayouts(); 117 118 // ----------------------------- Rendering settings --------------------------- 119 renderer.enableAlphaBlending(); 120 glEnable(GL_SCISSOR_TEST); 121 } 122 123 void load(in string[] args) 124 { 125 126 } 127 128 void unload() 129 { 130 131 } 132 133 void update(double dt) 134 { 135 fpsHelper.update(dt); 136 timerManager.updateTimers(window.elapsedTime); 137 context.update(dt); 138 } 139 140 void draw() 141 { 142 guiRenderer.setClientArea(Rect(0, 0, window.size.x, window.size.y)); 143 glClear(GL_COLOR_BUFFER_BIT); 144 145 context.eventDispatcher.draw(); 146 147 window.swapBuffers(); 148 } 149 150 void closePressed() 151 { 152 } 153 154 void printTree() 155 { 156 void printWidget(Widget widget, string spacing) 157 { 158 auto children = widget["children"].get!(Widget[]); 159 writefln("-%s %s", widget["type"], widget["name"]); 160 161 if (children.length > 0) 162 { 163 foreach(child; children[0..$-1]) 164 { 165 writef("%s ├", spacing); 166 printWidget(child, spacing~" |"); 167 } 168 writef("%s └", spacing); 169 printWidget(children[$-1], spacing~" "); 170 } 171 } 172 173 foreach(root; context.roots) 174 { 175 printWidget(root, ""); 176 } 177 writeln; 178 } 179 }