summary refs log tree commit diff stats
path: root/doc/pydoc/ranger.ext.accumulator.html
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-01-18 00:15:48 +0100
committerhut <hut@lavabit.com>2010-01-18 00:17:10 +0100
commitc2f7c84c1aa524d6224e53888625c0e6fa153d6a (patch)
treea7bcf91d134680cf519323b0a44f177f62d6d7ee /doc/pydoc/ranger.ext.accumulator.html
parent1bcdf5d0239f7db122058f5ff1ce3b4f8448483e (diff)
downloadranger-c2f7c84c1aa524d6224e53888625c0e6fa153d6a.tar.gz
commands: fixed :e alias
it was mapped to :quit, now it is :edit
Diffstat (limited to 'doc/pydoc/ranger.ext.accumulator.html')
0 files changed, 0 insertions, 0 deletions
elioat <elioat@tilde.institute> 2024-01-15 07:02:23 -0500 *' href='/elioat/tour/commit/js/map-maker/map.js?id=e9bfdf4f4328ad0b100c0e02658a7dd2a0d4c6cc'>e9bfdf4 ^
79ec8ba ^
e9bfdf4 ^




79ec8ba ^
e9bfdf4 ^
79ec8ba ^







e9bfdf4 ^

79ec8ba ^







e9bfdf4 ^

79ec8ba ^

e9bfdf4 ^






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












                                                    
                       




























































                                                                                               











                                                       












                                











































                                                                                                

               
  
 
                                  




                                                                               
  
 







                                                                                               

  







                                                                  

  

                                                    






                          
const canvas = document.getElementById('map');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

const cellSize = 16; // Size of each cell in pixels
const gridWidth = 100; // Number of cells in width
const gridHeight = 100; // Number of cells in height

let grid = [];
let history = []; // undo stack
let offsetX = 0;
let offsetY = 0;
let selectedNumber = 0;

const initializeGrid = () => {
    grid = Array.from({ length: gridHeight }, () => Array(gridWidth).fill(0));
};

const drawGrid = () => {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    if (
        offsetX < 0 ||
        offsetX > gridWidth * cellSize - canvas.width ||
        offsetY < 0 ||
        offsetY > gridHeight * cellSize - canvas.height
    ) {
        ctx.fillStyle = '#91f7bd';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
    }

    grid.forEach((row, i) => {
        row.forEach((state, j) => {
            let color = '#000';
            switch (state) {
                case 0:
                    color = '#fff';
                    break;
                case 1:
                    color = '#000';
                    break;
                case 2:
                    color = '#f00';
                    break;
                case 3:
                    color = '#0f0';
                    break;
                case 4:
                    color = '#00f';
                    break;
                case 5:
                    color = '#ff0';
                    break;
                case 6:
                    color = '#f0f';
                    break;
                case 7:
                    color = '#0ff';
                    break;
                case 8:
                    color = '#f80';
                    break;
                case 9:
                    color = '#08f';
                    break;
            }
            ctx.fillStyle = color;
            ctx.fillRect(j * cellSize - offsetX, i * cellSize - offsetY, cellSize, cellSize);
            ctx.strokeStyle = '#ddd';
            ctx.strokeRect(j * cellSize - offsetX, i * cellSize - offsetY, cellSize, cellSize);
        });
    });
};

const saveGrid = () => {
    localStorage.setItem('grid', JSON.stringify(grid));
};

const loadGrid = () => {
    const savedGrid = localStorage.getItem('grid');
    if (savedGrid) {
        grid = JSON.parse(savedGrid);
    }
};

const handleKeyDown = (e) => {
    switch (e.key) {
        case 'ArrowUp':
            offsetY -= cellSize;
            break;
        case 'ArrowDown':
            offsetY += cellSize;
            break;
        case 'ArrowLeft':
            offsetX -= cellSize;
            break;
        case 'ArrowRight':
            offsetX += cellSize;
            break;
        case 'h':
            alert(`Controls:
                - Arrow keys to move the map
                - Click to place a tile
                - Number keys, 0 - 9, to select a tile type
                - Z to undo
                - C to clear the grid
                - E to export the grid as json
                - S to export the grid as a png
                - H to display this help text
            `);
            break;
        case 'z':
            if (history.length > 0) {
                const lastChange = history.pop(); // Pop last change from history
                grid[lastChange.y][lastChange.x] = lastChange.state; // Revert state
                drawGrid();
            }
            break;
        case 'c':
            if (window.confirm('Are you sure you want to clear the grid?')) {
                initializeGrid();
                drawGrid();
            }
            break;
        case 'e':
            const mapName = prompt('Please enter a name for the map:');
            if (mapName) {
                if (window.confirm(`Are you sure you want to export the grid as ${mapName}?`)) {
                    exportGridAsJson(mapName);
                }
            }
            break;
        case 's':
            const imgName = prompt('Please enter a name for the image:');
            if (imgName) {
                exportGridAsPng(imgName);
            }
            break;
        default:
            if (e.key >= '0' && e.key <= '9') {
                selectedNumber = parseInt(e.key);
            }
            break;
    }
    drawGrid();
};

const handleCanvasClick = (e) => {
    const x = Math.floor((e.clientX + offsetX) / cellSize);
    const y = Math.floor((e.clientY + offsetY) / cellSize);
    history.push({ x, y, state: grid[y][x] }); // Push current state to history
    grid[y][x] = selectedNumber; // Set state to selectedNumber
    drawGrid();
};

const exportGridAsJson = (mapName) => {
    const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(grid));
    const downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href", dataStr);
    downloadAnchorNode.setAttribute("download", `${mapName}.json`);
    document.body.appendChild(downloadAnchorNode); // evidently this is required for firefox
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
};

const exportGridAsPng = (imgName) => {
    const dataUrl = canvas.toDataURL('image/png');
    const downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href", dataUrl);
    downloadAnchorNode.setAttribute("download", `${imgName}.png`);
    document.body.appendChild(downloadAnchorNode);
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
};

window.addEventListener('keydown', handleKeyDown);
canvas.addEventListener('click', handleCanvasClick);
window.onload = () => {
    initializeGrid();
    loadGrid();
    drawGrid();
};

window.onunload = saveGrid;