-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.html
More file actions
164 lines (154 loc) · 4.25 KB
/
editor.html
File metadata and controls
164 lines (154 loc) · 4.25 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TinyDrawer</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#grid {
display: grid;
grid-template-columns: repeat(64, 10px);
grid-template-rows: repeat(32, 10px);
gap: 1px;
background-color: #000;
border: 1px solid #000;
}
.cell {
width: 10px;
height: 10px;
background-color: #000;
}
#palette {
display: flex;
justify-content: center;
margin-top: 20px;
}
.color {
width: 30px;
height: 30px;
margin: 0 5px;
cursor: pointer;
border: 2px solid #000;
}
.color.selected {
border: 2px solid #fff;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}
#raw {
margin-top: 20px;
}
input {
width: 640px;
}
</style>
</head>
<body>
<h1>TinyDrawer Sprite Buffer Editor</h1>
<div id="grid"></div>
<div id="palette"></div>
<div id="raw">
<span>Hex String</span>
<input type="text" id="hexString" />
</div>
<script>
const grid = document.getElementById('grid')
const palette = document.getElementById('palette')
const colors = [
'#000000', '#242449', '#6d2449', '#009249',
'#b64924', '#6d6d49', '#b6b6b6', '#ffffff',
'#ff0048', '#ff9200', '#ffdb24', '#00ff24',
'#24b6ff', '#926d92', '#ff4992', '#ffdbb6'
]
let selectedColorIndex = 4
let isDrawing = false
let hexData = ''
let isProgrammaticChange = false
const hexString = document.getElementById('hexString')
function updateGrid(content) {
const cells = document.querySelectorAll('.cell')
for (let i = 0; i < content.length; i++) {
const colorIndex = parseInt(content[i], 16)
cells[i].style.backgroundColor = colors[colorIndex]
cells[i].dataset.colorIndex = colorIndex.toString()
}
}
// Detect change from user
hexString.addEventListener('input', (event) => {
if (!isProgrammaticChange || document.activeElement === hexString) {
let content = hexString.value.replaceAll("\n", "").replaceAll(" ", "")
if (content.length >= 32 * 64) {
updateGrid(content)
hexString.value = content
}
}
// Reset the flag after detecting user change
isProgrammaticChange = false
});
for (let i = 0; i < 16; i++) {
const c = document.createElement('div')
c.className = 'color'
c.style.backgroundColor = colors[i]
c.setAttribute('data-value', i.toString())
palette.appendChild(c)
}
// Create grid
for (let y = 0; y < 32; y++) {
let index = Math.floor(y / 8) % 2
for (let x = 0; x < 64; x++) {
const cell = document.createElement('div')
cell.className = 'cell'
if(x > 0 && x % 8 == 0) index = (index + 1) % 2
cell.dataset.colorIndex = index.toString(16)
grid.appendChild(cell)
hexData += cell.dataset.colorIndex
}
}
isProgrammaticChange = true
hexString.value = hexData
updateGrid(hexData)
// Color selection
palette.addEventListener('click', (e) => {
if (e.target.classList.contains('color')) {
document.querySelector('.color.selected')?.classList.remove('selected')
e.target.classList.add('selected')
selectedColorIndex = parseInt(e.target.dataset.value)
}
})
// Drawing functionality
grid.addEventListener('mousedown', startDrawing)
grid.addEventListener('mousemove', draw)
grid.addEventListener('mouseup', stopDrawing)
grid.addEventListener('mouseleave', stopDrawing)
function startDrawing(e) {
isDrawing = true
draw(e)
}
function draw(e) {
if (!isDrawing) return
if (e.target.classList.contains('cell')) {
e.target.style.backgroundColor = colors[selectedColorIndex]
e.target.dataset.colorIndex = selectedColorIndex
}
hexData = ''
document.querySelectorAll('.cell').forEach(cell => {
hexData += parseInt(cell.dataset.colorIndex).toString(16)
})
isProgrammaticChange = true;
hexString.value = hexData
}
function stopDrawing() {
isDrawing = false
}
</script>
</body>
</html>