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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick3D
import QtQuick.Dialogs
import Example
RowLayout {
id: root
property Texture targetTexture: null
property bool stampMode: false
required property url defaultTexture
property url stampSource: ""
property alias defaultClearColor: drawer.clearColor
property bool envMapMode: false
GroupBox {
title: "Image Source Mode"
ColumnLayout {
RadioButton {
id: noTextureChoice
text: "None"
checked: true
onCheckedChanged: {
targetTexture = null
}
}
RadioButton {
id: selectTextureChoice
text: "Texture"
checked: false
onCheckedChanged: targetTexture = selectedTexture
}
RadioButton {
id: loadImageChoice
text: "Load Image"
onCheckedChanged: targetTexture = loadTextureTexture
}
RadioButton {
id: drawerChoice
text: "Draw Texture"
onCheckedChanged: targetTexture = drawerTexture
}
Label {
visible: uvScale.visible
Layout.fillWidth: true
text: "UV Scale (" + uvScale.value.toFixed(2) + ")"
}
Slider {
id: uvScale
visible: !noTextureChoice.checked
from: 0.0
to: 50.0
value: 1.0
onValueChanged: {
updateUVScale()
}
function updateUVScale(){
if ( targetTexture ) {
targetTexture.scaleU = value
targetTexture.scaleV = value
}
}
Connections {
target: root
function onTargetTextureChanged(targetTexture){
uvScale.updateUVScale()
}
}
}
}
}
Item {
visible: selectTextureChoice.checked
implicitWidth: 256
implicitHeight: 256
Image {
id: previewImage
anchors.fill: parent
sourceSize.width: width
sourceSize.height: height
fillMode: Image.PreserveAspectFit
source: root.defaultTexture
Texture {
id: selectedTexture
source: root.defaultTexture
mappingMode: root.envMapMode ? Texture.Environment : Texture.UV
}
}
}
Rectangle {
id: loadTextureFrame
implicitWidth: 256
implicitHeight: 256
color: "transparent"
border.color: "black"
visible: loadImageChoice.checked
property url textureSource: ""
Text {
anchors.centerIn: parent
text: "[Load Image]"
}
Image {
anchors.fill: parent
sourceSize.width: width
sourceSize.height: height
fillMode: Image.PreserveAspectFit
visible: loadTextureFrame.textureSource !== null
source: loadTextureFrame.textureSource
}
MouseArea {
anchors.fill: parent
onClicked: {
textureSourceDialog.open()
}
}
Texture {
id: loadTextureTexture
source: loadTextureFrame.textureSource
mappingMode: root.envMapMode ? Texture.Environment : Texture.UV
}
ImageHelper {
id: imageHelper
}
FileDialog {
id: textureSourceDialog
title: "Open an Image File"
nameFilters: [ imageHelper.getSupportedImageFormatsFilter()]
onAccepted: {
if (textureSourceDialog.selectedFile !== null) {
loadTextureFrame.textureSource = textureSourceDialog.selectedFile
}
}
}
}
ColumnLayout {
visible: drawerChoice.checked
Rectangle {
implicitWidth: 260
implicitHeight: 260
color: "transparent"
border.color: "black"
Canvas {
id: drawer
width: 256
height: 256
x: 2
y: 2
property color penColor: "blue"
property real penWidth: penWidthSlider.value
property bool needsClear: true
property color clearColor: "white"
property var commands: []
property var stampCommands: []
property bool stampMode: root.stampMode
//property point prevPoint : Qt.point(0, 0)
onPaint: {
let ctx = getContext('2d');
if (needsClear) {
ctx.fillStyle = Qt.rgba(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
ctx.fillRect(0, 0, width, height)
needsClear = false;
}
if (!stampMode) {
ctx.strokeStyle = Qt.rgba(penColor.r, penColor.g, penColor.b, penColor.a)
ctx.lineCap = "round"
ctx.lineWidth = penWidth;
for (let i = 0; i < commands.length; ++i) {
let command = commands[i];
ctx.beginPath()
ctx.moveTo(command.start.x, command.start.y);
ctx.lineTo(command.end.x, command.end.y);
ctx.stroke();
}
commands = [];
} else {
for (let i = 0; i < stampCommands.length; ++i) {
let stampCommand = stampCommands[i]
// get offset
let dX = stampCommand.x - stampcursor.width * 0.5
let dY = stampCommand.y - stampcursor.height * 0.5
ctx.drawImage(stampcursor, dX, dY)
}
stampCommands = [];
}
}
}
MouseArea {
id: mouseArea
anchors.fill: drawer
enabled: drawerChoice.checked
hoverEnabled: true
//acceptedButtons: Qt.LeftButton
property bool isDrawing: false
property var lastPosition: Qt.point(0, 0)
preventStealing: true
clip: true
Item {
id: cursor
Rectangle {
anchors.centerIn: parent
visible: !root.stampMode
width: drawer.penWidth
height: drawer.penWidth
radius: width * 0.5
color: drawer.penColor
}
Image {
id: stampcursor
anchors.centerIn: parent
visible: root.stampMode
source: root.stampSource
}
}
onEntered: cursor.visible = true
onExited: cursor.visible = false
onPressed: (mouse)=> {
if (mouse.button === Qt.LeftButton && !root.stampMode) {
lastPosition = Qt.point(mouse.x, mouse.y)
isDrawing = true
}
}
onPositionChanged: (mouse)=> {
if (isDrawing) {
let pos = Qt.point(mouse.x, mouse.y);
let command = {"start": lastPosition, "end": pos}
drawer.commands.push(command)
lastPosition = pos;
drawer.requestPaint();
}
cursor.x = mouse.x
cursor.y = mouse.y
}
onReleased: (mouse)=> {
if (mouse.button === Qt.LeftButton && isDrawing) {
let pos = Qt.point(mouse.x, mouse.y);
let command = {"start": lastPosition, "end": pos}
drawer.commands.push(command)
isDrawing = false;
drawer.requestPaint();
} else if (root.stampMode) {
drawer.stampCommands.push(Qt.point(mouse.x, mouse.y));
drawer.requestPaint();
}
}
}
}
RowLayout {
visible: !root.stampMode
spacing: 0
Rectangle {
id: whiteBrush
implicitWidth: 25
implicitHeight: 25
color: "white"
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
drawer.penColor = parent.color;
}
}
}
Rectangle {
id: blackBrush
implicitWidth: 25
implicitHeight: 25
color: "black"
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
drawer.penColor = parent.color;
}
}
}
Rectangle {
id: redBrush
implicitWidth: 25
implicitHeight: 25
color: "red"
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
drawer.penColor = parent.color;
}
}
}
Rectangle {
id: greenBrush
implicitWidth: 25
implicitHeight: 25
color: "green"
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
drawer.penColor = parent.color;
}
}
}
Rectangle {
id: blueBrush
implicitWidth: 25
implicitHeight: 25
color: "blue"
border.color: "black"
MouseArea {
anchors.fill: parent
onClicked: {
drawer.penColor = parent.color;
}
}
}
Label {
text: " "
}
Button {
text: "Clear"
onClicked: {
drawer.needsClear = true
drawer.requestPaint()
}
}
}
RowLayout {
visible: !root.stampMode
Slider {
id: penWidthSlider
from: 1
to: 50
value: 5
}
Label {
Layout.fillWidth: true
text: "Pen Width"
}
}
}
Texture {
id: drawerTexture
sourceItem: drawerChoice.checked ? drawer : null
mappingMode: root.envMapMode ? Texture.Environment : Texture.UV
}
}
|