uiimageview.lua

main 100 lines · 2.7 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UIImageView = extends(UIWidget, "UIImageView")
3
4function UIImageView.create()
5 local imageView = UIImageView.internalCreate()
6 imageView.zoom = 1
7 imageView.minZoom = math.pow(10, -2)
8 imageView.maxZoom = math.pow(10, 2)
9 imageView:setClipping(true)
10 return imageView
11end
12
13function UIImageView:getDefaultZoom()
14 local width = self:getWidth()
15 local height = self:getHeight()
16 local textureWidth = self:getImageTextureWidth()
17 local textureHeight = self:getImageTextureHeight()
18 local zoomX = width / textureWidth
19 local zoomY = height / textureHeight
20 return math.min(zoomX, zoomY)
21end
22
23function UIImageView:getImagePosition(x, y)
24 x = x or self:getWidth() / 2
25 y = y or self:getHeight() / 2
26 local offsetX = self:getImageOffsetX()
27 local offsetY = self:getImageOffsetY()
28 local posX = (x - offsetX) / self.zoom
29 local posY = (y - offsetY) / self.zoom
30 return posX, posY
31end
32
33function UIImageView:setImage(image)
34 self:setImageSource(image)
35 local zoom = self:getDefaultZoom()
36 self:setZoom(zoom)
37 self:center()
38end
39
40function UIImageView:setZoom(zoom, x, y)
41 zoom = math.max(math.min(zoom, self.maxZoom), self.minZoom)
42 local posX, posY = self:getImagePosition(x, y)
43 local textureWidth = self:getImageTextureWidth()
44 local textureHeight = self:getImageTextureHeight()
45 local imageWidth = textureWidth * zoom
46 local imageHeight = textureHeight * zoom
47 self:setImageWidth(imageWidth)
48 self:setImageHeight(imageHeight)
49 self.zoom = zoom
50 self:move(posX, posY, x, y)
51end
52
53function UIImageView:zoomIn(x, y)
54 local zoom = self.zoom * 1.1
55 self:setZoom(zoom, x, y)
56end
57
58function UIImageView:zoomOut(x, y)
59 local zoom = self.zoom / 1.1
60 self:setZoom(zoom, x, y)
61end
62
63function UIImageView:center()
64 self:move(self:getImageTextureWidth() / 2, self:getImageTextureHeight() / 2)
65end
66
67function UIImageView:move(x, y, centerX, centerY)
68 x = math.max(math.min(x, self:getImageTextureWidth()), 0)
69 y = math.max(math.min(y, self:getImageTextureHeight()), 0)
70 local centerX = centerX or self:getWidth() / 2
71 local centerY = centerY or self:getHeight() / 2
72 local offsetX = centerX - x * self.zoom
73 local offsetY = centerY - y * self.zoom
74 self:setImageOffset({x=offsetX, y=offsetY})
75end
76
77function UIImageView:onDragEnter(pos)
78 return true
79end
80
81function UIImageView:onDragMove(pos, moved)
82 local posX, posY = self:getImagePosition()
83 self:move(posX - moved.x / self.zoom, posY - moved.y / self.zoom)
84 return true
85end
86
87function UIImageView:onDragLeave(widget, pos)
88 return true
89end
90
91function UIImageView:onMouseWheel(mousePos, direction)
92 local x = mousePos.x - self:getX()
93 local y = mousePos.y - self:getY()
94 if direction == MouseWheelUp then
95 self:zoomIn(x, y)
96 elseif direction == MouseWheelDown then
97 self:zoomOut(x, y)
98 end
99end
100
Top