Files
armorpaint/Sources/arm/UIView2D.hx
T

158 lines
4.2 KiB
Haxe
Raw Normal View History

2018-02-28 09:53:15 +01:00
package arm;
2018-05-01 14:40:14 +02:00
import zui.*;
2018-10-15 14:36:24 +02:00
@:access(zui.Zui)
2018-05-01 14:40:14 +02:00
class UIView2D extends iron.Trait {
public static var inst:UIView2D;
public var show = false;
public var wx:Int;
public var wy:Int;
public var ww:Int;
2019-02-02 18:49:19 +01:00
public var uvmapCached = false;
public var ui:Zui;
public var hwnd = Id.handle();
2018-10-15 14:36:24 +02:00
var panX = 0.0;
var panY = 0.0;
var panScale = 1.0;
2018-05-01 14:40:14 +02:00
var pipe:kha.graphics4.PipelineState;
2018-10-15 14:36:24 +02:00
var texType = 0;
var uvmap:kha.Image = null;
var uvmapShow = false;
2018-05-01 14:40:14 +02:00
2018-02-28 09:53:15 +01:00
public function new() {
2018-05-01 14:40:14 +02:00
super();
inst = this;
pipe = new kha.graphics4.PipelineState();
2019-02-02 18:49:19 +01:00
pipe.vertexShader = kha.graphics4.VertexShader.fromSource(ConstData.painterVert);
pipe.fragmentShader = kha.graphics4.FragmentShader.fromSource(ConstData.painterFrag);
2018-05-01 14:40:14 +02:00
var vs = new kha.graphics4.VertexStructure();
vs.add("vertexPosition", kha.graphics4.VertexData.Float3);
vs.add("texPosition", kha.graphics4.VertexData.Float2);
vs.add("vertexColor", kha.graphics4.VertexData.Float4);
pipe.inputLayout = [vs];
pipe.compile();
2018-10-15 14:36:24 +02:00
var t = Reflect.copy(arm.App.theme);
t.ELEMENT_H = 18;
t.BUTTON_H = 16;
var scale = armory.data.Config.raw.window_scale;
ui = new Zui({font: arm.App.font, theme: t, color_wheel: arm.App.color_wheel, scaleFactor: scale});
ui.scrollEnabled = false;
2018-05-01 14:40:14 +02:00
notifyOnRender2D(render2D);
2018-10-15 14:36:24 +02:00
notifyOnUpdate(update);
2018-05-01 14:40:14 +02:00
}
function render2D(g:kha.graphics2.Graphics) {
ww = Std.int(iron.App.w());
2018-10-29 15:30:46 +01:00
var lay = UITrait.inst.C.ui_layout;
2018-10-15 14:36:24 +02:00
wx = lay == 0 ? Std.int(iron.App.w()) : UITrait.inst.windowW;
2018-05-01 14:40:14 +02:00
wy = 0;
2019-02-02 18:49:19 +01:00
if (!show ||
arm.App.realw() == 0 ||
arm.App.realh() == 0) return;
2018-10-15 14:36:24 +02:00
2018-10-23 23:41:56 +02:00
if (UITrait.inst.pdirty >= 0) hwnd.redraws = 2; // Paint was active
2018-10-15 14:36:24 +02:00
var tw = iron.App.w() * 0.95;
var tx = iron.App.w() / 2 - tw / 2;
var ty = iron.App.h() / 2 - tw / 2;
tx += panX;
ty += panY;
tw *= panScale;
2018-05-01 14:40:14 +02:00
g.end();
2018-10-15 14:36:24 +02:00
// Cache grid
2018-05-01 14:40:14 +02:00
if (UINodes.inst.grid == null) UINodes.inst.drawGrid();
2018-10-15 14:36:24 +02:00
// Cache UV map
if (uvmap == null && uvmapShow) {
uvmap = kha.Image.createRenderTarget(2048, 2048);
}
if (!uvmapCached && uvmapShow) {
uvmapCached = true;
2018-10-31 01:41:22 +01:00
var mesh = UITrait.inst.paintObject.data.raw;
2018-10-15 14:36:24 +02:00
var texa = mesh.vertex_arrays[2].values;
var inda = mesh.index_arrays[0].values;
uvmap.g2.begin(true, 0x00000000);
uvmap.g2.color = 0xffffffff;
for (i in 0...Std.int(inda.length / 3)) {
2019-01-05 18:06:17 +01:00
var x1 = (texa[inda[i * 3 + 0] * 2 + 0]) / 32767 * uvmap.width;
var x2 = (texa[inda[i * 3 + 1] * 2 + 0]) / 32767 * uvmap.width;
var x3 = (texa[inda[i * 3 + 2] * 2 + 0]) / 32767 * uvmap.width;
var y1 = (texa[inda[i * 3 + 0] * 2 + 1]) / 32767 * uvmap.width;
var y2 = (texa[inda[i * 3 + 1] * 2 + 1]) / 32767 * uvmap.width;
var y3 = (texa[inda[i * 3 + 2] * 2 + 1]) / 32767 * uvmap.width;
2018-10-15 14:36:24 +02:00
uvmap.g2.drawLine(x1, y1, x2, y2);
uvmap.g2.drawLine(x2, y2, x3, y3);
uvmap.g2.drawLine(x3, y3, x1, y1);
}
uvmap.g2.end();
}
ui.begin(g);
if (ui.window(hwnd, wx, wy, ww, iron.App.h())) {
// Grid
ui.g.color = 0xffffffff;
ui.g.drawImage(UINodes.inst.grid, (panX * panScale) % 40 - 40, (panY * panScale) % 40 - 40);
// Texture
ui.g.pipeline = pipe;
2018-11-04 00:27:07 +01:00
// var l = UITrait.inst.selectedLayer;
var l = UITrait.inst.layers[0];
2018-10-15 14:36:24 +02:00
var tex = texType == 0 ? l.texpaint : texType == 1 ? l.texpaint_nor : l.texpaint_pack;
ui.g.drawScaledImage(tex, tx, ty, tw, tw);
ui.g.pipeline = null;
// UV map
if (uvmapShow) {
ui.g.drawScaledImage(uvmap, tx, ty, tw, tw);
}
2018-05-01 14:40:14 +02:00
2018-10-15 14:36:24 +02:00
// Controls
2018-10-26 19:46:05 +02:00
var ew = Std.int(ui.ELEMENT_W());
2018-10-15 14:36:24 +02:00
ui.g.color = ui.t.WINDOW_BG_COL;
ui.g.fillRect(0, 0, ww, 24);
ui.g.color = 0xffffffff;
ui._x = 3;
ui._y = 3;
2018-10-26 19:46:05 +02:00
ui._w = ew;
2018-10-15 14:36:24 +02:00
texType = ui.combo(Id.handle({position: texType}), ["Base", "Normal", "ORM"], "Texture");
2018-10-26 19:46:05 +02:00
ui._x += ew + 3;
2018-10-15 14:36:24 +02:00
ui._y = 3;
uvmapShow = ui.check(Id.handle({selected: uvmapShow}), "UV Map");
2018-10-26 19:46:05 +02:00
ui._x += ew + 3;
2018-10-15 14:36:24 +02:00
ui._y = 3;
}
ui.end();
g.begin(false);
}
function update() {
var m = iron.system.Input.getMouse();
2019-02-02 18:49:19 +01:00
if (!arm.App.uienabled ||
!show ||
m.x + App.x() < wx ||
m.x + App.x() > wx + ww) return;
2018-10-15 14:36:24 +02:00
if (m.down("right")) {
panX += m.movementX;
panY += m.movementY;
}
if (m.wheelDelta != 0) {
panScale -= m.wheelDelta / 10;
if (panScale < 0.1) panScale = 0.1;
if (panScale > 3.0) panScale = 3.0;
}
2018-02-28 09:53:15 +01:00
}
}