aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lightmapviewer/LightmapViewer.qml
blob: e15e05104671762699d92f8a131b1746e1ce9bfa (plain)
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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Dialogs

import LightmapFile 1.0

ApplicationWindow {
    width: 640
    height: 480
    visible: true
    title: qsTr("Lightmap Viewer")

    id: window

    property string selectedKey: listView.model[0]
    property real imageZoom: 1
    property real imageCenterX: 0
    property real imageCenterY: 0

    function clampImagePosition() {
        // If the image is smaller than the scroll view, center it
        if (image.width <= scrollView.width) {
            imageCenterX = 0
        } else {
            const maxOffsetX = (image.width - scrollView.width) / 2
            imageCenterX = Math.max(-maxOffsetX, Math.min(imageCenterX,
                                                          maxOffsetX))
        }

        if (image.height <= scrollView.height) {
            imageCenterY = 0
        } else {
            const maxOffsetY = (image.height - scrollView.height) / 2
            imageCenterY = Math.max(-maxOffsetY, Math.min(imageCenterY,
                                                          maxOffsetY))
        }
    }

    header: ToolBar {
        RowLayout {
            Button {
                text: qsTr("Open Lightmap")
                onClicked: fileDialog.open()
            }

            Rectangle {
                width: 1
                color: "darkgray"
                Layout.fillHeight: true
                Layout.alignment: Qt.AlignVCenter
            }

            Label {
                text: "Zoom: " + window.imageZoom.toFixed(1)
            }

            Rectangle {
                width: 1
                color: "darkgray"
                Layout.fillHeight: true
                Layout.alignment: Qt.AlignVCenter
            }

            Switch {
                id: alphaSwitch
                padding: 0
                checked: true
                text: "Alpha"
            }

            Rectangle {
                width: 1
                color: "darkgray"
                Layout.fillHeight: true
                Layout.alignment: Qt.AlignVCenter
            }

            Text {
                text: "Path: " + LightmapFile.source
            }
        }
    }

    FileDialog {
        id: fileDialog
        onAccepted: {
            LightmapFile.source = selectedFile
            LightmapFile.loadData()
        }
    }

    Shortcut {
        sequences: [StandardKey.Open]
        onActivated: {
            fileDialog.open()
        }
    }

    SplitView {
        anchors.fill: parent
        orientation: Qt.Horizontal

        focus: true
        Keys.onPressed: event => {
                            if (event.key === Qt.Key_Up) {
                                listView.currentIndex = Math.max(
                                    0, listView.currentIndex - 1)
                                selectedKey = listView.model[listView.currentIndex]
                            } else if (event.key === Qt.Key_Down) {
                                listView.currentIndex = Math.min(
                                    listView.model.length - 1,
                                    listView.currentIndex + 1)
                                selectedKey = listView.model[listView.currentIndex]
                            }
                            clampImagePosition()
                        }
        ListView {
            id: listView
            SplitView.preferredWidth: 100
            SplitView.minimumWidth: 50
            model: LightmapFile.dataList
            delegate: Text {
                text: modelData
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        listView.currentIndex = index
                        selectedKey = modelData // Select this item
                    }
                }
            }
            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 1
            }
        }

        Rectangle {
            id: scrollView
            clip: true
            color: "black"

            property real lastMouseX: 0
            property real lastMouseY: 0

            onWidthChanged: {
                clampImagePosition()
            }
            onHeightChanged: {
                clampImagePosition()
            }

            MouseArea {
                id: mouseArea
                property bool dragging: false
                anchors.fill: parent
                onPressed: mouse => {
                               scrollView.lastMouseX = mouse.x
                               scrollView.lastMouseY = mouse.y
                               dragging = true
                           }
                onReleased: mouse => {
                                dragging = false
                            }

                onPositionChanged: mouse => {
                                       var dx = mouse.x - scrollView.lastMouseX
                                       var dy = mouse.y - scrollView.lastMouseY

                                       scrollView.lastMouseX = mouse.x
                                       scrollView.lastMouseY = mouse.y

                                       imageCenterX += dx
                                       imageCenterY += dy

                                       clampImagePosition()
                                   }
                cursorShape: mouseArea.dragging ? Qt.ClosedHandCursor : Qt.ArrowCursor

                onWheel: event => {
                             const oldZoom = imageZoom
                             const zoomDelta = event.angleDelta.y / 256
                             const newZoom = Math.max(
                                 1, Math.min(32, oldZoom + zoomDelta))

                             if (newZoom === oldZoom)
                             return

                             // Adjust center offset so the same point remains at the center
                             const scaleFactor = newZoom / oldZoom
                             imageCenterX *= scaleFactor
                             imageCenterY *= scaleFactor

                             imageZoom = newZoom
                             clampImagePosition()

                             event.accepted = true
                         }
            }

            Image {
                id: baseGrid
                anchors.fill: scrollView
                source: "grid.png"
                fillMode: Image.Tile
                opacity: 0.75
            }

            Rectangle {
                width: image.width + (border.width * 2)
                height: image.height + (border.width * 2)
                x: image.x - border.width
                y: image.y - border.width
                color: "white" // This is the border color

                border.width: 0
                border.color: "white"
                opacity: 0.25
            }

            Image {
                id: image
                x: Math.round(parent.width / 2 - width / 2) + imageCenterX
                y: Math.round(parent.height / 2 - height / 2) + imageCenterY
                source: `image://lightmaps/key=${selectedKey}&file=${LightmapFile.source}&alpha=${alphaSwitch.checked}`
                onWidthChanged: clampImagePosition()
                onHeightChanged: clampImagePosition()
                fillMode: Image.PreserveAspectFit
                smooth: false
                antialiasing: false

                // Let the image scale visibly
                width: sourceSize.width * imageZoom
                height: sourceSize.height * imageZoom
            }
        }
    }

    DropArea {
        id: dropArea
        anchors.fill: parent
        onEntered: (drag) => {
            drag.accept(Qt.LinkAction)
        }
            // Just take first url if several
        onDropped: (drop) => {
            if (drop.hasUrls) {
                LightmapFile.source = drop.urls[0]
                LightmapFile.loadData()
            }
        }
    }
}