-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageviewer.janet
163 lines (157 loc) · 4.39 KB
/
imageviewer.janet
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
(use ./libs/jaylib)
(ffi/context "tinyfd.dll")
(ffi/defbind tinyfd_selectFolderDialog :string
[tilte :string defaultpath :string])
(ffi/defbind tinyfd_utf8toMbcs :string
[instr :string])
#(set-trace-log-level :none)
(init-window 1920 1080 "Image viewer")
(set-target-fps 60)
#(hide-cursor)
(set-window-state :window-resizable)
(var imgpath "E:\\play\\photo\\wp")
(var imgnames @[])
(var bFix false)
(var pos 0)
(var ctexture nil)
(var imgw 0)
(var imgh 0)
(var lastsecond (get-time))
(var offsetx 0)
(var offsety 0)
(var mmovex 0)
(var mmovey 0)
(defn load_image_list []
(var tmppath (tinyfd_selectFolderDialog "select directory" imgpath))
(set imgpath (tinyfd_utf8toMbcs tmppath))
#(pp imgpath)
(array/clear imgnames)
(var tmpnames (os/dir imgpath))
(each fname tmpnames
(when
(or (string/has-suffix? ".jpg" fname) (string/has-suffix? ".JPG" fname) (string/has-suffix? ".png" fname) (string/has-suffix? ".PNG" fname))
(array/push imgnames fname)
)
)
)
(defn switch_image []
(if (not= ctexture nil)
(do
(unload-texture ctexture)
(set ctexture nil)
)
)
(var ciname (string imgpath "\\" (get imgnames pos)))
(set ctexture (load-texture ciname))
(set imgw (get ctexture :width))
(set imgh (get ctexture :height))
(set offsetx 0)
(set offsety 0)
)
# main loop
(while (not (window-should-close))
# mouse drag
(if (and (mouse-button-pressed? :left) (not bFix))
(do
(set mmovex (get-mouse-x))
(set mmovey (get-mouse-y))
)
)
(if (and (mouse-button-released? :left) (not bFix))
(when (and (> mmovex 0) (> mmovey 0))
(set offsetx (+ (- (get-mouse-x) mmovex) offsetx) )
(set offsety (+ (- (get-mouse-y) mmovey) offsety) )
)
)
# key press
(var pkey (get-key-pressed))
(when (= pkey :a)
(when (> pos 0)
(set pos (- pos 1))
(switch_image)
)
)
(when (= pkey :d)
(when (< pos (- (length imgnames) 1))
(set pos (+ pos 1))
(switch_image)
)
)
(when (= pkey :space)
(set bFix (if bFix false true))
)
(when (= pkey :f)
(toggle-fullscreen)
)
(when (= pkey :o)
(load_image_list)
(when (> (length imgnames) 0)
(set pos 0)
(switch_image)
)
)
# mouse wheel
(var wheeloff (get-mouse-wheel-move))
(when (> wheeloff 0)
(var clockdelta (- (get-time) lastsecond))
(set lastsecond (get-time))
(when (> clockdelta 0.2)
(when (> pos 0)
(set pos (- pos 1))
(switch_image)
)
)
)
(when (< wheeloff 0)
(var clockdelta (- (get-time) lastsecond))
(set lastsecond (get-time))
(when (> clockdelta 0.2)
(when (< pos (- (length imgnames) 1))
(set pos (+ pos 1))
(switch_image)
)
)
)
# draw
(begin-drawing)
(clear-background [0 0 0])
(if (> imgw 0)
(do
(var winw (get-screen-width))
(var winh (get-screen-height))
(if (= bFix true)
(do
(var fScale (/ winw imgw))
(var neww winw)
(var newh (* imgh fScale))
(when (> newh winh)
(do
(set fScale (/ winh imgh))
(set newh winh)
(set neww (* imgw fScale))
)
)
(draw-texture-ex ctexture [(math/floor (/ (- winw neww) 2)) (math/floor (/ (- winh newh) 2))] 0 fScale :white)
)
(draw-texture ctexture (math/floor (+ (/ (- winw imgw) 2) offsetx)) (math/floor (+ (/ (- winh imgh) 2) offsety)) :white)
)
)
(do
(draw-text "No image to show" 50 50 20 :blue)
(draw-text "A for prev image" 50 80 20 :blue)
(draw-text "D for next image" 50 110 20 :blue)
(draw-text "O for change folder" 50 140 20 :blue)
(draw-text "F for toggle fullscreen" 50 170 20 :blue)
(draw-text "SPACE for toggle fix image size" 50 200 20 :blue)
)
)
(end-drawing)
)
# release memory
(if (not= ctexture nil)
(do
(unload-texture ctexture)
(set ctexture nil)
)
)
(close-window)