1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

 define.class(function($ui$, view, label){
        // Splitcontainer adds dragbars between nodes to make all the nodes resizable.

        // should the splitter bars be introduced horizontally or vertically?
        this.attributes = {
                // wether the splitcontainer is vertical or not
                direction: Config({type: Enum("horizontal", "vertical"), value:"vertical"}),
                // set the width (or height) of the splitter bar
                splitsize: Config({type: float, value: 8}),
                // the minimum size of a child controlled by the splitter
                minimalchildsize: Config({type: float, value: 20}),
                // the color of the splitter bar
                splittercolor: Config({type: vec4, value: vec4("#484848")}),
                // color of splitter bar on hover
                hovercolor: Config({type: vec4, value: vec4("#707070")}),
                // color of the splitter bar when dragging it
                activecolor: Config({type: vec4, value: vec4("#7070a0")})
        }

        this.bgcolor = NaN
        this.flex = 1.0
        this.flexdirection = "row";
        this.position = "relative"
        this.borderwidth = 0
        this.bordercolor = vec4("#303060")

        this.direction = function(){
                this.flexdirection = this.direction=="horizontal"?"column":"row" ;

                }

        // the visual class that defines the draggable bar between the resizable children
        define.class(this, 'splitter', function(view){

                this.attributes = {
                        firstnode: Config({type: int, value: 0})
                }

                this.bgcolor = vec4("#737373");
                this.alignitem = "stretch";
                this.borderradius = 3;
                this.attributes = {
                        vertical: Config({type: Boolean, value: false}),
                        splitsize: Config({type: float, value: 6}),
                        splittercolor: Config({type: vec4, value: vec4("#404050")}),
                        hovercolor: Config({type: vec4, value: vec4("#f050a0")}),
                        bgcolor: Config({duration:0.1, motion:"linear"}),
                        activecolor: Config({type: vec4, value: vec4("#7070a0")})
                }
                this.flex = 0
                this.neutralcolor = this.bgcolor;

                this.pointerover  = function(){
                        this.bgcolor = this.hovercolor;
                }
                this.pointerout  = function(){
                        this.bgcolor = this.neutralcolor;
                }

                this.pointerstart = function(){
                        this._flexstart = {
                                left: this.parent.children[this.firstnode].flex,
                                right: this.parent.children[this.firstnode+2].flex,

                                leftwidth: this.parent.children[this.firstnode].layout.width,
                                leftheight: this.parent.children[this.firstnode].layout.height,

                                rightwidth: this.parent.children[this.firstnode+2].layout.width,
                                rightheight: this.parent.children[this.firstnode+2].layout.height
                        }
                }

                this.pointermove = function(event){
                        var leftnode = this.parent.children[this.firstnode]
                        var rightnode = this.parent.children[this.firstnode + 2]

                        var f1 = this._flexstart.left
                        var f2 = this._flexstart.right

                        var totf = f1 + f2

                        if (this.vertical){
                                var h1 = this._flexstart.leftheight
                                var h2 = this._flexstart.rightheight

                                var hadd = h1 + h2
                                h1 += event.delta[1]
                                h2 -= event.delta[1]
                                if (h1 < this.parent.minimalchildsize || h2 < this.parent.minimalchildsize) return
                                var f1n = h1 / (hadd)
                                var f2n = h2 / (hadd)
                                leftnode.flex = f1n * totf
                                rightnode.flex = f2n* totf
                        } else {
                                var w1 = this._flexstart.leftwidth
                                var w2 = this._flexstart.rightwidth

                                var wadd = w1 + w2
                                w1 += event.delta[0]
                                w2 -= event.delta[0]
                                if (w1 < this.parent.minimalchildsize || w2 < this.parent.minimalchildsize) return
                                var f1n = w1 / (wadd)
                                var f2n = w2 / (wadd)
                                leftnode.flex = f1n * totf
                                rightnode.flex = f2n * totf
                        }
                        this.parent.redraw()
                        // lets trigger layout dirty...
                        /*
                        leftnode.setDirty(true);
                        leftnode.reLayout();
                        rightnode.reLayout();
                        rightnode.setDirty(true);
                        */
                }
                /*
                this.atDraw = function(){
                        if (this.hovered > 0){
                                if (this.pressed > 0){
                                        this.bg_shader.color1 = this.activecolor;
                                }else{
                                        this.bg_shader.color1 = this.hovercolor;
                                }
                        }else{
                                this.bg_shader.color1 = this.splittercolor;
                        }
                }
                */
                this.hardrect = {
                        acolor: function(){

                                if (view.vertical )
                                {
                                        var yy = mesh.y*view.layout.height;
                                        var ymix = yy<1.0? 1.: ((yy>view.layout.height-1.0)?1.:0.0)
                                        return mix(view.bgcolor, "black", ymix);
                                }
                                var xx = mesh.x*view.layout.width;
                                var xmix = xx<1.0? 1.: ((xx>view.layout.width-1.0)?1.:0.0)
                                return mix(view.bgcolor, "black",xmix);
                        }
                }
                this.vertical = function(){
                        if (this.vertical){
                                this.height = this.splitsize
                                this.width = NaN
                        }
                        else{
                                this.width = this.splitsize
                                this.height = NaN
                        }
                }
        })

        this.render = function(){
                if (this.constructor_children.length > 1){
                        var children = []
                        children.push(view({bgcolor:NaN,clipping: true, flex: this.constructor_children[0].flex},this.constructor_children[0]));

                        for (var i = 1; i < this.constructor_children.length; i++){

                                children.push( this.splitter({
                                        vertical: this.direction=="horizontal",
                                        cursor: this.direction=="horizontal"?'ns-resize':'ew-resize',
                                        firstnode: (i-1)*2,
                                        splitsize: this.splitsize,
                                        splittercolor: this.splittercolor,
                                        hovercolor: this.hovercolor,
                                        activecolor: this.activecolor
                                }))

                                children.push(view({
                                        bgcolor:NaN,
                                        clipping: true,
                                        flex: this.constructor_children[i].flex
                                },this.constructor_children[i]))

                        }
                        return children
                }
                else {
                        return this.constructor_children;
                }
        }

        var splitcontainer = this.constructor

        // Basic usage of the splitcontainer
        this.constructor.examples = {
                Usage:function(){ return [
                        splitcontainer({alignself:'stretch', vertical: false, margin: 4, flex: 1.0, borderwidth:2, bordercolor: "darkblue", padding: vec4(2) },
                        label({flex: 0.2, fontsize: 26, text:"A", bgcolor: "transparent" ,multiline: true, align:"center" , fgcolor:"black", margin: 2})
                        ,label({flex: 0.2, fontsize: 26, text:"B", bgcolor: "transparent" ,multiline: true, align:"center" ,fgcolor:"black", margin: 2})
                        ,label({flex: 0.2, fontsize: 26, text:"C", bgcolor: "transparent" ,multiline: true, align:"center" , fgcolor:"black",margin: 2})
                        ,label({flex: 0.2, fontsize: 26, text:"D", bgcolor: "transparent" ,multiline: true, align:"center" , fgcolor:"black",margin: 2})
                )]}
        }

})