1 /**
2  * ae.demo.test.main
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.demo.ui.main;
15 
16 import std.conv;
17 
18 import ae.ui.app.application;
19 import ae.ui.app.posix.main;
20 import ae.ui.shell.shell;
21 import ae.ui.shell.sdl2.shell;
22 import ae.ui.video.renderer;
23 import ae.ui.video.sdl2.video;
24 import ae.ui.video.video;
25 import ae.ui.wm.application;
26 import ae.ui.wm.controls.control;
27 
28 final class MyApplication : WMApplication
29 {
30 	override string getName() { return "Demo/UI"; }
31 	override string getCompanyName() { return "CyberShadow"; }
32 
33 	override int run(string[] args)
34 	{
35 		shell = new SDL2Shell(this);
36 		shell.video = new SDL2SoftwareVideo();
37 		root.addChild(createView());
38 		shell.run();
39 		shell.video.shutdown();
40 		return 0;
41 	}
42 
43 	Control createView()
44 	{
45 		return (new Table(2, 2))
46 			.addChild((new Pad(10.px, 5.percent)).addChild(new SetBGColor!(PaintControl, 0x002222)))
47 			.addChild((new Pad(10.px, 5.percent)).addChild(new SetBGColor!(PaintControl, 0x220022)))
48 			.addChild((new Pad(10.px, 5.percent)).addChild(new SetBGColor!(PaintControl, 0x222200)))
49 			.addChild((new Pad(10.px, 5.percent)).addChild(new SetBGColor!(PaintControl, 0x002200)))
50 		;
51 	}
52 }
53 
54 shared static this()
55 {
56 	createApplication!MyApplication();
57 }
58 
59 // ***************************************************************************
60 
61 import ae.utils.meta;
62 
63 /// Subclass any control to give it an arbitrary background color.
64 class SetBGColor(BASE : Control, uint C) : BASE
65 {
66 	mixin GenerateConstructorProxies;
67 
68 	enum BGRX color = BGRX(C&0xFF, (C>>8)&0xFF, C>>16);
69 
70 	override void render(Renderer s, int x, int y)
71 	{
72 		s.fillRect(x, y, x+w, y+h, color);
73 		super.render(s, x, y);
74 	}
75 }
76 
77 // ***************************************************************************
78 
79 class PaintControl : Control
80 {
81 	override void arrange(int rw, int rh) { w = rw; h = rh; }
82 
83 	struct Coord { uint x, y; BGRX c; void* dummy; }
84 	Coord[] coords;
85 
86 	override void handleMouseMove(int x, int y, MouseButtons buttons)
87 	{
88 		if (buttons)
89 		{
90 			uint b = cast(uint)buttons;
91 			ubyte channel(ubyte m) { return ((b>>m)&1) ? 0xFF : 0; }
92 			coords ~= Coord(x, y, BGRX(channel(2), channel(1), channel(0)));
93 		}
94 	}
95 
96 	override void render(Renderer s, int x, int y)
97 	{
98 		//foreach (i; 0..100)
99 		//	coords ~= Coord(uniform(0, w), uniform(0, h), uniform(0, 0x1_00_00_00));
100 		static size_t oldCoordsLength;
101 		if (coords.length != oldCoordsLength)
102 		{
103 			//shell.setCaption(to!string(coords.length));
104 			oldCoordsLength = coords.length;
105 		}
106 
107 		// if (coords.length > 100) throw new Exception("derp");
108 
109 		auto b = s.lock();
110 		scope(exit) s.unlock();
111 		foreach (coord; coords)
112 			if (coord.x < w && coord.y < h)
113 				b[x+coord.x, y+coord.y] = coord.c;
114 		//s.unlock();
115 	}
116 }