summaryrefslogtreecommitdiff
path: root/mouse-component.js
blob: 10684cc53b3bb2d28bae0ec5672701ca69eabb54 (plain)
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
drawMessageComponent = {
    mouseDown: function(e){
        mouseIsDown = true
        let x = e.x/2;
        let y = e.y/2;
    
        if(206 < x && x < WIDTH && 292 < y && y < 336){
            messages.unshift({
                type: 2,
                from: username,
                text: currentMessageText,
                pixels: currentMessagePixels
            })
            currentMessageText = ""
            currentMessagePixels = []
        } else if(206 < x && x < WIDTH && 338 < y && y < 382){
            currentMessageText = ""
            currentMessagePixels = []
        } else if(0 < x && x < WIDTH && 198 < y && y < 288){
            currentMessagePixels.push({x: x, y: y - 288})
        }
        checkKeyBoardClick(x, y)
    },
    mouseUp: function(e){
        mouseIsDown = false
    },
    mouseMove: function(e){
        let x = Math.floor(e.x/2);
        let y = Math.floor(e.y/2);
        if(mouseIsDown && 0 < x && x < WIDTH && 198 < y && y < 288){
            currentMessagePixels.push({x: x, y: y - 288})
        }
    },
}
function inBox(x, y, boxX, boxY, width, height){
    return boxX < x && x < boxX + width && boxY < y && y < boxY + height
}
var normalKeys = [
    ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="],
    ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"],
    ["a", "s", "d", "f", "g", "h", "j", "k", "l"],
    ["z", "x", "c", "v", "b", "n", "m", ",", ".", "/"]
]
var keys = normalKeys
function checkKeyBoardClick(mouseX, mouseY){   
    var d1 = Math.floor( (WIDTH-54) / 12) - 1
    var d2 = Math.ceil( (WIDTH-54) / 12) - 1
    for(var j = 0; j < keys.length; j++){
        var x = 6 + 8*j
        var y = HEIGHT / 4 * 3 + 5 + (d1+1)*j
        for(var i = 0; i < keys[j].length; i++){
            if(inBox(mouseX, mouseY, x, y, d1, d1)){
                var char = keys[j][i]
                if(capsOn || shiftOn){
                    char = char.toUpperCase()
                }
                shiftOn = false
                keyDown({
                    keyCode: 32, // any printable char code (yeah probably a bad idea)
                    key: char // the char
                })
            }
            x += d2
        }
    }
    // space
    if(inBox(mouseX, mouseY, 6, HEIGHT - 27, WIDTH-65, 24)){
        keyDown({
            keyCode: 32,
            key: " " 
        })
    }
    // shift
    if(inBox(mouseX, mouseY, 6, HEIGHT - 27 - d2, d1+8, d1)){
        shiftOn = true && !shiftOn
    }
    // caps
    if(inBox(mouseX, mouseY, 6, HEIGHT - 27 - 2 * d2, d1, d1)){
        capsOn = !capsOn
    }
    // enter
    if(inBox(mouseX, mouseY, d1*11+1, HEIGHT - 27 - 2 * d2, d1*2+1, d1)){
        keyDown({
            keyCode: 13
        })
    }
    // backspace
    if(inBox(mouseX, mouseY, d1*11.5+1.5, HEIGHT - 27 - 3 * d2, d1+8, d1)){
        keyDown({
            keyCode: 8
        })
    }
    
}