forked from frisk-V3/Pixl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixelEngine.hx
More file actions
275 lines (235 loc) · 7.93 KB
/
PixelEngine.hx
File metadata and controls
275 lines (235 loc) · 7.93 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package;
import kha.Canvas;
import kha.Color;
import kha.Image;
import kha.graphics2.Graphics;
import kha.graphics2.ImageScaleQuality;
import kha.arrays.Uint32Array;
/**
* [コア] ピクセルデータの最小管理単位
* 高速アクセスとGPU同期を両立
*/
class PixelBuffer {
public var image(default, null):Image;
public var data(default, null):Uint32Array;
public var width:Int;
public var height:Int;
private var isDirty:Bool = true;
public function new(w:Int, h:Int) {
width = w; height = h;
image = Image.createRenderTarget(w, h);
data = new Uint32Array(w * h);
clear(0x00000000);
}
public inline function get(x:Int, y:Int):UInt {
return data[y * width + x];
}
public inline function set(x:Int, y:Int, c:UInt):Void {
var idx = y * width + x;
if (data[idx] != c) {
data[idx] = c;
isDirty = true;
}
}
public function clear(c:UInt):Void {
for (i in 0...data.length) data[i] = c;
isDirty = true;
}
public function sync():Void {
if (!isDirty) return;
var bytes = image.lock();
for (i in 0...data.length) bytes.setInt32(i * 4, data[i]);
image.unlock();
isDirty = false;
}
public function destroy() { image.unload(); }
}
/**
* [履歴] 差分保存によるUndoシステム
*/
class PixelHistory {
public var layerIdx:Int;
public var frameIdx:Int;
public var diff:Map<Int, {oldC:UInt, newC:UInt}> = [];
public function new(l:Int, f:Int) { layerIdx = l; frameIdx = f; }
}
/**
* [構造] レイヤーとフレームの管理
*/
class Layer {
public var name:String;
public var buffer:PixelBuffer;
public var visible:Bool = true;
public var opacity:Float = 1.0;
public function new(w, h, n) { name = n; buffer = new PixelBuffer(w, h); }
}
class Frame {
public var layers:Array<Layer> = [];
public function new(w, h) { layers.push(new Layer(w, h, "Layer 0")); }
}
/**
* [メイン] PixelMasterEngine
* 全機能を統合した司令塔
*/
class PixelEngine {
// キャンバス設定
public var width(default, null):Int;
public var height(default, null):Int;
public var frames:Array<Frame> = [];
// 状態
public var currentFrame:Int = 0;
public var currentLayer:Int = 0;
public var zoom:Float = 12.0;
public var panX:Float = 0;
public var panY:Float = 0;
// 描画設定
public var primaryColor:Color = Color.White;
public var onionSkin:Bool = true;
// 履歴
private var undoStack:Array<PixelHistory> = [];
private var activeHistory:PixelHistory = null;
// 入力補間用
private var lastX:Int = -1;
private var lastY:Int = -1;
public function new(w:Int, h:Int) {
width = w; height = h;
frames.push(new Frame(w, h));
}
// --- 描画メイン ---
public function render(canvas:Canvas):Void {
var g = canvas.g2;
// GPU同期
for (f in frames) for (l in f.layers) l.buffer.sync();
g.begin(true, Color.fromBytes(30, 30, 30));
g.imageScaleQuality = ImageScaleQuality.Low;
// キャンバス中央配置の計算
var drawX = (canvas.width - width * zoom) / 2 + panX;
var drawY = (canvas.height - height * zoom) / 2 + panY;
// 背景(チェッカーボード)
drawGrid(g, drawX, drawY);
// オニオンスキン(前のフレームを薄く表示)
if (onionSkin && currentFrame > 0) {
g.opacity = 0.2;
for (l in frames[currentFrame - 1].layers) {
g.drawScaledImage(l.buffer.image, drawX, drawY, width * zoom, height * zoom);
}
}
// メインレイヤー描画
var f = frames[currentFrame];
for (l in f.layers) {
if (!l.visible) continue;
g.opacity = l.opacity;
g.drawScaledImage(l.buffer.image, drawX, drawY, width * zoom, height * zoom);
}
g.end();
}
private function drawGrid(g:Graphics, x:Float, y:Float):Void {
g.color = Color.fromBytes(50, 50, 50);
g.fillRect(x, y, width * zoom, height * zoom);
g.color = Color.fromBytes(70, 70, 70);
var s = zoom;
for (yy in 0...height) {
for (xx in 0...width) {
if ((xx + yy) % 2 == 0) g.fillRect(x + xx * s, y + yy * s, s, s);
}
}
g.color = Color.White;
}
// --- ツール・アルゴリズム ---
public function startStroke():Void {
activeHistory = new PixelHistory(currentLayer, currentFrame);
}
public function endStroke():Void {
if (activeHistory != null && Lambda.count(activeHistory.diff) > 0) {
undoStack.push(activeHistory);
if (undoStack.length > 50) undoStack.shift();
}
activeHistory = null;
lastX = -1; lastY = -1;
}
/**
* ブレゼンハム・アルゴリズムによる補間描画
* 素早いドラッグでも線が途切れない
*/
public function paint(tx:Int, ty:Int):Void {
if (lastX == -1) {
setPixel(tx, ty);
} else {
var dx = Math.abs(tx - lastX);
var dy = Math.abs(ty - lastY);
var sx = (lastX < tx) ? 1 : -1;
var sy = (lastY < ty) ? 1 : -1;
var err = dx - dy;
var x = lastX;
var y = lastY;
while (true) {
setPixel(x, y);
if (x == tx && y == ty) break;
var e2 = 2 * err;
if (e2 > -dy) { err -= dy; x += sx; }
if (e2 < dx) { err += dx; y += sy; }
}
}
lastX = tx; lastY = ty;
}
private function setPixel(x:Int, y:Int):Void {
if (x < 0 || x >= width || y < 0 || y >= height) return;
var l = frames[currentFrame].layers[currentLayer];
var oldC = l.buffer.get(x, y);
var newC:UInt = cast primaryColor;
if (oldC != newC) {
l.buffer.set(x, y, newC);
if (activeHistory != null) {
var idx = y * width + x;
if (!activeHistory.diff.exists(idx)) {
activeHistory.diff.set(idx, {oldC: oldC, newC: newC});
}
}
}
}
/**
* 高速バケツ塗り
*/
public function floodFill(x:Int, y:Int):Void {
var l = frames[currentFrame].layers[currentLayer];
var target = l.buffer.get(x, y);
var fill:UInt = cast primaryColor;
if (target == fill) return;
startStroke();
var queue = [{x: x, y: y}];
while (queue.length > 0) {
var p = queue.shift();
if (p.x < 0 || p.x >= width || p.y < 0 || p.y >= height) continue;
if (l.buffer.get(p.x, p.y) == target) {
setPixel(p.x, p.y);
queue.push({x: p.x + 1, y: p.y});
queue.push({x: p.x - 1, y: p.y});
queue.push({x: p.x, y: p.y + 1});
queue.push({x: p.x, y: p.y - 1});
}
}
endStroke();
}
public function undo():Void {
if (undoStack.length == 0) return;
var h = undoStack.pop();
var buf = frames[h.frameIdx].layers[h.layerIdx].buffer;
for (idx in h.diff.keys()) {
buf.data[idx] = h.diff.get(idx).oldC;
}
@:privateAccess buf.isDirty = true;
}
// --- ユーティリティ ---
public function addFrame():Void {
frames.push(new Frame(width, height));
currentFrame = frames.length - 1;
}
public function screenToLocal(mx:Float, my:Float, canvasW:Int, canvasH:Int):{x:Int, y:Int} {
var drawX = (canvasW - width * zoom) / 2 + panX;
var drawY = (canvasH - height * zoom) / 2 + panY;
return {
x: Math.floor((mx - drawX) / zoom),
y: Math.floor((my - drawY) / zoom)
};
}
}